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(sql-review, snowflake): add sql review rule statement select no select all for snowflake (#7004)
h3n4l9e893affd8a9
diff --git a/backend/plugin/advisor/advisor.go b/backend/plugin/advisor/advisor.goindex 8d3bcd7748..7b07c6db0e 100644--- a/backend/plugin/advisor/advisor.go+++ b/backend/plugin/advisor/advisor.go@@ -426,6 +426,9 @@ const (// SnowflakeColumnNoNull is an advisor type for Snowflake column no NULL value.SnowflakeColumnNoNull Type = "bb.plugin.advisor.snowflake.column.no-null"++ // SnowflakeNoSelectAll is an advisor type for Snowflake no select all.+ SnowflakeNoSelectAll Type = "bb.plugin.advisor.snowflake.select.no-select-all")// Advice is the result of an advisor.diff --git a/backend/plugin/advisor/generator/snowflake.template b/backend/plugin/advisor/generator/snowflake.templateindex 4f121d132e..9cada1d59c 100644--- a/backend/plugin/advisor/generator/snowflake.template+++ b/backend/plugin/advisor/generator/snowflake.template@@ -4,6 +4,7 @@ package snowflakeimport ("github.com/antlr4-go/antlr/v4"parser "github.com/bytebase/snowsql-parser"+ "github.com/pkg/errors""github.com/bytebase/bytebase/backend/plugin/advisor""github.com/bytebase/bytebase/backend/plugin/advisor/db"diff --git a/backend/plugin/advisor/snowflake/advisor_select_no_select_all.go b/backend/plugin/advisor/snowflake/advisor_select_no_select_all.gonew file mode 100644index 0000000000..251845ba42--- /dev/null+++ b/backend/plugin/advisor/snowflake/advisor_select_no_select_all.go@@ -0,0 +1,83 @@+// Package snowflake is the advisor for snowflake database.+package snowflake++import (+ "github.com/antlr4-go/antlr/v4"+ parser "github.com/bytebase/snowsql-parser"+ "github.com/pkg/errors"++ "github.com/bytebase/bytebase/backend/plugin/advisor"+ "github.com/bytebase/bytebase/backend/plugin/advisor/db"+)++var (+ _ advisor.Advisor = (*SelectNoSelectAllAdvisor)(nil)+)++func init() {+ advisor.Register(db.Snowflake, advisor.SnowflakeNoSelectAll, &SelectNoSelectAllAdvisor{})+}++// SelectNoSelectAllAdvisor is the advisor checking for no select all.+type SelectNoSelectAllAdvisor struct {+}++// Check checks for no select all.+func (*SelectNoSelectAllAdvisor) Check(ctx advisor.Context, _ string) ([]advisor.Advice, error) {+ tree, ok := ctx.AST.(antlr.Tree)+ if !ok {+ return nil, errors.Errorf("failed to convert to Tree")+ }++ level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)+ if err != nil {+ return nil, err+ }++ listener := &selectNoSelectAllChecker{+ level: level,+ title: string(ctx.Rule.Type),+ }++ antlr.ParseTreeWalkerDefault.Walk(listener, tree)++ return listener.generateAdvice()+}++// selectNoSelectAllChecker is the listener for no select all.+type selectNoSelectAllChecker struct {+ *parser.BaseSnowflakeParserListener++ level advisor.Status+ title string++ adviceList []advisor.Advice+}++// generateAdvice returns the advices generated by the listener, the advices must not be empty.+func (l *selectNoSelectAllChecker) 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+}++// EnterSelect_list_elem enters the select_list_elem parser rule.+func (l *selectNoSelectAllChecker) EnterSelect_list_elem(ctx *parser.Select_list_elemContext) {+ if v := ctx.Column_elem(); v != nil {+ if v.STAR() != nil {+ l.adviceList = append(l.adviceList, advisor.Advice{+ Status: l.level,+ Code: advisor.StatementSelectAll,+ Title: l.title,+ Content: "Avoid using SELECT *.",+ Line: v.STAR().GetSymbol().GetLine(),+ })+ }+ }+}diff --git a/backend/plugin/advisor/snowflake/snowflake_rules_test.go b/backend/plugin/advisor/snowflake/snowflake_rules_test.goindex 70686a35e4..b2efb124a3 100644--- a/backend/plugin/advisor/snowflake/snowflake_rules_test.go+++ b/backend/plugin/advisor/snowflake/snowflake_rules_test.go@@ -20,6 +20,7 @@ func TestSnowflakeRules(t *testing.T) {advisor.SchemaRuleRequiredColumn,advisor.SchemaRuleIdentifierCase,advisor.SchemaRuleColumnNotNull,+ advisor.SchemaRuleStatementNoSelectAll,}for _, rule := range snowflakeRules {diff --git a/backend/plugin/advisor/snowflake/test/statement_select_no_select_all.yaml b/backend/plugin/advisor/snowflake/test/statement_select_no_select_all.yamlnew file mode 100644index 0000000000..e0c5ddbd9c--- /dev/null+++ b/backend/plugin/advisor/snowflake/test/statement_select_no_select_all.yaml@@ -0,0 +1,32 @@+- statement: SELECT * FROM t+ want:+ - status: WARN+ code: 203+ title: statement.select.no-select-all+ content: Avoid using SELECT *.+ line: 1+ details: ""+- statement: SELECT a, b FROM t+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+ details: ""+- statement: SELECT a, b FROM (SELECT * from t1, t2) t+ want:+ - status: WARN+ code: 203+ title: statement.select.no-select-all+ content: Avoid using SELECT *.+ line: 1+ details: ""+- statement: INSERT INTO t SELECT * FROM t1+ want:+ - status: WARN+ code: 203+ title: statement.select.no-select-all+ content: Avoid using SELECT *.+ line: 1+ details: ""diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.goindex c0897c2a05..20c1ed9258 100644--- a/backend/plugin/advisor/sql_review.go+++ b/backend/plugin/advisor/sql_review.go@@ -1040,6 +1040,8 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, errreturn PostgreSQLNoSelectAll, nilcase db.Oracle:return OracleNoSelectAll, nil+ case db.Snowflake:+ return SnowflakeNoSelectAll, nil}case SchemaRuleSchemaBackwardCompatibility:switch engine {