From ccdfcb81e76cee3d89e96e5cff611ad8c0923e93 Mon Sep 17 00:00:00 2001 From: Leon Zhang Date: Wed, 12 Dec 2018 09:50:13 +0800 Subject: [PATCH] fix #151 bit type not config as int, when two columns compare will give ARG.003 suggestion. fix #154, #157 by the way --- advisor/heuristic.go | 2 +- advisor/index_test.go | 43 ++++++++++++++++++++++++++++++++++++------- common/config.go | 2 +- env/env.go | 4 ++-- 4 files changed, 40 insertions(+), 11 deletions(-) diff --git a/advisor/heuristic.go b/advisor/heuristic.go index e5e031b..8945f03 100644 --- a/advisor/heuristic.go +++ b/advisor/heuristic.go @@ -293,7 +293,7 @@ func (idxAdv *IndexAdvisor) RuleImplicitConversion() Rule { "date", "time", "datetime", "timestamp", "year", }, sqlparser.IntVal: { - "tinyint", "smallint", "mediumint", "int", "integer", "bigint", "timestamp", "year", + "tinyint", "smallint", "mediumint", "int", "integer", "bigint", "timestamp", "year", "bit", }, sqlparser.FloatVal: { "float", "double", "real", "decimal", diff --git a/advisor/index_test.go b/advisor/index_test.go index 314655f..b667b08 100644 --- a/advisor/index_test.go +++ b/advisor/index_test.go @@ -59,19 +59,27 @@ func TestRuleImplicitConversion(t *testing.T) { `CREATE TABLE t1 (id int, title varchar(255) CHARSET utf8 COLLATE utf8_general_ci);`, `CREATE TABLE t2 (id int, title varchar(255) CHARSET utf8mb4);`, `CREATE TABLE t3 (id int, title varchar(255) CHARSET utf8 COLLATE utf8_bin);`, + `CREATE TABLE t4 (id int, col bit(1));`, } for _, sql := range initSQLs { vEnv.BuildVirtualEnv(rEnv, sql) } - sqls := []string{ - "SELECT * FROM t1 WHERE title >= 60;", - "SELECT * FROM t1, t2 WHERE t1.title = t2.title;", - "SELECT * FROM t1, t3 WHERE t1.title = t3.title;", - "SELECT * FROM t1 WHERE title in (60, '60');", - "SELECT * FROM t1 WHERE title in (60);", + sqls := [][]string{ + { + "SELECT * FROM t1 WHERE title >= 60;", + "SELECT * FROM t1, t2 WHERE t1.title = t2.title;", + "SELECT * FROM t1, t3 WHERE t1.title = t3.title;", + "SELECT * FROM t1 WHERE title in (60, '60');", + "SELECT * FROM t1 WHERE title in (60);", + "SELECT * FROM t4 WHERE col = '1'", + }, + { + // https://github.com/XiaoMi/soar/issues/151 + "SELECT * FROM t4 WHERE col = 1", + }, } - for _, sql := range sqls { + for _, sql := range sqls[0] { stmt, syntaxErr := sqlparser.Parse(sql) if syntaxErr != nil { common.Log.Critical("Syntax Error: %v, SQL: %s", syntaxErr, sql) @@ -91,6 +99,27 @@ func TestRuleImplicitConversion(t *testing.T) { } } } + for _, sql := range sqls[1] { + stmt, syntaxErr := sqlparser.Parse(sql) + if syntaxErr != nil { + common.Log.Critical("Syntax Error: %v, SQL: %s", syntaxErr, sql) + } + + q := &Query4Audit{Query: sql, Stmt: stmt} + + idxAdvisor, err := NewAdvisor(vEnv, *rEnv, *q) + if err != nil { + t.Error("NewAdvisor Error: ", err, "SQL: ", sql) + } + + if idxAdvisor != nil { + rule := idxAdvisor.RuleImplicitConversion() + if rule.Item != "OK" { + t.Error("Rule not match:", rule, "Expect : OK, SQL:", sql) + } + } + } + common.Log.Debug("Exiting function: %s", common.GetFunctionName()) common.Config.OnlineDSN = dsn } diff --git a/common/config.go b/common/config.go index e211956..090d3b6 100644 --- a/common/config.go +++ b/common/config.go @@ -515,7 +515,7 @@ func readCmdFlags() error { // +++++++++++++++日志相关+++++++++++++++++ logLevel := flag.Int("log-level", Config.LogLevel, "LogLevel, 日志级别, [0:Emergency, 1:Alert, 2:Critical, 3:Error, 4:Warning, 5:Notice, 6:Informational, 7:Debug]") logOutput := flag.String("log-output", Config.LogOutput, "LogOutput, 日志输出位置") - reportType := flag.String("report-type", Config.ReportType, "ReportType, 化建议输出格式,目前支持: json, text, markdown, html等") + reportType := flag.String("report-type", Config.ReportType, "ReportType, 优化建议输出格式,目前支持: json, text, markdown, html等") reportCSS := flag.String("report-css", Config.ReportCSS, "ReportCSS, 当 ReportType 为 html 格式时使用的 css 风格,如不指定会提供一个默认风格。CSS可以是本地文件,也可以是一个URL") reportJavascript := flag.String("report-javascript", Config.ReportJavascript, "ReportJavascript, 当 ReportType 为 html 格式时使用的javascript脚本,如不指定默认会加载SQL pretty 使用的 javascript。像CSS一样可以是本地文件,也可以是一个URL") reportTitle := flag.String("report-title", Config.ReportTitle, "ReportTitle, 当 ReportType 为 html 格式时,HTML 的 title") diff --git a/env/env.go b/env/env.go index cbffde9..5a938e6 100644 --- a/env/env.go +++ b/env/env.go @@ -91,11 +91,11 @@ func BuildEnv() (*VirtualEnv, *database.Connector) { } // 检查线上环境可用性版本 - rEnvVersion, err := vEnv.Version() + rEnvVersion, err := conn.Version() common.Config.OnlineDSN.Version = rEnvVersion if err != nil { common.Log.Warn("BuildEnv OnlineDSN: %s:********@%s/%s not available , Error: %s", - vEnv.User, vEnv.Addr, vEnv.Database, err.Error()) + conn.User, conn.Addr, conn.Database, err.Error()) common.Config.TestDSN.Disable = true } -- GitLab