Skip to content

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

fix: handle GENERATED ALWAYS AS constraint for pg (#8662)

zll600d7891825e71c
diff --git a/backend/plugin/parser/sql/ast/constraint_def.go b/backend/plugin/parser/sql/ast/constraint_def.go
index 432e3faf30..fea47f97ab 100644
--- a/backend/plugin/parser/sql/ast/constraint_def.go
+++ b/backend/plugin/parser/sql/ast/constraint_def.go
@@ -26,6 +26,8 @@ const (
ConstraintTypeExclusion
// ConstraintTypeDefault is the default constraint.
ConstraintTypeDefault
+ // ConstraintTypeGenerated is the generated constraint.
+ ConstraintTypeGenerated
)
// ConstraintDef is struct for constraint definition.
diff --git a/backend/plugin/parser/sql/engine/pg/convert.go b/backend/plugin/parser/sql/engine/pg/convert.go
index f30c4a2b7f..ab9e0b62f8 100644
--- a/backend/plugin/parser/sql/engine/pg/convert.go
+++ b/backend/plugin/parser/sql/engine/pg/convert.go
@@ -1504,7 +1504,7 @@ func convertConstraint(in *pgquery.Node_Constraint) (*ast.ConstraintDef, error)
}
case ast.ConstraintTypePrimaryUsingIndex, ast.ConstraintTypeUniqueUsingIndex:
cons.IndexName = in.Constraint.Indexname
- case ast.ConstraintTypeCheck, ast.ConstraintTypeDefault:
+ case ast.ConstraintTypeCheck, ast.ConstraintTypeDefault, ast.ConstraintTypeGenerated:
expression, _, _, err := convertExpressionNode(in.Constraint.RawExpr)
if err != nil {
return nil, err
@@ -1594,6 +1594,8 @@ func convertConstraintType(in pgquery.ConstrType, usingIndex bool) ast.Constrain
return ast.ConstraintTypeDefault
case pgquery.ConstrType_CONSTR_EXCLUSION:
return ast.ConstraintTypeExclusion
+ case pgquery.ConstrType_CONSTR_GENERATED:
+ return ast.ConstraintTypeGenerated
}
return ast.ConstraintTypeUndefined
}
diff --git a/backend/plugin/parser/sql/engine/pg/deparse.go b/backend/plugin/parser/sql/engine/pg/deparse.go
index 751eb8e0c0..8f0636bb28 100644
--- a/backend/plugin/parser/sql/engine/pg/deparse.go
+++ b/backend/plugin/parser/sql/engine/pg/deparse.go
@@ -1053,6 +1053,10 @@ func deparseColumnConstraint(_ DeparseContext, in *ast.ConstraintDef, buf *strin
if _, err := buf.WriteString(in.Expression.Text()); err != nil {
return err
}
+ case ast.ConstraintTypeGenerated:
+ if _, err := buf.WriteString(fmt.Sprintf("GENERATED ALWAYS AS (%s) STORED", in.Expression.Text())); err != nil {
+ return err
+ }
default:
return errors.Errorf("failed to deparse column constraint: not support %d", in.Type)
}
diff --git a/backend/plugin/parser/sql/engine/pg/test-data/test_create_table_data.yaml b/backend/plugin/parser/sql/engine/pg/test-data/test_create_table_data.yaml
index 42adca9bea..1480c2da08 100644
--- a/backend/plugin/parser/sql/engine/pg/test-data/test_create_table_data.yaml
+++ b/backend/plugin/parser/sql/engine/pg/test-data/test_create_table_data.yaml
@@ -81,3 +81,31 @@
CONSTRAINT "uk1" UNIQUE ("a", "b")
)
PARTITION BY RANGE ("a", "b", (c + 1));
+
+- stmt: |-
+ CREATE TABLE t (
+ a numeric,
+ b numeric,
+ c numeric GENERATED ALWAYS AS (a * b) STORED
+ );
+ want: |-
+ CREATE TABLE "t" (
+ "a" numeric,
+ "b" numeric,
+ "c" numeric GENERATED ALWAYS AS (a * b) STORED
+ );
+
+- stmt: |-
+ CREATE TABLE public.t (
+ action text NOT NULL,
+ action_date timestamp NOT NULL,
+ number smallint GENERATED ALWAYS AS ((date_part('week'::text, ((action_date)::date + '1 day'::interval)))::smallint) STORED,
+ year smallint GENERATED ALWAYS AS ((date_part('year'::text, date_trunc('week'::text, action_date)))::smallint) STORED
+ );
+ want: |-
+ CREATE TABLE "public"."t" (
+ "action" text NOT NULL,
+ "action_date" timestamp NOT NULL,
+ "number" smallint GENERATED ALWAYS AS (date_part('week'::text, action_date::date + '1 day'::interval)::smallint) STORED,
+ "year" smallint GENERATED ALWAYS AS (date_part('year'::text, date_trunc('week'::text, action_date))::smallint) STORED
+ );