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(advisor): require WHERE on BigQuery UPDATE and DELETE (#21607)
Danny Xuddafe1ea29cf
diff --git a/backend/plugin/advisor/bigquery/bigquery_rules_test.go b/backend/plugin/advisor/bigquery/bigquery_rules_test.gonew file mode 100644index 0000000000..482a900ad5--- /dev/null+++ b/backend/plugin/advisor/bigquery/bigquery_rules_test.go@@ -0,0 +1,18 @@+package bigquery++import (+ "testing"++ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/plugin/advisor"+)++func TestBigQueryRules(t *testing.T) {+ rules := []*storepb.SQLReviewRule{+ {Type: storepb.SQLReviewRule_STATEMENT_WHERE_REQUIRE_UPDATE_DELETE, Level: storepb.SQLReviewRule_WARNING},+ }++ for _, rule := range rules {+ advisor.RunSQLReviewRuleTest(t, rule, storepb.Engine_BIGQUERY, false /* record */)+ }+}diff --git a/backend/plugin/advisor/bigquery/rule_where_require_update_delete.go b/backend/plugin/advisor/bigquery/rule_where_require_update_delete.gonew file mode 100644index 0000000000..cd17bdc59d--- /dev/null+++ b/backend/plugin/advisor/bigquery/rule_where_require_update_delete.go@@ -0,0 +1,103 @@+// Package bigquery is the advisor for BigQuery.+package bigquery++import (+ "context"+ "fmt"+ "strings"++ omniast "github.com/bytebase/omni/googlesql/ast"++ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/plugin/advisor"+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"+ "github.com/bytebase/bytebase/backend/plugin/parser/base"++ // Registers the BigQuery parser so ParseStatements resolves the engine.+ _ "github.com/bytebase/bytebase/backend/plugin/parser/bigquery"+ "github.com/bytebase/bytebase/backend/plugin/parser/googlesql"+)++var (+ _ advisor.Advisor = (*WhereRequireForUpdateDeleteAdvisor)(nil)+)++func init() {+ advisor.Register(storepb.Engine_BIGQUERY, storepb.SQLReviewRule_STATEMENT_WHERE_REQUIRE_UPDATE_DELETE, &WhereRequireForUpdateDeleteAdvisor{})+}++// WhereRequireForUpdateDeleteAdvisor is the advisor checking for WHERE clause requirement for UPDATE and DELETE statement.+type WhereRequireForUpdateDeleteAdvisor struct {+}++// Check checks for WHERE clause requirement.+func (*WhereRequireForUpdateDeleteAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([]*storepb.Advice, error) {+ level, err := advisor.NewStatusBySQLReviewRuleLevel(checkCtx.Rule.Level)+ if err != nil {+ return nil, err+ }++ checker := &whereRequireForUpdateDeleteChecker{+ level: level,+ title: checkCtx.Rule.Type.String(),+ }++ for _, stmt := range checkCtx.ParsedStatements {+ ast, ok := stmt.AST.(*googlesql.AST)+ if !ok || ast.Node == nil {+ continue+ }+ checker.checkStmt(ast.Node, stmt.Text, stmt.BaseLine())+ }++ return checker.adviceList, nil+}++type whereRequireForUpdateDeleteChecker struct {+ adviceList []*storepb.Advice+ level storepb.Advice_Status+ title string+}++// checkStmt flags UPDATE and DELETE statements without a WHERE clause,+// including those nested in scripts (BEGIN ... END, IF, loops). MERGE+// WHEN ... THEN UPDATE/DELETE actions are separate merge nodes and are not+// flagged.+func (c *whereRequireForUpdateDeleteChecker) checkStmt(node omniast.Node, text string, baseLine int) {+ omniast.Inspect(node, func(n omniast.Node) bool {+ switch stmt := n.(type) {+ case *omniast.UpdateStmt:+ if stmt.Where == nil {+ c.addAdvice("UPDATE", text, baseLine, stmt.Loc.Start)+ }+ case *omniast.DeleteStmt:+ if stmt.Where == nil {+ c.addAdvice("DELETE", text, baseLine, stmt.Loc.Start)+ }+ default:+ }+ return true+ })+}++func (c *whereRequireForUpdateDeleteChecker) addAdvice(statementType, text string, baseLine, offset int) {+ c.adviceList = append(c.adviceList, &storepb.Advice{+ Status: c.level,+ Code: code.StatementNoWhere.Int32(),+ Title: c.title,+ Content: fmt.Sprintf("WHERE clause is required for %s statement.", statementType),+ StartPosition: base.ConvertANTLRLineToPosition(baseLine + statementLineForOffset(text, offset)),+ })+}++// statementLineForOffset returns the 1-based line within text that holds the+// byte offset. An unknown (-1) or out-of-range offset clamps to the text bounds.+func statementLineForOffset(text string, offset int) int {+ if offset < 0 {+ offset = 0+ }+ if offset > len(text) {+ offset = len(text)+ }+ return strings.Count(text[:offset], "\n") + 1+}diff --git a/backend/plugin/advisor/bigquery/test/statement_where_require_update_delete.yaml b/backend/plugin/advisor/bigquery/test/statement_where_require_update_delete.yamlnew file mode 100644index 0000000000..e7d1911e6d--- /dev/null+++ b/backend/plugin/advisor/bigquery/test/statement_where_require_update_delete.yaml@@ -0,0 +1,74 @@+- statement: UPDATE `proj.ds.t` SET a = 1;+ changeType: 1+ want:+ - status: 2+ code: 202+ title: STATEMENT_WHERE_REQUIRE_UPDATE_DELETE+ content: WHERE clause is required for UPDATE statement.+ startposition:+ line: 1+ column: 0+ endposition: null+- statement: UPDATE `proj.ds.t` SET a = 1 WHERE b = 2;+ changeType: 1+- statement: UPDATE ds.t SET a = 1 WHERE TRUE;+ changeType: 1+- statement: DELETE FROM `proj.ds.t`;+ changeType: 1+ want:+ - status: 2+ code: 202+ title: STATEMENT_WHERE_REQUIRE_UPDATE_DELETE+ content: WHERE clause is required for DELETE statement.+ startposition:+ line: 1+ column: 0+ endposition: null+- statement: DELETE ds.t;+ changeType: 1+ want:+ - status: 2+ code: 202+ title: STATEMENT_WHERE_REQUIRE_UPDATE_DELETE+ content: WHERE clause is required for DELETE statement.+ startposition:+ line: 1+ column: 0+ endposition: null+- statement: DELETE FROM ds.t WHERE a = 1;+ changeType: 1+- statement: |-+ SELECT 1;+ UPDATE ds.t SET a = 1 WHERE b = 2;+ DELETE FROM ds.t;+ changeType: 1+ want:+ - status: 2+ code: 202+ title: STATEMENT_WHERE_REQUIRE_UPDATE_DELETE+ content: WHERE clause is required for DELETE statement.+ startposition:+ line: 3+ column: 0+ endposition: null+- statement: |-+ MERGE ds.t AS t+ USING ds.s AS s+ ON t.id = s.id+ WHEN MATCHED THEN UPDATE SET a = s.a+ WHEN NOT MATCHED BY SOURCE THEN DELETE;+ changeType: 1+- statement: |-+ BEGIN+ UPDATE ds.t SET a = 1;+ END;+ changeType: 1+ want:+ - status: 2+ code: 202+ title: STATEMENT_WHERE_REQUIRE_UPDATE_DELETE+ content: WHERE clause is required for UPDATE statement.+ startposition:+ line: 2+ column: 0+ endposition: nulldiff --git a/backend/server/ultimate.go b/backend/server/ultimate.goindex d34a113152..17261e8e7d 100644--- a/backend/server/ultimate.go+++ b/backend/server/ultimate.go@@ -47,6 +47,7 @@ import (_ "github.com/bytebase/bytebase/backend/plugin/parser/tsql"// Advisors.+ _ "github.com/bytebase/bytebase/backend/plugin/advisor/bigquery"_ "github.com/bytebase/bytebase/backend/plugin/advisor/mssql"_ "github.com/bytebase/bytebase/backend/plugin/advisor/mysql"_ "github.com/bytebase/bytebase/backend/plugin/advisor/oceanbase"diff --git a/frontend/src/types/sql-review-schema.yaml b/frontend/src/types/sql-review-schema.yamlindex 3683f099c4..2b22e2806e 100644--- a/frontend/src/types/sql-review-schema.yaml+++ b/frontend/src/types/sql-review-schema.yaml@@ -326,6 +326,9 @@- type: STATEMENT_WHERE_REQUIRE_UPDATE_DELETEcategory: STATEMENTengine: MARIADB+- type: STATEMENT_WHERE_REQUIRE_UPDATE_DELETE+ category: STATEMENT+ engine: BIGQUERY- type: STATEMENT_WHERE_NO_LEADING_WILDCARD_LIKEcategory: STATEMENTengine: MYSQLdiff --git a/frontend/src/types/sql-review.dev.yaml b/frontend/src/types/sql-review.dev.yamlindex b65597a6f6..3b4182f830 100644--- a/frontend/src/types/sql-review.dev.yaml+++ b/frontend/src/types/sql-review.dev.yaml@@ -214,6 +214,9 @@ ruleList:- type: STATEMENT_WHERE_REQUIRE_UPDATE_DELETElevel: WARNINGengine: MARIADB+ - type: STATEMENT_WHERE_REQUIRE_UPDATE_DELETE+ level: WARNING+ engine: BIGQUERY- type: STATEMENT_WHERE_NO_LEADING_WILDCARD_LIKElevel: WARNINGengine: MYSQLdiff --git a/frontend/src/types/sql-review.prod.yaml b/frontend/src/types/sql-review.prod.yamlindex 63c2abfb0f..7824d7c5f0 100644--- a/frontend/src/types/sql-review.prod.yaml+++ b/frontend/src/types/sql-review.prod.yaml@@ -237,6 +237,9 @@ ruleList:- type: STATEMENT_WHERE_REQUIRE_UPDATE_DELETElevel: ERRORengine: MARIADB+ - type: STATEMENT_WHERE_REQUIRE_UPDATE_DELETE+ level: ERROR+ engine: BIGQUERY- type: STATEMENT_WHERE_NO_LEADING_WILDCARD_LIKElevel: ERRORengine: MYSQL