Skip to content

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

chore: implement no equal null rule for MySQL (#11018)

boojack43c4b9c34949
diff --git a/backend/plugin/advisor/advisor.go b/backend/plugin/advisor/advisor.go
index 0b0a2f56b6..5507cc052e 100644
--- a/backend/plugin/advisor/advisor.go
+++ b/backend/plugin/advisor/advisor.go
@@ -245,14 +245,21 @@ const (
// MySQLStatementSelectFullTableScan is an advisor type for checking MySQL select full table scan or not.
MySQLStatementSelectFullTableScan Type = "bb.plugin.advisor.mysql.statement.select-full-table-scan"
+ // MySQLStatementWhereNoEqualNull is an advisor type for checking MySQL no equal null in WHERE clause.
+ MySQLStatementWhereNoEqualNull Type = "bb.plugin.advisor.mysql.statement.where.no-equal-null"
+
// MySQLProcedureDisallowCreate is an advisor type for MySQL disallow create procedure.
MySQLProcedureDisallowCreate Type = "bb.plugin.advisor.mysql.procedure.disallow-create"
+
// MySQLEventDisallowCreate is an advisor type for MySQL disallow create event.
MySQLEventDisallowCreate Type = "bb.plugin.advisor.mysql.event.disallow-create"
+
// MySQLViewDisallowCreate is an advisor type for MySQL disallow create view.
MySQLViewDisallowCreate Type = "bb.plugin.advisor.mysql.view.disallow-create"
+
// MySQLFunctionDisallowCreate is an advisor type for MySQL disallow create function.
MySQLFunctionDisallowCreate Type = "bb.plugin.advisor.mysql.function.disallow-create"
+
// MySQLFunctionDisallowedList is an advisor type for MySQL disallowed function list.
MySQLFunctionDisallowedList Type = "bb.plugin.advisor.mysql.function.disallowed-list"
diff --git a/backend/plugin/advisor/code.go b/backend/plugin/advisor/code.go
index 896b818aa3..bbb6ae5d15 100644
--- a/backend/plugin/advisor/code.go
+++ b/backend/plugin/advisor/code.go
@@ -45,6 +45,7 @@ const (
StatementExplainQueryFailed Code = 218
StatementHasUsingFilesort Code = 219
StatementHasUsingTemporary Code = 220
+ StatementWhereNoEqualNull Code = 221
// 301 ~ 399 naming error code
// 301 table naming advisor error code.
diff --git a/backend/plugin/advisor/mysql/advisor_statement_where_no_equal_null.go b/backend/plugin/advisor/mysql/advisor_statement_where_no_equal_null.go
new file mode 100644
index 0000000000..b5ca7aab36
--- /dev/null
+++ b/backend/plugin/advisor/mysql/advisor_statement_where_no_equal_null.go
@@ -0,0 +1,100 @@
+package mysql
+
+import (
+ "fmt"
+
+ "github.com/antlr4-go/antlr/v4"
+ "github.com/pkg/errors"
+
+ mysql "github.com/bytebase/mysql-parser"
+
+ "github.com/bytebase/bytebase/backend/plugin/advisor"
+ mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
+)
+
+var (
+ _ advisor.Advisor = (*StatementWhereNoEqualNullAdvisor)(nil)
+)
+
+func init() {
+ advisor.Register(storepb.Engine_MYSQL, advisor.MySQLStatementWhereNoEqualNull, &StatementWhereNoEqualNullAdvisor{})
+}
+
+type StatementWhereNoEqualNullAdvisor struct {
+}
+
+func (*StatementWhereNoEqualNullAdvisor) Check(ctx advisor.Context, _ string) ([]advisor.Advice, error) {
+ stmtList, ok := ctx.AST.([]*mysqlparser.ParseResult)
+ if !ok {
+ return nil, errors.Errorf("failed to convert to mysql parse result")
+ }
+
+ level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)
+ if err != nil {
+ return nil, err
+ }
+ checker := &statementWhereNoEqualNullChecker{
+ level: level,
+ title: string(ctx.Rule.Type),
+ }
+
+ for _, stmt := range stmtList {
+ checker.baseLine = stmt.BaseLine
+ antlr.ParseTreeWalkerDefault.Walk(checker, stmt.Tree)
+ }
+
+ if len(checker.adviceList) == 0 {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: advisor.Success,
+ Code: advisor.Ok,
+ Title: "OK",
+ Content: "",
+ })
+ }
+ return checker.adviceList, nil
+}
+
+type statementWhereNoEqualNullChecker struct {
+ *mysql.BaseMySQLParserListener
+
+ baseLine int
+ adviceList []advisor.Advice
+ level advisor.Status
+ title string
+ text string
+ isSelect bool
+}
+
+func (checker *statementWhereNoEqualNullChecker) EnterQuery(ctx *mysql.QueryContext) {
+ checker.text = ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx)
+}
+
+func (checker *statementWhereNoEqualNullChecker) EnterSelectStatement(*mysql.SelectStatementContext) {
+ checker.isSelect = true
+}
+
+func (checker *statementWhereNoEqualNullChecker) ExitSelectStatement(*mysql.SelectStatementContext) {
+ checker.isSelect = false
+}
+
+func (checker *statementWhereNoEqualNullChecker) EnterPrimaryExprCompare(ctx *mysql.PrimaryExprCompareContext) {
+ if !checker.isSelect {
+ return
+ }
+
+ compOp := ctx.CompOp()
+ // We only check for equal and not equal.
+ if compOp == nil || (compOp.EQUAL_OPERATOR() == nil && compOp.NOT_EQUAL_OPERATOR() == nil) {
+ return
+ }
+ if ctx.Predicate() != nil && ctx.Predicate().GetText() == "NULL" {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: advisor.StatementWhereNoEqualNull,
+ Title: checker.title,
+ Content: fmt.Sprintf("WHERE clause contains equal null: %s", checker.text),
+ Line: checker.baseLine + ctx.GetStart().GetLine(),
+ })
+ }
+}
diff --git a/backend/plugin/advisor/mysql/mysql_rules_test.go b/backend/plugin/advisor/mysql/mysql_rules_test.go
index 25d26850d5..82c389f345 100644
--- a/backend/plugin/advisor/mysql/mysql_rules_test.go
+++ b/backend/plugin/advisor/mysql/mysql_rules_test.go
@@ -52,7 +52,8 @@ func TestMySQLRules(t *testing.T) {
// advisor.SchemaRuleStatementAffectedRowLimit enforce the UPDATE/DELETE affected row limit.
advisor.SchemaRuleStatementAffectedRowLimit,
// advisor.SchemaRuleStatementDMLDryRun dry run the dml.
- advisor.SchemaRuleStatementDMLDryRun,
+ // advisor.SchemaRuleStatementDMLDryRun,
+ advisor.SchemaRuleStatementWhereNoEqualNull,
// Database related rules.
// advisor.SchemaRuleDropEmptyDatabase enforce the MySQL support check if the database is empty before users drop it.
diff --git a/backend/plugin/advisor/mysql/test/statement_where_no_equal_null.yaml b/backend/plugin/advisor/mysql/test/statement_where_no_equal_null.yaml
new file mode 100644
index 0000000000..c77a57726d
--- /dev/null
+++ b/backend/plugin/advisor/mysql/test/statement_where_no_equal_null.yaml
@@ -0,0 +1,19 @@
+- statement: |
+ SELECT * FROM employee WHERE name = 'Jack' AND id IS NULL;
+ want:
+ - status: SUCCESS
+ title: OK
+- statement: |
+ UPDATE employee SET name = NULL;
+ want:
+ - status: SUCCESS
+ title: OK
+- statement: |
+ SELECT * FROM t WHERE name = 'test' AND id = NULL;
+ want:
+ - status: WARN
+ code: 221
+ title: statement.where.no-equal-null
+ content: |-
+ WHERE clause contains equal null: SELECT * FROM t WHERE name = 'test' AND id = NULL;
+ line: 1
diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.go
index 4caf66a852..bdce346516 100644
--- a/backend/plugin/advisor/sql_review.go
+++ b/backend/plugin/advisor/sql_review.go
@@ -105,6 +105,8 @@ const (
SchemaRuleStatementDisallowUsingFilesort = "statement.disallow-using-filesort"
// SchemaRuleStatementDisallowUsingTemporary disallow using temporary in execution plan.
SchemaRuleStatementDisallowUsingTemporary = "statement.disallow-using-temporary"
+ // SchemaRuleStatementWhereNoEqualNull check the WHERE clause no equal null.
+ SchemaRuleStatementWhereNoEqualNull = "statement.where.no-equal-null"
// SchemaRuleTableRequirePK require the table to have a primary key.
SchemaRuleTableRequirePK SQLReviewRuleType = "table.require-pk"
@@ -1499,6 +1501,10 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine storepb.Engine) (Ty
if engine == storepb.Engine_POSTGRES {
return PostgreSQLStatementCheckSetRoleVariable, nil
}
+ case SchemaRuleStatementWhereNoEqualNull:
+ if engine == storepb.Engine_MYSQL {
+ return MySQLStatementWhereNoEqualNull, nil
+ }
case SchemaRuleCommentLength:
if engine == storepb.Engine_POSTGRES {
return PostgreSQLCommentConvention, nil
diff --git a/backend/plugin/advisor/utils_for_tests.go b/backend/plugin/advisor/utils_for_tests.go
index 6d9a8b91ae..a92e11bbce 100644
--- a/backend/plugin/advisor/utils_for_tests.go
+++ b/backend/plugin/advisor/utils_for_tests.go
@@ -354,6 +354,7 @@ func SetDefaultSQLReviewRulePayload(ruleTp SQLReviewRuleType, dbType storepb.Eng
SchemaRuleCreateIndexConcurrently,
SchemaRuleStatementAddCheckNotValid,
SchemaRuleStatementDisallowAddNotNull,
+ SchemaRuleStatementWhereNoEqualNull,
SchemaRuleIndexTypeNoBlob,
SchemaRuleIdentifierNoKeyword,
SchemaRuleTableNameNoKeyword,