forked from pingcap/parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
digester_test.go
169 lines (154 loc) · 5.9 KB
/
digester_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package parser_test
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"testing"
. "github.com/pingcap/check"
"github.com/pingcap/parser"
)
var _ = Suite(&testSQLDigestSuite{})
type testSQLDigestSuite struct {
}
func (s *testSQLDigestSuite) TestNormalize(c *C) {
tests := []struct {
input string
expect string
}{
{"select _utf8mb4'123'", "select (_charset) ?"},
{"SELECT 1", "select ?"},
{"select null", "select ?"},
{"select \\N", "select ?"},
{"SELECT `null`", "select `null`"},
{"select * from b where id = 1", "select * from `b` where `id` = ?"},
{"select 1 from b where id in (1, 3, '3', 1, 2, 3, 4)", "select ? from `b` where `id` in ( ... )"},
{"select 1 from b where id in (1, a, 4)", "select ? from `b` where `id` in ( ? , `a` , ? )"},
{"select 1 from b order by 2", "select ? from `b` order by 2"},
{"select /*+ a hint */ 1", "select ?"},
{"select /* a hint */ 1", "select ?"},
{"select truncate(1, 2)", "select truncate ( ... )"},
{"select -1 + - 2 + b - c + 0.2 + (-2) from c where d in (1, -2, +3)", "select ? + ? + `b` - `c` + ? + ( ? ) from `c` where `d` in ( ... )"},
{"select * from t where a <= -1 and b < -2 and c = -3 and c > -4 and c >= -5 and e is 1", "select * from `t` where `a` <= ? and `b` < ? and `c` = ? and `c` > ? and `c` >= ? and `e` is ?"},
{"select count(a), b from t group by 2", "select count ( `a` ) , `b` from `t` group by 2"},
{"select count(a), b, c from t group by 2, 3", "select count ( `a` ) , `b` , `c` from `t` group by 2 , 3"},
{"select count(a), b, c from t group by (2, 3)", "select count ( `a` ) , `b` , `c` from `t` group by ( 2 , 3 )"},
{"select a, b from t order by 1, 2", "select `a` , `b` from `t` order by 1 , 2"},
{"select count(*) from t", "select count ( ? ) from `t`"},
{"select * from t Force Index(kk)", "select * from `t`"},
{"select * from t USE Index(kk)", "select * from `t`"},
{"select * from t Ignore Index(kk)", "select * from `t`"},
{"select * from t1 straight_join t2 on t1.id=t2.id", "select * from `t1` join `t2` on `t1` . `id` = `t2` . `id`"},
{"select * from `table`", "select * from `table`"},
{"select * from `30`", "select * from `30`"},
{"select * from `select`", "select * from `select`"},
// test syntax error, it will be checked by parser, but it should not make normalize dead loop.
{"select * from t ignore index(", "select * from `t` ignore index"},
{"select /*+ ", "select "},
{"select * from 🥳", "select * from"},
{"select 1 / 2", "select ? / ?"},
{"select * from t where a = 40 limit ?, ?", "select * from `t` where `a` = ? limit ..."},
{"select * from t where a > ?", "select * from `t` where `a` > ?"},
{"select @a=b from t", "select @a = `b` from `t`"},
{"select * from `table", "select * from"},
}
for _, test := range tests {
normalized := parser.Normalize(test.input)
digest := parser.DigestNormalized(normalized)
c.Assert(normalized, Equals, test.expect)
normalized2, digest2 := parser.NormalizeDigest(test.input)
c.Assert(normalized2, Equals, normalized)
c.Assert(digest2.String(), Equals, digest.String(), Commentf("%+v", test))
}
}
func (s *testSQLDigestSuite) TestNormalizeDigest(c *C) {
tests := []struct {
sql string
normalized string
digest string
}{
{"select 1 from b where id in (1, 3, '3', 1, 2, 3, 4)", "select ? from `b` where `id` in ( ... )", "e1c8cc2738f596dc24f15ef8eb55e0d902910d7298983496362a7b46dbc0b310"},
}
for _, test := range tests {
normalized, digest := parser.NormalizeDigest(test.sql)
c.Assert(normalized, Equals, test.normalized)
c.Assert(digest.String(), Equals, test.digest)
normalized = parser.Normalize(test.sql)
digest = parser.DigestNormalized(normalized)
c.Assert(normalized, Equals, test.normalized)
c.Assert(digest.String(), Equals, test.digest)
}
}
func (s *testSQLDigestSuite) TestDigestHashEqForSimpleSQL(c *C) {
sqlGroups := [][]string{
{"select * from b where id = 1", "select * from b where id = '1'", "select * from b where id =2"},
{"select 2 from b, c where c.id > 1", "select 4 from b, c where c.id > 23"},
{"Select 3", "select 1"},
}
for _, sqlGroup := range sqlGroups {
var d string
for _, sql := range sqlGroup {
dig := parser.DigestHash(sql)
if d == "" {
d = dig.String()
continue
}
c.Assert(d, Equals, dig.String())
}
}
}
func (s *testSQLDigestSuite) TestDigestHashNotEqForSimpleSQL(c *C) {
sqlGroups := [][]string{
{"select * from b where id = 1", "select a from b where id = 1", "select * from d where bid =1"},
}
for _, sqlGroup := range sqlGroups {
var d string
for _, sql := range sqlGroup {
dig := parser.DigestHash(sql)
if d == "" {
d = dig.String()
continue
}
c.Assert(d, Not(Equals), dig.String())
}
}
}
func (s *testSQLDigestSuite) TestGenDigest(c *C) {
hash := genRandDigest("abc")
digest := parser.NewDigest(hash)
c.Assert(digest.String(), Equals, fmt.Sprintf("%x", hash))
c.Assert(digest.Bytes(), DeepEquals, hash)
digest = parser.NewDigest(nil)
c.Assert(digest.String(), Equals, "")
c.Assert(digest.Bytes(), IsNil)
}
func genRandDigest(str string) []byte {
hasher := sha256.New()
hasher.Write([]byte(str))
return hasher.Sum(nil)
}
func BenchmarkDigestHexEncode(b *testing.B) {
digest1 := genRandDigest("abc")
b.ResetTimer()
for i := 0; i < b.N; i++ {
hex.EncodeToString(digest1)
}
}
func BenchmarkDigestSprintf(b *testing.B) {
digest1 := genRandDigest("abc")
b.ResetTimer()
for i := 0; i < b.N; i++ {
fmt.Sprintf("%x", digest1)
}
}