Skip to content

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

refactor(advisor): improve PostgreSQL identifier normalization in ANTLR advisors (#17820)

h3n4l3fc383aa8968
diff --git a/backend/plugin/advisor/pgantlr/advisor_column_comment_convention.go b/backend/plugin/advisor/pgantlr/advisor_column_comment_convention.go
index 25bfdc70da..ceadf44712 100644
--- a/backend/plugin/advisor/pgantlr/advisor_column_comment_convention.go
+++ b/backend/plugin/advisor/pgantlr/advisor_column_comment_convention.go
@@ -11,6 +11,7 @@ import (
"github.com/bytebase/bytebase/backend/common"
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 (
@@ -96,7 +97,7 @@ func (c *columnCommentConventionChecker) EnterCreatestmt(ctx *parser.CreatestmtC
allElements := ctx.Opttableelementlist().Tableelementlist().AllTableelement()
for _, elem := range allElements {
if elem.ColumnDef() != nil && elem.ColumnDef().Colid() != nil {
- columnName := elem.ColumnDef().Colid().GetText()
+ columnName := pg.NormalizePostgreSQLColid(elem.ColumnDef().Colid())
c.columns = append(c.columns, columnInfo{
schema: "public", // Default schema
table: tableName,
@@ -128,7 +129,7 @@ func (c *columnCommentConventionChecker) EnterAltertablestmt(ctx *parser.Alterta
for _, cmd := range allCmds {
// ADD COLUMN
if cmd.ADD_P() != nil && cmd.ColumnDef() != nil && cmd.ColumnDef().Colid() != nil {
- columnName := cmd.ColumnDef().Colid().GetText()
+ columnName := pg.NormalizePostgreSQLColid(cmd.ColumnDef().Colid())
c.columns = append(c.columns, columnInfo{
schema: "public", // Default schema
table: tableName,
@@ -151,12 +152,9 @@ func (c *columnCommentConventionChecker) EnterCommentstmt(ctx *parser.Commentstm
}
// Extract table.column name from any_name
- // any_name is like: table.column
+ // any_name is like: table.column or schema.table.column
anyName := ctx.Any_name()
- text := anyName.GetText()
-
- // Split by dot to get table and column
- parts := splitIdentifier(text)
+ parts := pg.NormalizePostgreSQLAnyName(anyName)
if len(parts) < 2 {
return
}
@@ -167,7 +165,7 @@ func (c *columnCommentConventionChecker) EnterCommentstmt(ctx *parser.Commentstm
// Extract comment text
comment := ""
if ctx.Comment_text() != nil && ctx.Comment_text().Sconst() != nil {
- comment = c.extractStringConstant(ctx.Comment_text().Sconst())
+ comment = extractStringConstant(ctx.Comment_text().Sconst())
}
c.comments = append(c.comments, commentInfo{
@@ -184,30 +182,8 @@ func (*columnCommentConventionChecker) extractTableName(qualifiedNames []parser.
return ""
}
- // Take the first qualified name
- text := qualifiedNames[0].GetText()
-
- // Remove schema if present (e.g., "public.table" -> "table")
- parts := splitIdentifier(text)
- if len(parts) == 0 {
- return ""
- }
-
- // Return the last part (table name)
- return parts[len(parts)-1]
-}
-
-func (*columnCommentConventionChecker) extractStringConstant(sconst parser.ISconstContext) string {
- if sconst == nil {
- return ""
- }
-
- // Get the text and remove surrounding quotes
- text := sconst.GetText()
- if len(text) >= 2 && text[0] == '\'' && text[len(text)-1] == '\'' {
- return text[1 : len(text)-1]
- }
- return text
+ // Return the last part (table name) from qualified name
+ return extractTableName(qualifiedNames[0])
}
func (c *columnCommentConventionChecker) generateAdvice() []*storepb.Advice {
diff --git a/backend/plugin/advisor/pgantlr/advisor_column_default_disallow_volatile.go b/backend/plugin/advisor/pgantlr/advisor_column_default_disallow_volatile.go
index e7b2c8ddf8..f4a196ae67 100644
--- a/backend/plugin/advisor/pgantlr/advisor_column_default_disallow_volatile.go
+++ b/backend/plugin/advisor/pgantlr/advisor_column_default_disallow_volatile.go
@@ -11,6 +11,7 @@ import (
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 (
@@ -87,7 +88,7 @@ func (c *columnDefaultDisallowVolatileChecker) EnterAltertablestmt(ctx *parser.A
if cmd.ADD_P() != nil && cmd.ColumnDef() != nil {
colDef := cmd.ColumnDef()
if colDef.Colid() != nil {
- columnName := colDef.Colid().GetText()
+ columnName := pg.NormalizePostgreSQLColid(colDef.Colid())
// Check if this column has a volatile DEFAULT
if c.hasVolatileDefault(colDef) {
diff --git a/backend/plugin/advisor/pgantlr/advisor_column_maximum_character_length.go b/backend/plugin/advisor/pgantlr/advisor_column_maximum_character_length.go
index c472276621..fa3aa275bd 100644
--- a/backend/plugin/advisor/pgantlr/advisor_column_maximum_character_length.go
+++ b/backend/plugin/advisor/pgantlr/advisor_column_maximum_character_length.go
@@ -3,7 +3,6 @@ package pgantlr
import (
"context"
"fmt"
- "strconv"
"github.com/antlr4-go/antlr/v4"
@@ -11,6 +10,7 @@ import (
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 (
@@ -80,7 +80,7 @@ func (c *columnMaximumCharacterLengthChecker) EnterCreatestmt(ctx *parser.Create
if elem.ColumnDef() != nil {
colDef := elem.ColumnDef()
if colDef.Colid() != nil && colDef.Typename() != nil {
- columnName := colDef.Colid().GetText()
+ columnName := pg.NormalizePostgreSQLColid(colDef.Colid())
charLength := c.getCharLength(colDef.Typename())
if charLength > c.maximum {
c.addAdvice(tableName, columnName, ctx.GetStart().GetLine())
@@ -101,8 +101,7 @@ func (c *columnMaximumCharacterLengthChecker) EnterAltertablestmt(ctx *parser.Al
return
}
- text := ctx.Relation_expr().Qualified_name().GetText()
- parts := splitIdentifier(text)
+ parts := pg.NormalizePostgreSQLQualifiedName(ctx.Relation_expr().Qualified_name())
if len(parts) == 0 {
return
}
@@ -123,7 +122,7 @@ func (c *columnMaximumCharacterLengthChecker) EnterAltertablestmt(ctx *parser.Al
if cmd.ADD_P() != nil && cmd.ColumnDef() != nil {
colDef := cmd.ColumnDef()
if colDef.Colid() != nil && colDef.Typename() != nil {
- columnName := colDef.Colid().GetText()
+ columnName := pg.NormalizePostgreSQLColid(colDef.Colid())
charLength := c.getCharLength(colDef.Typename())
if charLength > c.maximum {
c.addAdvice(tableName, columnName, ctx.GetStart().GetLine())
@@ -137,7 +136,7 @@ func (c *columnMaximumCharacterLengthChecker) EnterAltertablestmt(ctx *parser.Al
// Get column name
allColids := cmd.AllColid()
if len(allColids) > 0 {
- columnName := allColids[0].GetText()
+ columnName := pg.NormalizePostgreSQLColid(allColids[0])
charLength := c.getCharLength(cmd.Typename())
if charLength > c.maximum {
c.addAdvice(tableName, columnName, ctx.GetStart().GetLine())
@@ -154,8 +153,7 @@ func (*columnMaximumCharacterLengthChecker) extractTableName(qualifiedNames []pa
return ""
}
- text := qualifiedNames[0].GetText()
- parts := splitIdentifier(text)
+ parts := pg.NormalizePostgreSQLQualifiedName(qualifiedNames[0])
if len(parts) == 0 {
return ""
}
@@ -204,9 +202,9 @@ func (*columnMaximumCharacterLengthChecker) getCharLength(typename parser.ITypen
// Now check if it has a size
if character.Iconst() != nil {
- sizeText := character.Iconst().GetText()
- size, err := strconv.Atoi(sizeText)
+ size, err := extractIntegerConstant(character.Iconst())
if err != nil {
+ // If parsing fails, return 0 (no length limit to check)
return 0
}
return size
diff --git a/backend/plugin/advisor/pgantlr/advisor_column_no_null.go b/backend/plugin/advisor/pgantlr/advisor_column_no_null.go
index 1bf9867c7a..141da36c52 100644
--- a/backend/plugin/advisor/pgantlr/advisor_column_no_null.go
+++ b/backend/plugin/advisor/pgantlr/advisor_column_no_null.go
@@ -12,6 +12,7 @@ import (
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/advisor"
"github.com/bytebase/bytebase/backend/plugin/advisor/catalog"
+ "github.com/bytebase/bytebase/backend/plugin/parser/pg"
)
var (
@@ -93,7 +94,7 @@ func (c *columnNoNullChecker) EnterCreatestmt(ctx *parser.CreatestmtContext) {
if elem.ColumnDef() != nil {
colDef := elem.ColumnDef()
if colDef.Colid() != nil {
- columnName := colDef.Colid().GetText()
+ columnName := pg.NormalizePostgreSQLColid(colDef.Colid())
// Add column as nullable by default
c.addColumn("public", tableName, columnName, elem.GetStart().GetLine())
@@ -129,7 +130,7 @@ func (c *columnNoNullChecker) EnterAltertablestmt(ctx *parser.AltertablestmtCont
if cmd.ADD_P() != nil && cmd.ColumnDef() != nil {
colDef := cmd.ColumnDef()
if colDef.Colid() != nil {
- columnName := colDef.Colid().GetText()
+ columnName := pg.NormalizePostgreSQLColid(colDef.Colid())
c.addColumn("public", tableName, columnName, ctx.GetStart().GetLine())
c.removeColumnByColConstraints("public", tableName, colDef)
}
@@ -139,7 +140,7 @@ func (c *columnNoNullChecker) EnterAltertablestmt(ctx *parser.AltertablestmtCont
if cmd.ALTER() != nil && cmd.SET() != nil && cmd.NOT() != nil && cmd.NULL_P() != nil {
allColids := cmd.AllColid()
if len(allColids) > 0 {
- columnName := allColids[0].GetText()
+ columnName := pg.NormalizePostgreSQLColid(allColids[0])
c.removeColumn("public", tableName, columnName)
}
}
@@ -148,7 +149,7 @@ func (c *columnNoNullChecker) EnterAltertablestmt(ctx *parser.AltertablestmtCont
if cmd.ALTER() != nil && cmd.DROP() != nil && cmd.NOT() != nil && cmd.NULL_P() != nil {
allColids := cmd.AllColid()
if len(allColids) > 0 {
- columnName := allColids[0].GetText()
+ columnName := pg.NormalizePostgreSQLColid(allColids[0])
c.addColumn("public", tableName, columnName, ctx.GetStart().GetLine())
}
}
@@ -166,13 +167,7 @@ func (*columnNoNullChecker) extractTableName(qualifiedNames []parser.IQualified_
return ""
}
- text := qualifiedNames[0].GetText()
- parts := splitIdentifier(text)
- if len(parts) == 0 {
- return ""
- }
-
- return parts[len(parts)-1]
+ return extractTableName(qualifiedNames[0])
}
func (c *columnNoNullChecker) addColumn(schema, table, column string, line int) {
@@ -194,7 +189,7 @@ func (c *columnNoNullChecker) removeColumnByColConstraints(schema, table string,
return
}
- columnName := colDef.Colid().GetText()
+ columnName := pg.NormalizePostgreSQLColid(colDef.Colid())
allConstraints := colDef.Colquallist().AllColconstraint()
for _, constraint := range allConstraints {
if constraint.Colconstraintelem() == nil {
@@ -229,7 +224,7 @@ func (c *columnNoNullChecker) removeColumnByTableConstraint(schema, table string
allColumnElems := elem.Columnlist().AllColumnElem()
for _, columnElem := range allColumnElems {
if columnElem.Colid() != nil {
- c.removeColumn(schema, table, columnElem.Colid().GetText())
+ c.removeColumn(schema, table, pg.NormalizePostgreSQLColid(columnElem.Colid()))
}
}
return
@@ -239,7 +234,7 @@ func (c *columnNoNullChecker) removeColumnByTableConstraint(schema, table string
if elem.PRIMARY() != nil && elem.KEY() != nil && elem.Existingindex() != nil {
existingIndex := elem.Existingindex()
if existingIndex.Name() != nil {
- indexName := existingIndex.Name().GetText()
+ indexName := pg.NormalizePostgreSQLName(existingIndex.Name())
// Try to find index in catalog
if c.catalog != nil {
_, index := c.catalog.Origin.FindIndex(&catalog.IndexFind{
diff --git a/backend/plugin/advisor/pgantlr/advisor_column_require_default.go b/backend/plugin/advisor/pgantlr/advisor_column_require_default.go
index b28e892e11..378e36d245 100644
--- a/backend/plugin/advisor/pgantlr/advisor_column_require_default.go
+++ b/backend/plugin/advisor/pgantlr/advisor_column_require_default.go
@@ -3,6 +3,7 @@ package pgantlr
import (
"context"
"fmt"
+ "strings"
"github.com/antlr4-go/antlr/v4"
@@ -10,6 +11,7 @@ import (
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 (
@@ -65,7 +67,7 @@ func (c *columnRequireDefaultChecker) EnterCreatestmt(ctx *parser.CreatestmtCont
return
}
- tableName := c.extractTableName(qualifiedNames[0])
+ tableName := extractTableName(qualifiedNames[0])
if tableName == "" {
return
}
@@ -77,7 +79,7 @@ func (c *columnRequireDefaultChecker) EnterCreatestmt(ctx *parser.CreatestmtCont
if elem.ColumnDef() != nil {
colDef := elem.ColumnDef()
if colDef.Colid() != nil {
- columnName := colDef.Colid().GetText()
+ columnName := pg.NormalizePostgreSQLColid(colDef.Colid())
// Check if column has DEFAULT
if !c.hasDefault(colDef) {
c.adviceList = append(c.adviceList, &storepb.Advice{
@@ -106,7 +108,7 @@ func (c *columnRequireDefaultChecker) EnterAltertablestmt(ctx *parser.Altertable
return
}
- tableName := c.extractTableName(ctx.Relation_expr().Qualified_name())
+ tableName := extractTableName(ctx.Relation_expr().Qualified_name())
if tableName == "" {
return
}
@@ -119,7 +121,7 @@ func (c *columnRequireDefaultChecker) EnterAltertablestmt(ctx *parser.Altertable
if cmd.ADD_P() != nil && cmd.ColumnDef() != nil {
colDef := cmd.ColumnDef()
if colDef.Colid() != nil {
- columnName := colDef.Colid().GetText()
+ columnName := pg.NormalizePostgreSQLColid(colDef.Colid())
// Check if column has DEFAULT
if !c.hasDefault(colDef) {
c.adviceList = append(c.adviceList, &storepb.Advice{
@@ -139,28 +141,13 @@ func (c *columnRequireDefaultChecker) EnterAltertablestmt(ctx *parser.Altertable
}
}
-func (*columnRequireDefaultChecker) extractTableName(qualifiedNameCtx parser.IQualified_nameContext) string {
- if qualifiedNameCtx == nil {
- return ""
- }
-
- text := qualifiedNameCtx.GetText()
- parts := splitIdentifier(text)
- if len(parts) == 0 {
- return ""
- }
-
- // Return the last part (table name)
- return parts[len(parts)-1]
-}
-
// hasDefault checks if a column definition has a DEFAULT clause
// or uses a type that implicitly includes a default (like serial, bigserial, smallserial)
func (*columnRequireDefaultChecker) hasDefault(colDef parser.IColumnDefContext) bool {
// Check if the type is serial/bigserial/smallserial (which have implicit defaults)
if colDef.Typename() != nil && colDef.Typename().Simpletypename() != nil {
simpleType := colDef.Typename().Simpletypename()
- typeText := simpleType.GetText()
+ typeText := strings.ToLower(simpleType.GetText())
// serial, bigserial, smallserial all have implicit DEFAULT nextval()
if typeText == "serial" || typeText == "bigserial" || typeText == "smallserial" {
return true
diff --git a/backend/plugin/advisor/pgantlr/advisor_column_required.go b/backend/plugin/advisor/pgantlr/advisor_column_required.go
index 77baa698a3..2764f053e3 100644
--- a/backend/plugin/advisor/pgantlr/advisor_column_required.go
+++ b/backend/plugin/advisor/pgantlr/advisor_column_required.go
@@ -12,6 +12,7 @@ import (
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 (
@@ -89,7 +90,7 @@ func (c *columnRequirementChecker) EnterCreatestmt(ctx *parser.CreatestmtContext
return
}
- tableName := c.extractTableName(qualifiedNames[0])
+ tableName := extractTableName(qualifiedNames[0])
if tableName == "" {
return
}
@@ -99,7 +100,7 @@ func (c *columnRequirementChecker) EnterCreatestmt(ctx *parser.CreatestmtContext
allElements := ctx.Opttableelementlist().Tableelementlist().AllTableelement()
for _, elem := range allElements {
if elem.ColumnDef() != nil && elem.ColumnDef().Colid() != nil {
- columnName := elem.ColumnDef().Colid().GetText()
+ columnName := pg.NormalizePostgreSQLColid(elem.ColumnDef().Colid())
delete(c.requiredColumns, columnName)
}
}
@@ -135,7 +136,7 @@ func (c *columnRequirementChecker) EnterAltertablestmt(ctx *parser.Altertablestm
return
}
- tableName := c.extractTableName(ctx.Relation_expr().Qualified_name())
+ tableName := extractTableName(ctx.Relation_expr().Qualified_name())
if tableName == "" {
return
}
@@ -148,7 +149,7 @@ func (c *columnRequirementChecker) EnterAltertablestmt(ctx *parser.Altertablestm
if cmd.DROP() != nil {
allColids := cmd.AllColid()
if len(allColids) > 0 {
- columnName := allColids[0].GetText()
+ columnName := pg.NormalizePostgreSQLColid(allColids[0])
// Check if this is a required column (O(1) lookup)
if c.requiredColumnsMap[columnName] {
c.adviceList = append(c.adviceList, &storepb.Advice{
@@ -181,7 +182,7 @@ func (c *columnRequirementChecker) EnterRenamestmt(ctx *parser.RenamestmtContext
// Get table name
var tableName string
if ctx.Relation_expr() != nil && ctx.Relation_expr().Qualified_name() != nil {
- tableName = c.extractTableName(ctx.Relation_expr().Qualified_name())
+ tableName = extractTableName(ctx.Relation_expr().Qualified_name())
}
if tableName == "" {
return
@@ -193,8 +194,8 @@ func (c *columnRequirementChecker) EnterRenamestmt(ctx *parser.RenamestmtContext
return
}
- oldName := allNames[0].GetText()
- newName := allNames[1].GetText()
+ oldName := pg.NormalizePostgreSQLName(allNames[0])
+ newName := pg.NormalizePostgreSQLName(allNames[1])
// Check if renaming away from a required column name (O(1) lookup)
if c.requiredColumnsMap[oldName] && oldName != newName {
@@ -210,18 +211,3 @@ func (c *columnRequirementChecker) EnterRenamestmt(ctx *parser.RenamestmtContext
})
}
}
-
-func (*columnRequirementChecker) extractTableName(qualifiedNameCtx parser.IQualified_nameContext) string {
- if qualifiedNameCtx == nil {
- return ""
- }
-
- text := qualifiedNameCtx.GetText()
- parts := splitIdentifier(text)
- if len(parts) == 0 {
- return ""
- }
-
- // Return the last part (table name)
- return parts[len(parts)-1]
-}
diff --git a/backend/plugin/advisor/pgantlr/utils.go b/backend/plugin/advisor/pgantlr/utils.go
index 9f85bd96e6..3b12202b24 100644
--- a/backend/plugin/advisor/pgantlr/utils.go
+++ b/backend/plugin/advisor/pgantlr/utils.go
@@ -1,6 +1,8 @@
package pgantlr
import (
+ "strconv"
+
"github.com/antlr4-go/antlr/v4"
parser "github.com/bytebase/parser/postgresql"
"github.com/pkg/errors"
@@ -26,37 +28,6 @@ func isTopLevel(ctx antlr.Tree) bool {
}
}
-// splitIdentifier splits a qualified identifier by dots, handling quoted parts.
-// For example:
-// - "public.table" -> ["public", "table"]
-// - "\"public\".\"table\"" -> ["public", "table"]
-// - "table" -> ["table"]
-func splitIdentifier(s string) []string {
- var parts []string
- var current string
- inQuote := false
-
- for i := 0; i < len(s); i++ {
- ch := s[i]
- if ch == '"' {
- inQuote = !inQuote
- } else if ch == '.' && !inQuote {
- if current != "" {
- parts = append(parts, current)
- current = ""
- }
- } else {
- current += string(ch)
- }
- }
-
- if current != "" {
- parts = append(parts, current)
- }
-
- return parts
-}
-
// getANTLRTree extracts the ANTLR parse tree from the advisor context.
// The AST must be pre-parsed and passed via checkCtx.AST (e.g., in tests or by the framework).
// This enforces proper AST caching and makes any missing cache obvious.
@@ -72,3 +43,42 @@ func getANTLRTree(checkCtx advisor.Context) (*pg.ParseResult, error) {
return parseResult, nil
}
+
+// extractTableName extracts the table name (last component) from a qualified name.
+// Handles both "schema.table" and "table" formats.
+func extractTableName(ctx parser.IQualified_nameContext) string {
+ parts := pg.NormalizePostgreSQLQualifiedName(ctx)
+ if len(parts) == 0 {
+ return ""
+ }
+ return parts[len(parts)-1]
+}
+
+// extractIntegerConstant extracts an integer value from an Iconst context.
+// Returns the integer value and an error if parsing fails.
+func extractIntegerConstant(ctx parser.IIconstContext) (int, error) {
+ if ctx == nil {
+ return 0, errors.New("iconst context is nil")
+ }
+ text := ctx.GetText()
+ val, err := strconv.Atoi(text)
+ if err != nil {
+ return 0, errors.Wrapf(err, "failed to parse integer constant: %s", text)
+ }
+ return val, nil
+}
+
+// extractStringConstant extracts a string value from an Sconst context.
+// Removes surrounding quotes and handles basic escape sequences.
+func extractStringConstant(ctx parser.ISconstContext) string {
+ if ctx == nil {
+ return ""
+ }
+
+ text := ctx.GetText()
+ // Remove surrounding single quotes
+ if len(text) >= 2 && text[0] == '\'' && text[len(text)-1] == '\'' {
+ return text[1 : len(text)-1]
+ }
+ return text
+}