Skip to content

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

chore: implement disallow using functions in where clause (#11081)

boojackf3eeb86d46ac
diff --git a/backend/plugin/advisor/mysql/advisor_statement_where_disallow_using_function.go b/backend/plugin/advisor/mysql/advisor_statement_where_disallow_using_function.go
index f2b2ff92b5..fbdb087638 100644
--- a/backend/plugin/advisor/mysql/advisor_statement_where_disallow_using_function.go
+++ b/backend/plugin/advisor/mysql/advisor_statement_where_disallow_using_function.go
@@ -1,6 +1,8 @@
package mysql
import (
+ "fmt"
+
"github.com/antlr4-go/antlr/v4"
"github.com/pkg/errors"
@@ -53,17 +55,51 @@ func (*StatementWhereDisallowUsingFunctionAdvisor) Check(ctx advisor.Context, _
return checker.adviceList, nil
}
-// TODO(sql-review): implement me please.
type statementWhereDisallowUsingFunctionChecker struct {
*mysql.BaseMySQLParserListener
- baseLine int
- adviceList []advisor.Advice
- level advisor.Status
- title string
- text string
+ baseLine int
+ adviceList []advisor.Advice
+ level advisor.Status
+ title string
+ text string
+ isSelect bool
+ inWhereClause bool
}
func (checker *statementWhereDisallowUsingFunctionChecker) EnterQuery(ctx *mysql.QueryContext) {
checker.text = ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx)
}
+
+func (checker *statementWhereDisallowUsingFunctionChecker) EnterSelectStatement(*mysql.SelectStatementContext) {
+ checker.isSelect = true
+}
+
+func (checker *statementWhereDisallowUsingFunctionChecker) ExitSelectStatement(*mysql.SelectStatementContext) {
+ checker.isSelect = false
+}
+
+func (checker *statementWhereDisallowUsingFunctionChecker) EnterWhereClause(*mysql.WhereClauseContext) {
+ checker.inWhereClause = true
+}
+
+func (checker *statementWhereDisallowUsingFunctionChecker) ExitWhereClause(*mysql.WhereClauseContext) {
+ checker.inWhereClause = false
+}
+
+func (checker *statementWhereDisallowUsingFunctionChecker) EnterFunctionCall(ctx *mysql.FunctionCallContext) {
+ if !checker.isSelect || !checker.inWhereClause {
+ return
+ }
+
+ pi := ctx.PureIdentifier()
+ if pi != nil {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: advisor.DisabledFunction,
+ Title: checker.title,
+ Content: fmt.Sprintf("Function is disallowed in where clause, but \"%s\" uses", 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 8136c75f4b..9e08e894d6 100644
--- a/backend/plugin/advisor/mysql/mysql_rules_test.go
+++ b/backend/plugin/advisor/mysql/mysql_rules_test.go
@@ -59,6 +59,8 @@ func TestMySQLRules(t *testing.T) {
advisor.SchemaRuleStatementMaximumLimitValue,
// advisor.SchemaRuleStatementMaximumJoinTableCount enforces maximum of tables in the joins.
advisor.SchemaRuleStatementMaximumJoinTableCount,
+ // advisor.SchemaRuleStatementWhereDisallowUsingFunction disallow using function in where clause.
+ advisor.SchemaRuleStatementWhereDisallowUsingFunction,
// 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_disallow_using_function.yaml b/backend/plugin/advisor/mysql/test/statement_where_disallow_using_function.yaml
new file mode 100644
index 0000000000..3d84812b69
--- /dev/null
+++ b/backend/plugin/advisor/mysql/test/statement_where_disallow_using_function.yaml
@@ -0,0 +1,9 @@
+- statement: SELECT * FROM employee WHERE id = RAND();
+ want:
+ - status: WARN
+ code: 1702
+ title: statement.where.disallow-using-function
+ content: |-
+ Function is disallowed in where clause, but "SELECT * FROM employee WHERE id = RAND();" uses
+ line: 1
+ details: ""
diff --git a/backend/plugin/advisor/utils_for_tests.go b/backend/plugin/advisor/utils_for_tests.go
index a7c4094f1a..cffd84a9a3 100644
--- a/backend/plugin/advisor/utils_for_tests.go
+++ b/backend/plugin/advisor/utils_for_tests.go
@@ -363,7 +363,8 @@ func SetDefaultSQLReviewRulePayload(ruleTp SQLReviewRuleType, dbType storepb.Eng
SchemaRuleViewDisallowCreate,
SchemaRuleFunctionDisallowCreate,
SchemaRuleStatementCreateSpecifySchema,
- SchemaRuleStatementCheckSetRoleVariable:
+ SchemaRuleStatementCheckSetRoleVariable,
+ SchemaRuleStatementWhereDisallowUsingFunction:
case SchemaRuleTableDropNamingConvention:
payload, err = json.Marshal(NamingRulePayload{
Format: "_delete$",