From b57106b506bd2c9c528466179b37e81ebca05950 Mon Sep 17 00:00:00 2001 From: Leon Zhang Date: Tue, 8 Jan 2019 22:08:29 +0800 Subject: [PATCH] fix #173 with JSONFind WHERE col = col = '' and col1 = 'xx' --- advisor/heuristic.go | 42 ++++++++++----------------------------- advisor/heuristic_test.go | 3 +++ 2 files changed, 14 insertions(+), 31 deletions(-) diff --git a/advisor/heuristic.go b/advisor/heuristic.go index 3485c07..a1e6152 100644 --- a/advisor/heuristic.go +++ b/advisor/heuristic.go @@ -31,6 +31,7 @@ import ( "github.com/percona/go-mysql/query" tidb "github.com/pingcap/parser/ast" "github.com/pingcap/parser/mysql" + "github.com/tidwall/gjson" "vitess.io/vitess/go/vt/sqlparser" ) @@ -1312,37 +1313,16 @@ func (q *Query4Audit) RuleLoadFile() Rule { func (q *Query4Audit) RuleMultiCompare() Rule { var rule = q.RuleOK() if q.TiStmt != nil { - for _, tiStmt := range q.TiStmt { - switch node := tiStmt.(type) { - case *tidb.SelectStmt: - switch where := node.Where.(type) { - case *tidb.BinaryOperationExpr: - switch where.L.(type) { - case *tidb.BinaryOperationExpr: - if where.Op.String() == "eq" { - rule = HeuristicRules["RES.009"] - } - } - } - case *tidb.UpdateStmt: - switch where := node.Where.(type) { - case *tidb.BinaryOperationExpr: - switch where.L.(type) { - case *tidb.BinaryOperationExpr: - if where.Op.String() == "eq" { - rule = HeuristicRules["RES.009"] - } - } - } - case *tidb.DeleteStmt: - switch where := node.Where.(type) { - case *tidb.BinaryOperationExpr: - switch where.L.(type) { - case *tidb.BinaryOperationExpr: - if where.Op.String() == "eq" { - rule = HeuristicRules["RES.009"] - } - } + json := ast.StmtNode2JSON(q.Query, "", "") + whereJSON := common.JSONFind(json, "Where") + for _, where := range whereJSON { + conds := []string{where} + conds = append(conds, common.JSONFind(where, "L")...) + conds = append(conds, common.JSONFind(where, "R")...) + for _, cond := range conds { + if gjson.Get(cond, "Op").Int() == 7 && gjson.Get(cond, "L.Op").Int() == 7 { + rule = HeuristicRules["RES.009"] + return rule } } } diff --git a/advisor/heuristic_test.go b/advisor/heuristic_test.go index e163f23..be33590 100644 --- a/advisor/heuristic_test.go +++ b/advisor/heuristic_test.go @@ -946,6 +946,9 @@ func TestRuleMultiCompare(t *testing.T) { sqls := [][]string{ { "SELECT * FROM tbl WHERE col = col = 'abc'", + "SELECT * FROM tbl WHERE col = 'def' and col = col = 'abc'", + "SELECT * FROM tbl WHERE col = 'def' or col = col = 'abc'", + "SELECT * FROM tbl WHERE col = col = 'abc' and col = 'def'", "UPDATE tbl set col = 1 WHERE col = col = 'abc'", "DELETE FROM tbl WHERE col = col = 'abc'", }, -- GitLab