Skip to content

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

feat: support PostgreSQL CREATE/RENAME/DROP [UNIQUE] INDEX (#1741)

rebelice7aebfef28037
diff --git a/plugin/parser/ast/create_index_stmt.go b/plugin/parser/ast/create_index_stmt.go
new file mode 100644
index 0000000000..1483aca57d
--- /dev/null
+++ b/plugin/parser/ast/create_index_stmt.go
@@ -0,0 +1,8 @@
+package ast
+
+// CreateIndexStmt is the struct for create index statement.
+type CreateIndexStmt struct {
+ node
+
+ Index *IndexDef
+}
diff --git a/plugin/parser/ast/drop_index_stmt.go b/plugin/parser/ast/drop_index_stmt.go
new file mode 100644
index 0000000000..8f677bfa03
--- /dev/null
+++ b/plugin/parser/ast/drop_index_stmt.go
@@ -0,0 +1,10 @@
+package ast
+
+// DropIndexStmt is the struct for drop index statement.
+type DropIndexStmt struct {
+ node
+
+ // Here use IndexDef because the drop index statement needs the schema name for PostgreSQL.
+ // If the drop index statement doesn't contain schema name, the Table of this index is nil.
+ IndexList []*IndexDef
+}
diff --git a/plugin/parser/ast/index_def.go b/plugin/parser/ast/index_def.go
new file mode 100644
index 0000000000..fa4d5f7053
--- /dev/null
+++ b/plugin/parser/ast/index_def.go
@@ -0,0 +1,11 @@
+package ast
+
+// IndexDef is the struct for index definition.
+type IndexDef struct {
+ node
+
+ Name string
+ Table *TableDef
+ Unique bool
+ KeyList []*IndexKeyDef
+}
diff --git a/plugin/parser/ast/index_key_def.go b/plugin/parser/ast/index_key_def.go
new file mode 100644
index 0000000000..36bb1adde3
--- /dev/null
+++ b/plugin/parser/ast/index_key_def.go
@@ -0,0 +1,21 @@
+package ast
+
+// IndexKeyType is the type for index key.
+type IndexKeyType int
+
+const (
+ // IndexKeyTypeColumn is the type for the column.
+ IndexKeyTypeColumn IndexKeyType = iota
+ // IndexKeyTypeExpression is the type for the expression.
+ IndexKeyTypeExpression
+)
+
+// IndexKeyDef is the struct for index key definition.
+// Only support conversion IndexKeyTypeColumn now.
+// TODO(rebelice): support conversion IndexKeyTypeExpression.
+type IndexKeyDef struct {
+ node
+
+ Type IndexKeyType
+ Key string
+}
diff --git a/plugin/parser/ast/rename_index_stmt.go b/plugin/parser/ast/rename_index_stmt.go
new file mode 100644
index 0000000000..686a231846
--- /dev/null
+++ b/plugin/parser/ast/rename_index_stmt.go
@@ -0,0 +1,12 @@
+package ast
+
+// RenameIndexStmt is the struct for the rename index statement.
+type RenameIndexStmt struct {
+ node
+
+ // For PostgreSQL, we only use TableDef.Schema.
+ // If this rename index statement doesn't contain schema name, Table will be nil.
+ Table *TableDef
+ IndexName string
+ NewName string
+}
diff --git a/plugin/parser/engine/pg/convert.go b/plugin/parser/engine/pg/convert.go
index 56d7af4ebc..39b981cc83 100644
--- a/plugin/parser/engine/pg/convert.go
+++ b/plugin/parser/engine/pg/convert.go
@@ -105,12 +105,95 @@ func convert(node *pgquery.Node) (ast.Node, error) {
ConstraintName: in.RenameStmt.Subname,
NewName: in.RenameStmt.Newname,
}, nil
+ case pgquery.ObjectType_OBJECT_INDEX:
+ return &ast.RenameIndexStmt{
+ Table: convertRangeVarToIndexTableName(in.RenameStmt.Relation),
+ IndexName: in.RenameStmt.Relation.Relname,
+ NewName: in.RenameStmt.Newname,
+ }, nil
+ }
+ case *pgquery.Node_IndexStmt:
+ indexDef := &ast.IndexDef{
+ Table: convertRangeVarToTableName(in.IndexStmt.Relation),
+ Name: in.IndexStmt.Idxname,
+ Unique: in.IndexStmt.Unique,
+ }
+
+ for _, key := range in.IndexStmt.IndexParams {
+ index, ok := key.Node.(*pgquery.Node_IndexElem)
+ if !ok {
+ return nil, parser.NewConvertErrorf("expected IndexElem but found %t", key.Node)
+ }
+ // We only support index on columns now.
+ // TODO(rebelice): support index on expressions.
+ if index.IndexElem.Name != "" {
+ indexDef.KeyList = append(indexDef.KeyList, &ast.IndexKeyDef{
+ Type: ast.IndexKeyTypeColumn,
+ Key: index.IndexElem.Name,
+ })
+ } else {
+ indexDef.KeyList = append(indexDef.KeyList, &ast.IndexKeyDef{
+ Type: ast.IndexKeyTypeExpression,
+ })
+ }
+ }
+
+ return &ast.CreateIndexStmt{Index: indexDef}, nil
+ case *pgquery.Node_DropStmt:
+ switch in.DropStmt.RemoveType {
+ case pgquery.ObjectType_OBJECT_INDEX:
+ dropIndex := &ast.DropIndexStmt{}
+ for _, object := range in.DropStmt.Objects {
+ list, ok := object.Node.(*pgquery.Node_List)
+ if !ok {
+ return nil, parser.NewConvertErrorf("expected List but found %t", object.Node)
+ }
+ indexDef, err := convertListToIndexDef(list)
+ if err != nil {
+ return nil, err
+ }
+ dropIndex.IndexList = append(dropIndex.IndexList, indexDef)
+ }
+ return dropIndex, nil
+ case pgquery.ObjectType_OBJECT_TABLE:
+ // TODO.
+ // Trick linter to use switch...case...
}
}
return nil, nil
}
+func convertListToIndexDef(in *pgquery.Node_List) (*ast.IndexDef, error) {
+ stringList, err := convertListToStringList(in)
+ if err != nil {
+ return nil, err
+ }
+ indexDef := &ast.IndexDef{}
+ switch len(in.List.Items) {
+ case 2:
+ indexDef.Table = &ast.TableDef{Schema: stringList[0]}
+ indexDef.Name = stringList[1]
+ case 1:
+ indexDef.Name = stringList[0]
+ default:
+ return nil, parser.NewConvertErrorf("expected length is 1 or 2, but found %d", len(in.List.Items))
+ }
+ return indexDef, nil
+}
+
+func convertListToStringList(in *pgquery.Node_List) ([]string, error) {
+ var res []string
+ for _, item := range in.List.Items {
+ s, ok := item.Node.(*pgquery.Node_String_)
+ if !ok {
+ return nil, parser.NewConvertErrorf("expected String but found %t", item.Node)
+ }
+ res = append(res, s.String_.Str)
+ }
+ return res, nil
+}
+
func convertRangeVarToTableName(in *pgquery.RangeVar) *ast.TableDef {
return &ast.TableDef{
Database: in.Catalogname,
@@ -119,6 +202,15 @@ func convertRangeVarToTableName(in *pgquery.RangeVar) *ast.TableDef {
}
}
+func convertRangeVarToIndexTableName(in *pgquery.RangeVar) *ast.TableDef {
+ if in.Schemaname == "" {
+ return nil
+ }
+ return &ast.TableDef{
+ Schema: in.Schemaname,
+ }
+}
+
func convertConstraint(in *pgquery.Node_Constraint) (*ast.ConstraintDef, error) {
cons := &ast.ConstraintDef{
Name: in.Constraint.Conname,
diff --git a/plugin/parser/engine/pg/convert_test.go b/plugin/parser/engine/pg/convert_test.go
index 97be0271de..ae14cf5db1 100644
--- a/plugin/parser/engine/pg/convert_test.go
+++ b/plugin/parser/engine/pg/convert_test.go
@@ -261,6 +261,96 @@ func TestPGRenameConstraintStmt(t *testing.T) {
runTests(t, tests)
}
+func TestPGCreateIndexStmt(t *testing.T) {
+ tests := []testData{
+ {
+ stmt: "CREATE INDEX idx_id ON tech_book (id)",
+ want: []ast.Node{
+ &ast.CreateIndexStmt{
+ Index: &ast.IndexDef{
+ Name: "idx_id",
+ Table: &ast.TableDef{Name: "tech_book"},
+ Unique: false,
+ KeyList: []*ast.IndexKeyDef{
+ {
+ Type: ast.IndexKeyTypeColumn,
+ Key: "id",
+ },
+ },
+ },
+ },
+ },
+ },
+ {
+ stmt: "CREATE UNIQUE INDEX idx_id ON tech_book (id)",
+ want: []ast.Node{
+ &ast.CreateIndexStmt{
+ Index: &ast.IndexDef{
+ Name: "idx_id",
+ Table: &ast.TableDef{Name: "tech_book"},
+ Unique: true,
+ KeyList: []*ast.IndexKeyDef{
+ {
+ Type: ast.IndexKeyTypeColumn,
+ Key: "id",
+ },
+ },
+ },
+ },
+ },
+ },
+ }
+
+ runTests(t, tests)
+}
+
+func TestPGDropIndexStmt(t *testing.T) {
+ tests := []testData{
+ {
+ stmt: "DROP INDEX xschema.idx_id, idx_x",
+ want: []ast.Node{
+ &ast.DropIndexStmt{
+ IndexList: []*ast.IndexDef{
+ {
+ Table: &ast.TableDef{Schema: "xschema"},
+ Name: "idx_id",
+ },
+ {Name: "idx_x"},
+ },
+ },
+ },
+ },
+ }
+
+ runTests(t, tests)
+}
+
+func TestPGAlterIndexStmt(t *testing.T) {
+ tests := []testData{
+ {
+ stmt: "ALTER INDEX xschema.idx_id RENAME TO \"IDX_ID\"",
+ want: []ast.Node{
+ &ast.RenameIndexStmt{
+ Table: &ast.TableDef{Schema: "xschema"},
+ IndexName: "idx_id",
+ NewName: "IDX_ID",
+ },
+ },
+ },
+ {
+ stmt: "ALTER INDEX idx_id RENAME TO \"IDX_ID\"",
+ want: []ast.Node{
+ &ast.RenameIndexStmt{
+ IndexName: "idx_id",
+ NewName: "IDX_ID",
+ },
+ },
+ },
+ }
+
+ runTests(t, tests)
+}
+
func TestPGDropConstraintStmt(t *testing.T) {
tests := []testData{
{