Skip to content

Commit

Permalink
feat(postgresql): Add materialized view support (#1509)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludusrusso authored Mar 29, 2022
1 parent 3d20d04 commit 367889d
Show file tree
Hide file tree
Showing 17 changed files with 246 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"
}
]
}
29 changes: 29 additions & 0 deletions internal/endtoend/testdata/create_table_as/postgresql/go/db.go

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

13 changes: 13 additions & 0 deletions internal/endtoend/testdata/create_table_as/postgresql/go/models.go

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 second_table;

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

CREATE TABLE second_table AS SELECT * FROM foo;
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/create_table_as/postgresql/sqlc.json
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.

3 changes: 2 additions & 1 deletion internal/engine/postgresql/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -1264,13 +1264,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
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:
err = c.createTableAs(n, colGen)

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

Expand Down
42 changes: 42 additions & 0 deletions internal/sql/catalog/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,45 @@ 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)

return nil
}

0 comments on commit 367889d

Please sign in to comment.