forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconst_test.go
283 lines (254 loc) · 8.64 KB
/
const_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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Copyright 2017 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 types_test
import (
"context"
"flag"
. "github.com/pingcap/check"
"github.com/pingcap/parser"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/store/mockstore/cluster"
"github.com/pingcap/tidb/store/mockstore/mocktikv"
"github.com/pingcap/tidb/util/testkit"
"github.com/pingcap/tidb/util/testleak"
)
var _ = Suite(&testMySQLConstSuite{})
type testMySQLConstSuite struct {
cluster cluster.Cluster
mvccStore mocktikv.MVCCStore
store kv.Storage
dom *domain.Domain
*parser.Parser
}
var mockTikv = flag.Bool("mockTikv", true, "use mock tikv store in executor test")
func (s *testMySQLConstSuite) SetUpSuite(c *C) {
s.Parser = parser.New()
flag.Lookup("mockTikv")
useMockTikv := *mockTikv
if useMockTikv {
store, err := mockstore.NewMockStore(
mockstore.WithClusterInspector(func(c cluster.Cluster) {
mockstore.BootstrapWithSingleStore(c)
s.cluster = c
}),
)
c.Assert(err, IsNil)
s.store = store
session.SetSchemaLease(0)
session.DisableStats4Test()
}
var err error
s.dom, err = session.BootstrapSession(s.store)
c.Assert(err, IsNil)
}
func (s *testMySQLConstSuite) TearDownSuite(c *C) {
s.dom.Close()
s.store.Close()
testleak.AfterTest(c)()
}
func (s *testMySQLConstSuite) TestGetSQLMode(c *C) {
positiveCases := []struct {
arg string
}{
{"NO_ZERO_DATE"},
{",,NO_ZERO_DATE"},
{"NO_ZERO_DATE,NO_ZERO_IN_DATE"},
{""},
{", "},
{","},
}
for _, t := range positiveCases {
_, err := mysql.GetSQLMode(mysql.FormatSQLModeStr(t.arg))
c.Assert(err, IsNil)
}
negativeCases := []struct {
arg string
}{
{"NO_ZERO_DATE, NO_ZERO_IN_DATE"},
{"NO_ZERO_DATE,adfadsdfasdfads"},
{", ,NO_ZERO_DATE"},
{" ,"},
}
for _, t := range negativeCases {
_, err := mysql.GetSQLMode(mysql.FormatSQLModeStr(t.arg))
c.Assert(err, NotNil)
}
}
func (s *testMySQLConstSuite) TestSQLMode(c *C) {
tests := []struct {
arg string
hasNoZeroDateMode bool
hasNoZeroInDateMode bool
hasErrorForDivisionByZeroMode bool
}{
{"NO_ZERO_DATE", true, false, false},
{"NO_ZERO_IN_DATE", false, true, false},
{"ERROR_FOR_DIVISION_BY_ZERO", false, false, true},
{"NO_ZERO_IN_DATE,NO_ZERO_DATE", true, true, false},
{"NO_ZERO_DATE,NO_ZERO_IN_DATE", true, true, false},
{"NO_ZERO_DATE,NO_ZERO_IN_DATE", true, true, false},
{"NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO", true, true, true},
{"NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO", false, true, true},
{"", false, false, false},
}
for _, t := range tests {
sqlMode, _ := mysql.GetSQLMode(t.arg)
c.Assert(sqlMode.HasNoZeroDateMode(), Equals, t.hasNoZeroDateMode)
c.Assert(sqlMode.HasNoZeroInDateMode(), Equals, t.hasNoZeroInDateMode)
c.Assert(sqlMode.HasErrorForDivisionByZeroMode(), Equals, t.hasErrorForDivisionByZeroMode)
}
}
func (s *testMySQLConstSuite) TestRealAsFloatMode(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t (a real);")
result := tk.MustQuery("desc t")
c.Check(result.Rows(), HasLen, 1)
row := result.Rows()[0]
c.Assert(row[1], Equals, "double")
tk.MustExec("drop table if exists t;")
tk.MustExec("set sql_mode='REAL_AS_FLOAT'")
tk.MustExec("create table t (a real)")
result = tk.MustQuery("desc t")
c.Check(result.Rows(), HasLen, 1)
row = result.Rows()[0]
c.Assert(row[1], Equals, "float")
}
func (s *testMySQLConstSuite) TestPipesAsConcatMode(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("SET sql_mode='PIPES_AS_CONCAT';")
r := tk.MustQuery(`SELECT 'hello' || 'world';`)
r.Check(testkit.Rows("helloworld"))
}
func (s *testMySQLConstSuite) TestNoUnsignedSubtractionMode(c *C) {
tk := testkit.NewTestKit(c, s.store)
ctx := context.Background()
tk.MustExec("set sql_mode='NO_UNSIGNED_SUBTRACTION'")
r := tk.MustQuery("SELECT CAST(0 as UNSIGNED) - 1;")
r.Check(testkit.Rows("-1"))
rs, _ := tk.Exec("SELECT CAST(18446744073709551615 as UNSIGNED) - 1;")
_, err := session.GetRows4Test(ctx, tk.Se, rs)
c.Assert(err, NotNil)
c.Assert(rs.Close(), IsNil)
rs, _ = tk.Exec("SELECT 1 - CAST(18446744073709551615 as UNSIGNED);")
_, err = session.GetRows4Test(ctx, tk.Se, rs)
c.Assert(err, NotNil)
c.Assert(rs.Close(), IsNil)
rs, _ = tk.Exec("SELECT CAST(-1 as UNSIGNED) - 1")
_, err = session.GetRows4Test(ctx, tk.Se, rs)
c.Assert(err, NotNil)
c.Assert(rs.Close(), IsNil)
rs, _ = tk.Exec("SELECT CAST(9223372036854775808 as UNSIGNED) - 1")
_, err = session.GetRows4Test(ctx, tk.Se, rs)
c.Assert(err, NotNil)
c.Assert(rs.Close(), IsNil)
}
func (s *testMySQLConstSuite) TestHighNotPrecedenceMode(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1")
tk.MustExec("create table t1 (a int);")
tk.MustExec("insert into t1 values (0),(1),(NULL);")
r := tk.MustQuery(`SELECT * FROM t1 WHERE NOT a BETWEEN 2 AND 3;`)
r.Check(testkit.Rows("0", "1"))
r = tk.MustQuery(`SELECT NOT 1 BETWEEN -5 AND 5;`)
r.Check(testkit.Rows("0"))
tk.MustExec("set sql_mode='high_not_precedence';")
r = tk.MustQuery(`SELECT * FROM t1 WHERE NOT a BETWEEN 2 AND 3;`)
r.Check(testkit.Rows())
r = tk.MustQuery(`SELECT NOT 1 BETWEEN -5 AND 5;`)
r.Check(testkit.Rows("1"))
}
func (s *testMySQLConstSuite) TestIgnoreSpaceMode(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("set sql_mode=''")
tk.MustExec("CREATE TABLE COUNT (a bigint);")
tk.MustExec("DROP TABLE COUNT;")
tk.MustExec("CREATE TABLE `COUNT` (a bigint);")
tk.MustExec("DROP TABLE COUNT;")
_, err := tk.Exec("CREATE TABLE COUNT(a bigint);")
c.Assert(err, NotNil)
tk.MustExec("CREATE TABLE test.COUNT(a bigint);")
tk.MustExec("DROP TABLE COUNT;")
tk.MustExec("CREATE TABLE BIT_AND (a bigint);")
tk.MustExec("DROP TABLE BIT_AND;")
tk.MustExec("CREATE TABLE `BIT_AND` (a bigint);")
tk.MustExec("DROP TABLE BIT_AND;")
_, err = tk.Exec("CREATE TABLE BIT_AND(a bigint);")
c.Assert(err, NotNil)
tk.MustExec("CREATE TABLE test.BIT_AND(a bigint);")
tk.MustExec("DROP TABLE BIT_AND;")
tk.MustExec("CREATE TABLE NOW (a bigint);")
tk.MustExec("DROP TABLE NOW;")
tk.MustExec("CREATE TABLE `NOW` (a bigint);")
tk.MustExec("DROP TABLE NOW;")
_, err = tk.Exec("CREATE TABLE NOW(a bigint);")
c.Assert(err, NotNil)
tk.MustExec("CREATE TABLE test.NOW(a bigint);")
tk.MustExec("DROP TABLE NOW;")
tk.MustExec("set sql_mode='IGNORE_SPACE'")
_, err = tk.Exec("CREATE TABLE COUNT (a bigint);")
c.Assert(err, NotNil)
tk.MustExec("CREATE TABLE `COUNT` (a bigint);")
tk.MustExec("DROP TABLE COUNT;")
_, err = tk.Exec("CREATE TABLE COUNT(a bigint);")
c.Assert(err, NotNil)
tk.MustExec("CREATE TABLE test.COUNT(a bigint);")
tk.MustExec("DROP TABLE COUNT;")
_, err = tk.Exec("CREATE TABLE BIT_AND (a bigint);")
c.Assert(err, NotNil)
tk.MustExec("CREATE TABLE `BIT_AND` (a bigint);")
tk.MustExec("DROP TABLE BIT_AND;")
_, err = tk.Exec("CREATE TABLE BIT_AND(a bigint);")
c.Assert(err, NotNil)
tk.MustExec("CREATE TABLE test.BIT_AND(a bigint);")
tk.MustExec("DROP TABLE BIT_AND;")
_, err = tk.Exec("CREATE TABLE NOW (a bigint);")
c.Assert(err, NotNil)
tk.MustExec("CREATE TABLE `NOW` (a bigint);")
tk.MustExec("DROP TABLE NOW;")
_, err = tk.Exec("CREATE TABLE NOW(a bigint);")
c.Assert(err, NotNil)
tk.MustExec("CREATE TABLE test.NOW(a bigint);")
tk.MustExec("DROP TABLE NOW;")
}
func (s *testMySQLConstSuite) TestNoBackslashEscapesMode(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("set sql_mode=''")
r := tk.MustQuery("SELECT '\\\\'")
r.Check(testkit.Rows("\\"))
tk.MustExec("set sql_mode='NO_BACKSLASH_ESCAPES'")
r = tk.MustQuery("SELECT '\\\\'")
r.Check(testkit.Rows("\\\\"))
}
func (s *testMySQLConstSuite) TestServerStatus(c *C) {
tests := []struct {
arg uint16
IsCursorExists bool
}{
{0, false},
{mysql.ServerStatusInTrans | mysql.ServerStatusNoBackslashEscaped, false},
{mysql.ServerStatusCursorExists, true},
{mysql.ServerStatusCursorExists | mysql.ServerStatusLastRowSend, true},
}
for _, t := range tests {
ret := mysql.HasCursorExistsFlag(t.arg)
c.Assert(ret, Equals, t.IsCursorExists)
}
}