Skip to content

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

feat(parser): support DEFAULT constraint (#3276)

rebelice44e0bb833398
diff --git a/go.mod b/go.mod
index 62dcae6984..0f53402e02 100644
--- a/go.mod
+++ b/go.mod
@@ -169,3 +169,5 @@ replace github.com/github/gh-ost => github.com/bytebase/gh-ost v1.1.3-0.20220728
replace github.com/pingcap/tidb => github.com/bytebase/tidb v0.0.0-20221028035959-5d3b71eadf24
replace github.com/pingcap/tidb/parser => github.com/bytebase/tidb/parser v0.0.0-20221028035959-5d3b71eadf24
+
+replace github.com/pganalyze/pg_query_go/v2 => github.com/bytebase/pg_query_go/v2 v2.0.0-20221102094723-0d12de86f87c
diff --git a/go.sum b/go.sum
index 4411760dcb..5c147f3e23 100644
--- a/go.sum
+++ b/go.sum
@@ -145,6 +145,8 @@ github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dR
github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/bytebase/gh-ost v1.1.3-0.20220728080340-11d9c9027651 h1:NpoTuRY6Ckj37zTlEax/UTfvHCOdanI5/5dVCarOD18=
github.com/bytebase/gh-ost v1.1.3-0.20220728080340-11d9c9027651/go.mod h1:/HsnX0+34LXVz3MxtZoW15GiwEOXz/PrEhutjb3icfE=
+github.com/bytebase/pg_query_go/v2 v2.0.0-20221102094723-0d12de86f87c h1:ySQRx63mU0OzCJ30nPFQf5BvbHCqvpCW4gizDLgN87I=
+github.com/bytebase/pg_query_go/v2 v2.0.0-20221102094723-0d12de86f87c/go.mod h1:XAxmVqz1tEGqizcQ3YSdN90vCOHBWjJi8URL1er5+cA=
github.com/bytebase/tidb v0.0.0-20221028035959-5d3b71eadf24 h1:XR25hLtR8TtpW/4mgP7EcOlzi8ewNY5EVan97WzDQxc=
github.com/bytebase/tidb v0.0.0-20221028035959-5d3b71eadf24/go.mod h1:xrVXE+0V54QRGgrD9o+tuZmBDDx8vJpQgrPtRQvodWo=
github.com/bytebase/tidb/parser v0.0.0-20221028035959-5d3b71eadf24 h1:DZPxIt2aZJ6L/gh8tH9jspTTIufBt7Wvgae1T7TmXAg=
@@ -517,8 +519,6 @@ github.com/otiai10/mint v1.3.3/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH
github.com/paulmach/orb v0.7.1 h1:Zha++Z5OX/l168sqHK3k4z18LDvr+YAO/VjK0ReQ9rU=
github.com/paulmach/orb v0.7.1/go.mod h1:FWRlTgl88VI1RBx/MkrwWDRhQ96ctqMCh8boXhmqB/A=
github.com/paulmach/protoscan v0.2.1/go.mod h1:SpcSwydNLrxUGSDvXvO0P7g7AuhJ7lcKfDlhJCDw2gY=
-github.com/pganalyze/pg_query_go/v2 v2.1.2 h1:LiNm3WoBV/cc144X6jlVKD5DPL+fI2wfvCXT7z25B8U=
-github.com/pganalyze/pg_query_go/v2 v2.1.2/go.mod h1:XAxmVqz1tEGqizcQ3YSdN90vCOHBWjJi8URL1er5+cA=
github.com/phpdave11/gofpdf v1.4.2/go.mod h1:zpO6xFn9yxo3YLyMvW8HcKWVdbNqgIfOOp2dXMnm1mY=
github.com/phpdave11/gofpdi v1.0.12/go.mod h1:vBmVV0Do6hSBHC8uKUQ71JGW+ZGQq74llk/7bXwjDoI=
github.com/pierrec/lz4/v4 v4.1.8/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
diff --git a/plugin/parser/ast/constraint_def.go b/plugin/parser/ast/constraint_def.go
index 529809341c..7c0065fbbd 100644
--- a/plugin/parser/ast/constraint_def.go
+++ b/plugin/parser/ast/constraint_def.go
@@ -22,6 +22,8 @@ const (
ConstraintTypeNotNull
// ConstraintTypeCheck is the check constraint.
ConstraintTypeCheck
+ // ConstraintTypeDefault is the default constraint.
+ ConstraintTypeDefault
)
// ConstraintDef is struct for constraint definition.
@@ -57,8 +59,10 @@ type ConstraintDef struct {
// For PG, the option SkipValidation is currently only allowed for foreign key and CHECK constraints.
// If SkipValidation is true, the constraint will only be enforced against subsequent inserts or updates.
SkipValidation bool
- // CheckExpression is the expression for the check constraint.
- CheckExpression ExpressionNode
+ // Expression is the expression for
+ // 1. CHECK constraint
+ // 2. DEFAULT constraint
+ Expression ExpressionNode
// https://www.postgresql.org/docs/14/sql-altertable.html
Deferrable bool
Initdeferred bool
diff --git a/plugin/parser/engine/pg/convert.go b/plugin/parser/engine/pg/convert.go
index ad0df78352..463e94aa18 100644
--- a/plugin/parser/engine/pg/convert.go
+++ b/plugin/parser/engine/pg/convert.go
@@ -827,12 +827,17 @@ func convertConstraint(in *pgquery.Node_Constraint) (*ast.ConstraintDef, error)
}
case ast.ConstraintTypePrimaryUsingIndex, ast.ConstraintTypeUniqueUsingIndex:
cons.IndexName = in.Constraint.Indexname
- case ast.ConstraintTypeCheck:
+ case ast.ConstraintTypeCheck, ast.ConstraintTypeDefault:
expression, _, _, err := convertExpressionNode(in.Constraint.RawExpr)
if err != nil {
return nil, err
}
- cons.CheckExpression = expression
+ text, err := pgquery.DeparseNode(pgquery.DeparseTypeExpr, in.Constraint.RawExpr)
+ if err != nil {
+ return nil, err
+ }
+ expression.SetText(text)
+ cons.Expression = expression
}
return cons, nil
@@ -859,6 +864,8 @@ func convertConstraintType(in pgquery.ConstrType, usingIndex bool) ast.Constrain
return ast.ConstraintTypeNotNull
case pgquery.ConstrType_CONSTR_CHECK:
return ast.ConstraintTypeCheck
+ case pgquery.ConstrType_CONSTR_DEFAULT:
+ return ast.ConstraintTypeDefault
}
return ast.ConstraintTypeUndefined
}
diff --git a/plugin/parser/engine/pg/convert_test.go b/plugin/parser/engine/pg/convert_test.go
index 2e4bdc0634..86817bffb4 100644
--- a/plugin/parser/engine/pg/convert_test.go
+++ b/plugin/parser/engine/pg/convert_test.go
@@ -301,7 +301,7 @@ func TestPGConvertCreateTableStmt(t *testing.T) {
},
},
{
- stmt: "CREATE TABLE IF NOT EXISTS techBook (\"A\" int, b int)",
+ stmt: "CREATE TABLE IF NOT EXISTS techBook (\"A\" int, b int DEFAULT 1+2+3-4+5)",
want: []ast.Node{
&ast.CreateTableStmt{
IfNotExists: true,
@@ -317,13 +317,20 @@ func TestPGConvertCreateTableStmt(t *testing.T) {
{
ColumnName: "b",
Type: &ast.Integer{Size: 4},
+ ConstraintList: []*ast.ConstraintDef{
+ {
+ Type: ast.ConstraintTypeDefault,
+ KeyList: []string{"b"},
+ Expression: expressionWithText(&ast.UnconvertedExpressionDef{}, "(((1 + 2) + 3) - 4) + 5"),
+ },
+ },
},
},
},
},
statementList: []parser.SingleSQL{
{
- Text: "CREATE TABLE IF NOT EXISTS techBook (\"A\" int, b int)",
+ Text: "CREATE TABLE IF NOT EXISTS techBook (\"A\" int, b int DEFAULT 1+2+3-4+5)",
LastLine: 1,
},
},
@@ -857,6 +864,11 @@ func TestPGDropConstraintStmt(t *testing.T) {
runTests(t, tests)
}
+func expressionWithText(expression ast.ExpressionNode, text string) ast.ExpressionNode {
+ expression.SetText(text)
+ return expression
+}
+
func TestPGAddConstraintStmt(t *testing.T) {
tests := []testData{
{
@@ -874,10 +886,10 @@ func TestPGAddConstraintStmt(t *testing.T) {
Name: "tech_book",
},
Constraint: &ast.ConstraintDef{
- Type: ast.ConstraintTypeCheck,
- Name: "check_a_bigger_than_b",
- SkipValidation: true,
- CheckExpression: &ast.UnconvertedExpressionDef{},
+ Type: ast.ConstraintTypeCheck,
+ Name: "check_a_bigger_than_b",
+ SkipValidation: true,
+ Expression: expressionWithText(&ast.UnconvertedExpressionDef{}, "a > b"),
},
},
},
diff --git a/plugin/parser/engine/pg/deparse.go b/plugin/parser/engine/pg/deparse.go
index 6362969334..82c4c71628 100644
--- a/plugin/parser/engine/pg/deparse.go
+++ b/plugin/parser/engine/pg/deparse.go
@@ -351,6 +351,13 @@ func deparseColumnConstraint(_ parser.DeparseContext, in *ast.ConstraintDef, buf
if _, err := buf.WriteString("PRIMARY KEY"); err != nil {
return err
}
+ case ast.ConstraintTypeDefault:
+ if _, err := buf.WriteString("DEFAULT "); err != nil {
+ return err
+ }
+ if _, err := buf.WriteString(in.Expression.Text()); err != nil {
+ return err
+ }
default:
return errors.Errorf("failed to deparse column constraint: not support %d", in.Type)
}
diff --git a/plugin/parser/engine/pg/test-data/test_create_table_data.yaml b/plugin/parser/engine/pg/test-data/test_create_table_data.yaml
index 31c831e51f..1937add7a3 100644
--- a/plugin/parser/engine/pg/test-data/test_create_table_data.yaml
+++ b/plugin/parser/engine/pg/test-data/test_create_table_data.yaml
@@ -6,7 +6,7 @@
- stmt: |-
CREATE TABLE tech_book(
a char(20),
- b character(30),
+ b character(30) default 'this is default',
c varchar(330),
d character varying(400),
e text
@@ -14,7 +14,7 @@
want: |-
CREATE TABLE "tech_book" (
"a" character(20),
- "b" character(30),
+ "b" character(30) DEFAULT 'this is default',
"c" character varying(330),
"d" character varying(400),
"e" text
@@ -34,7 +34,7 @@
k int8,
l serial8,
m float8,
- n int,
+ n int default 1+23+5,
o int4,
p float4,
q int2,
@@ -56,7 +56,7 @@
"k" bigint,
"l" bigserial,
"m" double precision,
- "n" integer,
+ "n" integer DEFAULT (1 + 23) + 5,
"o" integer,
"p" real,
"q" smallint,