diff --git a/sqle/driver/mysql/audit.go b/sqle/driver/mysql/audit.go index d3e4f09562..bca88177a0 100644 --- a/sqle/driver/mysql/audit.go +++ b/sqle/driver/mysql/audit.go @@ -1177,6 +1177,9 @@ func (i *MysqlDriverImpl) checkInvalidSelect(stmt *ast.SelectStmt) error { // checkUnparsedStmt might add more check in future. func (i *MysqlDriverImpl) checkUnparsedStmt(stmt *ast.UnparsedStmt) error { - i.result.Add(driverV2.RuleLevelWarn, "", "语法错误或者解析器不支持,请人工确认SQL正确性") + if i.cnf.parsingSQLFailureCheckEnable { + parsingRule := rulepkg.RuleHandlerMap[rulepkg.ConfigParsingSQLFailure] + i.result.Add(i.cnf.parsingSQLFailureLevel, rulepkg.ConfigParsingSQLFailure, parsingRule.Message) + } return nil } diff --git a/sqle/driver/mysql/audit_offline_test.go b/sqle/driver/mysql/audit_offline_test.go index 8fe23cf0f0..7ce178dde0 100644 --- a/sqle/driver/mysql/audit_offline_test.go +++ b/sqle/driver/mysql/audit_offline_test.go @@ -2396,7 +2396,7 @@ SELECT * FROM exist_db.exist_tb_1; OPTIMIZE TABLE exist_db.exist_tb_1; SELECT * FROM exist_db.exist_tb_2; `, newTestResult().addResult(rulepkg.DMLCheckWhereIsInvalid), - newTestResult().add(driverV2.RuleLevelWarn, "", "语法错误或者解析器不支持,请人工确认SQL正确性"), + newTestResult().addResult(rulepkg.ConfigParsingSQLFailure), newTestResult().addResult(rulepkg.DMLCheckWhereIsInvalid)) } @@ -2428,7 +2428,7 @@ CREATE `, } { runSingleRuleInspectCase(rulepkg.RuleHandlerMap[rulepkg.DDLCheckCreateTrigger].Rule, t, "", DefaultMysqlInspectOffline(), sql, - newTestResult().add(driverV2.RuleLevelWarn, "", "语法错误或者解析器不支持,请人工确认SQL正确性").addResult(rulepkg.DDLCheckCreateTrigger)) + newTestResult().addResult(rulepkg.ConfigParsingSQLFailure).addResult(rulepkg.DDLCheckCreateTrigger)) } for _, sql := range []string{ @@ -2439,7 +2439,7 @@ CREATE `CREATE TRIGGER my_trigger BEEEFORE INSERT ON t1 FOR EACH ROW insert into t2(id, c1) values(1, '2');`, } { runSingleRuleInspectCase(rulepkg.RuleHandlerMap[rulepkg.DDLCheckCreateTrigger].Rule, t, "", DefaultMysqlInspectOffline(), sql, - newTestResult().add(driverV2.RuleLevelWarn, "", "语法错误或者解析器不支持,请人工确认SQL正确性")) + newTestResult().addResult(rulepkg.ConfigParsingSQLFailure)) } } @@ -2457,7 +2457,7 @@ CREATE `, } { runSingleRuleInspectCase(rulepkg.RuleHandlerMap[rulepkg.DDLCheckCreateFunction].Rule, t, "", DefaultMysqlInspectOffline(), sql, - newTestResult().add(driverV2.RuleLevelWarn, "", "语法错误或者解析器不支持,请人工确认SQL正确性").addResult(rulepkg.DDLCheckCreateFunction)) + newTestResult().addResult(rulepkg.ConfigParsingSQLFailure).addResult(rulepkg.DDLCheckCreateFunction)) } for _, sql := range []string{ @@ -2467,7 +2467,7 @@ CREATE `CREATE DEFINER='sqle_op'@'localhost' hello (s CHAR(20)) RETURNS CHAR(50) DETERMINISTIC RETURN CONCAT('Hello, ',s,'!');`, } { runSingleRuleInspectCase(rulepkg.RuleHandlerMap[rulepkg.DDLCheckCreateFunction].Rule, t, "", DefaultMysqlInspectOffline(), sql, - newTestResult().add(driverV2.RuleLevelWarn, "", "语法错误或者解析器不支持,请人工确认SQL正确性")) + newTestResult().addResult(rulepkg.ConfigParsingSQLFailure)) } } @@ -2514,7 +2514,7 @@ select * from t1;`, runSingleRuleInspectCase( rulepkg.RuleHandlerMap[rulepkg.DDLCheckCreateProcedure].Rule, t, "", DefaultMysqlInspectOffline(), sql, - newTestResult().add(driverV2.RuleLevelWarn, "", "语法错误或者解析器不支持,请人工确认SQL正确性"). + newTestResult().addResult(rulepkg.ConfigParsingSQLFailure). addResult(rulepkg.DDLCheckCreateProcedure)) } @@ -2549,7 +2549,7 @@ end;`, runSingleRuleInspectCase( rulepkg.RuleHandlerMap[rulepkg.DDLCheckCreateProcedure].Rule, t, "", DefaultMysqlInspectOffline(), sql, - newTestResult().add(driverV2.RuleLevelWarn, "", "语法错误或者解析器不支持,请人工确认SQL正确性")) + newTestResult().addResult(rulepkg.ConfigParsingSQLFailure)) } } @@ -3746,7 +3746,7 @@ func TestDDLAvoidEvent(t *testing.T) { ``, DefaultMysqlInspectOffline(), `create event my_event on schedule every 10 second do update schema.table set mycol = mycol + 1;`, - newTestResult().add(driverV2.RuleLevelWarn, "", "语法错误或者解析器不支持,请人工确认SQL正确性").addResult(rulepkg.DDLAvoidEvent)) + newTestResult().addResult(rulepkg.ConfigParsingSQLFailure).addResult(rulepkg.DDLAvoidEvent)) }) t.Run(`create event with DEFINER`, func(t *testing.T) { runSingleRuleInspectCase( @@ -3755,7 +3755,7 @@ func TestDDLAvoidEvent(t *testing.T) { ``, DefaultMysqlInspectOffline(), `create DEFINER=user event my_event on schedule every 10 second do update schema.table set mycol = mycol + 1;`, - newTestResult().add(driverV2.RuleLevelWarn, "", "语法错误或者解析器不支持,请人工确认SQL正确性").addResult(rulepkg.DDLAvoidEvent)) + newTestResult().addResult(rulepkg.ConfigParsingSQLFailure).addResult(rulepkg.DDLAvoidEvent)) }) t.Run(`alter event`, func(t *testing.T) { runSingleRuleInspectCase( @@ -3771,7 +3771,7 @@ func TestDDLAvoidEvent(t *testing.T) { -- 修改事件的具体操作 UPDATE your_table SET your_column = your_value WHERE your_condition; `, - newTestResult().add(driverV2.RuleLevelWarn, "", "语法错误或者解析器不支持,请人工确认SQL正确性").addResult(rulepkg.DDLAvoidEvent)) + newTestResult().addResult(rulepkg.ConfigParsingSQLFailure).addResult(rulepkg.DDLAvoidEvent)) }) t.Run(`alter event with DEFINER`, func(t *testing.T) { runSingleRuleInspectCase( @@ -3787,7 +3787,7 @@ func TestDDLAvoidEvent(t *testing.T) { -- 修改事件的具体操作 UPDATE your_table SET your_column = your_value WHERE your_condition; `, - newTestResult().add(driverV2.RuleLevelWarn, "", "语法错误或者解析器不支持,请人工确认SQL正确性").addResult(rulepkg.DDLAvoidEvent)) + newTestResult().addResult(rulepkg.ConfigParsingSQLFailure).addResult(rulepkg.DDLAvoidEvent)) }) t.Run(`create event with blank line`, func(t *testing.T) { runSingleRuleInspectCase( @@ -3799,7 +3799,7 @@ func TestDDLAvoidEvent(t *testing.T) { create event my_event on schedule every 10 second do update schema.table set mycol = mycol + 1;`, - newTestResult().add(driverV2.RuleLevelWarn, "", "语法错误或者解析器不支持,请人工确认SQL正确性").addResult(rulepkg.DDLAvoidEvent)) + newTestResult().addResult(rulepkg.ConfigParsingSQLFailure).addResult(rulepkg.DDLAvoidEvent)) }) t.Run(`create event with space`, func(t *testing.T) { runSingleRuleInspectCase( @@ -3808,6 +3808,6 @@ func TestDDLAvoidEvent(t *testing.T) { ``, DefaultMysqlInspectOffline(), ` create event my_event on schedule every 10 second do update schema.table set mycol = mycol + 1;`, - newTestResult().add(driverV2.RuleLevelWarn, "", "语法错误或者解析器不支持,请人工确认SQL正确性").addResult(rulepkg.DDLAvoidEvent)) + newTestResult().addResult(rulepkg.ConfigParsingSQLFailure).addResult(rulepkg.DDLAvoidEvent)) }) } diff --git a/sqle/driver/mysql/audit_test.go b/sqle/driver/mysql/audit_test.go index e700a296cd..215bd1d967 100644 --- a/sqle/driver/mysql/audit_test.go +++ b/sqle/driver/mysql/audit_test.go @@ -120,7 +120,10 @@ func NewMockInspectWithIsExecutedSQL(e *executor.Executor) *MysqlDriverImpl { } func runSingleRuleInspectCase(rule driverV2.Rule, t *testing.T, desc string, i *MysqlDriverImpl, sql string, results ...*testResult) { - i.rules = []*driverV2.Rule{&rule} + parsingSQLFailureRule := rulepkg.RuleHandlerMap[rulepkg.ConfigParsingSQLFailure].Rule + i.cnf.parsingSQLFailureCheckEnable = true + i.cnf.parsingSQLFailureLevel = parsingSQLFailureRule.Level + i.rules = []*driverV2.Rule{&parsingSQLFailureRule, &rule} inspectCase(t, desc, i, sql, results...) } @@ -4434,7 +4437,7 @@ SELECT * FROM exist_db.exist_tb_1; OPTIMIZE TABLE exist_db.exist_tb_1; SELECT * FROM exist_db.exist_tb_2; `, newTestResult().addResult(rulepkg.DMLCheckWhereIsInvalid), - newTestResult().add(driverV2.RuleLevelWarn, "", "语法错误或者解析器不支持,请人工确认SQL正确性"), + newTestResult().addResult(rulepkg.ConfigParsingSQLFailure), newTestResult().addResult(rulepkg.DMLCheckWhereIsInvalid)) } @@ -4467,7 +4470,7 @@ CREATE `, } { runSingleRuleInspectCase(rulepkg.RuleHandlerMap[rulepkg.DDLCheckCreateTrigger].Rule, t, "", DefaultMysqlInspect(), sql, - newTestResult().add(driverV2.RuleLevelWarn, "", "语法错误或者解析器不支持,请人工确认SQL正确性").addResult(rulepkg.DDLCheckCreateTrigger)) + newTestResult().addResult(rulepkg.ConfigParsingSQLFailure).addResult(rulepkg.DDLCheckCreateTrigger)) } for _, sql := range []string{ @@ -4480,7 +4483,7 @@ CREATE `AFTER CREATE`, } { runSingleRuleInspectCase(rulepkg.RuleHandlerMap[rulepkg.DDLCheckCreateTrigger].Rule, t, "", DefaultMysqlInspect(), sql, - newTestResult().add(driverV2.RuleLevelWarn, "", "语法错误或者解析器不支持,请人工确认SQL正确性")) + newTestResult().addResult(rulepkg.ConfigParsingSQLFailure)) } } @@ -4498,7 +4501,7 @@ CREATE `, } { runSingleRuleInspectCase(rulepkg.RuleHandlerMap[rulepkg.DDLCheckCreateFunction].Rule, t, "", DefaultMysqlInspect(), sql, - newTestResult().add(driverV2.RuleLevelWarn, "", "语法错误或者解析器不支持,请人工确认SQL正确性").addResult(rulepkg.DDLCheckCreateFunction)) + newTestResult().addResult(rulepkg.ConfigParsingSQLFailure).addResult(rulepkg.DDLCheckCreateFunction)) } for _, sql := range []string{ @@ -4508,7 +4511,7 @@ CREATE `CREATE DEFINER='sqle_op'@'localhost' hello (s CHAR(20)) RETURNS CHAR(50) DETERMINISTIC RETURN CONCAT('Hello, ',s,'!');`, } { runSingleRuleInspectCase(rulepkg.RuleHandlerMap[rulepkg.DDLCheckCreateFunction].Rule, t, "", DefaultMysqlInspect(), sql, - newTestResult().add(driverV2.RuleLevelWarn, "", "语法错误或者解析器不支持,请人工确认SQL正确性")) + newTestResult().addResult(rulepkg.ConfigParsingSQLFailure)) } } @@ -4555,7 +4558,7 @@ select * from t1;`, runSingleRuleInspectCase( rulepkg.RuleHandlerMap[rulepkg.DDLCheckCreateProcedure].Rule, t, "", DefaultMysqlInspect(), sql, - newTestResult().add(driverV2.RuleLevelWarn, "", "语法错误或者解析器不支持,请人工确认SQL正确性"). + newTestResult().addResult(rulepkg.ConfigParsingSQLFailure). addResult(rulepkg.DDLCheckCreateProcedure)) } @@ -4590,7 +4593,7 @@ end;`, runSingleRuleInspectCase( rulepkg.RuleHandlerMap[rulepkg.DDLCheckCreateProcedure].Rule, t, "", DefaultMysqlInspect(), sql, - newTestResult().add(driverV2.RuleLevelWarn, "", "语法错误或者解析器不支持,请人工确认SQL正确性")) + newTestResult().addResult(rulepkg.ConfigParsingSQLFailure)) } } diff --git a/sqle/driver/mysql/mysql.go b/sqle/driver/mysql/mysql.go index ed0d1b0e8b..b0e3b6d3b1 100644 --- a/sqle/driver/mysql/mysql.go +++ b/sqle/driver/mysql/mysql.go @@ -103,6 +103,10 @@ func NewInspect(log *logrus.Entry, cfg *driverV2.Config) (*MysqlDriverImpl, erro if rule.Name == rulepkg.ConfigSQLIsExecuted { inspect.cnf.isExecutedSQL = true } + if rule.Name == rulepkg.ConfigParsingSQLFailure { + inspect.cnf.parsingSQLFailureCheckEnable = true + inspect.cnf.parsingSQLFailureLevel = rule.Level + } } return inspect, nil @@ -515,11 +519,13 @@ type Config struct { DDLOSCMinSize int64 DDLGhostMinSize int64 - optimizeIndexEnabled bool - dmlExplainPreCheckEnable bool - compositeIndexMaxColumn int - indexSelectivityMinValue float64 - isExecutedSQL bool + optimizeIndexEnabled bool + dmlExplainPreCheckEnable bool + compositeIndexMaxColumn int + indexSelectivityMinValue float64 + isExecutedSQL bool + parsingSQLFailureCheckEnable bool + parsingSQLFailureLevel driverV2.RuleLevel } func (i *MysqlDriverImpl) Context() *session.Context { diff --git a/sqle/driver/mysql/rule/rule.go b/sqle/driver/mysql/rule/rule.go index 1153cbdc27..a8a63d2a23 100644 --- a/sqle/driver/mysql/rule/rule.go +++ b/sqle/driver/mysql/rule/rule.go @@ -207,6 +207,7 @@ const ( ConfigOptimizeIndexEnabled = "optimize_index_enabled" ConfigDMLExplainPreCheckEnable = "dml_enable_explain_pre_check" ConfigSQLIsExecuted = "sql_is_executed" + ConfigParsingSQLFailure = "parsing_sql_failure" ) // 计算单位 diff --git a/sqle/driver/mysql/rule/rule_list.go b/sqle/driver/mysql/rule/rule_list.go index edac7cd5c1..b82b2ba8a9 100644 --- a/sqle/driver/mysql/rule/rule_list.go +++ b/sqle/driver/mysql/rule/rule_list.go @@ -2134,4 +2134,15 @@ var RuleHandlers = []RuleHandler{ Message: "禁止char, varchar类型字段字符长度总和超过阈值 %v", Func: checkCharLength, }, + { + Rule: driverV2.Rule{ + Name: ConfigParsingSQLFailure, + Desc: "语法错误或者解析器不支持", + Annotation: "语法错误或者解析器不支持,请人工确认SQL正确性。", + Level: driverV2.RuleLevelError, + Category: RuleTypeGlobalConfig, + }, + Message: "语法错误或者解析器不支持,请人工确认SQL正确性。", + Func: nil, + }, }