Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: add Postgres CREATE MATERIALIZED VIEW statement (#14200)

rebelice9c6117df19d3
diff --git a/backend/plugin/parser/sql/ast/create_materialized_view_stmt.go b/backend/plugin/parser/sql/ast/create_materialized_view_stmt.go
new file mode 100644
index 0000000000..e4fe1c2fa3
--- /dev/null
+++ b/backend/plugin/parser/sql/ast/create_materialized_view_stmt.go
@@ -0,0 +1,19 @@
+package ast
+
+import pgquery "github.com/pganalyze/pg_query_go/v5"
+
+type CreateMaterializedViewStmt struct {
+ ddl
+
+ Name *TableDef
+
+ originalNode *pgquery.Node_CreateTableAsStmt
+}
+
+func (cv *CreateMaterializedViewStmt) SetOriginalNode(node *pgquery.Node_CreateTableAsStmt) {
+ cv.originalNode = node
+}
+
+func (cv *CreateMaterializedViewStmt) GetOriginalNode() *pgquery.Node_CreateTableAsStmt {
+ return cv.originalNode
+}
diff --git a/backend/plugin/parser/sql/ast/table_def.go b/backend/plugin/parser/sql/ast/table_def.go
index 04db7021dd..5f2651e777 100644
--- a/backend/plugin/parser/sql/ast/table_def.go
+++ b/backend/plugin/parser/sql/ast/table_def.go
@@ -10,6 +10,8 @@ const (
TableTypeBaseTable
// TableTypeView is the type for view.
TableTypeView
+ // TableTypeMaterializedView is the type for materialized view.
+ TableTypeMaterializedView
)
// TableDef is the strcut for table.
diff --git a/backend/plugin/parser/sql/engine/pg/convert.go b/backend/plugin/parser/sql/engine/pg/convert.go
index 7c7174d392..a02460af51 100644
--- a/backend/plugin/parser/sql/engine/pg/convert.go
+++ b/backend/plugin/parser/sql/engine/pg/convert.go
@@ -881,6 +881,15 @@ func convert(node *pgquery.Node, statement base.SingleSQL) (res ast.Node, err er
Name: in.VariableShowStmt.GetName(),
}
return variableShowStmt, nil
+ case *pgquery.Node_CreateTableAsStmt:
+ if in.CreateTableAsStmt.Objtype != pgquery.ObjectType_OBJECT_MATVIEW {
+ return &ast.UnconvertedStmt{}, nil
+ }
+ materializedViewStmt := &ast.CreateMaterializedViewStmt{
+ Name: convertRangeVarToTableName(in.CreateTableAsStmt.Into.Rel, ast.TableTypeMaterializedView),
+ }
+ materializedViewStmt.SetOriginalNode(in)
+ return materializedViewStmt, nil
default:
return &ast.UnconvertedStmt{}, nil
}
diff --git a/backend/plugin/parser/sql/engine/pg/convert_test.go b/backend/plugin/parser/sql/engine/pg/convert_test.go
index 55e6fd4d4d..c357fae1ec 100644
--- a/backend/plugin/parser/sql/engine/pg/convert_test.go
+++ b/backend/plugin/parser/sql/engine/pg/convert_test.go
@@ -64,6 +64,30 @@ func newExpression(expression ast.ExpressionNode, text string) ast.ExpressionNod
return expression
}
+func TestPGConvertCreateMaterializedViewStmt(t *testing.T) {
+ tests := []testData{
+ {
+ stmt: `CREATE MATERIALIZED VIEW tech_book(a, b) AS SELECT * FROM book`,
+ want: []ast.Node{
+ &ast.CreateMaterializedViewStmt{
+ Name: &ast.TableDef{
+ Type: ast.TableTypeMaterializedView,
+ Name: "tech_book",
+ },
+ },
+ },
+ statementList: []base.SingleSQL{
+ {
+ Text: `CREATE MATERIALIZED VIEW tech_book(a, b) AS SELECT * FROM book`,
+ LastLine: 1,
+ },
+ },
+ },
+ }
+
+ runTests(t, tests)
+}
+
func TestPGVariableShowStmt(t *testing.T) {
tests := []testData{
{