Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat: support snowflake sql review identifier no keyword (#6655)
h3n4l25876f917702
diff --git a/backend/plugin/advisor/advisor.go b/backend/plugin/advisor/advisor.goindex c047b22233..fdb82133fc 100644--- a/backend/plugin/advisor/advisor.go+++ b/backend/plugin/advisor/advisor.go@@ -412,6 +412,9 @@ const (// SnowflakeWhereRequirement is an advisor type for Snowflake WHERE clause requirement.SnowflakeWhereRequirement Type = "bb.plugin.advisor.snowflake.where.require"+ // SnowflakeIdentifierNamingNoKeyword is an advisor type for Snowflake identifier naming convention without keyword.+ SnowflakeIdentifierNamingNoKeyword Type = "bb.plugin.advisor.snowflake.naming.identifier-no-keyword"+// SnowflakeColumnRequirement is an advisor type for Snowflake column requirement.SnowflakeColumnRequirement Type = "bb.plugin.advisor.snowflake.column.require")diff --git a/backend/plugin/advisor/snowflake/advisor_naming_identifier_no_keyword.go b/backend/plugin/advisor/snowflake/advisor_naming_identifier_no_keyword.gonew file mode 100644index 0000000000..5e76d44f4e--- /dev/null+++ b/backend/plugin/advisor/snowflake/advisor_naming_identifier_no_keyword.go@@ -0,0 +1,140 @@+// Package snowflake is the advisor for snowflake database.+package snowflake++import (+ "fmt"++ "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"+ bbparser "github.com/bytebase/bytebase/backend/plugin/parser/sql"+)++var (+ _ advisor.Advisor = (*NamingIdentifierNoKeywordAdvisor)(nil)+)++func init() {+ advisor.Register(db.Snowflake, advisor.SnowflakeIdentifierNamingNoKeyword, &NamingIdentifierNoKeywordAdvisor{})+}++// NamingIdentifierNoKeywordAdvisor is the advisor checking for identifier naming convention without keyword.+type NamingIdentifierNoKeywordAdvisor struct {+}++// Check checks for identifier naming convention without keyword.+func (*NamingIdentifierNoKeywordAdvisor) 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 := &namingIdentifierNoKeywordChecker{+ level: level,+ title: string(ctx.Rule.Type),+ }++ antlr.ParseTreeWalkerDefault.Walk(listener, tree)++ return listener.generateAdvice()+}++// namingIdentifierNoKeywordChecker is the listener for identifier naming convention without keyword.+type namingIdentifierNoKeywordChecker 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 *namingIdentifierNoKeywordChecker) 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 *namingIdentifierNoKeywordChecker) EnterCreate_table(ctx *parser.Create_tableContext) {+ l.currentOriginalTableName = ctx.Object_name().GetText()+}++// ExitCreate_table is called when production create_table is exited.+func (l *namingIdentifierNoKeywordChecker) ExitCreate_table(*parser.Create_tableContext) {+ l.currentOriginalTableName = ""+}++// EnterCreate_table_as_select is called when production create_table_as_select is entered.+func (l *namingIdentifierNoKeywordChecker) 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 *namingIdentifierNoKeywordChecker) 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 *namingIdentifierNoKeywordChecker) 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 bbparser.IsSnowflakeKeyword(originalColName, false) {+ l.adviceList = append(l.adviceList, advisor.Advice{+ Status: l.level,+ Code: advisor.NameIsKeywordIdentifier,+ Title: l.title,+ Content: fmt.Sprintf("Identifier %s is a keyword and should be avoided", originalID.GetText()),+ Line: ctx.GetStart().GetLine(),+ })+ }+ }+ }+}++// EnterAlter_table is called when production alter_table is entered.+func (l *namingIdentifierNoKeywordChecker) 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 bbparser.IsSnowflakeKeyword(renameToColName, false) {+ l.adviceList = append(l.adviceList, advisor.Advice{+ Status: l.level,+ Code: advisor.NameIsKeywordIdentifier,+ Title: l.title,+ Content: fmt.Sprintf("Identifier %s is a keyword and should be avoided", renameToID.GetText()),+ Line: renameToID.GetStart().GetLine(),+ })+ }+}diff --git a/backend/plugin/advisor/snowflake/snowflake_rules_test.go b/backend/plugin/advisor/snowflake/snowflake_rules_test.goindex 90e9fd2ae0..a7b9e9f402 100644--- a/backend/plugin/advisor/snowflake/snowflake_rules_test.go+++ b/backend/plugin/advisor/snowflake/snowflake_rules_test.go@@ -16,6 +16,7 @@ func TestSnowflakeRules(t *testing.T) {advisor.SchemaRuleColumnMaximumVarcharLength,advisor.SchemaRuleTableNameNoKeyword,advisor.SchemaRuleStatementRequireWhere,+ advisor.SchemaRuleIdentifierNoKeyword,advisor.SchemaRuleRequiredColumn,}diff --git a/backend/plugin/advisor/snowflake/test/naming_identifier_no_keyword.yaml b/backend/plugin/advisor/snowflake/test/naming_identifier_no_keyword.yamlnew file mode 100644index 0000000000..98289af9d7--- /dev/null+++ b/backend/plugin/advisor/snowflake/test/naming_identifier_no_keyword.yaml@@ -0,0 +1,76 @@+- statement: |-+ CREATE TABLE t(+ name VARCHAR,+ "CONNECT" VARCHAR+ );+ want:+ - status: WARN+ code: 308+ title: naming.identifier.no-keyword+ content: Identifier "CONNECT" is a keyword and should be avoided+ line: 2+ details: ""+- statement: |-+ CREATE TABLE t(+ name VARCHAR,+ "connect" VARCHAR+ );+ want:+ - status: WARN+ code: 308+ title: naming.identifier.no-keyword+ content: Identifier "connect" is a keyword and should be avoided+ line: 2+ details: ""+- statement: |-+ CREATE TABLE t(+ name VARCHAR,+ age INT+ );+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+ details: ""+- statement: |-+ CREATE TABLE mytable_copy (+ name INT,+ "connect" INT+ ) AS SELECT a + 1, b + 2 FROM mytable;+ want:+ - status: WARN+ code: 308+ title: naming.identifier.no-keyword+ content: Identifier "connect" is a keyword and should be avoided+ line: 2+ details: ""+- statement: |-+ CREATE TABLE mytable_copy (+ name INT,+ age INT+ ) 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 "connect";+ want:+ - status: WARN+ code: 308+ title: naming.identifier.no-keyword+ content: Identifier "connect" is a keyword and should be avoided+ line: 1+ details: ""+- statement: ALTER TABLE mytable RENAME COLUMN name TO age;+ 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.goindex ef1c9c8d68..51b9b9827f 100644--- a/backend/plugin/advisor/sql_review.go+++ b/backend/plugin/advisor/sql_review.go@@ -883,8 +883,11 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, errreturn SnowflakeTableNamingNoKeyword, nil}case SchemaRuleIdentifierNoKeyword:- if engine == db.Oracle {+ switch engine {+ case db.Oracle:return OracleIdentifierNamingNoKeyword, nil+ case db.Snowflake:+ return SnowflakeIdentifierNamingNoKeyword, nil}case SchemaRuleIdentifierCase:if engine == db.Oracle {