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

fix type check bug #2196

Merged
merged 1 commit into from
Jan 2, 2024
Merged
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
4 changes: 4 additions & 0 deletions sqle/driver/mysql/audit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
8 changes: 8 additions & 0 deletions sqle/driver/mysql/rule/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 26 additions & 0 deletions sqle/driver/mysql/session/mock_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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
}
Loading