Skip to content

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

feat(sql-review): support snowflake sql review identifier case (#6665)

h3n4lfc8d3ddd1c13
diff --git a/backend/plugin/advisor/advisor.go b/backend/plugin/advisor/advisor.go
index fdb82133fc..395207e9d9 100644
--- a/backend/plugin/advisor/advisor.go
+++ b/backend/plugin/advisor/advisor.go
@@ -417,6 +417,9 @@ const (
// SnowflakeColumnRequirement is an advisor type for Snowflake column requirement.
SnowflakeColumnRequirement Type = "bb.plugin.advisor.snowflake.column.require"
+
+ // SnowflakeIdentifierCase is an advisor type for Snowflake identifier case.
+ SnowflakeIdentifierCase Type = "bb.plugin.advisor.snowflake.naming.identifier-case"
)
// Advice is the result of an advisor.
diff --git a/backend/plugin/advisor/snowflake/advisor_naming_identifier_case.go b/backend/plugin/advisor/snowflake/advisor_naming_identifier_case.go
new file mode 100644
index 0000000000..e126aa3d4e
--- /dev/null
+++ b/backend/plugin/advisor/snowflake/advisor_naming_identifier_case.go
@@ -0,0 +1,141 @@
+// Package snowflake is the advisor for snowflake database.
+package snowflake
+
+import (
+ "fmt"
+ "strings"
+
+ "github.com/antlr4-go/antlr/v4"
+ parser "github.com/bytebase/snowsql-parser"
+
+ "github.com/bytebase/bytebase/backend/plugin/advisor"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/db"
+)
+
+var (
+ _ advisor.Advisor = (*NamingIdentifierCaseAdvisor)(nil)
+)
+
+func init() {
+ advisor.Register(db.Snowflake, advisor.SnowflakeIdentifierCase, &NamingIdentifierCaseAdvisor{})
+}
+
+// NamingIdentifierCaseAdvisor is the advisor checking for identifier case.
+type NamingIdentifierCaseAdvisor struct {
+}
+
+// Check checks for identifier case.
+func (*NamingIdentifierCaseAdvisor) Check(ctx advisor.Context, statement string) ([]advisor.Advice, error) {
+ tree, errAdvice := parseStatement(statement)
+ if errAdvice != nil {
+ return errAdvice, nil
+ }
+
+ level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)
+ if err != nil {
+ return nil, err
+ }
+
+ listener := &namingIdentifierCaseChecker{
+ level: level,
+ title: string(ctx.Rule.Type),
+ currentOriginalTableName: "",
+ }
+
+ antlr.ParseTreeWalkerDefault.Walk(listener, tree)
+
+ return listener.generateAdvice()
+}
+
+// namingIdentifierCaseChecker is the listener for identifier case.
+type namingIdentifierCaseChecker struct {
+ *parser.BaseSnowflakeParserListener
+
+ level advisor.Status
+ title string
+
+ // currentOriginalTableName is the original table name in the statement.
+ currentOriginalTableName string
+
+ adviceList []advisor.Advice
+}
+
+// generateAdvice returns the advices generated by the listener, the advices must not be empty.
+func (l *namingIdentifierCaseChecker) generateAdvice() ([]advisor.Advice, error) {
+ if len(l.adviceList) == 0 {
+ l.adviceList = append(l.adviceList, advisor.Advice{
+ Status: advisor.Success,
+ Code: advisor.Ok,
+ Title: "OK",
+ Content: "",
+ })
+ }
+ return l.adviceList, nil
+}
+
+// EnterCreate_table is called when production create_table is entered.
+func (l *namingIdentifierCaseChecker) EnterCreate_table(ctx *parser.Create_tableContext) {
+ l.currentOriginalTableName = ctx.Object_name().GetText()
+}
+
+// ExitCreate_table is called when production create_table is exited.
+func (l *namingIdentifierCaseChecker) ExitCreate_table(*parser.Create_tableContext) {
+ l.currentOriginalTableName = ""
+}
+
+// EnterCreate_table_as_select is called when production create_table_as_select is entered.
+func (l *namingIdentifierCaseChecker) EnterCreate_table_as_select(ctx *parser.Create_table_as_selectContext) {
+ l.currentOriginalTableName = ctx.Object_name().GetText()
+}
+
+// ExitCreate_table_as_select is called when production create_table_as_select is exited.
+func (l *namingIdentifierCaseChecker) ExitCreate_table_as_select(*parser.Create_table_as_selectContext) {
+ l.currentOriginalTableName = ""
+}
+
+// EnterColumn_decl_item_list is called when production column_decl_item_list is entered.
+func (l *namingIdentifierCaseChecker) EnterColumn_decl_item_list(ctx *parser.Column_decl_item_listContext) {
+ if l.currentOriginalTableName == "" {
+ return
+ }
+
+ allItems := ctx.AllColumn_decl_item()
+ if len(allItems) == 0 {
+ return
+ }
+
+ for _, item := range allItems {
+ if fullColDecl := item.Full_col_decl(); fullColDecl != nil {
+ originalID := fullColDecl.Col_decl().Column_name().Id_()
+ originalColName := normalizeObjectNamePart(originalID)
+ if strings.ToUpper(originalColName) != originalColName {
+ l.adviceList = append(l.adviceList, advisor.Advice{
+ Status: l.level,
+ Code: advisor.NamingCaseMismatch,
+ Title: l.title,
+ Content: fmt.Sprintf("Identifier %q should be upper case", originalColName),
+ Line: ctx.GetStart().GetLine(),
+ })
+ }
+ }
+ }
+}
+
+// EnterAlter_table is called when production alter_table is entered.
+func (l *namingIdentifierCaseChecker) EnterAlter_table(ctx *parser.Alter_tableContext) {
+ if ctx.Table_column_action() == nil || ctx.Table_column_action().RENAME() == nil {
+ return
+ }
+ l.currentOriginalTableName = ctx.Object_name(0).GetText()
+ renameToID := ctx.Table_column_action().Column_name(1).Id_()
+ renameToColName := normalizeObjectNamePart(renameToID)
+ if strings.ToUpper(renameToColName) != renameToColName {
+ l.adviceList = append(l.adviceList, advisor.Advice{
+ Status: l.level,
+ Code: advisor.NamingCaseMismatch,
+ Title: l.title,
+ Content: fmt.Sprintf("Identifier %q should be upper case", renameToColName),
+ Line: renameToID.GetStart().GetLine(),
+ })
+ }
+}
diff --git a/backend/plugin/advisor/snowflake/snowflake_rules_test.go b/backend/plugin/advisor/snowflake/snowflake_rules_test.go
index a7b9e9f402..ea0dc889df 100644
--- a/backend/plugin/advisor/snowflake/snowflake_rules_test.go
+++ b/backend/plugin/advisor/snowflake/snowflake_rules_test.go
@@ -18,6 +18,7 @@ func TestSnowflakeRules(t *testing.T) {
advisor.SchemaRuleStatementRequireWhere,
advisor.SchemaRuleIdentifierNoKeyword,
advisor.SchemaRuleRequiredColumn,
+ advisor.SchemaRuleIdentifierCase,
}
for _, rule := range snowflakeRules {
diff --git a/backend/plugin/advisor/snowflake/test/naming_identifier_case.yaml b/backend/plugin/advisor/snowflake/test/naming_identifier_case.yaml
new file mode 100644
index 0000000000..95dbd0efdf
--- /dev/null
+++ b/backend/plugin/advisor/snowflake/test/naming_identifier_case.yaml
@@ -0,0 +1,73 @@
+- statement: |-
+ -- The rule should be friendly with engine behavior(i.e. stored and resolved as uppercase characters if not quoted)
+ CREATE TABLE t(id INT);
+ want:
+ - status: SUCCESS
+ code: 0
+ title: OK
+ content: ""
+ line: 0
+ details: ""
+- statement: CREATE TABLE t(ID INT);
+ want:
+ - status: SUCCESS
+ code: 0
+ title: OK
+ content: ""
+ line: 0
+ details: ""
+- statement: |-
+ CREATE TABLE t(
+ name VARCHAR,
+ age INT,
+ "address" VARCHAR
+ );
+ want:
+ - status: WARN
+ code: 309
+ title: naming.identifier.case
+ content: Identifier "address" should be upper case
+ line: 2
+ details: ""
+- statement: |-
+ CREATE TABLE mytable_copy (
+ name INT,
+ age INT,
+ "address" VARCHAR
+ ) AS SELECT a + 1, b + 2 FROM mytable;
+ want:
+ - status: WARN
+ code: 309
+ title: naming.identifier.case
+ content: Identifier "address" should be upper case
+ line: 2
+ details: ""
+- statement: |-
+ CREATE TABLE mytable_copy (
+ name INT,
+ age INT,
+ address VARCHAR
+ ) AS SELECT a + 1, b + 2 FROM mytable;
+ want:
+ - status: SUCCESS
+ code: 0
+ title: OK
+ content: ""
+ line: 0
+ details: ""
+- statement: ALTER TABLE mytable RENAME COLUMN name TO "address";
+ want:
+ - status: WARN
+ code: 309
+ title: naming.identifier.case
+ content: Identifier "address" should be upper case
+ line: 1
+ details: ""
+- statement: ALTER TABLE mytable RENAME COLUMN name TO address;
+ want:
+ - status: SUCCESS
+ code: 0
+ title: OK
+ content: ""
+ line: 0
+ details: ""
diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.go
index 51b9b9827f..ea921860a3 100644
--- a/backend/plugin/advisor/sql_review.go
+++ b/backend/plugin/advisor/sql_review.go
@@ -890,8 +890,11 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, err
return SnowflakeIdentifierNamingNoKeyword, nil
}
case SchemaRuleIdentifierCase:
- if engine == db.Oracle {
+ switch engine {
+ case db.Oracle:
return OracleIdentifierCase, nil
+ case db.Snowflake:
+ return SnowflakeIdentifierCase, nil
}
case SchemaRuleRequiredColumn:
switch engine {