Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 2460 #2466

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion sqle/driver/mysql/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
26 changes: 13 additions & 13 deletions sqle/driver/mysql/audit_offline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down Expand Up @@ -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{
Expand All @@ -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))
}
}

Expand All @@ -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{
Expand All @@ -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))
}
}

Expand Down Expand Up @@ -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))
}

Expand Down Expand Up @@ -2549,7 +2549,7 @@ end;`,
runSingleRuleInspectCase(
rulepkg.RuleHandlerMap[rulepkg.DDLCheckCreateProcedure].Rule, t, "",
DefaultMysqlInspectOffline(), sql,
newTestResult().add(driverV2.RuleLevelWarn, "", "语法错误或者解析器不支持,请人工确认SQL正确性"))
newTestResult().addResult(rulepkg.ConfigParsingSQLFailure))
}
}

Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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))
})
}
19 changes: 11 additions & 8 deletions sqle/driver/mysql/audit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}

Expand Down Expand Up @@ -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))
}

Expand Down Expand Up @@ -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{
Expand All @@ -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))
}
}

Expand All @@ -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{
Expand All @@ -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))
}
}

Expand Down Expand Up @@ -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))
}

Expand Down Expand Up @@ -4590,7 +4593,7 @@ end;`,
runSingleRuleInspectCase(
rulepkg.RuleHandlerMap[rulepkg.DDLCheckCreateProcedure].Rule, t, "",
DefaultMysqlInspect(), sql,
newTestResult().add(driverV2.RuleLevelWarn, "", "语法错误或者解析器不支持,请人工确认SQL正确性"))
newTestResult().addResult(rulepkg.ConfigParsingSQLFailure))
}
}

Expand Down
16 changes: 11 additions & 5 deletions sqle/driver/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions sqle/driver/mysql/rule/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ const (
ConfigOptimizeIndexEnabled = "optimize_index_enabled"
ConfigDMLExplainPreCheckEnable = "dml_enable_explain_pre_check"
ConfigSQLIsExecuted = "sql_is_executed"
ConfigParsingSQLFailure = "parsing_sql_failure"
)

// 计算单位
Expand Down
11 changes: 11 additions & 0 deletions sqle/driver/mysql/rule/rule_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
Loading