Skip to content

Commit

Permalink
add materialized view support
Browse files Browse the repository at this point in the history
  • Loading branch information
ludusrusso committed Mar 24, 2022
1 parent 0897308 commit af92724
Show file tree
Hide file tree
Showing 12 changed files with 185 additions and 1 deletion.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- name: GetFirst :many
SELECT * FROM mat_first_view;

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE TABLE foo (val text not null);

CREATE MATERIALIZED VIEW mat_first_view AS SELECT * FROM foo;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"engine": "postgresql",
"path": "go",
"name": "querytest",
"schema": "schema.sql",
"queries": "query.sql"
}
]
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion internal/engine/postgresql/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -1237,13 +1237,14 @@ func convertCreateTableAsStmt(n *pg.CreateTableAsStmt) *ast.CreateTableAsStmt {
if n == nil {
return nil
}
return &ast.CreateTableAsStmt{
res := &ast.CreateTableAsStmt{
Query: convertNode(n.Query),
Into: convertIntoClause(n.Into),
Relkind: ast.ObjectType(n.Relkind),
IsSelectInto: n.IsSelectInto,
IfNotExists: n.IfNotExists,
}
return res
}

func convertCreateTableSpaceStmt(n *pg.CreateTableSpaceStmt) *ast.CreateTableSpaceStmt {
Expand Down Expand Up @@ -2842,6 +2843,20 @@ func convertViewStmt(n *pg.ViewStmt) *ast.ViewStmt {
}
}

// func convertMatViewStmt(n *pg.ViewStmt) *ast.MatViewStmt {
// if n == nil {
// return nil
// }
// return &ast.MatViewStmt{
// MatView: convertRangeVar(n.View),
// Aliases: convertSlice(n.Aliases),
// Query: convertNode(n.Query),
// Replace: n.Replace,
// Options: convertSlice(n.Options),
// WithCheckOption: ast.ViewCheckOption(n.WithCheckOption),
// }
// }

func convertWindowClause(n *pg.WindowClause) *ast.WindowClause {
if n == nil {
return nil
Expand Down
14 changes: 14 additions & 0 deletions internal/sql/ast/matview_stmt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ast

type MatViewStmt struct {
MatView *RangeVar
Aliases *List
Query Node
Replace bool
Options *List
WithCheckOption ViewCheckOption
}

func (n *MatViewStmt) Pos() int {
return 0
}
4 changes: 4 additions & 0 deletions internal/sql/catalog/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ func (c *Catalog) Update(stmt ast.Statement, colGen columnGenerator) error {
}
var err error
switch n := stmt.Raw.Stmt.(type) {

case *ast.AlterTableStmt:
err = c.alterTable(n)

Expand Down Expand Up @@ -330,6 +331,9 @@ func (c *Catalog) Update(stmt ast.Statement, colGen columnGenerator) error {
case *ast.CreateTableStmt:
err = c.createTable(n)

case *ast.CreateTableAsStmt:
return c.createTableAs(n, colGen)

case *ast.ViewStmt:
err = c.createView(n, colGen)

Expand Down
48 changes: 48 additions & 0 deletions internal/sql/catalog/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,51 @@ func (c *Catalog) renameTable(stmt *ast.RenameTableStmt) error {
}
return nil
}

func (c *Catalog) createTableAs(stmt *ast.CreateTableAsStmt, colGen columnGenerator) error {
cols, err := colGen.OutputColumns(stmt.Query)
if err != nil {
return err
}

catName := ""
if stmt.Into.Rel.Catalogname != nil {
catName = *stmt.Into.Rel.Catalogname
}
schemaName := ""
if stmt.Into.Rel.Schemaname != nil {
schemaName = *stmt.Into.Rel.Schemaname
}

tbl := Table{
Rel: &ast.TableName{
Catalog: catName,
Schema: schemaName,
Name: *stmt.Into.Rel.Relname,
},
Columns: cols,
}

ns := tbl.Rel.Schema
if ns == "" {
ns = c.DefaultSchema
}
schema, err := c.getSchema(ns)
if err != nil {
return err
}
_, _, err = schema.getTable(tbl.Rel)
if err == nil {
return sqlerr.RelationExists(tbl.Rel.Name)
}

schema.Tables = append(schema.Tables, &tbl)

// if stmt.Replace && err == nil {
// schema.Tables[existingIdx] = &tbl
// } else {
// schema.Tables = append(schema.Tables, &tbl)
// }

return nil
}

0 comments on commit af92724

Please sign in to comment.