From e05ef2adc3b5410a42e875fe9317c7b537a5d0e8 Mon Sep 17 00:00:00 2001 From: taolx0 Date: Tue, 2 Jan 2024 11:25:43 +0800 Subject: [PATCH] fix type check bug --- sqle/driver/mysql/audit_test.go | 4 ++++ sqle/driver/mysql/rule/rule.go | 8 +++++++ sqle/driver/mysql/session/mock_context.go | 26 +++++++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/sqle/driver/mysql/audit_test.go b/sqle/driver/mysql/audit_test.go index 827ed62907..11b55f7daf 100644 --- a/sqle/driver/mysql/audit_test.go +++ b/sqle/driver/mysql/audit_test.go @@ -3061,6 +3061,10 @@ func TestCheckMultiSelectWhereExistImplicitConversion(t *testing.T) { `select t1.v1 from exist_db.exist_tb_1 t1, exist_db.exist_tb_9 t2 where t2.v1 in (3)`, `select t1.v1 from exist_db.exist_tb_1 t1, exist_db.exist_tb_9 where exist_tb_9.v1 in (3);`, `select t1.v1 from exist_db.exist_tb_1 t1, exist_db.exist_tb_9 where exist_tb_9.v1 in (3, 2, 1);`, + + `select * from exist_db.exist_tb_11 where create_time = '2020-01-01 00:00:00'`, + `select * from exist_db.exist_tb_11 where create_time >= 2020-01-01`, + `select * from exist_db.exist_tb_11 where year_time >= '2020'`, } { runSingleRuleInspectCase(rule, t, "multi select: check where exist implicit conversion", DefaultMysqlInspect(), sql, newTestResult()) } diff --git a/sqle/driver/mysql/rule/rule.go b/sqle/driver/mysql/rule/rule.go index dc1448ee36..43985db851 100644 --- a/sqle/driver/mysql/rule/rule.go +++ b/sqle/driver/mysql/rule/rule.go @@ -4949,6 +4949,14 @@ func checkWhereColumnImplicitConversionFunc(ctx *session.Context, rule driverV2. if col.Name.Name.L != cn.Name.L { continue } + + // datetime, date, timestamp, time, year 类型的列不做检查 + // 因为这些类型的列不会发生隐式转换,mysql可以自动识别各种日期格式 + switch col.Tp.Tp { + case mysql.TypeDatetime, mysql.TypeDate, mysql.TypeTimestamp, mysql.TypeDuration, mysql.TypeYear: + continue + } + for _, v := range values { if !checkColumnTypeIsMatch(v, col.Tp.Tp) { addResult(res, rule, DMLCheckWhereExistImplicitConversion) diff --git a/sqle/driver/mysql/session/mock_context.go b/sqle/driver/mysql/session/mock_context.go index 4ce9646e2b..750ddde862 100644 --- a/sqle/driver/mysql/session/mock_context.go +++ b/sqle/driver/mysql/session/mock_context.go @@ -85,6 +85,12 @@ func NewMockContext(e *executor.Executor) *Context { Size: 1, OriginalTable: getTestCreateTableStmt10(), }, + "exist_tb_11": { + sizeLoad: true, + isLoad: true, + Size: 1, + OriginalTable: getTestCreateTableStmt11(), + }, }, }, "myisam_utf8_db": { @@ -386,3 +392,23 @@ PRIMARY KEY (id) USING BTREE stmt, _ := node.(*ast.CreateTableStmt) return stmt } + +func getTestCreateTableStmt11() *ast.CreateTableStmt { + baseCreateQuery := ` +CREATE TABLE exist_db.exist_tb_11 ( +id bigint(10) unsigned NOT NULL AUTO_INCREMENT COMMENT "unit test", +create_time datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP, +upgrade_time timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, +year_time year(4) NOT NULL DEFAULT '2020', +data_time date NOT NULL DEFAULT '2020-01-01 00:00:00', +data_time2 TIME NOT NULL DEFAULT '12:00:00', +PRIMARY KEY (id) USING BTREE +)ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COMMENT="unit test"; +` + node, err := util.ParseOneSql(baseCreateQuery) + if err != nil { + panic(err) + } + stmt, _ := node.(*ast.CreateTableStmt) + return stmt +}