Skip to content

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

feat(parser): convert PG numeric data type (#2440)

rebelice0d349812daef
diff --git a/plugin/parser/ast/column_def.go b/plugin/parser/ast/column_def.go
index 9234630e4c..eb60f32d7a 100644
--- a/plugin/parser/ast/column_def.go
+++ b/plugin/parser/ast/column_def.go
@@ -5,5 +5,6 @@ type ColumnDef struct {
node
ColumnName string
+ Type DataType
ConstraintList []*ConstraintDef
}
diff --git a/plugin/parser/ast/data_type.go b/plugin/parser/ast/data_type.go
new file mode 100644
index 0000000000..c0f4df9653
--- /dev/null
+++ b/plugin/parser/ast/data_type.go
@@ -0,0 +1,32 @@
+package ast
+
+var (
+ _ DataType = (*dataType)(nil)
+ _ NumericType = (*numericType)(nil)
+)
+
+// DataType is the interface for data type.
+type DataType interface {
+ Node
+
+ dataTypeInterface()
+}
+
+type dataType struct {
+ node
+}
+
+func (*dataType) dataTypeInterface() {}
+
+// NumericType is the interface for numeric type.
+type NumericType interface {
+ DataType
+
+ numericTypeInterface()
+}
+
+type numericType struct {
+ dataType
+}
+
+func (*numericType) numericTypeInterface() {}
diff --git a/plugin/parser/ast/decimal.go b/plugin/parser/ast/decimal.go
new file mode 100644
index 0000000000..2cba79209f
--- /dev/null
+++ b/plugin/parser/ast/decimal.go
@@ -0,0 +1,10 @@
+package ast
+
+// Decimal is the struct for decimal.
+type Decimal struct {
+ numericType
+
+ // See https://www.postgresql.org/docs/14/datatype-numeric.html.
+ Precision int
+ Scale int
+}
diff --git a/plugin/parser/ast/float.go b/plugin/parser/ast/float.go
new file mode 100644
index 0000000000..a3a6648fc5
--- /dev/null
+++ b/plugin/parser/ast/float.go
@@ -0,0 +1,9 @@
+package ast
+
+// Float is the struct for floating-point.
+type Float struct {
+ numericType
+
+ // the storage size(bytes)
+ Size int
+}
diff --git a/plugin/parser/ast/integer.go b/plugin/parser/ast/integer.go
new file mode 100644
index 0000000000..e2f7c71dd8
--- /dev/null
+++ b/plugin/parser/ast/integer.go
@@ -0,0 +1,9 @@
+package ast
+
+// Integer is the struct for integer.
+type Integer struct {
+ numericType
+
+ // the storage size(bytes)
+ Size int
+}
diff --git a/plugin/parser/ast/unconverted_data_type.go b/plugin/parser/ast/unconverted_data_type.go
new file mode 100644
index 0000000000..692fdf5e41
--- /dev/null
+++ b/plugin/parser/ast/unconverted_data_type.go
@@ -0,0 +1,8 @@
+package ast
+
+// UnconvertedDataType is the struct for unconverted data type.
+// TODO(rebelice): remove it.
+// We define this because we can not convert all data types now.
+type UnconvertedDataType struct {
+ dataType
+}
diff --git a/plugin/parser/ast/utils.go b/plugin/parser/ast/utils.go
index a4cc829e44..12639660b0 100644
--- a/plugin/parser/ast/utils.go
+++ b/plugin/parser/ast/utils.go
@@ -41,6 +41,9 @@ func Walk(v Visitor, node Node) {
Walk(v, n.Column)
}
case *ColumnDef:
+ if n.Type != nil {
+ Walk(v, n.Type)
+ }
for _, cons := range n.ConstraintList {
Walk(v, cons)
}
diff --git a/plugin/parser/engine/pg/convert.go b/plugin/parser/engine/pg/convert.go
index 3e7ff495b1..7deb8cdcf0 100644
--- a/plugin/parser/engine/pg/convert.go
+++ b/plugin/parser/engine/pg/convert.go
@@ -1,6 +1,9 @@
package pg
import (
+ "strconv"
+ "strings"
+
pgquery "github.com/pganalyze/pg_query_go/v2"
"github.com/pkg/errors"
@@ -738,6 +741,9 @@ func convertColumnDef(in *pgquery.Node_ColumnDef) (*ast.ColumnDef, error) {
column := &ast.ColumnDef{
ColumnName: in.ColumnDef.Colname,
}
+ columnType, constraintList := convertDataType(in.ColumnDef.TypeName)
+ column.Type = columnType
+ column.ConstraintList = append(column.ConstraintList, constraintList...)
for _, cons := range in.ColumnDef.Constraints {
constraint, ok := cons.Node.(*pgquery.Node_Constraint)
@@ -765,3 +771,92 @@ func convertToTableType(relationType pgquery.ObjectType) (ast.TableType, error)
return ast.TableTypeUnknown, parser.NewConvertErrorf("expected TABLE or VIEW but found %s", relationType)
}
}
+
+func stripPgCatalogPrefix(tp *pgquery.TypeName) *pgquery.TypeName {
+ // The built-in data type may have the "pg_catalog" prefix.
+ if len(tp.Names) > 0 {
+ if first, ok := tp.Names[0].Node.(*pgquery.Node_String_); ok && first.String_.Str == "pg_catalog" {
+ tp.Names = tp.Names[1:]
+ }
+ }
+ return tp
+}
+
+func convertDataType(tp *pgquery.TypeName) (ast.DataType, []*ast.ConstraintDef) {
+ tp = stripPgCatalogPrefix(tp)
+ switch len(tp.Names) {
+ case 1:
+ name, ok := tp.Names[0].Node.(*pgquery.Node_String_)
+ if !ok {
+ return &ast.UnconvertedDataType{}, nil
+ }
+ s := name.String_.Str
+ switch {
+ case strings.HasPrefix(s, "int"):
+ size, err := strconv.Atoi(s[3:])
+ if err != nil {
+ return &ast.UnconvertedDataType{}, nil
+ }
+ return &ast.Integer{Size: size}, nil
+ case strings.HasPrefix(s, "float"):
+ size, err := strconv.Atoi(s[5:])
+ if err != nil {
+ return &ast.UnconvertedDataType{}, nil
+ }
+ return &ast.Float{Size: size}, nil
+ case s == "serial":
+ return &ast.Integer{Size: 4}, []*ast.ConstraintDef{{Type: ast.ConstraintTypeNotNull}}
+ case s == "smallserial":
+ return &ast.Integer{Size: 2}, []*ast.ConstraintDef{{Type: ast.ConstraintTypeNotNull}}
+ case s == "bigserial":
+ return &ast.Integer{Size: 8}, []*ast.ConstraintDef{{Type: ast.ConstraintTypeNotNull}}
+ case strings.HasPrefix(s, "serial"):
+ size, err := strconv.Atoi(s[6:])
+ if err != nil {
+ return &ast.UnconvertedDataType{}, nil
+ }
+ return &ast.Integer{Size: size}, []*ast.ConstraintDef{{Type: ast.ConstraintTypeNotNull}}
+ case s == "numeric":
+ return convertToDecimal(tp.Typmods), nil
+ }
+ default:
+ return &ast.UnconvertedDataType{}, nil
+ }
+ return &ast.UnconvertedDataType{}, nil
+}
+
+func convertToDecimal(typmods []*pgquery.Node) ast.DataType {
+ ok := false
+ decimal := &ast.Decimal{}
+ switch len(typmods) {
+ case 0:
+ return decimal
+ case 1:
+ if decimal.Precision, ok = convertToInteger(typmods[0]); !ok {
+ return &ast.UnconvertedDataType{}
+ }
+ return decimal
+ case 2:
+ if decimal.Precision, ok = convertToInteger(typmods[0]); !ok {
+ return &ast.UnconvertedDataType{}
+ }
+ if decimal.Scale, ok = convertToInteger(typmods[1]); !ok {
+ return &ast.UnconvertedDataType{}
+ }
+ return decimal
+ default:
+ return &ast.UnconvertedDataType{}
+ }
+}
+
+func convertToInteger(in *pgquery.Node) (int, bool) {
+ aConst, ok := in.Node.(*pgquery.Node_AConst)
+ if !ok {
+ return 0, false
+ }
+ integer, ok := aConst.AConst.Val.Node.(*pgquery.Node_Integer)
+ if !ok {
+ return 0, false
+ }
+ return int(integer.Integer.Ival), true
+}
diff --git a/plugin/parser/engine/pg/convert_test.go b/plugin/parser/engine/pg/convert_test.go
index b6e84ae762..5f01f32b9d 100644
--- a/plugin/parser/engine/pg/convert_test.go
+++ b/plugin/parser/engine/pg/convert_test.go
@@ -50,6 +50,179 @@ func runTests(t *testing.T, tests []testData) {
func TestPGConvertCreateTableStmt(t *testing.T) {
tests := []testData{
+ {
+ stmt: `CREATE TABLE tech_book(
+ a smallint,
+ b integer,
+ c bigint,
+ d decimal(10, 2),
+ e numeric(4),
+ f real,
+ g double precision,
+ h smallserial,
+ i serial,
+ j bigserial,
+ k int8,
+ l serial8,
+ m float8,
+ n int,
+ o int4,
+ p float4,
+ q int2,
+ r serial2,
+ s serial4,
+ t decimal)`,
+ want: []ast.Node{
+ &ast.CreateTableStmt{
+ IfNotExists: false,
+ Name: &ast.TableDef{
+ Type: ast.TableTypeBaseTable,
+ Name: "tech_book",
+ },
+ ColumnList: []*ast.ColumnDef{
+ {
+ ColumnName: "a",
+ Type: &ast.Integer{Size: 2},
+ },
+ {
+ ColumnName: "b",
+ Type: &ast.Integer{Size: 4},
+ },
+ {
+ ColumnName: "c",
+ Type: &ast.Integer{Size: 8},
+ },
+ {
+ ColumnName: "d",
+ Type: &ast.Decimal{Precision: 10, Scale: 2},
+ },
+ {
+ ColumnName: "e",
+ Type: &ast.Decimal{Precision: 4, Scale: 0},
+ },
+ {
+ ColumnName: "f",
+ Type: &ast.Float{Size: 4},
+ },
+ {
+ ColumnName: "g",
+ Type: &ast.Float{Size: 8},
+ },
+ {
+ ColumnName: "h",
+ Type: &ast.Integer{Size: 2},
+ ConstraintList: []*ast.ConstraintDef{
+ {
+ Type: ast.ConstraintTypeNotNull,
+ },
+ },
+ },
+ {
+ ColumnName: "i",
+ Type: &ast.Integer{Size: 4},
+ ConstraintList: []*ast.ConstraintDef{
+ {
+ Type: ast.ConstraintTypeNotNull,
+ },
+ },
+ },
+ {
+ ColumnName: "j",
+ Type: &ast.Integer{Size: 8},
+ ConstraintList: []*ast.ConstraintDef{
+ {
+ Type: ast.ConstraintTypeNotNull,
+ },
+ },
+ },
+ {
+ ColumnName: "k",
+ Type: &ast.Integer{Size: 8},
+ },
+ {
+ ColumnName: "l",
+ Type: &ast.Integer{Size: 8},
+ ConstraintList: []*ast.ConstraintDef{
+ {
+ Type: ast.ConstraintTypeNotNull,
+ },
+ },
+ },
+ {
+ ColumnName: "m",
+ Type: &ast.Float{Size: 8},
+ },
+ {
+ ColumnName: "n",
+ Type: &ast.Integer{Size: 4},
+ },
+ {
+ ColumnName: "o",
+ Type: &ast.Integer{Size: 4},
+ },
+ {
+ ColumnName: "p",
+ Type: &ast.Float{Size: 4},
+ },
+ {
+ ColumnName: "q",
+ Type: &ast.Integer{Size: 2},
+ },
+ {
+ ColumnName: "r",
+ Type: &ast.Integer{Size: 2},
+ ConstraintList: []*ast.ConstraintDef{
+ {
+ Type: ast.ConstraintTypeNotNull,
+ },
+ },
+ },
+ {
+ ColumnName: "s",
+ Type: &ast.Integer{Size: 4},
+ ConstraintList: []*ast.ConstraintDef{
+ {
+ Type: ast.ConstraintTypeNotNull,
+ },
+ },
+ },
+ {
+ ColumnName: "t",
+ Type: &ast.Decimal{Precision: 0, Scale: 0},
+ },
+ },
+ },
+ },
+ statementList: []parser.SingleSQL{
+ {
+ Text: `CREATE TABLE tech_book(
+ a smallint,
+ b integer,
+ c bigint,
+ d decimal(10, 2),
+ e numeric(4),
+ f real,
+ g double precision,
+ h smallserial,
+ i serial,
+ j bigserial,
+ k int8,
+ l serial8,
+ m float8,
+ n int,
+ o int4,
+ p float4,
+ q int2,
+ r serial2,
+ s serial4,
+ t decimal)`,
+ Line: 1,
+ },
+ },
+ columnLine: [][]int{
+ {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21},
+ },
+ },
{
stmt: "CREATE TABLE \"techBook\" (a int NOT NULL, b int CONSTRAINT b_not_null NOT NULL)",
want: []ast.Node{
@@ -62,6 +235,7 @@ func TestPGConvertCreateTableStmt(t *testing.T) {
ColumnList: []*ast.ColumnDef{
{
ColumnName: "a",
+ Type: &ast.Integer{Size: 4},
ConstraintList: []*ast.ConstraintDef{
{
Type: ast.ConstraintTypeNotNull,
@@ -71,6 +245,7 @@ func TestPGConvertCreateTableStmt(t *testing.T) {
},
{
ColumnName: "b",
+ Type: &ast.Integer{Size: 4},
ConstraintList: []*ast.ConstraintDef{
{
Type: ast.ConstraintTypeNotNull,
@@ -102,8 +277,14 @@ func TestPGConvertCreateTableStmt(t *testing.T) {
Name: "techbook",
},
ColumnList: []*ast.ColumnDef{
- {ColumnName: "A"},
- {ColumnName: "b"},
+ {
+ ColumnName: "A",
+ Type: &ast.Integer{Size: 4},
+ },
+ {
+ ColumnName: "b",
+ Type: &ast.Integer{Size: 4},
+ },
},
},
},
@@ -128,6 +309,7 @@ func TestPGConvertCreateTableStmt(t *testing.T) {
ColumnList: []*ast.ColumnDef{
{
ColumnName: "a",
+ Type: &ast.Integer{Size: 4},
ConstraintList: []*ast.ConstraintDef{
{
Name: "t_pk_a",
@@ -160,9 +342,11 @@ func TestPGConvertCreateTableStmt(t *testing.T) {
ColumnList: []*ast.ColumnDef{
{
ColumnName: "a",
+ Type: &ast.Integer{Size: 4},
},
{
ColumnName: "b",
+ Type: &ast.Integer{Size: 4},
ConstraintList: []*ast.ConstraintDef{
{
Name: "uk_b",
@@ -205,6 +389,7 @@ func TestPGConvertCreateTableStmt(t *testing.T) {
ColumnList: []*ast.ColumnDef{
{
ColumnName: "a",
+ Type: &ast.Integer{Size: 4},
ConstraintList: []*ast.ConstraintDef{
{
Name: "fk_a",
@@ -272,7 +457,10 @@ func TestPGAddColumnStmt(t *testing.T) {
Name: "techbook",
},
ColumnList: []*ast.ColumnDef{
- {ColumnName: "a"},
+ {
+ ColumnName: "a",
+ Type: &ast.Integer{Size: 4},
+ },
},
},
},
@@ -302,6 +490,7 @@ func TestPGAddColumnStmt(t *testing.T) {
ColumnList: []*ast.ColumnDef{
{
ColumnName: "a",
+ Type: &ast.Integer{Size: 4},
ConstraintList: []*ast.ConstraintDef{
{
Type: ast.ConstraintTypeUnique,