Skip to content

Commit

Permalink
sampling data type trading
Browse files Browse the repository at this point in the history
  • Loading branch information
martianzhang committed Dec 24, 2018
1 parent 86da258 commit b9e152b
Show file tree
Hide file tree
Showing 25 changed files with 407 additions and 148 deletions.
2 changes: 2 additions & 0 deletions advisor/explainer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
)

func TestDigestExplainText(t *testing.T) {
common.Log.Debug("Enter function: %s", common.GetFunctionName())
var text = `+----+-------------+---------+-------+---------------------------------------------------------+-------------------+---------+---------------------------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+-------+---------------------------------------------------------+-------------------+---------+---------------------------+------+-------------+
Expand All @@ -34,4 +35,5 @@ func TestDigestExplainText(t *testing.T) {
if nil != err {
t.Fatal(err)
}
common.Log.Debug("Exiting function: %s", common.GetFunctionName())
}
2 changes: 1 addition & 1 deletion advisor/heuristic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestRuleImplicitAlias(t *testing.T) {
"select col from tbl tb where id < 1000",
},
{
"do 1",
"select 1",
},
}
for _, sql := range sqls[0] {
Expand Down
8 changes: 7 additions & 1 deletion advisor/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ func TestRuleImplicitConversion(t *testing.T) {
}
}
}
common.Log.Debug("Exiting function: %s", common.GetFunctionName())
common.Config.OnlineDSN = dsn
common.Log.Debug("Exiting function: %s", common.GetFunctionName())
}

// JOI.003 & JOI.004
Expand Down Expand Up @@ -383,9 +383,11 @@ func TestDuplicateKeyChecker(t *testing.T) {
if len(rule) != 0 {
t.Errorf("got rules: %s", pretty.Sprint(rule))
}
common.Log.Debug("Exiting function: %s", common.GetFunctionName())
}

func TestMergeAdvices(t *testing.T) {
common.Log.Debug("Entering function: %s", common.GetFunctionName())
dst := []IndexInfo{
{
Name: "test",
Expand All @@ -405,6 +407,7 @@ func TestMergeAdvices(t *testing.T) {
if len(advise) != 1 {
t.Error(pretty.Sprint(advise))
}
common.Log.Debug("Exiting function: %s", common.GetFunctionName())
}

func TestIdxColsTypeCheck(t *testing.T) {
Expand Down Expand Up @@ -450,13 +453,16 @@ func TestIdxColsTypeCheck(t *testing.T) {
}
}
}
common.Log.Debug("Exiting function: %s", common.GetFunctionName())
}

func TestGetRandomIndexSuffix(t *testing.T) {
common.Log.Debug("Enter function: %s", common.GetFunctionName())
for i := 0; i < 5; i++ {
r := getRandomIndexSuffix()
if !(strings.HasPrefix(r, "_") && len(r) == 5) {
t.Errorf("getRandomIndexSuffix should return a string with prefix `_` and 5 length, but got:%s", r)
}
}
common.Log.Debug("Exiting function: %s", common.GetFunctionName())
}
2 changes: 1 addition & 1 deletion advisor/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func NewQuery4Audit(sql string, options ...string) (*Query4Audit, error) {
// vitess 语法解析不上报,以 tidb parser 为主
q.Stmt, vErr = sqlparser.Parse(sql)
if vErr != nil {
common.Log.Warn("NewQuery4Audit vitess parse Error: %s", vErr.Error())
common.Log.Warn("NewQuery4Audit vitess parse Error: %s, Query: %s", vErr.Error(), sql)
}

// TODO: charset, collation
Expand Down
8 changes: 8 additions & 0 deletions advisor/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,37 @@ import (
)

func TestListTestSQLs(t *testing.T) {
common.Log.Debug("Entering function: %s", common.GetFunctionName())
err := common.GoldenDiff(func() { ListTestSQLs() }, t.Name(), update)
if nil != err {
t.Fatal(err)
}
common.Log.Debug("Exiting function: %s", common.GetFunctionName())
}

func TestListHeuristicRules(t *testing.T) {
common.Log.Debug("Entering function: %s", common.GetFunctionName())
err := common.GoldenDiff(func() { ListHeuristicRules(HeuristicRules) }, t.Name(), update)
if nil != err {
t.Fatal(err)
}
common.Log.Debug("Exiting function: %s", common.GetFunctionName())
}

func TestInBlackList(t *testing.T) {
common.Log.Debug("Entering function: %s", common.GetFunctionName())
common.BlackList = []string{"select"}
if !InBlackList("select 1") {
t.Error("should be true")
}
common.Log.Debug("Exiting function: %s", common.GetFunctionName())
}

func TestIsIgnoreRule(t *testing.T) {
common.Log.Debug("Entering function: %s", common.GetFunctionName())
common.Config.IgnoreRules = []string{"test"}
if !IsIgnoreRule("test") {
t.Error("should be true")
}
common.Log.Debug("Exiting function: %s", common.GetFunctionName())
}
31 changes: 30 additions & 1 deletion ast/meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
)

func TestGetTableFromExprs(t *testing.T) {
common.Log.Debug("Entering function: %s", common.GetFunctionName())
tbExprs := sqlparser.TableExprs{
&sqlparser.AliasedTableExpr{
Expr: sqlparser.TableName{
Expand All @@ -40,9 +41,11 @@ func TestGetTableFromExprs(t *testing.T) {
if tb, ok := meta["db"]; !ok {
t.Errorf("no table qualifier, meta: %s", pretty.Sprint(tb))
}
common.Log.Debug("Exiting function: %s", common.GetFunctionName())
}

func TestGetParseTableWithStmt(t *testing.T) {
common.Log.Debug("Entering function: %s", common.GetFunctionName())
for _, sql := range common.TestSQLs {
fmt.Println(sql)
stmt, err := sqlparser.Parse(sql)
Expand All @@ -53,9 +56,11 @@ func TestGetParseTableWithStmt(t *testing.T) {
pretty.Println(meta)
fmt.Println()
}
common.Log.Debug("Exiting function: %s", common.GetFunctionName())
}

func TestFindCondition(t *testing.T) {
common.Log.Debug("Entering function: %s", common.GetFunctionName())
for _, sql := range common.TestSQLs {
fmt.Println(sql)
stmt, err := sqlparser.Parse(sql)
Expand All @@ -71,9 +76,11 @@ func TestFindCondition(t *testing.T) {
pretty.Println(inEq)
fmt.Println()
}
common.Log.Debug("Exiting function: %s", common.GetFunctionName())
}

func TestFindGroupBy(t *testing.T) {
common.Log.Debug("Entering function: %s", common.GetFunctionName())
sqlList := []string{
"select a from t group by c",
}
Expand All @@ -88,9 +95,11 @@ func TestFindGroupBy(t *testing.T) {
pretty.Println(res)
fmt.Println()
}
common.Log.Debug("Exiting function: %s", common.GetFunctionName())
}

func TestFindOrderBy(t *testing.T) {
common.Log.Debug("Entering function: %s", common.GetFunctionName())
sqlList := []string{
"select a from t group by c order by d, c desc",
"select a from t group by c order by d desc",
Expand All @@ -106,9 +115,11 @@ func TestFindOrderBy(t *testing.T) {
pretty.Println(res)
fmt.Println()
}
common.Log.Debug("Exiting function: %s", common.GetFunctionName())
}

func TestFindSubquery(t *testing.T) {
common.Log.Debug("Entering function: %s", common.GetFunctionName())
sqlList := []string{
"SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM (SELECT column1 FROM t2) a);",
"select column1 from t2",
Expand All @@ -127,10 +138,11 @@ func TestFindSubquery(t *testing.T) {
fmt.Println(len(subquery))
pretty.Println(subquery)
}

common.Log.Debug("Exiting function: %s", common.GetFunctionName())
}

func TestFindJoinTable(t *testing.T) {
common.Log.Debug("Entering function: %s", common.GetFunctionName())
sqlList := []string{
"SELECT * FROM t1 LEFT JOIN (t2 CROSS JOIN t3 CROSS JOIN t4) ON (t2.a = t1.a AND t3.b = t1.b AND t4.c = t1.c)",
"select ID,name from (select address from customer_list where SID=1 order by phone limit 50,10) a join customer_list l on (a.address=l.address) join city c on (c.city=l.city) order by phone desc;",
Expand All @@ -151,9 +163,11 @@ func TestFindJoinTable(t *testing.T) {
joinMeta := FindJoinTable(stmt, nil)
pretty.Println(joinMeta)
}
common.Log.Debug("Exiting function: %s", common.GetFunctionName())
}

func TestFindJoinCols(t *testing.T) {
common.Log.Debug("Entering function: %s", common.GetFunctionName())
sqlList := []string{
"SELECT * FROM t1 LEFT JOIN (t2 CROSS JOIN t3 CROSS JOIN t4) ON (t2.a = t1.a AND t3.b = t1.b AND t4.c = t1.c)",
"select t from a LEFT JOIN b USING (c1, c2, c3)",
Expand All @@ -175,9 +189,11 @@ func TestFindJoinCols(t *testing.T) {
columns := FindJoinCols(stmt)
pretty.Println(columns)
}
common.Log.Debug("Exiting function: %s", common.GetFunctionName())
}

func TestFindJoinColBeWhereEQ(t *testing.T) {
common.Log.Debug("Entering function: %s", common.GetFunctionName())
sqlList := []string{
"select ID,name from (select address from customer_list where SID=1 order by phone limit 50,10) a join customer_list l on (a.address=l.address) join city c on (c.city=l.city) order by phone desc;",
"SELECT * FROM t1 LEFT JOIN (t2, t3, t4) ON (t2.a = t1.a AND t3.b = t1.b AND t4.c = t1.c)",
Expand All @@ -197,9 +213,11 @@ func TestFindJoinColBeWhereEQ(t *testing.T) {
columns := FindEQColsInJoinCond(stmt)
pretty.Println(columns)
}
common.Log.Debug("Exiting function: %s", common.GetFunctionName())
}

func TestFindJoinColBeWhereINEQ(t *testing.T) {
common.Log.Debug("Entering function: %s", common.GetFunctionName())
sqlList := []string{
"select ID,name from (select address from customer_list where SID=1 order by phone limit 50,10) a join customer_list l on (a.address=l.address) join city c on (c.city=l.city) order by phone desc;",
"SELECT * FROM t1 LEFT JOIN (t2, t3, t4) ON (t2.a = t1.a AND t3.b = t1.b AND t4.c = t1.c)",
Expand All @@ -219,9 +237,11 @@ func TestFindJoinColBeWhereINEQ(t *testing.T) {
columns := FindINEQColsInJoinCond(stmt)
pretty.Println(columns)
}
common.Log.Debug("Exiting function: %s", common.GetFunctionName())
}

func TestFindAllCondition(t *testing.T) {
common.Log.Debug("Entering function: %s", common.GetFunctionName())
sqlList := []string{
"SELECT * FROM t1 LEFT JOIN (t2 CROSS JOIN t3 CROSS JOIN t4) ON (t2.a = t1.a AND t3.b = t1.b AND t4.c = t1.c)",
"select t from a LEFT JOIN b USING (c1, c2, c3)",
Expand All @@ -247,9 +267,11 @@ func TestFindAllCondition(t *testing.T) {
columns := FindAllCondition(stmt)
pretty.Println(columns)
}
common.Log.Debug("Exiting function: %s", common.GetFunctionName())
}

func TestFindColumn(t *testing.T) {
common.Log.Debug("Entering function: %s", common.GetFunctionName())
sqlList := []string{
"select col, col2, sum(col1) from tb group by col",
"select col from tb group by col,sum(col1)",
Expand All @@ -266,9 +288,11 @@ func TestFindColumn(t *testing.T) {
columns := FindColumn(stmt)
pretty.Println(columns)
}
common.Log.Debug("Exiting function: %s", common.GetFunctionName())
}

func TestFindAllCols(t *testing.T) {
common.Log.Debug("Entering function: %s", common.GetFunctionName())
sqlList := []string{
"select * from tb where a = '1' order by c",
"select * from tb where a = '1' group by c",
Expand Down Expand Up @@ -296,9 +320,11 @@ func TestFindAllCols(t *testing.T) {
t.Error(fmt.Errorf("want 'c' got %v", columns))
}
}
common.Log.Debug("Exiting function: %s", common.GetFunctionName())
}

func TestGetSubqueryDepth(t *testing.T) {
common.Log.Debug("Entering function: %s", common.GetFunctionName())
sqlList := []string{
"SELECT * FROM t1 LEFT JOIN (t2 CROSS JOIN t3 CROSS JOIN t4) ON (t2.a = t1.a AND t3.b = t1.b AND t4.c = t1.c)",
"select t from a LEFT JOIN b USING (c1, c2, c3)",
Expand All @@ -323,9 +349,11 @@ func TestGetSubqueryDepth(t *testing.T) {
dep := GetSubqueryDepth(stmt)
fmt.Println(dep)
}
common.Log.Debug("Exiting function: %s", common.GetFunctionName())
}

func TestAppendTable(t *testing.T) {
common.Log.Debug("Entering function: %s", common.GetFunctionName())
sqlList := []string{
"select ID,name from (select address from customer_list where SID=1 order by phone limit 50,10) a join customer_list l on (a.address=l.address) join city c on (c.city=l.city) order by phone desc;",
}
Expand Down Expand Up @@ -367,4 +395,5 @@ func TestAppendTable(t *testing.T) {
if meta[""].Table["customer_list"].TableAliases[0] != "l" || meta[""].Table["city"].TableAliases[0] != "c" {
t.Error("alias filed\n", pretty.Sprint(meta))
}
common.Log.Debug("Exiting function: %s", common.GetFunctionName())
}
8 changes: 8 additions & 0 deletions ast/pretty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ var TestSqlsPretty = []string{
}

func TestPretty(t *testing.T) {
common.Log.Debug("Entering function: %s", common.GetFunctionName())
err := common.GoldenDiff(func() {
for _, sql := range append(TestSqlsPretty, common.TestSQLs...) {
fmt.Println(sql)
Expand All @@ -137,9 +138,11 @@ func TestPretty(t *testing.T) {
if nil != err {
t.Fatal(err)
}
common.Log.Debug("Exiting function: %s", common.GetFunctionName())
}

func TestIsKeyword(t *testing.T) {
common.Log.Debug("Entering function: %s", common.GetFunctionName())
tks := map[string]bool{
"AGAINST": true,
"AUTO_INCREMENT": true,
Expand All @@ -155,19 +158,23 @@ func TestIsKeyword(t *testing.T) {
t.Error("isKeyword:", tk)
}
}
common.Log.Debug("Exiting function: %s", common.GetFunctionName())
}

func TestRemoveComments(t *testing.T) {
common.Log.Debug("Entering function: %s", common.GetFunctionName())
for _, sql := range TestSqlsPretty {
stmt, _ := sqlparser.Parse(sql)
newSQL := sqlparser.String(stmt)
if newSQL != sql {
fmt.Print(newSQL)
}
}
common.Log.Debug("Exiting function: %s", common.GetFunctionName())
}

func TestMysqlEscapeString(t *testing.T) {
common.Log.Debug("Entering function: %s", common.GetFunctionName())
var strs = []map[string]string{
{
"input": "abc",
Expand Down Expand Up @@ -198,4 +205,5 @@ abc`,
}
}
}
common.Log.Debug("Exiting function: %s", common.GetFunctionName())
}
Loading

0 comments on commit b9e152b

Please sign in to comment.