forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration_test.go
8579 lines (7775 loc) · 464 KB
/
integration_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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 expression_test
import (
"bytes"
"context"
"encoding/hex"
"fmt"
"math"
"sort"
"strconv"
"strings"
"time"
. "github.com/pingcap/check"
"github.com/pingcap/errors"
"github.com/pingcap/parser/auth"
"github.com/pingcap/parser/model"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/parser/terror"
"github.com/pingcap/tidb/ddl/placement"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/kv"
plannercore "github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/table/tables"
"github.com/pingcap/tidb/tablecodec"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/codec"
"github.com/pingcap/tidb/util/collate"
"github.com/pingcap/tidb/util/kvcache"
"github.com/pingcap/tidb/util/mock"
"github.com/pingcap/tidb/util/sqlexec"
"github.com/pingcap/tidb/util/testkit"
"github.com/pingcap/tidb/util/testutil"
)
var _ = Suite(&testIntegrationSuite{})
var _ = Suite(&testIntegrationSuite2{})
var _ = SerialSuites(&testIntegrationSerialSuite{})
type testIntegrationSuiteBase struct {
store kv.Storage
dom *domain.Domain
ctx sessionctx.Context
}
type testIntegrationSuite struct {
testIntegrationSuiteBase
}
type testIntegrationSuite2 struct {
testIntegrationSuiteBase
}
type testIntegrationSerialSuite struct {
testIntegrationSuiteBase
}
func (s *testIntegrationSuiteBase) cleanEnv(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
r := tk.MustQuery("show tables")
for _, tb := range r.Rows() {
tableName := tb[0]
tk.MustExec(fmt.Sprintf("drop table %v", tableName))
}
}
func (s *testIntegrationSuiteBase) SetUpSuite(c *C) {
var err error
s.store, s.dom, err = newStoreWithBootstrap()
c.Assert(err, IsNil)
s.ctx = mock.NewContext()
}
func (s *testIntegrationSuiteBase) TearDownSuite(c *C) {
s.dom.Close()
s.store.Close()
}
func (s *testIntegrationSuite) Test19654(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("USE test;")
// enum vs enum
tk.MustExec("drop table if exists t1, t2;")
tk.MustExec("create table t1 (b enum('a', 'b'));")
tk.MustExec("insert into t1 values ('a');")
tk.MustExec("create table t2 (b enum('b','a') not null, unique(b));")
tk.MustExec("insert into t2 values ('a');")
tk.MustQuery("select /*+ inl_join(t2)*/ * from t1, t2 where t1.b=t2.b;").Check(testkit.Rows("a a"))
// set vs set
tk.MustExec("drop table if exists t1, t2;")
tk.MustExec("create table t1 (b set('a', 'b'));")
tk.MustExec("insert into t1 values ('a');")
tk.MustExec("create table t2 (b set('b','a') not null, unique(b));")
tk.MustExec("insert into t2 values ('a');")
tk.MustQuery("select /*+ inl_join(t2)*/ * from t1, t2 where t1.b=t2.b;").Check(testkit.Rows("a a"))
// enum vs set
tk.MustExec("drop table if exists t1, t2;")
tk.MustExec("create table t1 (b enum('a', 'b'));")
tk.MustExec("insert into t1 values ('a');")
tk.MustExec("create table t2 (b set('b','a') not null, unique(b));")
tk.MustExec("insert into t2 values ('a');")
tk.MustQuery("select /*+ inl_join(t2)*/ * from t1, t2 where t1.b=t2.b;").Check(testkit.Rows("a a"))
// char vs enum
tk.MustExec("drop table if exists t1, t2;")
tk.MustExec("create table t1 (b char(10));")
tk.MustExec("insert into t1 values ('a');")
tk.MustExec("create table t2 (b enum('b','a') not null, unique(b));")
tk.MustExec("insert into t2 values ('a');")
tk.MustQuery("select /*+ inl_join(t2)*/ * from t1, t2 where t1.b=t2.b;").Check(testkit.Rows("a a"))
// char vs set
tk.MustExec("drop table if exists t1, t2;")
tk.MustExec("create table t1 (b char(10));")
tk.MustExec("insert into t1 values ('a');")
tk.MustExec("create table t2 (b set('b','a') not null, unique(b));")
tk.MustExec("insert into t2 values ('a');")
tk.MustQuery("select /*+ inl_join(t2)*/ * from t1, t2 where t1.b=t2.b;").Check(testkit.Rows("a a"))
}
func (s *testIntegrationSuite) Test19387(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 decimal(16, 2));")
tk.MustExec("select sum(case when 1 then a end) from t group by a;")
res := tk.MustQuery("show create table t")
c.Assert(len(res.Rows()), Equals, 1)
str := res.Rows()[0][1].(string)
c.Assert(strings.Contains(str, "decimal(16,2)"), IsTrue)
}
func (s *testIntegrationSuite) TestFuncREPEAT(c *C) {
tk := testkit.NewTestKit(c, s.store)
defer s.cleanEnv(c)
tk.MustExec("USE test;")
tk.MustExec("DROP TABLE IF EXISTS table_string;")
tk.MustExec("CREATE TABLE table_string(a CHAR(20), b VARCHAR(20), c TINYTEXT, d TEXT(20), e MEDIUMTEXT, f LONGTEXT, g BIGINT);")
tk.MustExec("INSERT INTO table_string (a, b, c, d, e, f, g) VALUES ('a', 'b', 'c', 'd', 'e', 'f', 2);")
tk.CheckExecResult(1, 0)
r := tk.MustQuery("SELECT REPEAT(a, g), REPEAT(b, g), REPEAT(c, g), REPEAT(d, g), REPEAT(e, g), REPEAT(f, g) FROM table_string;")
r.Check(testkit.Rows("aa bb cc dd ee ff"))
r = tk.MustQuery("SELECT REPEAT(NULL, g), REPEAT(NULL, g), REPEAT(NULL, g), REPEAT(NULL, g), REPEAT(NULL, g), REPEAT(NULL, g) FROM table_string;")
r.Check(testkit.Rows("<nil> <nil> <nil> <nil> <nil> <nil>"))
r = tk.MustQuery("SELECT REPEAT(a, NULL), REPEAT(b, NULL), REPEAT(c, NULL), REPEAT(d, NULL), REPEAT(e, NULL), REPEAT(f, NULL) FROM table_string;")
r.Check(testkit.Rows("<nil> <nil> <nil> <nil> <nil> <nil>"))
r = tk.MustQuery("SELECT REPEAT(a, 2), REPEAT(b, 2), REPEAT(c, 2), REPEAT(d, 2), REPEAT(e, 2), REPEAT(f, 2) FROM table_string;")
r.Check(testkit.Rows("aa bb cc dd ee ff"))
r = tk.MustQuery("SELECT REPEAT(NULL, 2), REPEAT(NULL, 2), REPEAT(NULL, 2), REPEAT(NULL, 2), REPEAT(NULL, 2), REPEAT(NULL, 2) FROM table_string;")
r.Check(testkit.Rows("<nil> <nil> <nil> <nil> <nil> <nil>"))
r = tk.MustQuery("SELECT REPEAT(a, -1), REPEAT(b, -2), REPEAT(c, -2), REPEAT(d, -2), REPEAT(e, -2), REPEAT(f, -2) FROM table_string;")
r.Check(testkit.Rows(" "))
r = tk.MustQuery("SELECT REPEAT(a, 0), REPEAT(b, 0), REPEAT(c, 0), REPEAT(d, 0), REPEAT(e, 0), REPEAT(f, 0) FROM table_string;")
r.Check(testkit.Rows(" "))
r = tk.MustQuery("SELECT REPEAT(a, 16777217), REPEAT(b, 16777217), REPEAT(c, 16777217), REPEAT(d, 16777217), REPEAT(e, 16777217), REPEAT(f, 16777217) FROM table_string;")
r.Check(testkit.Rows("<nil> <nil> <nil> <nil> <nil> <nil>"))
}
func (s *testIntegrationSuite) TestFuncLpadAndRpad(c *C) {
tk := testkit.NewTestKit(c, s.store)
defer s.cleanEnv(c)
tk.MustExec(`USE test;`)
tk.MustExec(`DROP TABLE IF EXISTS t;`)
tk.MustExec(`CREATE TABLE t(a BINARY(10), b CHAR(10));`)
tk.MustExec(`INSERT INTO t SELECT "中文", "abc";`)
result := tk.MustQuery(`SELECT LPAD(a, 11, "a"), LPAD(b, 2, "xx") FROM t;`)
result.Check(testkit.Rows("a中文\x00\x00\x00\x00 ab"))
result = tk.MustQuery(`SELECT RPAD(a, 11, "a"), RPAD(b, 2, "xx") FROM t;`)
result.Check(testkit.Rows("中文\x00\x00\x00\x00a ab"))
result = tk.MustQuery(`SELECT LPAD("中文", 5, "字符"), LPAD("中文", 1, "a");`)
result.Check(testkit.Rows("字符字中文 中"))
result = tk.MustQuery(`SELECT RPAD("中文", 5, "字符"), RPAD("中文", 1, "a");`)
result.Check(testkit.Rows("中文字符字 中"))
result = tk.MustQuery(`SELECT RPAD("中文", -5, "字符"), RPAD("中文", 10, "");`)
result.Check(testkit.Rows("<nil> <nil>"))
result = tk.MustQuery(`SELECT LPAD("中文", -5, "字符"), LPAD("中文", 10, "");`)
result.Check(testkit.Rows("<nil> <nil>"))
}
func (s *testIntegrationSuite) TestMiscellaneousBuiltin(c *C) {
ctx := context.Background()
defer s.cleanEnv(c)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
// for uuid
r := tk.MustQuery("select uuid(), uuid(), uuid(), uuid(), uuid(), uuid();")
for _, it := range r.Rows() {
for _, item := range it {
uuid, ok := item.(string)
c.Assert(ok, Equals, true)
list := strings.Split(uuid, "-")
c.Assert(len(list), Equals, 5)
c.Assert(len(list[0]), Equals, 8)
c.Assert(len(list[1]), Equals, 4)
c.Assert(len(list[2]), Equals, 4)
c.Assert(len(list[3]), Equals, 4)
c.Assert(len(list[4]), Equals, 12)
}
}
tk.MustQuery("select sleep(1);").Check(testkit.Rows("0"))
tk.MustQuery("select sleep(0);").Check(testkit.Rows("0"))
tk.MustQuery("select sleep('a');").Check(testkit.Rows("0"))
tk.MustQuery("show warnings;").Check(testkit.Rows("Warning 1292 Truncated incorrect FLOAT value: 'a'"))
rs, err := tk.Exec("select sleep(-1);")
c.Assert(err, IsNil)
c.Assert(rs, NotNil)
_, err = session.GetRows4Test(ctx, tk.Se, rs)
c.Assert(err, NotNil)
c.Assert(rs.Close(), IsNil)
tk.MustQuery("SELECT INET_ATON('10.0.5.9');").Check(testkit.Rows("167773449"))
tk.MustQuery("SELECT INET_NTOA(167773449);").Check(testkit.Rows("10.0.5.9"))
tk.MustQuery("SELECT HEX(INET6_ATON('fdfe::5a55:caff:fefa:9089'));").Check(testkit.Rows("FDFE0000000000005A55CAFFFEFA9089"))
tk.MustQuery("SELECT HEX(INET6_ATON('10.0.5.9'));").Check(testkit.Rows("0A000509"))
tk.MustQuery("SELECT INET6_NTOA(INET6_ATON('fdfe::5a55:caff:fefa:9089'));").Check(testkit.Rows("fdfe::5a55:caff:fefa:9089"))
tk.MustQuery("SELECT INET6_NTOA(INET6_ATON('10.0.5.9'));").Check(testkit.Rows("10.0.5.9"))
tk.MustQuery("SELECT INET6_NTOA(UNHEX('FDFE0000000000005A55CAFFFEFA9089'));").Check(testkit.Rows("fdfe::5a55:caff:fefa:9089"))
tk.MustQuery("SELECT INET6_NTOA(UNHEX('0A000509'));").Check(testkit.Rows("10.0.5.9"))
tk.MustQuery(`SELECT IS_IPV4('10.0.5.9'), IS_IPV4('10.0.5.256');`).Check(testkit.Rows("1 0"))
tk.MustQuery(`SELECT IS_IPV4_COMPAT(INET6_ATON('::10.0.5.9'));`).Check(testkit.Rows("1"))
tk.MustQuery(`SELECT IS_IPV4_COMPAT(INET6_ATON('::ffff:10.0.5.9'));`).Check(testkit.Rows("0"))
tk.MustQuery(`SELECT
IS_IPV4_COMPAT(INET6_ATON('::192.168.0.1')),
IS_IPV4_COMPAT(INET6_ATON('::c0a8:0001')),
IS_IPV4_COMPAT(INET6_ATON('::c0a8:1'));`).Check(testkit.Rows("1 1 1"))
tk.MustQuery(`SELECT IS_IPV4_MAPPED(INET6_ATON('::10.0.5.9'));`).Check(testkit.Rows("0"))
tk.MustQuery(`SELECT IS_IPV4_MAPPED(INET6_ATON('::ffff:10.0.5.9'));`).Check(testkit.Rows("1"))
tk.MustQuery(`SELECT
IS_IPV4_MAPPED(INET6_ATON('::ffff:192.168.0.1')),
IS_IPV4_MAPPED(INET6_ATON('::ffff:c0a8:0001')),
IS_IPV4_MAPPED(INET6_ATON('::ffff:c0a8:1'));`).Check(testkit.Rows("1 1 1"))
tk.MustQuery(`SELECT IS_IPV6('10.0.5.9'), IS_IPV6('::1');`).Check(testkit.Rows("0 1"))
tk.MustExec("drop table if exists t1;")
tk.MustExec(`create table t1(
a int,
b int not null,
c int not null default 0,
d int default 0,
unique key(b,c),
unique key(b,d)
);`)
tk.MustExec("insert into t1 (a,b) values(1,10),(1,20),(2,30),(2,40);")
tk.MustQuery("select any_value(a), sum(b) from t1;").Check(testkit.Rows("1 100"))
tk.MustQuery("select a,any_value(b),sum(c) from t1 group by a order by a;").Check(testkit.Rows("1 10 0", "2 30 0"))
// for locks
tk.MustExec(`set tidb_enable_noop_functions=1;`)
result := tk.MustQuery(`SELECT GET_LOCK('test_lock1', 10);`)
result.Check(testkit.Rows("1"))
result = tk.MustQuery(`SELECT GET_LOCK('test_lock2', 10);`)
result.Check(testkit.Rows("1"))
result = tk.MustQuery(`SELECT RELEASE_LOCK('test_lock2');`)
result.Check(testkit.Rows("1"))
result = tk.MustQuery(`SELECT RELEASE_LOCK('test_lock1');`)
result.Check(testkit.Rows("1"))
}
func (s *testIntegrationSuite) TestConvertToBit(c *C) {
defer s.cleanEnv(c)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t, t1")
tk.MustExec("create table t (a bit(64))")
tk.MustExec("create table t1 (a varchar(2))")
tk.MustExec(`insert t1 value ('10')`)
tk.MustExec(`insert t select a from t1`)
tk.MustQuery("select a+0 from t").Check(testkit.Rows("12592"))
tk.MustExec("drop table if exists t, t1")
tk.MustExec("create table t (a bit(64))")
tk.MustExec("create table t1 (a binary(2))")
tk.MustExec(`insert t1 value ('10')`)
tk.MustExec(`insert t select a from t1`)
tk.MustQuery("select a+0 from t").Check(testkit.Rows("12592"))
tk.MustExec("drop table if exists t, t1")
tk.MustExec("create table t (a bit(64))")
tk.MustExec("create table t1 (a datetime)")
tk.MustExec(`insert t1 value ('09-01-01')`)
tk.MustExec(`insert t select a from t1`)
tk.MustQuery("select a+0 from t").Check(testkit.Rows("20090101000000"))
// For issue 20118
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t(a tinyint, b bit(63));")
tk.MustExec("insert ignore into t values(599999999, -1);")
tk.MustQuery("show warnings;").Check(testkit.Rows(
"Warning 1690 constant 599999999 overflows tinyint",
"Warning 1406 Data Too Long, field len 63"))
tk.MustQuery("select * from t;").Check(testkit.Rows("127 \u007f\xff\xff\xff\xff\xff\xff\xff"))
}
func (s *testIntegrationSuite2) TestMathBuiltin(c *C) {
ctx := context.Background()
defer s.cleanEnv(c)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
// for degrees
result := tk.MustQuery("select degrees(0), degrees(1)")
result.Check(testkit.Rows("0 57.29577951308232"))
result = tk.MustQuery("select degrees(2), degrees(5)")
result.Check(testkit.Rows("114.59155902616465 286.4788975654116"))
// for sin
result = tk.MustQuery("select sin(0), sin(1.5707963267949)")
result.Check(testkit.Rows("0 1"))
result = tk.MustQuery("select sin(1), sin(100)")
result.Check(testkit.Rows("0.8414709848078965 -0.5063656411097588"))
result = tk.MustQuery("select sin('abcd')")
result.Check(testkit.Rows("0"))
// for cos
result = tk.MustQuery("select cos(0), cos(3.1415926535898)")
result.Check(testkit.Rows("1 -1"))
result = tk.MustQuery("select cos('abcd')")
result.Check(testkit.Rows("1"))
// for tan
result = tk.MustQuery("select tan(0.00), tan(PI()/4)")
result.Check(testkit.Rows("0 1"))
result = tk.MustQuery("select tan('abcd')")
result.Check(testkit.Rows("0"))
// for log2
result = tk.MustQuery("select log2(0.0)")
result.Check(testkit.Rows("<nil>"))
result = tk.MustQuery("select log2(4)")
result.Check(testkit.Rows("2"))
result = tk.MustQuery("select log2('8.0abcd')")
result.Check(testkit.Rows("3"))
result = tk.MustQuery("select log2(-1)")
result.Check(testkit.Rows("<nil>"))
result = tk.MustQuery("select log2(NULL)")
result.Check(testkit.Rows("<nil>"))
// for log10
result = tk.MustQuery("select log10(0.0)")
result.Check(testkit.Rows("<nil>"))
result = tk.MustQuery("select log10(100)")
result.Check(testkit.Rows("2"))
result = tk.MustQuery("select log10('1000.0abcd')")
result.Check(testkit.Rows("3"))
result = tk.MustQuery("select log10(-1)")
result.Check(testkit.Rows("<nil>"))
result = tk.MustQuery("select log10(NULL)")
result.Check(testkit.Rows("<nil>"))
// for log
result = tk.MustQuery("select log(0.0)")
result.Check(testkit.Rows("<nil>"))
result = tk.MustQuery("select log(100)")
result.Check(testkit.Rows("4.605170185988092"))
result = tk.MustQuery("select log('100.0abcd')")
result.Check(testkit.Rows("4.605170185988092"))
result = tk.MustQuery("select log(-1)")
result.Check(testkit.Rows("<nil>"))
result = tk.MustQuery("select log(NULL)")
result.Check(testkit.Rows("<nil>"))
result = tk.MustQuery("select log(NULL, NULL)")
result.Check(testkit.Rows("<nil>"))
result = tk.MustQuery("select log(1, 100)")
result.Check(testkit.Rows("<nil>"))
result = tk.MustQuery("select log(0.5, 0.25)")
result.Check(testkit.Rows("2"))
result = tk.MustQuery("select log(-1, 0.25)")
result.Check(testkit.Rows("<nil>"))
// for atan
result = tk.MustQuery("select atan(0), atan(-1), atan(1), atan(1,2)")
result.Check(testkit.Rows("0 -0.7853981633974483 0.7853981633974483 0.4636476090008061"))
result = tk.MustQuery("select atan('tidb')")
result.Check(testkit.Rows("0"))
// for asin
result = tk.MustQuery("select asin(0), asin(-2), asin(2), asin(1)")
result.Check(testkit.Rows("0 <nil> <nil> 1.5707963267948966"))
result = tk.MustQuery("select asin('tidb')")
result.Check(testkit.Rows("0"))
// for acos
result = tk.MustQuery("select acos(0), acos(-2), acos(2), acos(1)")
result.Check(testkit.Rows("1.5707963267948966 <nil> <nil> 0"))
result = tk.MustQuery("select acos('tidb')")
result.Check(testkit.Rows("1.5707963267948966"))
// for pi
result = tk.MustQuery("select pi()")
result.Check(testkit.Rows("3.141592653589793"))
// for floor
result = tk.MustQuery("select floor(0), floor(null), floor(1.23), floor(-1.23), floor(1)")
result.Check(testkit.Rows("0 <nil> 1 -2 1"))
result = tk.MustQuery("select floor('tidb'), floor('1tidb'), floor('tidb1')")
result.Check(testkit.Rows("0 1 0"))
result = tk.MustQuery("SELECT floor(t.c_datetime) FROM (select CAST('2017-07-19 00:00:00' AS DATETIME) AS c_datetime) AS t")
result.Check(testkit.Rows("20170719000000"))
result = tk.MustQuery("SELECT floor(t.c_time) FROM (select CAST('12:34:56' AS TIME) AS c_time) AS t")
result.Check(testkit.Rows("123456"))
result = tk.MustQuery("SELECT floor(t.c_time) FROM (select CAST('00:34:00' AS TIME) AS c_time) AS t")
result.Check(testkit.Rows("3400"))
result = tk.MustQuery("SELECT floor(t.c_time) FROM (select CAST('00:00:00' AS TIME) AS c_time) AS t")
result.Check(testkit.Rows("0"))
result = tk.MustQuery("SELECT floor(t.c_decimal) FROM (SELECT CAST('-10.01' AS DECIMAL(10,2)) AS c_decimal) AS t")
result.Check(testkit.Rows("-11"))
result = tk.MustQuery("SELECT floor(t.c_decimal) FROM (SELECT CAST('-10.01' AS DECIMAL(10,1)) AS c_decimal) AS t")
result.Check(testkit.Rows("-10"))
// for ceil/ceiling
result = tk.MustQuery("select ceil(0), ceil(null), ceil(1.23), ceil(-1.23), ceil(1)")
result.Check(testkit.Rows("0 <nil> 2 -1 1"))
result = tk.MustQuery("select ceiling(0), ceiling(null), ceiling(1.23), ceiling(-1.23), ceiling(1)")
result.Check(testkit.Rows("0 <nil> 2 -1 1"))
result = tk.MustQuery("select ceil('tidb'), ceil('1tidb'), ceil('tidb1'), ceiling('tidb'), ceiling('1tidb'), ceiling('tidb1')")
result.Check(testkit.Rows("0 1 0 0 1 0"))
result = tk.MustQuery("select ceil(t.c_datetime), ceiling(t.c_datetime) from (select cast('2017-07-20 00:00:00' as datetime) as c_datetime) as t")
result.Check(testkit.Rows("20170720000000 20170720000000"))
result = tk.MustQuery("select ceil(t.c_time), ceiling(t.c_time) from (select cast('12:34:56' as time) as c_time) as t")
result.Check(testkit.Rows("123456 123456"))
result = tk.MustQuery("select ceil(t.c_time), ceiling(t.c_time) from (select cast('00:34:00' as time) as c_time) as t")
result.Check(testkit.Rows("3400 3400"))
result = tk.MustQuery("select ceil(t.c_time), ceiling(t.c_time) from (select cast('00:00:00' as time) as c_time) as t")
result.Check(testkit.Rows("0 0"))
result = tk.MustQuery("select ceil(t.c_decimal), ceiling(t.c_decimal) from (select cast('-10.01' as decimal(10,2)) as c_decimal) as t")
result.Check(testkit.Rows("-10 -10"))
result = tk.MustQuery("select ceil(t.c_decimal), ceiling(t.c_decimal) from (select cast('-10.01' as decimal(10,1)) as c_decimal) as t")
result.Check(testkit.Rows("-10 -10"))
result = tk.MustQuery("select floor(18446744073709551615), ceil(18446744073709551615)")
result.Check(testkit.Rows("18446744073709551615 18446744073709551615"))
result = tk.MustQuery("select floor(18446744073709551615.1233), ceil(18446744073709551615.1233)")
result.Check(testkit.Rows("18446744073709551615 18446744073709551616"))
result = tk.MustQuery("select floor(-18446744073709551617), ceil(-18446744073709551617), floor(-18446744073709551617.11), ceil(-18446744073709551617.11)")
result.Check(testkit.Rows("-18446744073709551617 -18446744073709551617 -18446744073709551618 -18446744073709551617"))
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t(a decimal(40,20) UNSIGNED);")
tk.MustExec("insert into t values(2.99999999900000000000), (12), (0);")
tk.MustQuery("select a, ceil(a) from t where ceil(a) > 1;").Check(testkit.Rows("2.99999999900000000000 3", "12.00000000000000000000 12"))
tk.MustQuery("select a, ceil(a) from t;").Check(testkit.Rows("2.99999999900000000000 3", "12.00000000000000000000 12", "0.00000000000000000000 0"))
tk.MustQuery("select ceil(-29464);").Check(testkit.Rows("-29464"))
tk.MustQuery("select a, floor(a) from t where floor(a) > 1;").Check(testkit.Rows("2.99999999900000000000 2", "12.00000000000000000000 12"))
tk.MustQuery("select a, floor(a) from t;").Check(testkit.Rows("2.99999999900000000000 2", "12.00000000000000000000 12", "0.00000000000000000000 0"))
tk.MustQuery("select floor(-29464);").Check(testkit.Rows("-29464"))
tk.MustExec(`drop table if exists t;`)
tk.MustExec(`create table t(a decimal(40,20), b bigint);`)
tk.MustExec(`insert into t values(-2.99999990000000000000, -1);`)
tk.MustQuery(`select floor(a), floor(a), floor(a) from t;`).Check(testkit.Rows(`-3 -3 -3`))
tk.MustQuery(`select b, floor(b) from t;`).Check(testkit.Rows(`-1 -1`))
// for cot
result = tk.MustQuery("select cot(1), cot(-1), cot(NULL)")
result.Check(testkit.Rows("0.6420926159343308 -0.6420926159343308 <nil>"))
result = tk.MustQuery("select cot('1tidb')")
result.Check(testkit.Rows("0.6420926159343308"))
rs, err := tk.Exec("select cot(0)")
c.Assert(err, IsNil)
_, err = session.GetRows4Test(ctx, tk.Se, rs)
c.Assert(err, NotNil)
terr := errors.Cause(err).(*terror.Error)
c.Assert(terr.Code(), Equals, errors.ErrCode(mysql.ErrDataOutOfRange))
c.Assert(rs.Close(), IsNil)
// for exp
result = tk.MustQuery("select exp(0), exp(1), exp(-1), exp(1.2), exp(NULL)")
result.Check(testkit.Rows("1 2.718281828459045 0.36787944117144233 3.3201169227365472 <nil>"))
result = tk.MustQuery("select exp('tidb'), exp('1tidb')")
result.Check(testkit.Rows("1 2.718281828459045"))
rs, err = tk.Exec("select exp(1000000)")
c.Assert(err, IsNil)
_, err = session.GetRows4Test(ctx, tk.Se, rs)
c.Assert(err, NotNil)
terr = errors.Cause(err).(*terror.Error)
c.Assert(terr.Code(), Equals, errors.ErrCode(mysql.ErrDataOutOfRange))
c.Assert(rs.Close(), IsNil)
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a float)")
tk.MustExec("insert into t values(1000000)")
rs, err = tk.Exec("select exp(a) from t")
c.Assert(err, IsNil)
_, err = session.GetRows4Test(ctx, tk.Se, rs)
c.Assert(err, NotNil)
terr = errors.Cause(err).(*terror.Error)
c.Assert(terr.Code(), Equals, errors.ErrCode(mysql.ErrDataOutOfRange))
c.Assert(err.Error(), Equals, "[types:1690]DOUBLE value is out of range in 'exp(test.t.a)'")
c.Assert(rs.Close(), IsNil)
// for conv
result = tk.MustQuery("SELECT CONV('a', 16, 2);")
result.Check(testkit.Rows("1010"))
result = tk.MustQuery("SELECT CONV('6E', 18, 8);")
result.Check(testkit.Rows("172"))
result = tk.MustQuery("SELECT CONV(-17, 10, -18);")
result.Check(testkit.Rows("-H"))
result = tk.MustQuery("SELECT CONV(10+'10'+'10'+X'0a', 10, 10);")
result.Check(testkit.Rows("40"))
result = tk.MustQuery("SELECT CONV('a', 1, 10);")
result.Check(testkit.Rows("<nil>"))
result = tk.MustQuery("SELECT CONV('a', 37, 10);")
result.Check(testkit.Rows("<nil>"))
result = tk.MustQuery("SELECT CONV(0x0020, 2, 2);")
result.Check(testkit.Rows("100000"))
result = tk.MustQuery("SELECT CONV(0b10, 16, 2)")
result.Check(testkit.Rows("10"))
result = tk.MustQuery("SELECT CONV(0b10, 16, 8)")
result.Check(testkit.Rows("2"))
tk.MustExec("drop table if exists bit")
tk.MustExec("create table bit(b bit(10))")
tk.MustExec(`INSERT INTO bit (b) VALUES
(0b0000010101),
(0b0000010101),
(NULL),
(0b0000000001),
(0b0000000000),
(0b1111111111),
(0b1111111111),
(0b1111111111),
(0b0000000000),
(0b0000000000),
(0b0000000000),
(0b0000000000),
(0b0000100000);`)
tk.MustQuery("select conv(b, 2, 2) from `bit`").Check(testkit.Rows(
"10101",
"10101",
"<nil>",
"1",
"0",
"1111111111",
"1111111111",
"1111111111",
"0",
"0",
"0",
"0",
"100000"))
// for abs
result = tk.MustQuery("SELECT ABS(-1);")
result.Check(testkit.Rows("1"))
result = tk.MustQuery("SELECT ABS('abc');")
result.Check(testkit.Rows("0"))
result = tk.MustQuery("SELECT ABS(18446744073709551615);")
result.Check(testkit.Rows("18446744073709551615"))
result = tk.MustQuery("SELECT ABS(123.4);")
result.Check(testkit.Rows("123.4"))
result = tk.MustQuery("SELECT ABS(-123.4);")
result.Check(testkit.Rows("123.4"))
result = tk.MustQuery("SELECT ABS(1234E-1);")
result.Check(testkit.Rows("123.4"))
result = tk.MustQuery("SELECT ABS(-9223372036854775807);")
result.Check(testkit.Rows("9223372036854775807"))
result = tk.MustQuery("SELECT ABS(NULL);")
result.Check(testkit.Rows("<nil>"))
rs, err = tk.Exec("SELECT ABS(-9223372036854775808);")
c.Assert(err, IsNil)
_, err = session.GetRows4Test(ctx, tk.Se, rs)
c.Assert(err, NotNil)
terr = errors.Cause(err).(*terror.Error)
c.Assert(terr.Code(), Equals, errors.ErrCode(mysql.ErrDataOutOfRange))
c.Assert(rs.Close(), IsNil)
// for round
result = tk.MustQuery("SELECT ROUND(2.5), ROUND(-2.5), ROUND(25E-1);")
result.Check(testkit.Rows("3 -3 2"))
result = tk.MustQuery("SELECT ROUND(2.5, NULL), ROUND(NULL, 4), ROUND(NULL, NULL), ROUND(NULL);")
result.Check(testkit.Rows("<nil> <nil> <nil> <nil>"))
result = tk.MustQuery("SELECT ROUND('123.4'), ROUND('123e-2');")
result.Check(testkit.Rows("123 1"))
result = tk.MustQuery("SELECT ROUND(-9223372036854775808);")
result.Check(testkit.Rows("-9223372036854775808"))
result = tk.MustQuery("SELECT ROUND(123.456, 0), ROUND(123.456, 1), ROUND(123.456, 2), ROUND(123.456, 3), ROUND(123.456, 4), ROUND(123.456, -1), ROUND(123.456, -2), ROUND(123.456, -3), ROUND(123.456, -4);")
result.Check(testkit.Rows("123 123.5 123.46 123.456 123.4560 120 100 0 0"))
result = tk.MustQuery("SELECT ROUND(123456E-3, 0), ROUND(123456E-3, 1), ROUND(123456E-3, 2), ROUND(123456E-3, 3), ROUND(123456E-3, 4), ROUND(123456E-3, -1), ROUND(123456E-3, -2), ROUND(123456E-3, -3), ROUND(123456E-3, -4);")
result.Check(testkit.Rows("123 123.5 123.46 123.456 123.456 120 100 0 0")) // TODO: Column 5 should be 123.4560
result = tk.MustQuery("SELECT ROUND(1e14, 1), ROUND(1e15, 1), ROUND(1e308, 1)")
result.Check(testkit.Rows("100000000000000 1000000000000000 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"))
result = tk.MustQuery("SELECT ROUND(1e-14, 1), ROUND(1e-15, 1), ROUND(1e-308, 1)")
result.Check(testkit.Rows("0 0 0"))
// for truncate
result = tk.MustQuery("SELECT truncate(123, -2), truncate(123, 2), truncate(123, 1), truncate(123, -1);")
result.Check(testkit.Rows("100 123 123 120"))
result = tk.MustQuery("SELECT truncate(123.456, -2), truncate(123.456, 2), truncate(123.456, 1), truncate(123.456, 3), truncate(1.23, 100), truncate(123456E-3, 2);")
result.Check(testkit.Rows("100 123.45 123.4 123.456 1.230000000000000000000000000000 123.45"))
result = tk.MustQuery("SELECT truncate(9223372036854775807, -7), truncate(9223372036854775808, -10), truncate(cast(-1 as unsigned), -10);")
result.Check(testkit.Rows("9223372036850000000 9223372030000000000 18446744070000000000"))
// issue 17181,19390
tk.MustQuery("select truncate(42, -9223372036854775808);").Check(testkit.Rows("0"))
tk.MustQuery("select truncate(42, 9223372036854775808);").Check(testkit.Rows("42"))
tk.MustQuery("select truncate(42, -2147483648);").Check(testkit.Rows("0"))
tk.MustQuery("select truncate(42, 2147483648);").Check(testkit.Rows("42"))
tk.MustQuery("select truncate(42, 18446744073709551615);").Check(testkit.Rows("42"))
tk.MustQuery("select truncate(42, 4294967295);").Check(testkit.Rows("42"))
tk.MustQuery("select truncate(42, -0);").Check(testkit.Rows("42"))
tk.MustQuery("select truncate(42, -307);").Check(testkit.Rows("0"))
tk.MustQuery("select truncate(42, -308);").Check(testkit.Rows("0"))
tk.MustQuery("select truncate(42, -309);").Check(testkit.Rows("0"))
tk.MustExec(`drop table if exists t;`)
tk.MustExec("create table t (a bigint unsigned);")
tk.MustExec("insert into t values (18446744073709551615), (4294967295), (9223372036854775808), (2147483648);")
tk.MustQuery("select truncate(42, a) from t;").Check(testkit.Rows("42", "42", "42", "42"))
tk.MustExec(`drop table if exists t;`)
tk.MustExec(`create table t(a date, b datetime, c timestamp, d varchar(20));`)
tk.MustExec(`insert into t select "1234-12-29", "1234-12-29 16:24:13.9912", "2014-12-29 16:19:28", "12.34567";`)
// NOTE: the actually result is: 12341220 12341229.0 12341200 12341229.00,
// but Datum.ToString() don't format decimal length for float numbers.
result = tk.MustQuery(`select truncate(a, -1), truncate(a, 1), truncate(a, -2), truncate(a, 2) from t;`)
result.Check(testkit.Rows("12341220 12341229 12341200 12341229"))
// NOTE: the actually result is: 12341229162410 12341229162414.0 12341229162400 12341229162414.00,
// but Datum.ToString() don't format decimal length for float numbers.
result = tk.MustQuery(`select truncate(b, -1), truncate(b, 1), truncate(b, -2), truncate(b, 2) from t;`)
result.Check(testkit.Rows("12341229162410 12341229162414 12341229162400 12341229162414"))
// NOTE: the actually result is: 20141229161920 20141229161928.0 20141229161900 20141229161928.00,
// but Datum.ToString() don't format decimal length for float numbers.
result = tk.MustQuery(`select truncate(c, -1), truncate(c, 1), truncate(c, -2), truncate(c, 2) from t;`)
result.Check(testkit.Rows("20141229161920 20141229161928 20141229161900 20141229161928"))
result = tk.MustQuery(`select truncate(d, -1), truncate(d, 1), truncate(d, -2), truncate(d, 2) from t;`)
result.Check(testkit.Rows("10 12.3 0 12.34"))
result = tk.MustQuery(`select truncate(json_array(), 1), truncate("cascasc", 1);`)
result.Check(testkit.Rows("0 0"))
// for pow
result = tk.MustQuery("SELECT POW('12', 2), POW(1.2e1, '2.0'), POW(12, 2.0);")
result.Check(testkit.Rows("144 144 144"))
result = tk.MustQuery("SELECT POW(null, 2), POW(2, null), POW(null, null);")
result.Check(testkit.Rows("<nil> <nil> <nil>"))
result = tk.MustQuery("SELECT POW(0, 0);")
result.Check(testkit.Rows("1"))
result = tk.MustQuery("SELECT POW(0, 0.1), POW(0, 0.5), POW(0, 1);")
result.Check(testkit.Rows("0 0 0"))
rs, err = tk.Exec("SELECT POW(0, -1);")
c.Assert(err, IsNil)
_, err = session.GetRows4Test(ctx, tk.Se, rs)
c.Assert(err, NotNil)
terr = errors.Cause(err).(*terror.Error)
c.Assert(terr.Code(), Equals, errors.ErrCode(mysql.ErrDataOutOfRange))
c.Assert(rs.Close(), IsNil)
// for sign
result = tk.MustQuery("SELECT SIGN('12'), SIGN(1.2e1), SIGN(12), SIGN(0.0000012);")
result.Check(testkit.Rows("1 1 1 1"))
result = tk.MustQuery("SELECT SIGN('-12'), SIGN(-1.2e1), SIGN(-12), SIGN(-0.0000012);")
result.Check(testkit.Rows("-1 -1 -1 -1"))
result = tk.MustQuery("SELECT SIGN('0'), SIGN('-0'), SIGN(0);")
result.Check(testkit.Rows("0 0 0"))
result = tk.MustQuery("SELECT SIGN(NULL);")
result.Check(testkit.Rows("<nil>"))
result = tk.MustQuery("SELECT SIGN(-9223372036854775808), SIGN(9223372036854775808);")
result.Check(testkit.Rows("-1 1"))
// for sqrt
result = tk.MustQuery("SELECT SQRT(-10), SQRT(144), SQRT(4.84), SQRT(0.04), SQRT(0);")
result.Check(testkit.Rows("<nil> 12 2.2 0.2 0"))
// for crc32
result = tk.MustQuery("SELECT crc32(0), crc32(-0), crc32('0'), crc32('abc'), crc32('ABC'), crc32(NULL), crc32(''), crc32('hello world!')")
result.Check(testkit.Rows("4108050209 4108050209 4108050209 891568578 2743272264 <nil> 0 62177901"))
// for radians
result = tk.MustQuery("SELECT radians(1.0), radians(pi()), radians(pi()/2), radians(180), radians(1.009);")
result.Check(testkit.Rows("0.017453292519943295 0.05483113556160754 0.02741556778080377 3.141592653589793 0.01761037215262278"))
// for rand
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int)")
tk.MustExec("insert into t values(1),(2),(3)")
tk.Se.GetSessionVars().MaxChunkSize = 1
tk.MustQuery("select rand(1) from t").Sort().Check(testkit.Rows("0.1418603212962489", "0.40540353712197724", "0.8716141803857071"))
tk.MustQuery("select rand(a) from t").Check(testkit.Rows("0.40540353712197724", "0.6555866465490187", "0.9057697559760601"))
tk.MustQuery("select rand(1), rand(2), rand(3)").Check(testkit.Rows("0.40540353712197724 0.6555866465490187 0.9057697559760601"))
}
func (s *testIntegrationSuite2) TestStringBuiltin(c *C) {
defer s.cleanEnv(c)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
var err error
// for length
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b double, c datetime, d time, e char(20), f bit(10))")
tk.MustExec(`insert into t values(1, 1.1, "2017-01-01 12:01:01", "12:01:01", "abcdef", 0b10101)`)
result := tk.MustQuery("select length(a), length(b), length(c), length(d), length(e), length(f), length(null) from t")
result.Check(testkit.Rows("1 3 19 8 6 2 <nil>"))
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a char(20))")
tk.MustExec(`insert into t values("tidb "), (concat("a ", "b "))`)
result = tk.MustQuery("select a, length(a) from t")
result.Check(testkit.Rows("tidb 4", "a b 4"))
// for concat
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b double, c datetime, d time, e char(20))")
tk.MustExec(`insert into t values(1, 1.1, "2017-01-01 12:01:01", "12:01:01", "abcdef")`)
result = tk.MustQuery("select concat(a, b, c, d, e) from t")
result.Check(testkit.Rows("11.12017-01-01 12:01:0112:01:01abcdef"))
result = tk.MustQuery("select concat(null)")
result.Check(testkit.Rows("<nil>"))
result = tk.MustQuery("select concat(null, a, b) from t")
result.Check(testkit.Rows("<nil>"))
tk.MustExec("drop table if exists t")
// Fix issue 9123
tk.MustExec("create table t(a char(32) not null, b float default '0') engine=innodb default charset=utf8mb4")
tk.MustExec("insert into t value('0a6f9d012f98467f8e671e9870044528', 208.867)")
result = tk.MustQuery("select concat_ws( ',', b) from t where a = '0a6f9d012f98467f8e671e9870044528';")
result.Check(testkit.Rows("208.867"))
// for concat_ws
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b double, c datetime, d time, e char(20))")
tk.MustExec(`insert into t values(1, 1.1, "2017-01-01 12:01:01", "12:01:01", "abcdef")`)
result = tk.MustQuery("select concat_ws('|', a, b, c, d, e) from t")
result.Check(testkit.Rows("1|1.1|2017-01-01 12:01:01|12:01:01|abcdef"))
result = tk.MustQuery("select concat_ws(null, null)")
result.Check(testkit.Rows("<nil>"))
result = tk.MustQuery("select concat_ws(null, a, b) from t")
result.Check(testkit.Rows("<nil>"))
result = tk.MustQuery("select concat_ws(',', 'a', 'b')")
result.Check(testkit.Rows("a,b"))
result = tk.MustQuery("select concat_ws(',','First name',NULL,'Last Name')")
result.Check(testkit.Rows("First name,Last Name"))
tk.MustExec(`drop table if exists t;`)
tk.MustExec(`create table t(a tinyint(2), b varchar(10));`)
tk.MustExec(`insert into t values (1, 'a'), (12, 'a'), (126, 'a'), (127, 'a')`)
tk.MustQuery(`select concat_ws('#', a, b) from t;`).Check(testkit.Rows(
`1#a`,
`12#a`,
`126#a`,
`127#a`,
))
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a binary(3))")
tk.MustExec("insert into t values('a')")
result = tk.MustQuery(`select concat_ws(',', a, 'test') = 'a\0\0,test' from t`)
result.Check(testkit.Rows("1"))
// for ascii
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a char(10), b int, c double, d datetime, e time, f bit(4))")
tk.MustExec(`insert into t values('2', 2, 2.3, "2017-01-01 12:01:01", "12:01:01", 0b1010)`)
result = tk.MustQuery("select ascii(a), ascii(b), ascii(c), ascii(d), ascii(e), ascii(f) from t")
result.Check(testkit.Rows("50 50 50 50 49 10"))
result = tk.MustQuery("select ascii('123'), ascii(123), ascii(''), ascii('你好'), ascii(NULL)")
result.Check(testkit.Rows("49 49 0 228 <nil>"))
// for lower
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b double, c datetime, d time, e char(20), f binary(3), g binary(3))")
tk.MustExec(`insert into t values(1, 1.1, "2017-01-01 12:01:01", "12:01:01", "abcdef", 'aa', 'BB')`)
result = tk.MustQuery("select lower(a), lower(b), lower(c), lower(d), lower(e), lower(f), lower(g), lower(null) from t")
result.Check(testkit.Rows("1 1.1 2017-01-01 12:01:01 12:01:01 abcdef aa\x00 BB\x00 <nil>"))
// for upper
result = tk.MustQuery("select upper(a), upper(b), upper(c), upper(d), upper(e), upper(f), upper(g), upper(null) from t")
result.Check(testkit.Rows("1 1.1 2017-01-01 12:01:01 12:01:01 ABCDEF aa\x00 BB\x00 <nil>"))
// for strcmp
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a char(10), b int, c double, d datetime, e time)")
tk.MustExec(`insert into t values("123", 123, 12.34, "2017-01-01 12:01:01", "12:01:01")`)
result = tk.MustQuery(`select strcmp(a, "123"), strcmp(b, "123"), strcmp(c, "12.34"), strcmp(d, "2017-01-01 12:01:01"), strcmp(e, "12:01:01") from t`)
result.Check(testkit.Rows("0 0 0 0 0"))
result = tk.MustQuery(`select strcmp("1", "123"), strcmp("123", "1"), strcmp("123", "45"), strcmp("123", null), strcmp(null, "123")`)
result.Check(testkit.Rows("-1 1 -1 <nil> <nil>"))
result = tk.MustQuery(`select strcmp("", "123"), strcmp("123", ""), strcmp("", ""), strcmp("", null), strcmp(null, "")`)
result.Check(testkit.Rows("-1 1 0 <nil> <nil>"))
// for left
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a char(10), b int, c double, d datetime, e time)")
tk.MustExec(`insert into t values('abcde', 1234, 12.34, "2017-01-01 12:01:01", "12:01:01")`)
result = tk.MustQuery("select left(a, 2), left(b, 2), left(c, 2), left(d, 2), left(e, 2) from t")
result.Check(testkit.Rows("ab 12 12 20 12"))
result = tk.MustQuery(`select left("abc", 0), left("abc", -1), left(NULL, 1), left("abc", NULL)`)
result.Check(testkit.Rows(" <nil> <nil>"))
result = tk.MustQuery(`select left("abc", "a"), left("abc", 1.9), left("abc", 1.2)`)
result.Check(testkit.Rows(" ab a"))
result = tk.MustQuery(`select left("中文abc", 2), left("中文abc", 3), left("中文abc", 4)`)
result.Check(testkit.Rows("中文 中文a 中文ab"))
// for right, reuse the table created for left
result = tk.MustQuery("select right(a, 3), right(b, 3), right(c, 3), right(d, 3), right(e, 3) from t")
result.Check(testkit.Rows("cde 234 .34 :01 :01"))
result = tk.MustQuery(`select right("abcde", 0), right("abcde", -1), right("abcde", 100), right(NULL, 1), right("abcde", NULL)`)
result.Check(testkit.Rows(" abcde <nil> <nil>"))
result = tk.MustQuery(`select right("abcde", "a"), right("abcde", 1.9), right("abcde", 1.2)`)
result.Check(testkit.Rows(" de e"))
result = tk.MustQuery(`select right("中文abc", 2), right("中文abc", 4), right("中文abc", 5)`)
result.Check(testkit.Rows("bc 文abc 中文abc"))
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a binary(10))")
tk.MustExec(`insert into t select "中文abc"`)
result = tk.MustQuery(`select left(a, 3), left(a, 6), left(a, 7) from t`)
result.Check(testkit.Rows("中 中文 中文a"))
result = tk.MustQuery(`select right(a, 2), right(a, 7) from t`)
result.Check(testkit.Rows("c\x00 文abc\x00"))
// for ord
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a char(10), b int, c double, d datetime, e time, f bit(4), g binary(20), h blob(10), i text(30))")
tk.MustExec(`insert into t values('2', 2, 2.3, "2017-01-01 12:01:01", "12:01:01", 0b1010, "512", "48", "tidb")`)
result = tk.MustQuery("select ord(a), ord(b), ord(c), ord(d), ord(e), ord(f), ord(g), ord(h), ord(i) from t")
result.Check(testkit.Rows("50 50 50 50 49 10 53 52 116"))
result = tk.MustQuery("select ord('123'), ord(123), ord(''), ord('你好'), ord(NULL), ord('👍')")
result.Check(testkit.Rows("49 49 0 14990752 <nil> 4036989325"))
result = tk.MustQuery("select ord(X''), ord(X'6161'), ord(X'e4bd'), ord(X'e4bda0'), ord(_ascii'你'), ord(_latin1'你')")
result.Check(testkit.Rows("0 97 228 228 228 228"))
// for space
result = tk.MustQuery(`select space(0), space(2), space(-1), space(1.1), space(1.9)`)
result.Check(testutil.RowsWithSep(",", ", ,, , "))
result = tk.MustQuery(`select space("abc"), space("2"), space("1.1"), space(''), space(null)`)
result.Check(testutil.RowsWithSep(",", ", , ,,<nil>"))
// for replace
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a char(20), b int, c double, d datetime, e time)")
tk.MustExec(`insert into t values('www.mysql.com', 1234, 12.34, "2017-01-01 12:01:01", "12:01:01")`)
result = tk.MustQuery(`select replace(a, 'mysql', 'pingcap'), replace(b, 2, 55), replace(c, 34, 0), replace(d, '-', '/'), replace(e, '01', '22') from t`)
result.Check(testutil.RowsWithSep(",", "www.pingcap.com,15534,12.0,2017/01/01 12:01:01,12:22:22"))
result = tk.MustQuery(`select replace('aaa', 'a', ''), replace(null, 'a', 'b'), replace('a', null, 'b'), replace('a', 'b', null)`)
result.Check(testkit.Rows(" <nil> <nil> <nil>"))
// for tobase64
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b double, c datetime, d time, e char(20), f bit(10), g binary(20), h blob(10))")
tk.MustExec(`insert into t values(1, 1.1, "2017-01-01 12:01:01", "12:01:01", "abcdef", 0b10101, "512", "abc")`)
result = tk.MustQuery("select to_base64(a), to_base64(b), to_base64(c), to_base64(d), to_base64(e), to_base64(f), to_base64(g), to_base64(h), to_base64(null) from t")
result.Check(testkit.Rows("MQ== MS4x MjAxNy0wMS0wMSAxMjowMTowMQ== MTI6MDE6MDE= YWJjZGVm ABU= NTEyAAAAAAAAAAAAAAAAAAAAAAA= YWJj <nil>"))
// for from_base64
result = tk.MustQuery(`select from_base64("abcd"), from_base64("asc")`)
result.Check(testkit.Rows("i\xb7\x1d <nil>"))
result = tk.MustQuery(`select from_base64("MQ=="), from_base64(1234)`)
result.Check(testkit.Rows("1 \xd7m\xf8"))
// for substr
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a char(10), b int, c double, d datetime, e time)")
tk.MustExec(`insert into t values('Sakila', 12345, 123.45, "2017-01-01 12:01:01", "12:01:01")`)
result = tk.MustQuery(`select substr(a, 3), substr(b, 2, 3), substr(c, -3), substr(d, -8), substr(e, -3, 100) from t`)
result.Check(testkit.Rows("kila 234 .45 12:01:01 :01"))
result = tk.MustQuery(`select substr('Sakila', 100), substr('Sakila', -100), substr('Sakila', -5, 3), substr('Sakila', 2, -1)`)
result.Check(testutil.RowsWithSep(",", ",,aki,"))
result = tk.MustQuery(`select substr('foobarbar' from 4), substr('Sakila' from -4 for 2)`)
result.Check(testkit.Rows("barbar ki"))
result = tk.MustQuery(`select substr(null, 2, 3), substr('foo', null, 3), substr('foo', 2, null)`)
result.Check(testkit.Rows("<nil> <nil> <nil>"))
result = tk.MustQuery(`select substr('中文abc', 2), substr('中文abc', 3), substr("中文abc", 1, 2)`)
result.Check(testkit.Rows("文abc abc 中文"))
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a binary(10))")
tk.MustExec(`insert into t select "中文abc"`)
result = tk.MustQuery(`select substr(a, 4), substr(a, 1, 3), substr(a, 1, 6) from t`)
result.Check(testkit.Rows("文abc\x00 中 中文"))
result = tk.MustQuery(`select substr("string", -1), substr("string", -2), substr("中文", -1), substr("中文", -2) from t`)
result.Check(testkit.Rows("g ng 文 中文"))
// for bit_length
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b double, c datetime, d time, e char(20), f bit(10), g binary(20), h varbinary(20))")
tk.MustExec(`insert into t values(1, 1.1, "2017-01-01 12:01:01", "12:01:01", "abcdef", 0b10101, "g", "h")`)
result = tk.MustQuery("select bit_length(a), bit_length(b), bit_length(c), bit_length(d), bit_length(e), bit_length(f), bit_length(g), bit_length(h), bit_length(null) from t")
result.Check(testkit.Rows("8 24 152 64 48 16 160 8 <nil>"))
// for substring_index
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a char(20), b int, c double, d datetime, e time)")
tk.MustExec(`insert into t values('www.pingcap.com', 12345, 123.45, "2017-01-01 12:01:01", "12:01:01")`)
result = tk.MustQuery(`select substring_index(a, '.', 2), substring_index(b, '.', 2), substring_index(c, '.', -1), substring_index(d, '-', 1), substring_index(e, ':', -2) from t`)
result.Check(testkit.Rows("www.pingcap 12345 45 2017 01:01"))
result = tk.MustQuery(`select substring_index('www.pingcap.com', '.', 0), substring_index('www.pingcap.com', '.', 100), substring_index('www.pingcap.com', '.', -100)`)
result.Check(testkit.Rows(" www.pingcap.com www.pingcap.com"))
tk.MustQuery(`select substring_index('xyz', 'abc', 9223372036854775808)`).Check(testkit.Rows(``))
result = tk.MustQuery(`select substring_index('www.pingcap.com', 'd', 1), substring_index('www.pingcap.com', '', 1), substring_index('', '.', 1)`)
result.Check(testutil.RowsWithSep(",", "www.pingcap.com,,"))
result = tk.MustQuery(`select substring_index(null, '.', 1), substring_index('www.pingcap.com', null, 1), substring_index('www.pingcap.com', '.', null)`)
result.Check(testkit.Rows("<nil> <nil> <nil>"))
// for hex
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a char(20), b int, c double, d datetime, e time, f decimal(5, 2), g bit(4))")
tk.MustExec(`insert into t values('www.pingcap.com', 12345, 123.45, "2017-01-01 12:01:01", "12:01:01", 123.45, 0b1100)`)
result = tk.MustQuery(`select hex(a), hex(b), hex(c), hex(d), hex(e), hex(f), hex(g) from t`)
result.Check(testkit.Rows("7777772E70696E676361702E636F6D 3039 7B 323031372D30312D30312031323A30313A3031 31323A30313A3031 7B C"))
result = tk.MustQuery(`select hex('abc'), hex('你好'), hex(12), hex(12.3), hex(12.8)`)
result.Check(testkit.Rows("616263 E4BDA0E5A5BD C C D"))
result = tk.MustQuery(`select hex(-1), hex(-12.3), hex(-12.8), hex(0x12), hex(null)`)
result.Check(testkit.Rows("FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFF4 FFFFFFFFFFFFFFF3 12 <nil>"))
tk.MustExec("drop table if exists t")
tk.MustExec("CREATE TABLE t(i int primary key auto_increment, a binary, b binary(0), c binary(20), d binary(255)) character set utf8 collate utf8_bin;")
tk.MustExec("insert into t(a, b, c, d) values ('a', NULL, 'a','a');")
tk.MustQuery("select i, hex(a), hex(b), hex(c), hex(d) from t;").Check(testkit.Rows("1 61 <nil> 6100000000000000000000000000000000000000 610000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"))
// for unhex
result = tk.MustQuery(`select unhex('4D7953514C'), unhex('313233'), unhex(313233), unhex('')`)
result.Check(testkit.Rows("MySQL 123 123 "))
result = tk.MustQuery(`select unhex('string'), unhex('你好'), unhex(123.4), unhex(null)`)
result.Check(testkit.Rows("<nil> <nil> <nil> <nil>"))
// for ltrim and rtrim
result = tk.MustQuery(`select ltrim(' bar '), ltrim('bar'), ltrim(''), ltrim(null)`)
result.Check(testutil.RowsWithSep(",", "bar ,bar,,<nil>"))
result = tk.MustQuery(`select rtrim(' bar '), rtrim('bar'), rtrim(''), rtrim(null)`)
result.Check(testutil.RowsWithSep(",", " bar,bar,,<nil>"))
result = tk.MustQuery(`select ltrim("\t bar "), ltrim(" \tbar"), ltrim("\n bar"), ltrim("\r bar")`)
result.Check(testutil.RowsWithSep(",", "\t bar ,\tbar,\n bar,\r bar"))
result = tk.MustQuery(`select rtrim(" bar \t"), rtrim("bar\t "), rtrim("bar \n"), rtrim("bar \r")`)
result.Check(testutil.RowsWithSep(",", " bar \t,bar\t,bar \n,bar \r"))
// for reverse
tk.MustExec(`DROP TABLE IF EXISTS t;`)
tk.MustExec(`CREATE TABLE t(a BINARY(6));`)
tk.MustExec(`INSERT INTO t VALUES("中文");`)
result = tk.MustQuery(`SELECT a, REVERSE(a), REVERSE("中文"), REVERSE("123 ") FROM t;`)
result.Check(testkit.Rows("中文 \x87\x96歸\xe4 文中 321"))
result = tk.MustQuery(`SELECT REVERSE(123), REVERSE(12.09) FROM t;`)
result.Check(testkit.Rows("321 90.21"))
// for trim
result = tk.MustQuery(`select trim(' bar '), trim(leading 'x' from 'xxxbarxxx'), trim(trailing 'xyz' from 'barxxyz'), trim(both 'x' from 'xxxbarxxx')`)
result.Check(testkit.Rows("bar barxxx barx bar"))
result = tk.MustQuery(`select trim('\t bar\n '), trim(' \rbar \t')`)
result.Check(testutil.RowsWithSep(",", "\t bar\n,\rbar \t"))
result = tk.MustQuery(`select trim(leading from ' bar'), trim('x' from 'xxxbarxxx'), trim('x' from 'bar'), trim('' from ' bar ')`)
result.Check(testutil.RowsWithSep(",", "bar,bar,bar, bar "))
result = tk.MustQuery(`select trim(''), trim('x' from '')`)
result.Check(testutil.RowsWithSep(",", ","))
result = tk.MustQuery(`select trim(null from 'bar'), trim('x' from null), trim(null), trim(leading null from 'bar')`)
// FIXME: the result for trim(leading null from 'bar') should be <nil>, current is 'bar'
result.Check(testkit.Rows("<nil> <nil> <nil> bar"))
// for locate
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a char(20), b int, c double, d datetime, e time, f binary(5))")
tk.MustExec(`insert into t values('www.pingcap.com', 12345, 123.45, "2017-01-01 12:01:01", "12:01:01", "HelLo")`)
result = tk.MustQuery(`select locate(".ping", a), locate(".ping", a, 5) from t`)
result.Check(testkit.Rows("4 0"))
result = tk.MustQuery(`select locate("234", b), locate("235", b, 10) from t`)
result.Check(testkit.Rows("2 0"))
result = tk.MustQuery(`select locate(".45", c), locate(".35", b) from t`)
result.Check(testkit.Rows("4 0"))
result = tk.MustQuery(`select locate("El", f), locate("ll", f), locate("lL", f), locate("Lo", f), locate("lo", f) from t`)
result.Check(testkit.Rows("0 0 3 4 0"))
result = tk.MustQuery(`select locate("01 12", d) from t`)
result.Check(testkit.Rows("9"))
result = tk.MustQuery(`select locate("文", "中文字符串", 2)`)
result.Check(testkit.Rows("2"))
result = tk.MustQuery(`select locate("文", "中文字符串", 3)`)
result.Check(testkit.Rows("0"))
result = tk.MustQuery(`select locate("文", "中文字符串")`)
result.Check(testkit.Rows("2"))
// for bin
result = tk.MustQuery(`select bin(-1);`)
result.Check(testkit.Rows("1111111111111111111111111111111111111111111111111111111111111111"))
result = tk.MustQuery(`select bin(5);`)