Skip to content

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

fix(advisor): improve line number accuracy for SQL Review advisors (#17961)

h3n4l344e062820a3
diff --git a/backend/plugin/advisor/mysql/rule_collation_allowlist.go b/backend/plugin/advisor/mysql/rule_collation_allowlist.go
index 89aa449d85..ca1857dcff 100644
--- a/backend/plugin/advisor/mysql/rule_collation_allowlist.go
+++ b/backend/plugin/advisor/mysql/rule_collation_allowlist.go
@@ -160,7 +160,7 @@ func (r *CollationAllowlistRule) checkCreateTable(ctx *mysql.CreateTableContext)
for _, attr := range columnDef.FieldDefinition().AllColumnAttribute() {
if attr != nil && attr.Collate() != nil && attr.Collate().CollationName() != nil {
collation := mysqlparser.NormalizeMySQLCollationName(attr.Collate().CollationName())
- r.checkCollation(collation, tableElement.GetStart().GetLine())
+ r.checkCollation(collation, columnDef.GetStart().GetLine())
}
}
}
diff --git a/backend/plugin/advisor/mysql/rule_column_maximum_character_length.go b/backend/plugin/advisor/mysql/rule_column_maximum_character_length.go
index 08f870e9ea..cb55b1cf2c 100644
--- a/backend/plugin/advisor/mysql/rule_column_maximum_character_length.go
+++ b/backend/plugin/advisor/mysql/rule_column_maximum_character_length.go
@@ -130,7 +130,7 @@ func (r *ColumnMaximumCharacterLengthRule) checkCreateTable(ctx *mysql.CreateTab
Code: advisor.CharLengthExceedsLimit.Int32(),
Title: r.title,
Content: fmt.Sprintf("The length of the CHAR column `%s.%s` is bigger than %d, please use VARCHAR instead", tableName, columnName, r.maximum),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + tableElement.GetStart().GetLine()),
+ StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + tableElement.ColumnDefinition().GetStart().GetLine()),
})
}
}
diff --git a/backend/plugin/advisor/mysql/rule_column_maximum_varchar_length.go b/backend/plugin/advisor/mysql/rule_column_maximum_varchar_length.go
index 9537fc1955..77e7bc5483 100644
--- a/backend/plugin/advisor/mysql/rule_column_maximum_varchar_length.go
+++ b/backend/plugin/advisor/mysql/rule_column_maximum_varchar_length.go
@@ -128,7 +128,7 @@ func (r *VarcharLengthRule) checkCreateTable(ctx *mysql.CreateTableContext) {
Code: advisor.VarcharLengthExceedsLimit.Int32(),
Title: r.title,
Content: fmt.Sprintf("The length of the VARCHAR column `%s.%s` is bigger than %d", tableName, columnName, r.maximum),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + tableElement.GetStart().GetLine()),
+ StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + tableElement.ColumnDefinition().GetStart().GetLine()),
})
}
}
diff --git a/backend/plugin/advisor/mysql/rule_column_no_null.go b/backend/plugin/advisor/mysql/rule_column_no_null.go
index 9c795b5674..772262ca8a 100644
--- a/backend/plugin/advisor/mysql/rule_column_no_null.go
+++ b/backend/plugin/advisor/mysql/rule_column_no_null.go
@@ -174,7 +174,7 @@ func (r *ColumnNoNullRule) checkCreateTable(ctx *mysql.CreateTableContext) {
col := columnName{
tableName: tableName,
columnName: column,
- line: r.baseLine + tableElement.GetStart().GetLine(),
+ line: r.baseLine + tableElement.ColumnDefinition().GetStart().GetLine(),
}
if _, exists := r.columnSet[col.name()]; !exists {
r.columnSet[col.name()] = col
diff --git a/backend/plugin/advisor/mysql/rule_column_set_default_for_not_null.go b/backend/plugin/advisor/mysql/rule_column_set_default_for_not_null.go
index ba0d3408fd..f888f63d39 100644
--- a/backend/plugin/advisor/mysql/rule_column_set_default_for_not_null.go
+++ b/backend/plugin/advisor/mysql/rule_column_set_default_for_not_null.go
@@ -153,7 +153,7 @@ func (r *ColumnSetDefaultForNotNullRule) checkCreateTable(ctx *mysql.CreateTable
Code: advisor.NotNullColumnWithNoDefault.Int32(),
Title: r.title,
Content: fmt.Sprintf("Column `%s`.`%s` is NOT NULL but doesn't have DEFAULT", tableName, columnName),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + tableElement.GetStart().GetLine()),
+ StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + tableElement.ColumnDefinition().GetStart().GetLine()),
})
}
}
diff --git a/backend/plugin/advisor/mysql/rule_naming_column.go b/backend/plugin/advisor/mysql/rule_naming_column.go
index 2d55237069..38a4355ee7 100644
--- a/backend/plugin/advisor/mysql/rule_naming_column.go
+++ b/backend/plugin/advisor/mysql/rule_naming_column.go
@@ -123,7 +123,7 @@ func (r *NamingColumnRule) checkCreateTable(ctx *mysql.CreateTableContext) {
}
_, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
- r.handleColumn(tableName, columnName, tableElement.GetStart().GetLine())
+ r.handleColumn(tableName, columnName, tableElement.ColumnDefinition().GetStart().GetLine())
}
}
@@ -161,7 +161,7 @@ func (r *NamingColumnRule) checkAlterTable(ctx *mysql.AlterTableContext) {
continue
}
_, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
- r.handleColumn(tableName, columnName, tableElement.GetStart().GetLine())
+ r.handleColumn(tableName, columnName, tableElement.ColumnDefinition().GetStart().GetLine())
}
default:
}
diff --git a/backend/plugin/advisor/pg/advisor_builtin_prior_backup_check.go b/backend/plugin/advisor/pg/advisor_builtin_prior_backup_check.go
index 566ea5939f..57f4a4a265 100644
--- a/backend/plugin/advisor/pg/advisor_builtin_prior_backup_check.go
+++ b/backend/plugin/advisor/pg/advisor_builtin_prior_backup_check.go
@@ -239,7 +239,7 @@ func (c *ddlChecker) EnterEveryRule(ctx antlr.ParserRuleContext) {
Code: advisor.BuiltinPriorBackupCheck.Int32(),
StartPosition: &storepb.Position{
Line: int32(ctx.GetStart().GetLine()),
- Column: int32(ctx.GetStart().GetColumn()),
+ Column: 0,
},
})
}
diff --git a/backend/plugin/advisor/pg/advisor_collation_allowlist.go b/backend/plugin/advisor/pg/advisor_collation_allowlist.go
index f1f4a40092..c3b9c70b55 100644
--- a/backend/plugin/advisor/pg/advisor_collation_allowlist.go
+++ b/backend/plugin/advisor/pg/advisor_collation_allowlist.go
@@ -174,7 +174,7 @@ func (c *collationAllowlistChecker) addAdvice(collation string, ctx antlr.Parser
Content: fmt.Sprintf("Use disabled collation \"%s\", related statement \"%s\"", collation, text),
StartPosition: &storepb.Position{
Line: int32(ctx.GetStart().GetLine()),
- Column: int32(ctx.GetStart().GetColumn()),
+ Column: 0,
},
})
}
diff --git a/backend/plugin/advisor/pg/advisor_column_comment_convention.go b/backend/plugin/advisor/pg/advisor_column_comment_convention.go
index 3790affc9c..8a4bd534c8 100644
--- a/backend/plugin/advisor/pg/advisor_column_comment_convention.go
+++ b/backend/plugin/advisor/pg/advisor_column_comment_convention.go
@@ -102,7 +102,7 @@ func (c *columnCommentConventionChecker) EnterCreatestmt(ctx *parser.CreatestmtC
schema: "public", // Default schema
table: tableName,
column: columnName,
- line: ctx.GetStart().GetLine(),
+ line: elem.ColumnDef().GetStart().GetLine(),
})
}
}
@@ -134,7 +134,7 @@ func (c *columnCommentConventionChecker) EnterAltertablestmt(ctx *parser.Alterta
schema: "public", // Default schema
table: tableName,
column: columnName,
- line: ctx.GetStart().GetLine(),
+ line: cmd.ColumnDef().GetStart().GetLine(),
})
}
}
diff --git a/backend/plugin/advisor/pg/advisor_column_default_disallow_volatile.go b/backend/plugin/advisor/pg/advisor_column_default_disallow_volatile.go
index 5aefbb3ecb..3b0ef02d34 100644
--- a/backend/plugin/advisor/pg/advisor_column_default_disallow_volatile.go
+++ b/backend/plugin/advisor/pg/advisor_column_default_disallow_volatile.go
@@ -92,7 +92,7 @@ func (c *columnDefaultDisallowVolatileChecker) EnterAltertablestmt(ctx *parser.A
// Check if this column has a volatile DEFAULT
if c.hasVolatileDefault(colDef) {
- c.addColumn("public", tableName, columnName, ctx.GetStart().GetLine())
+ c.addColumn("public", tableName, columnName, colDef.GetStart().GetLine())
}
}
}
diff --git a/backend/plugin/advisor/pg/advisor_column_disallow_changing_type.go b/backend/plugin/advisor/pg/advisor_column_disallow_changing_type.go
index f5d9101722..13cd3e300c 100644
--- a/backend/plugin/advisor/pg/advisor_column_disallow_changing_type.go
+++ b/backend/plugin/advisor/pg/advisor_column_disallow_changing_type.go
@@ -81,7 +81,7 @@ func (c *columnDisallowChangingTypeChecker) EnterAltertablestmt(ctx *parser.Alte
Content: fmt.Sprintf("The statement \"%s\" changes column type", text),
StartPosition: &storepb.Position{
Line: int32(ctx.GetStart().GetLine()),
- Column: int32(ctx.GetStart().GetColumn()),
+ Column: 0,
},
})
break // Only report once per ALTER TABLE statement
diff --git a/backend/plugin/advisor/pg/advisor_column_maximum_character_length.go b/backend/plugin/advisor/pg/advisor_column_maximum_character_length.go
index 4efdf3daa3..d48ee0802c 100644
--- a/backend/plugin/advisor/pg/advisor_column_maximum_character_length.go
+++ b/backend/plugin/advisor/pg/advisor_column_maximum_character_length.go
@@ -83,7 +83,7 @@ func (c *columnMaximumCharacterLengthChecker) EnterCreatestmt(ctx *parser.Create
columnName := pg.NormalizePostgreSQLColid(colDef.Colid())
charLength := c.getCharLength(colDef.Typename())
if charLength > c.maximum {
- c.addAdvice(tableName, columnName, ctx.GetStart().GetLine())
+ c.addAdvice(tableName, columnName, colDef.GetStart().GetLine())
return // Only report first violation
}
}
@@ -125,7 +125,7 @@ func (c *columnMaximumCharacterLengthChecker) EnterAltertablestmt(ctx *parser.Al
columnName := pg.NormalizePostgreSQLColid(colDef.Colid())
charLength := c.getCharLength(colDef.Typename())
if charLength > c.maximum {
- c.addAdvice(tableName, columnName, ctx.GetStart().GetLine())
+ c.addAdvice(tableName, columnName, colDef.GetStart().GetLine())
return
}
}
@@ -139,7 +139,7 @@ func (c *columnMaximumCharacterLengthChecker) EnterAltertablestmt(ctx *parser.Al
columnName := pg.NormalizePostgreSQLColid(allColids[0])
charLength := c.getCharLength(cmd.Typename())
if charLength > c.maximum {
- c.addAdvice(tableName, columnName, ctx.GetStart().GetLine())
+ c.addAdvice(tableName, columnName, cmd.GetStart().GetLine())
return
}
}
diff --git a/backend/plugin/advisor/pg/advisor_column_no_null.go b/backend/plugin/advisor/pg/advisor_column_no_null.go
index 63b0c79a9f..9352376918 100644
--- a/backend/plugin/advisor/pg/advisor_column_no_null.go
+++ b/backend/plugin/advisor/pg/advisor_column_no_null.go
@@ -96,7 +96,7 @@ func (c *columnNoNullChecker) EnterCreatestmt(ctx *parser.CreatestmtContext) {
if colDef.Colid() != nil {
columnName := pg.NormalizePostgreSQLColid(colDef.Colid())
// Add column as nullable by default
- c.addColumn("public", tableName, columnName, elem.GetStart().GetLine())
+ c.addColumn("public", tableName, columnName, colDef.GetStart().GetLine())
// Check column constraints for NOT NULL or PRIMARY KEY
c.removeColumnByColConstraints("public", tableName, colDef)
@@ -131,7 +131,7 @@ func (c *columnNoNullChecker) EnterAltertablestmt(ctx *parser.AltertablestmtCont
colDef := cmd.ColumnDef()
if colDef.Colid() != nil {
columnName := pg.NormalizePostgreSQLColid(colDef.Colid())
- c.addColumn("public", tableName, columnName, ctx.GetStart().GetLine())
+ c.addColumn("public", tableName, columnName, colDef.GetStart().GetLine())
c.removeColumnByColConstraints("public", tableName, colDef)
}
}
@@ -150,7 +150,7 @@ func (c *columnNoNullChecker) EnterAltertablestmt(ctx *parser.AltertablestmtCont
allColids := cmd.AllColid()
if len(allColids) > 0 {
columnName := pg.NormalizePostgreSQLColid(allColids[0])
- c.addColumn("public", tableName, columnName, ctx.GetStart().GetLine())
+ c.addColumn("public", tableName, columnName, cmd.GetStart().GetLine())
}
}
diff --git a/backend/plugin/advisor/pg/advisor_column_require_default.go b/backend/plugin/advisor/pg/advisor_column_require_default.go
index b548639998..7dc6d5f045 100644
--- a/backend/plugin/advisor/pg/advisor_column_require_default.go
+++ b/backend/plugin/advisor/pg/advisor_column_require_default.go
@@ -88,7 +88,7 @@ func (c *columnRequireDefaultChecker) EnterCreatestmt(ctx *parser.CreatestmtCont
Title: c.title,
Content: fmt.Sprintf("Column %q.%q in schema %q doesn't have DEFAULT", tableName, columnName, "public"),
StartPosition: &storepb.Position{
- Line: int32(elem.GetStart().GetLine()),
+ Line: int32(colDef.GetStart().GetLine()),
Column: 0,
},
})
@@ -130,7 +130,7 @@ func (c *columnRequireDefaultChecker) EnterAltertablestmt(ctx *parser.Altertable
Title: c.title,
Content: fmt.Sprintf("Column %q.%q in schema %q doesn't have DEFAULT", tableName, columnName, "public"),
StartPosition: &storepb.Position{
- Line: int32(ctx.GetStart().GetLine()),
+ Line: int32(colDef.GetStart().GetLine()),
Column: 0,
},
})
diff --git a/backend/plugin/advisor/pg/advisor_column_type_disallow_list.go b/backend/plugin/advisor/pg/advisor_column_type_disallow_list.go
index 37293f6fc9..d3150987a4 100644
--- a/backend/plugin/advisor/pg/advisor_column_type_disallow_list.go
+++ b/backend/plugin/advisor/pg/advisor_column_type_disallow_list.go
@@ -93,7 +93,7 @@ func (c *columnTypeDisallowListChecker) EnterCreatestmt(ctx *parser.CreatestmtCo
colDef := elem.ColumnDef()
if colDef.Colid() != nil && colDef.Typename() != nil {
columnName := pg.NormalizePostgreSQLColid(colDef.Colid())
- c.checkType(tableName, columnName, colDef.Typename(), elem.GetStart().GetLine())
+ c.checkType(tableName, columnName, colDef.Typename(), colDef.GetStart().GetLine())
}
}
}
@@ -123,7 +123,7 @@ func (c *columnTypeDisallowListChecker) EnterAltertablestmt(ctx *parser.Altertab
colDef := cmd.ColumnDef()
if colDef.Colid() != nil && colDef.Typename() != nil {
columnName := pg.NormalizePostgreSQLColid(colDef.Colid())
- c.checkType(tableName, columnName, colDef.Typename(), ctx.GetStart().GetLine())
+ c.checkType(tableName, columnName, colDef.Typename(), colDef.GetStart().GetLine())
}
}
@@ -132,7 +132,7 @@ func (c *columnTypeDisallowListChecker) EnterAltertablestmt(ctx *parser.Altertab
allColids := cmd.AllColid()
if len(allColids) > 0 {
columnName := pg.NormalizePostgreSQLColid(allColids[0])
- c.checkType(tableName, columnName, cmd.Typename(), ctx.GetStart().GetLine())
+ c.checkType(tableName, columnName, cmd.Typename(), cmd.GetStart().GetLine())
}
}
}
diff --git a/backend/plugin/advisor/pg/advisor_naming_column.go b/backend/plugin/advisor/pg/advisor_naming_column.go
index 11eada78a2..4db9e4f5c5 100644
--- a/backend/plugin/advisor/pg/advisor_naming_column.go
+++ b/backend/plugin/advisor/pg/advisor_naming_column.go
@@ -92,7 +92,7 @@ func (c *namingColumnConventionChecker) EnterCreatestmt(ctx *parser.CreatestmtCo
colDef := elem.ColumnDef()
if colDef.Colid() != nil {
columnName := pg.NormalizePostgreSQLColid(colDef.Colid())
- c.checkColumnName(tableName, columnName, elem.GetStart().GetLine())
+ c.checkColumnName(tableName, columnName, colDef.GetStart().GetLine())
}
}
}
@@ -122,7 +122,7 @@ func (c *namingColumnConventionChecker) EnterAltertablestmt(ctx *parser.Altertab
colDef := cmd.ColumnDef()
if colDef.Colid() != nil {
columnName := pg.NormalizePostgreSQLColid(colDef.Colid())
- c.checkColumnName(tableName, columnName, cmd.GetStart().GetLine())
+ c.checkColumnName(tableName, columnName, colDef.GetStart().GetLine())
}
}
}
diff --git a/backend/plugin/advisor/pg/advisor_statement_disallow_rm_tbl_cascade.go b/backend/plugin/advisor/pg/advisor_statement_disallow_rm_tbl_cascade.go
index 082ae6d6fe..e519761fe9 100644
--- a/backend/plugin/advisor/pg/advisor_statement_disallow_rm_tbl_cascade.go
+++ b/backend/plugin/advisor/pg/advisor_statement_disallow_rm_tbl_cascade.go
@@ -74,7 +74,7 @@ func (c *statementDisallowRemoveTblCascadeChecker) EnterDropstmt(ctx *parser.Dro
Content: "The use of CASCADE is not permitted when removing a table",
StartPosition: &storepb.Position{
Line: int32(ctx.GetStart().GetLine()),
- Column: int32(ctx.GetStart().GetColumn()),
+ Column: 0,
},
})
}
@@ -95,7 +95,7 @@ func (c *statementDisallowRemoveTblCascadeChecker) EnterTruncatestmt(ctx *parser
Content: "The use of CASCADE is not permitted when removing a table",
StartPosition: &storepb.Position{
Line: int32(ctx.GetStart().GetLine()),
- Column: int32(ctx.GetStart().GetColumn()),
+ Column: 0,
},
})
}
diff --git a/backend/plugin/advisor/pg/test/naming_column.yaml b/backend/plugin/advisor/pg/test/naming_column.yaml
index 476da7f842..7e96c3fe6d 100644
--- a/backend/plugin/advisor/pg/test/naming_column.yaml
+++ b/backend/plugin/advisor/pg/test/naming_column.yaml
@@ -15,6 +15,21 @@
changeType: 1
- statement: CREATE TABLE book(id int, creator_id int)
changeType: 1
+- statement: |-
+ CREATE TABLE book(
+ id int,
+ "creatorId" int
+ )
+ changeType: 1
+ want:
+ - status: 2
+ code: 302
+ title: naming.column
+ content: '"book"."creatorId" mismatches column naming convention, naming format should be "^[a-z]+(_[a-z]+)*$"'
+ startposition:
+ line: 3
+ column: 0
+ endposition: null
- statement: |-
CREATE TABLE book(id int, creator_id int);
ALTER TABLE book ADD COLUMN "creatorId" int