From af92724fd4ab95e187b460ea2fdc840092ad8acc Mon Sep 17 00:00:00 2001 From: Ludovico Russo Date: Thu, 24 Mar 2022 17:51:14 +0100 Subject: [PATCH] add materialized view support --- .../postgresql/go/db.go | 29 +++++++++++ .../postgresql/go/models.go | 13 +++++ .../postgresql/go/query.sql.go | 35 ++++++++++++++ .../postgresql/query.sql | 3 ++ .../postgresql/schema.sql | 3 ++ .../postgresql/sqlc.json | 12 +++++ .../postgresql/pgx/go/models.go | 4 ++ .../postgresql/stdlib/go/models.go | 4 ++ internal/engine/postgresql/convert.go | 17 ++++++- internal/sql/ast/matview_stmt.go | 14 ++++++ internal/sql/catalog/catalog.go | 4 ++ internal/sql/catalog/table.go | 48 +++++++++++++++++++ 12 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 internal/endtoend/testdata/create_materialized_view/postgresql/go/db.go create mode 100644 internal/endtoend/testdata/create_materialized_view/postgresql/go/models.go create mode 100644 internal/endtoend/testdata/create_materialized_view/postgresql/go/query.sql.go create mode 100644 internal/endtoend/testdata/create_materialized_view/postgresql/query.sql create mode 100644 internal/endtoend/testdata/create_materialized_view/postgresql/schema.sql create mode 100644 internal/endtoend/testdata/create_materialized_view/postgresql/sqlc.json create mode 100644 internal/sql/ast/matview_stmt.go diff --git a/internal/endtoend/testdata/create_materialized_view/postgresql/go/db.go b/internal/endtoend/testdata/create_materialized_view/postgresql/go/db.go new file mode 100644 index 0000000000..6a99519302 --- /dev/null +++ b/internal/endtoend/testdata/create_materialized_view/postgresql/go/db.go @@ -0,0 +1,29 @@ +// Code generated by sqlc. DO NOT EDIT. + +package querytest + +import ( + "context" + "database/sql" +) + +type DBTX interface { + ExecContext(context.Context, string, ...interface{}) (sql.Result, error) + PrepareContext(context.Context, string) (*sql.Stmt, error) + QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error) + QueryRowContext(context.Context, string, ...interface{}) *sql.Row +} + +func New(db DBTX) *Queries { + return &Queries{db: db} +} + +type Queries struct { + db DBTX +} + +func (q *Queries) WithTx(tx *sql.Tx) *Queries { + return &Queries{ + db: tx, + } +} diff --git a/internal/endtoend/testdata/create_materialized_view/postgresql/go/models.go b/internal/endtoend/testdata/create_materialized_view/postgresql/go/models.go new file mode 100644 index 0000000000..36a6973d88 --- /dev/null +++ b/internal/endtoend/testdata/create_materialized_view/postgresql/go/models.go @@ -0,0 +1,13 @@ +// Code generated by sqlc. DO NOT EDIT. + +package querytest + +import () + +type Foo struct { + Val string +} + +type MatFirstView struct { + Val string +} diff --git a/internal/endtoend/testdata/create_materialized_view/postgresql/go/query.sql.go b/internal/endtoend/testdata/create_materialized_view/postgresql/go/query.sql.go new file mode 100644 index 0000000000..4c66199a67 --- /dev/null +++ b/internal/endtoend/testdata/create_materialized_view/postgresql/go/query.sql.go @@ -0,0 +1,35 @@ +// Code generated by sqlc. DO NOT EDIT. +// source: query.sql + +package querytest + +import ( + "context" +) + +const getFirst = `-- name: GetFirst :many +SELECT val FROM mat_first_view +` + +func (q *Queries) GetFirst(ctx context.Context) ([]string, error) { + rows, err := q.db.QueryContext(ctx, getFirst) + if err != nil { + return nil, err + } + defer rows.Close() + var items []string + for rows.Next() { + var val string + if err := rows.Scan(&val); err != nil { + return nil, err + } + items = append(items, val) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/internal/endtoend/testdata/create_materialized_view/postgresql/query.sql b/internal/endtoend/testdata/create_materialized_view/postgresql/query.sql new file mode 100644 index 0000000000..0c86bfde56 --- /dev/null +++ b/internal/endtoend/testdata/create_materialized_view/postgresql/query.sql @@ -0,0 +1,3 @@ +-- name: GetFirst :many +SELECT * FROM mat_first_view; + diff --git a/internal/endtoend/testdata/create_materialized_view/postgresql/schema.sql b/internal/endtoend/testdata/create_materialized_view/postgresql/schema.sql new file mode 100644 index 0000000000..fc0c213b6b --- /dev/null +++ b/internal/endtoend/testdata/create_materialized_view/postgresql/schema.sql @@ -0,0 +1,3 @@ +CREATE TABLE foo (val text not null); + +CREATE MATERIALIZED VIEW mat_first_view AS SELECT * FROM foo; diff --git a/internal/endtoend/testdata/create_materialized_view/postgresql/sqlc.json b/internal/endtoend/testdata/create_materialized_view/postgresql/sqlc.json new file mode 100644 index 0000000000..cd518671ac --- /dev/null +++ b/internal/endtoend/testdata/create_materialized_view/postgresql/sqlc.json @@ -0,0 +1,12 @@ +{ + "version": "1", + "packages": [ + { + "engine": "postgresql", + "path": "go", + "name": "querytest", + "schema": "schema.sql", + "queries": "query.sql" + } + ] +} diff --git a/internal/endtoend/testdata/materialized_views/postgresql/pgx/go/models.go b/internal/endtoend/testdata/materialized_views/postgresql/pgx/go/models.go index 41ff924fa5..553b3c6800 100644 --- a/internal/endtoend/testdata/materialized_views/postgresql/pgx/go/models.go +++ b/internal/endtoend/testdata/materialized_views/postgresql/pgx/go/models.go @@ -12,3 +12,7 @@ type Author struct { Bio sql.NullString Gender sql.NullInt32 } + +type AuthorsName struct { + Name string +} diff --git a/internal/endtoend/testdata/materialized_views/postgresql/stdlib/go/models.go b/internal/endtoend/testdata/materialized_views/postgresql/stdlib/go/models.go index 41ff924fa5..553b3c6800 100644 --- a/internal/endtoend/testdata/materialized_views/postgresql/stdlib/go/models.go +++ b/internal/endtoend/testdata/materialized_views/postgresql/stdlib/go/models.go @@ -12,3 +12,7 @@ type Author struct { Bio sql.NullString Gender sql.NullInt32 } + +type AuthorsName struct { + Name string +} diff --git a/internal/engine/postgresql/convert.go b/internal/engine/postgresql/convert.go index c6e8976b66..f91bd43841 100644 --- a/internal/engine/postgresql/convert.go +++ b/internal/engine/postgresql/convert.go @@ -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 { @@ -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 diff --git a/internal/sql/ast/matview_stmt.go b/internal/sql/ast/matview_stmt.go new file mode 100644 index 0000000000..566d5a2dd2 --- /dev/null +++ b/internal/sql/ast/matview_stmt.go @@ -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 +} diff --git a/internal/sql/catalog/catalog.go b/internal/sql/catalog/catalog.go index e7964cf16f..a14559da94 100644 --- a/internal/sql/catalog/catalog.go +++ b/internal/sql/catalog/catalog.go @@ -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) @@ -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) diff --git a/internal/sql/catalog/table.go b/internal/sql/catalog/table.go index 92d9156b88..9052fbdcd5 100644 --- a/internal/sql/catalog/table.go +++ b/internal/sql/catalog/table.go @@ -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 +}