From cb63ad79545539faf3a667eae0ec6c8377d6c9bb Mon Sep 17 00:00:00 2001 From: Leon Zhang Date: Mon, 17 May 2021 14:05:16 +0800 Subject: [PATCH] update soar ARG.003 check rules string -> int can use index --- advisor/heuristic.go | 5 ++++- advisor/index_test.go | 13 ++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/advisor/heuristic.go b/advisor/heuristic.go index 4d7087b..e03ef11 100644 --- a/advisor/heuristic.go +++ b/advisor/heuristic.go @@ -261,7 +261,8 @@ func (idxAdv *IndexAdvisor) RuleImplicitConversion() Rule { common.Log.Debug("DataType: `%s`.`%s` (%s) VS `%s`.`%s` (%s)", colList[0].Table, colList[0].Name, type1, colList[1].Table, colList[1].Name, type2) - if strings.ToLower(type1) != strings.ToLower(type2) { + // case-insensitive check type1, type2 + if !strings.EqualFold(type1, type2) { content = append(content, fmt.Sprintf("`%s`.`%s` (%s) VS `%s`.`%s` (%s) datatype not match", colList[0].Table, colList[0].Name, type1, colList[1].Table, colList[1].Name, type2)) @@ -296,6 +297,8 @@ func (idxAdv *IndexAdvisor) RuleImplicitConversion() Rule { sqlparser.StrVal: { "char", "varchar", "tinytext", "text", "mediumtext", "longtext", "date", "time", "datetime", "timestamp", "year", + "tinyint", "smallint", "mediumint", "int", "integer", "bigint", + "float", "double", "real", "decimal", }, sqlparser.IntVal: { "tinyint", "smallint", "mediumint", "int", "integer", "bigint", diff --git a/advisor/index_test.go b/advisor/index_test.go index 6e755d8..53b061e 100644 --- a/advisor/index_test.go +++ b/advisor/index_test.go @@ -83,6 +83,7 @@ func TestRuleImplicitConversion(t *testing.T) { } sqls := [][]string{ + // ARG.003 { "SELECT * FROM t1 WHERE title >= 60;", "SELECT * FROM t1, t2 WHERE t1.title = t2.title;", @@ -90,11 +91,13 @@ func TestRuleImplicitConversion(t *testing.T) { "SELECT * FROM t1 WHERE title in (60, '60');", "SELECT * FROM t1 WHERE title in (60);", "SELECT * FROM t1 WHERE title in (60, 60);", - "SELECT * FROM t4 WHERE col = '1'", + "SELECT * FROM t1 WHERE title = 1", }, + // OK { - // https://github.com/XiaoMi/soar/issues/151 - "SELECT * FROM t4 WHERE col = 1", + "SELECT * FROM t1 WHERE id = '1'", // string -> int can use index + "SELECT * FROM t1 WHERE id = 1", + "SELECT * FROM t4 WHERE col = 1", // https://github.com/XiaoMi/soar/issues/151 "SELECT * FROM sakila.film WHERE rental_rate > 1", }, } @@ -114,7 +117,7 @@ func TestRuleImplicitConversion(t *testing.T) { if idxAdvisor != nil { rule := idxAdvisor.RuleImplicitConversion() if rule.Item != "ARG.003" { - t.Error("Rule not match:", rule, "Expect : ARG.003, SQL:", sql) + t.Error("Rule not match:", rule.Item, "Expect : ARG.003, SQL:", sql) } } } @@ -134,7 +137,7 @@ func TestRuleImplicitConversion(t *testing.T) { if idxAdvisor != nil { rule := idxAdvisor.RuleImplicitConversion() if rule.Item != "OK" { - t.Error("Rule not match:", rule, "Expect : OK, SQL:", sql) + t.Error("Rule not match:", rule.Item, "Expect : OK, SQL:", sql) } } } -- GitLab