Skip to content

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

feat: migrate 5 PostgreSQL advisors to ANTLR and add type equivalence support (#17827)

h3n4ld6326756d832
diff --git a/backend/plugin/advisor/pgantlr/advisor_column_type_disallow_list.go b/backend/plugin/advisor/pgantlr/advisor_column_type_disallow_list.go
index 3b520f0a5d..d45c9fd971 100644
--- a/backend/plugin/advisor/pgantlr/advisor_column_type_disallow_list.go
+++ b/backend/plugin/advisor/pgantlr/advisor_column_type_disallow_list.go
@@ -144,16 +144,24 @@ func (c *columnTypeDisallowListChecker) checkType(tableName, columnName string,
return
}
- // Get the type text and normalize to lowercase for comparison
- typeText := strings.ToLower(typename.GetText())
+ // Get the type text
+ typeText := typename.GetText()
+
+ // Check if this type is equivalent to any type in the disallow list
+ var matchedDisallowedType string
+ for disallowedType := range c.typeRestriction {
+ if areTypesEquivalent(typeText, disallowedType) {
+ matchedDisallowedType = disallowedType
+ break
+ }
+ }
- // Check if this type is in the disallow list
- if c.typeRestriction[typeText] {
+ if matchedDisallowedType != "" {
c.adviceList = append(c.adviceList, &storepb.Advice{
Status: c.level,
Code: advisor.DisabledColumnType.Int32(),
Title: c.title,
- Content: fmt.Sprintf("Disallow column type %s but column %q.%q is", strings.ToUpper(typeText), tableName, columnName),
+ Content: fmt.Sprintf("Disallow column type %s but column %q.%q is", strings.ToUpper(matchedDisallowedType), tableName, columnName),
StartPosition: &storepb.Position{
Line: int32(line),
Column: 0,
diff --git a/backend/plugin/advisor/pgantlr/advisor_encoding_allowlist.go b/backend/plugin/advisor/pgantlr/advisor_encoding_allowlist.go
new file mode 100644
index 0000000000..669fc73b07
--- /dev/null
+++ b/backend/plugin/advisor/pgantlr/advisor_encoding_allowlist.go
@@ -0,0 +1,132 @@
+package pgantlr
+
+import (
+ "context"
+ "fmt"
+ "strings"
+
+ "github.com/antlr4-go/antlr/v4"
+
+ parser "github.com/bytebase/parser/postgresql"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/advisor"
+)
+
+var (
+ _ advisor.Advisor = (*EncodingAllowlistAdvisor)(nil)
+)
+
+func init() {
+ advisor.Register(storepb.Engine_POSTGRES, advisor.SchemaRuleCharsetAllowlist, &EncodingAllowlistAdvisor{})
+}
+
+// EncodingAllowlistAdvisor is the advisor checking for encoding allowlist.
+type EncodingAllowlistAdvisor struct {
+}
+
+// Check checks for encoding allowlist.
+func (*EncodingAllowlistAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([]*storepb.Advice, error) {
+ tree, err := getANTLRTree(checkCtx)
+ if err != nil {
+ return nil, err
+ }
+
+ level, err := advisor.NewStatusBySQLReviewRuleLevel(checkCtx.Rule.Level)
+ if err != nil {
+ return nil, err
+ }
+
+ payload, err := advisor.UnmarshalStringArrayTypeRulePayload(checkCtx.Rule.Payload)
+ if err != nil {
+ return nil, err
+ }
+
+ // Convert allowlist to lowercase for case-insensitive comparison
+ allowlist := make(map[string]bool)
+ for _, encoding := range payload.List {
+ allowlist[strings.ToLower(encoding)] = true
+ }
+
+ checker := &encodingAllowlistChecker{
+ BasePostgreSQLParserListener: &parser.BasePostgreSQLParserListener{},
+ level: level,
+ title: string(checkCtx.Rule.Type),
+ allowlist: allowlist,
+ }
+
+ antlr.ParseTreeWalkerDefault.Walk(checker, tree.Tree)
+
+ return checker.adviceList, nil
+}
+
+type encodingAllowlistChecker struct {
+ *parser.BasePostgreSQLParserListener
+
+ adviceList []*storepb.Advice
+ level storepb.Advice_Status
+ title string
+ allowlist map[string]bool
+}
+
+func (c *encodingAllowlistChecker) EnterCreatedbstmt(ctx *parser.CreatedbstmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
+ }
+
+ // Extract encoding from createdb_opt_list
+ // Check both with and without WITH keyword
+ var encoding string
+ if ctx.Createdb_opt_list() != nil {
+ encoding = c.extractEncoding(ctx.Createdb_opt_list())
+ }
+
+ if encoding != "" {
+ // Check if encoding is in allowlist
+ if !c.allowlist[strings.ToLower(encoding)] {
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: c.level,
+ Code: advisor.DisabledCharset.Int32(),
+ Title: c.title,
+ Content: fmt.Sprintf("\"\" used disabled encoding '%s'", strings.ToLower(encoding)),
+ StartPosition: &storepb.Position{
+ Line: 0,
+ Column: 0,
+ },
+ })
+ }
+ }
+}
+
+func (*encodingAllowlistChecker) extractEncoding(optList parser.ICreatedb_opt_listContext) string {
+ if optList == nil {
+ return ""
+ }
+
+ // Iterate through all createdb_opt_items
+ if optList.Createdb_opt_items() == nil {
+ return ""
+ }
+
+ for _, item := range optList.Createdb_opt_items().AllCreatedb_opt_item() {
+ if item == nil {
+ continue
+ }
+
+ // Check if this is an ENCODING option
+ if item.Createdb_opt_name() != nil && item.Createdb_opt_name().ENCODING() != nil {
+ // Get the encoding value from Opt_boolean_or_string
+ if item.Opt_boolean_or_string() != nil {
+ // Could be a string constant or identifier
+ text := item.Opt_boolean_or_string().GetText()
+ // Remove quotes if present
+ if len(text) >= 2 && text[0] == '\'' && text[len(text)-1] == '\'' {
+ return text[1 : len(text)-1]
+ }
+ return text
+ }
+ }
+ }
+
+ return ""
+}
diff --git a/backend/plugin/advisor/pgantlr/advisor_index_create_concurrently.go b/backend/plugin/advisor/pgantlr/advisor_index_create_concurrently.go
new file mode 100644
index 0000000000..ccbc4833a8
--- /dev/null
+++ b/backend/plugin/advisor/pgantlr/advisor_index_create_concurrently.go
@@ -0,0 +1,130 @@
+package pgantlr
+
+import (
+ "context"
+
+ "github.com/antlr4-go/antlr/v4"
+
+ parser "github.com/bytebase/parser/postgresql"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/advisor"
+)
+
+var (
+ _ advisor.Advisor = (*IndexConcurrentlyAdvisor)(nil)
+)
+
+func init() {
+ advisor.Register(storepb.Engine_POSTGRES, advisor.SchemaRuleCreateIndexConcurrently, &IndexConcurrentlyAdvisor{})
+}
+
+// IndexConcurrentlyAdvisor is the advisor checking for to create index concurrently.
+type IndexConcurrentlyAdvisor struct {
+}
+
+// Check checks for to create index concurrently.
+func (*IndexConcurrentlyAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([]*storepb.Advice, error) {
+ tree, err := getANTLRTree(checkCtx)
+ if err != nil {
+ return nil, err
+ }
+
+ level, err := advisor.NewStatusBySQLReviewRuleLevel(checkCtx.Rule.Level)
+ if err != nil {
+ return nil, err
+ }
+
+ checker := &indexCreateConcurrentlyChecker{
+ BasePostgreSQLParserListener: &parser.BasePostgreSQLParserListener{},
+ level: level,
+ title: string(checkCtx.Rule.Type),
+ newlyCreatedTables: make(map[string]bool),
+ }
+
+ // First pass: collect all newly created tables
+ antlr.ParseTreeWalkerDefault.Walk(checker, tree.Tree)
+
+ return checker.adviceList, nil
+}
+
+type indexCreateConcurrentlyChecker struct {
+ *parser.BasePostgreSQLParserListener
+
+ adviceList []*storepb.Advice
+ level storepb.Advice_Status
+ title string
+ newlyCreatedTables map[string]bool
+}
+
+// EnterCreatestmt collects newly created tables
+func (c *indexCreateConcurrentlyChecker) EnterCreatestmt(ctx *parser.CreatestmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
+ }
+
+ // Extract table name
+ qualifiedNames := ctx.AllQualified_name()
+ if len(qualifiedNames) > 0 {
+ tableName := extractTableName(qualifiedNames[0])
+ if tableName != "" {
+ c.newlyCreatedTables[tableName] = true
+ }
+ }
+}
+
+// EnterIndexstmt checks CREATE INDEX statements
+func (c *indexCreateConcurrentlyChecker) EnterIndexstmt(ctx *parser.IndexstmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
+ }
+
+ // Check if CONCURRENTLY is used (via Opt_concurrently)
+ hasConcurrently := ctx.Opt_concurrently() != nil && ctx.Opt_concurrently().CONCURRENTLY() != nil
+
+ if !hasConcurrently {
+ // Check if the index is being created on a newly created table
+ if ctx.Relation_expr() != nil && ctx.Relation_expr().Qualified_name() != nil {
+ tableName := extractTableName(ctx.Relation_expr().Qualified_name())
+ // Skip the check if the table is newly created
+ if c.newlyCreatedTables[tableName] {
+ return
+ }
+ }
+
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: c.level,
+ Code: advisor.CreateIndexUnconcurrently.Int32(),
+ Title: c.title,
+ Content: "Creating indexes will block writes on the table, unless use CONCURRENTLY",
+ StartPosition: &storepb.Position{
+ Line: int32(ctx.GetStart().GetLine()),
+ Column: 0,
+ },
+ })
+ }
+}
+
+// EnterDropstmt checks DROP INDEX statements
+func (c *indexCreateConcurrentlyChecker) EnterDropstmt(ctx *parser.DropstmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
+ }
+
+ // Check if this is a DROP INDEX statement
+ if ctx.INDEX() != nil {
+ // Check if CONCURRENTLY is used
+ if ctx.CONCURRENTLY() == nil {
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: c.level,
+ Code: advisor.DropIndexUnconcurrently.Int32(),
+ Title: c.title,
+ Content: "Droping indexes will block writes on the table, unless use CONCURRENTLY",
+ StartPosition: &storepb.Position{
+ Line: int32(ctx.GetStart().GetLine()),
+ Column: 0,
+ },
+ })
+ }
+ }
+}
diff --git a/backend/plugin/advisor/pgantlr/advisor_index_key_number_limit.go b/backend/plugin/advisor/pgantlr/advisor_index_key_number_limit.go
new file mode 100644
index 0000000000..5e585002ec
--- /dev/null
+++ b/backend/plugin/advisor/pgantlr/advisor_index_key_number_limit.go
@@ -0,0 +1,219 @@
+package pgantlr
+
+import (
+ "context"
+ "fmt"
+
+ "github.com/antlr4-go/antlr/v4"
+
+ parser "github.com/bytebase/parser/postgresql"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/advisor"
+ "github.com/bytebase/bytebase/backend/plugin/parser/pg"
+)
+
+var (
+ _ advisor.Advisor = (*IndexKeyNumberLimitAdvisor)(nil)
+)
+
+func init() {
+ advisor.Register(storepb.Engine_POSTGRES, advisor.SchemaRuleIndexKeyNumberLimit, &IndexKeyNumberLimitAdvisor{})
+}
+
+// IndexKeyNumberLimitAdvisor is the advisor checking for index key number limit.
+type IndexKeyNumberLimitAdvisor struct {
+}
+
+// Check checks for index key number limit.
+func (*IndexKeyNumberLimitAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([]*storepb.Advice, error) {
+ tree, err := getANTLRTree(checkCtx)
+ if err != nil {
+ return nil, err
+ }
+
+ level, err := advisor.NewStatusBySQLReviewRuleLevel(checkCtx.Rule.Level)
+ if err != nil {
+ return nil, err
+ }
+
+ payload, err := advisor.UnmarshalNumberTypeRulePayload(checkCtx.Rule.Payload)
+ if err != nil {
+ return nil, err
+ }
+
+ checker := &indexKeyNumberLimitChecker{
+ BasePostgreSQLParserListener: &parser.BasePostgreSQLParserListener{},
+ level: level,
+ title: string(checkCtx.Rule.Type),
+ max: payload.Number,
+ }
+
+ antlr.ParseTreeWalkerDefault.Walk(checker, tree.Tree)
+
+ return checker.adviceList, nil
+}
+
+type indexKeyNumberLimitChecker struct {
+ *parser.BasePostgreSQLParserListener
+
+ adviceList []*storepb.Advice
+ level storepb.Advice_Status
+ title string
+ max int
+}
+
+// EnterIndexstmt checks CREATE INDEX statements
+func (c *indexKeyNumberLimitChecker) EnterIndexstmt(ctx *parser.IndexstmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
+ }
+
+ // Count the number of index parameters
+ if ctx.Index_params() != nil {
+ keyCount := c.countIndexKeys(ctx.Index_params())
+ if c.max > 0 && keyCount > c.max {
+ indexName := ""
+ if ctx.Name() != nil {
+ indexName = pg.NormalizePostgreSQLName(ctx.Name())
+ }
+
+ tableName := ""
+ if ctx.Relation_expr() != nil && ctx.Relation_expr().Qualified_name() != nil {
+ tableName = extractTableName(ctx.Relation_expr().Qualified_name())
+ }
+
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: c.level,
+ Code: advisor.IndexKeyNumberExceedsLimit.Int32(),
+ Title: c.title,
+ Content: fmt.Sprintf("The number of keys of index %q in table %q should be not greater than %d", indexName, tableName, c.max),
+ StartPosition: &storepb.Position{
+ Line: int32(ctx.GetStart().GetLine()),
+ Column: 0,
+ },
+ })
+ }
+ }
+}
+
+// EnterCreatestmt checks CREATE TABLE with inline constraints
+func (c *indexKeyNumberLimitChecker) EnterCreatestmt(ctx *parser.CreatestmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
+ }
+
+ qualifiedNames := ctx.AllQualified_name()
+ if len(qualifiedNames) == 0 {
+ return
+ }
+
+ tableName := extractTableName(qualifiedNames[0])
+ if tableName == "" {
+ return
+ }
+
+ // Check table-level constraints
+ if ctx.Opttableelementlist() != nil && ctx.Opttableelementlist().Tableelementlist() != nil {
+ allElements := ctx.Opttableelementlist().Tableelementlist().AllTableelement()
+ for _, elem := range allElements {
+ if elem.Tablelikeclause() != nil {
+ continue
+ }
+ if elem.Tableconstraint() != nil {
+ c.checkTableConstraint(elem.Tableconstraint(), tableName, ctx.GetStart().GetLine())
+ }
+ }
+ }
+}
+
+// EnterAltertablestmt checks ALTER TABLE ADD CONSTRAINT
+func (c *indexKeyNumberLimitChecker) EnterAltertablestmt(ctx *parser.AltertablestmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
+ }
+
+ if ctx.Relation_expr() == nil || ctx.Relation_expr().Qualified_name() == nil {
+ return
+ }
+
+ tableName := extractTableName(ctx.Relation_expr().Qualified_name())
+ if tableName == "" {
+ return
+ }
+
+ // Check ALTER TABLE ADD CONSTRAINT
+ if ctx.Alter_table_cmds() != nil {
+ allCmds := ctx.Alter_table_cmds().AllAlter_table_cmd()
+ for _, cmd := range allCmds {
+ // ADD CONSTRAINT
+ if cmd.ADD_P() != nil && cmd.Tableconstraint() != nil {
+ c.checkTableConstraint(cmd.Tableconstraint(), tableName, ctx.GetStart().GetLine())
+ }
+ }
+ }
+}
+
+func (c *indexKeyNumberLimitChecker) checkTableConstraint(constraint parser.ITableconstraintContext, tableName string, line int) {
+ if constraint == nil {
+ return
+ }
+
+ var keyCount int
+ var constraintName string
+
+ // Get constraint name if present
+ if constraint.Name() != nil {
+ constraintName = pg.NormalizePostgreSQLName(constraint.Name())
+ }
+
+ // Check different constraint types
+ if constraint.Constraintelem() != nil {
+ elem := constraint.Constraintelem()
+
+ // PRIMARY KEY or UNIQUE
+ if (elem.PRIMARY() != nil && elem.KEY() != nil) || (elem.UNIQUE() != nil) {
+ if elem.Columnlist() != nil {
+ keyCount = c.countColumnList(elem.Columnlist())
+ }
+ }
+
+ // FOREIGN KEY
+ if elem.FOREIGN() != nil && elem.KEY() != nil {
+ if elem.Columnlist() != nil {
+ keyCount = c.countColumnList(elem.Columnlist())
+ }
+ }
+ }
+
+ if c.max > 0 && keyCount > c.max {
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: c.level,
+ Code: advisor.IndexKeyNumberExceedsLimit.Int32(),
+ Title: c.title,
+ Content: fmt.Sprintf("The number of keys of index %q in table %q should be not greater than %d", constraintName, tableName, c.max),
+ StartPosition: &storepb.Position{
+ Line: int32(line),
+ Column: 0,
+ },
+ })
+ }
+}
+
+func (*indexKeyNumberLimitChecker) countIndexKeys(params parser.IIndex_paramsContext) int {
+ if params == nil {
+ return 0
+ }
+
+ allParams := params.AllIndex_elem()
+ return len(allParams)
+}
+
+func (*indexKeyNumberLimitChecker) countColumnList(columnList parser.IColumnlistContext) int {
+ if columnList == nil {
+ return 0
+ }
+
+ allColumns := columnList.AllColumnElem()
+ return len(allColumns)
+}
diff --git a/backend/plugin/advisor/pgantlr/advisor_index_no_duplicate_column.go b/backend/plugin/advisor/pgantlr/advisor_index_no_duplicate_column.go
new file mode 100644
index 0000000000..df83b1e12d
--- /dev/null
+++ b/backend/plugin/advisor/pgantlr/advisor_index_no_duplicate_column.go
@@ -0,0 +1,243 @@
+package pgantlr
+
+import (
+ "context"
+ "fmt"
+
+ "github.com/antlr4-go/antlr/v4"
+
+ parser "github.com/bytebase/parser/postgresql"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/advisor"
+ "github.com/bytebase/bytebase/backend/plugin/parser/pg"
+)
+
+var (
+ _ advisor.Advisor = (*IndexNoDuplicateColumnAdvisor)(nil)
+)
+
+func init() {
+ advisor.Register(storepb.Engine_POSTGRES, advisor.SchemaRuleIndexNoDuplicateColumn, &IndexNoDuplicateColumnAdvisor{})
+}
+
+// IndexNoDuplicateColumnAdvisor is the advisor checking for no duplicate columns in index.
+type IndexNoDuplicateColumnAdvisor struct {
+}
+
+// Check checks for no duplicate columns in index.
+func (*IndexNoDuplicateColumnAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([]*storepb.Advice, error) {
+ tree, err := getANTLRTree(checkCtx)
+ if err != nil {
+ return nil, err
+ }
+
+ level, err := advisor.NewStatusBySQLReviewRuleLevel(checkCtx.Rule.Level)
+ if err != nil {
+ return nil, err
+ }
+
+ checker := &indexNoDuplicateColumnChecker{
+ BasePostgreSQLParserListener: &parser.BasePostgreSQLParserListener{},
+ level: level,
+ title: string(checkCtx.Rule.Type),
+ }
+
+ antlr.ParseTreeWalkerDefault.Walk(checker, tree.Tree)
+
+ return checker.adviceList, nil
+}
+
+type indexNoDuplicateColumnChecker struct {
+ *parser.BasePostgreSQLParserListener
+
+ adviceList []*storepb.Advice
+ level storepb.Advice_Status
+ title string
+}
+
+// EnterIndexstmt checks CREATE INDEX statements
+func (c *indexNoDuplicateColumnChecker) EnterIndexstmt(ctx *parser.IndexstmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
+ }
+
+ // Get index name
+ indexName := ""
+ if ctx.Name() != nil {
+ indexName = pg.NormalizePostgreSQLName(ctx.Name())
+ }
+
+ // Get table name
+ tableName := ""
+ if ctx.Relation_expr() != nil && ctx.Relation_expr().Qualified_name() != nil {
+ tableName = extractTableName(ctx.Relation_expr().Qualified_name())
+ }
+
+ // Check for duplicate columns in index parameters
+ if ctx.Index_params() != nil {
+ columns := c.extractIndexColumns(ctx.Index_params())
+ if dupCol := findDuplicate(columns); dupCol != "" {
+ c.addAdvice("INDEX", indexName, tableName, dupCol, ctx.GetStart().GetLine())
+ }
+ }
+}
+
+// EnterCreatestmt checks CREATE TABLE with inline constraints
+func (c *indexNoDuplicateColumnChecker) EnterCreatestmt(ctx *parser.CreatestmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
+ }
+
+ qualifiedNames := ctx.AllQualified_name()
+ if len(qualifiedNames) == 0 {
+ return
+ }
+
+ tableName := extractTableName(qualifiedNames[0])
+ if tableName == "" {
+ return
+ }
+
+ // Check table-level constraints
+ if ctx.Opttableelementlist() != nil && ctx.Opttableelementlist().Tableelementlist() != nil {
+ allElements := ctx.Opttableelementlist().Tableelementlist().AllTableelement()
+ for _, elem := range allElements {
+ if elem.Tableconstraint() != nil {
+ c.checkTableConstraint(elem.Tableconstraint(), tableName, elem.GetStart().GetLine())
+ }
+ }
+ }
+}
+
+// EnterAltertablestmt checks ALTER TABLE ADD CONSTRAINT
+func (c *indexNoDuplicateColumnChecker) EnterAltertablestmt(ctx *parser.AltertablestmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
+ }
+
+ if ctx.Relation_expr() == nil || ctx.Relation_expr().Qualified_name() == nil {
+ return
+ }
+
+ tableName := extractTableName(ctx.Relation_expr().Qualified_name())
+ if tableName == "" {
+ return
+ }
+
+ // Check ALTER TABLE ADD CONSTRAINT
+ if ctx.Alter_table_cmds() != nil {
+ allCmds := ctx.Alter_table_cmds().AllAlter_table_cmd()
+ for _, cmd := range allCmds {
+ // ADD CONSTRAINT
+ if cmd.ADD_P() != nil && cmd.Tableconstraint() != nil {
+ c.checkTableConstraint(cmd.Tableconstraint(), tableName, ctx.GetStart().GetLine())
+ }
+ }
+ }
+}
+
+func (c *indexNoDuplicateColumnChecker) checkTableConstraint(constraint parser.ITableconstraintContext, tableName string, line int) {
+ if constraint == nil {
+ return
+ }
+
+ // Get constraint name
+ constraintName := ""
+ if constraint.Name() != nil {
+ constraintName = pg.NormalizePostgreSQLName(constraint.Name())
+ }
+
+ // Check different constraint types
+ if constraint.Constraintelem() != nil {
+ elem := constraint.Constraintelem()
+
+ // PRIMARY KEY
+ if elem.PRIMARY() != nil && elem.KEY() != nil {
+ if elem.Columnlist() != nil {
+ columns := c.extractColumnList(elem.Columnlist())
+ if dupCol := findDuplicate(columns); dupCol != "" {
+ c.addAdvice("PRIMARY KEY", constraintName, tableName, dupCol, line)
+ }
+ }
+ }
+
+ // UNIQUE
+ if elem.UNIQUE() != nil {
+ if elem.Columnlist() != nil {
+ columns := c.extractColumnList(elem.Columnlist())
+ if dupCol := findDuplicate(columns); dupCol != "" {
+ c.addAdvice("UNIQUE KEY", constraintName, tableName, dupCol, line)
+ }
+ }
+ }
+
+ // FOREIGN KEY
+ if elem.FOREIGN() != nil && elem.KEY() != nil {
+ if elem.Columnlist() != nil {
+ columns := c.extractColumnList(elem.Columnlist())
+ if dupCol := findDuplicate(columns); dupCol != "" {
+ c.addAdvice("FOREIGN KEY", constraintName, tableName, dupCol, line)
+ }
+ }
+ }
+ }
+}
+
+func (*indexNoDuplicateColumnChecker) extractIndexColumns(params parser.IIndex_paramsContext) []string {
+ if params == nil {
+ return nil
+ }
+
+ var columns []string
+ allParams := params.AllIndex_elem()
+ for _, param := range allParams {
+ if param.Colid() != nil {
+ colName := pg.NormalizePostgreSQLColid(param.Colid())
+ columns = append(columns, colName)
+ }
+ }
+
+ return columns
+}
+
+func (*indexNoDuplicateColumnChecker) extractColumnList(columnList parser.IColumnlistContext) []string {
+ if columnList == nil {
+ return nil
+ }
+
+ var columns []string
+ allColumns := columnList.AllColumnElem()
+ for _, col := range allColumns {
+ if col.Colid() != nil {
+ colName := pg.NormalizePostgreSQLColid(col.Colid())
+ columns = append(columns, colName)
+ }
+ }
+
+ return columns
+}
+
+func findDuplicate(columns []string) string {
+ seen := make(map[string]bool)
+ for _, col := range columns {
+ if seen[col] {
+ return col
+ }
+ seen[col] = true
+ }
+ return ""
+}
+
+func (c *indexNoDuplicateColumnChecker) addAdvice(constraintType, constraintName, tableName, duplicateColumn string, line int) {
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: c.level,
+ Code: advisor.DuplicateColumnInIndex.Int32(),
+ Title: c.title,
+ Content: fmt.Sprintf("%s %q has duplicate column %q.%q", constraintType, constraintName, tableName, duplicateColumn),
+ StartPosition: &storepb.Position{
+ Line: int32(line),
+ Column: 0,
+ },
+ })
+}
diff --git a/backend/plugin/advisor/pgantlr/advisor_index_primary_key_type_allowlist.go b/backend/plugin/advisor/pgantlr/advisor_index_primary_key_type_allowlist.go
new file mode 100644
index 0000000000..ff5acd7dbb
--- /dev/null
+++ b/backend/plugin/advisor/pgantlr/advisor_index_primary_key_type_allowlist.go
@@ -0,0 +1,177 @@
+package pgantlr
+
+import (
+ "context"
+ "fmt"
+
+ "github.com/antlr4-go/antlr/v4"
+
+ parser "github.com/bytebase/parser/postgresql"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/advisor"
+ "github.com/bytebase/bytebase/backend/plugin/parser/pg"
+)
+
+var (
+ _ advisor.Advisor = (*IndexPrimaryKeyTypeAllowlistAdvisor)(nil)
+)
+
+func init() {
+ advisor.Register(storepb.Engine_POSTGRES, advisor.SchemaRuleIndexPrimaryKeyTypeAllowlist, &IndexPrimaryKeyTypeAllowlistAdvisor{})
+}
+
+// IndexPrimaryKeyTypeAllowlistAdvisor is the advisor checking for primary key type allowlist.
+type IndexPrimaryKeyTypeAllowlistAdvisor struct {
+}
+
+// Check checks for primary key type allowlist.
+func (*IndexPrimaryKeyTypeAllowlistAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([]*storepb.Advice, error) {
+ tree, err := getANTLRTree(checkCtx)
+ if err != nil {
+ return nil, err
+ }
+
+ level, err := advisor.NewStatusBySQLReviewRuleLevel(checkCtx.Rule.Level)
+ if err != nil {
+ return nil, err
+ }
+
+ payload, err := advisor.UnmarshalStringArrayTypeRulePayload(checkCtx.Rule.Payload)
+ if err != nil {
+ return nil, err
+ }
+
+ checker := &indexPrimaryKeyTypeAllowlistChecker{
+ BasePostgreSQLParserListener: &parser.BasePostgreSQLParserListener{},
+ level: level,
+ title: string(checkCtx.Rule.Type),
+ allowlist: payload.List,
+ }
+
+ antlr.ParseTreeWalkerDefault.Walk(checker, tree.Tree)
+
+ return checker.adviceList, nil
+}
+
+type indexPrimaryKeyTypeAllowlistChecker struct {
+ *parser.BasePostgreSQLParserListener
+
+ adviceList []*storepb.Advice
+ level storepb.Advice_Status
+ title string
+ allowlist []string
+}
+
+// EnterCreatestmt checks CREATE TABLE with inline PRIMARY KEY constraints
+func (c *indexPrimaryKeyTypeAllowlistChecker) EnterCreatestmt(ctx *parser.CreatestmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
+ }
+
+ // Build maps of column name to column type and line number
+ columnTypes := make(map[string]string)
+ columnLines := make(map[string]int)
+
+ if ctx.Opttableelementlist() != nil && ctx.Opttableelementlist().Tableelementlist() != nil {
+ allElements := ctx.Opttableelementlist().Tableelementlist().AllTableelement()
+ for _, elem := range allElements {
+ if elem.ColumnDef() != nil {
+ colDef := elem.ColumnDef()
+ if colDef.Colid() != nil && colDef.Typename() != nil {
+ columnName := pg.NormalizePostgreSQLColid(colDef.Colid())
+ columnType := c.getTypeName(colDef.Typename())
+ columnTypes[columnName] = columnType
+ columnLines[columnName] = colDef.GetStart().GetLine()
+
+ // Check if this column has PRIMARY KEY constraint
+ if c.hasColumnPrimaryKeyConstraint(colDef) {
+ if !c.isTypeAllowed(columnType) {
+ c.addAdvice(columnName, columnType, colDef.GetStart().GetLine())
+ }
+ }
+ }
+ }
+ }
+
+ // Check table-level PRIMARY KEY constraints
+ for _, elem := range allElements {
+ if elem.Tableconstraint() != nil {
+ c.checkTablePrimaryKey(elem.Tableconstraint(), columnTypes, columnLines)
+ }
+ }
+ }
+}
+
+func (*indexPrimaryKeyTypeAllowlistChecker) hasColumnPrimaryKeyConstraint(colDef parser.IColumnDefContext) bool {
+ if colDef.Colquallist() == nil {
+ return false
+ }
+
+ allConstraints := colDef.Colquallist().AllColconstraint()
+ for _, constraint := range allConstraints {
+ if constraint.Colconstraintelem() != nil {
+ elem := constraint.Colconstraintelem()
+ if elem.PRIMARY() != nil && elem.KEY() != nil {
+ return true
+ }
+ }
+ }
+
+ return false
+}
+
+func (c *indexPrimaryKeyTypeAllowlistChecker) checkTablePrimaryKey(constraint parser.ITableconstraintContext, columnTypes map[string]string, columnLines map[string]int) {
+ if constraint == nil || constraint.Constraintelem() == nil {
+ return
+ }
+
+ elem := constraint.Constraintelem()
+
+ // Check if this is a PRIMARY KEY constraint
+ if elem.PRIMARY() != nil && elem.KEY() != nil {
+ if elem.Columnlist() != nil {
+ // Get all columns in the PRIMARY KEY
+ allColumns := elem.Columnlist().AllColumnElem()
+ for _, col := range allColumns {
+ if col.Colid() != nil {
+ columnName := pg.NormalizePostgreSQLColid(col.Colid())
+ if columnType, exists := columnTypes[columnName]; exists {
+ if !c.isTypeAllowed(columnType) {
+ // Use the column definition's line, not the constraint's line
+ line := columnLines[columnName]
+ c.addAdvice(columnName, columnType, line)
+ }
+ }
+ }
+ }
+ }
+ }
+}
+
+func (*indexPrimaryKeyTypeAllowlistChecker) getTypeName(typename parser.ITypenameContext) string {
+ if typename == nil {
+ return ""
+ }
+
+ // Get the type text and normalize it
+ return normalizePostgreSQLType(typename.GetText())
+}
+
+func (c *indexPrimaryKeyTypeAllowlistChecker) isTypeAllowed(columnType string) bool {
+ // Check if the column type is equivalent to any type in the allowlist
+ return isTypeInList(columnType, c.allowlist)
+}
+
+func (c *indexPrimaryKeyTypeAllowlistChecker) addAdvice(columnName, columnType string, line int) {
+ c.adviceList = append(c.adviceList, &storepb.Advice{
+ Status: c.level,
+ Code: advisor.IndexPKType.Int32(),
+ Title: c.title,
+ Content: fmt.Sprintf("The column %q is one of the primary key, but its type %q is not in allowlist", columnName, columnType),
+ StartPosition: &storepb.Position{
+ Line: int32(line),
+ Column: 0,
+ },
+ })
+}
diff --git a/backend/plugin/advisor/pgantlr/pgantlr_test.go b/backend/plugin/advisor/pgantlr/pgantlr_test.go
index 9ffd6065c8..5cd5969463 100644
--- a/backend/plugin/advisor/pgantlr/pgantlr_test.go
+++ b/backend/plugin/advisor/pgantlr/pgantlr_test.go
@@ -11,6 +11,7 @@ func TestPostgreSQLANTLRRules(t *testing.T) {
antlrRules := []advisor.SQLReviewRuleType{
HelloWorldRule, // Test advisor to verify framework works
advisor.BuiltinRulePriorBackupCheck, // Migrated from legacy
+ advisor.SchemaRuleCharsetAllowlist, // Migrated from legacy
advisor.SchemaRuleCollationAllowlist, // Migrated from legacy
advisor.SchemaRuleColumnCommentConvention, // Migrated from legacy
advisor.SchemaRuleColumnDefaultDisallowVolatile, // Migrated from legacy
@@ -20,6 +21,10 @@ func TestPostgreSQLANTLRRules(t *testing.T) {
advisor.SchemaRuleColumnRequireDefault, // Migrated from legacy
advisor.SchemaRuleColumnTypeDisallowList, // Migrated from legacy
advisor.SchemaRuleCommentLength, // Migrated from legacy
+ advisor.SchemaRuleCreateIndexConcurrently, // Migrated from legacy
+ advisor.SchemaRuleIndexKeyNumberLimit, // Migrated from legacy
+ advisor.SchemaRuleIndexNoDuplicateColumn, // Migrated from legacy
+ advisor.SchemaRuleIndexPrimaryKeyTypeAllowlist, // Migrated from legacy
advisor.SchemaRuleRequiredColumn, // Migrated from legacy
// Add real rules here as you migrate them from legacy pg/ folder
// Example:
diff --git a/backend/plugin/advisor/pgantlr/test/index_create_concurrently.yaml b/backend/plugin/advisor/pgantlr/test/index_create_concurrently.yaml
new file mode 100644
index 0000000000..564d6485c9
--- /dev/null
+++ b/backend/plugin/advisor/pgantlr/test/index_create_concurrently.yaml
@@ -0,0 +1,22 @@
+- statement: create index on tech_book(id);
+ changeType: 1
+ want:
+ - status: 2
+ code: 814
+ title: index.create-concurrently
+ content: Creating indexes will block writes on the table, unless use CONCURRENTLY
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
+- statement: create index concurrently on tech_book(id);
+ changeType: 1
+- statement: |
+ CREATE TABLE feed_subscription (
+ id uuid NOT NULL DEFAULT gen_random_uuid(),
+ userId uuid NOT NULL,
+ projectId uuid,
+ CONSTRAINT PK_feed_subscription PRIMARY KEY (id)
+ );
+ CREATE INDEX IDX_feed_subscription_userId ON feed_subscription (userId);
+ changeType: 1
diff --git a/backend/plugin/advisor/pgantlr/test/index_key_number_limit.yaml b/backend/plugin/advisor/pgantlr/test/index_key_number_limit.yaml
new file mode 100644
index 0000000000..7f413d596f
--- /dev/null
+++ b/backend/plugin/advisor/pgantlr/test/index_key_number_limit.yaml
@@ -0,0 +1,67 @@
+- statement: CREATE TABLE t(name char(20));
+ changeType: 1
+- statement: CREATE TABLE t(name varchar(225));
+ changeType: 1
+- statement: CREATE TABLE t(name char(225), PRIMARY KEY (name));
+ changeType: 1
+- statement: CREATE TABLE t(id int, name char(225), c1 int, c2 int, c3 int, c4 int, CONSTRAINT t_id_name PRIMARY KEY (id, name, c1, c2, c3, c4));
+ changeType: 1
+ want:
+ - status: 2
+ code: 802
+ title: index.key-number-limit
+ content: The number of keys of index "t_id_name" in table "t" should be not greater than 5
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
+- statement: CREATE TABLE t(id int, name char(225), c1 int, c2 int, c3 int, c4 int, CONSTRAINT t_id_name UNIQUE (id, name, c1, c2, c3, c4));
+ changeType: 1
+ want:
+ - status: 2
+ code: 802
+ title: index.key-number-limit
+ content: The number of keys of index "t_id_name" in table "t" should be not greater than 5
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
+- statement: |-
+ CREATE TABLE address(id int, phone varchar(225), c1 int, c2 int, c3 int, c4 int);
+ CREATE INDEX idx_address_phone ON address(id, phone, c1, c2, c3, c4);
+ changeType: 1
+ want:
+ - status: 2
+ code: 802
+ title: index.key-number-limit
+ content: The number of keys of index "idx_address_phone" in table "address" should be not greater than 5
+ startposition:
+ line: 2
+ column: 0
+ endposition: null
+- statement: |-
+ CREATE TABLE address(id int, phone varchar(225), c1 int, c2 int, c3 int, c4 int);
+ CREATE UNIQUE INDEX idx_address_phone ON address(id, phone, c2, c1, c3, c4);
+ changeType: 1
+ want:
+ - status: 2
+ code: 802
+ title: index.key-number-limit
+ content: The number of keys of index "idx_address_phone" in table "address" should be not greater than 5
+ startposition:
+ line: 2
+ column: 0
+ endposition: null
+- statement: |-
+ CREATE TABLE address(id int, phone varchar(225), c1 int, c2 int, c3 int, c4 int);
+ ALTER TABLE address ADD CONSTRAINT address_id_name UNIQUE (id, phone, c1, c2, c3, c4);
+ changeType: 1
+ want:
+ - status: 2
+ code: 802
+ title: index.key-number-limit
+ content: The number of keys of index "address_id_name" in table "address" should be not greater than 5
+ startposition:
+ line: 2
+ column: 0
+ endposition: null
diff --git a/backend/plugin/advisor/pgantlr/test/index_no_duplicate_column.yaml b/backend/plugin/advisor/pgantlr/test/index_no_duplicate_column.yaml
new file mode 100644
index 0000000000..f64dbd808b
--- /dev/null
+++ b/backend/plugin/advisor/pgantlr/test/index_no_duplicate_column.yaml
@@ -0,0 +1,68 @@
+- statement: CREATE TABLE t (a INT, PRIMARY KEY (a));
+ changeType: 1
+- statement: |-
+ CREATE TABLE t (
+ a int,
+ PRIMARY KEY (a, a));
+ changeType: 1
+ want:
+ - status: 2
+ code: 812
+ title: index.no-duplicate-column
+ content: PRIMARY KEY "" has duplicate column "t"."a"
+ startposition:
+ line: 3
+ column: 0
+ endposition: null
+- statement: |-
+ CREATE TABLE t(a int);
+ CREATE INDEX idx_a on t(a, a);
+ changeType: 1
+ want:
+ - status: 2
+ code: 812
+ title: index.no-duplicate-column
+ content: INDEX "idx_a" has duplicate column "t"."a"
+ startposition:
+ line: 2
+ column: 0
+ endposition: null
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE t ADD CONSTRAINT uk_a UNIQUE (a, a);
+ changeType: 1
+ want:
+ - status: 2
+ code: 812
+ title: index.no-duplicate-column
+ content: UNIQUE KEY "uk_a" has duplicate column "t"."a"
+ startposition:
+ line: 2
+ column: 0
+ endposition: null
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE t ADD CONSTRAINT pk_a PRIMARY KEY (a, a);
+ changeType: 1
+ want:
+ - status: 2
+ code: 812
+ title: index.no-duplicate-column
+ content: PRIMARY KEY "pk_a" has duplicate column "t"."a"
+ startposition:
+ line: 2
+ column: 0
+ endposition: null
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE t ADD CONSTRAINT fk_a FOREIGN KEY (a, a) REFERENCES t1(a, b);
+ changeType: 1
+ want:
+ - status: 2
+ code: 812
+ title: index.no-duplicate-column
+ content: FOREIGN KEY "fk_a" has duplicate column "t"."a"
+ startposition:
+ line: 2
+ column: 0
+ endposition: null
diff --git a/backend/plugin/advisor/pgantlr/test/index_primary_key_type_allowlist.yaml b/backend/plugin/advisor/pgantlr/test/index_primary_key_type_allowlist.yaml
new file mode 100644
index 0000000000..081fdbe689
--- /dev/null
+++ b/backend/plugin/advisor/pgantlr/test/index_primary_key_type_allowlist.yaml
@@ -0,0 +1,33 @@
+- statement: CREATE TABLE t(a SERIAL PRIMARY KEY);
+ changeType: 1
+- statement: CREATE TABLE t(a BIGSERIAL, PRIMARY KEY(a));
+ changeType: 1
+- statement: |-
+ CREATE TABLE t(
+ a varchar(20) PRIMARY KEY
+ );
+ changeType: 1
+ want:
+ - status: 2
+ code: 803
+ title: index.primary-key-type-allowlist
+ content: The column "a" is one of the primary key, but its type "character varying(20)" is not in allowlist
+ startposition:
+ line: 2
+ column: 0
+ endposition: null
+- statement: |-
+ CREATE TABLE t(
+ a varchar(20),
+ PRIMARY KEY(a)
+ );
+ changeType: 1
+ want:
+ - status: 2
+ code: 803
+ title: index.primary-key-type-allowlist
+ content: The column "a" is one of the primary key, but its type "character varying(20)" is not in allowlist
+ startposition:
+ line: 2
+ column: 0
+ endposition: null
diff --git a/backend/plugin/advisor/pgantlr/test/system_charset_allowlist.yaml b/backend/plugin/advisor/pgantlr/test/system_charset_allowlist.yaml
new file mode 100644
index 0000000000..a73f504104
--- /dev/null
+++ b/backend/plugin/advisor/pgantlr/test/system_charset_allowlist.yaml
@@ -0,0 +1,17 @@
+- statement: CREATE DATABASE test WITH ENCODING 'UTF8'
+ changeType: 1
+- statement: CREATE DATABASE test
+ changeType: 1
+- statement: CREATE DATABASE test WITH ENCODING 'LATIN1'
+ changeType: 1
+ want:
+ - status: 2
+ code: 1001
+ title: system.charset.allowlist
+ content: '"" used disabled encoding ''latin1'''
+ startposition:
+ line: 0
+ column: 0
+ endposition: null
+- statement: /* this is a comment */
+ changeType: 1
diff --git a/backend/plugin/advisor/pgantlr/type_equivalence.go b/backend/plugin/advisor/pgantlr/type_equivalence.go
new file mode 100644
index 0000000000..cd799986cf
--- /dev/null
+++ b/backend/plugin/advisor/pgantlr/type_equivalence.go
@@ -0,0 +1,190 @@
+package pgantlr
+
+import "strings"
+
+// PostgreSQL type equivalence helper functions
+
+// normalizePostgreSQLType normalizes a PostgreSQL type name to its canonical form.
+// This handles common aliases and returns the standard representation.
+func normalizePostgreSQLType(typeName string) string {
+ typeName = strings.ToLower(strings.TrimSpace(typeName))
+
+ // Remove whitespace inside parentheses: " VARCHAR ( 20 ) " -> "varchar(20)"
+ typeName = removeWhitespaceInParams(typeName)
+
+ // Handle SERIAL types - they're aliases for INTEGER types with sequences
+ switch typeName {
+ case "serial":
+ return "integer"
+ case "bigserial":
+ return "bigint"
+ case "smallserial":
+ return "smallint"
+ }
+
+ // Handle VARCHAR - it's an alias for CHARACTER VARYING
+ if strings.HasPrefix(typeName, "varchar") {
+ if typeName == "varchar" {
+ return "character varying"
+ }
+ // varchar(N) -> character varying(N)
+ if strings.HasPrefix(typeName, "varchar(") {
+ params := strings.TrimPrefix(typeName, "varchar")
+ return "character varying" + params
+ }
+ }
+
+ return typeName
+}
+
+// removeWhitespaceInParams removes whitespace inside parentheses
+// " VARCHAR ( 20 ) " -> "varchar(20)"
+// "double precision" -> "double precision" (unchanged)
+func removeWhitespaceInParams(typeName string) string {
+ idx := strings.Index(typeName, "(")
+ if idx == -1 {
+ // No parameters, just normalize spaces between words to single space
+ return normalizeSpaces(typeName)
+ }
+
+ // Split into base type and parameters
+ baseType := normalizeSpaces(typeName[:idx])
+ params := strings.ReplaceAll(typeName[idx:], " ", "")
+ return baseType + params
+}
+
+// normalizeSpaces normalizes multiple spaces to single space
+func normalizeSpaces(s string) string {
+ // Replace multiple spaces with single space
+ for strings.Contains(s, " ") {
+ s = strings.ReplaceAll(s, " ", " ")
+ }
+ return strings.TrimSpace(s)
+}
+
+// areTypesEquivalent checks if two PostgreSQL types are equivalent.
+// This includes:
+// - Exact matches after normalization
+// - Type family equivalence (e.g., int/integer/int4)
+func areTypesEquivalent(typeA, typeB string) bool {
+ typeA = normalizePostgreSQLType(typeA)
+ typeB = normalizePostgreSQLType(typeB)
+
+ // Exact match after normalization
+ if typeA == typeB {
+ return true
+ }
+
+ // Check type families
+ // Integer family: int, integer, int4
+ intTypes := []string{"int", "integer", "int4"}
+ if inSlice(typeA, intTypes) && inSlice(typeB, intTypes) {
+ return true
+ }
+
+ // Bigint family: bigint, int8
+ bigintTypes := []string{"bigint", "int8"}
+ if inSlice(typeA, bigintTypes) && inSlice(typeB, bigintTypes) {
+ return true
+ }
+
+ // Smallint family: smallint, int2
+ smallintTypes := []string{"smallint", "int2"}
+ if inSlice(typeA, smallintTypes) && inSlice(typeB, smallintTypes) {
+ return true
+ }
+
+ // Real family: real, float4
+ realTypes := []string{"real", "float4"}
+ if inSlice(typeA, realTypes) && inSlice(typeB, realTypes) {
+ return true
+ }
+
+ // Double precision family: double precision, float8
+ doubleTypes := []string{"double precision", "float8"}
+ if inSlice(typeA, doubleTypes) && inSlice(typeB, doubleTypes) {
+ return true
+ }
+
+ // Boolean family: boolean, bool
+ boolTypes := []string{"boolean", "bool"}
+ if inSlice(typeA, boolTypes) && inSlice(typeB, boolTypes) {
+ return true
+ }
+
+ // Character family: character, char (but we need to preserve length parameters)
+ // For types with parameters, check base type equivalence
+ if hasTypeParameters(typeA) && hasTypeParameters(typeB) {
+ baseA, paramsA := splitTypeAndParams(typeA)
+ baseB, paramsB := splitTypeAndParams(typeB)
+
+ // Check if base types are equivalent
+ if areBaseTypesEquivalent(baseA, baseB) {
+ // For character types, parameters must also match
+ return paramsA == paramsB
+ }
+ }
+
+ return false
+}
+
+// hasTypeParameters checks if a type has parameters (e.g., varchar(20))
+func hasTypeParameters(typeName string) bool {
+ return strings.Contains(typeName, "(")
+}
+
+// splitTypeAndParams splits a type into base type and parameters
+// e.g., "character varying(20)" -> ("character varying", "(20)")
+func splitTypeAndParams(typeName string) (string, string) {
+ idx := strings.Index(typeName, "(")
+ if idx == -1 {
+ return typeName, ""
+ }
+ return strings.TrimSpace(typeName[:idx]), typeName[idx:]
+}
+
+// areBaseTypesEquivalent checks if two base types (without parameters) are equivalent
+func areBaseTypesEquivalent(baseA, baseB string) bool {
+ // Normalize base types
+ baseA = strings.ToLower(strings.TrimSpace(baseA))
+ baseB = strings.ToLower(strings.TrimSpace(baseB))
+
+ if baseA == baseB {
+ return true
+ }
+
+ // Character varying family
+ charVaryingTypes := []string{"character varying", "varchar"}
+ if inSlice(baseA, charVaryingTypes) && inSlice(baseB, charVaryingTypes) {
+ return true
+ }
+
+ // Character family
+ charTypes := []string{"character", "char"}
+ if inSlice(baseA, charTypes) && inSlice(baseB, charTypes) {
+ return true
+ }
+
+ return false
+}
+
+// inSlice checks if a string is in a slice
+func inSlice(str string, slice []string) bool {
+ for _, s := range slice {
+ if s == str {
+ return true
+ }
+ }
+ return false
+}
+
+// isTypeInList checks if a column type matches any type in the given list,
+// considering type equivalence.
+func isTypeInList(columnType string, typeList []string) bool {
+ for _, listType := range typeList {
+ if areTypesEquivalent(columnType, listType) {
+ return true
+ }
+ }
+ return false
+}
diff --git a/backend/plugin/advisor/pgantlr/type_equivalence_test.go b/backend/plugin/advisor/pgantlr/type_equivalence_test.go
new file mode 100644
index 0000000000..9913b0b8b4
--- /dev/null
+++ b/backend/plugin/advisor/pgantlr/type_equivalence_test.go
@@ -0,0 +1,174 @@
+package pgantlr
+
+import (
+ "testing"
+)
+
+func TestAreTypesEquivalent(t *testing.T) {
+ tests := []struct {
+ typeA string
+ typeB string
+ expected bool
+ }{
+ // Exact matches
+ {"int", "int", true},
+ {"INTEGER", "INTEGER", true},
+ {"json", "json", true},
+
+ // Integer family
+ {"int", "integer", true},
+ {"INT", "INTEGER", true}, // Case insensitive
+ {"int", "int4", true},
+ {"integer", "int4", true},
+ {"INT", "INT4", true},
+
+ // Bigint family
+ {"bigint", "int8", true},
+ {"BIGINT", "INT8", true},
+
+ // Smallint family
+ {"smallint", "int2", true},
+ {"SMALLINT", "INT2", true},
+
+ // Serial types - normalized to their base types
+ {"serial", "integer", true},
+ {"SERIAL", "INTEGER", true},
+ {"serial", "int", true},
+ {"bigserial", "bigint", true},
+ {"BIGSERIAL", "BIGINT", true},
+ {"smallserial", "smallint", true},
+
+ // Real family
+ {"real", "float4", true},
+ {"REAL", "FLOAT4", true},
+
+ // Double precision family
+ {"double precision", "float8", true},
+ {"DOUBLE PRECISION", "FLOAT8", true},
+
+ // Boolean family
+ {"boolean", "bool", true},
+ {"BOOLEAN", "BOOL", true},
+
+ // VARCHAR / CHARACTER VARYING
+ {"varchar", "character varying", true},
+ {"VARCHAR", "CHARACTER VARYING", true},
+ {"varchar(20)", "character varying(20)", true},
+ {"VARCHAR(20)", "CHARACTER VARYING(20)", true},
+
+ // Character family
+ {"character(10)", "char(10)", true},
+ {"CHAR(10)", "CHARACTER(10)", true},
+
+ // Different types should not be equivalent
+ {"int", "bigint", false},
+ {"integer", "smallint", false},
+ {"text", "varchar", false},
+ {"json", "jsonb", false},
+
+ // Different parameters should not be equivalent
+ {"varchar(20)", "varchar(30)", false},
+ {"char(10)", "char(20)", false},
+
+ // Types not in same family
+ {"int", "text", false},
+ {"boolean", "int", false},
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.typeA+"_vs_"+tt.typeB, func(t *testing.T) {
+ result := areTypesEquivalent(tt.typeA, tt.typeB)
+ if result != tt.expected {
+ t.Errorf("areTypesEquivalent(%q, %q) = %v, want %v", tt.typeA, tt.typeB, result, tt.expected)
+ }
+
+ // Test symmetry: areTypesEquivalent should be commutative
+ resultReversed := areTypesEquivalent(tt.typeB, tt.typeA)
+ if resultReversed != tt.expected {
+ t.Errorf("areTypesEquivalent(%q, %q) = %v, want %v (symmetry test)", tt.typeB, tt.typeA, resultReversed, tt.expected)
+ }
+ })
+ }
+}
+
+func TestNormalizePostgreSQLType(t *testing.T) {
+ tests := []struct {
+ input string
+ expected string
+ }{
+ // Serial types
+ {"SERIAL", "integer"},
+ {"serial", "integer"},
+ {"BIGSERIAL", "bigint"},
+ {"bigserial", "bigint"},
+ {"SMALLSERIAL", "smallint"},
+ {"smallserial", "smallint"},
+
+ // VARCHAR variants
+ {"VARCHAR", "character varying"},
+ {"varchar", "character varying"},
+ {"VARCHAR(20)", "character varying(20)"},
+ {"varchar(100)", "character varying(100)"},
+
+ // Types that don't need normalization
+ {"integer", "integer"},
+ {"INT", "int"},
+ {"text", "text"},
+ {"JSON", "json"},
+ {"jsonb", "jsonb"},
+
+ // Whitespace handling
+ {" INTEGER ", "integer"},
+ {" VARCHAR ( 20 ) ", "character varying(20)"},
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.input, func(t *testing.T) {
+ result := normalizePostgreSQLType(tt.input)
+ if result != tt.expected {
+ t.Errorf("normalizePostgreSQLType(%q) = %q, want %q", tt.input, result, tt.expected)
+ }
+ })
+ }
+}
+
+func TestIsTypeInList(t *testing.T) {
+ tests := []struct {
+ columnType string
+ typeList []string
+ expected bool
+ }{
+ // Exact match
+ {"int", []string{"int", "text"}, true},
+ {"json", []string{"json", "jsonb"}, true},
+
+ // Equivalent type match
+ {"integer", []string{"int", "text"}, true},
+ {"INT4", []string{"int", "text"}, true},
+ {"serial", []string{"int", "text"}, true},
+
+ // VARCHAR equivalence
+ {"varchar", []string{"character varying", "text"}, true},
+ {"varchar(20)", []string{"character varying", "text"}, false}, // Different parameters
+
+ // No match
+ {"bigint", []string{"int", "text"}, false},
+ {"jsonb", []string{"json", "text"}, false},
+
+ // Empty list
+ {"int", []string{}, false},
+
+ // Case insensitive
+ {"INTEGER", []string{"INT", "TEXT"}, true},
+ {"INT", []string{"integer", "text"}, true},
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.columnType, func(t *testing.T) {
+ result := isTypeInList(tt.columnType, tt.typeList)
+ if result != tt.expected {
+ t.Errorf("isTypeInList(%q, %v) = %v, want %v", tt.columnType, tt.typeList, result, tt.expected)
+ }
+ })
+ }
+}