github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/dbs/memristed/memex/integration_test.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package memex_test
    15  
    16  import (
    17  	"bytes"
    18  	"context"
    19  	"fmt"
    20  	"math"
    21  	"sort"
    22  	"strconv"
    23  	"strings"
    24  	"time"
    25  
    26  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    27  	"github.com/whtcorpsinc/BerolinaSQL/auth"
    28  	"github.com/whtcorpsinc/BerolinaSQL/perceptron"
    29  	"github.com/whtcorpsinc/BerolinaSQL/terror"
    30  	. "github.com/whtcorpsinc/check"
    31  	"github.com/whtcorpsinc/errors"
    32  	"github.com/whtcorpsinc/milevadb/causet"
    33  	causetembedded "github.com/whtcorpsinc/milevadb/causet/embedded"
    34  	"github.com/whtcorpsinc/milevadb/causetstore/mockstore"
    35  	"github.com/whtcorpsinc/milevadb/ekv"
    36  	"github.com/whtcorpsinc/milevadb/memex"
    37  	"github.com/whtcorpsinc/milevadb/petri"
    38  	"github.com/whtcorpsinc/milevadb/soliton/defCauslate"
    39  	"github.com/whtcorpsinc/milevadb/soliton/ekvcache"
    40  	"github.com/whtcorpsinc/milevadb/soliton/mock"
    41  	"github.com/whtcorpsinc/milevadb/soliton/solitonutil"
    42  	"github.com/whtcorpsinc/milevadb/soliton/sqlexec"
    43  	"github.com/whtcorpsinc/milevadb/soliton/testkit"
    44  	"github.com/whtcorpsinc/milevadb/stochastik"
    45  	"github.com/whtcorpsinc/milevadb/stochastikctx"
    46  	"github.com/whtcorpsinc/milevadb/stochastikctx/variable"
    47  	"github.com/whtcorpsinc/milevadb/types"
    48  )
    49  
    50  var _ = Suite(&testIntegrationSuite{})
    51  var _ = Suite(&testIntegrationSuite2{})
    52  var _ = SerialSuites(&testIntegrationSerialSuite{})
    53  
    54  type testIntegrationSuiteBase struct {
    55  	causetstore ekv.CausetStorage
    56  	dom         *petri.Petri
    57  	ctx         stochastikctx.Context
    58  }
    59  
    60  type testIntegrationSuite struct {
    61  	testIntegrationSuiteBase
    62  }
    63  
    64  type testIntegrationSuite2 struct {
    65  	testIntegrationSuiteBase
    66  }
    67  
    68  type testIntegrationSerialSuite struct {
    69  	testIntegrationSuiteBase
    70  }
    71  
    72  func (s *testIntegrationSuiteBase) cleanEnv(c *C) {
    73  	tk := testkit.NewTestKit(c, s.causetstore)
    74  	tk.MustInterDirc("use test")
    75  	r := tk.MustQuery("show blocks")
    76  	for _, tb := range r.Events() {
    77  		blockName := tb[0]
    78  		tk.MustInterDirc(fmt.Sprintf("drop causet %v", blockName))
    79  	}
    80  }
    81  
    82  func (s *testIntegrationSuiteBase) SetUpSuite(c *C) {
    83  	var err error
    84  	s.causetstore, s.dom, err = newStoreWithBootstrap()
    85  	c.Assert(err, IsNil)
    86  	s.ctx = mock.NewContext()
    87  }
    88  
    89  func (s *testIntegrationSuiteBase) TearDownSuite(c *C) {
    90  	s.dom.Close()
    91  	s.causetstore.Close()
    92  }
    93  
    94  func (s *testIntegrationSuite) Test19654(c *C) {
    95  	tk := testkit.NewTestKit(c, s.causetstore)
    96  	tk.MustInterDirc("USE test;")
    97  
    98  	// enum vs enum
    99  	tk.MustInterDirc("drop causet if exists t1, t2;")
   100  	tk.MustInterDirc("create causet t1 (b enum('a', 'b'));")
   101  	tk.MustInterDirc("insert into t1 values ('a');")
   102  	tk.MustInterDirc("create causet t2 (b enum('b','a') not null, unique(b));")
   103  	tk.MustInterDirc("insert into t2 values ('a');")
   104  	tk.MustQuery("select /*+ inl_join(t2)*/ * from t1, t2 where t1.b=t2.b;").Check(testkit.Events("a a"))
   105  
   106  	// set vs set
   107  	tk.MustInterDirc("drop causet if exists t1, t2;")
   108  	tk.MustInterDirc("create causet t1 (b set('a', 'b'));")
   109  	tk.MustInterDirc("insert into t1 values ('a');")
   110  	tk.MustInterDirc("create causet t2 (b set('b','a') not null, unique(b));")
   111  	tk.MustInterDirc("insert into t2 values ('a');")
   112  	tk.MustQuery("select /*+ inl_join(t2)*/ * from t1, t2 where t1.b=t2.b;").Check(testkit.Events("a a"))
   113  
   114  	// enum vs set
   115  	tk.MustInterDirc("drop causet if exists t1, t2;")
   116  	tk.MustInterDirc("create causet t1 (b enum('a', 'b'));")
   117  	tk.MustInterDirc("insert into t1 values ('a');")
   118  	tk.MustInterDirc("create causet t2 (b set('b','a') not null, unique(b));")
   119  	tk.MustInterDirc("insert into t2 values ('a');")
   120  	tk.MustQuery("select /*+ inl_join(t2)*/ * from t1, t2 where t1.b=t2.b;").Check(testkit.Events("a a"))
   121  
   122  	// char vs enum
   123  	tk.MustInterDirc("drop causet if exists t1, t2;")
   124  	tk.MustInterDirc("create causet t1 (b char(10));")
   125  	tk.MustInterDirc("insert into t1 values ('a');")
   126  	tk.MustInterDirc("create causet t2 (b enum('b','a') not null, unique(b));")
   127  	tk.MustInterDirc("insert into t2 values ('a');")
   128  	tk.MustQuery("select /*+ inl_join(t2)*/ * from t1, t2 where t1.b=t2.b;").Check(testkit.Events("a a"))
   129  
   130  	// char vs set
   131  	tk.MustInterDirc("drop causet if exists t1, t2;")
   132  	tk.MustInterDirc("create causet t1 (b char(10));")
   133  	tk.MustInterDirc("insert into t1 values ('a');")
   134  	tk.MustInterDirc("create causet t2 (b set('b','a') not null, unique(b));")
   135  	tk.MustInterDirc("insert into t2 values ('a');")
   136  	tk.MustQuery("select /*+ inl_join(t2)*/ * from t1, t2 where t1.b=t2.b;").Check(testkit.Events("a a"))
   137  }
   138  
   139  func (s *testIntegrationSuite) TestFuncREPEAT(c *C) {
   140  	tk := testkit.NewTestKit(c, s.causetstore)
   141  	defer s.cleanEnv(c)
   142  	tk.MustInterDirc("USE test;")
   143  	tk.MustInterDirc("DROP TABLE IF EXISTS block_string;")
   144  	tk.MustInterDirc("CREATE TABLE block_string(a CHAR(20), b VARCHAR(20), c TINYTEXT, d TEXT(20), e MEDIUMTEXT, f LONGTEXT, g BIGINT);")
   145  	tk.MustInterDirc("INSERT INTO block_string (a, b, c, d, e, f, g) VALUES ('a', 'b', 'c', 'd', 'e', 'f', 2);")
   146  	tk.CheckInterDircResult(1, 0)
   147  
   148  	r := tk.MustQuery("SELECT REPEAT(a, g), REPEAT(b, g), REPEAT(c, g), REPEAT(d, g), REPEAT(e, g), REPEAT(f, g) FROM block_string;")
   149  	r.Check(testkit.Events("aa bb cc dd ee ff"))
   150  
   151  	r = tk.MustQuery("SELECT REPEAT(NULL, g), REPEAT(NULL, g), REPEAT(NULL, g), REPEAT(NULL, g), REPEAT(NULL, g), REPEAT(NULL, g) FROM block_string;")
   152  	r.Check(testkit.Events("<nil> <nil> <nil> <nil> <nil> <nil>"))
   153  
   154  	r = tk.MustQuery("SELECT REPEAT(a, NULL), REPEAT(b, NULL), REPEAT(c, NULL), REPEAT(d, NULL), REPEAT(e, NULL), REPEAT(f, NULL) FROM block_string;")
   155  	r.Check(testkit.Events("<nil> <nil> <nil> <nil> <nil> <nil>"))
   156  
   157  	r = tk.MustQuery("SELECT REPEAT(a, 2), REPEAT(b, 2), REPEAT(c, 2), REPEAT(d, 2), REPEAT(e, 2), REPEAT(f, 2) FROM block_string;")
   158  	r.Check(testkit.Events("aa bb cc dd ee ff"))
   159  
   160  	r = tk.MustQuery("SELECT REPEAT(NULL, 2), REPEAT(NULL, 2), REPEAT(NULL, 2), REPEAT(NULL, 2), REPEAT(NULL, 2), REPEAT(NULL, 2) FROM block_string;")
   161  	r.Check(testkit.Events("<nil> <nil> <nil> <nil> <nil> <nil>"))
   162  
   163  	r = tk.MustQuery("SELECT REPEAT(a, -1), REPEAT(b, -2), REPEAT(c, -2), REPEAT(d, -2), REPEAT(e, -2), REPEAT(f, -2) FROM block_string;")
   164  	r.Check(testkit.Events("     "))
   165  
   166  	r = tk.MustQuery("SELECT REPEAT(a, 0), REPEAT(b, 0), REPEAT(c, 0), REPEAT(d, 0), REPEAT(e, 0), REPEAT(f, 0) FROM block_string;")
   167  	r.Check(testkit.Events("     "))
   168  
   169  	r = tk.MustQuery("SELECT REPEAT(a, 16777217), REPEAT(b, 16777217), REPEAT(c, 16777217), REPEAT(d, 16777217), REPEAT(e, 16777217), REPEAT(f, 16777217) FROM block_string;")
   170  	r.Check(testkit.Events("<nil> <nil> <nil> <nil> <nil> <nil>"))
   171  }
   172  
   173  func (s *testIntegrationSuite) TestFuncLpadAndRpad(c *C) {
   174  	tk := testkit.NewTestKit(c, s.causetstore)
   175  	defer s.cleanEnv(c)
   176  	tk.MustInterDirc(`USE test;`)
   177  	tk.MustInterDirc(`DROP TABLE IF EXISTS t;`)
   178  	tk.MustInterDirc(`CREATE TABLE t(a BINARY(10), b CHAR(10));`)
   179  	tk.MustInterDirc(`INSERT INTO t SELECT "中文", "abc";`)
   180  	result := tk.MustQuery(`SELECT LPAD(a, 11, "a"), LPAD(b, 2, "xx") FROM t;`)
   181  	result.Check(testkit.Events("a中文\x00\x00\x00\x00 ab"))
   182  	result = tk.MustQuery(`SELECT RPAD(a, 11, "a"), RPAD(b, 2, "xx") FROM t;`)
   183  	result.Check(testkit.Events("中文\x00\x00\x00\x00a ab"))
   184  	result = tk.MustQuery(`SELECT LPAD("中文", 5, "字符"), LPAD("中文", 1, "a");`)
   185  	result.Check(testkit.Events("字符字中文 中"))
   186  	result = tk.MustQuery(`SELECT RPAD("中文", 5, "字符"), RPAD("中文", 1, "a");`)
   187  	result.Check(testkit.Events("中文字符字 中"))
   188  	result = tk.MustQuery(`SELECT RPAD("中文", -5, "字符"), RPAD("中文", 10, "");`)
   189  	result.Check(testkit.Events("<nil> <nil>"))
   190  	result = tk.MustQuery(`SELECT LPAD("中文", -5, "字符"), LPAD("中文", 10, "");`)
   191  	result.Check(testkit.Events("<nil> <nil>"))
   192  }
   193  
   194  func (s *testIntegrationSuite) TestMiscellaneousBuiltin(c *C) {
   195  	ctx := context.Background()
   196  	defer s.cleanEnv(c)
   197  
   198  	tk := testkit.NewTestKit(c, s.causetstore)
   199  	tk.MustInterDirc("use test")
   200  	// for uuid
   201  	r := tk.MustQuery("select uuid(), uuid(), uuid(), uuid(), uuid(), uuid();")
   202  	for _, it := range r.Events() {
   203  		for _, item := range it {
   204  			uuid, ok := item.(string)
   205  			c.Assert(ok, Equals, true)
   206  			list := strings.Split(uuid, "-")
   207  			c.Assert(len(list), Equals, 5)
   208  			c.Assert(len(list[0]), Equals, 8)
   209  			c.Assert(len(list[1]), Equals, 4)
   210  			c.Assert(len(list[2]), Equals, 4)
   211  			c.Assert(len(list[3]), Equals, 4)
   212  			c.Assert(len(list[4]), Equals, 12)
   213  		}
   214  	}
   215  	tk.MustQuery("select sleep(1);").Check(testkit.Events("0"))
   216  	tk.MustQuery("select sleep(0);").Check(testkit.Events("0"))
   217  	tk.MustQuery("select sleep('a');").Check(testkit.Events("0"))
   218  	tk.MustQuery("show warnings;").Check(testkit.Events("Warning 1292 Truncated incorrect FLOAT value: 'a'"))
   219  	rs, err := tk.InterDirc("select sleep(-1);")
   220  	c.Assert(err, IsNil)
   221  	c.Assert(rs, NotNil)
   222  	_, err = stochastik.GetEvents4Test(ctx, tk.Se, rs)
   223  	c.Assert(err, NotNil)
   224  	c.Assert(rs.Close(), IsNil)
   225  
   226  	tk.MustQuery("SELECT INET_ATON('10.0.5.9');").Check(testkit.Events("167773449"))
   227  	tk.MustQuery("SELECT INET_NTOA(167773449);").Check(testkit.Events("10.0.5.9"))
   228  	tk.MustQuery("SELECT HEX(INET6_ATON('fdfe::5a55:caff:fefa:9089'));").Check(testkit.Events("FDFE0000000000005A55CAFFFEFA9089"))
   229  	tk.MustQuery("SELECT HEX(INET6_ATON('10.0.5.9'));").Check(testkit.Events("0A000509"))
   230  	tk.MustQuery("SELECT INET6_NTOA(INET6_ATON('fdfe::5a55:caff:fefa:9089'));").Check(testkit.Events("fdfe::5a55:caff:fefa:9089"))
   231  	tk.MustQuery("SELECT INET6_NTOA(INET6_ATON('10.0.5.9'));").Check(testkit.Events("10.0.5.9"))
   232  	tk.MustQuery("SELECT INET6_NTOA(UNHEX('FDFE0000000000005A55CAFFFEFA9089'));").Check(testkit.Events("fdfe::5a55:caff:fefa:9089"))
   233  	tk.MustQuery("SELECT INET6_NTOA(UNHEX('0A000509'));").Check(testkit.Events("10.0.5.9"))
   234  
   235  	tk.MustQuery(`SELECT IS_IPV4('10.0.5.9'), IS_IPV4('10.0.5.256');`).Check(testkit.Events("1 0"))
   236  	tk.MustQuery(`SELECT IS_IPV4_COMPAT(INET6_ATON('::10.0.5.9'));`).Check(testkit.Events("1"))
   237  	tk.MustQuery(`SELECT IS_IPV4_COMPAT(INET6_ATON('::ffff:10.0.5.9'));`).Check(testkit.Events("0"))
   238  	tk.MustQuery(`SELECT
   239  	  IS_IPV4_COMPAT(INET6_ATON('::192.168.0.1')),
   240  	  IS_IPV4_COMPAT(INET6_ATON('::c0a8:0001')),
   241  	  IS_IPV4_COMPAT(INET6_ATON('::c0a8:1'));`).Check(testkit.Events("1 1 1"))
   242  	tk.MustQuery(`SELECT IS_IPV4_MAPPED(INET6_ATON('::10.0.5.9'));`).Check(testkit.Events("0"))
   243  	tk.MustQuery(`SELECT IS_IPV4_MAPPED(INET6_ATON('::ffff:10.0.5.9'));`).Check(testkit.Events("1"))
   244  	tk.MustQuery(`SELECT
   245  	  IS_IPV4_MAPPED(INET6_ATON('::ffff:192.168.0.1')),
   246  	  IS_IPV4_MAPPED(INET6_ATON('::ffff:c0a8:0001')),
   247  	  IS_IPV4_MAPPED(INET6_ATON('::ffff:c0a8:1'));`).Check(testkit.Events("1 1 1"))
   248  	tk.MustQuery(`SELECT IS_IPV6('10.0.5.9'), IS_IPV6('::1');`).Check(testkit.Events("0 1"))
   249  
   250  	tk.MustInterDirc("drop causet if exists t1;")
   251  	tk.MustInterDirc(`create causet t1(
   252          a int,
   253          b int not null,
   254          c int not null default 0,
   255          d int default 0,
   256          unique key(b,c),
   257          unique key(b,d)
   258  );`)
   259  	tk.MustInterDirc("insert into t1 (a,b) values(1,10),(1,20),(2,30),(2,40);")
   260  	tk.MustQuery("select any_value(a), sum(b) from t1;").Check(testkit.Events("1 100"))
   261  	tk.MustQuery("select a,any_value(b),sum(c) from t1 group by a order by a;").Check(testkit.Events("1 10 0", "2 30 0"))
   262  
   263  	// for locks
   264  	tk.MustInterDirc(`set milevadb_enable_noop_functions=1;`)
   265  	result := tk.MustQuery(`SELECT GET_LOCK('test_lock1', 10);`)
   266  	result.Check(testkit.Events("1"))
   267  	result = tk.MustQuery(`SELECT GET_LOCK('test_lock2', 10);`)
   268  	result.Check(testkit.Events("1"))
   269  
   270  	result = tk.MustQuery(`SELECT RELEASE_LOCK('test_lock2');`)
   271  	result.Check(testkit.Events("1"))
   272  	result = tk.MustQuery(`SELECT RELEASE_LOCK('test_lock1');`)
   273  	result.Check(testkit.Events("1"))
   274  }
   275  
   276  func (s *testIntegrationSuite) TestConvertToBit(c *C) {
   277  	defer s.cleanEnv(c)
   278  	tk := testkit.NewTestKit(c, s.causetstore)
   279  	tk.MustInterDirc("use test")
   280  	tk.MustInterDirc("drop causet if exists t, t1")
   281  	tk.MustInterDirc("create causet t (a bit(64))")
   282  	tk.MustInterDirc("create causet t1 (a varchar(2))")
   283  	tk.MustInterDirc(`insert t1 value ('10')`)
   284  	tk.MustInterDirc(`insert t select a from t1`)
   285  	tk.MustQuery("select a+0 from t").Check(testkit.Events("12592"))
   286  
   287  	tk.MustInterDirc("drop causet if exists t, t1")
   288  	tk.MustInterDirc("create causet t (a bit(64))")
   289  	tk.MustInterDirc("create causet t1 (a binary(2))")
   290  	tk.MustInterDirc(`insert t1 value ('10')`)
   291  	tk.MustInterDirc(`insert t select a from t1`)
   292  	tk.MustQuery("select a+0 from t").Check(testkit.Events("12592"))
   293  
   294  	tk.MustInterDirc("drop causet if exists t, t1")
   295  	tk.MustInterDirc("create causet t (a bit(64))")
   296  	tk.MustInterDirc("create causet t1 (a datetime)")
   297  	tk.MustInterDirc(`insert t1 value ('09-01-01')`)
   298  	tk.MustInterDirc(`insert t select a from t1`)
   299  	tk.MustQuery("select a+0 from t").Check(testkit.Events("20090101000000"))
   300  }
   301  
   302  func (s *testIntegrationSuite2) TestMathBuiltin(c *C) {
   303  	ctx := context.Background()
   304  	defer s.cleanEnv(c)
   305  	tk := testkit.NewTestKit(c, s.causetstore)
   306  	tk.MustInterDirc("use test")
   307  
   308  	// for degrees
   309  	result := tk.MustQuery("select degrees(0), degrees(1)")
   310  	result.Check(testkit.Events("0 57.29577951308232"))
   311  	result = tk.MustQuery("select degrees(2), degrees(5)")
   312  	result.Check(testkit.Events("114.59155902616465 286.4788975654116"))
   313  
   314  	// for sin
   315  	result = tk.MustQuery("select sin(0), sin(1.5707963267949)")
   316  	result.Check(testkit.Events("0 1"))
   317  	result = tk.MustQuery("select sin(1), sin(100)")
   318  	result.Check(testkit.Events("0.8414709848078965 -0.5063656411097588"))
   319  	result = tk.MustQuery("select sin('abcd')")
   320  	result.Check(testkit.Events("0"))
   321  
   322  	// for cos
   323  	result = tk.MustQuery("select cos(0), cos(3.1415926535898)")
   324  	result.Check(testkit.Events("1 -1"))
   325  	result = tk.MustQuery("select cos('abcd')")
   326  	result.Check(testkit.Events("1"))
   327  
   328  	// for tan
   329  	result = tk.MustQuery("select tan(0.00), tan(PI()/4)")
   330  	result.Check(testkit.Events("0 1"))
   331  	result = tk.MustQuery("select tan('abcd')")
   332  	result.Check(testkit.Events("0"))
   333  
   334  	// for log2
   335  	result = tk.MustQuery("select log2(0.0)")
   336  	result.Check(testkit.Events("<nil>"))
   337  	result = tk.MustQuery("select log2(4)")
   338  	result.Check(testkit.Events("2"))
   339  	result = tk.MustQuery("select log2('8.0abcd')")
   340  	result.Check(testkit.Events("3"))
   341  	result = tk.MustQuery("select log2(-1)")
   342  	result.Check(testkit.Events("<nil>"))
   343  	result = tk.MustQuery("select log2(NULL)")
   344  	result.Check(testkit.Events("<nil>"))
   345  
   346  	// for log10
   347  	result = tk.MustQuery("select log10(0.0)")
   348  	result.Check(testkit.Events("<nil>"))
   349  	result = tk.MustQuery("select log10(100)")
   350  	result.Check(testkit.Events("2"))
   351  	result = tk.MustQuery("select log10('1000.0abcd')")
   352  	result.Check(testkit.Events("3"))
   353  	result = tk.MustQuery("select log10(-1)")
   354  	result.Check(testkit.Events("<nil>"))
   355  	result = tk.MustQuery("select log10(NULL)")
   356  	result.Check(testkit.Events("<nil>"))
   357  
   358  	//for log
   359  	result = tk.MustQuery("select log(0.0)")
   360  	result.Check(testkit.Events("<nil>"))
   361  	result = tk.MustQuery("select log(100)")
   362  	result.Check(testkit.Events("4.605170185988092"))
   363  	result = tk.MustQuery("select log('100.0abcd')")
   364  	result.Check(testkit.Events("4.605170185988092"))
   365  	result = tk.MustQuery("select log(-1)")
   366  	result.Check(testkit.Events("<nil>"))
   367  	result = tk.MustQuery("select log(NULL)")
   368  	result.Check(testkit.Events("<nil>"))
   369  	result = tk.MustQuery("select log(NULL, NULL)")
   370  	result.Check(testkit.Events("<nil>"))
   371  	result = tk.MustQuery("select log(1, 100)")
   372  	result.Check(testkit.Events("<nil>"))
   373  	result = tk.MustQuery("select log(0.5, 0.25)")
   374  	result.Check(testkit.Events("2"))
   375  	result = tk.MustQuery("select log(-1, 0.25)")
   376  	result.Check(testkit.Events("<nil>"))
   377  
   378  	// for atan
   379  	result = tk.MustQuery("select atan(0), atan(-1), atan(1), atan(1,2)")
   380  	result.Check(testkit.Events("0 -0.7853981633974483 0.7853981633974483 0.4636476090008061"))
   381  	result = tk.MustQuery("select atan('milevadb')")
   382  	result.Check(testkit.Events("0"))
   383  
   384  	// for asin
   385  	result = tk.MustQuery("select asin(0), asin(-2), asin(2), asin(1)")
   386  	result.Check(testkit.Events("0 <nil> <nil> 1.5707963267948966"))
   387  	result = tk.MustQuery("select asin('milevadb')")
   388  	result.Check(testkit.Events("0"))
   389  
   390  	// for acos
   391  	result = tk.MustQuery("select acos(0), acos(-2), acos(2), acos(1)")
   392  	result.Check(testkit.Events("1.5707963267948966 <nil> <nil> 0"))
   393  	result = tk.MustQuery("select acos('milevadb')")
   394  	result.Check(testkit.Events("1.5707963267948966"))
   395  
   396  	// for pi
   397  	result = tk.MustQuery("select pi()")
   398  	result.Check(testkit.Events("3.141592653589793"))
   399  
   400  	// for floor
   401  	result = tk.MustQuery("select floor(0), floor(null), floor(1.23), floor(-1.23), floor(1)")
   402  	result.Check(testkit.Events("0 <nil> 1 -2 1"))
   403  	result = tk.MustQuery("select floor('milevadb'), floor('1milevadb'), floor('milevadb1')")
   404  	result.Check(testkit.Events("0 1 0"))
   405  	result = tk.MustQuery("SELECT floor(t.c_datetime) FROM (select CAST('2020-07-19 00:00:00' AS DATETIME) AS c_datetime) AS t")
   406  	result.Check(testkit.Events("20170719000000"))
   407  	result = tk.MustQuery("SELECT floor(t.c_time) FROM (select CAST('12:34:56' AS TIME) AS c_time) AS t")
   408  	result.Check(testkit.Events("123456"))
   409  	result = tk.MustQuery("SELECT floor(t.c_time) FROM (select CAST('00:34:00' AS TIME) AS c_time) AS t")
   410  	result.Check(testkit.Events("3400"))
   411  	result = tk.MustQuery("SELECT floor(t.c_time) FROM (select CAST('00:00:00' AS TIME) AS c_time) AS t")
   412  	result.Check(testkit.Events("0"))
   413  	result = tk.MustQuery("SELECT floor(t.c_decimal) FROM (SELECT CAST('-10.01' AS DECIMAL(10,2)) AS c_decimal) AS t")
   414  	result.Check(testkit.Events("-11"))
   415  	result = tk.MustQuery("SELECT floor(t.c_decimal) FROM (SELECT CAST('-10.01' AS DECIMAL(10,1)) AS c_decimal) AS t")
   416  	result.Check(testkit.Events("-10"))
   417  
   418  	// for ceil/ceiling
   419  	result = tk.MustQuery("select ceil(0), ceil(null), ceil(1.23), ceil(-1.23), ceil(1)")
   420  	result.Check(testkit.Events("0 <nil> 2 -1 1"))
   421  	result = tk.MustQuery("select ceiling(0), ceiling(null), ceiling(1.23), ceiling(-1.23), ceiling(1)")
   422  	result.Check(testkit.Events("0 <nil> 2 -1 1"))
   423  	result = tk.MustQuery("select ceil('milevadb'), ceil('1milevadb'), ceil('milevadb1'), ceiling('milevadb'), ceiling('1milevadb'), ceiling('milevadb1')")
   424  	result.Check(testkit.Events("0 1 0 0 1 0"))
   425  	result = tk.MustQuery("select ceil(t.c_datetime), ceiling(t.c_datetime) from (select cast('2020-07-20 00:00:00' as datetime) as c_datetime) as t")
   426  	result.Check(testkit.Events("20170720000000 20170720000000"))
   427  	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")
   428  	result.Check(testkit.Events("123456 123456"))
   429  	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")
   430  	result.Check(testkit.Events("3400 3400"))
   431  	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")
   432  	result.Check(testkit.Events("0 0"))
   433  	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")
   434  	result.Check(testkit.Events("-10 -10"))
   435  	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")
   436  	result.Check(testkit.Events("-10 -10"))
   437  	result = tk.MustQuery("select floor(18446744073709551615), ceil(18446744073709551615)")
   438  	result.Check(testkit.Events("18446744073709551615 18446744073709551615"))
   439  	result = tk.MustQuery("select floor(18446744073709551615.1233), ceil(18446744073709551615.1233)")
   440  	result.Check(testkit.Events("18446744073709551615 18446744073709551616"))
   441  	result = tk.MustQuery("select floor(-18446744073709551617), ceil(-18446744073709551617), floor(-18446744073709551617.11), ceil(-18446744073709551617.11)")
   442  	result.Check(testkit.Events("-18446744073709551617 -18446744073709551617 -18446744073709551618 -18446744073709551617"))
   443  	tk.MustInterDirc("drop causet if exists t;")
   444  	tk.MustInterDirc("create causet t(a decimal(40,20) UNSIGNED);")
   445  	tk.MustInterDirc("insert into t values(2.99999999900000000000), (12), (0);")
   446  	tk.MustQuery("select a, ceil(a) from t where ceil(a) > 1;").Check(testkit.Events("2.99999999900000000000 3", "12.00000000000000000000 12"))
   447  	tk.MustQuery("select a, ceil(a) from t;").Check(testkit.Events("2.99999999900000000000 3", "12.00000000000000000000 12", "0.00000000000000000000 0"))
   448  	tk.MustQuery("select ceil(-29464);").Check(testkit.Events("-29464"))
   449  	tk.MustQuery("select a, floor(a) from t where floor(a) > 1;").Check(testkit.Events("2.99999999900000000000 2", "12.00000000000000000000 12"))
   450  	tk.MustQuery("select a, floor(a) from t;").Check(testkit.Events("2.99999999900000000000 2", "12.00000000000000000000 12", "0.00000000000000000000 0"))
   451  	tk.MustQuery("select floor(-29464);").Check(testkit.Events("-29464"))
   452  
   453  	tk.MustInterDirc(`drop causet if exists t;`)
   454  	tk.MustInterDirc(`create causet t(a decimal(40,20), b bigint);`)
   455  	tk.MustInterDirc(`insert into t values(-2.99999990000000000000, -1);`)
   456  	tk.MustQuery(`select floor(a), floor(a), floor(a) from t;`).Check(testkit.Events(`-3 -3 -3`))
   457  	tk.MustQuery(`select b, floor(b) from t;`).Check(testkit.Events(`-1 -1`))
   458  
   459  	// for cot
   460  	result = tk.MustQuery("select cot(1), cot(-1), cot(NULL)")
   461  	result.Check(testkit.Events("0.6420926159343308 -0.6420926159343308 <nil>"))
   462  	result = tk.MustQuery("select cot('1milevadb')")
   463  	result.Check(testkit.Events("0.6420926159343308"))
   464  	rs, err := tk.InterDirc("select cot(0)")
   465  	c.Assert(err, IsNil)
   466  	_, err = stochastik.GetEvents4Test(ctx, tk.Se, rs)
   467  	c.Assert(err, NotNil)
   468  	terr := errors.Cause(err).(*terror.Error)
   469  	c.Assert(terr.Code(), Equals, errors.ErrCode(allegrosql.ErrDataOutOfRange))
   470  	c.Assert(rs.Close(), IsNil)
   471  
   472  	//for exp
   473  	result = tk.MustQuery("select exp(0), exp(1), exp(-1), exp(1.2), exp(NULL)")
   474  	result.Check(testkit.Events("1 2.718281828459045 0.36787944117144233 3.3201169227365472 <nil>"))
   475  	result = tk.MustQuery("select exp('milevadb'), exp('1milevadb')")
   476  	result.Check(testkit.Events("1 2.718281828459045"))
   477  	rs, err = tk.InterDirc("select exp(1000000)")
   478  	c.Assert(err, IsNil)
   479  	_, err = stochastik.GetEvents4Test(ctx, tk.Se, rs)
   480  	c.Assert(err, NotNil)
   481  	terr = errors.Cause(err).(*terror.Error)
   482  	c.Assert(terr.Code(), Equals, errors.ErrCode(allegrosql.ErrDataOutOfRange))
   483  	c.Assert(rs.Close(), IsNil)
   484  	tk.MustInterDirc("drop causet if exists t")
   485  	tk.MustInterDirc("create causet t(a float)")
   486  	tk.MustInterDirc("insert into t values(1000000)")
   487  	rs, err = tk.InterDirc("select exp(a) from t")
   488  	c.Assert(err, IsNil)
   489  	_, err = stochastik.GetEvents4Test(ctx, tk.Se, rs)
   490  	c.Assert(err, NotNil)
   491  	terr = errors.Cause(err).(*terror.Error)
   492  	c.Assert(terr.Code(), Equals, errors.ErrCode(allegrosql.ErrDataOutOfRange))
   493  	c.Assert(err.Error(), Equals, "[types:1690]DOUBLE value is out of range in 'exp(test.t.a)'")
   494  	c.Assert(rs.Close(), IsNil)
   495  
   496  	// for conv
   497  	result = tk.MustQuery("SELECT CONV('a', 16, 2);")
   498  	result.Check(testkit.Events("1010"))
   499  	result = tk.MustQuery("SELECT CONV('6E', 18, 8);")
   500  	result.Check(testkit.Events("172"))
   501  	result = tk.MustQuery("SELECT CONV(-17, 10, -18);")
   502  	result.Check(testkit.Events("-H"))
   503  	result = tk.MustQuery("SELECT CONV(10+'10'+'10'+X'0a', 10, 10);")
   504  	result.Check(testkit.Events("40"))
   505  	result = tk.MustQuery("SELECT CONV('a', 1, 10);")
   506  	result.Check(testkit.Events("<nil>"))
   507  	result = tk.MustQuery("SELECT CONV('a', 37, 10);")
   508  	result.Check(testkit.Events("<nil>"))
   509  
   510  	// for abs
   511  	result = tk.MustQuery("SELECT ABS(-1);")
   512  	result.Check(testkit.Events("1"))
   513  	result = tk.MustQuery("SELECT ABS('abc');")
   514  	result.Check(testkit.Events("0"))
   515  	result = tk.MustQuery("SELECT ABS(18446744073709551615);")
   516  	result.Check(testkit.Events("18446744073709551615"))
   517  	result = tk.MustQuery("SELECT ABS(123.4);")
   518  	result.Check(testkit.Events("123.4"))
   519  	result = tk.MustQuery("SELECT ABS(-123.4);")
   520  	result.Check(testkit.Events("123.4"))
   521  	result = tk.MustQuery("SELECT ABS(1234E-1);")
   522  	result.Check(testkit.Events("123.4"))
   523  	result = tk.MustQuery("SELECT ABS(-9223372036854775807);")
   524  	result.Check(testkit.Events("9223372036854775807"))
   525  	result = tk.MustQuery("SELECT ABS(NULL);")
   526  	result.Check(testkit.Events("<nil>"))
   527  	rs, err = tk.InterDirc("SELECT ABS(-9223372036854775808);")
   528  	c.Assert(err, IsNil)
   529  	_, err = stochastik.GetEvents4Test(ctx, tk.Se, rs)
   530  	c.Assert(err, NotNil)
   531  	terr = errors.Cause(err).(*terror.Error)
   532  	c.Assert(terr.Code(), Equals, errors.ErrCode(allegrosql.ErrDataOutOfRange))
   533  	c.Assert(rs.Close(), IsNil)
   534  
   535  	// for round
   536  	result = tk.MustQuery("SELECT ROUND(2.5), ROUND(-2.5), ROUND(25E-1);")
   537  	result.Check(testkit.Events("3 -3 3")) // TODO: Should be 3 -3 2
   538  	result = tk.MustQuery("SELECT ROUND(2.5, NULL), ROUND(NULL, 4), ROUND(NULL, NULL), ROUND(NULL);")
   539  	result.Check(testkit.Events("<nil> <nil> <nil> <nil>"))
   540  	result = tk.MustQuery("SELECT ROUND('123.4'), ROUND('123e-2');")
   541  	result.Check(testkit.Events("123 1"))
   542  	result = tk.MustQuery("SELECT ROUND(-9223372036854775808);")
   543  	result.Check(testkit.Events("-9223372036854775808"))
   544  	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);")
   545  	result.Check(testkit.Events("123 123.5 123.46 123.456 123.4560 120 100 0 0"))
   546  	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);")
   547  	result.Check(testkit.Events("123 123.5 123.46 123.456 123.456 120 100 0 0")) // TODO: DeferredCauset 5 should be 123.4560
   548  
   549  	// for truncate
   550  	result = tk.MustQuery("SELECT truncate(123, -2), truncate(123, 2), truncate(123, 1), truncate(123, -1);")
   551  	result.Check(testkit.Events("100 123 123 120"))
   552  	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);")
   553  	result.Check(testkit.Events("100 123.45 123.4 123.456 1.230000000000000000000000000000 123.45"))
   554  	result = tk.MustQuery("SELECT truncate(9223372036854775807, -7), truncate(9223372036854775808, -10), truncate(cast(-1 as unsigned), -10);")
   555  	result.Check(testkit.Events("9223372036850000000 9223372030000000000 18446744070000000000"))
   556  	// issue 17181,19390
   557  	tk.MustQuery("select truncate(42, -9223372036854775808);").Check(testkit.Events("0"))
   558  	tk.MustQuery("select truncate(42, 9223372036854775808);").Check(testkit.Events("42"))
   559  	tk.MustQuery("select truncate(42, -2147483648);").Check(testkit.Events("0"))
   560  	tk.MustQuery("select truncate(42, 2147483648);").Check(testkit.Events("42"))
   561  	tk.MustQuery("select truncate(42, 18446744073709551615);").Check(testkit.Events("42"))
   562  	tk.MustQuery("select truncate(42, 4294967295);").Check(testkit.Events("42"))
   563  	tk.MustQuery("select truncate(42, -0);").Check(testkit.Events("42"))
   564  	tk.MustQuery("select truncate(42, -307);").Check(testkit.Events("0"))
   565  	tk.MustQuery("select truncate(42, -308);").Check(testkit.Events("0"))
   566  	tk.MustQuery("select truncate(42, -309);").Check(testkit.Events("0"))
   567  	tk.MustInterDirc(`drop causet if exists t;`)
   568  	tk.MustInterDirc("create causet t (a bigint unsigned);")
   569  	tk.MustInterDirc("insert into t values (18446744073709551615), (4294967295), (9223372036854775808), (2147483648);")
   570  	tk.MustQuery("select truncate(42, a) from t;").Check(testkit.Events("42", "42", "42", "42"))
   571  
   572  	tk.MustInterDirc(`drop causet if exists t;`)
   573  	tk.MustInterDirc(`create causet t(a date, b datetime, c timestamp, d varchar(20));`)
   574  	tk.MustInterDirc(`insert into t select "1234-12-29", "1234-12-29 16:24:13.9912", "2020-12-29 16:19:28", "12.34567";`)
   575  
   576  	// NOTE: the actually result is: 12341220 12341229.0 12341200 12341229.00,
   577  	// but Causet.ToString() don't format decimal length for float numbers.
   578  	result = tk.MustQuery(`select truncate(a, -1), truncate(a, 1), truncate(a, -2), truncate(a, 2) from t;`)
   579  	result.Check(testkit.Events("12341220 12341229 12341200 12341229"))
   580  
   581  	// NOTE: the actually result is: 12341229162410 12341229162414.0 12341229162400 12341229162414.00,
   582  	// but Causet.ToString() don't format decimal length for float numbers.
   583  	result = tk.MustQuery(`select truncate(b, -1), truncate(b, 1), truncate(b, -2), truncate(b, 2) from t;`)
   584  	result.Check(testkit.Events("12341229162410 12341229162414 12341229162400 12341229162414"))
   585  
   586  	// NOTE: the actually result is: 20141229161920 20141229161928.0 20141229161900 20141229161928.00,
   587  	// but Causet.ToString() don't format decimal length for float numbers.
   588  	result = tk.MustQuery(`select truncate(c, -1), truncate(c, 1), truncate(c, -2), truncate(c, 2) from t;`)
   589  	result.Check(testkit.Events("20141229161920 20141229161928 20141229161900 20141229161928"))
   590  
   591  	result = tk.MustQuery(`select truncate(d, -1), truncate(d, 1), truncate(d, -2), truncate(d, 2) from t;`)
   592  	result.Check(testkit.Events("10 12.3 0 12.34"))
   593  
   594  	result = tk.MustQuery(`select truncate(json_array(), 1), truncate("cascasc", 1);`)
   595  	result.Check(testkit.Events("0 0"))
   596  
   597  	// for pow
   598  	result = tk.MustQuery("SELECT POW('12', 2), POW(1.2e1, '2.0'), POW(12, 2.0);")
   599  	result.Check(testkit.Events("144 144 144"))
   600  	result = tk.MustQuery("SELECT POW(null, 2), POW(2, null), POW(null, null);")
   601  	result.Check(testkit.Events("<nil> <nil> <nil>"))
   602  	result = tk.MustQuery("SELECT POW(0, 0);")
   603  	result.Check(testkit.Events("1"))
   604  	result = tk.MustQuery("SELECT POW(0, 0.1), POW(0, 0.5), POW(0, 1);")
   605  	result.Check(testkit.Events("0 0 0"))
   606  	rs, err = tk.InterDirc("SELECT POW(0, -1);")
   607  	c.Assert(err, IsNil)
   608  	_, err = stochastik.GetEvents4Test(ctx, tk.Se, rs)
   609  	c.Assert(err, NotNil)
   610  	terr = errors.Cause(err).(*terror.Error)
   611  	c.Assert(terr.Code(), Equals, errors.ErrCode(allegrosql.ErrDataOutOfRange))
   612  	c.Assert(rs.Close(), IsNil)
   613  
   614  	// for sign
   615  	result = tk.MustQuery("SELECT SIGN('12'), SIGN(1.2e1), SIGN(12), SIGN(0.0000012);")
   616  	result.Check(testkit.Events("1 1 1 1"))
   617  	result = tk.MustQuery("SELECT SIGN('-12'), SIGN(-1.2e1), SIGN(-12), SIGN(-0.0000012);")
   618  	result.Check(testkit.Events("-1 -1 -1 -1"))
   619  	result = tk.MustQuery("SELECT SIGN('0'), SIGN('-0'), SIGN(0);")
   620  	result.Check(testkit.Events("0 0 0"))
   621  	result = tk.MustQuery("SELECT SIGN(NULL);")
   622  	result.Check(testkit.Events("<nil>"))
   623  	result = tk.MustQuery("SELECT SIGN(-9223372036854775808), SIGN(9223372036854775808);")
   624  	result.Check(testkit.Events("-1 1"))
   625  
   626  	// for sqrt
   627  	result = tk.MustQuery("SELECT SQRT(-10), SQRT(144), SQRT(4.84), SQRT(0.04), SQRT(0);")
   628  	result.Check(testkit.Events("<nil> 12 2.2 0.2 0"))
   629  
   630  	// for crc32
   631  	result = tk.MustQuery("SELECT crc32(0), crc32(-0), crc32('0'), crc32('abc'), crc32('ABC'), crc32(NULL), crc32(''), crc32('hello world!')")
   632  	result.Check(testkit.Events("4108050209 4108050209 4108050209 891568578 2743272264 <nil> 0 62177901"))
   633  
   634  	// for radians
   635  	result = tk.MustQuery("SELECT radians(1.0), radians(pi()), radians(pi()/2), radians(180), radians(1.009);")
   636  	result.Check(testkit.Events("0.017453292519943295 0.05483113556160754 0.02741556778080377 3.141592653589793 0.01761037215262278"))
   637  
   638  	// for rand
   639  	tk.MustInterDirc("drop causet if exists t")
   640  	tk.MustInterDirc("create causet t(a int)")
   641  	tk.MustInterDirc("insert into t values(1),(2),(3)")
   642  	tk.Se.GetStochastikVars().MaxChunkSize = 1
   643  	tk.MustQuery("select rand(1) from t").Sort().Check(testkit.Events("0.1418603212962489", "0.40540353712197724", "0.8716141803857071"))
   644  	tk.MustQuery("select rand(a) from t").Check(testkit.Events("0.40540353712197724", "0.6555866465490187", "0.9057697559760601"))
   645  	tk.MustQuery("select rand(1), rand(2), rand(3)").Check(testkit.Events("0.40540353712197724 0.6555866465490187 0.9057697559760601"))
   646  }
   647  
   648  func (s *testIntegrationSuite2) TestStringBuiltin(c *C) {
   649  	defer s.cleanEnv(c)
   650  	tk := testkit.NewTestKit(c, s.causetstore)
   651  	tk.MustInterDirc("use test")
   652  	ctx := context.Background()
   653  	var err error
   654  
   655  	// for length
   656  	tk.MustInterDirc("drop causet if exists t")
   657  	tk.MustInterDirc("create causet t(a int, b double, c datetime, d time, e char(20), f bit(10))")
   658  	tk.MustInterDirc(`insert into t values(1, 1.1, "2020-01-01 12:01:01", "12:01:01", "abcdef", 0b10101)`)
   659  	result := tk.MustQuery("select length(a), length(b), length(c), length(d), length(e), length(f), length(null) from t")
   660  	result.Check(testkit.Events("1 3 19 8 6 2 <nil>"))
   661  	tk.MustInterDirc("drop causet if exists t")
   662  	tk.MustInterDirc("create causet t(a char(20))")
   663  	tk.MustInterDirc(`insert into t values("milevadb  "), (concat("a  ", "b  "))`)
   664  	result = tk.MustQuery("select a, length(a) from t")
   665  	result.Check(testkit.Events("milevadb 4", "a  b 4"))
   666  
   667  	// for concat
   668  	tk.MustInterDirc("drop causet if exists t")
   669  	tk.MustInterDirc("create causet t(a int, b double, c datetime, d time, e char(20))")
   670  	tk.MustInterDirc(`insert into t values(1, 1.1, "2020-01-01 12:01:01", "12:01:01", "abcdef")`)
   671  	result = tk.MustQuery("select concat(a, b, c, d, e) from t")
   672  	result.Check(testkit.Events("11.12017-01-01 12:01:0112:01:01abcdef"))
   673  	result = tk.MustQuery("select concat(null)")
   674  	result.Check(testkit.Events("<nil>"))
   675  	result = tk.MustQuery("select concat(null, a, b) from t")
   676  	result.Check(testkit.Events("<nil>"))
   677  	tk.MustInterDirc("drop causet if exists t")
   678  	// Fix issue 9123
   679  	tk.MustInterDirc("create causet t(a char(32) not null, b float default '0') engine=innodb default charset=utf8mb4")
   680  	tk.MustInterDirc("insert into t value('0a6f9d012f98467f8e671e9870044528', 208.867)")
   681  	result = tk.MustQuery("select concat_ws( ',', b) from t where a = '0a6f9d012f98467f8e671e9870044528';")
   682  	result.Check(testkit.Events("208.867"))
   683  
   684  	// for concat_ws
   685  	tk.MustInterDirc("drop causet if exists t")
   686  	tk.MustInterDirc("create causet t(a int, b double, c datetime, d time, e char(20))")
   687  	tk.MustInterDirc(`insert into t values(1, 1.1, "2020-01-01 12:01:01", "12:01:01", "abcdef")`)
   688  	result = tk.MustQuery("select concat_ws('|', a, b, c, d, e) from t")
   689  	result.Check(testkit.Events("1|1.1|2020-01-01 12:01:01|12:01:01|abcdef"))
   690  	result = tk.MustQuery("select concat_ws(null, null)")
   691  	result.Check(testkit.Events("<nil>"))
   692  	result = tk.MustQuery("select concat_ws(null, a, b) from t")
   693  	result.Check(testkit.Events("<nil>"))
   694  	result = tk.MustQuery("select concat_ws(',', 'a', 'b')")
   695  	result.Check(testkit.Events("a,b"))
   696  	result = tk.MustQuery("select concat_ws(',','First name',NULL,'Last Name')")
   697  	result.Check(testkit.Events("First name,Last Name"))
   698  
   699  	tk.MustInterDirc(`drop causet if exists t;`)
   700  	tk.MustInterDirc(`create causet t(a tinyint(2), b varchar(10));`)
   701  	tk.MustInterDirc(`insert into t values (1, 'a'), (12, 'a'), (126, 'a'), (127, 'a')`)
   702  	tk.MustQuery(`select concat_ws('#', a, b) from t;`).Check(testkit.Events(
   703  		`1#a`,
   704  		`12#a`,
   705  		`126#a`,
   706  		`127#a`,
   707  	))
   708  
   709  	tk.MustInterDirc("drop causet if exists t")
   710  	tk.MustInterDirc("create causet t(a binary(3))")
   711  	tk.MustInterDirc("insert into t values('a')")
   712  	result = tk.MustQuery(`select concat_ws(',', a, 'test') = 'a\0\0,test' from t`)
   713  	result.Check(testkit.Events("1"))
   714  
   715  	// for ascii
   716  	tk.MustInterDirc("drop causet if exists t")
   717  	tk.MustInterDirc("create causet t(a char(10), b int, c double, d datetime, e time, f bit(4))")
   718  	tk.MustInterDirc(`insert into t values('2', 2, 2.3, "2020-01-01 12:01:01", "12:01:01", 0b1010)`)
   719  	result = tk.MustQuery("select ascii(a), ascii(b), ascii(c), ascii(d), ascii(e), ascii(f) from t")
   720  	result.Check(testkit.Events("50 50 50 50 49 10"))
   721  	result = tk.MustQuery("select ascii('123'), ascii(123), ascii(''), ascii('你好'), ascii(NULL)")
   722  	result.Check(testkit.Events("49 49 0 228 <nil>"))
   723  
   724  	// for lower
   725  	tk.MustInterDirc("drop causet if exists t")
   726  	tk.MustInterDirc("create causet t(a int, b double, c datetime, d time, e char(20), f binary(3), g binary(3))")
   727  	tk.MustInterDirc(`insert into t values(1, 1.1, "2020-01-01 12:01:01", "12:01:01", "abcdef", 'aa', 'BB')`)
   728  	result = tk.MustQuery("select lower(a), lower(b), lower(c), lower(d), lower(e), lower(f), lower(g), lower(null) from t")
   729  	result.Check(testkit.Events("1 1.1 2020-01-01 12:01:01 12:01:01 abcdef aa\x00 BB\x00 <nil>"))
   730  
   731  	// for upper
   732  	result = tk.MustQuery("select upper(a), upper(b), upper(c), upper(d), upper(e), upper(f), upper(g), upper(null) from t")
   733  	result.Check(testkit.Events("1 1.1 2020-01-01 12:01:01 12:01:01 ABCDEF aa\x00 BB\x00 <nil>"))
   734  
   735  	// for strcmp
   736  	tk.MustInterDirc("drop causet if exists t")
   737  	tk.MustInterDirc("create causet t(a char(10), b int, c double, d datetime, e time)")
   738  	tk.MustInterDirc(`insert into t values("123", 123, 12.34, "2020-01-01 12:01:01", "12:01:01")`)
   739  	result = tk.MustQuery(`select strcmp(a, "123"), strcmp(b, "123"), strcmp(c, "12.34"), strcmp(d, "2020-01-01 12:01:01"), strcmp(e, "12:01:01") from t`)
   740  	result.Check(testkit.Events("0 0 0 0 0"))
   741  	result = tk.MustQuery(`select strcmp("1", "123"), strcmp("123", "1"), strcmp("123", "45"), strcmp("123", null), strcmp(null, "123")`)
   742  	result.Check(testkit.Events("-1 1 -1 <nil> <nil>"))
   743  	result = tk.MustQuery(`select strcmp("", "123"), strcmp("123", ""), strcmp("", ""), strcmp("", null), strcmp(null, "")`)
   744  	result.Check(testkit.Events("-1 1 0 <nil> <nil>"))
   745  
   746  	// for left
   747  	tk.MustInterDirc("drop causet if exists t")
   748  	tk.MustInterDirc("create causet t(a char(10), b int, c double, d datetime, e time)")
   749  	tk.MustInterDirc(`insert into t values('abcde', 1234, 12.34, "2020-01-01 12:01:01", "12:01:01")`)
   750  	result = tk.MustQuery("select left(a, 2), left(b, 2), left(c, 2), left(d, 2), left(e, 2) from t")
   751  	result.Check(testkit.Events("ab 12 12 20 12"))
   752  	result = tk.MustQuery(`select left("abc", 0), left("abc", -1), left(NULL, 1), left("abc", NULL)`)
   753  	result.Check(testkit.Events("  <nil> <nil>"))
   754  	result = tk.MustQuery(`select left("abc", "a"), left("abc", 1.9), left("abc", 1.2)`)
   755  	result.Check(testkit.Events(" ab a"))
   756  	result = tk.MustQuery(`select left("中文abc", 2), left("中文abc", 3), left("中文abc", 4)`)
   757  	result.Check(testkit.Events("中文 中文a 中文ab"))
   758  	// for right, reuse the causet created for left
   759  	result = tk.MustQuery("select right(a, 3), right(b, 3), right(c, 3), right(d, 3), right(e, 3) from t")
   760  	result.Check(testkit.Events("cde 234 .34 :01 :01"))
   761  	result = tk.MustQuery(`select right("abcde", 0), right("abcde", -1), right("abcde", 100), right(NULL, 1), right("abcde", NULL)`)
   762  	result.Check(testkit.Events("  abcde <nil> <nil>"))
   763  	result = tk.MustQuery(`select right("abcde", "a"), right("abcde", 1.9), right("abcde", 1.2)`)
   764  	result.Check(testkit.Events(" de e"))
   765  	result = tk.MustQuery(`select right("中文abc", 2), right("中文abc", 4), right("中文abc", 5)`)
   766  	result.Check(testkit.Events("bc 文abc 中文abc"))
   767  	tk.MustInterDirc("drop causet if exists t")
   768  	tk.MustInterDirc("create causet t(a binary(10))")
   769  	tk.MustInterDirc(`insert into t select "中文abc"`)
   770  	result = tk.MustQuery(`select left(a, 3), left(a, 6), left(a, 7) from t`)
   771  	result.Check(testkit.Events("中 中文 中文a"))
   772  	result = tk.MustQuery(`select right(a, 2), right(a, 7) from t`)
   773  	result.Check(testkit.Events("c\x00 文abc\x00"))
   774  
   775  	// for ord
   776  	tk.MustInterDirc("drop causet if exists t")
   777  	tk.MustInterDirc("create causet t(a char(10), b int, c double, d datetime, e time, f bit(4), g binary(20), h blob(10), i text(30))")
   778  	tk.MustInterDirc(`insert into t values('2', 2, 2.3, "2020-01-01 12:01:01", "12:01:01", 0b1010, "512", "48", "milevadb")`)
   779  	result = tk.MustQuery("select ord(a), ord(b), ord(c), ord(d), ord(e), ord(f), ord(g), ord(h), ord(i) from t")
   780  	result.Check(testkit.Events("50 50 50 50 49 10 53 52 116"))
   781  	result = tk.MustQuery("select ord('123'), ord(123), ord(''), ord('你好'), ord(NULL), ord('👍')")
   782  	result.Check(testkit.Events("49 49 0 14990752 <nil> 4036989325"))
   783  	result = tk.MustQuery("select ord(X''), ord(X'6161'), ord(X'e4bd'), ord(X'e4bda0'), ord(_ascii'你'), ord(_latin1'你')")
   784  	result.Check(testkit.Events("0 97 228 228 228 228"))
   785  
   786  	// for space
   787  	result = tk.MustQuery(`select space(0), space(2), space(-1), space(1.1), space(1.9)`)
   788  	result.Check(solitonutil.EventsWithSep(",", ",  ,, ,  "))
   789  	result = tk.MustQuery(`select space("abc"), space("2"), space("1.1"), space(''), space(null)`)
   790  	result.Check(solitonutil.EventsWithSep(",", ",  , ,,<nil>"))
   791  
   792  	// for replace
   793  	tk.MustInterDirc("drop causet if exists t")
   794  	tk.MustInterDirc("create causet t(a char(20), b int, c double, d datetime, e time)")
   795  	tk.MustInterDirc(`insert into t values('www.allegrosql.com', 1234, 12.34, "2020-01-01 12:01:01", "12:01:01")`)
   796  	result = tk.MustQuery(`select replace(a, 'allegrosql', 'whtcorpsinc'), replace(b, 2, 55), replace(c, 34, 0), replace(d, '-', '/'), replace(e, '01', '22') from t`)
   797  	result.Check(solitonutil.EventsWithSep(",", "www.whtcorpsinc.com,15534,12.0,2020/01/01 12:01:01,12:22:22"))
   798  	result = tk.MustQuery(`select replace('aaa', 'a', ''), replace(null, 'a', 'b'), replace('a', null, 'b'), replace('a', 'b', null)`)
   799  	result.Check(testkit.Events(" <nil> <nil> <nil>"))
   800  
   801  	// for tobase64
   802  	tk.MustInterDirc("drop causet if exists t")
   803  	tk.MustInterDirc("create causet t(a int, b double, c datetime, d time, e char(20), f bit(10), g binary(20), h blob(10))")
   804  	tk.MustInterDirc(`insert into t values(1, 1.1, "2020-01-01 12:01:01", "12:01:01", "abcdef", 0b10101, "512", "abc")`)
   805  	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")
   806  	result.Check(testkit.Events("MQ== MS4x MjAxNy0wMS0wMSAxMjowMTowMQ== MTI6MDE6MDE= YWJjZGVm ABU= NTEyAAAAAAAAAAAAAAAAAAAAAAA= YWJj <nil>"))
   807  
   808  	// for from_base64
   809  	result = tk.MustQuery(`select from_base64("abcd"), from_base64("asc")`)
   810  	result.Check(testkit.Events("i\xb7\x1d <nil>"))
   811  	result = tk.MustQuery(`select from_base64("MQ=="), from_base64(1234)`)
   812  	result.Check(testkit.Events("1 \xd7m\xf8"))
   813  
   814  	// for substr
   815  	tk.MustInterDirc("drop causet if exists t")
   816  	tk.MustInterDirc("create causet t(a char(10), b int, c double, d datetime, e time)")
   817  	tk.MustInterDirc(`insert into t values('Sakila', 12345, 123.45, "2020-01-01 12:01:01", "12:01:01")`)
   818  	result = tk.MustQuery(`select substr(a, 3), substr(b, 2, 3), substr(c, -3), substr(d, -8), substr(e, -3, 100) from t`)
   819  	result.Check(testkit.Events("kila 234 .45 12:01:01 :01"))
   820  	result = tk.MustQuery(`select substr('Sakila', 100), substr('Sakila', -100), substr('Sakila', -5, 3), substr('Sakila', 2, -1)`)
   821  	result.Check(solitonutil.EventsWithSep(",", ",,aki,"))
   822  	result = tk.MustQuery(`select substr('foobarbar' from 4), substr('Sakila' from -4 for 2)`)
   823  	result.Check(testkit.Events("barbar ki"))
   824  	result = tk.MustQuery(`select substr(null, 2, 3), substr('foo', null, 3), substr('foo', 2, null)`)
   825  	result.Check(testkit.Events("<nil> <nil> <nil>"))
   826  	result = tk.MustQuery(`select substr('中文abc', 2), substr('中文abc', 3), substr("中文abc", 1, 2)`)
   827  	result.Check(testkit.Events("文abc abc 中文"))
   828  	tk.MustInterDirc("drop causet if exists t")
   829  	tk.MustInterDirc("create causet t(a binary(10))")
   830  	tk.MustInterDirc(`insert into t select "中文abc"`)
   831  	result = tk.MustQuery(`select substr(a, 4), substr(a, 1, 3), substr(a, 1, 6) from t`)
   832  	result.Check(testkit.Events("文abc\x00 中 中文"))
   833  	result = tk.MustQuery(`select substr("string", -1), substr("string", -2), substr("中文", -1), substr("中文", -2) from t`)
   834  	result.Check(testkit.Events("g ng 文 中文"))
   835  
   836  	// for bit_length
   837  	tk.MustInterDirc("drop causet if exists t")
   838  	tk.MustInterDirc("create causet t(a int, b double, c datetime, d time, e char(20), f bit(10), g binary(20), h varbinary(20))")
   839  	tk.MustInterDirc(`insert into t values(1, 1.1, "2020-01-01 12:01:01", "12:01:01", "abcdef", 0b10101, "g", "h")`)
   840  	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")
   841  	result.Check(testkit.Events("8 24 152 64 48 16 160 8 <nil>"))
   842  
   843  	// for substring_index
   844  	tk.MustInterDirc("drop causet if exists t")
   845  	tk.MustInterDirc("create causet t(a char(20), b int, c double, d datetime, e time)")
   846  	tk.MustInterDirc(`insert into t values('www.whtcorpsinc.com', 12345, 123.45, "2020-01-01 12:01:01", "12:01:01")`)
   847  	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`)
   848  	result.Check(testkit.Events("www.whtcorpsinc 12345 45 2020 01:01"))
   849  	result = tk.MustQuery(`select substring_index('www.whtcorpsinc.com', '.', 0), substring_index('www.whtcorpsinc.com', '.', 100), substring_index('www.whtcorpsinc.com', '.', -100)`)
   850  	result.Check(testkit.Events(" www.whtcorpsinc.com www.whtcorpsinc.com"))
   851  	tk.MustQuery(`select substring_index('xyz', 'abc', 9223372036854775808)`).Check(testkit.Events(``))
   852  	result = tk.MustQuery(`select substring_index('www.whtcorpsinc.com', 'd', 1), substring_index('www.whtcorpsinc.com', '', 1), substring_index('', '.', 1)`)
   853  	result.Check(solitonutil.EventsWithSep(",", "www.whtcorpsinc.com,,"))
   854  	result = tk.MustQuery(`select substring_index(null, '.', 1), substring_index('www.whtcorpsinc.com', null, 1), substring_index('www.whtcorpsinc.com', '.', null)`)
   855  	result.Check(testkit.Events("<nil> <nil> <nil>"))
   856  
   857  	// for hex
   858  	tk.MustInterDirc("drop causet if exists t")
   859  	tk.MustInterDirc("create causet t(a char(20), b int, c double, d datetime, e time, f decimal(5, 2), g bit(4))")
   860  	tk.MustInterDirc(`insert into t values('www.whtcorpsinc.com', 12345, 123.45, "2020-01-01 12:01:01", "12:01:01", 123.45, 0b1100)`)
   861  	result = tk.MustQuery(`select hex(a), hex(b), hex(c), hex(d), hex(e), hex(f), hex(g) from t`)
   862  	result.Check(testkit.Events("7777772E70696E676361702E636F6D 3039 7B 323031372D30312D30312031323A30313A3031 31323A30313A3031 7B C"))
   863  	result = tk.MustQuery(`select hex('abc'), hex('你好'), hex(12), hex(12.3), hex(12.8)`)
   864  	result.Check(testkit.Events("616263 E4BDA0E5A5BD C C D"))
   865  	result = tk.MustQuery(`select hex(-1), hex(-12.3), hex(-12.8), hex(0x12), hex(null)`)
   866  	result.Check(testkit.Events("FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFF4 FFFFFFFFFFFFFFF3 12 <nil>"))
   867  	tk.MustInterDirc("drop causet if exists t")
   868  	tk.MustInterDirc("CREATE TABLE t(i int primary key auto_increment, a binary, b binary(0), c binary(20), d binary(255)) character set utf8 defCauslate utf8_bin;")
   869  	tk.MustInterDirc("insert into t(a, b, c, d) values ('a', NULL, 'a','a');")
   870  	tk.MustQuery("select i, hex(a), hex(b), hex(c), hex(d) from t;").Check(testkit.Events("1 61 <nil> 6100000000000000000000000000000000000000 610000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"))
   871  
   872  	// for unhex
   873  	result = tk.MustQuery(`select unhex('4D7953514C'), unhex('313233'), unhex(313233), unhex('')`)
   874  	result.Check(testkit.Events("MyALLEGROSQL 123 123 "))
   875  	result = tk.MustQuery(`select unhex('string'), unhex('你好'), unhex(123.4), unhex(null)`)
   876  	result.Check(testkit.Events("<nil> <nil> <nil> <nil>"))
   877  
   878  	// for ltrim and rtrim
   879  	result = tk.MustQuery(`select ltrim('   bar   '), ltrim('bar'), ltrim(''), ltrim(null)`)
   880  	result.Check(solitonutil.EventsWithSep(",", "bar   ,bar,,<nil>"))
   881  	result = tk.MustQuery(`select rtrim('   bar   '), rtrim('bar'), rtrim(''), rtrim(null)`)
   882  	result.Check(solitonutil.EventsWithSep(",", "   bar,bar,,<nil>"))
   883  	result = tk.MustQuery(`select ltrim("\t   bar   "), ltrim("   \tbar"), ltrim("\n  bar"), ltrim("\r  bar")`)
   884  	result.Check(solitonutil.EventsWithSep(",", "\t   bar   ,\tbar,\n  bar,\r  bar"))
   885  	result = tk.MustQuery(`select rtrim("   bar   \t"), rtrim("bar\t   "), rtrim("bar   \n"), rtrim("bar   \r")`)
   886  	result.Check(solitonutil.EventsWithSep(",", "   bar   \t,bar\t,bar   \n,bar   \r"))
   887  
   888  	// for reverse
   889  	tk.MustInterDirc(`DROP TABLE IF EXISTS t;`)
   890  	tk.MustInterDirc(`CREATE TABLE t(a BINARY(6));`)
   891  	tk.MustInterDirc(`INSERT INTO t VALUES("中文");`)
   892  	result = tk.MustQuery(`SELECT a, REVERSE(a), REVERSE("中文"), REVERSE("123 ") FROM t;`)
   893  	result.Check(testkit.Events("中文 \x87\x96歸\xe4 文中  321"))
   894  	result = tk.MustQuery(`SELECT REVERSE(123), REVERSE(12.09) FROM t;`)
   895  	result.Check(testkit.Events("321 90.21"))
   896  
   897  	// for trim
   898  	result = tk.MustQuery(`select trim('   bar   '), trim(leading 'x' from 'xxxbarxxx'), trim(trailing 'xyz' from 'barxxyz'), trim(both 'x' from 'xxxbarxxx')`)
   899  	result.Check(testkit.Events("bar barxxx barx bar"))
   900  	result = tk.MustQuery(`select trim('\t   bar\n   '), trim('   \rbar   \t')`)
   901  	result.Check(solitonutil.EventsWithSep(",", "\t   bar\n,\rbar   \t"))
   902  	result = tk.MustQuery(`select trim(leading from '   bar'), trim('x' from 'xxxbarxxx'), trim('x' from 'bar'), trim('' from '   bar   ')`)
   903  	result.Check(solitonutil.EventsWithSep(",", "bar,bar,bar,   bar   "))
   904  	result = tk.MustQuery(`select trim(''), trim('x' from '')`)
   905  	result.Check(solitonutil.EventsWithSep(",", ","))
   906  	result = tk.MustQuery(`select trim(null from 'bar'), trim('x' from null), trim(null), trim(leading null from 'bar')`)
   907  	// FIXME: the result for trim(leading null from 'bar') should be <nil>, current is 'bar'
   908  	result.Check(testkit.Events("<nil> <nil> <nil> bar"))
   909  
   910  	// for locate
   911  	tk.MustInterDirc("drop causet if exists t")
   912  	tk.MustInterDirc("create causet t(a char(20), b int, c double, d datetime, e time, f binary(5))")
   913  	tk.MustInterDirc(`insert into t values('www.whtcorpsinc.com', 12345, 123.45, "2020-01-01 12:01:01", "12:01:01", "HelLo")`)
   914  	result = tk.MustQuery(`select locate(".ping", a), locate(".ping", a, 5) from t`)
   915  	result.Check(testkit.Events("4 0"))
   916  	result = tk.MustQuery(`select locate("234", b), locate("235", b, 10) from t`)
   917  	result.Check(testkit.Events("2 0"))
   918  	result = tk.MustQuery(`select locate(".45", c), locate(".35", b) from t`)
   919  	result.Check(testkit.Events("4 0"))
   920  	result = tk.MustQuery(`select locate("El", f), locate("ll", f), locate("lL", f), locate("Lo", f), locate("lo", f) from t`)
   921  	result.Check(testkit.Events("0 0 3 4 0"))
   922  	result = tk.MustQuery(`select locate("01 12", d) from t`)
   923  	result.Check(testkit.Events("9"))
   924  	result = tk.MustQuery(`select locate("文", "中文字符串", 2)`)
   925  	result.Check(testkit.Events("2"))
   926  	result = tk.MustQuery(`select locate("文", "中文字符串", 3)`)
   927  	result.Check(testkit.Events("0"))
   928  	result = tk.MustQuery(`select locate("文", "中文字符串")`)
   929  	result.Check(testkit.Events("2"))
   930  
   931  	// for bin
   932  	result = tk.MustQuery(`select bin(-1);`)
   933  	result.Check(testkit.Events("1111111111111111111111111111111111111111111111111111111111111111"))
   934  	result = tk.MustQuery(`select bin(5);`)
   935  	result.Check(testkit.Events("101"))
   936  	result = tk.MustQuery(`select bin("中文");`)
   937  	result.Check(testkit.Events("0"))
   938  
   939  	// for character_length
   940  	result = tk.MustQuery(`select character_length(null), character_length("Hello"), character_length("a中b文c"),
   941  	character_length(123), character_length(12.3456);`)
   942  	result.Check(testkit.Events("<nil> 5 5 3 7"))
   943  
   944  	// for char_length
   945  	result = tk.MustQuery(`select char_length(null), char_length("Hello"), char_length("a中b文c"), char_length(123),char_length(12.3456);`)
   946  	result.Check(testkit.Events("<nil> 5 5 3 7"))
   947  	result = tk.MustQuery(`select char_length(null), char_length("Hello"), char_length("a 中 b 文 c"), char_length("НОЧЬ НА ОКРАИНЕ МОСКВЫ");`)
   948  	result.Check(testkit.Events("<nil> 5 9 22"))
   949  	// for char_length, binary string type
   950  	result = tk.MustQuery(`select char_length(null), char_length(binary("Hello")), char_length(binary("a 中 b 文 c")), char_length(binary("НОЧЬ НА ОКРАИНЕ МОСКВЫ"));`)
   951  	result.Check(testkit.Events("<nil> 5 13 41"))
   952  
   953  	// for elt
   954  	result = tk.MustQuery(`select elt(0, "abc", "def"), elt(2, "hello", "中文", "milevadb"), elt(4, "hello", "中文",
   955  	"milevadb");`)
   956  	result.Check(testkit.Events("<nil> 中文 <nil>"))
   957  
   958  	// for instr
   959  	result = tk.MustQuery(`select instr("中国", "国"), instr("中国", ""), instr("abc", ""), instr("", ""), instr("", "abc");`)
   960  	result.Check(testkit.Events("2 1 1 1 0"))
   961  	result = tk.MustQuery(`select instr("中国", null), instr(null, ""), instr(null, null);`)
   962  	result.Check(testkit.Events("<nil> <nil> <nil>"))
   963  	tk.MustInterDirc(`drop causet if exists t;`)
   964  	tk.MustInterDirc(`create causet t(a binary(20), b char(20));`)
   965  	tk.MustInterDirc(`insert into t values("中国", cast("国" as binary)), ("中国", ""), ("abc", ""), ("", ""), ("", "abc");`)
   966  	result = tk.MustQuery(`select instr(a, b) from t;`)
   967  	result.Check(testkit.Events("4", "1", "1", "1", "0"))
   968  
   969  	// for oct
   970  	result = tk.MustQuery(`select oct("aaaa"), oct("-1.9"),  oct("-9999999999999999999999999"), oct("9999999999999999999999999");`)
   971  	result.Check(testkit.Events("0 1777777777777777777777 1777777777777777777777 1777777777777777777777"))
   972  	result = tk.MustQuery(`select oct(-1.9), oct(1.9), oct(-1), oct(1), oct(-9999999999999999999999999), oct(9999999999999999999999999);`)
   973  	result.Check(testkit.Events("1777777777777777777777 1 1777777777777777777777 1 1777777777777777777777 1777777777777777777777"))
   974  
   975  	// #issue 4356
   976  	tk.MustInterDirc("drop causet if exists t")
   977  	tk.MustInterDirc("CREATE TABLE t (b BIT(8));")
   978  	tk.MustInterDirc(`INSERT INTO t SET b = b'11111111';`)
   979  	tk.MustInterDirc(`INSERT INTO t SET b = b'1010';`)
   980  	tk.MustInterDirc(`INSERT INTO t SET b = b'0101';`)
   981  	result = tk.MustQuery(`SELECT b+0, BIN(b), OCT(b), HEX(b) FROM t;`)
   982  	result.Check(testkit.Events("255 11111111 377 FF", "10 1010 12 A", "5 101 5 5"))
   983  
   984  	// for find_in_set
   985  	result = tk.MustQuery(`select find_in_set("", ""), find_in_set("", ","), find_in_set("中文", "字符串,中文"), find_in_set("b,", "a,b,c,d");`)
   986  	result.Check(testkit.Events("0 1 2 0"))
   987  	result = tk.MustQuery(`select find_in_set(NULL, ""), find_in_set("", NULL), find_in_set(1, "2,3,1");`)
   988  	result.Check(testkit.Events("<nil> <nil> 3"))
   989  
   990  	// for make_set
   991  	result = tk.MustQuery(`select make_set(0, "12"), make_set(3, "aa", "11"), make_set(3, NULL, "中文"), make_set(NULL, "aa");`)
   992  	result.Check(testkit.Events(" aa,11 中文 <nil>"))
   993  
   994  	// for quote
   995  	result = tk.MustQuery(`select quote("aaaa"), quote(""), quote("\"\""), quote("\n\n");`)
   996  	result.Check(testkit.Events("'aaaa' '' '\"\"' '\n\n'"))
   997  	result = tk.MustQuery(`select quote(0121), quote(0000), quote("中文"), quote(NULL);`)
   998  	result.Check(testkit.Events("'121' '0' '中文' NULL"))
   999  	tk.MustQuery(`select quote(null) is NULL;`).Check(testkit.Events(`0`))
  1000  	tk.MustQuery(`select quote(null) is NOT NULL;`).Check(testkit.Events(`1`))
  1001  	tk.MustQuery(`select length(quote(null));`).Check(testkit.Events(`4`))
  1002  	tk.MustQuery(`select quote(null) REGEXP binary 'null'`).Check(testkit.Events(`0`))
  1003  	tk.MustQuery(`select quote(null) REGEXP binary 'NULL'`).Check(testkit.Events(`1`))
  1004  	tk.MustQuery(`select quote(null) REGEXP 'NULL'`).Check(testkit.Events(`1`))
  1005  	tk.MustQuery(`select quote(null) REGEXP 'null'`).Check(testkit.Events(`0`))
  1006  
  1007  	// for convert
  1008  	result = tk.MustQuery(`select convert("123" using "binary"), convert("中文" using "binary"), convert("中文" using "utf8"), convert("中文" using "utf8mb4"), convert(cast("中文" as binary) using "utf8");`)
  1009  	result.Check(testkit.Events("123 中文 中文 中文 中文"))
  1010  	// Charset 866 does not have a default defCauslation configured currently, so this will return error.
  1011  	err = tk.InterDircToErr(`select convert("123" using "866");`)
  1012  	c.Assert(err.Error(), Equals, "[BerolinaSQL:1115]Unknown character set: '866'")
  1013  	// Test case in issue #4436.
  1014  	tk.MustInterDirc("drop causet if exists t;")
  1015  	tk.MustInterDirc("create causet t(a char(20));")
  1016  	err = tk.InterDircToErr("select convert(a using a) from t;")
  1017  	c.Assert(err.Error(), Equals, "[BerolinaSQL:1115]Unknown character set: 'a'")
  1018  
  1019  	// for insert
  1020  	result = tk.MustQuery(`select insert("中文", 1, 1, cast("aaa" as binary)), insert("ba", -1, 1, "aaa"), insert("ba", 1, 100, "aaa"), insert("ba", 100, 1, "aaa");`)
  1021  	result.Check(testkit.Events("aaa文 ba aaa ba"))
  1022  	result = tk.MustQuery(`select insert("bb", NULL, 1, "aa"), insert("bb", 1, NULL, "aa"), insert(NULL, 1, 1, "aaa"), insert("bb", 1, 1, NULL);`)
  1023  	result.Check(testkit.Events("<nil> <nil> <nil> <nil>"))
  1024  	result = tk.MustQuery(`SELECT INSERT("bb", 0, 1, NULL), INSERT("bb", 0, NULL, "aaa");`)
  1025  	result.Check(testkit.Events("<nil> <nil>"))
  1026  	result = tk.MustQuery(`SELECT INSERT("中文", 0, 1, NULL), INSERT("中文", 0, NULL, "aaa");`)
  1027  	result.Check(testkit.Events("<nil> <nil>"))
  1028  
  1029  	// for export_set
  1030  	result = tk.MustQuery(`select export_set(7, "1", "0", ",", 65);`)
  1031  	result.Check(testkit.Events("1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"))
  1032  	result = tk.MustQuery(`select export_set(7, "1", "0", ",", -1);`)
  1033  	result.Check(testkit.Events("1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"))
  1034  	result = tk.MustQuery(`select export_set(7, "1", "0", ",");`)
  1035  	result.Check(testkit.Events("1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"))
  1036  	result = tk.MustQuery(`select export_set(7, "1", "0");`)
  1037  	result.Check(testkit.Events("1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0"))
  1038  	result = tk.MustQuery(`select export_set(NULL, "1", "0", ",", 65);`)
  1039  	result.Check(testkit.Events("<nil>"))
  1040  	result = tk.MustQuery(`select export_set(7, "1", "0", ",", 1);`)
  1041  	result.Check(testkit.Events("1"))
  1042  
  1043  	// for format
  1044  	result = tk.MustQuery(`select format(12332.1, 4), format(12332.2, 0), format(12332.2, 2,'en_US');`)
  1045  	result.Check(testkit.Events("12,332.1000 12,332 12,332.20"))
  1046  	result = tk.MustQuery(`select format(NULL, 4), format(12332.2, NULL);`)
  1047  	result.Check(testkit.Events("<nil> <nil>"))
  1048  	rs, err := tk.InterDirc(`select format(12332.2, 2,'es_EC');`)
  1049  	c.Assert(err, IsNil)
  1050  	_, err = stochastik.GetEvents4Test(ctx, tk.Se, rs)
  1051  	c.Assert(err, NotNil)
  1052  	c.Assert(err.Error(), Matches, "not support for the specific locale")
  1053  	c.Assert(rs.Close(), IsNil)
  1054  
  1055  	// for field
  1056  	result = tk.MustQuery(`select field(1, 2, 1), field(1, 0, NULL), field(1, NULL, 2, 1), field(NULL, 1, 2, NULL);`)
  1057  	result.Check(testkit.Events("2 0 3 0"))
  1058  	result = tk.MustQuery(`select field("1", 2, 1), field(1, "0", NULL), field("1", NULL, 2, 1), field(NULL, 1, "2", NULL);`)
  1059  	result.Check(testkit.Events("2 0 3 0"))
  1060  	result = tk.MustQuery(`select field("1", 2, 1), field(1, "abc", NULL), field("1", NULL, 2, 1), field(NULL, 1, "2", NULL);`)
  1061  	result.Check(testkit.Events("2 0 3 0"))
  1062  	result = tk.MustQuery(`select field("abc", "a", 1), field(1.3, "1.3", 1.5);`)
  1063  	result.Check(testkit.Events("1 1"))
  1064  
  1065  	tk.MustInterDirc("drop causet if exists t")
  1066  	tk.MustInterDirc("create causet t(a decimal(11, 8), b decimal(11,8))")
  1067  	tk.MustInterDirc("insert into t values('114.57011441','38.04620115'), ('-38.04620119', '38.04620115');")
  1068  	result = tk.MustQuery("select a,b,concat_ws(',',a,b) from t")
  1069  	result.Check(testkit.Events("114.57011441 38.04620115 114.57011441,38.04620115",
  1070  		"-38.04620119 38.04620115 -38.04620119,38.04620115"))
  1071  }
  1072  
  1073  func (s *testIntegrationSuite2) TestEncryptionBuiltin(c *C) {
  1074  	defer s.cleanEnv(c)
  1075  	tk := testkit.NewTestKit(c, s.causetstore)
  1076  	tk.MustInterDirc("use test")
  1077  	ctx := context.Background()
  1078  
  1079  	// for password
  1080  	tk.MustInterDirc("drop causet if exists t")
  1081  	tk.MustInterDirc("create causet t(a char(41), b char(41), c char(41))")
  1082  	tk.MustInterDirc(`insert into t values(NULL, '', 'abc')`)
  1083  	result := tk.MustQuery("select password(a) from t")
  1084  	result.Check(testkit.Events(""))
  1085  	result = tk.MustQuery("select password(b) from t")
  1086  	result.Check(testkit.Events(""))
  1087  	result = tk.MustQuery("select password(c) from t")
  1088  	result.Check(testkit.Events("*0D3CED9BEC10A777AEC23CCC353A8C08A633045E"))
  1089  
  1090  	// for md5
  1091  	tk.MustInterDirc("drop causet if exists t")
  1092  	tk.MustInterDirc("create causet t(a char(10), b int, c double, d datetime, e time, f bit(4), g binary(20), h blob(10), i text(30))")
  1093  	tk.MustInterDirc(`insert into t values('2', 2, 2.3, "2020-01-01 12:01:01", "12:01:01", 0b1010, "512", "48", "milevadb")`)
  1094  	result = tk.MustQuery("select md5(a), md5(b), md5(c), md5(d), md5(e), md5(f), md5(g), md5(h), md5(i) from t")
  1095  	result.Check(testkit.Events("c81e728d9d4c2f636f067f89cc14862c c81e728d9d4c2f636f067f89cc14862c 1a18da63cbbfb49cb9616e6bfd35f662 bad2fa88e1f35919ec7584cc2623a310 991f84d41d7acff6471e536caa8d97db 68b329da9893e34099c7d8ad5cb9c940 5c9f0e9b3b36276731bfba852a73ccc6 642e92efb79421734881b53e1e1b18b6 c337e11bfca9f12ae9b1342901e04379"))
  1096  	result = tk.MustQuery("select md5('123'), md5(123), md5(''), md5('你好'), md5(NULL), md5('👍')")
  1097  	result.Check(testkit.Events(`202cb962ac59075b964b07152d234b70 202cb962ac59075b964b07152d234b70 d41d8cd98f00b204e9800998ecf8427e 7eca689f0d3389d9dea66ae112e5cfd7 <nil> 0215ac4dab1ecaf71d83f98af5726984`))
  1098  
  1099  	// for sha/sha1
  1100  	tk.MustInterDirc("drop causet if exists t")
  1101  	tk.MustInterDirc("create causet t(a char(10), b int, c double, d datetime, e time, f bit(4), g binary(20), h blob(10), i text(30))")
  1102  	tk.MustInterDirc(`insert into t values('2', 2, 2.3, "2020-01-01 12:01:01", "12:01:01", 0b1010, "512", "48", "milevadb")`)
  1103  	result = tk.MustQuery("select sha1(a), sha1(b), sha1(c), sha1(d), sha1(e), sha1(f), sha1(g), sha1(h), sha1(i) from t")
  1104  	result.Check(testkit.Events("da4b9237bacccdf19c0760cab7aec4a8359010b0 da4b9237bacccdf19c0760cab7aec4a8359010b0 ce0d88c5002b6cf7664052f1fc7d652cbdadccec 6c6956de323692298e4e5ad3028ff491f7ad363c 1906f8aeb5a717ca0f84154724045839330b0ea9 adc83b19e793491b1c6ea0fd8b46cd9f32e592fc 9aadd14ceb737b28697b8026f205f4b3e31de147 64e095fe763fc62418378753f9402623bea9e227 4df56fc09a3e66b48fb896e90b0a6fc02c978e9e"))
  1105  	result = tk.MustQuery("select sha1('123'), sha1(123), sha1(''), sha1('你好'), sha1(NULL)")
  1106  	result.Check(testkit.Events(`40bd001563085fc35165329ea1ff5c5ecbdbbeef 40bd001563085fc35165329ea1ff5c5ecbdbbeef da39a3ee5e6b4b0d3255bfef95601890afd80709 440ee0853ad1e99f962b63e459ef992d7c211722 <nil>`))
  1107  	tk.MustInterDirc("drop causet if exists t")
  1108  	tk.MustInterDirc("create causet t(a char(10), b int, c double, d datetime, e time, f bit(4), g binary(20), h blob(10), i text(30))")
  1109  	tk.MustInterDirc(`insert into t values('2', 2, 2.3, "2020-01-01 12:01:01", "12:01:01", 0b1010, "512", "48", "milevadb")`)
  1110  	result = tk.MustQuery("select sha(a), sha(b), sha(c), sha(d), sha(e), sha(f), sha(g), sha(h), sha(i) from t")
  1111  	result.Check(testkit.Events("da4b9237bacccdf19c0760cab7aec4a8359010b0 da4b9237bacccdf19c0760cab7aec4a8359010b0 ce0d88c5002b6cf7664052f1fc7d652cbdadccec 6c6956de323692298e4e5ad3028ff491f7ad363c 1906f8aeb5a717ca0f84154724045839330b0ea9 adc83b19e793491b1c6ea0fd8b46cd9f32e592fc 9aadd14ceb737b28697b8026f205f4b3e31de147 64e095fe763fc62418378753f9402623bea9e227 4df56fc09a3e66b48fb896e90b0a6fc02c978e9e"))
  1112  	result = tk.MustQuery("select sha('123'), sha(123), sha(''), sha('你好'), sha(NULL)")
  1113  	result.Check(testkit.Events(`40bd001563085fc35165329ea1ff5c5ecbdbbeef 40bd001563085fc35165329ea1ff5c5ecbdbbeef da39a3ee5e6b4b0d3255bfef95601890afd80709 440ee0853ad1e99f962b63e459ef992d7c211722 <nil>`))
  1114  
  1115  	// for sha2
  1116  	tk.MustInterDirc("drop causet if exists t")
  1117  	tk.MustInterDirc("create causet t(a char(10), b int, c double, d datetime, e time, f bit(4), g binary(20), h blob(10), i text(30))")
  1118  	tk.MustInterDirc(`insert into t values('2', 2, 2.3, "2020-01-01 12:01:01", "12:01:01", 0b1010, "512", "48", "milevadb")`)
  1119  	result = tk.MustQuery("select sha2(a, 224), sha2(b, 0), sha2(c, 512), sha2(d, 256), sha2(e, 384), sha2(f, 0), sha2(g, 512), sha2(h, 256), sha2(i, 224) from t")
  1120  	result.Check(testkit.Events("58b2aaa0bfae7acc021b3260e941117b529b2e69de878fd7d45c61a9 d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35 42415572557b0ca47e14fa928e83f5746d33f90c74270172cc75c61a78db37fe1485159a4fd75f33ab571b154572a5a300938f7d25969bdd05d8ac9dd6c66123 8c2fa3f276952c92b0b40ed7d27454e44b8399a19769e6bceb40da236e45a20a b11d35f1a37e54d5800d210d8e6b80b42c9f6d20ea7ae548c762383ebaa12c5954c559223c6c7a428e37af96bb4f1e0d 01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b 9550da35ea1683abaf5bfa8de68fe02b9c6d756c64589d1ef8367544c254f5f09218a6466cadcee8d74214f0c0b7fb342d1a9f3bd4d406aacf7be59c327c9306 98010bd9270f9b100b6214a21754fd33bdc8d41b2bc9f9dd16ff54d3c34ffd71 a7cddb7346fbc66ab7f803e865b74cbd99aace8e7dabbd8884c148cb"))
  1121  	result = tk.MustQuery("select sha2('123', 512), sha2(123, 512), sha2('', 512), sha2('你好', 224), sha2(NULL, 256), sha2('foo', 123)")
  1122  	result.Check(testkit.Events(`3c9909afec25354d551dae21590bb26e38d53f2173b8d3dc3eee4c047e7ab1c1eb8b85103e3be7ba613b31bb5c9c36214dc9f14a42fd7a2fdb84856bca5c44c2 3c9909afec25354d551dae21590bb26e38d53f2173b8d3dc3eee4c047e7ab1c1eb8b85103e3be7ba613b31bb5c9c36214dc9f14a42fd7a2fdb84856bca5c44c2 cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e e91f006ed4e0882de2f6a3c96ec228a6a5c715f356d00091bce842b5 <nil> <nil>`))
  1123  
  1124  	// for AES_ENCRYPT
  1125  	tk.MustInterDirc("drop causet if exists t")
  1126  	tk.MustInterDirc("create causet t(a char(10), b int, c double, d datetime, e time, f bit(4), g binary(20), h blob(10), i text(30))")
  1127  	tk.MustInterDirc(`insert into t values('2', 2, 2.3, "2020-01-01 12:01:01", "12:01:01", 0b1010, "512", "48", "milevadb")`)
  1128  	tk.MustInterDirc("SET block_encryption_mode='aes-128-ecb';")
  1129  	result = tk.MustQuery("select HEX(AES_ENCRYPT(a, 'key')), HEX(AES_ENCRYPT(b, 'key')), HEX(AES_ENCRYPT(c, 'key')), HEX(AES_ENCRYPT(d, 'key')), HEX(AES_ENCRYPT(e, 'key')), HEX(AES_ENCRYPT(f, 'key')), HEX(AES_ENCRYPT(g, 'key')), HEX(AES_ENCRYPT(h, 'key')), HEX(AES_ENCRYPT(i, 'key')) from t")
  1130  	result.Check(testkit.Events("B3800B3A3CB4ECE2051A3E80FE373EAC B3800B3A3CB4ECE2051A3E80FE373EAC 9E018F7F2838DBA23C57F0E4CCF93287 E764D3E9D4AF8F926CD0979DDB1D0AF40C208B20A6C39D5D028644885280973A C452FFEEB76D3F5E9B26B8D48F7A228C 181BD5C81CBD36779A3C9DD5FF486B35 CE15F14AC7FF4E56ECCF148DE60E4BEDBDB6900AD51383970A5F32C59B3AC6E3 E1B29995CCF423C75519790F54A08CD2 84525677E95AC97698D22E1125B67E92"))
  1131  	result = tk.MustQuery("select HEX(AES_ENCRYPT('123', 'foobar')), HEX(AES_ENCRYPT(123, 'foobar')), HEX(AES_ENCRYPT('', 'foobar')), HEX(AES_ENCRYPT('你好', 'foobar')), AES_ENCRYPT(NULL, 'foobar')")
  1132  	result.Check(testkit.Events(`45ABDD5C4802EFA6771A94C43F805208 45ABDD5C4802EFA6771A94C43F805208 791F1AEB6A6B796E6352BF381895CA0E D0147E2EB856186F146D9F6DE33F9546 <nil>`))
  1133  	result = tk.MustQuery("select HEX(AES_ENCRYPT(a, 'key', 'iv')), HEX(AES_ENCRYPT(b, 'key', 'iv')) from t")
  1134  	result.Check(testkit.Events("B3800B3A3CB4ECE2051A3E80FE373EAC B3800B3A3CB4ECE2051A3E80FE373EAC"))
  1135  	tk.MustQuery("show warnings").Check(solitonutil.EventsWithSep("|", "Warning|1618|<IV> option ignored", "Warning|1618|<IV> option ignored"))
  1136  	tk.MustInterDirc("SET block_encryption_mode='aes-128-cbc';")
  1137  	result = tk.MustQuery("select HEX(AES_ENCRYPT(a, 'key', '1234567890123456')), HEX(AES_ENCRYPT(b, 'key', '1234567890123456')), HEX(AES_ENCRYPT(c, 'key', '1234567890123456')), HEX(AES_ENCRYPT(d, 'key', '1234567890123456')), HEX(AES_ENCRYPT(e, 'key', '1234567890123456')), HEX(AES_ENCRYPT(f, 'key', '1234567890123456')), HEX(AES_ENCRYPT(g, 'key', '1234567890123456')), HEX(AES_ENCRYPT(h, 'key', '1234567890123456')), HEX(AES_ENCRYPT(i, 'key', '1234567890123456')) from t")
  1138  	result.Check(testkit.Events("341672829F84CB6B0BE690FEC4C4DAE9 341672829F84CB6B0BE690FEC4C4DAE9 D43734E147A12BB96C6897C4BBABA283 16F2C972411948DCEF3659B726D2CCB04AD1379A1A367FA64242058A50211B67 41E71D0C58967C1F50EEC074523946D1 1117D292E2D39C3EAA3B435371BE56FC 8ACB7ECC0883B672D7BD1CFAA9FA5FAF5B731ADE978244CD581F114D591C2E7E D2B13C30937E3251AEDA73859BA32E4B 2CF4A6051FF248A67598A17AA2C17267"))
  1139  	result = tk.MustQuery("select HEX(AES_ENCRYPT('123', 'foobar', '1234567890123456')), HEX(AES_ENCRYPT(123, 'foobar', '1234567890123456')), HEX(AES_ENCRYPT('', 'foobar', '1234567890123456')), HEX(AES_ENCRYPT('你好', 'foobar', '1234567890123456')), AES_ENCRYPT(NULL, 'foobar', '1234567890123456')")
  1140  	result.Check(testkit.Events(`80D5646F07B4654B05A02D9085759770 80D5646F07B4654B05A02D9085759770 B3C14BA15030D2D7E99376DBE011E752 0CD2936EE4FEC7A8CDF6208438B2BC05 <nil>`))
  1141  	tk.MustInterDirc("SET block_encryption_mode='aes-128-ofb';")
  1142  	result = tk.MustQuery("select HEX(AES_ENCRYPT(a, 'key', '1234567890123456')), HEX(AES_ENCRYPT(b, 'key', '1234567890123456')), HEX(AES_ENCRYPT(c, 'key', '1234567890123456')), HEX(AES_ENCRYPT(d, 'key', '1234567890123456')), HEX(AES_ENCRYPT(e, 'key', '1234567890123456')), HEX(AES_ENCRYPT(f, 'key', '1234567890123456')), HEX(AES_ENCRYPT(g, 'key', '1234567890123456')), HEX(AES_ENCRYPT(h, 'key', '1234567890123456')), HEX(AES_ENCRYPT(i, 'key', '1234567890123456')) from t")
  1143  	result.Check(testkit.Events("40 40 40C35C 40DD5EBDFCAA397102386E27DDF97A39ECCEC5 43DF55BAE0A0386D 78 47DC5D8AD19A085C32094E16EFC34A08D6FEF459 46D5 06840BE8"))
  1144  	result = tk.MustQuery("select HEX(AES_ENCRYPT('123', 'foobar', '1234567890123456')), HEX(AES_ENCRYPT(123, 'foobar', '1234567890123456')), HEX(AES_ENCRYPT('', 'foobar', '1234567890123456')), HEX(AES_ENCRYPT('你好', 'foobar', '1234567890123456')), AES_ENCRYPT(NULL, 'foobar', '1234567890123456')")
  1145  	result.Check(testkit.Events(`48E38A 48E38A  9D6C199101C3 <nil>`))
  1146  	tk.MustInterDirc("SET block_encryption_mode='aes-192-ofb';")
  1147  	result = tk.MustQuery("select HEX(AES_ENCRYPT(a, 'key', '1234567890123456')), HEX(AES_ENCRYPT(b, 'key', '1234567890123456')), HEX(AES_ENCRYPT(c, 'key', '1234567890123456')), HEX(AES_ENCRYPT(d, 'key', '1234567890123456')), HEX(AES_ENCRYPT(e, 'key', '1234567890123456')), HEX(AES_ENCRYPT(f, 'key', '1234567890123456')), HEX(AES_ENCRYPT(g, 'key', '1234567890123456')), HEX(AES_ENCRYPT(h, 'key', '1234567890123456')), HEX(AES_ENCRYPT(i, 'key', '1234567890123456')) from t")
  1148  	result.Check(testkit.Events("4B 4B 4B573F 4B493D42572E6477233A429BF3E0AD39DB816D 484B36454B24656B 73 4C483E757A1E555A130B62AAC1DA9D08E1B15C47 4D41 0D106817"))
  1149  	result = tk.MustQuery("select HEX(AES_ENCRYPT('123', 'foobar', '1234567890123456')), HEX(AES_ENCRYPT(123, 'foobar', '1234567890123456')), HEX(AES_ENCRYPT('', 'foobar', '1234567890123456')), HEX(AES_ENCRYPT('你好', 'foobar', '1234567890123456')), AES_ENCRYPT(NULL, 'foobar', '1234567890123456')")
  1150  	result.Check(testkit.Events(`3A76B0 3A76B0  EFF92304268E <nil>`))
  1151  	tk.MustInterDirc("SET block_encryption_mode='aes-256-ofb';")
  1152  	result = tk.MustQuery("select HEX(AES_ENCRYPT(a, 'key', '1234567890123456')), HEX(AES_ENCRYPT(b, 'key', '1234567890123456')), HEX(AES_ENCRYPT(c, 'key', '1234567890123456')), HEX(AES_ENCRYPT(d, 'key', '1234567890123456')), HEX(AES_ENCRYPT(e, 'key', '1234567890123456')), HEX(AES_ENCRYPT(f, 'key', '1234567890123456')), HEX(AES_ENCRYPT(g, 'key', '1234567890123456')), HEX(AES_ENCRYPT(h, 'key', '1234567890123456')), HEX(AES_ENCRYPT(i, 'key', '1234567890123456')) from t")
  1153  	result.Check(testkit.Events("16 16 16D103 16CF01CBC95D33E2ED721CBD930262415A69AD 15CD0ACCD55732FE 2E 11CE02FCE46D02CFDD433C8CA138527060599C35 10C7 5096549E"))
  1154  	result = tk.MustQuery("select HEX(AES_ENCRYPT('123', 'foobar', '1234567890123456')), HEX(AES_ENCRYPT(123, 'foobar', '1234567890123456')), HEX(AES_ENCRYPT('', 'foobar', '1234567890123456')), HEX(AES_ENCRYPT('你好', 'foobar', '1234567890123456')), AES_ENCRYPT(NULL, 'foobar', '1234567890123456')")
  1155  	result.Check(testkit.Events(`E842C5 E842C5  3DCD5646767D <nil>`))
  1156  
  1157  	// for AES_DECRYPT
  1158  	tk.MustInterDirc("SET block_encryption_mode='aes-128-ecb';")
  1159  	result = tk.MustQuery("select AES_DECRYPT(AES_ENCRYPT('foo', 'bar'), 'bar')")
  1160  	result.Check(testkit.Events("foo"))
  1161  	result = tk.MustQuery("select AES_DECRYPT(UNHEX('45ABDD5C4802EFA6771A94C43F805208'), 'foobar'), AES_DECRYPT(UNHEX('791F1AEB6A6B796E6352BF381895CA0E'), 'foobar'), AES_DECRYPT(UNHEX('D0147E2EB856186F146D9F6DE33F9546'), 'foobar'), AES_DECRYPT(NULL, 'foobar'), AES_DECRYPT('SOME_THING_STRANGE', 'foobar')")
  1162  	result.Check(testkit.Events(`123  你好 <nil> <nil>`))
  1163  	tk.MustInterDirc("SET block_encryption_mode='aes-128-cbc';")
  1164  	result = tk.MustQuery("select AES_DECRYPT(AES_ENCRYPT('foo', 'bar', '1234567890123456'), 'bar', '1234567890123456')")
  1165  	result.Check(testkit.Events("foo"))
  1166  	result = tk.MustQuery("select AES_DECRYPT(UNHEX('80D5646F07B4654B05A02D9085759770'), 'foobar', '1234567890123456'), AES_DECRYPT(UNHEX('B3C14BA15030D2D7E99376DBE011E752'), 'foobar', '1234567890123456'), AES_DECRYPT(UNHEX('0CD2936EE4FEC7A8CDF6208438B2BC05'), 'foobar', '1234567890123456'), AES_DECRYPT(NULL, 'foobar', '1234567890123456'), AES_DECRYPT('SOME_THING_STRANGE', 'foobar', '1234567890123456')")
  1167  	result.Check(testkit.Events(`123  你好 <nil> <nil>`))
  1168  	tk.MustInterDirc("SET block_encryption_mode='aes-128-ofb';")
  1169  	result = tk.MustQuery("select AES_DECRYPT(AES_ENCRYPT('foo', 'bar', '1234567890123456'), 'bar', '1234567890123456')")
  1170  	result.Check(testkit.Events("foo"))
  1171  	result = tk.MustQuery("select AES_DECRYPT(UNHEX('48E38A'), 'foobar', '1234567890123456'), AES_DECRYPT(UNHEX(''), 'foobar', '1234567890123456'), AES_DECRYPT(UNHEX('9D6C199101C3'), 'foobar', '1234567890123456'), AES_DECRYPT(NULL, 'foobar', '1234567890123456'), HEX(AES_DECRYPT('SOME_THING_STRANGE', 'foobar', '1234567890123456'))")
  1172  	result.Check(testkit.Events(`123  你好 <nil> 2A9EF431FB2ACB022D7F2E7C71EEC48C7D2B`))
  1173  	tk.MustInterDirc("SET block_encryption_mode='aes-192-ofb';")
  1174  	result = tk.MustQuery("select AES_DECRYPT(AES_ENCRYPT('foo', 'bar', '1234567890123456'), 'bar', '1234567890123456')")
  1175  	result.Check(testkit.Events("foo"))
  1176  	result = tk.MustQuery("select AES_DECRYPT(UNHEX('3A76B0'), 'foobar', '1234567890123456'), AES_DECRYPT(UNHEX(''), 'foobar', '1234567890123456'), AES_DECRYPT(UNHEX('EFF92304268E'), 'foobar', '1234567890123456'), AES_DECRYPT(NULL, 'foobar', '1234567890123456'), HEX(AES_DECRYPT('SOME_THING_STRANGE', 'foobar', '1234567890123456'))")
  1177  	result.Check(testkit.Events(`123  你好 <nil> 580BCEA4DC67CF33FF2C7C570D36ECC89437`))
  1178  	tk.MustInterDirc("SET block_encryption_mode='aes-256-ofb';")
  1179  	result = tk.MustQuery("select AES_DECRYPT(AES_ENCRYPT('foo', 'bar', '1234567890123456'), 'bar', '1234567890123456')")
  1180  	result.Check(testkit.Events("foo"))
  1181  	result = tk.MustQuery("select AES_DECRYPT(UNHEX('E842C5'), 'foobar', '1234567890123456'), AES_DECRYPT(UNHEX(''), 'foobar', '1234567890123456'), AES_DECRYPT(UNHEX('3DCD5646767D'), 'foobar', '1234567890123456'), AES_DECRYPT(NULL, 'foobar', '1234567890123456'), HEX(AES_DECRYPT('SOME_THING_STRANGE', 'foobar', '1234567890123456'))")
  1182  	result.Check(testkit.Events(`123  你好 <nil> 8A3FBBE68C9465834584430E3AEEBB04B1F5`))
  1183  
  1184  	// for COMPRESS
  1185  	tk.MustInterDirc("DROP TABLE IF EXISTS t1;")
  1186  	tk.MustInterDirc("CREATE TABLE t1(a VARCHAR(1000));")
  1187  	tk.MustInterDirc("INSERT INTO t1 VALUES('12345'), ('23456');")
  1188  	result = tk.MustQuery("SELECT HEX(COMPRESS(a)) FROM t1;")
  1189  	result.Check(testkit.Events("05000000789C323432363105040000FFFF02F80100", "05000000789C323236313503040000FFFF03070105"))
  1190  	tk.MustInterDirc("DROP TABLE IF EXISTS t2;")
  1191  	tk.MustInterDirc("CREATE TABLE t2(a VARCHAR(1000), b VARBINARY(1000));")
  1192  	tk.MustInterDirc("INSERT INTO t2 (a, b) SELECT a, COMPRESS(a) from t1;")
  1193  	result = tk.MustQuery("SELECT a, HEX(b) FROM t2;")
  1194  	result.Check(testkit.Events("12345 05000000789C323432363105040000FFFF02F80100", "23456 05000000789C323236313503040000FFFF03070105"))
  1195  
  1196  	// for UNCOMPRESS
  1197  	result = tk.MustQuery("SELECT UNCOMPRESS(COMPRESS('123'))")
  1198  	result.Check(testkit.Events("123"))
  1199  	result = tk.MustQuery("SELECT UNCOMPRESS(UNHEX('03000000789C3334320600012D0097'))")
  1200  	result.Check(testkit.Events("123"))
  1201  	result = tk.MustQuery("SELECT UNCOMPRESS(UNHEX('03000000789C32343206040000FFFF012D0097'))")
  1202  	result.Check(testkit.Events("123"))
  1203  	tk.MustInterDirc("INSERT INTO t2 VALUES ('12345', UNHEX('05000000789C3334323631050002F80100'))")
  1204  	result = tk.MustQuery("SELECT UNCOMPRESS(a), UNCOMPRESS(b) FROM t2;")
  1205  	result.Check(testkit.Events("<nil> 12345", "<nil> 23456", "<nil> 12345"))
  1206  
  1207  	// for UNCOMPRESSED_LENGTH
  1208  	result = tk.MustQuery("SELECT UNCOMPRESSED_LENGTH(COMPRESS('123'))")
  1209  	result.Check(testkit.Events("3"))
  1210  	result = tk.MustQuery("SELECT UNCOMPRESSED_LENGTH(UNHEX('03000000789C3334320600012D0097'))")
  1211  	result.Check(testkit.Events("3"))
  1212  	result = tk.MustQuery("SELECT UNCOMPRESSED_LENGTH(UNHEX('03000000789C32343206040000FFFF012D0097'))")
  1213  	result.Check(testkit.Events("3"))
  1214  	result = tk.MustQuery("SELECT UNCOMPRESSED_LENGTH('')")
  1215  	result.Check(testkit.Events("0"))
  1216  	result = tk.MustQuery("SELECT UNCOMPRESSED_LENGTH(UNHEX('0100'))")
  1217  	result.Check(testkit.Events("0"))
  1218  	result = tk.MustQuery("SELECT UNCOMPRESSED_LENGTH(a), UNCOMPRESSED_LENGTH(b) FROM t2;")
  1219  	result.Check(testkit.Events("875770417 5", "892613426 5", "875770417 5"))
  1220  
  1221  	// for RANDOM_BYTES
  1222  	lengths := []int{0, -5, 1025, 4000}
  1223  	for _, len := range lengths {
  1224  		rs, err := tk.InterDirc(fmt.Sprintf("SELECT RANDOM_BYTES(%d);", len))
  1225  		c.Assert(err, IsNil, Commentf("%v", len))
  1226  		_, err = stochastik.GetEvents4Test(ctx, tk.Se, rs)
  1227  		c.Assert(err, NotNil, Commentf("%v", len))
  1228  		terr := errors.Cause(err).(*terror.Error)
  1229  		c.Assert(terr.Code(), Equals, errors.ErrCode(allegrosql.ErrDataOutOfRange), Commentf("%v", len))
  1230  		c.Assert(rs.Close(), IsNil)
  1231  	}
  1232  	tk.MustQuery("SELECT RANDOM_BYTES('1');")
  1233  	tk.MustQuery("SELECT RANDOM_BYTES(1024);")
  1234  	result = tk.MustQuery("SELECT RANDOM_BYTES(NULL);")
  1235  	result.Check(testkit.Events("<nil>"))
  1236  }
  1237  
  1238  func (s *testIntegrationSuite2) TestTimeBuiltin(c *C) {
  1239  	originALLEGROSQLMode := s.ctx.GetStochastikVars().StrictALLEGROSQLMode
  1240  	s.ctx.GetStochastikVars().StrictALLEGROSQLMode = true
  1241  	defer func() {
  1242  		s.ctx.GetStochastikVars().StrictALLEGROSQLMode = originALLEGROSQLMode
  1243  		s.cleanEnv(c)
  1244  	}()
  1245  	tk := testkit.NewTestKit(c, s.causetstore)
  1246  	tk.MustInterDirc("use test")
  1247  
  1248  	// for makeDate
  1249  	tk.MustInterDirc("drop causet if exists t")
  1250  	tk.MustInterDirc("create causet t(a int, b double, c datetime, d time, e char(20), f bit(10))")
  1251  	tk.MustInterDirc(`insert into t values(1, 1.1, "2020-01-01 12:01:01", "12:01:01", "abcdef", 0b10101)`)
  1252  	result := tk.MustQuery("select makedate(a,a), makedate(b,b), makedate(c,c), makedate(d,d), makedate(e,e), makedate(f,f), makedate(null,null), makedate(a,b) from t")
  1253  	result.Check(testkit.Events("2001-01-01 2001-01-01 <nil> <nil> <nil> 2021-01-21 <nil> 2001-01-01"))
  1254  
  1255  	// for date
  1256  	result = tk.MustQuery(`select date("2020-09-12"), date("2020-09-12 12:12:09"), date("2020-09-12 12:12:09.121212");`)
  1257  	result.Check(testkit.Events("2020-09-12 2020-09-12 2020-09-12"))
  1258  	result = tk.MustQuery(`select date("0000-00-00"), date("0000-00-00 12:12:09"), date("0000-00-00 00:00:00.121212"), date("0000-00-00 00:00:00.000000");`)
  1259  	result.Check(testkit.Events("<nil> 0000-00-00 0000-00-00 <nil>"))
  1260  	result = tk.MustQuery(`select date("aa"), date(12.1), date("");`)
  1261  	result.Check(testkit.Events("<nil> <nil> <nil>"))
  1262  
  1263  	// for year
  1264  	result = tk.MustQuery(`select year("2020-01-09"), year("2020-00-09"), year("000-01-09"), year("1-01-09"), year("20131-01-09"), year(null);`)
  1265  	result.Check(testkit.Events("2020 2020 0 1 <nil> <nil>"))
  1266  	result = tk.MustQuery(`select year("2020-00-00"), year("2020-00-00 00:00:00"), year("0000-00-00 12:12:12"), year("2020-00-00 12:12:12");`)
  1267  	result.Check(testkit.Events("2020 2020 0 2020"))
  1268  	result = tk.MustQuery(`select year("aa"), year(2020), year(2012.09), year("1-01"), year("-09");`)
  1269  	result.Check(testkit.Events("<nil> <nil> <nil> <nil> <nil>"))
  1270  	tk.MustInterDirc(`drop causet if exists t`)
  1271  	tk.MustInterDirc(`create causet t(a bigint)`)
  1272  	_, err := tk.InterDirc(`insert into t select year("aa")`)
  1273  	c.Assert(err, NotNil)
  1274  	c.Assert(terror.ErrorEqual(err, types.ErrWrongValue), IsTrue, Commentf("err %v", err))
  1275  	tk.MustInterDirc(`set sql_mode='STRICT_TRANS_TABLES'`) // without zero date
  1276  	tk.MustInterDirc(`insert into t select year("0000-00-00 00:00:00")`)
  1277  	tk.MustInterDirc(`set sql_mode="NO_ZERO_DATE";`) // with zero date
  1278  	tk.MustInterDirc(`insert into t select year("0000-00-00 00:00:00")`)
  1279  	tk.MustQuery("show warnings").Check(solitonutil.EventsWithSep("|", "Warning|1292|Incorrect datetime value: '0000-00-00 00:00:00.000000'"))
  1280  	tk.MustInterDirc(`set sql_mode="NO_ZERO_DATE,STRICT_TRANS_TABLES";`)
  1281  	_, err = tk.InterDirc(`insert into t select year("0000-00-00 00:00:00");`)
  1282  	c.Assert(err, NotNil)
  1283  	c.Assert(types.ErrWrongValue.Equal(err), IsTrue, Commentf("err %v", err))
  1284  	tk.MustInterDirc(`insert into t select 1`)
  1285  	tk.MustInterDirc(`set sql_mode="STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION";`)
  1286  	_, err = tk.InterDirc(`uFIDelate t set a = year("aa")`)
  1287  	c.Assert(terror.ErrorEqual(err, types.ErrWrongValue), IsTrue, Commentf("err %v", err))
  1288  	_, err = tk.InterDirc(`delete from t where a = year("aa")`)
  1289  	// Only `code` can be used to compare because the error `class` information
  1290  	// will be lost after memex push-down
  1291  	c.Assert(errors.Cause(err).(*terror.Error).Code(), Equals, types.ErrWrongValue.Code(), Commentf("err %v", err))
  1292  
  1293  	// for month
  1294  	result = tk.MustQuery(`select month("2020-01-09"), month("2020-00-09"), month("000-01-09"), month("1-01-09"), month("20131-01-09"), month(null);`)
  1295  	result.Check(testkit.Events("1 0 1 1 <nil> <nil>"))
  1296  	result = tk.MustQuery(`select month("2020-00-00"), month("2020-00-00 00:00:00"), month("0000-00-00 12:12:12"), month("2020-00-00 12:12:12");`)
  1297  	result.Check(testkit.Events("0 0 0 0"))
  1298  	result = tk.MustQuery(`select month("aa"), month(2020), month(2012.09), month("1-01"), month("-09");`)
  1299  	result.Check(testkit.Events("<nil> <nil> <nil> <nil> <nil>"))
  1300  	result = tk.MustQuery(`select month("2020-012-09"), month("2020-0000000012-09"), month("2020-30-09"), month("000-41-09");`)
  1301  	result.Check(testkit.Events("12 12 <nil> <nil>"))
  1302  	tk.MustInterDirc(`drop causet if exists t`)
  1303  	tk.MustInterDirc(`create causet t(a bigint)`)
  1304  	_, err = tk.InterDirc(`insert into t select month("aa")`)
  1305  	c.Assert(err, NotNil)
  1306  	c.Assert(terror.ErrorEqual(err, types.ErrWrongValue), IsTrue, Commentf("err: %v", err))
  1307  	tk.MustInterDirc(`insert into t select month("0000-00-00 00:00:00")`)
  1308  	tk.MustInterDirc(`set sql_mode="NO_ZERO_DATE";`)
  1309  	tk.MustInterDirc(`insert into t select month("0000-00-00 00:00:00")`)
  1310  	tk.MustQuery("show warnings").Check(solitonutil.EventsWithSep("|", "Warning|1292|Incorrect datetime value: '0000-00-00 00:00:00.000000'"))
  1311  	tk.MustInterDirc(`set sql_mode="NO_ZERO_DATE,STRICT_TRANS_TABLES";`)
  1312  	_, err = tk.InterDirc(`insert into t select month("0000-00-00 00:00:00");`)
  1313  	c.Assert(err, NotNil)
  1314  	c.Assert(types.ErrWrongValue.Equal(err), IsTrue, Commentf("err %v", err))
  1315  	tk.MustInterDirc(`insert into t select 1`)
  1316  	tk.MustInterDirc(`set sql_mode="STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION";`)
  1317  	tk.MustInterDirc(`insert into t select 1`)
  1318  	_, err = tk.InterDirc(`uFIDelate t set a = month("aa")`)
  1319  	c.Assert(terror.ErrorEqual(err, types.ErrWrongValue), IsTrue)
  1320  	_, err = tk.InterDirc(`delete from t where a = month("aa")`)
  1321  	c.Assert(errors.Cause(err).(*terror.Error).Code(), Equals, types.ErrWrongValue.Code(), Commentf("err %v", err))
  1322  
  1323  	// for week
  1324  	result = tk.MustQuery(`select week("2012-12-22"), week("2012-12-22", -2), week("2012-12-22", 0), week("2012-12-22", 1), week("2012-12-22", 2), week("2012-12-22", 200);`)
  1325  	result.Check(testkit.Events("51 51 51 51 51 51"))
  1326  	result = tk.MustQuery(`select week("2008-02-20"), week("2008-02-20", 0), week("2008-02-20", 1), week("2009-02-20", 2), week("2008-02-20", 3), week("2008-02-20", 4);`)
  1327  	result.Check(testkit.Events("7 7 8 7 8 8"))
  1328  	result = tk.MustQuery(`select week("2008-02-20", 5), week("2008-02-20", 6), week("2009-02-20", 7), week("2008-02-20", 8), week("2008-02-20", 9);`)
  1329  	result.Check(testkit.Events("7 8 7 7 8"))
  1330  	result = tk.MustQuery(`select week("aa", 1), week(null, 2), week(11, 2), week(12.99, 2);`)
  1331  	result.Check(testkit.Events("<nil> <nil> <nil> <nil>"))
  1332  	result = tk.MustQuery(`select week("aa"), week(null), week(11), week(12.99);`)
  1333  	result.Check(testkit.Events("<nil> <nil> <nil> <nil>"))
  1334  	tk.MustInterDirc(`drop causet if exists t`)
  1335  	tk.MustInterDirc(`create causet t(a datetime)`)
  1336  	_, err = tk.InterDirc(`insert into t select week("aa", 1)`)
  1337  	c.Assert(err, NotNil)
  1338  	c.Assert(terror.ErrorEqual(err, types.ErrWrongValue), IsTrue)
  1339  	tk.MustInterDirc(`insert into t select now()`)
  1340  	_, err = tk.InterDirc(`uFIDelate t set a = week("aa", 1)`)
  1341  	c.Assert(terror.ErrorEqual(err, types.ErrWrongValue), IsTrue)
  1342  	_, err = tk.InterDirc(`delete from t where a = week("aa", 1)`)
  1343  	c.Assert(terror.ErrorEqual(err, types.ErrWrongValue), IsTrue)
  1344  
  1345  	// for weekofyear
  1346  	result = tk.MustQuery(`select weekofyear("2012-12-22"), weekofyear("2008-02-20"), weekofyear("aa"), weekofyear(null), weekofyear(11), weekofyear(12.99);`)
  1347  	result.Check(testkit.Events("51 8 <nil> <nil> <nil> <nil>"))
  1348  	tk.MustInterDirc(`drop causet if exists t`)
  1349  	tk.MustInterDirc(`create causet t(a bigint)`)
  1350  	_, err = tk.InterDirc(`insert into t select weekofyear("aa")`)
  1351  	c.Assert(err, NotNil)
  1352  	c.Assert(terror.ErrorEqual(err, types.ErrWrongValue), IsTrue)
  1353  	tk.MustInterDirc(`insert into t select 1`)
  1354  	_, err = tk.InterDirc(`uFIDelate t set a = weekofyear("aa")`)
  1355  	c.Assert(terror.ErrorEqual(err, types.ErrWrongValue), IsTrue)
  1356  	_, err = tk.InterDirc(`delete from t where a = weekofyear("aa")`)
  1357  	c.Assert(terror.ErrorEqual(err, types.ErrWrongValue), IsTrue)
  1358  
  1359  	// for weekday
  1360  	result = tk.MustQuery(`select weekday("2012-12-20"), weekday("2012-12-21"), weekday("2012-12-22"), weekday("2012-12-23"), weekday("2012-12-24"), weekday("2012-12-25"), weekday("2012-12-26"), weekday("2012-12-27");`)
  1361  	result.Check(testkit.Events("3 4 5 6 0 1 2 3"))
  1362  	result = tk.MustQuery(`select weekday("2012-12-90"), weekday("0000-00-00"), weekday("aa"), weekday(null), weekday(11), weekday(12.99);`)
  1363  	result.Check(testkit.Events("<nil> <nil> <nil> <nil> <nil> <nil>"))
  1364  
  1365  	// for quarter
  1366  	result = tk.MustQuery(`select quarter("2012-00-20"), quarter("2012-01-21"), quarter("2012-03-22"), quarter("2012-05-23"), quarter("2012-08-24"), quarter("2012-09-25"), quarter("2012-11-26"), quarter("2012-12-27");`)
  1367  	result.Check(testkit.Events("0 1 1 2 3 3 4 4"))
  1368  	result = tk.MustQuery(`select quarter("2012-14-20"), quarter("aa"), quarter(null), quarter(11), quarter(12.99);`)
  1369  	result.Check(testkit.Events("<nil> <nil> <nil> <nil> <nil>"))
  1370  	result = tk.MustQuery(`select quarter("0000-00-00"), quarter("0000-00-00 00:00:00");`)
  1371  	result.Check(testkit.Events("<nil> <nil>"))
  1372  	tk.MustQuery("show warnings").Check(solitonutil.EventsWithSep("|",
  1373  		"Warning|1292|Incorrect datetime value: '0000-00-00 00:00:00.000000'",
  1374  		"Warning|1292|Incorrect datetime value: '0000-00-00 00:00:00.000000'"))
  1375  	result = tk.MustQuery(`select quarter(0), quarter(0.0), quarter(0e1), quarter(0.00);`)
  1376  	result.Check(testkit.Events("0 0 0 0"))
  1377  	tk.MustQuery("show warnings").Check(testkit.Events())
  1378  
  1379  	// for from_days
  1380  	result = tk.MustQuery(`select from_days(0), from_days(-199), from_days(1111), from_days(120), from_days(1), from_days(1111111), from_days(9999999), from_days(22222);`)
  1381  	result.Check(testkit.Events("0000-00-00 0000-00-00 0003-01-16 0000-00-00 0000-00-00 3042-02-13 0000-00-00 0060-11-03"))
  1382  	result = tk.MustQuery(`select from_days("2012-14-20"), from_days("111a"), from_days("aa"), from_days(null), from_days("123asf"), from_days(12.99);`)
  1383  	result.Check(testkit.Events("0005-07-05 0000-00-00 0000-00-00 <nil> 0000-00-00 0000-00-00"))
  1384  
  1385  	// Fix issue #3923
  1386  	result = tk.MustQuery("select timediff(cast('2004-12-30 12:00:00' as time), '12:00:00');")
  1387  	result.Check(testkit.Events("00:00:00"))
  1388  	result = tk.MustQuery("select timediff('12:00:00', cast('2004-12-30 12:00:00' as time));")
  1389  	result.Check(testkit.Events("00:00:00"))
  1390  	result = tk.MustQuery("select timediff(cast('2004-12-30 12:00:00' as time), '2004-12-30 12:00:00');")
  1391  	result.Check(testkit.Events("<nil>"))
  1392  	result = tk.MustQuery("select timediff('2004-12-30 12:00:00', cast('2004-12-30 12:00:00' as time));")
  1393  	result.Check(testkit.Events("<nil>"))
  1394  	result = tk.MustQuery("select timediff(cast('2004-12-30 12:00:01' as datetime), '2004-12-30 12:00:00');")
  1395  	result.Check(testkit.Events("00:00:01"))
  1396  	result = tk.MustQuery("select timediff('2004-12-30 12:00:00', cast('2004-12-30 12:00:01' as datetime));")
  1397  	result.Check(testkit.Events("-00:00:01"))
  1398  	result = tk.MustQuery("select timediff(cast('2004-12-30 12:00:01' as time), '-34 00:00:00');")
  1399  	result.Check(testkit.Events("828:00:01"))
  1400  	result = tk.MustQuery("select timediff('-34 00:00:00', cast('2004-12-30 12:00:01' as time));")
  1401  	result.Check(testkit.Events("-828:00:01"))
  1402  	result = tk.MustQuery("select timediff(cast('2004-12-30 12:00:01' as datetime), cast('2004-12-30 11:00:01' as datetime));")
  1403  	result.Check(testkit.Events("01:00:00"))
  1404  	result = tk.MustQuery("select timediff(cast('2004-12-30 12:00:01' as datetime), '2004-12-30 12:00:00.1');")
  1405  	result.Check(testkit.Events("00:00:00.9"))
  1406  	result = tk.MustQuery("select timediff('2004-12-30 12:00:00.1', cast('2004-12-30 12:00:01' as datetime));")
  1407  	result.Check(testkit.Events("-00:00:00.9"))
  1408  	result = tk.MustQuery("select timediff(cast('2004-12-30 12:00:01' as datetime), '-34 124:00:00');")
  1409  	result.Check(testkit.Events("<nil>"))
  1410  	result = tk.MustQuery("select timediff('-34 124:00:00', cast('2004-12-30 12:00:01' as datetime));")
  1411  	result.Check(testkit.Events("<nil>"))
  1412  	result = tk.MustQuery("select timediff(cast('2004-12-30 12:00:01' as time), '-34 124:00:00');")
  1413  	result.Check(testkit.Events("838:59:59"))
  1414  	result = tk.MustQuery("select timediff('-34 124:00:00', cast('2004-12-30 12:00:01' as time));")
  1415  	result.Check(testkit.Events("-838:59:59"))
  1416  	result = tk.MustQuery("select timediff(cast('2004-12-30' as datetime), '12:00:00');")
  1417  	result.Check(testkit.Events("<nil>"))
  1418  	result = tk.MustQuery("select timediff('12:00:00', cast('2004-12-30' as datetime));")
  1419  	result.Check(testkit.Events("<nil>"))
  1420  	result = tk.MustQuery("select timediff('12:00:00', '-34 12:00:00');")
  1421  	result.Check(testkit.Events("838:59:59"))
  1422  	result = tk.MustQuery("select timediff('12:00:00', '34 12:00:00');")
  1423  	result.Check(testkit.Events("-816:00:00"))
  1424  	result = tk.MustQuery("select timediff('2020-1-2 12:00:00', '-34 12:00:00');")
  1425  	result.Check(testkit.Events("<nil>"))
  1426  	result = tk.MustQuery("select timediff('-34 12:00:00', '2020-1-2 12:00:00');")
  1427  	result.Check(testkit.Events("<nil>"))
  1428  	result = tk.MustQuery("select timediff('2020-1-2 12:00:00', '12:00:00');")
  1429  	result.Check(testkit.Events("<nil>"))
  1430  	result = tk.MustQuery("select timediff('12:00:00', '2020-1-2 12:00:00');")
  1431  	result.Check(testkit.Events("<nil>"))
  1432  	result = tk.MustQuery("select timediff('2020-1-2 12:00:00', '2020-1-1 12:00:00');")
  1433  	result.Check(testkit.Events("24:00:00"))
  1434  	tk.MustQuery("select timediff(cast('10:10:10' as time), cast('10:10:11' as time))").Check(testkit.Events("-00:00:01"))
  1435  
  1436  	result = tk.MustQuery("select timestampadd(MINUTE, 1, '2003-01-02'), timestampadd(WEEK, 1, '2003-01-02 23:59:59')" +
  1437  		", timestampadd(MICROSECOND, 1, 950501);")
  1438  	result.Check(testkit.Events("2003-01-02 00:01:00 2003-01-09 23:59:59 1995-05-01 00:00:00.000001"))
  1439  	result = tk.MustQuery("select timestampadd(day, 2, 950501), timestampadd(MINUTE, 37.5,'2003-01-02'), timestampadd(MINUTE, 37.49,'2003-01-02')," +
  1440  		" timestampadd(YeAr, 1, '2003-01-02');")
  1441  	result.Check(testkit.Events("1995-05-03 00:00:00 2003-01-02 00:38:00 2003-01-02 00:37:00 2004-01-02 00:00:00"))
  1442  	result = tk.MustQuery("select to_seconds(950501), to_seconds('2009-11-29'), to_seconds('2009-11-29 13:43:32'), to_seconds('09-11-29 13:43:32');")
  1443  	result.Check(testkit.Events("62966505600 63426672000 63426721412 63426721412"))
  1444  	result = tk.MustQuery("select to_days(950501), to_days('2007-10-07'), to_days('2007-10-07 00:00:59'), to_days('0000-01-01')")
  1445  	result.Check(testkit.Events("728779 733321 733321 1"))
  1446  
  1447  	result = tk.MustQuery("select last_day('2003-02-05'), last_day('2004-02-05'), last_day('2004-01-01 01:01:01'), last_day(950501);")
  1448  	result.Check(testkit.Events("2003-02-28 2004-02-29 2004-01-31 1995-05-31"))
  1449  
  1450  	tk.MustInterDirc("SET ALLEGROSQL_MODE='';")
  1451  	result = tk.MustQuery("select last_day('0000-00-00');")
  1452  	result.Check(testkit.Events("<nil>"))
  1453  	result = tk.MustQuery("select to_days('0000-00-00');")
  1454  	result.Check(testkit.Events("<nil>"))
  1455  	result = tk.MustQuery("select to_seconds('0000-00-00');")
  1456  	result.Check(testkit.Events("<nil>"))
  1457  
  1458  	result = tk.MustQuery("select timestamp('2003-12-31'), timestamp('2003-12-31 12:00:00','12:00:00');")
  1459  	result.Check(testkit.Events("2003-12-31 00:00:00 2004-01-01 00:00:00"))
  1460  	result = tk.MustQuery("select timestamp(20170118123950.123), timestamp(20170118123950.999);")
  1461  	result.Check(testkit.Events("2020-01-18 12:39:50.123 2020-01-18 12:39:50.999"))
  1462  	result = tk.MustQuery("select timestamp('2003-12-31', '01:01:01.01'), timestamp('2003-12-31 12:34', '01:01:01.01')," +
  1463  		" timestamp('2008-12-31','00:00:00.0'), timestamp('2008-12-31 00:00:00.000');")
  1464  
  1465  	tk.MustQuery(`select timestampadd(second, 1, cast("2001-01-01" as date))`).Check(testkit.Events("2001-01-01 00:00:01"))
  1466  	tk.MustQuery(`select timestampadd(hour, 1, cast("2001-01-01" as date))`).Check(testkit.Events("2001-01-01 01:00:00"))
  1467  	tk.MustQuery(`select timestampadd(day, 1, cast("2001-01-01" as date))`).Check(testkit.Events("2001-01-02"))
  1468  	tk.MustQuery(`select timestampadd(month, 1, cast("2001-01-01" as date))`).Check(testkit.Events("2001-02-01"))
  1469  	tk.MustQuery(`select timestampadd(year, 1, cast("2001-01-01" as date))`).Check(testkit.Events("2002-01-01"))
  1470  	tk.MustQuery(`select timestampadd(second, 1, cast("2001-01-01" as datetime))`).Check(testkit.Events("2001-01-01 00:00:01"))
  1471  	tk.MustQuery(`select timestampadd(hour, 1, cast("2001-01-01" as datetime))`).Check(testkit.Events("2001-01-01 01:00:00"))
  1472  	tk.MustQuery(`select timestampadd(day, 1, cast("2001-01-01" as datetime))`).Check(testkit.Events("2001-01-02 00:00:00"))
  1473  	tk.MustQuery(`select timestampadd(month, 1, cast("2001-01-01" as datetime))`).Check(testkit.Events("2001-02-01 00:00:00"))
  1474  	tk.MustQuery(`select timestampadd(year, 1, cast("2001-01-01" as datetime))`).Check(testkit.Events("2002-01-01 00:00:00"))
  1475  
  1476  	result.Check(testkit.Events("2003-12-31 01:01:01.01 2003-12-31 13:35:01.01 2008-12-31 00:00:00.0 2008-12-31 00:00:00.000"))
  1477  	result = tk.MustQuery("select timestamp('2003-12-31', 1), timestamp('2003-12-31', -1);")
  1478  	result.Check(testkit.Events("2003-12-31 00:00:01 2003-12-30 23:59:59"))
  1479  	result = tk.MustQuery("select timestamp('2003-12-31', '2000-12-12 01:01:01.01'), timestamp('2003-14-31','01:01:01.01');")
  1480  	result.Check(testkit.Events("<nil> <nil>"))
  1481  
  1482  	result = tk.MustQuery("select TIMESTAMFIDelIFF(MONTH,'2003-02-01','2003-05-01'), TIMESTAMFIDelIFF(yEaR,'2002-05-01', " +
  1483  		"'2001-01-01'), TIMESTAMFIDelIFF(minute,binary('2003-02-01'),'2003-05-01 12:05:55'), TIMESTAMFIDelIFF(day," +
  1484  		"'1995-05-02', 950501);")
  1485  	result.Check(testkit.Events("3 -1 128885 -1"))
  1486  
  1487  	result = tk.MustQuery("select datediff('2007-12-31 23:59:59','2007-12-30'), datediff('2010-11-30 23:59:59', " +
  1488  		"'2010-12-31'), datediff(950501,'2020-01-13'), datediff(950501.9,'2020-01-13'), datediff(binary(950501), '2020-01-13');")
  1489  	result.Check(testkit.Events("1 -31 -7562 -7562 -7562"))
  1490  	result = tk.MustQuery("select datediff('0000-01-01','0001-01-01'), datediff('0001-00-01', '0001-00-01'), datediff('0001-01-00','0001-01-00'), datediff('2020-01-01','2020-01-01');")
  1491  	result.Check(testkit.Events("-365 <nil> <nil> 0"))
  1492  
  1493  	// for ADDTIME
  1494  	result = tk.MustQuery("select addtime('01:01:11', '00:00:01.013'), addtime('01:01:11.00', '00:00:01'), addtime" +
  1495  		"('2020-01-01 01:01:11.12', '00:00:01'), addtime('2020-01-01 01:01:11.12', '00:00:01.88');")
  1496  	result.Check(testkit.Events("01:01:12.013000 01:01:12 2020-01-01 01:01:12.120000 2020-01-01 01:01:13"))
  1497  	result = tk.MustQuery("select addtime(cast('01:01:11' as time(4)), '00:00:01.013'), addtime(cast('01:01:11.00' " +
  1498  		"as datetime(3)), '00:00:01')," + " addtime(cast('2020-01-01 01:01:11.12' as date), '00:00:01'), addtime(cast" +
  1499  		"(cast('2020-01-01 01:01:11.12' as date) as datetime(2)), '00:00:01.88');")
  1500  	result.Check(testkit.Events("01:01:12.0130 2001-01-11 00:00:01.000 00:00:01 2020-01-01 00:00:01.88"))
  1501  	result = tk.MustQuery("select addtime('2020-01-01 01:01:01', 5), addtime('2020-01-01 01:01:01', -5), addtime('2020-01-01 01:01:01', 0.0), addtime('2020-01-01 01:01:01', 1.34);")
  1502  	result.Check(testkit.Events("2020-01-01 01:01:06 2020-01-01 01:00:56 2020-01-01 01:01:01 2020-01-01 01:01:02.340000"))
  1503  	result = tk.MustQuery("select addtime(cast('01:01:11.00' as datetime(3)), cast('00:00:01' as time)), addtime(cast('01:01:11.00' as datetime(3)), cast('00:00:01' as time(5)))")
  1504  	result.Check(testkit.Events("2001-01-11 00:00:01.000 2001-01-11 00:00:01.00000"))
  1505  	result = tk.MustQuery("select addtime(cast('01:01:11.00' as date), cast('00:00:01' as time));")
  1506  	result.Check(testkit.Events("00:00:01"))
  1507  	tk.MustInterDirc("drop causet if exists t")
  1508  	tk.MustInterDirc("create causet t(a datetime, b timestamp, c time)")
  1509  	tk.MustInterDirc(`insert into t values("2020-01-01 12:30:31", "2020-01-01 12:30:31", "01:01:01")`)
  1510  	result = tk.MustQuery("select addtime(a, b), addtime(cast(a as date), b), addtime(b,a), addtime(a,c), addtime(b," +
  1511  		"c), addtime(c,a), addtime(c,b)" +
  1512  		" from t;")
  1513  	result.Check(testkit.Events("<nil> <nil> <nil> 2020-01-01 13:31:32 2020-01-01 13:31:32 <nil> <nil>"))
  1514  	result = tk.MustQuery("select addtime('01:01:11', cast('1' as time))")
  1515  	result.Check(testkit.Events("01:01:12"))
  1516  	tk.MustQuery("select addtime(cast(null as char(20)), cast('1' as time))").Check(testkit.Events("<nil>"))
  1517  	c.Assert(tk.QueryToErr(`select addtime("01:01:11", cast('sdf' as time))`), IsNil)
  1518  	tk.MustQuery(`select addtime("01:01:11", cast(null as char(20)))`).Check(testkit.Events("<nil>"))
  1519  	tk.MustQuery(`select addtime(cast(1 as time), cast(1 as time))`).Check(testkit.Events("00:00:02"))
  1520  	tk.MustQuery(`select addtime(cast(null as time), cast(1 as time))`).Check(testkit.Events("<nil>"))
  1521  	tk.MustQuery(`select addtime(cast(1 as time), cast(null as time))`).Check(testkit.Events("<nil>"))
  1522  
  1523  	// for SUBTIME
  1524  	result = tk.MustQuery("select subtime('01:01:11', '00:00:01.013'), subtime('01:01:11.00', '00:00:01'), subtime" +
  1525  		"('2020-01-01 01:01:11.12', '00:00:01'), subtime('2020-01-01 01:01:11.12', '00:00:01.88');")
  1526  	result.Check(testkit.Events("01:01:09.987000 01:01:10 2020-01-01 01:01:10.120000 2020-01-01 01:01:09.240000"))
  1527  	result = tk.MustQuery("select subtime(cast('01:01:11' as time(4)), '00:00:01.013'), subtime(cast('01:01:11.00' " +
  1528  		"as datetime(3)), '00:00:01')," + " subtime(cast('2020-01-01 01:01:11.12' as date), '00:00:01'), subtime(cast" +
  1529  		"(cast('2020-01-01 01:01:11.12' as date) as datetime(2)), '00:00:01.88');")
  1530  	result.Check(testkit.Events("01:01:09.9870 2001-01-10 23:59:59.000 -00:00:01 2020-12-31 23:59:58.12"))
  1531  	result = tk.MustQuery("select subtime('2020-01-01 01:01:01', 5), subtime('2020-01-01 01:01:01', -5), subtime('2020-01-01 01:01:01', 0.0), subtime('2020-01-01 01:01:01', 1.34);")
  1532  	result.Check(testkit.Events("2020-01-01 01:00:56 2020-01-01 01:01:06 2020-01-01 01:01:01 2020-01-01 01:00:59.660000"))
  1533  	result = tk.MustQuery("select subtime('01:01:11', '0:0:1.013'), subtime('01:01:11.00', '0:0:1'), subtime('2020-01-01 01:01:11.12', '0:0:1'), subtime('2020-01-01 01:01:11.12', '0:0:1.120000');")
  1534  	result.Check(testkit.Events("01:01:09.987000 01:01:10 2020-01-01 01:01:10.120000 2020-01-01 01:01:10"))
  1535  	result = tk.MustQuery("select subtime(cast('01:01:11.00' as datetime(3)), cast('00:00:01' as time)), subtime(cast('01:01:11.00' as datetime(3)), cast('00:00:01' as time(5)))")
  1536  	result.Check(testkit.Events("2001-01-10 23:59:59.000 2001-01-10 23:59:59.00000"))
  1537  	result = tk.MustQuery("select subtime(cast('01:01:11.00' as date), cast('00:00:01' as time));")
  1538  	result.Check(testkit.Events("-00:00:01"))
  1539  	result = tk.MustQuery("select subtime(a, b), subtime(cast(a as date), b), subtime(b,a), subtime(a,c), subtime(b," +
  1540  		"c), subtime(c,a), subtime(c,b) from t;")
  1541  	result.Check(testkit.Events("<nil> <nil> <nil> 2020-01-01 11:29:30 2020-01-01 11:29:30 <nil> <nil>"))
  1542  	tk.MustQuery("select subtime(cast('10:10:10' as time), cast('9:10:10' as time))").Check(testkit.Events("01:00:00"))
  1543  	tk.MustQuery("select subtime('10:10:10', cast('9:10:10' as time))").Check(testkit.Events("01:00:00"))
  1544  
  1545  	// ADDTIME & SUBTIME issue #5966
  1546  	tk.MustInterDirc("drop causet if exists t")
  1547  	tk.MustInterDirc("create causet t(a datetime, b timestamp, c time, d date, e bit(1))")
  1548  	tk.MustInterDirc(`insert into t values("2020-01-01 12:30:31", "2020-01-01 12:30:31", "01:01:01", "2020-01-01", 0b1)`)
  1549  
  1550  	result = tk.MustQuery("select addtime(a, e), addtime(b, e), addtime(c, e), addtime(d, e) from t")
  1551  	result.Check(testkit.Events("<nil> <nil> <nil> <nil>"))
  1552  	result = tk.MustQuery("select addtime('2020-01-01 01:01:01', 0b1), addtime('2020-01-01', b'1'), addtime('01:01:01', 0b1011)")
  1553  	result.Check(testkit.Events("<nil> <nil> <nil>"))
  1554  	result = tk.MustQuery("select addtime('2020-01-01', 1), addtime('2020-01-01 01:01:01', 1), addtime(cast('2020-01-01' as date), 1)")
  1555  	result.Check(testkit.Events("2020-01-01 00:00:01 2020-01-01 01:01:02 00:00:01"))
  1556  	result = tk.MustQuery("select subtime(a, e), subtime(b, e), subtime(c, e), subtime(d, e) from t")
  1557  	result.Check(testkit.Events("<nil> <nil> <nil> <nil>"))
  1558  	result = tk.MustQuery("select subtime('2020-01-01 01:01:01', 0b1), subtime('2020-01-01', b'1'), subtime('01:01:01', 0b1011)")
  1559  	result.Check(testkit.Events("<nil> <nil> <nil>"))
  1560  	result = tk.MustQuery("select subtime('2020-01-01', 1), subtime('2020-01-01 01:01:01', 1), subtime(cast('2020-01-01' as date), 1)")
  1561  	result.Check(testkit.Events("2020-12-31 23:59:59 2020-01-01 01:01:00 -00:00:01"))
  1562  
  1563  	result = tk.MustQuery("select addtime(-32073, 0), addtime(0, -32073);")
  1564  	result.Check(testkit.Events("<nil> <nil>"))
  1565  	tk.MustQuery("show warnings").Check(solitonutil.EventsWithSep("|",
  1566  		"Warning|1292|Truncated incorrect time value: '-32073'",
  1567  		"Warning|1292|Truncated incorrect time value: '-32073'"))
  1568  	result = tk.MustQuery("select addtime(-32073, c), addtime(c, -32073) from t;")
  1569  	result.Check(testkit.Events("<nil> <nil>"))
  1570  	tk.MustQuery("show warnings").Check(solitonutil.EventsWithSep("|",
  1571  		"Warning|1292|Truncated incorrect time value: '-32073'",
  1572  		"Warning|1292|Truncated incorrect time value: '-32073'"))
  1573  	result = tk.MustQuery("select addtime(a, -32073), addtime(b, -32073), addtime(d, -32073) from t;")
  1574  	result.Check(testkit.Events("<nil> <nil> <nil>"))
  1575  	tk.MustQuery("show warnings").Check(solitonutil.EventsWithSep("|",
  1576  		"Warning|1292|Truncated incorrect time value: '-32073'",
  1577  		"Warning|1292|Truncated incorrect time value: '-32073'",
  1578  		"Warning|1292|Truncated incorrect time value: '-32073'"))
  1579  
  1580  	result = tk.MustQuery("select subtime(-32073, 0), subtime(0, -32073);")
  1581  	result.Check(testkit.Events("<nil> <nil>"))
  1582  	tk.MustQuery("show warnings").Check(solitonutil.EventsWithSep("|",
  1583  		"Warning|1292|Truncated incorrect time value: '-32073'",
  1584  		"Warning|1292|Truncated incorrect time value: '-32073'"))
  1585  	result = tk.MustQuery("select subtime(-32073, c), subtime(c, -32073) from t;")
  1586  	result.Check(testkit.Events("<nil> <nil>"))
  1587  	tk.MustQuery("show warnings").Check(solitonutil.EventsWithSep("|",
  1588  		"Warning|1292|Truncated incorrect time value: '-32073'",
  1589  		"Warning|1292|Truncated incorrect time value: '-32073'"))
  1590  	result = tk.MustQuery("select subtime(a, -32073), subtime(b, -32073), subtime(d, -32073) from t;")
  1591  	result.Check(testkit.Events("<nil> <nil> <nil>"))
  1592  	tk.MustQuery("show warnings").Check(solitonutil.EventsWithSep("|",
  1593  		"Warning|1292|Truncated incorrect time value: '-32073'",
  1594  		"Warning|1292|Truncated incorrect time value: '-32073'",
  1595  		"Warning|1292|Truncated incorrect time value: '-32073'"))
  1596  
  1597  	// fixed issue #3986
  1598  	tk.MustInterDirc("SET ALLEGROSQL_MODE='NO_ENGINE_SUBSTITUTION';")
  1599  	tk.MustInterDirc("SET TIME_ZONE='+03:00';")
  1600  	tk.MustInterDirc("DROP TABLE IF EXISTS t;")
  1601  	tk.MustInterDirc("CREATE TABLE t (ix TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UFIDelATE CURRENT_TIMESTAMP);")
  1602  	tk.MustInterDirc("INSERT INTO t VALUES (0), (20030101010160), (20030101016001), (20030101240101), (20030132010101), (20031301010101), (20031200000000), (20030000000000);")
  1603  	result = tk.MustQuery("SELECT CAST(ix AS SIGNED) FROM t;")
  1604  	result.Check(testkit.Events("0", "0", "0", "0", "0", "0", "0", "0"))
  1605  
  1606  	// test time
  1607  	result = tk.MustQuery("select time('2003-12-31 01:02:03')")
  1608  	result.Check(testkit.Events("01:02:03"))
  1609  	result = tk.MustQuery("select time('2003-12-31 01:02:03.000123')")
  1610  	result.Check(testkit.Events("01:02:03.000123"))
  1611  	result = tk.MustQuery("select time('01:02:03.000123')")
  1612  	result.Check(testkit.Events("01:02:03.000123"))
  1613  	result = tk.MustQuery("select time('01:02:03')")
  1614  	result.Check(testkit.Events("01:02:03"))
  1615  	result = tk.MustQuery("select time('-838:59:59.000000')")
  1616  	result.Check(testkit.Events("-838:59:59.000000"))
  1617  	result = tk.MustQuery("select time('-838:59:59.000001')")
  1618  	result.Check(testkit.Events("-838:59:59.000000"))
  1619  	result = tk.MustQuery("select time('-839:59:59.000000')")
  1620  	result.Check(testkit.Events("-838:59:59.000000"))
  1621  	result = tk.MustQuery("select time('840:59:59.000000')")
  1622  	result.Check(testkit.Events("838:59:59.000000"))
  1623  	// FIXME: #issue 4193
  1624  	// result = tk.MustQuery("select time('840:59:60.000000')")
  1625  	// result.Check(testkit.Events("<nil>"))
  1626  	// result = tk.MustQuery("select time('800:59:59.9999999')")
  1627  	// result.Check(testkit.Events("801:00:00.000000"))
  1628  	// result = tk.MustQuery("select time('12003-12-10 01:02:03.000123')")
  1629  	// result.Check(testkit.Events("<nil>")
  1630  	// result = tk.MustQuery("select time('')")
  1631  	// result.Check(testkit.Events("<nil>")
  1632  	// result = tk.MustQuery("select time('2003-12-10-10 01:02:03.000123')")
  1633  	// result.Check(testkit.Events("00:20:03")
  1634  
  1635  	//for hour
  1636  	result = tk.MustQuery(`SELECT hour("12:13:14.123456"), hour("12:13:14.000010"), hour("272:59:55"), hour(020005), hour(null), hour("27aaaa2:59:55");`)
  1637  	result.Check(testkit.Events("12 12 272 2 <nil> <nil>"))
  1638  
  1639  	// for hour, issue #4340
  1640  	result = tk.MustQuery(`SELECT HOUR(20171222020005);`)
  1641  	result.Check(testkit.Events("2"))
  1642  	result = tk.MustQuery(`SELECT HOUR(20171222020005.1);`)
  1643  	result.Check(testkit.Events("2"))
  1644  	result = tk.MustQuery(`SELECT HOUR(20171222020005.1e0);`)
  1645  	result.Check(testkit.Events("2"))
  1646  	result = tk.MustQuery(`SELECT HOUR("20171222020005");`)
  1647  	result.Check(testkit.Events("2"))
  1648  	result = tk.MustQuery(`SELECT HOUR("20171222020005.1");`)
  1649  	result.Check(testkit.Events("2"))
  1650  	result = tk.MustQuery(`select hour(20171222);`)
  1651  	result.Check(testkit.Events("<nil>"))
  1652  	result = tk.MustQuery(`select hour(8381222);`)
  1653  	result.Check(testkit.Events("838"))
  1654  	result = tk.MustQuery(`select hour(10000000000);`)
  1655  	result.Check(testkit.Events("<nil>"))
  1656  	result = tk.MustQuery(`select hour(10100000000);`)
  1657  	result.Check(testkit.Events("<nil>"))
  1658  	result = tk.MustQuery(`select hour(10001000000);`)
  1659  	result.Check(testkit.Events("<nil>"))
  1660  	result = tk.MustQuery(`select hour(10101000000);`)
  1661  	result.Check(testkit.Events("0"))
  1662  
  1663  	// for minute
  1664  	result = tk.MustQuery(`SELECT minute("12:13:14.123456"), minute("12:13:14.000010"), minute("272:59:55"), minute(null), minute("27aaaa2:59:55");`)
  1665  	result.Check(testkit.Events("13 13 59 <nil> <nil>"))
  1666  
  1667  	// for second
  1668  	result = tk.MustQuery(`SELECT second("12:13:14.123456"), second("12:13:14.000010"), second("272:59:55"), second(null), second("27aaaa2:59:55");`)
  1669  	result.Check(testkit.Events("14 14 55 <nil> <nil>"))
  1670  
  1671  	// for microsecond
  1672  	result = tk.MustQuery(`SELECT microsecond("12:00:00.123456"), microsecond("12:00:00.000010"), microsecond(null), microsecond("27aaaa2:59:55");`)
  1673  	result.Check(testkit.Events("123456 10 <nil> <nil>"))
  1674  
  1675  	// for period_add
  1676  	result = tk.MustQuery(`SELECT period_add(200807, 2), period_add(200807, -2);`)
  1677  	result.Check(testkit.Events("200809 200805"))
  1678  	result = tk.MustQuery(`SELECT period_add(NULL, 2), period_add(-191, NULL), period_add(NULL, NULL), period_add(12.09, -2), period_add("200207aa", "1aa");`)
  1679  	result.Check(testkit.Events("<nil> <nil> <nil> 200010 200208"))
  1680  	for _, errPeriod := range []string{
  1681  		"period_add(0, 20)", "period_add(0, 0)", "period_add(-1, 1)", "period_add(200013, 1)", "period_add(-200012, 1)", "period_add('', '')",
  1682  	} {
  1683  		err := tk.QueryToErr(fmt.Sprintf("SELECT %v;", errPeriod))
  1684  		c.Assert(err.Error(), Equals, "[memex:1210]Incorrect arguments to period_add")
  1685  	}
  1686  
  1687  	// for period_diff
  1688  	result = tk.MustQuery(`SELECT period_diff(200807, 200705), period_diff(200807, 200908);`)
  1689  	result.Check(testkit.Events("14 -13"))
  1690  	result = tk.MustQuery(`SELECT period_diff(NULL, 2), period_diff(-191, NULL), period_diff(NULL, NULL), period_diff(12.09, 2), period_diff("12aa", "11aa");`)
  1691  	result.Check(testkit.Events("<nil> <nil> <nil> 10 1"))
  1692  	for _, errPeriod := range []string{
  1693  		"period_diff(-00013,1)", "period_diff(00013,1)", "period_diff(0, 0)", "period_diff(200013, 1)", "period_diff(5612, 4513)", "period_diff('', '')",
  1694  	} {
  1695  		err := tk.QueryToErr(fmt.Sprintf("SELECT %v;", errPeriod))
  1696  		c.Assert(err.Error(), Equals, "[memex:1210]Incorrect arguments to period_diff")
  1697  	}
  1698  
  1699  	// TODO: fix `CAST(xx as duration)` and release the test below:
  1700  	// result = tk.MustQuery(`SELECT hour("aaa"), hour(123456), hour(1234567);`)
  1701  	// result = tk.MustQuery(`SELECT minute("aaa"), minute(123456), minute(1234567);`)
  1702  	// result = tk.MustQuery(`SELECT second("aaa"), second(123456), second(1234567);`)
  1703  	// result = tk.MustQuery(`SELECT microsecond("aaa"), microsecond(123456), microsecond(1234567);`)
  1704  
  1705  	// for time_format
  1706  	result = tk.MustQuery("SELECT TIME_FORMAT('150:02:28', '%H:%i:%s %p');")
  1707  	result.Check(testkit.Events("150:02:28 AM"))
  1708  	result = tk.MustQuery("SELECT TIME_FORMAT('bad string', '%H:%i:%s %p');")
  1709  	result.Check(testkit.Events("<nil>"))
  1710  	result = tk.MustQuery("SELECT TIME_FORMAT(null, '%H:%i:%s %p');")
  1711  	result.Check(testkit.Events("<nil>"))
  1712  	result = tk.MustQuery("SELECT TIME_FORMAT(123, '%H:%i:%s %p');")
  1713  	result.Check(testkit.Events("00:01:23 AM"))
  1714  	result = tk.MustQuery("SELECT TIME_FORMAT('24:00:00', '%r');")
  1715  	result.Check(testkit.Events("12:00:00 AM"))
  1716  	result = tk.MustQuery("SELECT TIME_FORMAT('25:00:00', '%r');")
  1717  	result.Check(testkit.Events("01:00:00 AM"))
  1718  	result = tk.MustQuery("SELECT TIME_FORMAT('24:00:00', '%l %p');")
  1719  	result.Check(testkit.Events("12 AM"))
  1720  
  1721  	// for date_format
  1722  	result = tk.MustQuery(`SELECT DATE_FORMAT('2020-06-15', '%W %M %e %Y %r %y');`)
  1723  	result.Check(testkit.Events("Thursday June 15 2020 12:00:00 AM 17"))
  1724  	result = tk.MustQuery(`SELECT DATE_FORMAT(151113102019.12, '%W %M %e %Y %r %y');`)
  1725  	result.Check(testkit.Events("Friday November 13 2020 10:20:19 AM 15"))
  1726  	result = tk.MustQuery(`SELECT DATE_FORMAT('0000-00-00', '%W %M %e %Y %r %y');`)
  1727  	result.Check(testkit.Events("<nil>"))
  1728  	tk.MustQuery("show warnings").Check(solitonutil.EventsWithSep("|",
  1729  		"Warning|1292|Incorrect datetime value: '0000-00-00 00:00:00.000000'"))
  1730  	result = tk.MustQuery(`SELECT DATE_FORMAT('0', '%W %M %e %Y %r %y'), DATE_FORMAT('0.0', '%W %M %e %Y %r %y'), DATE_FORMAT(0, 0);`)
  1731  	result.Check(testkit.Events("<nil> <nil> 0"))
  1732  	tk.MustQuery("show warnings").Check(solitonutil.EventsWithSep("|",
  1733  		"Warning|1292|Incorrect time value: '0'",
  1734  		"Warning|1292|Incorrect time value: '0.0'"))
  1735  	result = tk.MustQuery(`SELECT DATE_FORMAT(0, '%W %M %e %Y %r %y'), DATE_FORMAT(0.0, '%W %M %e %Y %r %y');`)
  1736  	result.Check(testkit.Events("<nil> <nil>"))
  1737  	tk.MustQuery("show warnings").Check(testkit.Events())
  1738  
  1739  	// for yearweek
  1740  	result = tk.MustQuery(`select yearweek("2020-12-27"), yearweek("2020-29-27"), yearweek("2020-00-27"), yearweek("2020-12-27 12:38:32"), yearweek("2020-12-27 12:38:32.1111111"), yearweek("2020-12-27 12:90:32"), yearweek("2020-12-27 89:38:32.1111111");`)
  1741  	result.Check(testkit.Events("201451 <nil> <nil> 201451 201451 <nil> <nil>"))
  1742  	result = tk.MustQuery(`select yearweek(12121), yearweek(1.00009), yearweek("aaaaa"), yearweek(""), yearweek(NULL);`)
  1743  	result.Check(testkit.Events("<nil> <nil> <nil> <nil> <nil>"))
  1744  	result = tk.MustQuery(`select yearweek("0000-00-00"), yearweek("2020-01-29", "aa"), yearweek("2011-01-01", null);`)
  1745  	result.Check(testkit.Events("<nil> 201904 201052"))
  1746  
  1747  	// for dayOfWeek, dayOfMonth, dayOfYear
  1748  	result = tk.MustQuery(`select dayOfWeek(null), dayOfWeek("2020-08-12"), dayOfWeek("0000-00-00"), dayOfWeek("2020-00-00"), dayOfWeek("0000-00-00 12:12:12"), dayOfWeek("2020-00-00 12:12:12")`)
  1749  	result.Check(testkit.Events("<nil> 7 <nil> <nil> <nil> <nil>"))
  1750  	result = tk.MustQuery(`select dayOfYear(null), dayOfYear("2020-08-12"), dayOfYear("0000-00-00"), dayOfYear("2020-00-00"), dayOfYear("0000-00-00 12:12:12"), dayOfYear("2020-00-00 12:12:12")`)
  1751  	result.Check(testkit.Events("<nil> 224 <nil> <nil> <nil> <nil>"))
  1752  	result = tk.MustQuery(`select dayOfMonth(null), dayOfMonth("2020-08-12"), dayOfMonth("0000-00-00"), dayOfMonth("2020-00-00"), dayOfMonth("0000-00-00 12:12:12"), dayOfMonth("2020-00-00 12:12:12")`)
  1753  	result.Check(testkit.Events("<nil> 12 0 0 0 0"))
  1754  
  1755  	tk.MustInterDirc("set sql_mode = 'NO_ZERO_DATE'")
  1756  	result = tk.MustQuery(`select dayOfWeek(null), dayOfWeek("2020-08-12"), dayOfWeek("0000-00-00"), dayOfWeek("2020-00-00"), dayOfWeek("0000-00-00 12:12:12"), dayOfWeek("2020-00-00 12:12:12")`)
  1757  	result.Check(testkit.Events("<nil> 7 <nil> <nil> <nil> <nil>"))
  1758  	result = tk.MustQuery(`select dayOfYear(null), dayOfYear("2020-08-12"), dayOfYear("0000-00-00"), dayOfYear("2020-00-00"), dayOfYear("0000-00-00 12:12:12"), dayOfYear("2020-00-00 12:12:12")`)
  1759  	result.Check(testkit.Events("<nil> 224 <nil> <nil> <nil> <nil>"))
  1760  	result = tk.MustQuery(`select dayOfMonth(null), dayOfMonth("2020-08-12"), dayOfMonth("0000-00-00"), dayOfMonth("2020-00-00"), dayOfMonth("0000-00-00 12:12:12"), dayOfMonth("2020-00-00 12:12:12")`)
  1761  	result.Check(testkit.Events("<nil> 12 <nil> 0 0 0"))
  1762  
  1763  	tk.MustInterDirc(`drop causet if exists t`)
  1764  	tk.MustInterDirc(`create causet t(a bigint)`)
  1765  	tk.MustInterDirc(`insert into t value(1)`)
  1766  	tk.MustInterDirc("set sql_mode = 'STRICT_TRANS_TABLES'")
  1767  
  1768  	_, err = tk.InterDirc("insert into t value(dayOfWeek('0000-00-00'))")
  1769  	c.Assert(causet.ErrTruncatedWrongValueForField.Equal(err), IsTrue, Commentf("%v", err))
  1770  	_, err = tk.InterDirc(`uFIDelate t set a = dayOfWeek("0000-00-00")`)
  1771  	c.Assert(types.ErrWrongValue.Equal(err), IsTrue)
  1772  	_, err = tk.InterDirc(`delete from t where a = dayOfWeek(123)`)
  1773  	c.Assert(err, IsNil)
  1774  
  1775  	_, err = tk.InterDirc("insert into t value(dayOfMonth('2020-00-00'))")
  1776  	c.Assert(causet.ErrTruncatedWrongValueForField.Equal(err), IsTrue)
  1777  	tk.MustInterDirc("insert into t value(dayOfMonth('0000-00-00'))")
  1778  	tk.MustInterDirc(`uFIDelate t set a = dayOfMonth("0000-00-00")`)
  1779  	tk.MustInterDirc("set sql_mode = 'NO_ZERO_DATE';")
  1780  	tk.MustInterDirc("insert into t value(dayOfMonth('0000-00-00'))")
  1781  	tk.MustQuery("show warnings").Check(solitonutil.EventsWithSep("|", "Warning|1292|Incorrect datetime value: '0000-00-00 00:00:00.000000'"))
  1782  	tk.MustInterDirc(`uFIDelate t set a = dayOfMonth("0000-00-00")`)
  1783  	tk.MustInterDirc("set sql_mode = 'NO_ZERO_DATE,STRICT_TRANS_TABLES';")
  1784  	_, err = tk.InterDirc("insert into t value(dayOfMonth('0000-00-00'))")
  1785  	c.Assert(causet.ErrTruncatedWrongValueForField.Equal(err), IsTrue)
  1786  	tk.MustInterDirc("insert into t value(0)")
  1787  	_, err = tk.InterDirc(`uFIDelate t set a = dayOfMonth("0000-00-00")`)
  1788  	c.Assert(types.ErrWrongValue.Equal(err), IsTrue)
  1789  	_, err = tk.InterDirc(`delete from t where a = dayOfMonth(123)`)
  1790  	c.Assert(err, IsNil)
  1791  
  1792  	_, err = tk.InterDirc("insert into t value(dayOfYear('0000-00-00'))")
  1793  	c.Assert(causet.ErrTruncatedWrongValueForField.Equal(err), IsTrue)
  1794  	_, err = tk.InterDirc(`uFIDelate t set a = dayOfYear("0000-00-00")`)
  1795  	c.Assert(types.ErrWrongValue.Equal(err), IsTrue)
  1796  	_, err = tk.InterDirc(`delete from t where a = dayOfYear(123)`)
  1797  	c.Assert(err, IsNil)
  1798  
  1799  	tk.MustInterDirc("set sql_mode = ''")
  1800  
  1801  	// for unix_timestamp
  1802  	tk.MustInterDirc("SET time_zone = '+00:00';")
  1803  	result = tk.MustQuery("SELECT UNIX_TIMESTAMP(151113);")
  1804  	result.Check(testkit.Events("1447372800"))
  1805  	result = tk.MustQuery("SELECT UNIX_TIMESTAMP(20151113);")
  1806  	result.Check(testkit.Events("1447372800"))
  1807  	result = tk.MustQuery("SELECT UNIX_TIMESTAMP(151113102019);")
  1808  	result.Check(testkit.Events("1447410019"))
  1809  	result = tk.MustQuery("SELECT UNIX_TIMESTAMP(151113102019e0);")
  1810  	result.Check(testkit.Events("1447410019.000000"))
  1811  	result = tk.MustQuery("SELECT UNIX_TIMESTAMP(15111310201912e-2);")
  1812  	result.Check(testkit.Events("1447410019.120000"))
  1813  	result = tk.MustQuery("SELECT UNIX_TIMESTAMP(151113102019.12);")
  1814  	result.Check(testkit.Events("1447410019.12"))
  1815  	result = tk.MustQuery("SELECT UNIX_TIMESTAMP(151113102019.1234567);")
  1816  	result.Check(testkit.Events("1447410019.123457"))
  1817  	result = tk.MustQuery("SELECT UNIX_TIMESTAMP(20151113102019);")
  1818  	result.Check(testkit.Events("1447410019"))
  1819  	result = tk.MustQuery("SELECT UNIX_TIMESTAMP('2020-11-13 10:20:19');")
  1820  	result.Check(testkit.Events("1447410019"))
  1821  	result = tk.MustQuery("SELECT UNIX_TIMESTAMP('2020-11-13 10:20:19.012');")
  1822  	result.Check(testkit.Events("1447410019.012"))
  1823  	result = tk.MustQuery("SELECT UNIX_TIMESTAMP('1970-01-01 00:00:00');")
  1824  	result.Check(testkit.Events("0"))
  1825  	result = tk.MustQuery("SELECT UNIX_TIMESTAMP('1969-12-31 23:59:59');")
  1826  	result.Check(testkit.Events("0"))
  1827  	result = tk.MustQuery("SELECT UNIX_TIMESTAMP('1970-13-01 00:00:00');")
  1828  	// FIXME: MyALLEGROSQL returns 0 here.
  1829  	result.Check(testkit.Events("<nil>"))
  1830  	result = tk.MustQuery("SELECT UNIX_TIMESTAMP('2038-01-19 03:14:07.999999');")
  1831  	result.Check(testkit.Events("2147483647.999999"))
  1832  	result = tk.MustQuery("SELECT UNIX_TIMESTAMP('2038-01-19 03:14:08');")
  1833  	result.Check(testkit.Events("0"))
  1834  	result = tk.MustQuery("SELECT UNIX_TIMESTAMP(0);")
  1835  	result.Check(testkit.Events("0"))
  1836  	//result = tk.MustQuery("SELECT UNIX_TIMESTAMP(-1);")
  1837  	//result.Check(testkit.Events("0"))
  1838  	//result = tk.MustQuery("SELECT UNIX_TIMESTAMP(12345);")
  1839  	//result.Check(testkit.Events("0"))
  1840  	result = tk.MustQuery("SELECT UNIX_TIMESTAMP('2020-01-01')")
  1841  	result.Check(testkit.Events("1483228800"))
  1842  	// Test different time zone.
  1843  	tk.MustInterDirc("SET time_zone = '+08:00';")
  1844  	result = tk.MustQuery("SELECT UNIX_TIMESTAMP('1970-01-01 00:00:00');")
  1845  	result.Check(testkit.Events("0"))
  1846  	result = tk.MustQuery("SELECT UNIX_TIMESTAMP('1970-01-01 08:00:00');")
  1847  	result.Check(testkit.Events("0"))
  1848  	result = tk.MustQuery("SELECT UNIX_TIMESTAMP('2020-11-13 18:20:19.012'), UNIX_TIMESTAMP('2020-11-13 18:20:19.0123');")
  1849  	result.Check(testkit.Events("1447410019.012 1447410019.0123"))
  1850  	result = tk.MustQuery("SELECT UNIX_TIMESTAMP('2038-01-19 11:14:07.999999');")
  1851  	result.Check(testkit.Events("2147483647.999999"))
  1852  
  1853  	result = tk.MustQuery("SELECT TIME_FORMAT('bad string', '%H:%i:%s %p');")
  1854  	result.Check(testkit.Events("<nil>"))
  1855  	result = tk.MustQuery("SELECT TIME_FORMAT(null, '%H:%i:%s %p');")
  1856  	result.Check(testkit.Events("<nil>"))
  1857  	result = tk.MustQuery("SELECT TIME_FORMAT(123, '%H:%i:%s %p');")
  1858  	result.Check(testkit.Events("00:01:23 AM"))
  1859  
  1860  	// for monthname
  1861  	tk.MustInterDirc(`drop causet if exists t`)
  1862  	tk.MustInterDirc(`create causet t(a varchar(10))`)
  1863  	tk.MustInterDirc(`insert into t value("abc")`)
  1864  	tk.MustInterDirc("set sql_mode = 'STRICT_TRANS_TABLES'")
  1865  
  1866  	tk.MustInterDirc("insert into t value(monthname('0000-00-00'))")
  1867  	tk.MustInterDirc(`uFIDelate t set a = monthname("0000-00-00")`)
  1868  	tk.MustInterDirc("set sql_mode = 'NO_ZERO_DATE'")
  1869  	tk.MustInterDirc("insert into t value(monthname('0000-00-00'))")
  1870  	tk.MustQuery("show warnings").Check(solitonutil.EventsWithSep("|", "Warning|1292|Incorrect datetime value: '0000-00-00 00:00:00.000000'"))
  1871  	tk.MustInterDirc(`uFIDelate t set a = monthname("0000-00-00")`)
  1872  	tk.MustInterDirc("set sql_mode = ''")
  1873  	tk.MustInterDirc("insert into t value(monthname('0000-00-00'))")
  1874  	tk.MustInterDirc("set sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_DATE'")
  1875  	_, err = tk.InterDirc(`uFIDelate t set a = monthname("0000-00-00")`)
  1876  	c.Assert(types.ErrWrongValue.Equal(err), IsTrue)
  1877  	_, err = tk.InterDirc(`delete from t where a = monthname(123)`)
  1878  	c.Assert(err, IsNil)
  1879  	result = tk.MustQuery(`select monthname("2020-12-01"), monthname("0000-00-00"), monthname("0000-01-00"), monthname("0000-01-00 00:00:00")`)
  1880  	result.Check(testkit.Events("December <nil> January January"))
  1881  	tk.MustQuery("show warnings").Check(solitonutil.EventsWithSep("|", "Warning|1292|Incorrect datetime value: '0000-00-00 00:00:00.000000'"))
  1882  
  1883  	// for dayname
  1884  	tk.MustInterDirc(`drop causet if exists t`)
  1885  	tk.MustInterDirc(`create causet t(a varchar(10))`)
  1886  	tk.MustInterDirc(`insert into t value("abc")`)
  1887  	tk.MustInterDirc("set sql_mode = 'STRICT_TRANS_TABLES'")
  1888  
  1889  	_, err = tk.InterDirc("insert into t value(dayname('0000-00-00'))")
  1890  	c.Assert(causet.ErrTruncatedWrongValueForField.Equal(err), IsTrue)
  1891  	_, err = tk.InterDirc(`uFIDelate t set a = dayname("0000-00-00")`)
  1892  	c.Assert(types.ErrWrongValue.Equal(err), IsTrue)
  1893  	_, err = tk.InterDirc(`delete from t where a = dayname(123)`)
  1894  	c.Assert(err, IsNil)
  1895  	result = tk.MustQuery(`select dayname("2020-12-01"), dayname("0000-00-00"), dayname("0000-01-00"), dayname("0000-01-00 00:00:00")`)
  1896  	result.Check(testkit.Events("Friday <nil> <nil> <nil>"))
  1897  	tk.MustQuery("show warnings").Check(solitonutil.EventsWithSep("|",
  1898  		"Warning|1292|Incorrect datetime value: '0000-00-00 00:00:00.000000'",
  1899  		"Warning|1292|Incorrect datetime value: '0000-01-00 00:00:00.000000'",
  1900  		"Warning|1292|Incorrect datetime value: '0000-01-00 00:00:00.000000'"))
  1901  
  1902  	// for sec_to_time
  1903  	result = tk.MustQuery("select sec_to_time(NULL)")
  1904  	result.Check(testkit.Events("<nil>"))
  1905  	result = tk.MustQuery("select sec_to_time(2378), sec_to_time(3864000), sec_to_time(-3864000)")
  1906  	result.Check(testkit.Events("00:39:38 838:59:59 -838:59:59"))
  1907  	result = tk.MustQuery("select sec_to_time(86401.4), sec_to_time(-86401.4), sec_to_time(864014e-1), sec_to_time(-864014e-1), sec_to_time('86401.4'), sec_to_time('-86401.4')")
  1908  	result.Check(testkit.Events("24:00:01.4 -24:00:01.4 24:00:01.400000 -24:00:01.400000 24:00:01.400000 -24:00:01.400000"))
  1909  	result = tk.MustQuery("select sec_to_time(86401.54321), sec_to_time(86401.543212345)")
  1910  	result.Check(testkit.Events("24:00:01.54321 24:00:01.543212"))
  1911  	result = tk.MustQuery("select sec_to_time('123.4'), sec_to_time('123.4567891'), sec_to_time('123')")
  1912  	result.Check(testkit.Events("00:02:03.400000 00:02:03.456789 00:02:03.000000"))
  1913  
  1914  	// for time_to_sec
  1915  	result = tk.MustQuery("select time_to_sec(NULL)")
  1916  	result.Check(testkit.Events("<nil>"))
  1917  	result = tk.MustQuery("select time_to_sec('22:23:00'), time_to_sec('00:39:38'), time_to_sec('23:00'), time_to_sec('00:00'), time_to_sec('00:00:00'), time_to_sec('23:59:59')")
  1918  	result.Check(testkit.Events("80580 2378 82800 0 0 86399"))
  1919  	result = tk.MustQuery("select time_to_sec('1:0'), time_to_sec('1:00'), time_to_sec('1:0:0'), time_to_sec('-02:00'), time_to_sec('-02:00:05'), time_to_sec('020005')")
  1920  	result.Check(testkit.Events("3600 3600 3600 -7200 -7205 7205"))
  1921  	result = tk.MustQuery("select time_to_sec('20171222020005'), time_to_sec(020005), time_to_sec(20171222020005), time_to_sec(171222020005)")
  1922  	result.Check(testkit.Events("7205 7205 7205 7205"))
  1923  
  1924  	// for str_to_date
  1925  	result = tk.MustQuery("select str_to_date('01-01-2020', '%d-%m-%Y'), str_to_date('59:20:12 01-01-2020', '%s:%i:%H %d-%m-%Y'), str_to_date('59:20:12', '%s:%i:%H')")
  1926  	result.Check(testkit.Events("2020-01-01 2020-01-01 12:20:59 12:20:59"))
  1927  	result = tk.MustQuery("select str_to_date('aaa01-01-2020', 'aaa%d-%m-%Y'), str_to_date('59:20:12 aaa01-01-2020', '%s:%i:%H aaa%d-%m-%Y'), str_to_date('59:20:12aaa', '%s:%i:%Haaa')")
  1928  	result.Check(testkit.Events("2020-01-01 2020-01-01 12:20:59 12:20:59"))
  1929  	result = tk.MustQuery("select str_to_date('01-01-2020', '%d'), str_to_date('59', '%d-%Y')")
  1930  	// TODO: MyALLEGROSQL returns "<nil> <nil>".
  1931  	result.Check(testkit.Events("0000-00-01 <nil>"))
  1932  	tk.MustQuery("show warnings").Check(solitonutil.EventsWithSep("|", "Warning|1292|Incorrect datetime value: '0000-00-00 00:00:00'"))
  1933  	result = tk.MustQuery("select str_to_date('2020-6-1', '%Y-%m-%d'), str_to_date('2020-6-1', '%Y-%c-%d'), str_to_date('59:20:1', '%s:%i:%k'), str_to_date('59:20:1', '%s:%i:%l')")
  1934  	result.Check(testkit.Events("2020-06-01 2020-06-01 01:20:59 01:20:59"))
  1935  
  1936  	// for maketime
  1937  	tk.MustInterDirc(`drop causet if exists t`)
  1938  	tk.MustInterDirc(`create causet t(a double, b float, c decimal(10,4));`)
  1939  	tk.MustInterDirc(`insert into t value(1.23, 2.34, 3.1415)`)
  1940  	result = tk.MustQuery("select maketime(1,1,a), maketime(2,2,b), maketime(3,3,c) from t;")
  1941  	result.Check(testkit.Events("01:01:01.230000 02:02:02.340000 03:03:03.1415"))
  1942  	result = tk.MustQuery("select maketime(12, 13, 14), maketime('12', '15', 30.1), maketime(0, 1, 59.1), maketime(0, 1, '59.1'), maketime(0, 1, 59.5)")
  1943  	result.Check(testkit.Events("12:13:14 12:15:30.1 00:01:59.1 00:01:59.100000 00:01:59.5"))
  1944  	result = tk.MustQuery("select maketime(12, 15, 60), maketime(12, 15, '60'), maketime(12, 60, 0), maketime(12, 15, null)")
  1945  	result.Check(testkit.Events("<nil> <nil> <nil> <nil>"))
  1946  	result = tk.MustQuery("select maketime('', '', ''), maketime('h', 'm', 's');")
  1947  	result.Check(testkit.Events("00:00:00.000000 00:00:00.000000"))
  1948  
  1949  	// for get_format
  1950  	result = tk.MustQuery(`select GET_FORMAT(DATE,'USA'), GET_FORMAT(DATE,'JIS'), GET_FORMAT(DATE,'ISO'), GET_FORMAT(DATE,'EUR'),
  1951  	GET_FORMAT(DATE,'INTERNAL'), GET_FORMAT(DATETIME,'USA') , GET_FORMAT(DATETIME,'JIS'), GET_FORMAT(DATETIME,'ISO'),
  1952  	GET_FORMAT(DATETIME,'EUR') , GET_FORMAT(DATETIME,'INTERNAL'), GET_FORMAT(TIME,'USA') , GET_FORMAT(TIME,'JIS'),
  1953  	GET_FORMAT(TIME,'ISO'), GET_FORMAT(TIME,'EUR'), GET_FORMAT(TIME,'INTERNAL')`)
  1954  	result.Check(testkit.Events("%m.%d.%Y %Y-%m-%d %Y-%m-%d %d.%m.%Y %Y%m%d %Y-%m-%d %H.%i.%s %Y-%m-%d %H:%i:%s %Y-%m-%d %H:%i:%s %Y-%m-%d %H.%i.%s %Y%m%d%H%i%s %h:%i:%s %p %H:%i:%s %H:%i:%s %H.%i.%s %H%i%s"))
  1955  
  1956  	// for convert_tz
  1957  	result = tk.MustQuery(`select convert_tz("2004-01-01 12:00:00", "+00:00", "+10:32"), convert_tz("2004-01-01 12:00:00.01", "+00:00", "+10:32"), convert_tz("2004-01-01 12:00:00.01234567", "+00:00", "+10:32");`)
  1958  	result.Check(testkit.Events("2004-01-01 22:32:00 2004-01-01 22:32:00.01 2004-01-01 22:32:00.012346"))
  1959  	result = tk.MustQuery(`select convert_tz(20040101, "+00:00", "+10:32"), convert_tz(20040101.01, "+00:00", "+10:32"), convert_tz(20040101.01234567, "+00:00", "+10:32");`)
  1960  	result.Check(testkit.Events("2004-01-01 10:32:00 2004-01-01 10:32:00.00 2004-01-01 10:32:00.000000"))
  1961  	result = tk.MustQuery(`select convert_tz(NULL, "+00:00", "+10:32"), convert_tz("2004-01-01 12:00:00", NULL, "+10:32"), convert_tz("2004-01-01 12:00:00", "+00:00", NULL);`)
  1962  	result.Check(testkit.Events("<nil> <nil> <nil>"))
  1963  	result = tk.MustQuery(`select convert_tz("a", "+00:00", "+10:32"), convert_tz("2004-01-01 12:00:00", "a", "+10:32"), convert_tz("2004-01-01 12:00:00", "+00:00", "a");`)
  1964  	result.Check(testkit.Events("<nil> <nil> <nil>"))
  1965  	result = tk.MustQuery(`select convert_tz("", "+00:00", "+10:32"), convert_tz("2004-01-01 12:00:00", "", "+10:32"), convert_tz("2004-01-01 12:00:00", "+00:00", "");`)
  1966  	result.Check(testkit.Events("<nil> <nil> <nil>"))
  1967  	result = tk.MustQuery(`select convert_tz("0", "+00:00", "+10:32"), convert_tz("2004-01-01 12:00:00", "0", "+10:32"), convert_tz("2004-01-01 12:00:00", "+00:00", "0");`)
  1968  	result.Check(testkit.Events("<nil> <nil> <nil>"))
  1969  
  1970  	// for from_unixtime
  1971  	tk.MustInterDirc(`set @@stochastik.time_zone = "+08:00"`)
  1972  	result = tk.MustQuery(`select from_unixtime(20170101), from_unixtime(20170101.9999999), from_unixtime(20170101.999), from_unixtime(20170101.999, "%Y %D %M %h:%i:%s %x"), from_unixtime(20170101.999, "%Y %D %M %h:%i:%s %x")`)
  1973  	result.Check(testkit.Events("1970-08-22 18:48:21 1970-08-22 18:48:22.000000 1970-08-22 18:48:21.999 1970 22nd August 06:48:21 1970 1970 22nd August 06:48:21 1970"))
  1974  	tk.MustInterDirc(`set @@stochastik.time_zone = "+00:00"`)
  1975  	result = tk.MustQuery(`select from_unixtime(20170101), from_unixtime(20170101.9999999), from_unixtime(20170101.999), from_unixtime(20170101.999, "%Y %D %M %h:%i:%s %x"), from_unixtime(20170101.999, "%Y %D %M %h:%i:%s %x")`)
  1976  	result.Check(testkit.Events("1970-08-22 10:48:21 1970-08-22 10:48:22.000000 1970-08-22 10:48:21.999 1970 22nd August 10:48:21 1970 1970 22nd August 10:48:21 1970"))
  1977  	tk.MustInterDirc(`set @@stochastik.time_zone = @@global.time_zone`)
  1978  
  1979  	// for extract
  1980  	result = tk.MustQuery(`select extract(day from '800:12:12'), extract(hour from '800:12:12'), extract(month from 20170101), extract(day_second from '2020-01-01 12:12:12')`)
  1981  	result.Check(testkit.Events("12 800 1 1121212"))
  1982  
  1983  	// for adddate, subdate
  1984  	dateArithmeticalTests := []struct {
  1985  		Date      string
  1986  		Interval  string
  1987  		Unit      string
  1988  		AddResult string
  1989  		SubResult string
  1990  	}{
  1991  		{"\"2011-11-11\"", "1", "DAY", "2011-11-12", "2011-11-10"},
  1992  		{"NULL", "1", "DAY", "<nil>", "<nil>"},
  1993  		{"\"2011-11-11\"", "NULL", "DAY", "<nil>", "<nil>"},
  1994  		{"\"2011-11-11 10:10:10\"", "1000", "MICROSECOND", "2011-11-11 10:10:10.001000", "2011-11-11 10:10:09.999000"},
  1995  		{"\"2011-11-11 10:10:10\"", "\"10\"", "SECOND", "2011-11-11 10:10:20", "2011-11-11 10:10:00"},
  1996  		{"\"2011-11-11 10:10:10\"", "\"10\"", "MINUTE", "2011-11-11 10:20:10", "2011-11-11 10:00:10"},
  1997  		{"\"2011-11-11 10:10:10\"", "\"10\"", "HOUR", "2011-11-11 20:10:10", "2011-11-11 00:10:10"},
  1998  		{"\"2011-11-11 10:10:10\"", "\"11\"", "DAY", "2011-11-22 10:10:10", "2011-10-31 10:10:10"},
  1999  		{"\"2011-11-11 10:10:10\"", "\"2\"", "WEEK", "2011-11-25 10:10:10", "2011-10-28 10:10:10"},
  2000  		{"\"2011-11-11 10:10:10\"", "\"2\"", "MONTH", "2012-01-11 10:10:10", "2011-09-11 10:10:10"},
  2001  		{"\"2011-11-11 10:10:10\"", "\"4\"", "QUARTER", "2012-11-11 10:10:10", "2010-11-11 10:10:10"},
  2002  		{"\"2011-11-11 10:10:10\"", "\"2\"", "YEAR", "2020-11-11 10:10:10", "2009-11-11 10:10:10"},
  2003  		{"\"2011-11-11 10:10:10\"", "\"10.00100000\"", "SECOND_MICROSECOND", "2011-11-11 10:10:20.100000", "2011-11-11 10:09:59.900000"},
  2004  		{"\"2011-11-11 10:10:10\"", "\"10.0010000000\"", "SECOND_MICROSECOND", "2011-11-11 10:10:30", "2011-11-11 10:09:50"},
  2005  		{"\"2011-11-11 10:10:10\"", "\"10.0010000010\"", "SECOND_MICROSECOND", "2011-11-11 10:10:30.000010", "2011-11-11 10:09:49.999990"},
  2006  		{"\"2011-11-11 10:10:10\"", "\"10:10.100\"", "MINUTE_MICROSECOND", "2011-11-11 10:20:20.100000", "2011-11-11 09:59:59.900000"},
  2007  		{"\"2011-11-11 10:10:10\"", "\"10:10\"", "MINUTE_SECOND", "2011-11-11 10:20:20", "2011-11-11 10:00:00"},
  2008  		{"\"2011-11-11 10:10:10\"", "\"10:10:10.100\"", "HOUR_MICROSECOND", "2011-11-11 20:20:20.100000", "2011-11-10 23:59:59.900000"},
  2009  		{"\"2011-11-11 10:10:10\"", "\"10:10:10\"", "HOUR_SECOND", "2011-11-11 20:20:20", "2011-11-11 00:00:00"},
  2010  		{"\"2011-11-11 10:10:10\"", "\"10:10\"", "HOUR_MINUTE", "2011-11-11 20:20:10", "2011-11-11 00:00:10"},
  2011  		{"\"2011-11-11 10:10:10\"", "\"11 10:10:10.100\"", "DAY_MICROSECOND", "2011-11-22 20:20:20.100000", "2011-10-30 23:59:59.900000"},
  2012  		{"\"2011-11-11 10:10:10\"", "\"11 10:10:10\"", "DAY_SECOND", "2011-11-22 20:20:20", "2011-10-31 00:00:00"},
  2013  		{"\"2011-11-11 10:10:10\"", "\"11 10:10\"", "DAY_MINUTE", "2011-11-22 20:20:10", "2011-10-31 00:00:10"},
  2014  		{"\"2011-11-11 10:10:10\"", "\"11 10\"", "DAY_HOUR", "2011-11-22 20:10:10", "2011-10-31 00:10:10"},
  2015  		{"\"2011-11-11 10:10:10\"", "\"11-1\"", "YEAR_MONTH", "2022-12-11 10:10:10", "2000-10-11 10:10:10"},
  2016  		{"\"2011-11-11 10:10:10\"", "\"11-11\"", "YEAR_MONTH", "2023-10-11 10:10:10", "1999-12-11 10:10:10"},
  2017  		{"\"2011-11-11 10:10:10\"", "\"20\"", "DAY", "2011-12-01 10:10:10", "2011-10-22 10:10:10"},
  2018  		{"\"2011-11-11 10:10:10\"", "19.88", "DAY", "2011-12-01 10:10:10", "2011-10-22 10:10:10"},
  2019  		{"\"2011-11-11 10:10:10\"", "\"19.88\"", "DAY", "2011-11-30 10:10:10", "2011-10-23 10:10:10"},
  2020  		{"\"2011-11-11 10:10:10\"", "\"prefix19suffix\"", "DAY", "2011-11-30 10:10:10", "2011-10-23 10:10:10"},
  2021  		{"\"2011-11-11 10:10:10\"", "\"20-11\"", "DAY", "2011-12-01 10:10:10", "2011-10-22 10:10:10"},
  2022  		{"\"2011-11-11 10:10:10\"", "\"20,11\"", "daY", "2011-12-01 10:10:10", "2011-10-22 10:10:10"},
  2023  		{"\"2011-11-11 10:10:10\"", "\"1000\"", "dAy", "2020-08-07 10:10:10", "2009-02-14 10:10:10"},
  2024  		{"\"2011-11-11 10:10:10\"", "\"true\"", "Day", "2011-11-12 10:10:10", "2011-11-10 10:10:10"},
  2025  		{"\"2011-11-11 10:10:10\"", "true", "Day", "2011-11-12 10:10:10", "2011-11-10 10:10:10"},
  2026  		{"\"2011-11-11\"", "1", "DAY", "2011-11-12", "2011-11-10"},
  2027  		{"\"2011-11-11\"", "10", "HOUR", "2011-11-11 10:00:00", "2011-11-10 14:00:00"},
  2028  		{"\"2011-11-11\"", "10", "MINUTE", "2011-11-11 00:10:00", "2011-11-10 23:50:00"},
  2029  		{"\"2011-11-11\"", "10", "SECOND", "2011-11-11 00:00:10", "2011-11-10 23:59:50"},
  2030  		{"\"2011-11-11\"", "\"10:10\"", "HOUR_MINUTE", "2011-11-11 10:10:00", "2011-11-10 13:50:00"},
  2031  		{"\"2011-11-11\"", "\"10:10:10\"", "HOUR_SECOND", "2011-11-11 10:10:10", "2011-11-10 13:49:50"},
  2032  		{"\"2011-11-11\"", "\"10:10:10.101010\"", "HOUR_MICROSECOND", "2011-11-11 10:10:10.101010", "2011-11-10 13:49:49.898990"},
  2033  		{"\"2011-11-11\"", "\"10:10\"", "MINUTE_SECOND", "2011-11-11 00:10:10", "2011-11-10 23:49:50"},
  2034  		{"\"2011-11-11\"", "\"10:10.101010\"", "MINUTE_MICROSECOND", "2011-11-11 00:10:10.101010", "2011-11-10 23:49:49.898990"},
  2035  		{"\"2011-11-11\"", "\"10.101010\"", "SECOND_MICROSECOND", "2011-11-11 00:00:10.101010", "2011-11-10 23:59:49.898990"},
  2036  		{"\"2011-11-11 00:00:00\"", "1", "DAY", "2011-11-12 00:00:00", "2011-11-10 00:00:00"},
  2037  		{"\"2011-11-11 00:00:00\"", "10", "HOUR", "2011-11-11 10:00:00", "2011-11-10 14:00:00"},
  2038  		{"\"2011-11-11 00:00:00\"", "10", "MINUTE", "2011-11-11 00:10:00", "2011-11-10 23:50:00"},
  2039  		{"\"2011-11-11 00:00:00\"", "10", "SECOND", "2011-11-11 00:00:10", "2011-11-10 23:59:50"},
  2040  
  2041  		{"\"2011-11-11\"", "\"abc1000\"", "MICROSECOND", "2011-11-11 00:00:00", "2011-11-11 00:00:00"},
  2042  		{"\"20111111 10:10:10\"", "\"1\"", "DAY", "<nil>", "<nil>"},
  2043  		{"\"2011-11-11\"", "\"10\"", "SECOND_MICROSECOND", "2011-11-11 00:00:00.100000", "2011-11-10 23:59:59.900000"},
  2044  		{"\"2011-11-11\"", "\"10.0000\"", "MINUTE_MICROSECOND", "2011-11-11 00:00:10", "2011-11-10 23:59:50"},
  2045  		{"\"2011-11-11\"", "\"10:10:10\"", "MINUTE_MICROSECOND", "2011-11-11 00:10:10.100000", "2011-11-10 23:49:49.900000"},
  2046  
  2047  		{"cast(\"2011-11-11\" as datetime)", "\"10:10:10\"", "MINUTE_MICROSECOND", "2011-11-11 00:10:10.100000", "2011-11-10 23:49:49.900000"},
  2048  		{"cast(\"2011-11-11 00:00:00\" as datetime)", "1", "DAY", "2011-11-12 00:00:00", "2011-11-10 00:00:00"},
  2049  		{"cast(\"2011-11-11 00:00:00\" as datetime)", "10", "HOUR", "2011-11-11 10:00:00", "2011-11-10 14:00:00"},
  2050  		{"cast(\"2011-11-11 00:00:00\" as datetime)", "10", "MINUTE", "2011-11-11 00:10:00", "2011-11-10 23:50:00"},
  2051  		{"cast(\"2011-11-11 00:00:00\" as datetime)", "10", "SECOND", "2011-11-11 00:00:10", "2011-11-10 23:59:50"},
  2052  
  2053  		{"cast(\"2011-11-11 00:00:00\" as datetime)", "\"1\"", "DAY", "2011-11-12 00:00:00", "2011-11-10 00:00:00"},
  2054  		{"cast(\"2011-11-11 00:00:00\" as datetime)", "\"10\"", "HOUR", "2011-11-11 10:00:00", "2011-11-10 14:00:00"},
  2055  		{"cast(\"2011-11-11 00:00:00\" as datetime)", "\"10\"", "MINUTE", "2011-11-11 00:10:00", "2011-11-10 23:50:00"},
  2056  		{"cast(\"2011-11-11 00:00:00\" as datetime)", "\"10\"", "SECOND", "2011-11-11 00:00:10", "2011-11-10 23:59:50"},
  2057  
  2058  		{"cast(\"2011-11-11\" as date)", "\"10:10:10\"", "MINUTE_MICROSECOND", "2011-11-11 00:10:10.100000", "2011-11-10 23:49:49.900000"},
  2059  		{"cast(\"2011-11-11 00:00:00\" as date)", "1", "DAY", "2011-11-12", "2011-11-10"},
  2060  		{"cast(\"2011-11-11 00:00:00\" as date)", "10", "HOUR", "2011-11-11 10:00:00", "2011-11-10 14:00:00"},
  2061  		{"cast(\"2011-11-11 00:00:00\" as date)", "10", "MINUTE", "2011-11-11 00:10:00", "2011-11-10 23:50:00"},
  2062  		{"cast(\"2011-11-11 00:00:00\" as date)", "10", "SECOND", "2011-11-11 00:00:10", "2011-11-10 23:59:50"},
  2063  
  2064  		{"cast(\"2011-11-11 00:00:00\" as date)", "\"1\"", "DAY", "2011-11-12", "2011-11-10"},
  2065  		{"cast(\"2011-11-11 00:00:00\" as date)", "\"10\"", "HOUR", "2011-11-11 10:00:00", "2011-11-10 14:00:00"},
  2066  		{"cast(\"2011-11-11 00:00:00\" as date)", "\"10\"", "MINUTE", "2011-11-11 00:10:00", "2011-11-10 23:50:00"},
  2067  		{"cast(\"2011-11-11 00:00:00\" as date)", "\"10\"", "SECOND", "2011-11-11 00:00:10", "2011-11-10 23:59:50"},
  2068  
  2069  		// interval decimal support
  2070  		{"\"2011-01-01 00:00:00\"", "10.10", "YEAR_MONTH", "2021-11-01 00:00:00", "2000-03-01 00:00:00"},
  2071  		{"\"2011-01-01 00:00:00\"", "10.10", "DAY_HOUR", "2011-01-11 10:00:00", "2010-12-21 14:00:00"},
  2072  		{"\"2011-01-01 00:00:00\"", "10.10", "HOUR_MINUTE", "2011-01-01 10:10:00", "2010-12-31 13:50:00"},
  2073  		{"\"2011-01-01 00:00:00\"", "10.10", "DAY_MINUTE", "2011-01-01 10:10:00", "2010-12-31 13:50:00"},
  2074  		{"\"2011-01-01 00:00:00\"", "10.10", "DAY_SECOND", "2011-01-01 00:10:10", "2010-12-31 23:49:50"},
  2075  		{"\"2011-01-01 00:00:00\"", "10.10", "HOUR_SECOND", "2011-01-01 00:10:10", "2010-12-31 23:49:50"},
  2076  		{"\"2011-01-01 00:00:00\"", "10.10", "MINUTE_SECOND", "2011-01-01 00:10:10", "2010-12-31 23:49:50"},
  2077  		{"\"2011-01-01 00:00:00\"", "10.10", "DAY_MICROSECOND", "2011-01-01 00:00:10.100000", "2010-12-31 23:59:49.900000"},
  2078  		{"\"2011-01-01 00:00:00\"", "10.10", "HOUR_MICROSECOND", "2011-01-01 00:00:10.100000", "2010-12-31 23:59:49.900000"},
  2079  		{"\"2011-01-01 00:00:00\"", "10.10", "MINUTE_MICROSECOND", "2011-01-01 00:00:10.100000", "2010-12-31 23:59:49.900000"},
  2080  		{"\"2011-01-01 00:00:00\"", "10.10", "SECOND_MICROSECOND", "2011-01-01 00:00:10.100000", "2010-12-31 23:59:49.900000"},
  2081  		{"\"2011-01-01 00:00:00\"", "10.10", "YEAR", "2021-01-01 00:00:00", "2001-01-01 00:00:00"},
  2082  		{"\"2011-01-01 00:00:00\"", "10.10", "QUARTER", "2020-07-01 00:00:00", "2008-07-01 00:00:00"},
  2083  		{"\"2011-01-01 00:00:00\"", "10.10", "MONTH", "2011-11-01 00:00:00", "2010-03-01 00:00:00"},
  2084  		{"\"2011-01-01 00:00:00\"", "10.10", "WEEK", "2011-03-12 00:00:00", "2010-10-23 00:00:00"},
  2085  		{"\"2011-01-01 00:00:00\"", "10.10", "DAY", "2011-01-11 00:00:00", "2010-12-22 00:00:00"},
  2086  		{"\"2011-01-01 00:00:00\"", "10.10", "HOUR", "2011-01-01 10:00:00", "2010-12-31 14:00:00"},
  2087  		{"\"2011-01-01 00:00:00\"", "10.10", "MINUTE", "2011-01-01 00:10:00", "2010-12-31 23:50:00"},
  2088  		{"\"2011-01-01 00:00:00\"", "10.10", "SECOND", "2011-01-01 00:00:10.100000", "2010-12-31 23:59:49.900000"},
  2089  		{"\"2011-01-01 00:00:00\"", "10.10", "MICROSECOND", "2011-01-01 00:00:00.000010", "2010-12-31 23:59:59.999990"},
  2090  		{"\"2011-01-01 00:00:00\"", "10.90", "MICROSECOND", "2011-01-01 00:00:00.000011", "2010-12-31 23:59:59.999989"},
  2091  
  2092  		{"\"2009-01-01\"", "6/4", "HOUR_MINUTE", "2009-01-04 12:20:00", "2008-12-28 11:40:00"},
  2093  		{"\"2009-01-01\"", "6/0", "HOUR_MINUTE", "<nil>", "<nil>"},
  2094  		{"\"1970-01-01 12:00:00\"", "CAST(6/4 AS DECIMAL(3,1))", "HOUR_MINUTE", "1970-01-01 13:05:00", "1970-01-01 10:55:00"},
  2095  		//for issue #8077
  2096  		{"\"2012-01-02\"", "\"prefix8\"", "HOUR", "2012-01-02 08:00:00", "2012-01-01 16:00:00"},
  2097  		{"\"2012-01-02\"", "\"prefix8prefix\"", "HOUR", "2012-01-02 08:00:00", "2012-01-01 16:00:00"},
  2098  		{"\"2012-01-02\"", "\"8:00\"", "HOUR", "2012-01-02 08:00:00", "2012-01-01 16:00:00"},
  2099  		{"\"2012-01-02\"", "\"8:00:00\"", "HOUR", "2012-01-02 08:00:00", "2012-01-01 16:00:00"},
  2100  	}
  2101  	for _, tc := range dateArithmeticalTests {
  2102  		addDate := fmt.Sprintf("select adddate(%s, interval %s %s);", tc.Date, tc.Interval, tc.Unit)
  2103  		subDate := fmt.Sprintf("select subdate(%s, interval %s %s);", tc.Date, tc.Interval, tc.Unit)
  2104  		result = tk.MustQuery(addDate)
  2105  		result.Check(testkit.Events(tc.AddResult))
  2106  		result = tk.MustQuery(subDate)
  2107  		result.Check(testkit.Events(tc.SubResult))
  2108  	}
  2109  	tk.MustQuery(`select subdate(cast("2000-02-01" as datetime), cast(1 as decimal))`).Check(testkit.Events("2000-01-31 00:00:00"))
  2110  	tk.MustQuery(`select subdate(cast("2000-02-01" as datetime), cast(null as decimal))`).Check(testkit.Events("<nil>"))
  2111  	tk.MustQuery(`select subdate(cast(null as datetime), cast(1 as decimal))`).Check(testkit.Events("<nil>"))
  2112  	tk.MustQuery(`select subdate(cast("2000-02-01" as datetime), cast("xxx" as decimal))`).Check(testkit.Events("2000-02-01 00:00:00"))
  2113  	tk.MustQuery(`select subdate(cast("xxx" as datetime), cast(1 as decimal))`).Check(testkit.Events("<nil>"))
  2114  	tk.MustQuery(`select subdate(cast(20000101 as SIGNED), cast("1" as decimal))`).Check(testkit.Events("1999-12-31"))
  2115  	tk.MustQuery(`select subdate(cast(20000101 as SIGNED), cast("xxx" as decimal))`).Check(testkit.Events("2000-01-01"))
  2116  	tk.MustQuery(`select subdate(cast("abc" as SIGNED), cast("1" as decimal))`).Check(testkit.Events("<nil>"))
  2117  	tk.MustQuery(`select subdate(cast(null as SIGNED), cast("1" as decimal))`).Check(testkit.Events("<nil>"))
  2118  	tk.MustQuery(`select subdate(cast(20000101 as SIGNED), cast(null as decimal))`).Check(testkit.Events("<nil>"))
  2119  	tk.MustQuery(`select adddate(cast("2000-02-01" as datetime), cast(1 as decimal))`).Check(testkit.Events("2000-02-02 00:00:00"))
  2120  	tk.MustQuery(`select adddate(cast("2000-02-01" as datetime), cast(null as decimal))`).Check(testkit.Events("<nil>"))
  2121  	tk.MustQuery(`select adddate(cast(null as datetime), cast(1 as decimal))`).Check(testkit.Events("<nil>"))
  2122  	tk.MustQuery(`select adddate(cast("2000-02-01" as datetime), cast("xxx" as decimal))`).Check(testkit.Events("2000-02-01 00:00:00"))
  2123  	tk.MustQuery(`select adddate(cast("xxx" as datetime), cast(1 as decimal))`).Check(testkit.Events("<nil>"))
  2124  	tk.MustQuery(`select adddate(cast("2000-02-01" as datetime), cast(1 as SIGNED))`).Check(testkit.Events("2000-02-02 00:00:00"))
  2125  	tk.MustQuery(`select adddate(cast("2000-02-01" as datetime), cast(null as SIGNED))`).Check(testkit.Events("<nil>"))
  2126  	tk.MustQuery(`select adddate(cast(null as datetime), cast(1 as SIGNED))`).Check(testkit.Events("<nil>"))
  2127  	tk.MustQuery(`select adddate(cast("2000-02-01" as datetime), cast("xxx" as SIGNED))`).Check(testkit.Events("2000-02-01 00:00:00"))
  2128  	tk.MustQuery(`select adddate(cast("xxx" as datetime), cast(1 as SIGNED))`).Check(testkit.Events("<nil>"))
  2129  	tk.MustQuery(`select adddate(20100101, cast(1 as decimal))`).Check(testkit.Events("2010-01-02"))
  2130  	tk.MustQuery(`select adddate(cast('10:10:10' as time), 1)`).Check(testkit.Events("34:10:10"))
  2131  	tk.MustQuery(`select adddate(cast('10:10:10' as time), cast(1 as decimal))`).Check(testkit.Events("34:10:10"))
  2132  
  2133  	// for localtime, localtimestamp
  2134  	result = tk.MustQuery(`select localtime() = now(), localtime = now(), localtimestamp() = now(), localtimestamp = now()`)
  2135  	result.Check(testkit.Events("1 1 1 1"))
  2136  
  2137  	// for current_timestamp, current_timestamp()
  2138  	result = tk.MustQuery(`select current_timestamp() = now(), current_timestamp = now()`)
  2139  	result.Check(testkit.Events("1 1"))
  2140  
  2141  	// for milevadb_parse_tso
  2142  	tk.MustInterDirc("SET time_zone = '+00:00';")
  2143  	result = tk.MustQuery(`select milevadb_parse_tso(404411537129996288)`)
  2144  	result.Check(testkit.Events("2020-11-20 09:53:04.877000"))
  2145  	result = tk.MustQuery(`select milevadb_parse_tso("404411537129996288")`)
  2146  	result.Check(testkit.Events("2020-11-20 09:53:04.877000"))
  2147  	result = tk.MustQuery(`select milevadb_parse_tso(1)`)
  2148  	result.Check(testkit.Events("1970-01-01 00:00:00.000000"))
  2149  	result = tk.MustQuery(`select milevadb_parse_tso(0)`)
  2150  	result.Check(testkit.Events("<nil>"))
  2151  	result = tk.MustQuery(`select milevadb_parse_tso(-1)`)
  2152  	result.Check(testkit.Events("<nil>"))
  2153  
  2154  	// fix issue 10308
  2155  	result = tk.MustQuery("select time(\"- -\");")
  2156  	result.Check(testkit.Events("00:00:00"))
  2157  	tk.MustQuery("show warnings;").Check(testkit.Events("Warning 1292 Truncated incorrect time value: '- -'"))
  2158  	result = tk.MustQuery("select time(\"---1\");")
  2159  	result.Check(testkit.Events("00:00:00"))
  2160  	tk.MustQuery("show warnings;").Check(testkit.Events("Warning 1292 Truncated incorrect time value: '---1'"))
  2161  	result = tk.MustQuery("select time(\"-- --1\");")
  2162  	result.Check(testkit.Events("00:00:00"))
  2163  	tk.MustQuery("show warnings;").Check(testkit.Events("Warning 1292 Truncated incorrect time value: '-- --1'"))
  2164  }
  2165  
  2166  func (s *testIntegrationSuite) TestOpBuiltin(c *C) {
  2167  	defer s.cleanEnv(c)
  2168  	tk := testkit.NewTestKit(c, s.causetstore)
  2169  	tk.MustInterDirc("use test")
  2170  
  2171  	// for logicAnd
  2172  	result := tk.MustQuery("select 1 && 1, 1 && 0, 0 && 1, 0 && 0, 2 && -1, null && 1, '1a' && 'a'")
  2173  	result.Check(testkit.Events("1 0 0 0 1 <nil> 0"))
  2174  	// for bitNeg
  2175  	result = tk.MustQuery("select ~123, ~-123, ~null")
  2176  	result.Check(testkit.Events("18446744073709551492 122 <nil>"))
  2177  	// for logicNot
  2178  	result = tk.MustQuery("select !1, !123, !0, !null")
  2179  	result.Check(testkit.Events("0 0 1 <nil>"))
  2180  	// for logicalXor
  2181  	result = tk.MustQuery("select 1 xor 1, 1 xor 0, 0 xor 1, 0 xor 0, 2 xor -1, null xor 1, '1a' xor 'a'")
  2182  	result.Check(testkit.Events("0 1 1 0 0 <nil> 1"))
  2183  	// for bitAnd
  2184  	result = tk.MustQuery("select 123 & 321, -123 & 321, null & 1")
  2185  	result.Check(testkit.Events("65 257 <nil>"))
  2186  	// for bitOr
  2187  	result = tk.MustQuery("select 123 | 321, -123 | 321, null | 1")
  2188  	result.Check(testkit.Events("379 18446744073709551557 <nil>"))
  2189  	// for bitXor
  2190  	result = tk.MustQuery("select 123 ^ 321, -123 ^ 321, null ^ 1")
  2191  	result.Check(testkit.Events("314 18446744073709551300 <nil>"))
  2192  	// for leftShift
  2193  	result = tk.MustQuery("select 123 << 2, -123 << 2, null << 1")
  2194  	result.Check(testkit.Events("492 18446744073709551124 <nil>"))
  2195  	// for rightShift
  2196  	result = tk.MustQuery("select 123 >> 2, -123 >> 2, null >> 1")
  2197  	result.Check(testkit.Events("30 4611686018427387873 <nil>"))
  2198  	// for logicOr
  2199  	result = tk.MustQuery("select 1 || 1, 1 || 0, 0 || 1, 0 || 0, 2 || -1, null || 1, '1a' || 'a'")
  2200  	result.Check(testkit.Events("1 1 1 0 1 1 1"))
  2201  	// for unaryPlus
  2202  	result = tk.MustQuery(`select +1, +0, +(-9), +(-0.001), +0.999, +null, +"aaa"`)
  2203  	result.Check(testkit.Events("1 0 -9 -0.001 0.999 <nil> aaa"))
  2204  	// for unaryMinus
  2205  	tk.MustInterDirc("drop causet if exists f")
  2206  	tk.MustInterDirc("create causet f(a decimal(65,0))")
  2207  	tk.MustInterDirc("insert into f value (-17000000000000000000)")
  2208  	result = tk.MustQuery("select a from f")
  2209  	result.Check(testkit.Events("-17000000000000000000"))
  2210  }
  2211  
  2212  func (s *testIntegrationSuite) TestDatetimeOverflow(c *C) {
  2213  	defer s.cleanEnv(c)
  2214  	tk := testkit.NewTestKit(c, s.causetstore)
  2215  	tk.MustInterDirc("use test")
  2216  
  2217  	tk.MustInterDirc("create causet t1 (d date)")
  2218  	tk.MustInterDirc("set sql_mode='traditional'")
  2219  	overflowALLEGROSQLs := []string{
  2220  		"insert into t1 (d) select date_add('2000-01-01',interval 8000 year)",
  2221  		"insert into t1 (d) select date_sub('2000-01-01', INTERVAL 2001 YEAR)",
  2222  		"insert into t1 (d) select date_add('9999-12-31',interval 1 year)",
  2223  		"insert into t1 (d) select date_sub('1000-01-01', INTERVAL 1 YEAR)",
  2224  		"insert into t1 (d) select date_add('9999-12-31',interval 1 day)",
  2225  		"insert into t1 (d) select date_sub('1000-01-01', INTERVAL 1 day)",
  2226  		"insert into t1 (d) select date_sub('1000-01-01', INTERVAL 1 second)",
  2227  	}
  2228  
  2229  	for _, allegrosql := range overflowALLEGROSQLs {
  2230  		_, err := tk.InterDirc(allegrosql)
  2231  		c.Assert(err.Error(), Equals, "[types:1441]Datetime function: datetime field overflow")
  2232  	}
  2233  
  2234  	tk.MustInterDirc("set sql_mode=''")
  2235  	for _, allegrosql := range overflowALLEGROSQLs {
  2236  		tk.MustInterDirc(allegrosql)
  2237  	}
  2238  
  2239  	rows := make([]string, 0, len(overflowALLEGROSQLs))
  2240  	for range overflowALLEGROSQLs {
  2241  		rows = append(rows, "<nil>")
  2242  	}
  2243  	tk.MustQuery("select * from t1").Check(testkit.Events(rows...))
  2244  
  2245  	//Fix ISSUE 11256
  2246  	tk.MustQuery(`select DATE_ADD('2000-04-13 07:17:02',INTERVAL -1465647104 YEAR);`).Check(testkit.Events("<nil>"))
  2247  	tk.MustQuery(`select DATE_ADD('2008-11-23 22:47:31',INTERVAL 266076160 QUARTER);`).Check(testkit.Events("<nil>"))
  2248  	tk.MustQuery(`select DATE_SUB('2000-04-13 07:17:02',INTERVAL 1465647104 YEAR);`).Check(testkit.Events("<nil>"))
  2249  	tk.MustQuery(`select DATE_SUB('2008-11-23 22:47:31',INTERVAL -266076160 QUARTER);`).Check(testkit.Events("<nil>"))
  2250  }
  2251  
  2252  func (s *testIntegrationSuite2) TestBuiltin(c *C) {
  2253  	defer s.cleanEnv(c)
  2254  	tk := testkit.NewTestKit(c, s.causetstore)
  2255  	tk.MustInterDirc("use test")
  2256  	ctx := context.Background()
  2257  
  2258  	// for is true && is false
  2259  	tk.MustInterDirc("drop causet if exists t")
  2260  	tk.MustInterDirc("create causet t (a int, b int, index idx_b (b))")
  2261  	tk.MustInterDirc("insert t values (1, 1)")
  2262  	tk.MustInterDirc("insert t values (2, 2)")
  2263  	tk.MustInterDirc("insert t values (3, 2)")
  2264  	result := tk.MustQuery("select * from t where b is true")
  2265  	result.Check(testkit.Events("1 1", "2 2", "3 2"))
  2266  	result = tk.MustQuery("select all + a from t where a = 1")
  2267  	result.Check(testkit.Events("1"))
  2268  	result = tk.MustQuery("select * from t where a is false")
  2269  	result.Check(nil)
  2270  	result = tk.MustQuery("select * from t where a is not true")
  2271  	result.Check(nil)
  2272  	result = tk.MustQuery(`select 1 is true, 0 is true, null is true, "aaa" is true, "" is true, -12.00 is true, 0.0 is true, 0.0000001 is true;`)
  2273  	result.Check(testkit.Events("1 0 0 0 0 1 0 1"))
  2274  	result = tk.MustQuery(`select 1 is false, 0 is false, null is false, "aaa" is false, "" is false, -12.00 is false, 0.0 is false, 0.0000001 is false;`)
  2275  	result.Check(testkit.Events("0 1 0 1 1 0 1 0"))
  2276  
  2277  	// for in
  2278  	result = tk.MustQuery("select * from t where b in (a)")
  2279  	result.Check(testkit.Events("1 1", "2 2"))
  2280  	result = tk.MustQuery("select * from t where b not in (a)")
  2281  	result.Check(testkit.Events("3 2"))
  2282  
  2283  	// test cast
  2284  	result = tk.MustQuery("select cast(1 as decimal(3,2))")
  2285  	result.Check(testkit.Events("1.00"))
  2286  	result = tk.MustQuery("select cast('1991-09-05 11:11:11' as datetime)")
  2287  	result.Check(testkit.Events("1991-09-05 11:11:11"))
  2288  	result = tk.MustQuery("select cast(cast('1991-09-05 11:11:11' as datetime) as char)")
  2289  	result.Check(testkit.Events("1991-09-05 11:11:11"))
  2290  	result = tk.MustQuery("select cast('11:11:11' as time)")
  2291  	result.Check(testkit.Events("11:11:11"))
  2292  	result = tk.MustQuery("select * from t where a > cast(2 as decimal)")
  2293  	result.Check(testkit.Events("3 2"))
  2294  	result = tk.MustQuery("select cast(-1 as unsigned)")
  2295  	result.Check(testkit.Events("18446744073709551615"))
  2296  	tk.MustInterDirc("drop causet if exists t")
  2297  	tk.MustInterDirc("create causet t(a decimal(3, 1), b double, c datetime, d time, e int)")
  2298  	tk.MustInterDirc("insert into t value(12.3, 1.23, '2020-01-01 12:12:12', '12:12:12', 123)")
  2299  	result = tk.MustQuery("select cast(a as json), cast(b as json), cast(c as json), cast(d as json), cast(e as json) from t")
  2300  	result.Check(testkit.Events(`12.3 1.23 "2020-01-01 12:12:12.000000" "12:12:12.000000" 123`))
  2301  	result = tk.MustQuery(`select cast(10101000000 as time);`)
  2302  	result.Check(testkit.Events("00:00:00"))
  2303  	result = tk.MustQuery(`select cast(10101001000 as time);`)
  2304  	result.Check(testkit.Events("00:10:00"))
  2305  	result = tk.MustQuery(`select cast(10000000000 as time);`)
  2306  	result.Check(testkit.Events("<nil>"))
  2307  	result = tk.MustQuery(`select cast(20171222020005 as time);`)
  2308  	result.Check(testkit.Events("02:00:05"))
  2309  	result = tk.MustQuery(`select cast(8380000 as time);`)
  2310  	result.Check(testkit.Events("838:00:00"))
  2311  	result = tk.MustQuery(`select cast(8390000 as time);`)
  2312  	result.Check(testkit.Events("<nil>"))
  2313  	result = tk.MustQuery(`select cast(8386000 as time);`)
  2314  	result.Check(testkit.Events("<nil>"))
  2315  	result = tk.MustQuery(`select cast(8385960 as time);`)
  2316  	result.Check(testkit.Events("<nil>"))
  2317  	result = tk.MustQuery(`select cast(cast('2020-01-01 01:01:11.12' as date) as datetime(2));`)
  2318  	result.Check(testkit.Events("2020-01-01 00:00:00.00"))
  2319  	result = tk.MustQuery(`select cast(20170118.999 as datetime);`)
  2320  	result.Check(testkit.Events("2020-01-18 00:00:00"))
  2321  	tk.MustQuery(`select convert(a2.a, unsigned int) from (select cast('"9223372036854775808"' as json) as a) as a2;`)
  2322  
  2323  	tk.MustInterDirc(`create causet tb5(a bigint(64) unsigned, b double);`)
  2324  	tk.MustInterDirc(`insert into tb5 (a, b) values (9223372036854776000, 9223372036854776000);`)
  2325  	tk.MustInterDirc(`insert into tb5 (a, b) select * from (select cast(a as json) as a1, b from tb5) as t where t.a1 = t.b;`)
  2326  	tk.MustInterDirc(`drop causet tb5;`)
  2327  
  2328  	tk.MustInterDirc(`create causet tb5(a float(64));`)
  2329  	tk.MustInterDirc(`insert into tb5(a) values (13835058055282163712);`)
  2330  	tk.MustQuery(`select convert(t.a1, signed int) from (select convert(a, json) as a1 from tb5) as t`)
  2331  	tk.MustInterDirc(`drop causet tb5;`)
  2332  
  2333  	// test builtinCastIntAsIntSig
  2334  	// Cast MaxUint64 to unsigned should be -1
  2335  	tk.MustQuery("select cast(0xffffffffffffffff as signed);").Check(testkit.Events("-1"))
  2336  	tk.MustQuery("select cast(0x9999999999999999999999999999999999999999999 as signed);").Check(testkit.Events("-1"))
  2337  	tk.MustInterDirc("create causet tb5(a bigint);")
  2338  	tk.MustInterDirc("set sql_mode=''")
  2339  	tk.MustInterDirc("insert into tb5(a) values (0xfffffffffffffffffffffffff);")
  2340  	tk.MustQuery("select * from tb5;").Check(testkit.Events("9223372036854775807"))
  2341  	tk.MustInterDirc("drop causet tb5;")
  2342  
  2343  	tk.MustInterDirc(`create causet tb5(a double);`)
  2344  	tk.MustInterDirc(`insert into test.tb5 (a) values (18446744073709551616);`)
  2345  	tk.MustInterDirc(`insert into test.tb5 (a) values (184467440737095516160);`)
  2346  	result = tk.MustQuery(`select cast(a as unsigned) from test.tb5;`)
  2347  	// Note: MyALLEGROSQL will return 9223372036854775807, and it should be a bug.
  2348  	result.Check(testkit.Events("18446744073709551615", "18446744073709551615"))
  2349  	tk.MustInterDirc(`drop causet tb5;`)
  2350  
  2351  	// test builtinCastIntAsDecimalSig
  2352  	tk.MustInterDirc(`create causet tb5(a bigint(64) unsigned, b decimal(64, 10));`)
  2353  	tk.MustInterDirc(`insert into tb5 (a, b) values (9223372036854775808, 9223372036854775808);`)
  2354  	tk.MustInterDirc(`insert into tb5 (select * from tb5 where a = b);`)
  2355  	result = tk.MustQuery(`select * from tb5;`)
  2356  	result.Check(testkit.Events("9223372036854775808 9223372036854775808.0000000000", "9223372036854775808 9223372036854775808.0000000000"))
  2357  	tk.MustInterDirc(`drop causet tb5;`)
  2358  
  2359  	// test builtinCastIntAsRealSig
  2360  	tk.MustInterDirc(`create causet tb5(a bigint(64) unsigned, b double(64, 10));`)
  2361  	tk.MustInterDirc(`insert into tb5 (a, b) values (13835058000000000000, 13835058000000000000);`)
  2362  	tk.MustInterDirc(`insert into tb5 (select * from tb5 where a = b);`)
  2363  	result = tk.MustQuery(`select * from tb5;`)
  2364  	result.Check(testkit.Events("13835058000000000000 13835058000000000000", "13835058000000000000 13835058000000000000"))
  2365  	tk.MustInterDirc(`drop causet tb5;`)
  2366  
  2367  	// test builtinCastRealAsIntSig
  2368  	tk.MustInterDirc(`create causet tb5(a double, b float);`)
  2369  	tk.MustInterDirc(`insert into tb5 (a, b) values (184467440737095516160, 184467440737095516160);`)
  2370  	tk.MustQuery(`select * from tb5 where cast(a as unsigned int)=0;`).Check(testkit.Events())
  2371  	tk.MustQuery("show warnings;").Check(testkit.Events("Warning 1690 constant 1.844674407370955e+20 overflows bigint"))
  2372  	_ = tk.MustQuery(`select * from tb5 where cast(b as unsigned int)=0;`)
  2373  	tk.MustQuery("show warnings;").Check(testkit.Events("Warning 1690 constant 1.844674407370955e+20 overflows bigint"))
  2374  	tk.MustInterDirc(`drop causet tb5;`)
  2375  	tk.MustInterDirc(`create causet tb5(a double, b bigint unsigned);`)
  2376  	tk.MustInterDirc(`insert into tb5 (a, b) values (18446744073709551616, 18446744073709551615);`)
  2377  	_ = tk.MustQuery(`select * from tb5 where cast(a as unsigned int)=b;`)
  2378  	// TODO `obtained string = "[18446744073709552000 18446744073709551615]`
  2379  	// result.Check(testkit.Events("18446744073709551616 18446744073709551615"))
  2380  	tk.MustQuery("show warnings;").Check(testkit.Events())
  2381  	tk.MustInterDirc(`drop causet tb5;`)
  2382  
  2383  	// test builtinCastJSONAsIntSig
  2384  	tk.MustInterDirc(`create causet tb5(a json, b bigint unsigned);`)
  2385  	tk.MustInterDirc(`insert into tb5 (a, b) values ('184467440737095516160', 18446744073709551615);`)
  2386  	_ = tk.MustQuery(`select * from tb5 where cast(a as unsigned int)=b;`)
  2387  	tk.MustQuery("show warnings;").Check(testkit.Events("Warning 1690 constant 1.844674407370955e+20 overflows bigint"))
  2388  	_ = tk.MustQuery(`select * from tb5 where cast(b as unsigned int)=0;`)
  2389  	tk.MustQuery("show warnings;").Check(testkit.Events())
  2390  	tk.MustInterDirc(`drop causet tb5;`)
  2391  	tk.MustInterDirc(`create causet tb5(a json, b bigint unsigned);`)
  2392  	tk.MustInterDirc(`insert into tb5 (a, b) values ('92233720368547758080', 18446744073709551615);`)
  2393  	_ = tk.MustQuery(`select * from tb5 where cast(a as signed int)=b;`)
  2394  	tk.MustQuery("show warnings;").Check(testkit.Events("Warning 1690 constant 9.223372036854776e+19 overflows bigint"))
  2395  	tk.MustInterDirc(`drop causet tb5;`)
  2396  
  2397  	// test builtinCastIntAsStringSig
  2398  	tk.MustInterDirc(`create causet tb5(a bigint(64) unsigned,b varchar(50));`)
  2399  	tk.MustInterDirc(`insert into tb5(a, b) values (9223372036854775808, '9223372036854775808');`)
  2400  	tk.MustInterDirc(`insert into tb5(select * from tb5 where a = b);`)
  2401  	result = tk.MustQuery(`select * from tb5;`)
  2402  	result.Check(testkit.Events("9223372036854775808 9223372036854775808", "9223372036854775808 9223372036854775808"))
  2403  	tk.MustInterDirc(`drop causet tb5;`)
  2404  
  2405  	// test builtinCastIntAsDecimalSig
  2406  	tk.MustInterDirc(`drop causet if exists tb5`)
  2407  	tk.MustInterDirc(`create causet tb5 (a decimal(65), b bigint(64) unsigned);`)
  2408  	tk.MustInterDirc(`insert into tb5 (a, b) values (9223372036854775808, 9223372036854775808);`)
  2409  	result = tk.MustQuery(`select cast(b as decimal(64)) from tb5 union all select b from tb5;`)
  2410  	result.Check(testkit.Events("9223372036854775808", "9223372036854775808"))
  2411  	tk.MustInterDirc(`drop causet tb5`)
  2412  
  2413  	// test builtinCastIntAsRealSig
  2414  	tk.MustInterDirc(`drop causet if exists tb5`)
  2415  	tk.MustInterDirc(`create causet tb5 (a bigint(64) unsigned, b double(64, 10));`)
  2416  	tk.MustInterDirc(`insert into tb5 (a, b) values (9223372036854775808, 9223372036854775808);`)
  2417  	result = tk.MustQuery(`select a from tb5 where a = b union all select b from tb5;`)
  2418  	result.Check(testkit.Events("9223372036854776000", "9223372036854776000"))
  2419  	tk.MustInterDirc(`drop causet tb5`)
  2420  
  2421  	// Test corner cases of cast string as datetime
  2422  	result = tk.MustQuery(`select cast("170102034" as datetime);`)
  2423  	result.Check(testkit.Events("2020-01-02 03:04:00"))
  2424  	result = tk.MustQuery(`select cast("1701020304" as datetime);`)
  2425  	result.Check(testkit.Events("2020-01-02 03:04:00"))
  2426  	result = tk.MustQuery(`select cast("1701020304." as datetime);`)
  2427  	result.Check(testkit.Events("2020-01-02 03:04:00"))
  2428  	result = tk.MustQuery(`select cast("1701020304.1" as datetime);`)
  2429  	result.Check(testkit.Events("2020-01-02 03:04:01"))
  2430  	result = tk.MustQuery(`select cast("1701020304.111" as datetime);`)
  2431  	result.Check(testkit.Events("2020-01-02 03:04:11"))
  2432  	tk.MustQuery("show warnings;").Check(testkit.Events("Warning 1292 Truncated incorrect datetime value: '1701020304.111'"))
  2433  	result = tk.MustQuery(`select cast("17011" as datetime);`)
  2434  	result.Check(testkit.Events("2020-01-01 00:00:00"))
  2435  	result = tk.MustQuery(`select cast("150101." as datetime);`)
  2436  	result.Check(testkit.Events("2020-01-01 00:00:00"))
  2437  	result = tk.MustQuery(`select cast("150101.a" as datetime);`)
  2438  	result.Check(testkit.Events("2020-01-01 00:00:00"))
  2439  	tk.MustQuery("show warnings;").Check(testkit.Events("Warning 1292 Truncated incorrect datetime value: '150101.a'"))
  2440  	result = tk.MustQuery(`select cast("150101.1a" as datetime);`)
  2441  	result.Check(testkit.Events("2020-01-01 01:00:00"))
  2442  	tk.MustQuery("show warnings;").Check(testkit.Events("Warning 1292 Truncated incorrect datetime value: '150101.1a'"))
  2443  	result = tk.MustQuery(`select cast("150101.1a1" as datetime);`)
  2444  	result.Check(testkit.Events("2020-01-01 01:00:00"))
  2445  	tk.MustQuery("show warnings;").Check(testkit.Events("Warning 1292 Truncated incorrect datetime value: '150101.1a1'"))
  2446  	result = tk.MustQuery(`select cast("1101010101.111" as datetime);`)
  2447  	result.Check(testkit.Events("2011-01-01 01:01:11"))
  2448  	tk.MustQuery("show warnings;").Check(testkit.Events("Warning 1292 Truncated incorrect datetime value: '1101010101.111'"))
  2449  	result = tk.MustQuery(`select cast("1101010101.11aaaaa" as datetime);`)
  2450  	result.Check(testkit.Events("2011-01-01 01:01:11"))
  2451  	tk.MustQuery("show warnings;").Check(testkit.Events("Warning 1292 Truncated incorrect datetime value: '1101010101.11aaaaa'"))
  2452  	result = tk.MustQuery(`select cast("1101010101.a1aaaaa" as datetime);`)
  2453  	result.Check(testkit.Events("2011-01-01 01:01:00"))
  2454  	tk.MustQuery("show warnings;").Check(testkit.Events("Warning 1292 Truncated incorrect datetime value: '1101010101.a1aaaaa'"))
  2455  	result = tk.MustQuery(`select cast("1101010101.11" as datetime);`)
  2456  	result.Check(testkit.Events("2011-01-01 01:01:11"))
  2457  	tk.MustQuery("select @@warning_count;").Check(testkit.Events("0"))
  2458  	result = tk.MustQuery(`select cast("1101010101.111" as datetime);`)
  2459  	result.Check(testkit.Events("2011-01-01 01:01:11"))
  2460  	tk.MustQuery("show warnings;").Check(testkit.Events("Warning 1292 Truncated incorrect datetime value: '1101010101.111'"))
  2461  	result = tk.MustQuery(`select cast("970101.111" as datetime);`)
  2462  	result.Check(testkit.Events("1997-01-01 11:01:00"))
  2463  	tk.MustQuery("select @@warning_count;").Check(testkit.Events("0"))
  2464  	result = tk.MustQuery(`select cast("970101.11111" as datetime);`)
  2465  	result.Check(testkit.Events("1997-01-01 11:11:01"))
  2466  	tk.MustQuery("select @@warning_count;").Check(testkit.Events("0"))
  2467  	result = tk.MustQuery(`select cast("970101.111a1" as datetime);`)
  2468  	result.Check(testkit.Events("1997-01-01 11:01:00"))
  2469  	tk.MustQuery("show warnings;").Check(testkit.Events("Warning 1292 Truncated incorrect datetime value: '970101.111a1'"))
  2470  
  2471  	// for ISNULL
  2472  	tk.MustInterDirc("drop causet if exists t")
  2473  	tk.MustInterDirc("create causet t (a int, b int, c int, d char(10), e datetime, f float, g decimal(10, 3))")
  2474  	tk.MustInterDirc("insert t values (1, 0, null, null, null, null, null)")
  2475  	result = tk.MustQuery("select ISNULL(a), ISNULL(b), ISNULL(c), ISNULL(d), ISNULL(e), ISNULL(f), ISNULL(g) from t")
  2476  	result.Check(testkit.Events("0 0 1 1 1 1 1"))
  2477  
  2478  	// fix issue #3942
  2479  	result = tk.MustQuery("select cast('-24 100:00:00' as time);")
  2480  	result.Check(testkit.Events("-676:00:00"))
  2481  	result = tk.MustQuery("select cast('12:00:00.000000' as datetime);")
  2482  	result.Check(testkit.Events("2012-00-00 00:00:00"))
  2483  	result = tk.MustQuery("select cast('-34 100:00:00' as time);")
  2484  	result.Check(testkit.Events("-838:59:59"))
  2485  
  2486  	// fix issue #4324. cast decimal/int/string to time compatibility.
  2487  	invalidTimes := []string{
  2488  		"10009010",
  2489  		"239010",
  2490  		"233070",
  2491  		"23:90:10",
  2492  		"23:30:70",
  2493  		"239010.2",
  2494  		"233070.8",
  2495  	}
  2496  	tk.MustInterDirc("DROP TABLE IF EXISTS t;")
  2497  	tk.MustInterDirc("CREATE TABLE t (ix TIME);")
  2498  	tk.MustInterDirc("SET ALLEGROSQL_MODE='';")
  2499  	for _, invalidTime := range invalidTimes {
  2500  		msg := fmt.Sprintf("Warning 1292 Truncated incorrect time value: '%s'", invalidTime)
  2501  		result = tk.MustQuery(fmt.Sprintf("select cast('%s' as time);", invalidTime))
  2502  		result.Check(testkit.Events("<nil>"))
  2503  		result = tk.MustQuery("show warnings")
  2504  		result.Check(testkit.Events(msg))
  2505  		_, err := tk.InterDirc(fmt.Sprintf("insert into t select cast('%s' as time);", invalidTime))
  2506  		c.Assert(err, IsNil)
  2507  		result = tk.MustQuery("show warnings")
  2508  		result.Check(testkit.Events(msg))
  2509  	}
  2510  	tk.MustInterDirc("set sql_mode = 'STRICT_TRANS_TABLES'")
  2511  	for _, invalidTime := range invalidTimes {
  2512  		msg := fmt.Sprintf("Warning 1292 Truncated incorrect time value: '%s'", invalidTime)
  2513  		result = tk.MustQuery(fmt.Sprintf("select cast('%s' as time);", invalidTime))
  2514  		result.Check(testkit.Events("<nil>"))
  2515  		result = tk.MustQuery("show warnings")
  2516  		result.Check(testkit.Events(msg))
  2517  		_, err := tk.InterDirc(fmt.Sprintf("insert into t select cast('%s' as time);", invalidTime))
  2518  		c.Assert(err.Error(), Equals, fmt.Sprintf("[types:1292]Truncated incorrect time value: '%s'", invalidTime))
  2519  	}
  2520  
  2521  	// Fix issue #3691, cast compatibility.
  2522  	result = tk.MustQuery("select cast('18446744073709551616' as unsigned);")
  2523  	result.Check(testkit.Events("18446744073709551615"))
  2524  	result = tk.MustQuery("select cast('18446744073709551616' as signed);")
  2525  	result.Check(testkit.Events("-1"))
  2526  	result = tk.MustQuery("select cast('9223372036854775808' as signed);")
  2527  	result.Check(testkit.Events("-9223372036854775808"))
  2528  	result = tk.MustQuery("select cast('9223372036854775809' as signed);")
  2529  	result.Check(testkit.Events("-9223372036854775807"))
  2530  	result = tk.MustQuery("select cast('9223372036854775807' as signed);")
  2531  	result.Check(testkit.Events("9223372036854775807"))
  2532  	result = tk.MustQuery("select cast('18446744073709551615' as signed);")
  2533  	result.Check(testkit.Events("-1"))
  2534  	result = tk.MustQuery("select cast('18446744073709551614' as signed);")
  2535  	result.Check(testkit.Events("-2"))
  2536  	result = tk.MustQuery("select cast(18446744073709551615 as unsigned);")
  2537  	result.Check(testkit.Events("18446744073709551615"))
  2538  	result = tk.MustQuery("select cast(18446744073709551616 as unsigned);")
  2539  	result.Check(testkit.Events("18446744073709551615"))
  2540  	result = tk.MustQuery("select cast(18446744073709551616 as signed);")
  2541  	result.Check(testkit.Events("9223372036854775807"))
  2542  	result = tk.MustQuery("select cast(18446744073709551617 as signed);")
  2543  	result.Check(testkit.Events("9223372036854775807"))
  2544  	result = tk.MustQuery("select cast(18446744073709551615 as signed);")
  2545  	result.Check(testkit.Events("-1"))
  2546  	result = tk.MustQuery("select cast(18446744073709551614 as signed);")
  2547  	result.Check(testkit.Events("-2"))
  2548  	result = tk.MustQuery("select cast(-18446744073709551616 as signed);")
  2549  	result.Check(testkit.Events("-9223372036854775808"))
  2550  	result = tk.MustQuery("select cast(18446744073709551614.9 as unsigned);") // Round up
  2551  	result.Check(testkit.Events("18446744073709551615"))
  2552  	result = tk.MustQuery("select cast(18446744073709551614.4 as unsigned);") // Round down
  2553  	result.Check(testkit.Events("18446744073709551614"))
  2554  	result = tk.MustQuery("select cast(-9223372036854775809 as signed);")
  2555  	result.Check(testkit.Events("-9223372036854775808"))
  2556  	result = tk.MustQuery("select cast(-9223372036854775809 as unsigned);")
  2557  	result.Check(testkit.Events("0"))
  2558  	result = tk.MustQuery("select cast(-9223372036854775808 as unsigned);")
  2559  	result.Check(testkit.Events("9223372036854775808"))
  2560  	result = tk.MustQuery("select cast('-9223372036854775809' as unsigned);")
  2561  	result.Check(testkit.Events("9223372036854775808"))
  2562  	result = tk.MustQuery("select cast('-9223372036854775807' as unsigned);")
  2563  	result.Check(testkit.Events("9223372036854775809"))
  2564  	result = tk.MustQuery("select cast('-2' as unsigned);")
  2565  	result.Check(testkit.Events("18446744073709551614"))
  2566  	result = tk.MustQuery("select cast(cast(1-2 as unsigned) as signed integer);")
  2567  	result.Check(testkit.Events("-1"))
  2568  	result = tk.MustQuery("select cast(1 as signed int)")
  2569  	result.Check(testkit.Events("1"))
  2570  
  2571  	// test cast as double
  2572  	result = tk.MustQuery("select cast(1 as double)")
  2573  	result.Check(testkit.Events("1"))
  2574  	result = tk.MustQuery("select cast(cast(12345 as unsigned) as double)")
  2575  	result.Check(testkit.Events("12345"))
  2576  	result = tk.MustQuery("select cast(1.1 as double)")
  2577  	result.Check(testkit.Events("1.1"))
  2578  	result = tk.MustQuery("select cast(-1.1 as double)")
  2579  	result.Check(testkit.Events("-1.1"))
  2580  	result = tk.MustQuery("select cast('123.321' as double)")
  2581  	result.Check(testkit.Events("123.321"))
  2582  	result = tk.MustQuery("select cast('12345678901234567890' as double) = 1.2345678901234567e19")
  2583  	result.Check(testkit.Events("1"))
  2584  	result = tk.MustQuery("select cast(-1 as double)")
  2585  	result.Check(testkit.Events("-1"))
  2586  	result = tk.MustQuery("select cast(null as double)")
  2587  	result.Check(testkit.Events("<nil>"))
  2588  	result = tk.MustQuery("select cast(12345678901234567890 as double) = 1.2345678901234567e19")
  2589  	result.Check(testkit.Events("1"))
  2590  	result = tk.MustQuery("select cast(cast(-1 as unsigned) as double) = 1.8446744073709552e19")
  2591  	result.Check(testkit.Events("1"))
  2592  	result = tk.MustQuery("select cast(1e100 as double) = 1e100")
  2593  	result.Check(testkit.Events("1"))
  2594  	result = tk.MustQuery("select cast(123456789012345678901234567890 as double) = 1.2345678901234568e29")
  2595  	result.Check(testkit.Events("1"))
  2596  	result = tk.MustQuery("select cast(0x12345678 as double)")
  2597  	result.Check(testkit.Events("305419896"))
  2598  
  2599  	// test cast as float
  2600  	result = tk.MustQuery("select cast(1 as float)")
  2601  	result.Check(testkit.Events("1"))
  2602  	result = tk.MustQuery("select cast(cast(12345 as unsigned) as float)")
  2603  	result.Check(testkit.Events("12345"))
  2604  	result = tk.MustQuery("select cast(1.1 as float) = 1.1")
  2605  	result.Check(testkit.Events("1"))
  2606  	result = tk.MustQuery("select cast(-1.1 as float) = -1.1")
  2607  	result.Check(testkit.Events("1"))
  2608  	result = tk.MustQuery("select cast('123.321' as float) =123.321")
  2609  	result.Check(testkit.Events("1"))
  2610  	result = tk.MustQuery("select cast('12345678901234567890' as float) = 1.2345678901234567e19")
  2611  	result.Check(testkit.Events("1"))
  2612  	result = tk.MustQuery("select cast(-1 as float)")
  2613  	result.Check(testkit.Events("-1"))
  2614  	result = tk.MustQuery("select cast(null as float)")
  2615  	result.Check(testkit.Events("<nil>"))
  2616  	result = tk.MustQuery("select cast(12345678901234567890 as float) = 1.2345678901234567e19")
  2617  	result.Check(testkit.Events("1"))
  2618  	result = tk.MustQuery("select cast(cast(-1 as unsigned) as float) = 1.8446744073709552e19")
  2619  	result.Check(testkit.Events("1"))
  2620  	result = tk.MustQuery("select cast(1e100 as float(40)) = 1e100")
  2621  	result.Check(testkit.Events("1"))
  2622  	result = tk.MustQuery("select cast(123456789012345678901234567890 as float(40)) = 1.2345678901234568e29")
  2623  	result.Check(testkit.Events("1"))
  2624  	result = tk.MustQuery("select cast(0x12345678 as float(40)) = 305419896")
  2625  	result.Check(testkit.Events("1"))
  2626  
  2627  	// test cast as real
  2628  	result = tk.MustQuery("select cast(1 as real)")
  2629  	result.Check(testkit.Events("1"))
  2630  	result = tk.MustQuery("select cast(cast(12345 as unsigned) as real)")
  2631  	result.Check(testkit.Events("12345"))
  2632  	result = tk.MustQuery("select cast(1.1 as real) = 1.1")
  2633  	result.Check(testkit.Events("1"))
  2634  	result = tk.MustQuery("select cast(-1.1 as real) = -1.1")
  2635  	result.Check(testkit.Events("1"))
  2636  	result = tk.MustQuery("select cast('123.321' as real) =123.321")
  2637  	result.Check(testkit.Events("1"))
  2638  	result = tk.MustQuery("select cast('12345678901234567890' as real) = 1.2345678901234567e19")
  2639  	result.Check(testkit.Events("1"))
  2640  	result = tk.MustQuery("select cast(-1 as real)")
  2641  	result.Check(testkit.Events("-1"))
  2642  	result = tk.MustQuery("select cast(null as real)")
  2643  	result.Check(testkit.Events("<nil>"))
  2644  	result = tk.MustQuery("select cast(12345678901234567890 as real) = 1.2345678901234567e19")
  2645  	result.Check(testkit.Events("1"))
  2646  	result = tk.MustQuery("select cast(cast(-1 as unsigned) as real) = 1.8446744073709552e19")
  2647  	result.Check(testkit.Events("1"))
  2648  	result = tk.MustQuery("select cast(1e100 as real) = 1e100")
  2649  	result.Check(testkit.Events("1"))
  2650  	result = tk.MustQuery("select cast(123456789012345678901234567890 as real) = 1.2345678901234568e29")
  2651  	result.Check(testkit.Events("1"))
  2652  	result = tk.MustQuery("select cast(0x12345678 as real) = 305419896")
  2653  	result.Check(testkit.Events("1"))
  2654  
  2655  	// test cast time as decimal overflow
  2656  	tk.MustInterDirc("drop causet if exists t1")
  2657  	tk.MustInterDirc("create causet t1(s1 time);")
  2658  	tk.MustInterDirc("insert into t1 values('11:11:11');")
  2659  	result = tk.MustQuery("select cast(s1 as decimal(7, 2)) from t1;")
  2660  	result.Check(testkit.Events("99999.99"))
  2661  	result = tk.MustQuery("select cast(s1 as decimal(8, 2)) from t1;")
  2662  	result.Check(testkit.Events("111111.00"))
  2663  	_, err := tk.InterDirc("insert into t1 values(cast('111111.00' as decimal(7, 2)));")
  2664  	c.Assert(err, NotNil)
  2665  
  2666  	result = tk.MustQuery(`select CAST(0x8fffffffffffffff as signed) a,
  2667  	CAST(0xfffffffffffffffe as signed) b,
  2668  	CAST(0xffffffffffffffff as unsigned) c;`)
  2669  	result.Check(testkit.Events("-8070450532247928833 -2 18446744073709551615"))
  2670  
  2671  	result = tk.MustQuery(`select cast("1:2:3" as TIME) = "1:02:03"`)
  2672  	result.Check(testkit.Events("0"))
  2673  
  2674  	// fixed issue #3471
  2675  	tk.MustInterDirc("drop causet if exists t")
  2676  	tk.MustInterDirc("create causet t(a time(6));")
  2677  	tk.MustInterDirc("insert into t value('12:59:59.999999')")
  2678  	result = tk.MustQuery("select cast(a as signed) from t")
  2679  	result.Check(testkit.Events("130000"))
  2680  
  2681  	// fixed issue #3762
  2682  	result = tk.MustQuery("select -9223372036854775809;")
  2683  	result.Check(testkit.Events("-9223372036854775809"))
  2684  	result = tk.MustQuery("select --9223372036854775809;")
  2685  	result.Check(testkit.Events("9223372036854775809"))
  2686  	result = tk.MustQuery("select -9223372036854775808;")
  2687  	result.Check(testkit.Events("-9223372036854775808"))
  2688  
  2689  	tk.MustInterDirc("drop causet if exists t")
  2690  	tk.MustInterDirc("create causet t(a bigint(30));")
  2691  	_, err = tk.InterDirc("insert into t values(-9223372036854775809)")
  2692  	c.Assert(err, NotNil)
  2693  
  2694  	// test case decimal precision less than the scale.
  2695  	rs, err := tk.InterDirc("select cast(12.1 as decimal(3, 4));")
  2696  	c.Assert(err, IsNil)
  2697  	_, err = stochastik.GetEvents4Test(ctx, tk.Se, rs)
  2698  	c.Assert(err, NotNil)
  2699  	c.Assert(err.Error(), Equals, "[types:1427]For float(M,D), double(M,D) or decimal(M,D), M must be >= D (defCausumn '').")
  2700  	c.Assert(rs.Close(), IsNil)
  2701  
  2702  	// test unhex and hex
  2703  	result = tk.MustQuery("select unhex('4D7953514C')")
  2704  	result.Check(testkit.Events("MyALLEGROSQL"))
  2705  	result = tk.MustQuery("select unhex(hex('string'))")
  2706  	result.Check(testkit.Events("string"))
  2707  	result = tk.MustQuery("select unhex('ggg')")
  2708  	result.Check(testkit.Events("<nil>"))
  2709  	result = tk.MustQuery("select unhex(-1)")
  2710  	result.Check(testkit.Events("<nil>"))
  2711  	result = tk.MustQuery("select hex(unhex('1267'))")
  2712  	result.Check(testkit.Events("1267"))
  2713  	result = tk.MustQuery("select hex(unhex(1267))")
  2714  	result.Check(testkit.Events("1267"))
  2715  	tk.MustInterDirc("drop causet if exists t")
  2716  	tk.MustInterDirc("create causet t(a binary(8))")
  2717  	tk.MustInterDirc(`insert into t values('test')`)
  2718  	result = tk.MustQuery("select hex(a) from t")
  2719  	result.Check(testkit.Events("7465737400000000"))
  2720  	result = tk.MustQuery("select unhex(a) from t")
  2721  	result.Check(testkit.Events("<nil>"))
  2722  
  2723  	// select from_unixtime
  2724  	// NOTE (#17013): make from_unixtime sblock in different timezone: the result of from_unixtime
  2725  	// depends on the local time zone of the test environment, thus the result checking must
  2726  	// consider the time zone convert.
  2727  	tz := tk.Se.GetStochastikVars().StmtCtx.TimeZone
  2728  	result = tk.MustQuery("select from_unixtime(1451606400)")
  2729  	unixTime := time.Unix(1451606400, 0).In(tz).String()[:19]
  2730  	result.Check(testkit.Events(unixTime))
  2731  	result = tk.MustQuery("select from_unixtime(14516064000/10)")
  2732  	result.Check(testkit.Events(fmt.Sprintf("%s.0000", unixTime)))
  2733  	result = tk.MustQuery("select from_unixtime('14516064000'/10)")
  2734  	result.Check(testkit.Events(fmt.Sprintf("%s.000000", unixTime)))
  2735  	result = tk.MustQuery("select from_unixtime(cast(1451606400 as double))")
  2736  	result.Check(testkit.Events(fmt.Sprintf("%s.000000", unixTime)))
  2737  	result = tk.MustQuery("select from_unixtime(cast(cast(1451606400 as double) as DECIMAL))")
  2738  	result.Check(testkit.Events(unixTime))
  2739  	result = tk.MustQuery("select from_unixtime(cast(cast(1451606400 as double) as DECIMAL(65,1)))")
  2740  	result.Check(testkit.Events(fmt.Sprintf("%s.0", unixTime)))
  2741  	result = tk.MustQuery("select from_unixtime(1451606400.123456)")
  2742  	unixTime = time.Unix(1451606400, 123456000).In(tz).String()[:26]
  2743  	result.Check(testkit.Events(unixTime))
  2744  	result = tk.MustQuery("select from_unixtime(1451606400.1234567)")
  2745  	unixTime = time.Unix(1451606400, 123456700).In(tz).Round(time.Microsecond).Format("2006-01-02 15:04:05.000000")[:26]
  2746  	result.Check(testkit.Events(unixTime))
  2747  	result = tk.MustQuery("select from_unixtime(1451606400.999999)")
  2748  	unixTime = time.Unix(1451606400, 999999000).In(tz).String()[:26]
  2749  	result.Check(testkit.Events(unixTime))
  2750  	result = tk.MustQuery("select from_unixtime(1511247196661)")
  2751  	result.Check(testkit.Events("<nil>"))
  2752  	result = tk.MustQuery("select from_unixtime('1451606400.123');")
  2753  	unixTime = time.Unix(1451606400, 0).In(tz).String()[:19]
  2754  	result.Check(testkit.Events(fmt.Sprintf("%s.123000", unixTime)))
  2755  
  2756  	tk.MustInterDirc("drop causet if exists t;")
  2757  	tk.MustInterDirc("create causet t(a int);")
  2758  	tk.MustInterDirc("insert into t value(1451606400);")
  2759  	result = tk.MustQuery("select from_unixtime(a) from t;")
  2760  	result.Check(testkit.Events(unixTime))
  2761  
  2762  	// test strcmp
  2763  	result = tk.MustQuery("select strcmp('abc', 'def')")
  2764  	result.Check(testkit.Events("-1"))
  2765  	result = tk.MustQuery("select strcmp('abc', 'aba')")
  2766  	result.Check(testkit.Events("1"))
  2767  	result = tk.MustQuery("select strcmp('abc', 'abc')")
  2768  	result.Check(testkit.Events("0"))
  2769  	result = tk.MustQuery("select substr(null, 1, 2)")
  2770  	result.Check(testkit.Events("<nil>"))
  2771  	result = tk.MustQuery("select substr('123', null, 2)")
  2772  	result.Check(testkit.Events("<nil>"))
  2773  	result = tk.MustQuery("select substr('123', 1, null)")
  2774  	result.Check(testkit.Events("<nil>"))
  2775  
  2776  	// for case
  2777  	tk.MustInterDirc("drop causet if exists t")
  2778  	tk.MustInterDirc("create causet t (a varchar(255), b int)")
  2779  	tk.MustInterDirc("insert t values ('str1', 1)")
  2780  	result = tk.MustQuery("select * from t where a = case b when 1 then 'str1' when 2 then 'str2' end")
  2781  	result.Check(testkit.Events("str1 1"))
  2782  	result = tk.MustQuery("select * from t where a = case b when 1 then 'str2' when 2 then 'str3' end")
  2783  	result.Check(nil)
  2784  	tk.MustInterDirc("insert t values ('str2', 2)")
  2785  	result = tk.MustQuery("select * from t where a = case b when 2 then 'str2' when 3 then 'str3' end")
  2786  	result.Check(testkit.Events("str2 2"))
  2787  	tk.MustInterDirc("insert t values ('str3', 3)")
  2788  	result = tk.MustQuery("select * from t where a = case b when 4 then 'str4' when 5 then 'str5' else 'str3' end")
  2789  	result.Check(testkit.Events("str3 3"))
  2790  	result = tk.MustQuery("select * from t where a = case b when 4 then 'str4' when 5 then 'str5' else 'str6' end")
  2791  	result.Check(nil)
  2792  	result = tk.MustQuery("select * from t where a = case  when b then 'str3' when 1 then 'str1' else 'str2' end")
  2793  	result.Check(testkit.Events("str3 3"))
  2794  	tk.MustInterDirc("delete from t")
  2795  	tk.MustInterDirc("insert t values ('str2', 0)")
  2796  	result = tk.MustQuery("select * from t where a = case  when b then 'str3' when 0 then 'str1' else 'str2' end")
  2797  	result.Check(testkit.Events("str2 0"))
  2798  	tk.MustInterDirc("insert t values ('str1', null)")
  2799  	result = tk.MustQuery("select * from t where a = case b when null then 'str3' when 10 then 'str1' else 'str2' end")
  2800  	result.Check(testkit.Events("str2 0"))
  2801  	result = tk.MustQuery("select * from t where a = case null when b then 'str3' when 10 then 'str1' else 'str2' end")
  2802  	result.Check(testkit.Events("str2 0"))
  2803  	tk.MustInterDirc("insert t values (null, 4)")
  2804  	result = tk.MustQuery("select * from t where b < case a when null then 0 when 'str2' then 0 else 9 end")
  2805  	result.Check(testkit.Events("<nil> 4"))
  2806  	result = tk.MustQuery("select * from t where b = case when a is null then 4 when  a = 'str5' then 7 else 9 end")
  2807  	result.Check(testkit.Events("<nil> 4"))
  2808  	// test warnings
  2809  	tk.MustQuery("select case when b=0 then 1 else 1/b end from t")
  2810  	tk.MustQuery("show warnings").Check(testkit.Events())
  2811  	tk.MustQuery("select if(b=0, 1, 1/b) from t")
  2812  	tk.MustQuery("show warnings").Check(testkit.Events())
  2813  	tk.MustQuery("select ifnull(b, b/0) from t")
  2814  	tk.MustQuery("show warnings").Check(testkit.Events())
  2815  
  2816  	tk.MustQuery("select case when 1 then 1 else 1/0 end")
  2817  	tk.MustQuery("show warnings").Check(testkit.Events())
  2818  	tk.MustQuery(" select if(1,1,1/0)")
  2819  	tk.MustQuery("show warnings").Check(testkit.Events())
  2820  	tk.MustQuery("select ifnull(1, 1/0)")
  2821  	tk.MustQuery("show warnings").Check(testkit.Events())
  2822  
  2823  	tk.MustInterDirc("delete from t")
  2824  	tk.MustInterDirc("insert t values ('str2', 0)")
  2825  	tk.MustQuery("select case when b < 1 then 1 else 1/0 end from t")
  2826  	tk.MustQuery("show warnings").Check(testkit.Events())
  2827  	tk.MustQuery("select case when b < 1 then 1 when 1/0 then b else 1/0 end from t")
  2828  	tk.MustQuery("show warnings").Check(testkit.Events())
  2829  	tk.MustQuery("select if(b < 1 , 1, 1/0) from t")
  2830  	tk.MustQuery("show warnings").Check(testkit.Events())
  2831  	tk.MustQuery("select ifnull(b, 1/0) from t")
  2832  	tk.MustQuery("show warnings").Check(testkit.Events())
  2833  	tk.MustQuery("select COALESCE(1, b, b/0) from t")
  2834  	tk.MustQuery("show warnings").Check(testkit.Events())
  2835  	tk.MustQuery("select 0 and b/0 from t")
  2836  	tk.MustQuery("show warnings").Check(testkit.Events())
  2837  	tk.MustQuery("select 1 or b/0 from t")
  2838  	tk.MustQuery("show warnings").Check(testkit.Events())
  2839  
  2840  	tk.MustQuery("select case 2.0 when 2.0 then 3.0 when 3.0 then 2.0 end").Check(testkit.Events("3.0"))
  2841  	tk.MustQuery("select case 2.0 when 3.0 then 2.0 when 4.0 then 3.0 else 5.0 end").Check(testkit.Events("5.0"))
  2842  	tk.MustQuery("select case cast('2011-01-01' as date) when cast('2011-01-01' as date) then cast('2011-02-02' as date) end").Check(testkit.Events("2011-02-02"))
  2843  	tk.MustQuery("select case cast('2012-01-01' as date) when cast('2011-01-01' as date) then cast('2011-02-02' as date) else cast('2011-03-03' as date) end").Check(testkit.Events("2011-03-03"))
  2844  	tk.MustQuery("select case cast('10:10:10' as time) when cast('10:10:10' as time) then cast('11:11:11' as time) end").Check(testkit.Events("11:11:11"))
  2845  	tk.MustQuery("select case cast('10:10:13' as time) when cast('10:10:10' as time) then cast('11:11:11' as time) else cast('22:22:22' as time) end").Check(testkit.Events("22:22:22"))
  2846  
  2847  	// for cast
  2848  	result = tk.MustQuery("select cast(1234 as char(3))")
  2849  	result.Check(testkit.Events("123"))
  2850  	result = tk.MustQuery("select cast(1234 as char(0))")
  2851  	result.Check(testkit.Events(""))
  2852  	result = tk.MustQuery("show warnings")
  2853  	result.Check(testkit.Events("Warning 1406 Data Too Long, field len 0, data len 4"))
  2854  	result = tk.MustQuery("select CAST( - 8 AS DECIMAL ) * + 52 + 87 < - 86")
  2855  	result.Check(testkit.Events("1"))
  2856  
  2857  	// for char
  2858  	result = tk.MustQuery("select char(97, 100, 256, 89)")
  2859  	result.Check(testkit.Events("ad\x01\x00Y"))
  2860  	result = tk.MustQuery("select char(97, null, 100, 256, 89)")
  2861  	result.Check(testkit.Events("ad\x01\x00Y"))
  2862  	result = tk.MustQuery("select char(97, null, 100, 256, 89 using utf8)")
  2863  	result.Check(testkit.Events("ad\x01\x00Y"))
  2864  	result = tk.MustQuery("select char(97, null, 100, 256, 89 using ascii)")
  2865  	result.Check(testkit.Events("ad\x01\x00Y"))
  2866  	err = tk.InterDircToErr("select char(97, null, 100, 256, 89 using milevadb)")
  2867  	c.Assert(err.Error(), Equals, "[BerolinaSQL:1115]Unknown character set: 'milevadb'")
  2868  
  2869  	// issue 3884
  2870  	tk.MustInterDirc("drop causet if exists t")
  2871  	tk.MustInterDirc("CREATE TABLE t (c1 date, c2 datetime, c3 timestamp, c4 time, c5 year);")
  2872  	tk.MustInterDirc("INSERT INTO t values ('2000-01-01', '2000-01-01 12:12:12', '2000-01-01 12:12:12', '12:12:12', '2000');")
  2873  	tk.MustInterDirc("INSERT INTO t values ('2000-02-01', '2000-02-01 12:12:12', '2000-02-01 12:12:12', '13:12:12', 2000);")
  2874  	tk.MustInterDirc("INSERT INTO t values ('2000-03-01', '2000-03-01', '2000-03-01 12:12:12', '1 12:12:12', 2000);")
  2875  	tk.MustInterDirc("INSERT INTO t SET c1 = '2000-04-01', c2 = '2000-04-01', c3 = '2000-04-01 12:12:12', c4 = '-1 13:12:12', c5 = 2000;")
  2876  	result = tk.MustQuery("SELECT c4 FROM t where c4 < '-13:12:12';")
  2877  	result.Check(testkit.Events("-37:12:12"))
  2878  	result = tk.MustQuery(`SELECT 1 DIV - - 28 + ( - SUM( - + 25 ) ) * - CASE - 18 WHEN 44 THEN NULL ELSE - 41 + 32 + + - 70 - + COUNT( - 95 ) * 15 END + 92`)
  2879  	result.Check(testkit.Events("2442"))
  2880  
  2881  	// for regexp, rlike
  2882  	// https://github.com/whtcorpsinc/milevadb/issues/4080
  2883  	tk.MustInterDirc(`drop causet if exists t;`)
  2884  	tk.MustInterDirc(`create causet t (a char(10), b varchar(10), c binary(10), d varbinary(10));`)
  2885  	tk.MustInterDirc(`insert into t values ('text','text','text','text');`)
  2886  	result = tk.MustQuery(`select a regexp 'xt' from t;`)
  2887  	result.Check(testkit.Events("1"))
  2888  	result = tk.MustQuery(`select b regexp 'xt' from t;`)
  2889  	result.Check(testkit.Events("1"))
  2890  	result = tk.MustQuery(`select b regexp binary 'Xt' from t;`)
  2891  	result.Check(testkit.Events("0"))
  2892  	result = tk.MustQuery(`select c regexp 'Xt' from t;`)
  2893  	result.Check(testkit.Events("0"))
  2894  	result = tk.MustQuery(`select d regexp 'Xt' from t;`)
  2895  	result.Check(testkit.Events("0"))
  2896  	result = tk.MustQuery(`select a rlike 'xt' from t;`)
  2897  	result.Check(testkit.Events("1"))
  2898  	result = tk.MustQuery(`select a rlike binary 'Xt' from t;`)
  2899  	result.Check(testkit.Events("0"))
  2900  	result = tk.MustQuery(`select b rlike 'xt' from t;`)
  2901  	result.Check(testkit.Events("1"))
  2902  	result = tk.MustQuery(`select c rlike 'Xt' from t;`)
  2903  	result.Check(testkit.Events("0"))
  2904  	result = tk.MustQuery(`select d rlike 'Xt' from t;`)
  2905  	result.Check(testkit.Events("0"))
  2906  	result = tk.MustQuery(`select 'a' regexp 'A', 'a' regexp binary 'A'`)
  2907  	result.Check(testkit.Events("0 0"))
  2908  
  2909  	// testCase is for like and regexp
  2910  	type testCase struct {
  2911  		pattern string
  2912  		val     string
  2913  		result  int
  2914  	}
  2915  	patternMatching := func(c *C, tk *testkit.TestKit, queryOp string, data []testCase) {
  2916  		tk.MustInterDirc("drop causet if exists t")
  2917  		tk.MustInterDirc("create causet t (a varchar(255), b int)")
  2918  		for i, d := range data {
  2919  			tk.MustInterDirc(fmt.Sprintf("insert into t values('%s', %d)", d.val, i))
  2920  			result = tk.MustQuery(fmt.Sprintf("select * from t where a %s '%s'", queryOp, d.pattern))
  2921  			if d.result == 1 {
  2922  				rowStr := fmt.Sprintf("%s %d", d.val, i)
  2923  				result.Check(testkit.Events(rowStr))
  2924  			} else {
  2925  				result.Check(nil)
  2926  			}
  2927  			tk.MustInterDirc(fmt.Sprintf("delete from t where b = %d", i))
  2928  		}
  2929  	}
  2930  	// for like
  2931  	likeTests := []testCase{
  2932  		{"a", "a", 1},
  2933  		{"a", "b", 0},
  2934  		{"aA", "Aa", 0},
  2935  		{`aA%`, "aAab", 1},
  2936  		{"aA_", "Aaab", 0},
  2937  		{"Aa_", "Aab", 1},
  2938  		{"", "", 1},
  2939  		{"", "a", 0},
  2940  	}
  2941  	patternMatching(c, tk, "like", likeTests)
  2942  	// for regexp
  2943  	likeTests = []testCase{
  2944  		{"^$", "a", 0},
  2945  		{"a", "a", 1},
  2946  		{"a", "b", 0},
  2947  		{"aA", "aA", 1},
  2948  		{".", "a", 1},
  2949  		{"^.$", "ab", 0},
  2950  		{"..", "b", 0},
  2951  		{".ab", "aab", 1},
  2952  		{"ab.", "abcd", 1},
  2953  		{".*", "abcd", 1},
  2954  	}
  2955  	patternMatching(c, tk, "regexp", likeTests)
  2956  
  2957  	// for #9838
  2958  	result = tk.MustQuery("select cast(1 as signed) + cast(9223372036854775807 as unsigned);")
  2959  	result.Check(testkit.Events("9223372036854775808"))
  2960  	result = tk.MustQuery("select cast(9223372036854775807 as unsigned) + cast(1 as signed);")
  2961  	result.Check(testkit.Events("9223372036854775808"))
  2962  	err = tk.QueryToErr("select cast(9223372036854775807 as signed) + cast(9223372036854775809 as unsigned);")
  2963  	c.Assert(err, NotNil)
  2964  	err = tk.QueryToErr("select cast(9223372036854775809 as unsigned) + cast(9223372036854775807 as signed);")
  2965  	c.Assert(err, NotNil)
  2966  	err = tk.QueryToErr("select cast(-9223372036854775807 as signed) + cast(9223372036854775806 as unsigned);")
  2967  	c.Assert(err, NotNil)
  2968  	err = tk.QueryToErr("select cast(9223372036854775806 as unsigned) + cast(-9223372036854775807 as signed);")
  2969  	c.Assert(err, NotNil)
  2970  
  2971  	result = tk.MustQuery(`select 1 / '2007' div 1;`)
  2972  	result.Check(testkit.Events("0"))
  2973  }
  2974  
  2975  func (s *testIntegrationSuite) TestInfoBuiltin(c *C) {
  2976  	defer s.cleanEnv(c)
  2977  	tk := testkit.NewTestKit(c, s.causetstore)
  2978  	tk.MustInterDirc("use test")
  2979  
  2980  	// for last_insert_id
  2981  	tk.MustInterDirc("drop causet if exists t")
  2982  	tk.MustInterDirc("create causet t (id int auto_increment, a int, PRIMARY KEY (id))")
  2983  	tk.MustInterDirc("insert into t(a) values(1)")
  2984  	result := tk.MustQuery("select last_insert_id();")
  2985  	result.Check(testkit.Events("1"))
  2986  	tk.MustInterDirc("insert into t values(2, 1)")
  2987  	result = tk.MustQuery("select last_insert_id();")
  2988  	result.Check(testkit.Events("1"))
  2989  	tk.MustInterDirc("insert into t(a) values(1)")
  2990  	result = tk.MustQuery("select last_insert_id();")
  2991  	result.Check(testkit.Events("3"))
  2992  
  2993  	result = tk.MustQuery("select last_insert_id(5);")
  2994  	result.Check(testkit.Events("5"))
  2995  	result = tk.MustQuery("select last_insert_id();")
  2996  	result.Check(testkit.Events("5"))
  2997  
  2998  	// for found_rows
  2999  	tk.MustInterDirc("drop causet if exists t")
  3000  	tk.MustInterDirc("create causet t (a int)")
  3001  	tk.MustQuery("select * from t") // Test XSelectBlockInterDirc
  3002  	result = tk.MustQuery("select found_rows()")
  3003  	result.Check(testkit.Events("0"))
  3004  	result = tk.MustQuery("select found_rows()")
  3005  	result.Check(testkit.Events("1")) // Last query is found_rows(), it returns 1 event with value 0
  3006  	tk.MustInterDirc("insert t values (1),(2),(2)")
  3007  	tk.MustQuery("select * from t")
  3008  	result = tk.MustQuery("select found_rows()")
  3009  	result.Check(testkit.Events("3"))
  3010  	tk.MustQuery("select * from t where a = 0")
  3011  	result = tk.MustQuery("select found_rows()")
  3012  	result.Check(testkit.Events("0"))
  3013  	tk.MustQuery("select * from t where a = 1")
  3014  	result = tk.MustQuery("select found_rows()")
  3015  	result.Check(testkit.Events("1"))
  3016  	tk.MustQuery("select * from t where a like '2'") // Test SelectionInterDirc
  3017  	result = tk.MustQuery("select found_rows()")
  3018  	result.Check(testkit.Events("2"))
  3019  	tk.MustQuery("show blocks like 't'")
  3020  	result = tk.MustQuery("select found_rows()")
  3021  	result.Check(testkit.Events("1"))
  3022  	tk.MustQuery("select count(*) from t") // Test ProjectionInterDirc
  3023  	result = tk.MustQuery("select found_rows()")
  3024  	result.Check(testkit.Events("1"))
  3025  
  3026  	// for database
  3027  	result = tk.MustQuery("select database()")
  3028  	result.Check(testkit.Events("test"))
  3029  	tk.MustInterDirc("drop database test")
  3030  	result = tk.MustQuery("select database()")
  3031  	result.Check(testkit.Events("<nil>"))
  3032  	tk.MustInterDirc("create database test")
  3033  	tk.MustInterDirc("use test")
  3034  
  3035  	// for current_user
  3036  	stochastikVars := tk.Se.GetStochastikVars()
  3037  	originUser := stochastikVars.User
  3038  	stochastikVars.User = &auth.UserIdentity{Username: "root", Hostname: "localhost", AuthUsername: "root", AuthHostname: "127.0.%%"}
  3039  	result = tk.MustQuery("select current_user()")
  3040  	result.Check(testkit.Events("root@127.0.%%"))
  3041  	stochastikVars.User = originUser
  3042  
  3043  	// for user
  3044  	stochastikVars.User = &auth.UserIdentity{Username: "root", Hostname: "localhost", AuthUsername: "root", AuthHostname: "127.0.%%"}
  3045  	result = tk.MustQuery("select user()")
  3046  	result.Check(testkit.Events("root@localhost"))
  3047  	stochastikVars.User = originUser
  3048  
  3049  	// for connection_id
  3050  	originConnectionID := stochastikVars.ConnectionID
  3051  	stochastikVars.ConnectionID = uint64(1)
  3052  	result = tk.MustQuery("select connection_id()")
  3053  	result.Check(testkit.Events("1"))
  3054  	stochastikVars.ConnectionID = originConnectionID
  3055  
  3056  	// for version
  3057  	result = tk.MustQuery("select version()")
  3058  	result.Check(testkit.Events(allegrosql.ServerVersion))
  3059  
  3060  	// for row_count
  3061  	tk.MustInterDirc("drop causet if exists t")
  3062  	tk.MustInterDirc("create causet t (a int, b int, PRIMARY KEY (a))")
  3063  	result = tk.MustQuery("select row_count();")
  3064  	result.Check(testkit.Events("0"))
  3065  	tk.MustInterDirc("insert into t(a, b) values(1, 11), (2, 22), (3, 33)")
  3066  	result = tk.MustQuery("select row_count();")
  3067  	result.Check(testkit.Events("3"))
  3068  	tk.MustInterDirc("select * from t")
  3069  	result = tk.MustQuery("select row_count();")
  3070  	result.Check(testkit.Events("-1"))
  3071  	tk.MustInterDirc("uFIDelate t set b=22 where a=1")
  3072  	result = tk.MustQuery("select row_count();")
  3073  	result.Check(testkit.Events("1"))
  3074  	tk.MustInterDirc("uFIDelate t set b=22 where a=1")
  3075  	result = tk.MustQuery("select row_count();")
  3076  	result.Check(testkit.Events("0"))
  3077  	tk.MustInterDirc("delete from t where a=2")
  3078  	result = tk.MustQuery("select row_count();")
  3079  	result.Check(testkit.Events("1"))
  3080  	result = tk.MustQuery("select row_count();")
  3081  	result.Check(testkit.Events("-1"))
  3082  
  3083  	// for benchmark
  3084  	success := testkit.Events("0")
  3085  	tk.MustInterDirc("drop causet if exists t")
  3086  	tk.MustInterDirc("create causet t (a int, b int)")
  3087  	result = tk.MustQuery(`select benchmark(3, benchmark(2, length("abc")))`)
  3088  	result.Check(success)
  3089  	err := tk.InterDircToErr(`select benchmark(3, length("a", "b"))`)
  3090  	c.Assert(err, NotNil)
  3091  	// Quoted from https://dev.allegrosql.com/doc/refman/5.7/en/information-functions.html#function_benchmark
  3092  	// Although the memex can be a subquery, it must return a single defCausumn and at most a single event.
  3093  	// For example, BENCHMARK(10, (SELECT * FROM t)) will fail if the causet t has more than one defCausumn or
  3094  	// more than one event.
  3095  	oneDeferredCausetQuery := "select benchmark(10, (select a from t))"
  3096  	twoDeferredCausetQuery := "select benchmark(10, (select * from t))"
  3097  	// rows * defCausumns:
  3098  	// 0 * 1, success;
  3099  	result = tk.MustQuery(oneDeferredCausetQuery)
  3100  	result.Check(success)
  3101  	// 0 * 2, error;
  3102  	err = tk.InterDircToErr(twoDeferredCausetQuery)
  3103  	c.Assert(err, NotNil)
  3104  	// 1 * 1, success;
  3105  	tk.MustInterDirc("insert t values (1, 2)")
  3106  	result = tk.MustQuery(oneDeferredCausetQuery)
  3107  	result.Check(success)
  3108  	// 1 * 2, error;
  3109  	err = tk.InterDircToErr(twoDeferredCausetQuery)
  3110  	c.Assert(err, NotNil)
  3111  	// 2 * 1, error;
  3112  	tk.MustInterDirc("insert t values (3, 4)")
  3113  	err = tk.InterDircToErr(oneDeferredCausetQuery)
  3114  	c.Assert(err, NotNil)
  3115  	// 2 * 2, error.
  3116  	err = tk.InterDircToErr(twoDeferredCausetQuery)
  3117  	c.Assert(err, NotNil)
  3118  }
  3119  
  3120  func (s *testIntegrationSuite) TestControlBuiltin(c *C) {
  3121  	defer s.cleanEnv(c)
  3122  	tk := testkit.NewTestKit(c, s.causetstore)
  3123  	tk.MustInterDirc("use test")
  3124  
  3125  	// for ifnull
  3126  	result := tk.MustQuery("select ifnull(1, 2)")
  3127  	result.Check(testkit.Events("1"))
  3128  	result = tk.MustQuery("select ifnull(null, 2)")
  3129  	result.Check(testkit.Events("2"))
  3130  	result = tk.MustQuery("select ifnull(1, null)")
  3131  	result.Check(testkit.Events("1"))
  3132  	result = tk.MustQuery("select ifnull(null, null)")
  3133  	result.Check(testkit.Events("<nil>"))
  3134  
  3135  	tk.MustInterDirc("drop causet if exists t1")
  3136  	tk.MustInterDirc("create causet t1(a bigint not null)")
  3137  	result = tk.MustQuery("select ifnull(max(a),0) from t1")
  3138  	result.Check(testkit.Events("0"))
  3139  
  3140  	tk.MustInterDirc("drop causet if exists t1")
  3141  	tk.MustInterDirc("drop causet if exists t2")
  3142  	tk.MustInterDirc("create causet t1(a decimal(20,4))")
  3143  	tk.MustInterDirc("create causet t2(a decimal(20,4))")
  3144  	tk.MustInterDirc("insert into t1 select 1.2345")
  3145  	tk.MustInterDirc("insert into t2 select 1.2345")
  3146  
  3147  	result = tk.MustQuery(`select sum(ifnull(a, 0)) from (
  3148  	select ifnull(a, 0) as a from t1
  3149  	union all
  3150  	select ifnull(a, 0) as a from t2
  3151  	) t;`)
  3152  	result.Check(testkit.Events("2.4690"))
  3153  
  3154  	// for if
  3155  	result = tk.MustQuery(`select IF(0,"ERROR","this"),IF(1,"is","ERROR"),IF(NULL,"ERROR","a"),IF(1,2,3)|0,IF(1,2.0,3.0)+0;`)
  3156  	result.Check(testkit.Events("this is a 2 2.0"))
  3157  	tk.MustInterDirc("drop causet if exists t1;")
  3158  	tk.MustInterDirc("CREATE TABLE t1 (st varchar(255) NOT NULL, u int(11) NOT NULL);")
  3159  	tk.MustInterDirc("INSERT INTO t1 VALUES ('a',1),('A',1),('aa',1),('AA',1),('a',1),('aaa',0),('BBB',0);")
  3160  	result = tk.MustQuery("select if(1,st,st) s from t1 order by s;")
  3161  	result.Check(testkit.Events("A", "AA", "BBB", "a", "a", "aa", "aaa"))
  3162  	result = tk.MustQuery("select if(u=1,st,st) s from t1 order by s;")
  3163  	result.Check(testkit.Events("A", "AA", "BBB", "a", "a", "aa", "aaa"))
  3164  	tk.MustInterDirc("drop causet if exists t1;")
  3165  	tk.MustInterDirc("CREATE TABLE t1 (a varchar(255), b time, c int)")
  3166  	tk.MustInterDirc("INSERT INTO t1 VALUE('abc', '12:00:00', 0)")
  3167  	tk.MustInterDirc("INSERT INTO t1 VALUE('1abc', '00:00:00', 1)")
  3168  	tk.MustInterDirc("INSERT INTO t1 VALUE('0abc', '12:59:59', 0)")
  3169  	result = tk.MustQuery("select if(a, b, c), if(b, a, c), if(c, a, b) from t1")
  3170  	result.Check(testkit.Events("0 abc 12:00:00", "00:00:00 1 1abc", "0 0abc 12:59:59"))
  3171  	result = tk.MustQuery("select if(1, 1.0, 1)")
  3172  	result.Check(testkit.Events("1.0"))
  3173  	// FIXME: MyALLEGROSQL returns `1.0`.
  3174  	result = tk.MustQuery("select if(1, 1, 1.0)")
  3175  	result.Check(testkit.Events("1"))
  3176  	tk.MustQuery("select if(count(*), cast('2000-01-01' as date), cast('2011-01-01' as date)) from t1").Check(testkit.Events("2000-01-01"))
  3177  	tk.MustQuery("select if(count(*)=0, cast('2000-01-01' as date), cast('2011-01-01' as date)) from t1").Check(testkit.Events("2011-01-01"))
  3178  	tk.MustQuery("select if(count(*), cast('[]' as json), cast('{}' as json)) from t1").Check(testkit.Events("[]"))
  3179  	tk.MustQuery("select if(count(*)=0, cast('[]' as json), cast('{}' as json)) from t1").Check(testkit.Events("{}"))
  3180  
  3181  	result = tk.MustQuery("SELECT 79 + + + CASE -87 WHEN -30 THEN COALESCE(COUNT(*), +COALESCE(+15, -33, -12 ) + +72) WHEN +COALESCE(+AVG(DISTINCT(60)), 21) THEN NULL ELSE NULL END AS defCaus0;")
  3182  	result.Check(testkit.Events("<nil>"))
  3183  
  3184  	result = tk.MustQuery("SELECT -63 + COALESCE ( - 83, - 61 + - + 72 * - CAST( NULL AS SIGNED ) + + 3 );")
  3185  	result.Check(testkit.Events("-146"))
  3186  }
  3187  
  3188  func (s *testIntegrationSuite) TestArithmeticBuiltin(c *C) {
  3189  	defer s.cleanEnv(c)
  3190  	tk := testkit.NewTestKit(c, s.causetstore)
  3191  	tk.MustInterDirc("use test")
  3192  	ctx := context.Background()
  3193  
  3194  	// for plus
  3195  	tk.MustInterDirc("DROP TABLE IF EXISTS t;")
  3196  	tk.MustInterDirc("CREATE TABLE t(a DECIMAL(4, 2), b DECIMAL(5, 3));")
  3197  	tk.MustInterDirc("INSERT INTO t(a, b) VALUES(1.09, 1.999), (-1.1, -0.1);")
  3198  	result := tk.MustQuery("SELECT a+b FROM t;")
  3199  	result.Check(testkit.Events("3.089", "-1.200"))
  3200  	result = tk.MustQuery("SELECT b+12, b+0.01, b+0.00001, b+12.00001 FROM t;")
  3201  	result.Check(testkit.Events("13.999 2.009 1.99901 13.99901", "11.900 -0.090 -0.09999 11.90001"))
  3202  	result = tk.MustQuery("SELECT 1+12, 21+0.01, 89+\"11\", 12+\"a\", 12+NULL, NULL+1, NULL+NULL;")
  3203  	result.Check(testkit.Events("13 21.01 100 12 <nil> <nil> <nil>"))
  3204  	tk.MustInterDirc("DROP TABLE IF EXISTS t;")
  3205  	tk.MustInterDirc("CREATE TABLE t(a BIGINT UNSIGNED, b BIGINT UNSIGNED);")
  3206  	tk.MustInterDirc("INSERT INTO t SELECT 1<<63, 1<<63;")
  3207  	rs, err := tk.InterDirc("SELECT a+b FROM t;")
  3208  	c.Assert(errors.ErrorStack(err), Equals, "")
  3209  	c.Assert(rs, NotNil)
  3210  	rows, err := stochastik.GetEvents4Test(ctx, tk.Se, rs)
  3211  	c.Assert(rows, IsNil)
  3212  	c.Assert(err, NotNil)
  3213  	c.Assert(err.Error(), Equals, "[types:1690]BIGINT UNSIGNED value is out of range in '(test.t.a + test.t.b)'")
  3214  	c.Assert(rs.Close(), IsNil)
  3215  	rs, err = tk.InterDirc("select cast(-3 as signed) + cast(2 as unsigned);")
  3216  	c.Assert(errors.ErrorStack(err), Equals, "")
  3217  	c.Assert(rs, NotNil)
  3218  	rows, err = stochastik.GetEvents4Test(ctx, tk.Se, rs)
  3219  	c.Assert(rows, IsNil)
  3220  	c.Assert(err, NotNil)
  3221  	c.Assert(err.Error(), Equals, "[types:1690]BIGINT UNSIGNED value is out of range in '(-3 + 2)'")
  3222  	c.Assert(rs.Close(), IsNil)
  3223  	rs, err = tk.InterDirc("select cast(2 as unsigned) + cast(-3 as signed);")
  3224  	c.Assert(errors.ErrorStack(err), Equals, "")
  3225  	c.Assert(rs, NotNil)
  3226  	rows, err = stochastik.GetEvents4Test(ctx, tk.Se, rs)
  3227  	c.Assert(rows, IsNil)
  3228  	c.Assert(err, NotNil)
  3229  	c.Assert(err.Error(), Equals, "[types:1690]BIGINT UNSIGNED value is out of range in '(2 + -3)'")
  3230  	c.Assert(rs.Close(), IsNil)
  3231  
  3232  	// for minus
  3233  	tk.MustInterDirc("DROP TABLE IF EXISTS t;")
  3234  	tk.MustInterDirc("CREATE TABLE t(a DECIMAL(4, 2), b DECIMAL(5, 3));")
  3235  	tk.MustInterDirc("INSERT INTO t(a, b) VALUES(1.09, 1.999), (-1.1, -0.1);")
  3236  	result = tk.MustQuery("SELECT a-b FROM t;")
  3237  	result.Check(testkit.Events("-0.909", "-1.000"))
  3238  	result = tk.MustQuery("SELECT b-12, b-0.01, b-0.00001, b-12.00001 FROM t;")
  3239  	result.Check(testkit.Events("-10.001 1.989 1.99899 -10.00101", "-12.100 -0.110 -0.10001 -12.10001"))
  3240  	result = tk.MustQuery("SELECT 1-12, 21-0.01, 89-\"11\", 12-\"a\", 12-NULL, NULL-1, NULL-NULL;")
  3241  	result.Check(testkit.Events("-11 20.99 78 12 <nil> <nil> <nil>"))
  3242  	tk.MustInterDirc("DROP TABLE IF EXISTS t;")
  3243  	tk.MustInterDirc("CREATE TABLE t(a BIGINT UNSIGNED, b BIGINT UNSIGNED);")
  3244  	tk.MustInterDirc("INSERT INTO t SELECT 1, 4;")
  3245  	rs, err = tk.InterDirc("SELECT a-b FROM t;")
  3246  	c.Assert(errors.ErrorStack(err), Equals, "")
  3247  	c.Assert(rs, NotNil)
  3248  	rows, err = stochastik.GetEvents4Test(ctx, tk.Se, rs)
  3249  	c.Assert(rows, IsNil)
  3250  	c.Assert(err, NotNil)
  3251  	c.Assert(err.Error(), Equals, "[types:1690]BIGINT UNSIGNED value is out of range in '(test.t.a - test.t.b)'")
  3252  	c.Assert(rs.Close(), IsNil)
  3253  	rs, err = tk.InterDirc("select cast(-1 as signed) - cast(-1 as unsigned);")
  3254  	c.Assert(errors.ErrorStack(err), Equals, "")
  3255  	c.Assert(rs, NotNil)
  3256  	rows, err = stochastik.GetEvents4Test(ctx, tk.Se, rs)
  3257  	c.Assert(rows, IsNil)
  3258  	c.Assert(err, NotNil)
  3259  	c.Assert(err.Error(), Equals, "[types:1690]BIGINT UNSIGNED value is out of range in '(-1 - 18446744073709551615)'")
  3260  	c.Assert(rs.Close(), IsNil)
  3261  	rs, err = tk.InterDirc("select cast(-1 as unsigned) - cast(-1 as signed);")
  3262  	c.Assert(errors.ErrorStack(err), Equals, "")
  3263  	c.Assert(rs, NotNil)
  3264  	rows, err = stochastik.GetEvents4Test(ctx, tk.Se, rs)
  3265  	c.Assert(rows, IsNil)
  3266  	c.Assert(err, NotNil)
  3267  	c.Assert(err.Error(), Equals, "[types:1690]BIGINT UNSIGNED value is out of range in '(18446744073709551615 - -1)'")
  3268  	c.Assert(rs.Close(), IsNil)
  3269  	tk.MustQuery(`select cast(-3 as unsigned) - cast(-1 as signed);`).Check(testkit.Events("18446744073709551614"))
  3270  	tk.MustQuery("select 1.11 - 1.11;").Check(testkit.Events("0.00"))
  3271  	tk.MustInterDirc(`create causet tb5(a int(10));`)
  3272  	tk.MustInterDirc(`insert into tb5 (a) values (10);`)
  3273  	e := tk.QueryToErr(`select * from tb5 where a - -9223372036854775808;`)
  3274  	c.Assert(e, NotNil)
  3275  	c.Assert(strings.HasSuffix(e.Error(), `BIGINT value is out of range in '(DeferredCauset#0 - -9223372036854775808)'`), IsTrue, Commentf("err: %v", err))
  3276  	tk.MustInterDirc(`drop causet tb5`)
  3277  
  3278  	// for multiply
  3279  	tk.MustQuery("select 1234567890 * 1234567890").Check(testkit.Events("1524157875019052100"))
  3280  	rs, err = tk.InterDirc("select 1234567890 * 12345671890")
  3281  	c.Assert(err, IsNil)
  3282  	_, err = stochastik.GetEvents4Test(ctx, tk.Se, rs)
  3283  	c.Assert(terror.ErrorEqual(err, types.ErrOverflow), IsTrue)
  3284  	c.Assert(rs.Close(), IsNil)
  3285  	tk.MustQuery("select cast(1234567890 as unsigned int) * 12345671890").Check(testkit.Events("15241570095869612100"))
  3286  	tk.MustQuery("select 123344532434234234267890.0 * 1234567118923479823749823749.230").Check(testkit.Events("152277104042296270209916846800130443726237424001224.7000"))
  3287  	rs, err = tk.InterDirc("select 123344532434234234267890.0 * 12345671189234798237498232384982309489238402830480239849238048239084749.230")
  3288  	c.Assert(err, IsNil)
  3289  	_, err = stochastik.GetEvents4Test(ctx, tk.Se, rs)
  3290  	c.Assert(terror.ErrorEqual(err, types.ErrOverflow), IsTrue)
  3291  	c.Assert(rs.Close(), IsNil)
  3292  	// FIXME: There is something wrong in showing float number.
  3293  	//tk.MustQuery("select 1.797693134862315708145274237317043567981e+308 * 1").Check(testkit.Events("1.7976931348623157e308"))
  3294  	//tk.MustQuery("select 1.797693134862315708145274237317043567981e+308 * -1").Check(testkit.Events("-1.7976931348623157e308"))
  3295  	rs, err = tk.InterDirc("select 1.797693134862315708145274237317043567981e+308 * 1.1")
  3296  	c.Assert(err, IsNil)
  3297  	_, err = stochastik.GetEvents4Test(ctx, tk.Se, rs)
  3298  	c.Assert(terror.ErrorEqual(err, types.ErrOverflow), IsTrue)
  3299  	c.Assert(rs.Close(), IsNil)
  3300  	rs, err = tk.InterDirc("select 1.797693134862315708145274237317043567981e+308 * -1.1")
  3301  	c.Assert(err, IsNil)
  3302  	_, err = stochastik.GetEvents4Test(ctx, tk.Se, rs)
  3303  	c.Assert(terror.ErrorEqual(err, types.ErrOverflow), IsTrue)
  3304  	c.Assert(rs.Close(), IsNil)
  3305  	tk.MustQuery("select 0.0 * -1;").Check(testkit.Events("0.0"))
  3306  
  3307  	tk.MustInterDirc("DROP TABLE IF EXISTS t;")
  3308  	tk.MustInterDirc("CREATE TABLE t(a DECIMAL(4, 2), b DECIMAL(5, 3));")
  3309  	tk.MustInterDirc("INSERT INTO t(a, b) VALUES(-1.09, 1.999);")
  3310  	result = tk.MustQuery("SELECT a/b, a/12, a/-0.01, b/12, b/-0.01, b/0.000, NULL/b, b/NULL, NULL/NULL FROM t;")
  3311  	result.Check(testkit.Events("-0.545273 -0.090833 109.000000 0.1665833 -199.9000000 <nil> <nil> <nil> <nil>"))
  3312  	tk.MustQuery("show warnings;").Check(testkit.Events("Warning 1365 Division by 0"))
  3313  	rs, err = tk.InterDirc("select 1e200/1e-200")
  3314  	c.Assert(err, IsNil)
  3315  	_, err = stochastik.GetEvents4Test(ctx, tk.Se, rs)
  3316  	c.Assert(terror.ErrorEqual(err, types.ErrOverflow), IsTrue)
  3317  	c.Assert(rs.Close(), IsNil)
  3318  
  3319  	// for intDiv
  3320  	result = tk.MustQuery("SELECT 13 DIV 12, 13 DIV 0.01, -13 DIV 2, 13 DIV NULL, NULL DIV 13, NULL DIV NULL;")
  3321  	result.Check(testkit.Events("1 1300 -6 <nil> <nil> <nil>"))
  3322  	result = tk.MustQuery("SELECT 2.4 div 1.1, 2.4 div 1.2, 2.4 div 1.3;")
  3323  	result.Check(testkit.Events("2 2 1"))
  3324  	result = tk.MustQuery("SELECT 1.175494351E-37 div 1.7976931348623157E+308, 1.7976931348623157E+308 div -1.7976931348623157E+307, 1 div 1e-82;")
  3325  	result.Check(testkit.Events("0 -1 <nil>"))
  3326  	tk.MustQuery("show warnings").Check(solitonutil.EventsWithSep("|",
  3327  		"Warning|1292|Truncated incorrect DECIMAL value: '1.7976931348623157e+308'",
  3328  		"Warning|1292|Truncated incorrect DECIMAL value: '1.7976931348623157e+308'",
  3329  		"Warning|1292|Truncated incorrect DECIMAL value: '-1.7976931348623158e+307'",
  3330  		"Warning|1365|Division by 0"))
  3331  	rs, err = tk.InterDirc("select 1e300 DIV 1.5")
  3332  	c.Assert(err, IsNil)
  3333  	_, err = stochastik.GetEvents4Test(ctx, tk.Se, rs)
  3334  	c.Assert(terror.ErrorEqual(err, types.ErrOverflow), IsTrue)
  3335  	c.Assert(rs.Close(), IsNil)
  3336  
  3337  	tk.MustInterDirc("drop causet if exists t;")
  3338  	tk.MustInterDirc("CREATE TABLE t (c_varchar varchar(255), c_time time, nonzero int, zero int, c_int_unsigned int unsigned, c_timestamp timestamp, c_enum enum('a','b','c'));")
  3339  	tk.MustInterDirc("INSERT INTO t VALUE('abc', '12:00:00', 12, 0, 5, '2020-08-05 18:19:03', 'b');")
  3340  	result = tk.MustQuery("select c_varchar div nonzero, c_time div nonzero, c_time div zero, c_timestamp div nonzero, c_timestamp div zero, c_varchar div zero from t;")
  3341  	result.Check(testkit.Events("0 10000 <nil> 1680900431825 <nil> <nil>"))
  3342  	result = tk.MustQuery("select c_enum div nonzero from t;")
  3343  	result.Check(testkit.Events("0"))
  3344  	tk.MustQuery("select c_enum div zero from t").Check(testkit.Events("<nil>"))
  3345  	tk.MustQuery("select nonzero div zero from t").Check(testkit.Events("<nil>"))
  3346  	tk.MustQuery("show warnings;").Check(testkit.Events("Warning 1365 Division by 0"))
  3347  	result = tk.MustQuery("select c_time div c_enum, c_timestamp div c_time, c_timestamp div c_enum from t;")
  3348  	result.Check(testkit.Events("60000 168090043 10085402590951"))
  3349  	result = tk.MustQuery("select c_int_unsigned div nonzero, nonzero div c_int_unsigned, c_int_unsigned div zero from t;")
  3350  	result.Check(testkit.Events("0 2 <nil>"))
  3351  	tk.MustQuery("show warnings;").Check(testkit.Events("Warning 1365 Division by 0"))
  3352  
  3353  	// for mod
  3354  	result = tk.MustQuery("SELECT CAST(1 AS UNSIGNED) MOD -9223372036854775808, -9223372036854775808 MOD CAST(1 AS UNSIGNED);")
  3355  	result.Check(testkit.Events("1 0"))
  3356  	result = tk.MustQuery("SELECT 13 MOD 12, 13 MOD 0.01, -13 MOD 2, 13 MOD NULL, NULL MOD 13, NULL DIV NULL;")
  3357  	result.Check(testkit.Events("1 0.00 -1 <nil> <nil> <nil>"))
  3358  	result = tk.MustQuery("SELECT 2.4 MOD 1.1, 2.4 MOD 1.2, 2.4 mod 1.30;")
  3359  	result.Check(testkit.Events("0.2 0.0 1.10"))
  3360  	tk.MustInterDirc("drop causet if exists t;")
  3361  	tk.MustInterDirc("CREATE TABLE t (c_varchar varchar(255), c_time time, nonzero int, zero int, c_timestamp timestamp, c_enum enum('a','b','c'));")
  3362  	tk.MustInterDirc("INSERT INTO t VALUE('abc', '12:00:00', 12, 0, '2020-08-05 18:19:03', 'b');")
  3363  	result = tk.MustQuery("select c_varchar MOD nonzero, c_time MOD nonzero, c_timestamp MOD nonzero, c_enum MOD nonzero from t;")
  3364  	result.Check(testkit.Events("0 0 3 2"))
  3365  	result = tk.MustQuery("select c_time MOD c_enum, c_timestamp MOD c_time, c_timestamp MOD c_enum from t;")
  3366  	result.Check(testkit.Events("0 21903 1"))
  3367  	tk.MustQuery("select c_enum MOD zero from t;").Check(testkit.Events("<nil>"))
  3368  	tk.MustQuery("show warnings;").Check(testkit.Events("Warning 1365 Division by 0"))
  3369  	tk.MustInterDirc("SET ALLEGROSQL_MODE='ERROR_FOR_DIVISION_BY_ZERO,STRICT_ALL_TABLES';")
  3370  	tk.MustInterDirc("drop causet if exists t;")
  3371  	tk.MustInterDirc("CREATE TABLE t (v int);")
  3372  	tk.MustInterDirc("INSERT IGNORE INTO t VALUE(12 MOD 0);")
  3373  	tk.MustQuery("show warnings;").Check(testkit.Events("Warning 1365 Division by 0"))
  3374  	tk.MustQuery("select v from t;").Check(testkit.Events("<nil>"))
  3375  	tk.MustQuery("select 0.000 % 0.11234500000000000000;").Check(testkit.Events("0.00000000000000000000"))
  3376  
  3377  	_, err = tk.InterDirc("INSERT INTO t VALUE(12 MOD 0);")
  3378  	c.Assert(terror.ErrorEqual(err, memex.ErrDivisionByZero), IsTrue)
  3379  
  3380  	tk.MustQuery("select sum(1.2e2) * 0.1").Check(testkit.Events("12"))
  3381  	tk.MustInterDirc("drop causet if exists t")
  3382  	tk.MustInterDirc("create causet t(a double)")
  3383  	tk.MustInterDirc("insert into t value(1.2)")
  3384  	tk.MustQuery("select sum(a) * 0.1 from t").Check(testkit.Events("0.12"))
  3385  
  3386  	tk.MustInterDirc("drop causet if exists t")
  3387  	tk.MustInterDirc("create causet t(a double)")
  3388  	tk.MustInterDirc("insert into t value(1.2)")
  3389  	result = tk.MustQuery("select * from t where a/0 > 1")
  3390  	result.Check(testkit.Events())
  3391  	tk.MustQuery("show warnings").Check(solitonutil.EventsWithSep("|", "Warning|1365|Division by 0"))
  3392  
  3393  	tk.MustInterDirc("USE test;")
  3394  	tk.MustInterDirc("DROP TABLE IF EXISTS t;")
  3395  	tk.MustInterDirc("CREATE TABLE t(a BIGINT, b DECIMAL(6, 2));")
  3396  	tk.MustInterDirc("INSERT INTO t VALUES(0, 1.12), (1, 1.21);")
  3397  	tk.MustQuery("SELECT a/b FROM t;").Check(testkit.Events("0.0000", "0.8264"))
  3398  }
  3399  
  3400  func (s *testIntegrationSuite) TestCompareBuiltin(c *C) {
  3401  	defer s.cleanEnv(c)
  3402  	tk := testkit.NewTestKit(c, s.causetstore)
  3403  	tk.MustInterDirc("use test")
  3404  
  3405  	// compare as JSON
  3406  	tk.MustInterDirc("drop causet if exists t")
  3407  	tk.MustInterDirc("CREATE TABLE t (pk int  NOT NULL PRIMARY KEY AUTO_INCREMENT, i INT, j JSON);")
  3408  	tk.MustInterDirc(`INSERT INTO t(i, j) VALUES (0, NULL)`)
  3409  	tk.MustInterDirc(`INSERT INTO t(i, j) VALUES (1, '{"a": 2}')`)
  3410  	tk.MustInterDirc(`INSERT INTO t(i, j) VALUES (2, '[1,2]')`)
  3411  	tk.MustInterDirc(`INSERT INTO t(i, j) VALUES (3, '{"a":"b", "c":"d","ab":"abc", "bc": ["x", "y"]}')`)
  3412  	tk.MustInterDirc(`INSERT INTO t(i, j) VALUES (4, '["here", ["I", "am"], "!!!"]')`)
  3413  	tk.MustInterDirc(`INSERT INTO t(i, j) VALUES (5, '"scalar string"')`)
  3414  	tk.MustInterDirc(`INSERT INTO t(i, j) VALUES (6, 'true')`)
  3415  	tk.MustInterDirc(`INSERT INTO t(i, j) VALUES (7, 'false')`)
  3416  	tk.MustInterDirc(`INSERT INTO t(i, j) VALUES (8, 'null')`)
  3417  	tk.MustInterDirc(`INSERT INTO t(i, j) VALUES (9, '-1')`)
  3418  	tk.MustInterDirc(`INSERT INTO t(i, j) VALUES (10, CAST(CAST(1 AS UNSIGNED) AS JSON))`)
  3419  	tk.MustInterDirc(`INSERT INTO t(i, j) VALUES (11, '32767')`)
  3420  	tk.MustInterDirc(`INSERT INTO t(i, j) VALUES (12, '32768')`)
  3421  	tk.MustInterDirc(`INSERT INTO t(i, j) VALUES (13, '-32768')`)
  3422  	tk.MustInterDirc(`INSERT INTO t(i, j) VALUES (14, '-32769')`)
  3423  	tk.MustInterDirc(`INSERT INTO t(i, j) VALUES (15, '2147483647')`)
  3424  	tk.MustInterDirc(`INSERT INTO t(i, j) VALUES (16, '2147483648')`)
  3425  	tk.MustInterDirc(`INSERT INTO t(i, j) VALUES (17, '-2147483648')`)
  3426  	tk.MustInterDirc(`INSERT INTO t(i, j) VALUES (18, '-2147483649')`)
  3427  	tk.MustInterDirc(`INSERT INTO t(i, j) VALUES (19, '18446744073709551615')`)
  3428  	tk.MustInterDirc(`INSERT INTO t(i, j) VALUES (20, '18446744073709551616')`)
  3429  	tk.MustInterDirc(`INSERT INTO t(i, j) VALUES (21, '3.14')`)
  3430  	tk.MustInterDirc(`INSERT INTO t(i, j) VALUES (22, '{}')`)
  3431  	tk.MustInterDirc(`INSERT INTO t(i, j) VALUES (23, '[]')`)
  3432  	tk.MustInterDirc(`INSERT INTO t(i, j) VALUES (24, CAST(CAST('2020-01-15 23:24:25' AS DATETIME) AS JSON))`)
  3433  	tk.MustInterDirc(`INSERT INTO t(i, j) VALUES (25, CAST(CAST('23:24:25' AS TIME) AS JSON))`)
  3434  	tk.MustInterDirc(`INSERT INTO t(i, j) VALUES (26, CAST(CAST('2020-01-15' AS DATE) AS JSON))`)
  3435  	tk.MustInterDirc(`INSERT INTO t(i, j) VALUES (27, CAST(TIMESTAMP('2020-01-15 23:24:25') AS JSON))`)
  3436  	tk.MustInterDirc(`INSERT INTO t(i, j) VALUES (28, CAST('[]' AS CHAR CHARACTER SET 'ascii'))`)
  3437  
  3438  	result := tk.MustQuery(`SELECT i,
  3439  		(j = '"scalar string"') AS c1,
  3440  		(j = 'scalar string') AS c2,
  3441  		(j = CAST('"scalar string"' AS JSON)) AS c3,
  3442  		(j = CAST(CAST(j AS CHAR CHARACTER SET 'utf8mb4') AS JSON)) AS c4,
  3443  		(j = CAST(NULL AS JSON)) AS c5,
  3444  		(j = NULL) AS c6,
  3445  		(j <=> NULL) AS c7,
  3446  		(j <=> CAST(NULL AS JSON)) AS c8,
  3447  		(j IN (-1, 2, 32768, 3.14)) AS c9,
  3448  		(j IN (CAST('[1, 2]' AS JSON), CAST('{}' AS JSON), CAST(3.14 AS JSON))) AS c10,
  3449  		(j = (SELECT j FROM t WHERE j = CAST('null' AS JSON))) AS c11,
  3450  		(j = (SELECT j FROM t WHERE j IS NULL)) AS c12,
  3451  		(j = (SELECT j FROM t WHERE 1<>1)) AS c13,
  3452  		(j = DATE('2020-01-15')) AS c14,
  3453  		(j = TIME('23:24:25')) AS c15,
  3454  		(j = TIMESTAMP('2020-01-15 23:24:25')) AS c16,
  3455  		(j = CURRENT_TIMESTAMP) AS c17,
  3456  		(JSON_EXTRACT(j, '$.a') = 2) AS c18
  3457  		FROM t
  3458  		ORDER BY i;`)
  3459  	result.Check(testkit.Events("0 <nil> <nil> <nil> <nil> <nil> <nil> 1 1 <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil>",
  3460  		"1 0 0 0 1 <nil> <nil> 0 0 0 0 0 <nil> <nil> 0 0 0 0 1",
  3461  		"2 0 0 0 1 <nil> <nil> 0 0 0 1 0 <nil> <nil> 0 0 0 0 <nil>",
  3462  		"3 0 0 0 1 <nil> <nil> 0 0 0 0 0 <nil> <nil> 0 0 0 0 0",
  3463  		"4 0 0 0 1 <nil> <nil> 0 0 0 0 0 <nil> <nil> 0 0 0 0 <nil>",
  3464  		"5 0 1 1 1 <nil> <nil> 0 0 0 0 0 <nil> <nil> 0 0 0 0 <nil>",
  3465  		"6 0 0 0 1 <nil> <nil> 0 0 0 0 0 <nil> <nil> 0 0 0 0 <nil>",
  3466  		"7 0 0 0 1 <nil> <nil> 0 0 0 0 0 <nil> <nil> 0 0 0 0 <nil>",
  3467  		"8 0 0 0 1 <nil> <nil> 0 0 0 0 1 <nil> <nil> 0 0 0 0 <nil>",
  3468  		"9 0 0 0 1 <nil> <nil> 0 0 1 0 0 <nil> <nil> 0 0 0 0 <nil>",
  3469  		"10 0 0 0 1 <nil> <nil> 0 0 0 0 0 <nil> <nil> 0 0 0 0 <nil>",
  3470  		"11 0 0 0 1 <nil> <nil> 0 0 0 0 0 <nil> <nil> 0 0 0 0 <nil>",
  3471  		"12 0 0 0 1 <nil> <nil> 0 0 1 0 0 <nil> <nil> 0 0 0 0 <nil>",
  3472  		"13 0 0 0 1 <nil> <nil> 0 0 0 0 0 <nil> <nil> 0 0 0 0 <nil>",
  3473  		"14 0 0 0 1 <nil> <nil> 0 0 0 0 0 <nil> <nil> 0 0 0 0 <nil>",
  3474  		"15 0 0 0 1 <nil> <nil> 0 0 0 0 0 <nil> <nil> 0 0 0 0 <nil>",
  3475  		"16 0 0 0 1 <nil> <nil> 0 0 0 0 0 <nil> <nil> 0 0 0 0 <nil>",
  3476  		"17 0 0 0 1 <nil> <nil> 0 0 0 0 0 <nil> <nil> 0 0 0 0 <nil>",
  3477  		"18 0 0 0 1 <nil> <nil> 0 0 0 0 0 <nil> <nil> 0 0 0 0 <nil>",
  3478  		"19 0 0 0 1 <nil> <nil> 0 0 0 0 0 <nil> <nil> 0 0 0 0 <nil>",
  3479  		"20 0 0 0 1 <nil> <nil> 0 0 0 0 0 <nil> <nil> 0 0 0 0 <nil>",
  3480  		"21 0 0 0 1 <nil> <nil> 0 0 1 1 0 <nil> <nil> 0 0 0 0 <nil>",
  3481  		"22 0 0 0 1 <nil> <nil> 0 0 0 1 0 <nil> <nil> 0 0 0 0 <nil>",
  3482  		"23 0 0 0 1 <nil> <nil> 0 0 0 0 0 <nil> <nil> 0 0 0 0 <nil>",
  3483  		"24 0 0 0 1 <nil> <nil> 0 0 0 0 0 <nil> <nil> 0 0 1 0 <nil>",
  3484  		"25 0 0 0 1 <nil> <nil> 0 0 0 0 0 <nil> <nil> 0 1 0 0 <nil>",
  3485  		"26 0 0 0 1 <nil> <nil> 0 0 0 0 0 <nil> <nil> 1 0 0 0 <nil>",
  3486  		"27 0 0 0 1 <nil> <nil> 0 0 0 0 0 <nil> <nil> 0 0 1 0 <nil>",
  3487  		"28 0 0 0 1 <nil> <nil> 0 0 0 0 0 <nil> <nil> 0 0 0 0 <nil>"))
  3488  
  3489  	// for coalesce
  3490  	result = tk.MustQuery("select coalesce(NULL), coalesce(NULL, NULL), coalesce(NULL, NULL, NULL);")
  3491  	result.Check(testkit.Events("<nil> <nil> <nil>"))
  3492  	tk.MustQuery(`select coalesce(cast(1 as json), cast(2 as json));`).Check(testkit.Events(`1`))
  3493  	tk.MustQuery(`select coalesce(NULL, cast(2 as json));`).Check(testkit.Events(`2`))
  3494  	tk.MustQuery(`select coalesce(cast(1 as json), NULL);`).Check(testkit.Events(`1`))
  3495  	tk.MustQuery(`select coalesce(NULL, NULL);`).Check(testkit.Events(`<nil>`))
  3496  
  3497  	tk.MustInterDirc("drop causet if exists t2")
  3498  	tk.MustInterDirc("create causet t2(a int, b double, c datetime, d time, e char(20), f bit(10))")
  3499  	tk.MustInterDirc(`insert into t2 values(1, 1.1, "2020-08-01 12:01:01", "12:01:01", "abcdef", 0b10101)`)
  3500  
  3501  	result = tk.MustQuery("select coalesce(NULL, a), coalesce(NULL, b, a), coalesce(c, NULL, a, b), coalesce(d, NULL), coalesce(d, c), coalesce(NULL, NULL, e, 1), coalesce(f), coalesce(1, a, b, c, d, e, f) from t2")
  3502  	result.Check(testkit.Events(fmt.Sprintf("1 1.1 2020-08-01 12:01:01 12:01:01 %s 12:01:01 abcdef 21 1", time.Now().In(tk.Se.GetStochastikVars().Location()).Format("2006-01-02"))))
  3503  
  3504  	// nullif
  3505  	result = tk.MustQuery(`SELECT NULLIF(NULL, 1), NULLIF(1, NULL), NULLIF(1, 1), NULLIF(NULL, NULL);`)
  3506  	result.Check(testkit.Events("<nil> 1 <nil> <nil>"))
  3507  
  3508  	result = tk.MustQuery(`SELECT NULLIF(1, 1.0), NULLIF(1, "1.0");`)
  3509  	result.Check(testkit.Events("<nil> <nil>"))
  3510  
  3511  	result = tk.MustQuery(`SELECT NULLIF("abc", 1);`)
  3512  	result.Check(testkit.Events("abc"))
  3513  
  3514  	result = tk.MustQuery(`SELECT NULLIF(1+2, 1);`)
  3515  	result.Check(testkit.Events("3"))
  3516  
  3517  	result = tk.MustQuery(`SELECT NULLIF(1, 1+2);`)
  3518  	result.Check(testkit.Events("1"))
  3519  
  3520  	result = tk.MustQuery(`SELECT NULLIF(2+3, 1+2);`)
  3521  	result.Check(testkit.Events("5"))
  3522  
  3523  	result = tk.MustQuery(`SELECT HEX(NULLIF("abc", 1));`)
  3524  	result.Check(testkit.Events("616263"))
  3525  
  3526  	tk.MustInterDirc("drop causet if exists t;")
  3527  	tk.MustInterDirc("create causet t(a date)")
  3528  	result = tk.MustQuery("desc select a = a from t")
  3529  	result.Check(testkit.Events(
  3530  		"Projection_3 10000.00 root  eq(test.t.a, test.t.a)->DeferredCauset#3",
  3531  		"└─BlockReader_5 10000.00 root  data:BlockFullScan_4",
  3532  		"  └─BlockFullScan_4 10000.00 cop[einsteindb] causet:t keep order:false, stats:pseudo",
  3533  	))
  3534  
  3535  	// for interval
  3536  	result = tk.MustQuery(`select interval(null, 1, 2), interval(1, 2, 3), interval(2, 1, 3)`)
  3537  	result.Check(testkit.Events("-1 0 1"))
  3538  	result = tk.MustQuery(`select interval(3, 1, 2), interval(0, "b", "1", "2"), interval("a", "b", "1", "2")`)
  3539  	result.Check(testkit.Events("2 1 1"))
  3540  	result = tk.MustQuery(`select interval(23, 1, 23, 23, 23, 30, 44, 200), interval(23, 1.7, 15.3, 23.1, 30, 44, 200), interval(9007199254740992, 9007199254740993)`)
  3541  	result.Check(testkit.Events("4 2 0"))
  3542  	result = tk.MustQuery(`select interval(cast(9223372036854775808 as unsigned), cast(9223372036854775809 as unsigned)), interval(9223372036854775807, cast(9223372036854775808 as unsigned)), interval(-9223372036854775807, cast(9223372036854775808 as unsigned))`)
  3543  	result.Check(testkit.Events("0 0 0"))
  3544  	result = tk.MustQuery(`select interval(cast(9223372036854775806 as unsigned), 9223372036854775807), interval(cast(9223372036854775806 as unsigned), -9223372036854775807), interval("9007199254740991", "9007199254740992")`)
  3545  	result.Check(testkit.Events("0 1 0"))
  3546  	result = tk.MustQuery(`select interval(9007199254740992, "9007199254740993"), interval("9007199254740992", 9007199254740993), interval("9007199254740992", "9007199254740993")`)
  3547  	result.Check(testkit.Events("1 1 1"))
  3548  	result = tk.MustQuery(`select INTERVAL(100, NULL, NULL, NULL, NULL, NULL, 100);`)
  3549  	result.Check(testkit.Events("6"))
  3550  
  3551  	// for greatest
  3552  	result = tk.MustQuery(`select greatest(1, 2, 3), greatest("a", "b", "c"), greatest(1.1, 1.2, 1.3), greatest("123a", 1, 2)`)
  3553  	result.Check(testkit.Events("3 c 1.3 123"))
  3554  	tk.MustQuery("show warnings").Check(solitonutil.EventsWithSep("|", "Warning|1292|Truncated incorrect FLOAT value: '123a'"))
  3555  	result = tk.MustQuery(`select greatest(cast("2020-01-01" as datetime), "123", "234", cast("2020-01-01" as date)), greatest(cast("2020-01-01" as date), "123", null)`)
  3556  	// todo: MyALLEGROSQL returns "2020-01-01 <nil>"
  3557  	result.Check(testkit.Events("2020-01-01 00:00:00 <nil>"))
  3558  	tk.MustQuery("show warnings").Check(solitonutil.EventsWithSep("|", "Warning|1292|Incorrect time value: '123'", "Warning|1292|Incorrect time value: '234'", "Warning|1292|Incorrect time value: '123'"))
  3559  	// for least
  3560  	result = tk.MustQuery(`select least(1, 2, 3), least("a", "b", "c"), least(1.1, 1.2, 1.3), least("123a", 1, 2)`)
  3561  	result.Check(testkit.Events("1 a 1.1 1"))
  3562  	tk.MustQuery("show warnings").Check(solitonutil.EventsWithSep("|", "Warning|1292|Truncated incorrect FLOAT value: '123a'"))
  3563  	result = tk.MustQuery(`select least(cast("2020-01-01" as datetime), "123", "234", cast("2020-01-01" as date)), least(cast("2020-01-01" as date), "123", null)`)
  3564  	result.Check(testkit.Events("123 <nil>"))
  3565  	tk.MustQuery("show warnings").Check(solitonutil.EventsWithSep("|", "Warning|1292|Incorrect time value: '123'", "Warning|1292|Incorrect time value: '234'", "Warning|1292|Incorrect time value: '123'"))
  3566  	tk.MustQuery(`select 1 < 17666000000000000000, 1 > 17666000000000000000, 1 = 17666000000000000000`).Check(testkit.Events("1 0 0"))
  3567  
  3568  	tk.MustInterDirc("drop causet if exists t")
  3569  	// insert value at utc timezone
  3570  	tk.MustInterDirc("set time_zone = '+00:00'")
  3571  	tk.MustInterDirc("create causet t(a timestamp)")
  3572  	tk.MustInterDirc("insert into t value('1991-05-06 04:59:28')")
  3573  	// check daylight saving time in Asia/Shanghai
  3574  	tk.MustInterDirc("set time_zone='Asia/Shanghai'")
  3575  	tk.MustQuery("select * from t").Check(testkit.Events("1991-05-06 13:59:28"))
  3576  	// insert an nonexistent time
  3577  	tk.MustInterDirc("set time_zone = 'America/Los_Angeles'")
  3578  	_, err := tk.InterDirc("insert into t value('2011-03-13 02:00:00')")
  3579  	c.Assert(err, NotNil)
  3580  	// reset timezone to a +8 offset
  3581  	tk.MustInterDirc("set time_zone = '+08:00'")
  3582  	tk.MustQuery("select * from t").Check(testkit.Events("1991-05-06 12:59:28"))
  3583  
  3584  	tk.MustInterDirc("drop causet if exists t")
  3585  	tk.MustInterDirc("create causet t(a bigint unsigned)")
  3586  	tk.MustInterDirc("insert into t value(17666000000000000000)")
  3587  	tk.MustQuery("select * from t where a = 17666000000000000000").Check(testkit.Events("17666000000000000000"))
  3588  
  3589  	// test for compare event
  3590  	result = tk.MustQuery(`select event(1,2,3)=event(1,2,3)`)
  3591  	result.Check(testkit.Events("1"))
  3592  	result = tk.MustQuery(`select event(1,2,3)=event(1+3,2,3)`)
  3593  	result.Check(testkit.Events("0"))
  3594  	result = tk.MustQuery(`select event(1,2,3)<>event(1,2,3)`)
  3595  	result.Check(testkit.Events("0"))
  3596  	result = tk.MustQuery(`select event(1,2,3)<>event(1+3,2,3)`)
  3597  	result.Check(testkit.Events("1"))
  3598  	result = tk.MustQuery(`select event(1+3,2,3)<>event(1+3,2,3)`)
  3599  	result.Check(testkit.Events("0"))
  3600  }
  3601  
  3602  func (s *testIntegrationSuite) TestAggregationBuiltin(c *C) {
  3603  	defer s.cleanEnv(c)
  3604  	tk := testkit.NewTestKit(c, s.causetstore)
  3605  	tk.MustInterDirc("use test")
  3606  	tk.MustInterDirc("create causet t(a decimal(7, 6))")
  3607  	tk.MustInterDirc("insert into t values(1.123456), (1.123456)")
  3608  	result := tk.MustQuery("select avg(a) from t")
  3609  	result.Check(testkit.Events("1.1234560000"))
  3610  
  3611  	tk.MustInterDirc("use test")
  3612  	tk.MustInterDirc("drop causet t")
  3613  	tk.MustInterDirc("CREATE TABLE `t` (	`a` int, KEY `idx_a` (`a`))")
  3614  	result = tk.MustQuery("select avg(a) from t")
  3615  	result.Check(testkit.Events("<nil>"))
  3616  	result = tk.MustQuery("select max(a), min(a) from t")
  3617  	result.Check(testkit.Events("<nil> <nil>"))
  3618  	result = tk.MustQuery("select distinct a from t")
  3619  	result.Check(testkit.Events())
  3620  	result = tk.MustQuery("select sum(a) from t")
  3621  	result.Check(testkit.Events("<nil>"))
  3622  	result = tk.MustQuery("select count(a) from t")
  3623  	result.Check(testkit.Events("0"))
  3624  	result = tk.MustQuery("select bit_or(a) from t")
  3625  	result.Check(testkit.Events("0"))
  3626  	result = tk.MustQuery("select bit_xor(a) from t")
  3627  	result.Check(testkit.Events("0"))
  3628  	result = tk.MustQuery("select bit_and(a) from t")
  3629  	result.Check(testkit.Events("18446744073709551615"))
  3630  }
  3631  
  3632  func (s *testIntegrationSuite) TestAggregationBuiltinBitOr(c *C) {
  3633  	defer s.cleanEnv(c)
  3634  	tk := testkit.NewTestKit(c, s.causetstore)
  3635  	tk.MustInterDirc("use test")
  3636  	tk.MustInterDirc("drop causet if exists t;")
  3637  	tk.MustInterDirc("create causet t(a bigint)")
  3638  	tk.MustInterDirc("insert into t values(null);")
  3639  	result := tk.MustQuery("select bit_or(a) from t")
  3640  	result.Check(testkit.Events("0"))
  3641  	tk.MustInterDirc("insert into t values(1);")
  3642  	result = tk.MustQuery("select bit_or(a) from t")
  3643  	result.Check(testkit.Events("1"))
  3644  	tk.MustInterDirc("insert into t values(2);")
  3645  	result = tk.MustQuery("select bit_or(a) from t")
  3646  	result.Check(testkit.Events("3"))
  3647  	tk.MustInterDirc("insert into t values(4);")
  3648  	result = tk.MustQuery("select bit_or(a) from t")
  3649  	result.Check(testkit.Events("7"))
  3650  	result = tk.MustQuery("select a, bit_or(a) from t group by a order by a")
  3651  	result.Check(testkit.Events("<nil> 0", "1 1", "2 2", "4 4"))
  3652  	tk.MustInterDirc("insert into t values(-1);")
  3653  	result = tk.MustQuery("select bit_or(a) from t")
  3654  	result.Check(testkit.Events("18446744073709551615"))
  3655  }
  3656  
  3657  func (s *testIntegrationSuite) TestAggregationBuiltinBitXor(c *C) {
  3658  	defer s.cleanEnv(c)
  3659  	tk := testkit.NewTestKit(c, s.causetstore)
  3660  	tk.MustInterDirc("use test")
  3661  	tk.MustInterDirc("drop causet if exists t;")
  3662  	tk.MustInterDirc("create causet t(a bigint)")
  3663  	tk.MustInterDirc("insert into t values(null);")
  3664  	result := tk.MustQuery("select bit_xor(a) from t")
  3665  	result.Check(testkit.Events("0"))
  3666  	tk.MustInterDirc("insert into t values(1);")
  3667  	result = tk.MustQuery("select bit_xor(a) from t")
  3668  	result.Check(testkit.Events("1"))
  3669  	tk.MustInterDirc("insert into t values(2);")
  3670  	result = tk.MustQuery("select bit_xor(a) from t")
  3671  	result.Check(testkit.Events("3"))
  3672  	tk.MustInterDirc("insert into t values(3);")
  3673  	result = tk.MustQuery("select bit_xor(a) from t")
  3674  	result.Check(testkit.Events("0"))
  3675  	tk.MustInterDirc("insert into t values(3);")
  3676  	result = tk.MustQuery("select bit_xor(a) from t")
  3677  	result.Check(testkit.Events("3"))
  3678  	result = tk.MustQuery("select a, bit_xor(a) from t group by a order by a")
  3679  	result.Check(testkit.Events("<nil> 0", "1 1", "2 2", "3 0"))
  3680  }
  3681  
  3682  func (s *testIntegrationSuite) TestAggregationBuiltinBitAnd(c *C) {
  3683  	defer s.cleanEnv(c)
  3684  	tk := testkit.NewTestKit(c, s.causetstore)
  3685  	tk.MustInterDirc("use test")
  3686  	tk.MustInterDirc("drop causet if exists t;")
  3687  	tk.MustInterDirc("create causet t(a bigint)")
  3688  	tk.MustInterDirc("insert into t values(null);")
  3689  	result := tk.MustQuery("select bit_and(a) from t")
  3690  	result.Check(testkit.Events("18446744073709551615"))
  3691  	tk.MustInterDirc("insert into t values(7);")
  3692  	result = tk.MustQuery("select bit_and(a) from t")
  3693  	result.Check(testkit.Events("7"))
  3694  	tk.MustInterDirc("insert into t values(5);")
  3695  	result = tk.MustQuery("select bit_and(a) from t")
  3696  	result.Check(testkit.Events("5"))
  3697  	tk.MustInterDirc("insert into t values(3);")
  3698  	result = tk.MustQuery("select bit_and(a) from t")
  3699  	result.Check(testkit.Events("1"))
  3700  	tk.MustInterDirc("insert into t values(2);")
  3701  	result = tk.MustQuery("select bit_and(a) from t")
  3702  	result.Check(testkit.Events("0"))
  3703  	result = tk.MustQuery("select a, bit_and(a) from t group by a order by a desc")
  3704  	result.Check(testkit.Events("7 7", "5 5", "3 3", "2 2", "<nil> 18446744073709551615"))
  3705  }
  3706  
  3707  func (s *testIntegrationSuite) TestAggregationBuiltinGroupConcat(c *C) {
  3708  	defer s.cleanEnv(c)
  3709  	tk := testkit.NewTestKit(c, s.causetstore)
  3710  	tk.MustInterDirc("use test")
  3711  	tk.MustInterDirc("create causet t(a varchar(100))")
  3712  	tk.MustInterDirc("create causet d(a varchar(100))")
  3713  	tk.MustInterDirc("insert into t values('hello'), ('hello')")
  3714  	result := tk.MustQuery("select group_concat(a) from t")
  3715  	result.Check(testkit.Events("hello,hello"))
  3716  
  3717  	tk.MustInterDirc("set @@group_concat_max_len=7")
  3718  	result = tk.MustQuery("select group_concat(a) from t")
  3719  	result.Check(testkit.Events("hello,h"))
  3720  	tk.MustQuery("show warnings").Check(solitonutil.EventsWithSep("|", "Warning 1260 Some rows were cut by GROUPCONCAT(test.t.a)"))
  3721  
  3722  	_, err := tk.InterDirc("insert into d select group_concat(a) from t")
  3723  	c.Assert(errors.Cause(err).(*terror.Error).Code(), Equals, errors.ErrCode(allegrosql.ErrCutValueGroupConcat))
  3724  
  3725  	tk.InterDirc("set sql_mode=''")
  3726  	tk.MustInterDirc("insert into d select group_concat(a) from t")
  3727  	tk.MustQuery("show warnings").Check(solitonutil.EventsWithSep("|", "Warning 1260 Some rows were cut by GROUPCONCAT(test.t.a)"))
  3728  	tk.MustQuery("select * from d").Check(testkit.Events("hello,h"))
  3729  }
  3730  
  3731  func (s *testIntegrationSuite) TestAggregationBuiltinJSONObjectAgg(c *C) {
  3732  	defer s.cleanEnv(c)
  3733  	tk := testkit.NewTestKit(c, s.causetstore)
  3734  	tk.MustInterDirc("use test")
  3735  
  3736  	tk.MustInterDirc("drop causet if exists t;")
  3737  	tk.MustInterDirc(`CREATE TABLE t (
  3738  		a int(11),
  3739  		b varchar(100),
  3740  		c decimal(3,2),
  3741  		d json,
  3742  		e date,
  3743  		f time,
  3744  		g datetime DEFAULT '2012-01-01',
  3745  		h timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  3746  		i char(36),
  3747  		j text(50));`)
  3748  
  3749  	tk.MustInterDirc(`insert into t values(1, 'ab', 5.5, '{"id": 1}', '2020-01-10', '11:12:13', '2020-01-11', '0000-00-00 00:00:00', 'first', 'json_objectagg_test');`)
  3750  
  3751  	result := tk.MustQuery("select json_objectagg(a, b) from t group by a order by a;")
  3752  	result.Check(testkit.Events(`{"1": "ab"}`))
  3753  	result = tk.MustQuery("select json_objectagg(b, c) from t group by b order by b;")
  3754  	result.Check(testkit.Events(`{"ab": 5.5}`))
  3755  	result = tk.MustQuery("select json_objectagg(e, f) from t group by e order by e;")
  3756  	result.Check(testkit.Events(`{"2020-01-10": "11:12:13"}`))
  3757  	result = tk.MustQuery("select json_objectagg(f, g) from t group by f order by f;")
  3758  	result.Check(testkit.Events(`{"11:12:13": "2020-01-11 00:00:00"}`))
  3759  	result = tk.MustQuery("select json_objectagg(g, h) from t group by g order by g;")
  3760  	result.Check(testkit.Events(`{"2020-01-11 00:00:00": "0000-00-00 00:00:00"}`))
  3761  	result = tk.MustQuery("select json_objectagg(h, i) from t group by h order by h;")
  3762  	result.Check(testkit.Events(`{"0000-00-00 00:00:00": "first"}`))
  3763  	result = tk.MustQuery("select json_objectagg(i, j) from t group by i order by i;")
  3764  	result.Check(testkit.Events(`{"first": "json_objectagg_test"}`))
  3765  	result = tk.MustQuery("select json_objectagg(a, null) from t group by a order by a;")
  3766  	result.Check(testkit.Events(`{"1": null}`))
  3767  }
  3768  
  3769  func (s *testIntegrationSuite2) TestOtherBuiltin(c *C) {
  3770  	defer s.cleanEnv(c)
  3771  	tk := testkit.NewTestKit(c, s.causetstore)
  3772  	tk.MustInterDirc("use test")
  3773  
  3774  	tk.MustInterDirc("drop causet if exists t")
  3775  	tk.MustInterDirc("create causet t(a int, b double, c varchar(20), d datetime, e time)")
  3776  	tk.MustInterDirc("insert into t value(1, 2, 'string', '2020-01-01 12:12:12', '12:12:12')")
  3777  
  3778  	// for in
  3779  	result := tk.MustQuery("select 1 in (a, b, c), 'string' in (a, b, c), '2020-01-01 12:12:12' in (c, d, e), '12:12:12' in (c, d, e) from t")
  3780  	result.Check(testkit.Events("1 1 1 1"))
  3781  	result = tk.MustQuery("select 1 in (null, c), 2 in (null, c) from t")
  3782  	result.Check(testkit.Events("<nil> <nil>"))
  3783  	result = tk.MustQuery("select 0 in (a, b, c), 0 in (a, b, c), 3 in (a, b, c), 4 in (a, b, c) from t")
  3784  	result.Check(testkit.Events("1 1 0 0"))
  3785  	result = tk.MustQuery("select (0,1) in ((0,1), (0,2)), (0,1) in ((0,0), (0,2))")
  3786  	result.Check(testkit.Events("1 0"))
  3787  
  3788  	result = tk.MustQuery(`select bit_count(121), bit_count(-1), bit_count(null), bit_count("1231aaa");`)
  3789  	result.Check(testkit.Events("5 64 <nil> 7"))
  3790  
  3791  	tk.MustInterDirc("drop causet if exists t")
  3792  	tk.MustInterDirc("create causet t(a int primary key, b time, c double, d varchar(10))")
  3793  	tk.MustInterDirc(`insert into t values(1, '01:01:01', 1.1, "1"), (2, '02:02:02', 2.2, "2")`)
  3794  	tk.MustInterDirc(`insert into t(a, b) values(1, '12:12:12') on duplicate key uFIDelate a = values(b)`)
  3795  	result = tk.MustQuery(`select a from t order by a`)
  3796  	result.Check(testkit.Events("2", "121212"))
  3797  	tk.MustInterDirc(`insert into t values(2, '12:12:12', 1.1, "3.3") on duplicate key uFIDelate a = values(c) + values(d)`)
  3798  	result = tk.MustQuery(`select a from t order by a`)
  3799  	result.Check(testkit.Events("4", "121212"))
  3800  
  3801  	// for setvar, getvar
  3802  	tk.MustInterDirc(`set @varname = "Abc"`)
  3803  	result = tk.MustQuery(`select @varname, @VARNAME`)
  3804  	result.Check(testkit.Events("Abc Abc"))
  3805  
  3806  	// for values
  3807  	tk.MustInterDirc("drop causet t")
  3808  	tk.MustInterDirc("CREATE TABLE `t` (`id` varchar(32) NOT NULL, `count` decimal(18,2), PRIMARY KEY (`id`));")
  3809  	tk.MustInterDirc("INSERT INTO t (id,count)VALUES('abc',2) ON DUPLICATE KEY UFIDelATE count=if(VALUES(count) > count,VALUES(count),count)")
  3810  	result = tk.MustQuery("select count from t where id = 'abc'")
  3811  	result.Check(testkit.Events("2.00"))
  3812  	tk.MustInterDirc("INSERT INTO t (id,count)VALUES('abc',265.0) ON DUPLICATE KEY UFIDelATE count=if(VALUES(count) > count,VALUES(count),count)")
  3813  	result = tk.MustQuery("select count from t where id = 'abc'")
  3814  	result.Check(testkit.Events("265.00"))
  3815  
  3816  	// for values(issue #4884)
  3817  	tk.MustInterDirc("drop causet if exists t;")
  3818  	tk.MustInterDirc("create causet test(id int not null, val text, primary key(id));")
  3819  	tk.MustInterDirc("insert into test values(1,'hello');")
  3820  	result = tk.MustQuery("select * from test;")
  3821  	result.Check(testkit.Events("1 hello"))
  3822  	tk.MustInterDirc("insert into test values(1, NULL) on duplicate key uFIDelate val = VALUES(val);")
  3823  	result = tk.MustQuery("select * from test;")
  3824  	result.Check(testkit.Events("1 <nil>"))
  3825  
  3826  	tk.MustInterDirc("drop causet if exists test;")
  3827  	tk.MustInterDirc(`create causet test(
  3828  		id int not null,
  3829  		a text,
  3830  		b blob,
  3831  		c varchar(20),
  3832  		d int,
  3833  		e float,
  3834  		f DECIMAL(6,4),
  3835  		g JSON,
  3836  		primary key(id));`)
  3837  
  3838  	tk.MustInterDirc(`insert into test values(1,'txt hello', 'blb hello', 'vc hello', 1, 1.1, 1.0, '{"key1": "value1", "key2": "value2"}');`)
  3839  	tk.MustInterDirc(`insert into test values(1, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
  3840  	on duplicate key uFIDelate
  3841  	a = values(a),
  3842  	b = values(b),
  3843  	c = values(c),
  3844  	d = values(d),
  3845  	e = values(e),
  3846  	f = values(f),
  3847  	g = values(g);`)
  3848  
  3849  	result = tk.MustQuery("select * from test;")
  3850  	result.Check(testkit.Events("1 <nil> <nil> <nil> <nil> <nil> <nil> <nil>"))
  3851  }
  3852  
  3853  func (s *testIntegrationSuite) TestDateBuiltin(c *C) {
  3854  	ctx := context.Background()
  3855  	defer s.cleanEnv(c)
  3856  	tk := testkit.NewTestKit(c, s.causetstore)
  3857  	tk.MustInterDirc("USE test;")
  3858  	tk.MustInterDirc("DROP TABLE IF EXISTS t;")
  3859  	tk.MustInterDirc("create causet t (d date);")
  3860  	tk.MustInterDirc("insert into t values ('1997-01-02')")
  3861  	tk.MustInterDirc("insert into t values ('1998-01-02')")
  3862  	r := tk.MustQuery("select * from t where d < date '1998-01-01';")
  3863  	r.Check(testkit.Events("1997-01-02"))
  3864  
  3865  	r = tk.MustQuery("select date'20171212'")
  3866  	r.Check(testkit.Events("2020-12-12"))
  3867  
  3868  	r = tk.MustQuery("select date'2020/12/12'")
  3869  	r.Check(testkit.Events("2020-12-12"))
  3870  
  3871  	r = tk.MustQuery("select date'2020/12-12'")
  3872  	r.Check(testkit.Events("2020-12-12"))
  3873  
  3874  	tk.MustInterDirc("set sql_mode = ''")
  3875  	r = tk.MustQuery("select date '0000-00-00';")
  3876  	r.Check(testkit.Events("0000-00-00"))
  3877  
  3878  	tk.MustInterDirc("set sql_mode = 'NO_ZERO_IN_DATE'")
  3879  	r = tk.MustQuery("select date '0000-00-00';")
  3880  	r.Check(testkit.Events("0000-00-00"))
  3881  
  3882  	tk.MustInterDirc("set sql_mode = 'NO_ZERO_DATE'")
  3883  	rs, err := tk.InterDirc("select date '0000-00-00';")
  3884  	c.Assert(err, IsNil)
  3885  	_, err = stochastik.GetEvents4Test(ctx, tk.Se, rs)
  3886  	c.Assert(err, NotNil)
  3887  	c.Assert(terror.ErrorEqual(err, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, "0000-00-00")), IsTrue)
  3888  	c.Assert(rs.Close(), IsNil)
  3889  
  3890  	tk.MustInterDirc("set sql_mode = ''")
  3891  	r = tk.MustQuery("select date '2007-10-00';")
  3892  	r.Check(testkit.Events("2007-10-00"))
  3893  
  3894  	tk.MustInterDirc("set sql_mode = 'NO_ZERO_IN_DATE'")
  3895  	rs, _ = tk.InterDirc("select date '2007-10-00';")
  3896  	_, err = stochastik.GetEvents4Test(ctx, tk.Se, rs)
  3897  	c.Assert(err, NotNil)
  3898  	c.Assert(terror.ErrorEqual(err, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, "2020-10-00")), IsTrue)
  3899  	c.Assert(rs.Close(), IsNil)
  3900  
  3901  	tk.MustInterDirc("set sql_mode = 'NO_ZERO_DATE'")
  3902  	r = tk.MustQuery("select date '2007-10-00';")
  3903  	r.Check(testkit.Events("2007-10-00"))
  3904  
  3905  	tk.MustInterDirc("set sql_mode = 'NO_ZERO_IN_DATE,NO_ZERO_DATE'")
  3906  
  3907  	rs, _ = tk.InterDirc("select date '2007-10-00';")
  3908  	_, err = stochastik.GetEvents4Test(ctx, tk.Se, rs)
  3909  	c.Assert(err, NotNil)
  3910  	c.Assert(terror.ErrorEqual(err, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, "2020-10-00")), IsTrue)
  3911  	c.Assert(rs.Close(), IsNil)
  3912  
  3913  	rs, err = tk.InterDirc("select date '0000-00-00';")
  3914  	c.Assert(err, IsNil)
  3915  	_, err = stochastik.GetEvents4Test(ctx, tk.Se, rs)
  3916  	c.Assert(err, NotNil)
  3917  	c.Assert(terror.ErrorEqual(err, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, "0000-00-00")), IsTrue)
  3918  	c.Assert(rs.Close(), IsNil)
  3919  
  3920  	r = tk.MustQuery("select date'1998~01~02'")
  3921  	r.Check(testkit.Events("1998-01-02"))
  3922  
  3923  	r = tk.MustQuery("select date'731124', date '011124'")
  3924  	r.Check(testkit.Events("1973-11-24 2001-11-24"))
  3925  
  3926  	_, err = tk.InterDirc("select date '0000-00-00 00:00:00';")
  3927  	c.Assert(err, NotNil)
  3928  	c.Assert(terror.ErrorEqual(err, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, "0000-00-00 00:00:00")), IsTrue)
  3929  
  3930  	_, err = tk.InterDirc("select date '2020-99-99';")
  3931  	c.Assert(err, NotNil)
  3932  	c.Assert(terror.ErrorEqual(err, types.ErrWrongValue), IsTrue, Commentf("err: %v", err))
  3933  
  3934  	_, err = tk.InterDirc("select date '2020-2-31';")
  3935  	c.Assert(err, NotNil)
  3936  	c.Assert(terror.ErrorEqual(err, types.ErrWrongValue), IsTrue, Commentf("err: %v", err))
  3937  
  3938  	_, err = tk.InterDirc("select date '201712-31';")
  3939  	c.Assert(err, NotNil)
  3940  	c.Assert(terror.ErrorEqual(err, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, "201712-31")), IsTrue, Commentf("err: %v", err))
  3941  
  3942  	_, err = tk.InterDirc("select date 'abcdefg';")
  3943  	c.Assert(err, NotNil)
  3944  	c.Assert(terror.ErrorEqual(err, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, "abcdefg")), IsTrue, Commentf("err: %v", err))
  3945  }
  3946  
  3947  func (s *testIntegrationSuite) TestJSONBuiltin(c *C) {
  3948  	defer s.cleanEnv(c)
  3949  	tk := testkit.NewTestKit(c, s.causetstore)
  3950  	tk.MustInterDirc("USE test;")
  3951  	tk.MustInterDirc("DROP TABLE IF EXISTS t;")
  3952  	tk.MustInterDirc("CREATE TABLE `my_defCauslection` (	`doc` json DEFAULT NULL, `_id` varchar(32) GENERATED ALWAYS AS (JSON_UNQUOTE(JSON_EXTRACT(doc,'$._id'))) STORED NOT NULL, PRIMARY KEY (`_id`))")
  3953  	_, err := tk.InterDirc("UFIDelATE `test`.`my_defCauslection` SET doc=JSON_SET(doc) WHERE (JSON_EXTRACT(doc,'$.name') = 'clare');")
  3954  	c.Assert(err, NotNil)
  3955  
  3956  	r := tk.MustQuery("select json_valid(null);")
  3957  	r.Check(testkit.Events("<nil>"))
  3958  
  3959  	r = tk.MustQuery(`select json_valid("null");`)
  3960  	r.Check(testkit.Events("1"))
  3961  
  3962  	r = tk.MustQuery("select json_valid(0);")
  3963  	r.Check(testkit.Events("0"))
  3964  
  3965  	r = tk.MustQuery(`select json_valid("0");`)
  3966  	r.Check(testkit.Events("1"))
  3967  
  3968  	r = tk.MustQuery(`select json_valid("hello");`)
  3969  	r.Check(testkit.Events("0"))
  3970  
  3971  	r = tk.MustQuery(`select json_valid('"hello"');`)
  3972  	r.Check(testkit.Events("1"))
  3973  
  3974  	r = tk.MustQuery(`select json_valid('{"a":1}');`)
  3975  	r.Check(testkit.Events("1"))
  3976  
  3977  	r = tk.MustQuery("select json_valid('{}');")
  3978  	r.Check(testkit.Events("1"))
  3979  
  3980  	r = tk.MustQuery(`select json_valid('[]');`)
  3981  	r.Check(testkit.Events("1"))
  3982  
  3983  	r = tk.MustQuery("select json_valid('2020-8-19');")
  3984  	r.Check(testkit.Events("0"))
  3985  
  3986  	r = tk.MustQuery(`select json_valid('"2020-8-19"');`)
  3987  	r.Check(testkit.Events("1"))
  3988  }
  3989  
  3990  func (s *testIntegrationSuite) TestTimeLiteral(c *C) {
  3991  	defer s.cleanEnv(c)
  3992  	tk := testkit.NewTestKit(c, s.causetstore)
  3993  
  3994  	r := tk.MustQuery("select time '117:01:12';")
  3995  	r.Check(testkit.Events("117:01:12"))
  3996  
  3997  	r = tk.MustQuery("select time '01:00:00.999999';")
  3998  	r.Check(testkit.Events("01:00:00.999999"))
  3999  
  4000  	r = tk.MustQuery("select time '1 01:00:00';")
  4001  	r.Check(testkit.Events("25:00:00"))
  4002  
  4003  	r = tk.MustQuery("select time '110:00:00';")
  4004  	r.Check(testkit.Events("110:00:00"))
  4005  
  4006  	r = tk.MustQuery("select time'-1:1:1.123454656';")
  4007  	r.Check(testkit.Events("-01:01:01.123455"))
  4008  
  4009  	r = tk.MustQuery("select time '33:33';")
  4010  	r.Check(testkit.Events("33:33:00"))
  4011  
  4012  	r = tk.MustQuery("select time '1.1';")
  4013  	r.Check(testkit.Events("00:00:01.1"))
  4014  
  4015  	r = tk.MustQuery("select time '21';")
  4016  	r.Check(testkit.Events("00:00:21"))
  4017  
  4018  	r = tk.MustQuery("select time '20 20:20';")
  4019  	r.Check(testkit.Events("500:20:00"))
  4020  
  4021  	_, err := tk.InterDirc("select time '2020-01-01 00:00:00';")
  4022  	c.Assert(err, NotNil)
  4023  	c.Assert(terror.ErrorEqual(err, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, "2020-01-01 00:00:00")), IsTrue)
  4024  
  4025  	_, err = tk.InterDirc("select time '071231235959.999999';")
  4026  	c.Assert(err, NotNil)
  4027  	c.Assert(terror.ErrorEqual(err, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, "071231235959.999999")), IsTrue)
  4028  
  4029  	_, err = tk.InterDirc("select time '20171231235959.999999';")
  4030  	c.Assert(err, NotNil)
  4031  	c.Assert(terror.ErrorEqual(err, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, "20171231235959.999999")), IsTrue)
  4032  
  4033  	_, err = tk.InterDirc("select ADDDATE('2008-01-34', -1);")
  4034  	c.Assert(err, IsNil)
  4035  	tk.MustQuery("Show warnings;").Check(solitonutil.EventsWithSep("|",
  4036  		"Warning|1292|Incorrect datetime value: '2008-01-34'"))
  4037  }
  4038  
  4039  func (s *testIntegrationSuite) TestIssue13822(c *C) {
  4040  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
  4041  	tk.MustQuery("select ADDDATE(20111111, interval '-123' DAY);").Check(testkit.Events("2011-07-11"))
  4042  	tk.MustQuery("select SUBDATE(20111111, interval '-123' DAY);").Check(testkit.Events("2012-03-13"))
  4043  }
  4044  
  4045  func (s *testIntegrationSuite) TestTimestampLiteral(c *C) {
  4046  	defer s.cleanEnv(c)
  4047  	tk := testkit.NewTestKit(c, s.causetstore)
  4048  
  4049  	r := tk.MustQuery("select timestamp '2020-01-01 00:00:00';")
  4050  	r.Check(testkit.Events("2020-01-01 00:00:00"))
  4051  
  4052  	r = tk.MustQuery("select timestamp '2020@01@01 00:00:00';")
  4053  	r.Check(testkit.Events("2020-01-01 00:00:00"))
  4054  
  4055  	r = tk.MustQuery("select timestamp '2020@01@01 00~00~00';")
  4056  	r.Check(testkit.Events("2020-01-01 00:00:00"))
  4057  
  4058  	r = tk.MustQuery("select timestamp '2020@01@0001 00~00~00.333';")
  4059  	r.Check(testkit.Events("2020-01-01 00:00:00.333"))
  4060  
  4061  	_, err := tk.InterDirc("select timestamp '00:00:00';")
  4062  	c.Assert(err, NotNil)
  4063  	c.Assert(terror.ErrorEqual(err, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, "00:00:00")), IsTrue)
  4064  
  4065  	_, err = tk.InterDirc("select timestamp '1992-01-03';")
  4066  	c.Assert(err, NotNil)
  4067  	c.Assert(terror.ErrorEqual(err, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, "1992-01-03")), IsTrue)
  4068  
  4069  	_, err = tk.InterDirc("select timestamp '20171231235959.999999';")
  4070  	c.Assert(err, NotNil)
  4071  	c.Assert(terror.ErrorEqual(err, types.ErrWrongValue.GenWithStackByArgs(types.DateTimeStr, "20171231235959.999999")), IsTrue)
  4072  }
  4073  
  4074  func (s *testIntegrationSuite) TestLiterals(c *C) {
  4075  	defer s.cleanEnv(c)
  4076  	tk := testkit.NewTestKit(c, s.causetstore)
  4077  	r := tk.MustQuery("SELECT LENGTH(b''), LENGTH(B''), b''+1, b''-1, B''+1;")
  4078  	r.Check(testkit.Events("0 0 1 -1 1"))
  4079  }
  4080  
  4081  func (s *testIntegrationSuite) TestFuncJSON(c *C) {
  4082  	tk := testkit.NewTestKit(c, s.causetstore)
  4083  	defer s.cleanEnv(c)
  4084  	tk.MustInterDirc("USE test;")
  4085  	tk.MustInterDirc("DROP TABLE IF EXISTS block_json;")
  4086  	tk.MustInterDirc("CREATE TABLE block_json(a json, b VARCHAR(255));")
  4087  
  4088  	j1 := `{"\\"hello\\"": "world", "a": [1, "2", {"aa": "bb"}, 4.0, {"aa": "cc"}], "b": true, "c": ["d"]}`
  4089  	j2 := `[{"a": 1, "b": true}, 3, 3.5, "hello, world", null, true]`
  4090  	for _, j := range []string{j1, j2} {
  4091  		tk.MustInterDirc(fmt.Sprintf(`INSERT INTO block_json values('%s', '%s')`, j, j))
  4092  	}
  4093  
  4094  	r := tk.MustQuery(`select json_type(a), json_type(b) from block_json`)
  4095  	r.Check(testkit.Events("OBJECT OBJECT", "ARRAY ARRAY"))
  4096  
  4097  	tk.MustGetErrCode("select json_quote();", allegrosql.ErrWrongParamcountToNativeFct)
  4098  	tk.MustGetErrCode("select json_quote('abc', 'def');", allegrosql.ErrWrongParamcountToNativeFct)
  4099  	tk.MustGetErrCode("select json_quote(NULL, 'def');", allegrosql.ErrWrongParamcountToNativeFct)
  4100  	tk.MustGetErrCode("select json_quote('abc', NULL);", allegrosql.ErrWrongParamcountToNativeFct)
  4101  
  4102  	tk.MustGetErrCode("select json_unquote();", allegrosql.ErrWrongParamcountToNativeFct)
  4103  	tk.MustGetErrCode("select json_unquote('abc', 'def');", allegrosql.ErrWrongParamcountToNativeFct)
  4104  	tk.MustGetErrCode("select json_unquote(NULL, 'def');", allegrosql.ErrWrongParamcountToNativeFct)
  4105  	tk.MustGetErrCode("select json_unquote('abc', NULL);", allegrosql.ErrWrongParamcountToNativeFct)
  4106  
  4107  	tk.MustQuery("select json_quote(NULL);").Check(testkit.Events("<nil>"))
  4108  	tk.MustQuery("select json_unquote(NULL);").Check(testkit.Events("<nil>"))
  4109  
  4110  	tk.MustQuery("select json_quote('abc');").Check(testkit.Events(`"abc"`))
  4111  	tk.MustQuery(`select json_quote(convert('"abc"' using ascii));`).Check(testkit.Events(`"\"abc\""`))
  4112  	tk.MustQuery(`select json_quote(convert('"abc"' using latin1));`).Check(testkit.Events(`"\"abc\""`))
  4113  	tk.MustQuery(`select json_quote(convert('"abc"' using utf8));`).Check(testkit.Events(`"\"abc\""`))
  4114  	tk.MustQuery(`select json_quote(convert('"abc"' using utf8mb4));`).Check(testkit.Events(`"\"abc\""`))
  4115  
  4116  	tk.MustQuery("select json_unquote('abc');").Check(testkit.Events("abc"))
  4117  	tk.MustQuery(`select json_unquote('"abc"');`).Check(testkit.Events("abc"))
  4118  	tk.MustQuery(`select json_unquote(convert('"abc"' using ascii));`).Check(testkit.Events("abc"))
  4119  	tk.MustQuery(`select json_unquote(convert('"abc"' using latin1));`).Check(testkit.Events("abc"))
  4120  	tk.MustQuery(`select json_unquote(convert('"abc"' using utf8));`).Check(testkit.Events("abc"))
  4121  	tk.MustQuery(`select json_unquote(convert('"abc"' using utf8mb4));`).Check(testkit.Events("abc"))
  4122  
  4123  	tk.MustQuery(`select json_quote('"');`).Check(testkit.Events(`"\""`))
  4124  	tk.MustQuery(`select json_unquote('"');`).Check(testkit.Events(`"`))
  4125  
  4126  	tk.MustQuery(`select json_unquote('""');`).Check(testkit.Events(``))
  4127  	tk.MustQuery(`select char_length(json_unquote('""'));`).Check(testkit.Events(`0`))
  4128  	tk.MustQuery(`select json_unquote('"" ');`).Check(testkit.Events(`"" `))
  4129  	tk.MustQuery(`select json_unquote(cast(json_quote('abc') as json));`).Check(testkit.Events("abc"))
  4130  
  4131  	tk.MustQuery(`select json_unquote(cast('{"abc": "foo"}' as json));`).Check(testkit.Events(`{"abc": "foo"}`))
  4132  	tk.MustQuery(`select json_unquote(json_extract(cast('{"abc": "foo"}' as json), '$.abc'));`).Check(testkit.Events("foo"))
  4133  	tk.MustQuery(`select json_unquote('["a", "b", "c"]');`).Check(testkit.Events(`["a", "b", "c"]`))
  4134  	tk.MustQuery(`select json_unquote(cast('["a", "b", "c"]' as json));`).Check(testkit.Events(`["a", "b", "c"]`))
  4135  	tk.MustQuery(`select json_quote(convert(X'e68891' using utf8));`).Check(testkit.Events(`"我"`))
  4136  	tk.MustQuery(`select json_quote(convert(X'e68891' using utf8mb4));`).Check(testkit.Events(`"我"`))
  4137  	tk.MustQuery(`select cast(json_quote(convert(X'e68891' using utf8)) as json);`).Check(testkit.Events(`"我"`))
  4138  	tk.MustQuery(`select json_unquote(convert(X'e68891' using utf8));`).Check(testkit.Events("我"))
  4139  
  4140  	tk.MustQuery(`select json_quote(json_quote(json_quote('abc')));`).Check(testkit.Events(`"\"\\\"abc\\\"\""`))
  4141  	tk.MustQuery(`select json_unquote(json_unquote(json_unquote(json_quote(json_quote(json_quote('abc'))))));`).Check(testkit.Events("abc"))
  4142  
  4143  	tk.MustGetErrCode("select json_quote(123)", allegrosql.ErrIncorrectType)
  4144  	tk.MustGetErrCode("select json_quote(-100)", allegrosql.ErrIncorrectType)
  4145  	tk.MustGetErrCode("select json_quote(123.123)", allegrosql.ErrIncorrectType)
  4146  	tk.MustGetErrCode("select json_quote(-100.000)", allegrosql.ErrIncorrectType)
  4147  	tk.MustGetErrCode(`select json_quote(true);`, allegrosql.ErrIncorrectType)
  4148  	tk.MustGetErrCode(`select json_quote(false);`, allegrosql.ErrIncorrectType)
  4149  	tk.MustGetErrCode(`select json_quote(cast("{}" as JSON));`, allegrosql.ErrIncorrectType)
  4150  	tk.MustGetErrCode(`select json_quote(cast("[]" as JSON));`, allegrosql.ErrIncorrectType)
  4151  	tk.MustGetErrCode(`select json_quote(cast("2020-07-29" as date));`, allegrosql.ErrIncorrectType)
  4152  	tk.MustGetErrCode(`select json_quote(cast("12:18:29.000000" as time));`, allegrosql.ErrIncorrectType)
  4153  	tk.MustGetErrCode(`select json_quote(cast("2020-07-29 12:18:29.000000" as datetime));`, allegrosql.ErrIncorrectType)
  4154  
  4155  	tk.MustGetErrCode("select json_unquote(123)", allegrosql.ErrIncorrectType)
  4156  	tk.MustGetErrCode("select json_unquote(-100)", allegrosql.ErrIncorrectType)
  4157  	tk.MustGetErrCode("select json_unquote(123.123)", allegrosql.ErrIncorrectType)
  4158  	tk.MustGetErrCode("select json_unquote(-100.000)", allegrosql.ErrIncorrectType)
  4159  	tk.MustGetErrCode(`select json_unquote(true);`, allegrosql.ErrIncorrectType)
  4160  	tk.MustGetErrCode(`select json_unquote(false);`, allegrosql.ErrIncorrectType)
  4161  	tk.MustGetErrCode(`select json_unquote(cast("2020-07-29" as date));`, allegrosql.ErrIncorrectType)
  4162  	tk.MustGetErrCode(`select json_unquote(cast("12:18:29.000000" as time));`, allegrosql.ErrIncorrectType)
  4163  	tk.MustGetErrCode(`select json_unquote(cast("2020-07-29 12:18:29.000000" as datetime));`, allegrosql.ErrIncorrectType)
  4164  
  4165  	r = tk.MustQuery(`select json_extract(a, '$.a[1]'), json_extract(b, '$.b') from block_json`)
  4166  	r.Check(testkit.Events("\"2\" true", "<nil> <nil>"))
  4167  
  4168  	r = tk.MustQuery(`select json_extract(json_set(a, '$.a[1]', 3), '$.a[1]'), json_extract(json_set(b, '$.b', false), '$.b') from block_json`)
  4169  	r.Check(testkit.Events("3 false", "<nil> <nil>"))
  4170  
  4171  	r = tk.MustQuery(`select json_extract(json_insert(a, '$.a[1]', 3), '$.a[1]'), json_extract(json_insert(b, '$.b', false), '$.b') from block_json`)
  4172  	r.Check(testkit.Events("\"2\" true", "<nil> <nil>"))
  4173  
  4174  	r = tk.MustQuery(`select json_extract(json_replace(a, '$.a[1]', 3), '$.a[1]'), json_extract(json_replace(b, '$.b', false), '$.b') from block_json`)
  4175  	r.Check(testkit.Events("3 false", "<nil> <nil>"))
  4176  
  4177  	r = tk.MustQuery(`select json_extract(json_merge(a, cast(b as JSON)), '$[0].a[0]') from block_json`)
  4178  	r.Check(testkit.Events("1", "1"))
  4179  
  4180  	r = tk.MustQuery(`select json_extract(json_array(1,2,3), '$[1]')`)
  4181  	r.Check(testkit.Events("2"))
  4182  
  4183  	r = tk.MustQuery(`select json_extract(json_object(1,2,3,4), '$."1"')`)
  4184  	r.Check(testkit.Events("2"))
  4185  
  4186  	tk.MustInterDirc(`uFIDelate block_json set a=json_set(a,'$.a',json_object('a',1,'b',2)) where json_extract(a,'$.a[1]') = '2'`)
  4187  	r = tk.MustQuery(`select json_extract(a, '$.a.a'), json_extract(a, '$.a.b') from block_json`)
  4188  	r.Check(testkit.Events("1 2", "<nil> <nil>"))
  4189  
  4190  	r = tk.MustQuery(`select json_contains(NULL, '1'), json_contains('1', NULL), json_contains('1', '1', NULL)`)
  4191  	r.Check(testkit.Events("<nil> <nil> <nil>"))
  4192  	r = tk.MustQuery(`select json_contains('{}','{}'), json_contains('[1]','1'), json_contains('[1]','"1"'), json_contains('[1,2,[1,[5,[3]]]]', '[1,3]', '$[2]'), json_contains('[1,2,[1,[5,{"a":[2,3]}]]]', '[1,{"a":[3]}]', "$[2]"), json_contains('{"a":1}', '{"a":1,"b":2}', "$")`)
  4193  	r.Check(testkit.Events("1 1 0 1 1 0"))
  4194  	r = tk.MustQuery(`select json_contains('{"a": 1}', '1', "$.c"), json_contains('{"a": [1, 2]}', '1', "$.a[2]"), json_contains('{"a": [1, {"a": 1}]}', '1', "$.a[1].b")`)
  4195  	r.Check(testkit.Events("<nil> <nil> <nil>"))
  4196  	rs, err := tk.InterDirc("select json_contains('1','1','$.*')")
  4197  	c.Assert(err, IsNil)
  4198  	c.Assert(rs, NotNil)
  4199  	_, err = stochastik.GetEvents4Test(context.Background(), tk.Se, rs)
  4200  	c.Assert(err, NotNil)
  4201  	c.Assert(err.Error(), Equals, "[json:3149]In this situation, path memexs may not contain the * and ** tokens.")
  4202  
  4203  	r = tk.MustQuery(`select
  4204  		json_contains_path(NULL, 'one', "$.c"),
  4205  		json_contains_path(NULL, 'all', "$.c"),
  4206  		json_contains_path('{"a": 1}', NULL, "$.c"),
  4207  		json_contains_path('{"a": 1}', 'one', NULL),
  4208  		json_contains_path('{"a": 1}', 'all', NULL)
  4209  	`)
  4210  	r.Check(testkit.Events("<nil> <nil> <nil> <nil> <nil>"))
  4211  
  4212  	r = tk.MustQuery(`select
  4213  		json_contains_path('{"a": 1, "b": 2, "c": {"d": 4}}', 'one', '$.c.d'),
  4214  		json_contains_path('{"a": 1, "b": 2, "c": {"d": 4}}', 'one', '$.a.d'),
  4215  		json_contains_path('{"a": 1, "b": 2, "c": {"d": 4}}', 'all', '$.c.d'),
  4216  		json_contains_path('{"a": 1, "b": 2, "c": {"d": 4}}', 'all', '$.a.d')
  4217  	`)
  4218  	r.Check(testkit.Events("1 0 1 0"))
  4219  
  4220  	r = tk.MustQuery(`select
  4221  		json_contains_path('{"a": 1, "b": 2, "c": {"d": 4}}', 'one', '$.a', '$.e'),
  4222  		json_contains_path('{"a": 1, "b": 2, "c": {"d": 4}}', 'one', '$.a', '$.b'),
  4223  		json_contains_path('{"a": 1, "b": 2, "c": {"d": 4}}', 'all', '$.a', '$.e'),
  4224  		json_contains_path('{"a": 1, "b": 2, "c": {"d": 4}}', 'all', '$.a', '$.b')
  4225  	`)
  4226  	r.Check(testkit.Events("1 1 0 1"))
  4227  
  4228  	r = tk.MustQuery(`select
  4229  		json_contains_path('{"a": 1, "b": 2, "c": {"d": 4}}', 'one', '$.*'),
  4230  		json_contains_path('{"a": 1, "b": 2, "c": {"d": 4}}', 'one', '$[*]'),
  4231  		json_contains_path('{"a": 1, "b": 2, "c": {"d": 4}}', 'all', '$.*'),
  4232  		json_contains_path('{"a": 1, "b": 2, "c": {"d": 4}}', 'all', '$[*]')
  4233  	`)
  4234  	r.Check(testkit.Events("1 0 1 0"))
  4235  
  4236  	r = tk.MustQuery(`select
  4237  		json_keys('[]'),
  4238  		json_keys('{}'),
  4239  		json_keys('{"a": 1, "b": 2}'),
  4240  		json_keys('{"a": {"c": 3}, "b": 2}'),
  4241  		json_keys('{"a": {"c": 3}, "b": 2}', "$.a")
  4242  	`)
  4243  	r.Check(testkit.Events(`<nil> [] ["a", "b"] ["a", "b"] ["c"]`))
  4244  
  4245  	r = tk.MustQuery(`select
  4246  		json_length('1'),
  4247  		json_length('{}'),
  4248  		json_length('[]'),
  4249  		json_length('{"a": 1}'),
  4250  		json_length('{"a": 1, "b": 2}'),
  4251  		json_length('[1, 2, 3]')
  4252  	`)
  4253  	r.Check(testkit.Events("1 0 0 1 2 3"))
  4254  
  4255  	// #16267
  4256  	tk.MustQuery(`select json_array(922337203685477580) =  json_array(922337203685477581);`).Check(testkit.Events("0"))
  4257  }
  4258  
  4259  func (s *testIntegrationSuite) TestDeferredCausetInfoModified(c *C) {
  4260  	testKit := testkit.NewTestKit(c, s.causetstore)
  4261  	defer s.cleanEnv(c)
  4262  	testKit.MustInterDirc("use test")
  4263  	testKit.MustInterDirc("drop causet if exists tab0")
  4264  	testKit.MustInterDirc("CREATE TABLE tab0(defCaus0 INTEGER, defCaus1 INTEGER, defCaus2 INTEGER)")
  4265  	testKit.MustInterDirc("SELECT + - (- CASE + defCaus0 WHEN + CAST( defCaus0 AS SIGNED ) THEN defCaus1 WHEN 79 THEN NULL WHEN + - defCaus1 THEN defCaus0 / + defCaus0 END ) * - 16 FROM tab0")
  4266  	ctx := testKit.Se.(stochastikctx.Context)
  4267  	is := petri.GetPetri(ctx).SchemaReplicant()
  4268  	tbl, _ := is.BlockByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("tab0"))
  4269  	defCaus := causet.FindDefCaus(tbl.DefCauss(), "defCaus1")
  4270  	c.Assert(defCaus.Tp, Equals, allegrosql.TypeLong)
  4271  }
  4272  
  4273  func (s *testIntegrationSuite) TestSetVariables(c *C) {
  4274  	tk := testkit.NewTestKit(c, s.causetstore)
  4275  	defer s.cleanEnv(c)
  4276  	_, err := tk.InterDirc("set sql_mode='adfasdfadsfdasd';")
  4277  	c.Assert(err, NotNil)
  4278  	_, err = tk.InterDirc("set @@sql_mode='adfasdfadsfdasd';")
  4279  	c.Assert(err, NotNil)
  4280  	_, err = tk.InterDirc("set @@global.sql_mode='adfasdfadsfdasd';")
  4281  	c.Assert(err, NotNil)
  4282  	_, err = tk.InterDirc("set @@stochastik.sql_mode='adfasdfadsfdasd';")
  4283  	c.Assert(err, NotNil)
  4284  
  4285  	var r *testkit.Result
  4286  	_, err = tk.InterDirc("set @@stochastik.sql_mode=',NO_ZERO_DATE,ANSI,ANSI_QUOTES';")
  4287  	c.Assert(err, IsNil)
  4288  	r = tk.MustQuery(`select @@stochastik.sql_mode`)
  4289  	r.Check(testkit.Events("NO_ZERO_DATE,REAL_AS_FLOAT,PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE,ONLY_FULL_GROUP_BY,ANSI"))
  4290  	r = tk.MustQuery(`show variables like 'sql_mode'`)
  4291  	r.Check(testkit.Events("sql_mode NO_ZERO_DATE,REAL_AS_FLOAT,PIPES_AS_CONCAT,ANSI_QUOTES,IGNORE_SPACE,ONLY_FULL_GROUP_BY,ANSI"))
  4292  
  4293  	// for invalid ALLEGROALLEGROSQL mode.
  4294  	tk.MustInterDirc("use test")
  4295  	tk.MustInterDirc("drop causet if exists tab0")
  4296  	tk.MustInterDirc("CREATE TABLE tab0(defCaus1 time)")
  4297  	_, err = tk.InterDirc("set sql_mode='STRICT_TRANS_TABLES';")
  4298  	c.Assert(err, IsNil)
  4299  	_, err = tk.InterDirc("INSERT INTO tab0 select cast('999:44:33' as time);")
  4300  	c.Assert(err, NotNil)
  4301  	c.Assert(err.Error(), Equals, "[types:1292]Truncated incorrect time value: '999:44:33'")
  4302  	_, err = tk.InterDirc("set sql_mode=' ,';")
  4303  	c.Assert(err, NotNil)
  4304  	_, err = tk.InterDirc("INSERT INTO tab0 select cast('999:44:33' as time);")
  4305  	c.Assert(err, NotNil)
  4306  	c.Assert(err.Error(), Equals, "[types:1292]Truncated incorrect time value: '999:44:33'")
  4307  
  4308  	// issue #5478
  4309  	_, err = tk.InterDirc("set stochastik transaction read write;")
  4310  	c.Assert(err, IsNil)
  4311  	_, err = tk.InterDirc("set global transaction read write;")
  4312  	c.Assert(err, IsNil)
  4313  	r = tk.MustQuery(`select @@stochastik.tx_read_only, @@global.tx_read_only, @@stochastik.transaction_read_only, @@global.transaction_read_only;`)
  4314  	r.Check(testkit.Events("0 0 0 0"))
  4315  
  4316  	_, err = tk.InterDirc("set stochastik transaction read only;")
  4317  	c.Assert(err, IsNil)
  4318  	r = tk.MustQuery(`select @@stochastik.tx_read_only, @@global.tx_read_only, @@stochastik.transaction_read_only, @@global.transaction_read_only;`)
  4319  	r.Check(testkit.Events("1 0 1 0"))
  4320  	_, err = tk.InterDirc("set global transaction read only;")
  4321  	c.Assert(err, IsNil)
  4322  	r = tk.MustQuery(`select @@stochastik.tx_read_only, @@global.tx_read_only, @@stochastik.transaction_read_only, @@global.transaction_read_only;`)
  4323  	r.Check(testkit.Events("1 1 1 1"))
  4324  
  4325  	_, err = tk.InterDirc("set stochastik transaction read write;")
  4326  	c.Assert(err, IsNil)
  4327  	_, err = tk.InterDirc("set global transaction read write;")
  4328  	c.Assert(err, IsNil)
  4329  	r = tk.MustQuery(`select @@stochastik.tx_read_only, @@global.tx_read_only, @@stochastik.transaction_read_only, @@global.transaction_read_only;`)
  4330  	r.Check(testkit.Events("0 0 0 0"))
  4331  
  4332  	_, err = tk.InterDirc("set @@global.max_user_connections='';")
  4333  	c.Assert(err, NotNil)
  4334  	c.Assert(err.Error(), Equals, variable.ErrWrongTypeForVar.GenWithStackByArgs("max_user_connections").Error())
  4335  	_, err = tk.InterDirc("set @@global.max_prepared_stmt_count='';")
  4336  	c.Assert(err, NotNil)
  4337  	c.Assert(err.Error(), Equals, variable.ErrWrongTypeForVar.GenWithStackByArgs("max_prepared_stmt_count").Error())
  4338  }
  4339  
  4340  func (s *testIntegrationSuite) TestIssues(c *C) {
  4341  	// for issue #4954
  4342  	tk := testkit.NewTestKit(c, s.causetstore)
  4343  	defer s.cleanEnv(c)
  4344  	tk.MustInterDirc("use test")
  4345  	tk.MustInterDirc("drop causet if exists t")
  4346  	tk.MustInterDirc("CREATE TABLE t (a CHAR(5) CHARACTER SET latin1);")
  4347  	tk.MustInterDirc("INSERT INTO t VALUES ('oe');")
  4348  	tk.MustInterDirc("INSERT INTO t VALUES (0xf6);")
  4349  	r := tk.MustQuery(`SELECT * FROM t WHERE a= 'oe';`)
  4350  	r.Check(testkit.Events("oe"))
  4351  	r = tk.MustQuery(`SELECT HEX(a) FROM t WHERE a= 0xf6;`)
  4352  	r.Check(testkit.Events("F6"))
  4353  
  4354  	// for issue #4006
  4355  	tk.MustInterDirc(`drop causet if exists tb`)
  4356  	tk.MustInterDirc("create causet tb(id int auto_increment primary key, v varchar(32));")
  4357  	tk.MustInterDirc("insert into tb(v) (select v from tb);")
  4358  	r = tk.MustQuery(`SELECT * FROM tb;`)
  4359  	r.Check(testkit.Events())
  4360  	tk.MustInterDirc(`insert into tb(v) values('hello');`)
  4361  	tk.MustInterDirc("insert into tb(v) (select v from tb);")
  4362  	r = tk.MustQuery(`SELECT * FROM tb;`)
  4363  	r.Check(testkit.Events("1 hello", "2 hello"))
  4364  
  4365  	// for issue #5111
  4366  	tk.MustInterDirc(`drop causet if exists t`)
  4367  	tk.MustInterDirc("create causet t(c varchar(32));")
  4368  	tk.MustInterDirc("insert into t values('1e649'),('-1e649');")
  4369  	r = tk.MustQuery(`SELECT * FROM t where c < 1;`)
  4370  	r.Check(testkit.Events("-1e649"))
  4371  	tk.MustQuery("show warnings").Check(solitonutil.EventsWithSep("|",
  4372  		"Warning|1292|Truncated incorrect DOUBLE value: '1e649'",
  4373  		"Warning|1292|Truncated incorrect DOUBLE value: '-1e649'"))
  4374  	r = tk.MustQuery(`SELECT * FROM t where c > 1;`)
  4375  	r.Check(testkit.Events("1e649"))
  4376  	tk.MustQuery("show warnings").Check(solitonutil.EventsWithSep("|",
  4377  		"Warning|1292|Truncated incorrect DOUBLE value: '1e649'",
  4378  		"Warning|1292|Truncated incorrect DOUBLE value: '-1e649'"))
  4379  
  4380  	// for issue #5293
  4381  	tk.MustInterDirc("drop causet if exists t")
  4382  	tk.MustInterDirc("create causet t(a int)")
  4383  	tk.MustInterDirc("insert t values (1)")
  4384  	tk.MustQuery("select * from t where cast(a as binary)").Check(testkit.Events("1"))
  4385  
  4386  	// for issue #16351
  4387  	tk.MustInterDirc("drop causet if exists t2")
  4388  	tk.MustInterDirc("create causet t2(a int, b varchar(20))")
  4389  	tk.MustInterDirc(`insert into t2 values(1,"1111"),(2,"2222"),(3,"3333"),(4,"4444"),(5,"5555"),(6,"6666"),(7,"7777"),(8,"8888"),(9,"9999"),(10,"0000")`)
  4390  	tk.MustQuery(`select (@j := case when substr(t2.b,1,3)=@i then 1 else @j+1 end) from t2, (select @j := 0, @i := "0") tt limit 10`).Check(testkit.Events(
  4391  		"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"))
  4392  }
  4393  
  4394  func (s *testIntegrationSuite) TestInPredicate4UnsignedInt(c *C) {
  4395  	// for issue #6661
  4396  	tk := testkit.NewTestKit(c, s.causetstore)
  4397  	defer s.cleanEnv(c)
  4398  	tk.MustInterDirc("use test")
  4399  	tk.MustInterDirc("drop causet if exists t")
  4400  	tk.MustInterDirc("CREATE TABLE t (a bigint unsigned,key (a));")
  4401  	tk.MustInterDirc("INSERT INTO t VALUES (0), (4), (5), (6), (7), (8), (9223372036854775810), (18446744073709551614), (18446744073709551615);")
  4402  	r := tk.MustQuery(`SELECT a FROM t WHERE a NOT IN (-1, -2, 18446744073709551615);`)
  4403  	r.Check(testkit.Events("0", "4", "5", "6", "7", "8", "9223372036854775810", "18446744073709551614"))
  4404  	r = tk.MustQuery(`SELECT a FROM t WHERE a NOT IN (-1, -2, 4, 9223372036854775810);`)
  4405  	r.Check(testkit.Events("0", "5", "6", "7", "8", "18446744073709551614", "18446744073709551615"))
  4406  	r = tk.MustQuery(`SELECT a FROM t WHERE a NOT IN (-1, -2, 0, 4, 18446744073709551614);`)
  4407  	r.Check(testkit.Events("5", "6", "7", "8", "9223372036854775810", "18446744073709551615"))
  4408  
  4409  	// for issue #4473
  4410  	tk.MustInterDirc("drop causet if exists t")
  4411  	tk.MustInterDirc("create causet t1 (some_id smallint(5) unsigned,key (some_id) )")
  4412  	tk.MustInterDirc("insert into t1 values (1),(2)")
  4413  	r = tk.MustQuery(`select some_id from t1 where some_id not in(2,-1);`)
  4414  	r.Check(testkit.Events("1"))
  4415  }
  4416  
  4417  func (s *testIntegrationSuite) TestFilterExtractFromDNF(c *C) {
  4418  	tk := testkit.NewTestKit(c, s.causetstore)
  4419  	defer s.cleanEnv(c)
  4420  	tk.MustInterDirc("use test")
  4421  	tk.MustInterDirc("drop causet if exists t")
  4422  	tk.MustInterDirc("create causet t(a int, b int, c int)")
  4423  
  4424  	tests := []struct {
  4425  		exprStr string
  4426  		result  string
  4427  	}{
  4428  		{
  4429  			exprStr: "a = 1 or a = 1 or a = 1",
  4430  			result:  "[eq(test.t.a, 1)]",
  4431  		},
  4432  		{
  4433  			exprStr: "a = 1 or a = 1 or (a = 1 and b = 1)",
  4434  			result:  "[eq(test.t.a, 1)]",
  4435  		},
  4436  		{
  4437  			exprStr: "(a = 1 and a = 1) or a = 1 or b = 1",
  4438  			result:  "[or(or(and(eq(test.t.a, 1), eq(test.t.a, 1)), eq(test.t.a, 1)), eq(test.t.b, 1))]",
  4439  		},
  4440  		{
  4441  			exprStr: "(a = 1 and b = 2) or (a = 1 and b = 3) or (a = 1 and b = 4)",
  4442  			result:  "[eq(test.t.a, 1) or(eq(test.t.b, 2), or(eq(test.t.b, 3), eq(test.t.b, 4)))]",
  4443  		},
  4444  		{
  4445  			exprStr: "(a = 1 and b = 1 and c = 1) or (a = 1 and b = 1) or (a = 1 and b = 1 and c > 2 and c < 3)",
  4446  			result:  "[eq(test.t.a, 1) eq(test.t.b, 1)]",
  4447  		},
  4448  	}
  4449  
  4450  	ctx := context.Background()
  4451  	for _, tt := range tests {
  4452  		allegrosql := "select * from t where " + tt.exprStr
  4453  		sctx := tk.Se.(stochastikctx.Context)
  4454  		sc := sctx.GetStochastikVars().StmtCtx
  4455  		stmts, err := stochastik.Parse(sctx, allegrosql)
  4456  		c.Assert(err, IsNil, Commentf("error %v, for expr %s", err, tt.exprStr))
  4457  		c.Assert(stmts, HasLen, 1)
  4458  		is := petri.GetPetri(sctx).SchemaReplicant()
  4459  		err = causetembedded.Preprocess(sctx, stmts[0], is)
  4460  		c.Assert(err, IsNil, Commentf("error %v, for resolve name, expr %s", err, tt.exprStr))
  4461  		p, _, err := causetembedded.BuildLogicalCauset(ctx, sctx, stmts[0], is)
  4462  		c.Assert(err, IsNil, Commentf("error %v, for build plan, expr %s", err, tt.exprStr))
  4463  		selection := p.(causetembedded.LogicalCauset).Children()[0].(*causetembedded.LogicalSelection)
  4464  		conds := make([]memex.Expression, len(selection.Conditions))
  4465  		for i, cond := range selection.Conditions {
  4466  			conds[i] = memex.PushDownNot(sctx, cond)
  4467  		}
  4468  		afterFunc := memex.ExtractFiltersFromDNFs(sctx, conds)
  4469  		sort.Slice(afterFunc, func(i, j int) bool {
  4470  			return bytes.Compare(afterFunc[i].HashCode(sc), afterFunc[j].HashCode(sc)) < 0
  4471  		})
  4472  		c.Assert(fmt.Sprintf("%s", afterFunc), Equals, tt.result, Commentf("wrong result for expr: %s", tt.exprStr))
  4473  	}
  4474  }
  4475  
  4476  func (s *testIntegrationSuite) testMilevaDBIsTenantFunc(c *C) {
  4477  	tk := testkit.NewTestKit(c, s.causetstore)
  4478  	defer s.cleanEnv(c)
  4479  	result := tk.MustQuery("select milevadb_is_dbs_tenant()")
  4480  	dbsTenantChecker := tk.Se.DBSTenantChecker()
  4481  	c.Assert(dbsTenantChecker, NotNil)
  4482  	var ret int64
  4483  	if dbsTenantChecker.IsTenant() {
  4484  		ret = 1
  4485  	}
  4486  	result.Check(testkit.Events(fmt.Sprintf("%v", ret)))
  4487  }
  4488  
  4489  func (s *testIntegrationSuite) TestMilevaDBDecodeCausetFunc(c *C) {
  4490  	tk := testkit.NewTestKit(c, s.causetstore)
  4491  	defer s.cleanEnv(c)
  4492  	tk.MustQuery("select milevadb_decode_plan('')").Check(testkit.Events(""))
  4493  	tk.MustQuery("select milevadb_decode_plan('7APIMAk1XzEzCTAJMQlmdW5jczpjb3VudCgxKQoxCTE3XzE0CTAJMAlpbm5lciBqb2luLCBp" +
  4494  		"AQyQOlRhYmxlUmVhZGVyXzIxLCBlcXVhbDpbZXEoQ29sdW1uIzEsIA0KCDkpIBkXADIVFywxMCldCjIJMzFfMTgFZXhkYXRhOlNlbGVjdGlvbl" +
  4495  		"8xNwozCTFfMTcJMQkwCWx0HVlATlVMTCksIG5vdChpc251bGwVHAApUhcAUDIpKQo0CTEwXzE2CTEJMTAwMDAJdAHB2Dp0MSwgcmFuZ2U6Wy1p" +
  4496  		"bmYsK2luZl0sIGtlZXAgb3JkZXI6ZmFsc2UsIHN0YXRzOnBzZXVkbwoFtgAyAZcEMAk6tgAEMjAFtgQyMDq2AAg5LCBmtgAAMFa3AAA5FbcAO" +
  4497  		"T63AAAyzrcA')").Check(testkit.Events("" +
  4498  		"\tid                  \ttask\testEvents\toperator info\n" +
  4499  		"\tStreamAgg_13        \troot\t1      \tfuncs:count(1)\n" +
  4500  		"\t└─HashJoin_14       \troot\t0      \tinner join, inner:BlockReader_21, equal:[eq(DeferredCauset#1, DeferredCauset#9) eq(DeferredCauset#2, DeferredCauset#10)]\n" +
  4501  		"\t  ├─BlockReader_18  \troot\t0      \tdata:Selection_17\n" +
  4502  		"\t  │ └─Selection_17  \tcop \t0      \tlt(DeferredCauset#1, NULL), not(isnull(DeferredCauset#1)), not(isnull(DeferredCauset#2))\n" +
  4503  		"\t  │   └─BlockScan_16\tcop \t10000  \tcauset:t1, range:[-inf,+inf], keep order:false, stats:pseudo\n" +
  4504  		"\t  └─BlockReader_21  \troot\t0      \tdata:Selection_20\n" +
  4505  		"\t    └─Selection_20  \tcop \t0      \tlt(DeferredCauset#9, NULL), not(isnull(DeferredCauset#10)), not(isnull(DeferredCauset#9))\n" +
  4506  		"\t      └─BlockScan_19\tcop \t10000  \tcauset:t2, range:[-inf,+inf], keep order:false, stats:pseudo"))
  4507  	tk.MustQuery("select milevadb_decode_plan('rwPwcTAJNV8xNAkwCTEJZnVuY3M6bWF4KHRlc3QudC5hKS0+Q29sdW1uIzQJMQl0aW1lOj" +
  4508  		"IyMy45MzXCtXMsIGxvb3BzOjIJMTI4IEJ5dGVzCU4vQQoxCTE2XzE4CTAJMQlvZmZzZXQ6MCwgY291bnQ6MQkxCQlHFDE4LjQyMjJHAAhOL0" +
  4509  		"EBBCAKMgkzMl8yOAkBlEBpbmRleDpMaW1FIDelF8yNwkxCQ0+DDYuODUdPSwxLCBycGMgbnVtOiANDAUpGDE1MC44MjQFKjhwcm9jIGtleXM6MA" +
  4510  		"kxOTgdsgAzAbIAMgFearIAFDU3LjM5NgVKAGwN+BGxIDQJMTNfMjYJMQGgHGFibGU6dCwgCbqwaWR4KGEpLCByYW5nZTooMCwraW5mXSwga2" +
  4511  		"VlcCBvcmRlcjp0cnVlLCBkZXNjAT8kaW1lOjU2LjY2MR1rJDEJTi9BCU4vQQo=')").Check(testkit.Events("" +
  4512  		"\tid                  \ttask\testEvents\toperator info                                               \tactEvents\texecution info                                                       \tmemory   \tdisk\n" +
  4513  		"\tStreamAgg_14        \troot\t1      \tfuncs:max(test.t.a)->DeferredCauset#4                               \t1      \ttime:223.935µs, loops:2                                             \t128 Bytes\tN/A\n" +
  4514  		"\t└─Limit_18          \troot\t1      \toffset:0, count:1                                           \t1      \ttime:218.422µs, loops:2                                             \tN/A      \tN/A\n" +
  4515  		"\t  └─IndexReader_28  \troot\t1      \tindex:Limit_27                                              \t1      \ttime:216.85µs, loops:1, rpc num: 1, rpc time:150.824µs, proc keys:0\t198 Bytes\tN/A\n" +
  4516  		"\t    └─Limit_27      \tcop \t1      \toffset:0, count:1                                           \t1      \ttime:57.396µs, loops:2                                              \tN/A      \tN/A\n" +
  4517  		"\t      └─IndexScan_26\tcop \t1      \tcauset:t, index:idx(a), range:(0,+inf], keep order:true, desc\t1      \ttime:56.661µs, loops:1                                              \tN/A      \tN/A"))
  4518  }
  4519  
  4520  func (s *testIntegrationSuite) TestMilevaDBInternalFunc(c *C) {
  4521  	tk := testkit.NewTestKit(c, s.causetstore)
  4522  	defer s.cleanEnv(c)
  4523  	var result *testkit.Result
  4524  	result = tk.MustQuery("select milevadb_decode_key( '74800000000000002B5F72800000000000A5D3' )")
  4525  	result.Check(testkit.Events("blockID=43, _milevadb_rowid=42451"))
  4526  	result = tk.MustQuery("select milevadb_decode_key( '7480000000000000325f7205bff199999999999a013131000000000000f9' )")
  4527  	result.Check(testkit.Events("blockID=50, clusterHandle={1.1, 11}"))
  4528  
  4529  	result = tk.MustQuery("select milevadb_decode_key( '74800000000000019B5F698000000000000001015257303100000000FB013736383232313130FF3900000000000000F8010000000000000000F7' )")
  4530  	result.Check(testkit.Events("blockID=411, indexID=1, indexValues=RW01,768221109,"))
  4531  	result = tk.MustQuery("select milevadb_decode_key( '7480000000000000695F698000000000000001038000000000004E20' )")
  4532  	result.Check(testkit.Events("blockID=105, indexID=1, indexValues=20000"))
  4533  
  4534  	// Test invalid record/index key.
  4535  	result = tk.MustQuery("select milevadb_decode_key( '7480000000000000FF2E5F728000000011FFE1A3000000000000' )")
  4536  	result.Check(testkit.Events("7480000000000000FF2E5F728000000011FFE1A3000000000000"))
  4537  	warns := tk.Se.GetStochastikVars().StmtCtx.GetWarnings()
  4538  	c.Assert(warns, HasLen, 1)
  4539  	c.Assert(warns[0].Err.Error(), Equals, "invalid record/index key: 7480000000000000FF2E5F728000000011FFE1A3000000000000")
  4540  }
  4541  
  4542  func newStoreWithBootstrap() (ekv.CausetStorage, *petri.Petri, error) {
  4543  	causetstore, err := mockstore.NewMockStore()
  4544  	if err != nil {
  4545  		return nil, nil, err
  4546  	}
  4547  	stochastik.SetSchemaLease(0)
  4548  	dom, err := stochastik.BootstrapStochastik(causetstore)
  4549  	return causetstore, dom, err
  4550  }
  4551  
  4552  func (s *testIntegrationSuite) TestTwoDecimalTruncate(c *C) {
  4553  	tk := testkit.NewTestKit(c, s.causetstore)
  4554  	defer s.cleanEnv(c)
  4555  	tk.MustInterDirc("use test")
  4556  	tk.MustInterDirc("set sql_mode=''")
  4557  	tk.MustInterDirc("drop causet if exists t")
  4558  	tk.MustInterDirc("create causet t1(a decimal(10,5), b decimal(10,1))")
  4559  	tk.MustInterDirc("insert into t1 values(123.12345, 123.12345)")
  4560  	tk.MustInterDirc("uFIDelate t1 set b = a")
  4561  	res := tk.MustQuery("select a, b from t1")
  4562  	res.Check(testkit.Events("123.12345 123.1"))
  4563  	res = tk.MustQuery("select 2.00000000000000000000000000000001 * 1.000000000000000000000000000000000000000000002")
  4564  	res.Check(testkit.Events("2.000000000000000000000000000000"))
  4565  }
  4566  
  4567  func (s *testIntegrationSuite) TestPrefixIndex(c *C) {
  4568  	tk := testkit.NewTestKit(c, s.causetstore)
  4569  	defer s.cleanEnv(c)
  4570  	tk.MustInterDirc("use test")
  4571  	tk.MustInterDirc(`CREATE TABLE t1 (
  4572    			name varchar(12) DEFAULT NULL,
  4573    			KEY pname (name(12))
  4574  		) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci`)
  4575  
  4576  	tk.MustInterDirc("insert into t1 values('借款策略集_网页');")
  4577  	res := tk.MustQuery("select * from t1 where name = '借款策略集_网页';")
  4578  	res.Check(testkit.Events("借款策略集_网页"))
  4579  
  4580  	tk.MustInterDirc(`CREATE TABLE prefix (
  4581  		a int(11) NOT NULL,
  4582  		b varchar(55) DEFAULT NULL,
  4583  		c int(11) DEFAULT NULL,
  4584  		PRIMARY KEY (a),
  4585  		KEY prefix_index (b(2)),
  4586  		KEY prefix_complex (a,b(2))
  4587  	) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;`)
  4588  
  4589  	tk.MustInterDirc("INSERT INTO prefix VALUES(0, 'b', 2), (1, 'bbb', 3), (2, 'bbc', 4), (3, 'bbb', 5), (4, 'abc', 6), (5, 'abc', 7), (6, 'abc', 7), (7, 'ÿÿ', 8), (8, 'ÿÿ0', 9), (9, 'ÿÿÿ', 10);")
  4590  	res = tk.MustQuery("select c, b from prefix where b > 'ÿ' and b < 'ÿÿc'")
  4591  	res.Check(testkit.Events("8 ÿÿ", "9 ÿÿ0"))
  4592  
  4593  	res = tk.MustQuery("select a, b from prefix where b LIKE 'ÿÿ%'")
  4594  	res.Check(testkit.Events("7 ÿÿ", "8 ÿÿ0", "9 ÿÿÿ"))
  4595  }
  4596  
  4597  func (s *testIntegrationSuite) TestDecimalMul(c *C) {
  4598  	tk := testkit.NewTestKit(c, s.causetstore)
  4599  	tk.MustInterDirc("USE test")
  4600  	tk.MustInterDirc("create causet t(a decimal(38, 17));")
  4601  	tk.MustInterDirc("insert into t select 0.5999991229316*0.918755041726043;")
  4602  	res := tk.MustQuery("select * from t;")
  4603  	res.Check(testkit.Events("0.55125221922461136"))
  4604  }
  4605  
  4606  func (s *testIntegrationSuite) TestDecimalDiv(c *C) {
  4607  	tk := testkit.NewTestKit(c, s.causetstore)
  4608  	tk.MustQuery("select cast(1 as decimal(60,30)) / cast(1 as decimal(60,30)) / cast(1 as decimal(60, 30))").Check(testkit.Events("1.000000000000000000000000000000"))
  4609  	tk.MustQuery("select cast(1 as decimal(60,30)) / cast(3 as decimal(60,30)) / cast(7 as decimal(60, 30))").Check(testkit.Events("0.047619047619047619047619047619"))
  4610  	tk.MustQuery("select cast(1 as decimal(60,30)) / cast(3 as decimal(60,30)) / cast(7 as decimal(60, 30)) / cast(13 as decimal(60, 30))").Check(testkit.Events("0.003663003663003663003663003663"))
  4611  }
  4612  
  4613  func (s *testIntegrationSuite) TestUnknowHintIgnore(c *C) {
  4614  	tk := testkit.NewTestKit(c, s.causetstore)
  4615  	tk.MustInterDirc("USE test")
  4616  	tk.MustInterDirc("create causet t(a int)")
  4617  	tk.MustQuery("select /*+ unknown_hint(c1)*/ 1").Check(testkit.Events("1"))
  4618  	tk.MustQuery("show warnings").Check(testkit.Events("Warning 1064 You have an error in your ALLEGROALLEGROSQL syntax; check the manual that corresponds to your MilevaDB version for the right syntax to use [BerolinaSQL:8064]Optimizer hint syntax error at line 1 defCausumn 23 near \"unknown_hint(c1)*/\" "))
  4619  	_, err := tk.InterDirc("select 1 from /*+ test1() */ t")
  4620  	c.Assert(err, IsNil)
  4621  }
  4622  
  4623  func (s *testIntegrationSuite) TestValuesInNonInsertStmt(c *C) {
  4624  	tk := testkit.NewTestKit(c, s.causetstore)
  4625  	tk.MustInterDirc(`use test;`)
  4626  	tk.MustInterDirc(`drop causet if exists t;`)
  4627  	tk.MustInterDirc(`create causet t(a bigint, b double, c decimal, d varchar(20), e datetime, f time, g json);`)
  4628  	tk.MustInterDirc(`insert into t values(1, 1.1, 2.2, "abc", "2020-10-24", NOW(), "12");`)
  4629  	res := tk.MustQuery(`select values(a), values(b), values(c), values(d), values(e), values(f), values(g) from t;`)
  4630  	res.Check(testkit.Events(`<nil> <nil> <nil> <nil> <nil> <nil> <nil>`))
  4631  }
  4632  
  4633  func (s *testIntegrationSuite) TestForeignKeyVar(c *C) {
  4634  
  4635  	tk := testkit.NewTestKit(c, s.causetstore)
  4636  
  4637  	tk.MustInterDirc("SET FOREIGN_KEY_CHECKS=1")
  4638  	tk.MustQuery("SHOW WARNINGS").Check(testkit.Events("Warning 8047 variable 'foreign_key_checks' does not yet support value: 1"))
  4639  }
  4640  
  4641  func (s *testIntegrationSuite) TestUserVarMockWindFunc(c *C) {
  4642  	tk := testkit.NewTestKit(c, s.causetstore)
  4643  	tk.MustInterDirc(`use test;`)
  4644  	tk.MustInterDirc(`drop causet if exists t;`)
  4645  	tk.MustInterDirc(`create causet t (a int, b varchar (20), c varchar (20));`)
  4646  	tk.MustInterDirc(`insert into t values
  4647  					(1,'key1-value1','insert_order1'),
  4648      				(1,'key1-value2','insert_order2'),
  4649      				(1,'key1-value3','insert_order3'),
  4650      				(1,'key1-value4','insert_order4'),
  4651      				(1,'key1-value5','insert_order5'),
  4652      				(1,'key1-value6','insert_order6'),
  4653      				(2,'key2-value1','insert_order1'),
  4654      				(2,'key2-value2','insert_order2'),
  4655      				(2,'key2-value3','insert_order3'),
  4656      				(2,'key2-value4','insert_order4'),
  4657      				(2,'key2-value5','insert_order5'),
  4658      				(2,'key2-value6','insert_order6'),
  4659      				(3,'key3-value1','insert_order1'),
  4660      				(3,'key3-value2','insert_order2'),
  4661      				(3,'key3-value3','insert_order3'),
  4662      				(3,'key3-value4','insert_order4'),
  4663      				(3,'key3-value5','insert_order5'),
  4664      				(3,'key3-value6','insert_order6');
  4665  					`)
  4666  	tk.MustInterDirc(`SET @LAST_VAL := NULL;`)
  4667  	tk.MustInterDirc(`SET @ROW_NUM := 0;`)
  4668  
  4669  	tk.MustQuery(`select * from (
  4670  					SELECT a,
  4671      				       @ROW_NUM := IF(a = @LAST_VAL, @ROW_NUM + 1, 1) AS ROW_NUM,
  4672      				       @LAST_VAL := a AS LAST_VAL,
  4673      				       b,
  4674      				       c
  4675      				FROM (select * from t where a in (1, 2, 3) ORDER BY a, c) t1
  4676  				) t2
  4677  				where t2.ROW_NUM < 2;
  4678  				`).Check(testkit.Events(
  4679  		`1 1 1 key1-value1 insert_order1`,
  4680  		`2 1 2 key2-value1 insert_order1`,
  4681  		`3 1 3 key3-value1 insert_order1`,
  4682  	))
  4683  
  4684  	tk.MustQuery(`select * from (
  4685  					SELECT a,
  4686      				       @ROW_NUM := IF(a = @LAST_VAL, @ROW_NUM + 1, 1) AS ROW_NUM,
  4687      				       @LAST_VAL := a AS LAST_VAL,
  4688      				       b,
  4689      				       c
  4690      				FROM (select * from t where a in (1, 2, 3) ORDER BY a, c) t1
  4691  				) t2;
  4692  				`).Check(testkit.Events(
  4693  		`1 1 1 key1-value1 insert_order1`,
  4694  		`1 2 1 key1-value2 insert_order2`,
  4695  		`1 3 1 key1-value3 insert_order3`,
  4696  		`1 4 1 key1-value4 insert_order4`,
  4697  		`1 5 1 key1-value5 insert_order5`,
  4698  		`1 6 1 key1-value6 insert_order6`,
  4699  		`2 1 2 key2-value1 insert_order1`,
  4700  		`2 2 2 key2-value2 insert_order2`,
  4701  		`2 3 2 key2-value3 insert_order3`,
  4702  		`2 4 2 key2-value4 insert_order4`,
  4703  		`2 5 2 key2-value5 insert_order5`,
  4704  		`2 6 2 key2-value6 insert_order6`,
  4705  		`3 1 3 key3-value1 insert_order1`,
  4706  		`3 2 3 key3-value2 insert_order2`,
  4707  		`3 3 3 key3-value3 insert_order3`,
  4708  		`3 4 3 key3-value4 insert_order4`,
  4709  		`3 5 3 key3-value5 insert_order5`,
  4710  		`3 6 3 key3-value6 insert_order6`,
  4711  	))
  4712  }
  4713  
  4714  func (s *testIntegrationSuite) TestCastAsTime(c *C) {
  4715  	tk := testkit.NewTestKit(c, s.causetstore)
  4716  	tk.MustInterDirc(`use test;`)
  4717  	tk.MustInterDirc(`drop causet if exists t;`)
  4718  	tk.MustInterDirc(`create causet t (defCaus1 bigint, defCaus2 double, defCaus3 decimal, defCaus4 varchar(20), defCaus5 json);`)
  4719  	tk.MustInterDirc(`insert into t values (1, 1, 1, "1", "1");`)
  4720  	tk.MustInterDirc(`insert into t values (null, null, null, null, null);`)
  4721  	tk.MustQuery(`select cast(defCaus1 as time), cast(defCaus2 as time), cast(defCaus3 as time), cast(defCaus4 as time), cast(defCaus5 as time) from t where defCaus1 = 1;`).Check(testkit.Events(
  4722  		`00:00:01 00:00:01 00:00:01 00:00:01 00:00:01`,
  4723  	))
  4724  	tk.MustQuery(`select cast(defCaus1 as time), cast(defCaus2 as time), cast(defCaus3 as time), cast(defCaus4 as time), cast(defCaus5 as time) from t where defCaus1 is null;`).Check(testkit.Events(
  4725  		`<nil> <nil> <nil> <nil> <nil>`,
  4726  	))
  4727  
  4728  	err := tk.InterDircToErr(`select cast(defCaus1 as time(31)) from t where defCaus1 is null;`)
  4729  	c.Assert(err.Error(), Equals, "[memex:1426]Too big precision 31 specified for defCausumn 'CAST'. Maximum is 6.")
  4730  
  4731  	err = tk.InterDircToErr(`select cast(defCaus2 as time(31)) from t where defCaus1 is null;`)
  4732  	c.Assert(err.Error(), Equals, "[memex:1426]Too big precision 31 specified for defCausumn 'CAST'. Maximum is 6.")
  4733  
  4734  	err = tk.InterDircToErr(`select cast(defCaus3 as time(31)) from t where defCaus1 is null;`)
  4735  	c.Assert(err.Error(), Equals, "[memex:1426]Too big precision 31 specified for defCausumn 'CAST'. Maximum is 6.")
  4736  
  4737  	err = tk.InterDircToErr(`select cast(defCaus4 as time(31)) from t where defCaus1 is null;`)
  4738  	c.Assert(err.Error(), Equals, "[memex:1426]Too big precision 31 specified for defCausumn 'CAST'. Maximum is 6.")
  4739  
  4740  	err = tk.InterDircToErr(`select cast(defCaus5 as time(31)) from t where defCaus1 is null;`)
  4741  	c.Assert(err.Error(), Equals, "[memex:1426]Too big precision 31 specified for defCausumn 'CAST'. Maximum is 6.")
  4742  }
  4743  
  4744  func (s *testIntegrationSuite) TestValuesFloat32(c *C) {
  4745  	tk := testkit.NewTestKit(c, s.causetstore)
  4746  	tk.MustInterDirc("use test")
  4747  	tk.MustInterDirc(`drop causet if exists t;`)
  4748  	tk.MustInterDirc(`create causet t (i int key, j float);`)
  4749  	tk.MustInterDirc(`insert into t values (1, 0.01);`)
  4750  	tk.MustQuery(`select * from t;`).Check(testkit.Events(`1 0.01`))
  4751  	tk.MustInterDirc(`insert into t values (1, 0.02) on duplicate key uFIDelate j = values (j);`)
  4752  	tk.MustQuery(`select * from t;`).Check(testkit.Events(`1 0.02`))
  4753  }
  4754  
  4755  func (s *testIntegrationSuite) TestFuncNameConst(c *C) {
  4756  	tk := testkit.NewTestKit(c, s.causetstore)
  4757  	defer s.cleanEnv(c)
  4758  	tk.MustInterDirc("USE test;")
  4759  	tk.MustInterDirc("DROP TABLE IF EXISTS t;")
  4760  	tk.MustInterDirc("CREATE TABLE t(a CHAR(20), b VARCHAR(20), c BIGINT);")
  4761  	tk.MustInterDirc("INSERT INTO t (b, c) values('hello', 1);")
  4762  
  4763  	r := tk.MustQuery("SELECT name_const('test_int', 1), name_const('test_float', 3.1415);")
  4764  	r.Check(testkit.Events("1 3.1415"))
  4765  	r = tk.MustQuery("SELECT name_const('test_string', 'hello'), name_const('test_nil', null);")
  4766  	r.Check(testkit.Events("hello <nil>"))
  4767  	r = tk.MustQuery("SELECT name_const('test_string', 1) + c FROM t;")
  4768  	r.Check(testkit.Events("2"))
  4769  	r = tk.MustQuery("SELECT concat('hello', name_const('test_string', 'world')) FROM t;")
  4770  	r.Check(testkit.Events("helloworld"))
  4771  	r = tk.MustQuery("SELECT NAME_CONST('come', -1);")
  4772  	r.Check(testkit.Events("-1"))
  4773  	r = tk.MustQuery("SELECT NAME_CONST('come', -1.0);")
  4774  	r.Check(testkit.Events("-1.0"))
  4775  	err := tk.InterDircToErr(`select name_const(a,b) from t;`)
  4776  	c.Assert(err.Error(), Equals, "[causet:1210]Incorrect arguments to NAME_CONST")
  4777  	err = tk.InterDircToErr(`select name_const(a,"hello") from t;`)
  4778  	c.Assert(err.Error(), Equals, "[causet:1210]Incorrect arguments to NAME_CONST")
  4779  	err = tk.InterDircToErr(`select name_const("hello", b) from t;`)
  4780  	c.Assert(err.Error(), Equals, "[causet:1210]Incorrect arguments to NAME_CONST")
  4781  	err = tk.InterDircToErr(`select name_const("hello", 1+1) from t;`)
  4782  	c.Assert(err.Error(), Equals, "[causet:1210]Incorrect arguments to NAME_CONST")
  4783  	err = tk.InterDircToErr(`select name_const(concat('a', 'b'), 555) from t;`)
  4784  	c.Assert(err.Error(), Equals, "[causet:1210]Incorrect arguments to NAME_CONST")
  4785  	err = tk.InterDircToErr(`select name_const(555) from t;`)
  4786  	c.Assert(err.Error(), Equals, "[memex:1582]Incorrect parameter count in the call to native function 'name_const'")
  4787  
  4788  	var rs sqlexec.RecordSet
  4789  	rs, err = tk.InterDirc(`select name_const("hello", 1);`)
  4790  	c.Assert(err, IsNil)
  4791  	c.Assert(len(rs.Fields()), Equals, 1)
  4792  	c.Assert(rs.Fields()[0].DeferredCauset.Name.L, Equals, "hello")
  4793  }
  4794  
  4795  func (s *testIntegrationSuite) TestValuesEnum(c *C) {
  4796  	tk := testkit.NewTestKit(c, s.causetstore)
  4797  	tk.MustInterDirc("use test")
  4798  	tk.MustInterDirc(`drop causet if exists t;`)
  4799  	tk.MustInterDirc(`create causet t (a bigint primary key, b enum('a','b','c'));`)
  4800  	tk.MustInterDirc(`insert into t values (1, "a");`)
  4801  	tk.MustQuery(`select * from t;`).Check(testkit.Events(`1 a`))
  4802  	tk.MustInterDirc(`insert into t values (1, "b") on duplicate key uFIDelate b = values(b);`)
  4803  	tk.MustQuery(`select * from t;`).Check(testkit.Events(`1 b`))
  4804  }
  4805  
  4806  func (s *testIntegrationSuite) TestIssue9325(c *C) {
  4807  	tk := testkit.NewTestKit(c, s.causetstore)
  4808  	tk.MustInterDirc("use test")
  4809  	tk.MustInterDirc("drop causet if exists t")
  4810  	tk.MustInterDirc("create causet t(a timestamp) partition by range(unix_timestamp(a)) (partition p0 values less than(unix_timestamp('2020-02-16 14:20:00')), partition p1 values less than (maxvalue))")
  4811  	tk.MustInterDirc("insert into t values('2020-02-16 14:19:59'), ('2020-02-16 14:20:01')")
  4812  	result := tk.MustQuery("select * from t where a between timestamp'2020-02-16 14:19:00' and timestamp'2020-02-16 14:21:00'")
  4813  	c.Assert(result.Events(), HasLen, 2)
  4814  
  4815  	tk.MustInterDirc("drop causet if exists t")
  4816  	tk.MustInterDirc("create causet t(a timestamp)")
  4817  	tk.MustInterDirc("insert into t values('2020-02-16 14:19:59'), ('2020-02-16 14:20:01')")
  4818  	result = tk.MustQuery("select * from t where a < timestamp'2020-02-16 14:21:00'")
  4819  	result.Check(testkit.Events("2020-02-16 14:19:59", "2020-02-16 14:20:01"))
  4820  }
  4821  
  4822  func (s *testIntegrationSuite) TestIssue9710(c *C) {
  4823  	tk := testkit.NewTestKit(c, s.causetstore)
  4824  	getSAndMS := func(str string) (int, int) {
  4825  		results := strings.Split(str, ":")
  4826  		SAndMS := strings.Split(results[len(results)-1], ".")
  4827  		var s, ms int
  4828  		s, _ = strconv.Atoi(SAndMS[0])
  4829  		if len(SAndMS) > 1 {
  4830  			ms, _ = strconv.Atoi(SAndMS[1])
  4831  		}
  4832  		return s, ms
  4833  	}
  4834  
  4835  	for {
  4836  		rs := tk.MustQuery("select now(), now(6), unix_timestamp(), unix_timestamp(now())")
  4837  		s, ms := getSAndMS(rs.Events()[0][1].(string))
  4838  		if ms < 500000 {
  4839  			time.Sleep(time.Second / 10)
  4840  			continue
  4841  		}
  4842  
  4843  		s1, _ := getSAndMS(rs.Events()[0][0].(string))
  4844  		c.Assert(s, Equals, s1) // now() will truncate the result instead of rounding it
  4845  
  4846  		c.Assert(rs.Events()[0][2], Equals, rs.Events()[0][3]) // unix_timestamp() will truncate the result
  4847  		break
  4848  	}
  4849  }
  4850  
  4851  // TestDecimalConvertToTime for issue #9770
  4852  func (s *testIntegrationSuite) TestDecimalConvertToTime(c *C) {
  4853  	tk := testkit.NewTestKit(c, s.causetstore)
  4854  	defer s.cleanEnv(c)
  4855  
  4856  	tk.MustInterDirc("use test")
  4857  	tk.MustInterDirc("drop causet if exists t")
  4858  	tk.MustInterDirc("create causet t(a datetime(6), b timestamp)")
  4859  	tk.MustInterDirc("insert t values (20010101100000.123456, 20110707101112.123456)")
  4860  	tk.MustQuery("select * from t").Check(testkit.Events("2001-01-01 10:00:00.123456 2011-07-07 10:11:12"))
  4861  }
  4862  
  4863  func (s *testIntegrationSuite) TestIssue9732(c *C) {
  4864  	tk := testkit.NewTestKit(c, s.causetstore)
  4865  	defer s.cleanEnv(c)
  4866  
  4867  	tk.MustQuery(`select monthname(str_to_date(null, '%m')), monthname(str_to_date(null, '%m')),
  4868  monthname(str_to_date(1, '%m')), monthname(str_to_date(0, '%m'));`).Check(testkit.Events("<nil> <nil> <nil> <nil>"))
  4869  
  4870  	nullCases := []struct {
  4871  		allegrosql string
  4872  		ret        string
  4873  	}{
  4874  		{"select str_to_date(1, '%m')", "0000-01-00"},
  4875  		{"select str_to_date(01, '%d')", "0000-00-01"},
  4876  		{"select str_to_date(2020, '%Y')", "2020-00-00"},
  4877  		{"select str_to_date('5,2020','%m,%Y')", "2020-05-00"},
  4878  		{"select str_to_date('01,2020','%d,%Y')", "2020-00-01"},
  4879  		{"select str_to_date('01,5','%d,%m')", "0000-05-01"},
  4880  	}
  4881  
  4882  	for _, nullCase := range nullCases {
  4883  		tk.MustQuery(nullCase.allegrosql).Check(testkit.Events("<nil>"))
  4884  	}
  4885  
  4886  	// remove NO_ZERO_DATE mode
  4887  	tk.MustInterDirc("set sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'")
  4888  
  4889  	for _, nullCase := range nullCases {
  4890  		tk.MustQuery(nullCase.allegrosql).Check(testkit.Events(nullCase.ret))
  4891  	}
  4892  }
  4893  
  4894  func (s *testIntegrationSuite) TestDaynameArithmetic(c *C) {
  4895  	tk := testkit.NewTestKit(c, s.causetstore)
  4896  	defer s.cleanEnv(c)
  4897  
  4898  	cases := []struct {
  4899  		allegrosql string
  4900  		result     string
  4901  	}{
  4902  		{`select dayname("1962-03-01")+0;`, "3"},
  4903  		{`select dayname("1962-03-02")+0;`, "4"},
  4904  		{`select dayname("1962-03-03")+0;`, "5"},
  4905  		{`select dayname("1962-03-04")+0;`, "6"},
  4906  		{`select dayname("1962-03-05")+0;`, "0"},
  4907  		{`select dayname("1962-03-06")+0;`, "1"},
  4908  		{`select dayname("1962-03-07")+0;`, "2"},
  4909  		{`select dayname("1962-03-08")+0;`, "3"},
  4910  		{`select dayname("1962-03-01")+1;`, "4"},
  4911  		{`select dayname("1962-03-01")+2;`, "5"},
  4912  		{`select dayname("1962-03-01")+3;`, "6"},
  4913  		{`select dayname("1962-03-01")+4;`, "7"},
  4914  		{`select dayname("1962-03-01")+5;`, "8"},
  4915  		{`select dayname("1962-03-01")+6;`, "9"},
  4916  		{`select dayname("1962-03-01")+7;`, "10"},
  4917  		{`select dayname("1962-03-01")+2333;`, "2336"},
  4918  		{`select dayname("1962-03-01")+2.333;`, "5.333"},
  4919  		{`select dayname("1962-03-01")>2;`, "1"},
  4920  		{`select dayname("1962-03-01")<2;`, "0"},
  4921  		{`select dayname("1962-03-01")=3;`, "1"},
  4922  		{`select dayname("1962-03-01")!=3;`, "0"},
  4923  		{`select dayname("1962-03-01")<4;`, "1"},
  4924  		{`select dayname("1962-03-01")>4;`, "0"},
  4925  		{`select !dayname("1962-03-01");`, "0"},
  4926  		{`select dayname("1962-03-01")&1;`, "1"},
  4927  		{`select dayname("1962-03-01")&3;`, "3"},
  4928  		{`select dayname("1962-03-01")&7;`, "3"},
  4929  		{`select dayname("1962-03-01")|1;`, "3"},
  4930  		{`select dayname("1962-03-01")|3;`, "3"},
  4931  		{`select dayname("1962-03-01")|7;`, "7"},
  4932  		{`select dayname("1962-03-01")^1;`, "2"},
  4933  		{`select dayname("1962-03-01")^3;`, "0"},
  4934  		{`select dayname("1962-03-01")^7;`, "4"},
  4935  	}
  4936  
  4937  	for _, c := range cases {
  4938  		tk.MustQuery(c.allegrosql).Check(testkit.Events(c.result))
  4939  	}
  4940  }
  4941  
  4942  func (s *testIntegrationSuite) TestIssue10156(c *C) {
  4943  	tk := testkit.NewTestKit(c, s.causetstore)
  4944  	defer s.cleanEnv(c)
  4945  
  4946  	tk.MustInterDirc("use test")
  4947  	tk.MustInterDirc("CREATE TABLE `t1` (`period_name` varchar(24) DEFAULT NULL ,`period_id` bigint(20) DEFAULT NULL ,`starttime` bigint(20) DEFAULT NULL)")
  4948  	tk.MustInterDirc("CREATE TABLE `t2` (`bussid` bigint(20) DEFAULT NULL,`ct` bigint(20) DEFAULT NULL)")
  4949  	q := `
  4950  select
  4951      a.period_name,
  4952      b.date8
  4953  from
  4954      (select * from t1) a
  4955  left join
  4956      (select bussid,date(from_unixtime(ct)) date8 from t2) b
  4957  on
  4958      a.period_id = b.bussid
  4959  where
  4960      datediff(b.date8, date(from_unixtime(a.starttime))) >= 0`
  4961  	tk.MustQuery(q)
  4962  }
  4963  
  4964  func (s *testIntegrationSuite) TestIssue9727(c *C) {
  4965  	tk := testkit.NewTestKit(c, s.causetstore)
  4966  	defer s.cleanEnv(c)
  4967  
  4968  	cases := []struct {
  4969  		allegrosql string
  4970  		result     string
  4971  	}{
  4972  		{`SELECT "1900-01-01 00:00:00" + INTERVAL "100000000:214748364700" MINUTE_SECOND;`, "8895-03-27 22:11:40"},
  4973  		{`SELECT "1900-01-01 00:00:00" + INTERVAL 1 << 37 SECOND;`, "6255-04-08 15:04:32"},
  4974  		{`SELECT "1900-01-01 00:00:00" + INTERVAL 1 << 31 MINUTE;`, "5983-01-24 02:08:00"},
  4975  		{`SELECT "1900-01-01 00:00:00" + INTERVAL 1 << 38 SECOND;`, "<nil>"},
  4976  		{`SELECT "1900-01-01 00:00:00" + INTERVAL 1 << 33 MINUTE;`, "<nil>"},
  4977  		{`SELECT "1900-01-01 00:00:00" + INTERVAL 1 << 30 HOUR;`, "<nil>"},
  4978  		{`SELECT "1900-01-01 00:00:00" + INTERVAL "1000000000:214748364700" MINUTE_SECOND;`, "<nil>"},
  4979  		{`SELECT 19000101000000 + INTERVAL "100000000:214748364700" MINUTE_SECOND;`, "8895-03-27 22:11:40"},
  4980  		{`SELECT 19000101000000 + INTERVAL 1 << 37 SECOND;`, "6255-04-08 15:04:32"},
  4981  		{`SELECT 19000101000000 + INTERVAL 1 << 31 MINUTE;`, "5983-01-24 02:08:00"},
  4982  
  4983  		{`SELECT "8895-03-27 22:11:40" - INTERVAL "100000000:214748364700" MINUTE_SECOND;`, "1900-01-01 00:00:00"},
  4984  		{`SELECT "6255-04-08 15:04:32" - INTERVAL 1 << 37 SECOND;`, "1900-01-01 00:00:00"},
  4985  		{`SELECT "5983-01-24 02:08:00" - INTERVAL 1 << 31 MINUTE;`, "1900-01-01 00:00:00"},
  4986  		{`SELECT "9999-01-01 00:00:00" - INTERVAL 1 << 39 SECOND;`, "<nil>"},
  4987  		{`SELECT "9999-01-01 00:00:00" - INTERVAL 1 << 33 MINUTE;`, "<nil>"},
  4988  		{`SELECT "9999-01-01 00:00:00" - INTERVAL 1 << 30 HOUR;`, "<nil>"},
  4989  		{`SELECT "9999-01-01 00:00:00" - INTERVAL "10000000000:214748364700" MINUTE_SECOND;`, "<nil>"},
  4990  		{`SELECT 88950327221140 - INTERVAL "100000000:214748364700" MINUTE_SECOND ;`, "1900-01-01 00:00:00"},
  4991  		{`SELECT 62550408150432 - INTERVAL 1 << 37 SECOND;`, "1900-01-01 00:00:00"},
  4992  		{`SELECT 59830124020800 - INTERVAL 1 << 31 MINUTE;`, "1900-01-01 00:00:00"},
  4993  
  4994  		{`SELECT 10000101000000 + INTERVAL "111111111111111111" MICROSECOND;`, `4520-12-21 05:31:51.111111`},
  4995  		{`SELECT 10000101000000 + INTERVAL "111111111111.111111" SECOND;`, `4520-12-21 05:31:51.111111`},
  4996  		{`SELECT 10000101000000 + INTERVAL "111111111111.111111111" SECOND;`, `4520-12-21 05:31:51.111111`},
  4997  		{`SELECT 10000101000000 + INTERVAL "111111111111.111" SECOND;`, `4520-12-21 05:31:51.111000`},
  4998  		{`SELECT 10000101000000 + INTERVAL "111111111111." SECOND;`, `4520-12-21 05:31:51`},
  4999  		{`SELECT 10000101000000 + INTERVAL "111111111111111111.5" MICROSECOND;`, `4520-12-21 05:31:51.111112`},
  5000  		{`SELECT 10000101000000 + INTERVAL "111111111111111112.5" MICROSECOND;`, `4520-12-21 05:31:51.111113`},
  5001  		{`SELECT 10000101000000 + INTERVAL "111111111111111111.500000" MICROSECOND;`, `4520-12-21 05:31:51.111112`},
  5002  		{`SELECT 10000101000000 + INTERVAL "111111111111111111.50000000" MICROSECOND;`, `4520-12-21 05:31:51.111112`},
  5003  		{`SELECT 10000101000000 + INTERVAL "111111111111111111.6" MICROSECOND;`, `4520-12-21 05:31:51.111112`},
  5004  		{`SELECT 10000101000000 + INTERVAL "111111111111111111.499999" MICROSECOND;`, `4520-12-21 05:31:51.111111`},
  5005  		{`SELECT 10000101000000 + INTERVAL "111111111111111111.499999999999" MICROSECOND;`, `4520-12-21 05:31:51.111111`},
  5006  	}
  5007  
  5008  	for _, c := range cases {
  5009  		tk.MustQuery(c.allegrosql).Check(testkit.Events(c.result))
  5010  	}
  5011  }
  5012  
  5013  func (s *testIntegrationSuite) TestTimestamFIDelatumEncode(c *C) {
  5014  	tk := testkit.NewTestKit(c, s.causetstore)
  5015  	tk.MustInterDirc("use test")
  5016  	tk.MustInterDirc(`drop causet if exists t;`)
  5017  	tk.MustInterDirc(`create causet t (a bigint primary key, b timestamp)`)
  5018  	tk.MustInterDirc(`insert into t values (1, "2020-04-29 11:56:12")`)
  5019  	tk.MustQuery(`explain select * from t where b = (select max(b) from t)`).Check(testkit.Events(
  5020  		"BlockReader_43 10.00 root  data:Selection_42",
  5021  		"└─Selection_42 10.00 cop[einsteindb]  eq(test.t.b, 2020-04-29 11:56:12)",
  5022  		"  └─BlockFullScan_41 10000.00 cop[einsteindb] causet:t keep order:false, stats:pseudo",
  5023  	))
  5024  	tk.MustQuery(`select * from t where b = (select max(b) from t)`).Check(testkit.Events(`1 2020-04-29 11:56:12`))
  5025  }
  5026  
  5027  func (s *testIntegrationSuite) TestDateTimeAddReal(c *C) {
  5028  	tk := testkit.NewTestKit(c, s.causetstore)
  5029  	defer s.cleanEnv(c)
  5030  
  5031  	cases := []struct {
  5032  		allegrosql string
  5033  		result     string
  5034  	}{
  5035  		{`SELECT "1900-01-01 00:00:00" + INTERVAL 1.123456789e3 SECOND;`, "1900-01-01 00:18:43.456789"},
  5036  		{`SELECT 19000101000000 + INTERVAL 1.123456789e3 SECOND;`, "1900-01-01 00:18:43.456789"},
  5037  		{`select date("1900-01-01") + interval 1.123456789e3 second;`, "1900-01-01 00:18:43.456789"},
  5038  		{`SELECT "1900-01-01 00:18:43.456789" - INTERVAL 1.123456789e3 SECOND;`, "1900-01-01 00:00:00"},
  5039  		{`SELECT 19000101001843.456789 - INTERVAL 1.123456789e3 SECOND;`, "1900-01-01 00:00:00"},
  5040  		{`select date("1900-01-01") - interval 1.123456789e3 second;`, "1899-12-31 23:41:16.543211"},
  5041  		{`select 19000101000000 - interval 1.123456789e3 second;`, "1899-12-31 23:41:16.543211"},
  5042  	}
  5043  
  5044  	for _, c := range cases {
  5045  		tk.MustQuery(c.allegrosql).Check(testkit.Events(c.result))
  5046  	}
  5047  }
  5048  
  5049  func (s *testIntegrationSuite) TestIssue10181(c *C) {
  5050  	tk := testkit.NewTestKit(c, s.causetstore)
  5051  	tk.MustInterDirc("use test")
  5052  	tk.MustInterDirc(`drop causet if exists t;`)
  5053  	tk.MustInterDirc(`create causet t(a bigint unsigned primary key);`)
  5054  	tk.MustInterDirc(`insert into t values(9223372036854775807), (18446744073709551615)`)
  5055  	tk.MustQuery(`select * from t where a > 9223372036854775807-0.5 order by a`).Check(testkit.Events(`9223372036854775807`, `18446744073709551615`))
  5056  }
  5057  
  5058  func (s *testIntegrationSuite) TestExprPushdown(c *C) {
  5059  	tk := testkit.NewTestKit(c, s.causetstore)
  5060  	tk.MustInterDirc("use test")
  5061  	tk.MustInterDirc("drop causet if exists t")
  5062  	tk.MustInterDirc("create causet t(id int, defCaus1 varchar(10), defCaus2 varchar(10), defCaus3 int, defCaus4 int, defCaus5 int, index key1" +
  5063  		" (defCaus1, defCaus2, defCaus3, defCaus4), index key2 (defCaus4, defCaus3, defCaus2, defCaus1))")
  5064  	tk.MustInterDirc("insert into t values(1,'211111','311',4,5,6),(2,'311111','411',5,6,7),(3,'411111','511',6,7,8)," +
  5065  		"(4,'511111','611',7,8,9),(5,'611111','711',8,9,10)")
  5066  
  5067  	// case 1, index scan without double read, some filters can not be pushed to cop task
  5068  	rows := tk.MustQuery("explain select defCaus2, defCaus1 from t use index(key1) where defCaus2 like '5%' and substr(defCaus1, 1, 1) = '4'").Events()
  5069  	c.Assert(fmt.Sprintf("%v", rows[1][2]), Equals, "root")
  5070  	c.Assert(fmt.Sprintf("%v", rows[1][4]), Equals, "eq(substr(test.t.defCaus1, 1, 1), \"4\")")
  5071  	c.Assert(fmt.Sprintf("%v", rows[3][2]), Equals, "cop[einsteindb]")
  5072  	c.Assert(fmt.Sprintf("%v", rows[3][4]), Equals, "like(test.t.defCaus2, \"5%\", 92)")
  5073  	tk.MustQuery("select defCaus2, defCaus1 from t use index(key1) where defCaus2 like '5%' and substr(defCaus1, 1, 1) = '4'").Check(testkit.Events("511 411111"))
  5074  	tk.MustQuery("select count(defCaus2) from t use index(key1) where defCaus2 like '5%' and substr(defCaus1, 1, 1) = '4'").Check(testkit.Events("1"))
  5075  
  5076  	// case 2, index scan without double read, none of the filters can be pushed to cop task
  5077  	rows = tk.MustQuery("explain select defCaus1, defCaus2 from t use index(key2) where substr(defCaus2, 1, 1) = '5' and substr(defCaus1, 1, 1) = '4'").Events()
  5078  	c.Assert(fmt.Sprintf("%v", rows[0][2]), Equals, "root")
  5079  	c.Assert(fmt.Sprintf("%v", rows[0][4]), Equals, "eq(substr(test.t.defCaus1, 1, 1), \"4\"), eq(substr(test.t.defCaus2, 1, 1), \"5\")")
  5080  	tk.MustQuery("select defCaus1, defCaus2 from t use index(key2) where substr(defCaus2, 1, 1) = '5' and substr(defCaus1, 1, 1) = '4'").Check(testkit.Events("411111 511"))
  5081  	tk.MustQuery("select count(defCaus1) from t use index(key2) where substr(defCaus2, 1, 1) = '5' and substr(defCaus1, 1, 1) = '4'").Check(testkit.Events("1"))
  5082  
  5083  	// case 3, index scan with double read, some filters can not be pushed to cop task
  5084  	rows = tk.MustQuery("explain select id from t use index(key1) where defCaus2 like '5%' and substr(defCaus1, 1, 1) = '4'").Events()
  5085  	c.Assert(fmt.Sprintf("%v", rows[1][2]), Equals, "root")
  5086  	c.Assert(fmt.Sprintf("%v", rows[1][4]), Equals, "eq(substr(test.t.defCaus1, 1, 1), \"4\")")
  5087  	c.Assert(fmt.Sprintf("%v", rows[3][2]), Equals, "cop[einsteindb]")
  5088  	c.Assert(fmt.Sprintf("%v", rows[3][4]), Equals, "like(test.t.defCaus2, \"5%\", 92)")
  5089  	tk.MustQuery("select id from t use index(key1) where defCaus2 like '5%' and substr(defCaus1, 1, 1) = '4'").Check(testkit.Events("3"))
  5090  	tk.MustQuery("select count(id) from t use index(key1) where defCaus2 like '5%' and substr(defCaus1, 1, 1) = '4'").Check(testkit.Events("1"))
  5091  
  5092  	// case 4, index scan with double read, none of the filters can be pushed to cop task
  5093  	rows = tk.MustQuery("explain select id from t use index(key2) where substr(defCaus2, 1, 1) = '5' and substr(defCaus1, 1, 1) = '4'").Events()
  5094  	c.Assert(fmt.Sprintf("%v", rows[1][2]), Equals, "root")
  5095  	c.Assert(fmt.Sprintf("%v", rows[1][4]), Equals, "eq(substr(test.t.defCaus1, 1, 1), \"4\"), eq(substr(test.t.defCaus2, 1, 1), \"5\")")
  5096  	tk.MustQuery("select id from t use index(key2) where substr(defCaus2, 1, 1) = '5' and substr(defCaus1, 1, 1) = '4'").Check(testkit.Events("3"))
  5097  	tk.MustQuery("select count(id) from t use index(key2) where substr(defCaus2, 1, 1) = '5' and substr(defCaus1, 1, 1) = '4'").Check(testkit.Events("1"))
  5098  }
  5099  func (s *testIntegrationSuite) TestIssue16973(c *C) {
  5100  	tk := testkit.NewTestKit(c, s.causetstore)
  5101  	tk.MustInterDirc("use test")
  5102  	tk.MustInterDirc("drop causet if exists t1")
  5103  	tk.MustInterDirc("set @@milevadb_enable_clustered_index=0;")
  5104  	tk.MustInterDirc("create causet t1(id varchar(36) not null primary key, org_id varchar(36) not null, " +
  5105  		"status tinyint default 1 not null, ns varchar(36) default '' not null);")
  5106  	tk.MustInterDirc("create causet t2(id varchar(36) not null primary key, order_id varchar(36) not null, " +
  5107  		"begin_time timestamp(3) default CURRENT_TIMESTAMP(3) not null);")
  5108  	tk.MustInterDirc("create index idx_oid on t2(order_id);")
  5109  	tk.MustInterDirc("insert into t1 value (1,1,1,'a');")
  5110  	tk.MustInterDirc("insert into t1 value (2,1,2,'a');")
  5111  	tk.MustInterDirc("insert into t1 value (3,1,3,'a');")
  5112  	tk.MustInterDirc("insert into t2 value (1,2,date'2020-05-08');")
  5113  
  5114  	rows := tk.MustQuery("explain SELECT /*+ INL_MERGE_JOIN(t1,t2) */ COUNT(*) FROM  t1 LEFT JOIN t2 ON t1.id = t2.order_id WHERE t1.ns = 'a' AND t1.org_id IN (1) " +
  5115  		"AND t1.status IN (2,6,10) AND timestamFIDeliff(month, t2.begin_time, date'2020-05-06') = 0;").Events()
  5116  	c.Assert(fmt.Sprintf("%v", rows[1][0]), Matches, ".*IndexMergeJoin.*")
  5117  	c.Assert(fmt.Sprintf("%v", rows[4][3]), Equals, "causet:t1")
  5118  	c.Assert(fmt.Sprintf("%v", rows[5][0]), Matches, ".*Selection.*")
  5119  	c.Assert(fmt.Sprintf("%v", rows[9][3]), Equals, "causet:t2")
  5120  	tk.MustQuery("SELECT /*+ INL_MERGE_JOIN(t1,t2) */ COUNT(*) FROM  t1 LEFT JOIN t2 ON t1.id = t2.order_id WHERE t1.ns = 'a' AND t1.org_id IN (1) " +
  5121  		"AND t1.status IN (2,6,10) AND timestamFIDeliff(month, t2.begin_time, date'2020-05-06') = 0;").Check(testkit.Events("1"))
  5122  }
  5123  
  5124  func (s *testIntegrationSuite) TestExprPushdownBlacklist(c *C) {
  5125  	tk := testkit.NewTestKit(c, s.causetstore)
  5126  	tk.MustQuery(`select * from allegrosql.expr_pushdown_blacklist`).Check(testkit.Events(
  5127  		"date_add tiflash DST(daylight saving time) does not take effect in TiFlash date_add",
  5128  		"cast tiflash Behavior of some corner cases(overflow, truncate etc) is different in TiFlash and MilevaDB"))
  5129  
  5130  	tk.MustInterDirc("use test")
  5131  	tk.MustInterDirc("drop causet if exists t")
  5132  	tk.MustInterDirc("create causet t(a int , b date)")
  5133  
  5134  	// Create virtual tiflash replica info.
  5135  	dom := petri.GetPetri(tk.Se)
  5136  	is := dom.SchemaReplicant()
  5137  	EDB, exists := is.SchemaByName(perceptron.NewCIStr("test"))
  5138  	c.Assert(exists, IsTrue)
  5139  	for _, tblInfo := range EDB.Blocks {
  5140  		if tblInfo.Name.L == "t" {
  5141  			tblInfo.TiFlashReplica = &perceptron.TiFlashReplicaInfo{
  5142  				Count:     1,
  5143  				Available: true,
  5144  			}
  5145  		}
  5146  	}
  5147  
  5148  	tk.MustInterDirc("insert into allegrosql.expr_pushdown_blacklist " +
  5149  		"values('<', 'einsteindb,tiflash,milevadb', 'for test'),('date_format', 'einsteindb', 'for test')")
  5150  	tk.MustInterDirc("admin reload expr_pushdown_blacklist")
  5151  
  5152  	tk.MustInterDirc("set @@stochastik.milevadb_isolation_read_engines = 'tiflash'")
  5153  
  5154  	// < not pushed, cast only pushed to EinsteinDB, date_format only pushed to TiFlash,
  5155  	// > pushed to both EinsteinDB and TiFlash
  5156  	rows := tk.MustQuery("explain select * from test.t where b > date'1988-01-01' and b < date'1994-01-01' " +
  5157  		"and cast(a as decimal(10,2)) > 10.10 and date_format(b,'%m') = '11'").Events()
  5158  	c.Assert(fmt.Sprintf("%v", rows[0][4]), Equals, "lt(test.t.b, 1994-01-01)")
  5159  	c.Assert(fmt.Sprintf("%v", rows[1][4]), Equals, "gt(cast(test.t.a), 10.10)")
  5160  	c.Assert(fmt.Sprintf("%v", rows[3][4]), Equals, "eq(date_format(test.t.b, \"%m\"), \"11\"), gt(test.t.b, 1988-01-01)")
  5161  
  5162  	tk.MustInterDirc("set @@stochastik.milevadb_isolation_read_engines = 'einsteindb'")
  5163  	rows = tk.MustQuery("explain select * from test.t where b > date'1988-01-01' and b < date'1994-01-01' " +
  5164  		"and cast(a as decimal(10,2)) > 10.10 and date_format(b,'%m') = '11'").Events()
  5165  	c.Assert(fmt.Sprintf("%v", rows[0][4]), Equals, "lt(test.t.b, 1994-01-01)")
  5166  	c.Assert(fmt.Sprintf("%v", rows[1][4]), Equals, "eq(date_format(test.t.b, \"%m\"), \"11\")")
  5167  	c.Assert(fmt.Sprintf("%v", rows[3][4]), Equals, "gt(cast(test.t.a), 10.10), gt(test.t.b, 1988-01-01)")
  5168  
  5169  	tk.MustInterDirc("delete from allegrosql.expr_pushdown_blacklist where name = '<' and store_type = 'einsteindb,tiflash,milevadb' and reason = 'for test'")
  5170  	tk.MustInterDirc("delete from allegrosql.expr_pushdown_blacklist where name = 'date_format' and store_type = 'einsteindb' and reason = 'for test'")
  5171  	tk.MustInterDirc("admin reload expr_pushdown_blacklist")
  5172  }
  5173  
  5174  func (s *testIntegrationSuite) TestOptMemruleBlacklist(c *C) {
  5175  	tk := testkit.NewTestKit(c, s.causetstore)
  5176  	tk.MustQuery(`select * from allegrosql.opt_rule_blacklist`).Check(testkit.Events())
  5177  }
  5178  
  5179  func (s *testIntegrationSuite) TestIssue10804(c *C) {
  5180  	tk := testkit.NewTestKit(c, s.causetstore)
  5181  	tk.MustQuery(`SELECT @@information_schema_stats_expiry`).Check(testkit.Events(`86400`))
  5182  	tk.MustInterDirc("/*!80000 SET SESSION information_schema_stats_expiry=0 */")
  5183  	tk.MustQuery(`SELECT @@information_schema_stats_expiry`).Check(testkit.Events(`0`))
  5184  	tk.MustQuery(`SELECT @@GLOBAL.information_schema_stats_expiry`).Check(testkit.Events(`86400`))
  5185  	tk.MustInterDirc("/*!80000 SET GLOBAL information_schema_stats_expiry=0 */")
  5186  	tk.MustQuery(`SELECT @@GLOBAL.information_schema_stats_expiry`).Check(testkit.Events(`0`))
  5187  }
  5188  
  5189  func (s *testIntegrationSuite) TestInvalidEndingStatement(c *C) {
  5190  	tk := testkit.NewTestKit(c, s.causetstore)
  5191  	tk.MustInterDirc("use test")
  5192  	parseErrMsg := "[BerolinaSQL:1064]"
  5193  	errMsgLen := len(parseErrMsg)
  5194  
  5195  	assertParseErr := func(allegrosql string) {
  5196  		_, err := tk.InterDirc(allegrosql)
  5197  		c.Assert(err, NotNil)
  5198  		c.Assert(err.Error()[:errMsgLen], Equals, parseErrMsg)
  5199  	}
  5200  
  5201  	assertParseErr("drop causet if exists t'xyz")
  5202  	assertParseErr("drop causet if exists t'")
  5203  	assertParseErr("drop causet if exists t`")
  5204  	assertParseErr(`drop causet if exists t'`)
  5205  	assertParseErr(`drop causet if exists t"`)
  5206  }
  5207  
  5208  func (s *testIntegrationSuite) TestIssue15613(c *C) {
  5209  	tk := testkit.NewTestKit(c, s.causetstore)
  5210  	tk.MustQuery("select sec_to_time(1e-4)").Check(testkit.Events("00:00:00.000100"))
  5211  	tk.MustQuery("select sec_to_time(1e-5)").Check(testkit.Events("00:00:00.000010"))
  5212  	tk.MustQuery("select sec_to_time(1e-6)").Check(testkit.Events("00:00:00.000001"))
  5213  	tk.MustQuery("select sec_to_time(1e-7)").Check(testkit.Events("00:00:00.000000"))
  5214  }
  5215  
  5216  func (s *testIntegrationSuite) TestIssue10675(c *C) {
  5217  	tk := testkit.NewTestKit(c, s.causetstore)
  5218  	tk.MustInterDirc("use test")
  5219  	tk.MustInterDirc(`drop causet if exists t;`)
  5220  	tk.MustInterDirc(`create causet t(a int);`)
  5221  	tk.MustInterDirc(`insert into t values(1);`)
  5222  	tk.MustQuery(`select * from t where a < -184467440737095516167.1;`).Check(testkit.Events())
  5223  	tk.MustQuery(`select * from t where a > -184467440737095516167.1;`).Check(
  5224  		testkit.Events("1"))
  5225  	tk.MustQuery(`select * from t where a < 184467440737095516167.1;`).Check(
  5226  		testkit.Events("1"))
  5227  	tk.MustQuery(`select * from t where a > 184467440737095516167.1;`).Check(testkit.Events())
  5228  
  5229  	// issue 11647
  5230  	tk.MustInterDirc(`drop causet if exists t;`)
  5231  	tk.MustInterDirc(`create causet t(b bit(1));`)
  5232  	tk.MustInterDirc(`insert into t values(b'1');`)
  5233  	tk.MustQuery(`select count(*) from t where b = 1;`).Check(testkit.Events("1"))
  5234  	tk.MustQuery(`select count(*) from t where b = '1';`).Check(testkit.Events("1"))
  5235  	tk.MustQuery(`select count(*) from t where b = b'1';`).Check(testkit.Events("1"))
  5236  
  5237  	tk.MustInterDirc(`drop causet if exists t;`)
  5238  	tk.MustInterDirc(`create causet t(b bit(63));`)
  5239  	// Not 64, because the behavior of allegrosql is amazing. I have no idea to fix it.
  5240  	tk.MustInterDirc(`insert into t values(b'111111111111111111111111111111111111111111111111111111111111111');`)
  5241  	tk.MustQuery(`select count(*) from t where b = 9223372036854775807;`).Check(testkit.Events("1"))
  5242  	tk.MustQuery(`select count(*) from t where b = '9223372036854775807';`).Check(testkit.Events("1"))
  5243  	tk.MustQuery(`select count(*) from t where b = b'111111111111111111111111111111111111111111111111111111111111111';`).Check(testkit.Events("1"))
  5244  }
  5245  
  5246  func (s *testIntegrationSuite) TestDatetimeMicrosecond(c *C) {
  5247  	tk := testkit.NewTestKit(c, s.causetstore)
  5248  	// For int
  5249  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2 SECOND_MICROSECOND);`).Check(
  5250  		testkit.Events("2007-03-28 22:08:27.800000"))
  5251  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2 MINUTE_MICROSECOND);`).Check(
  5252  		testkit.Events("2007-03-28 22:08:27.800000"))
  5253  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2 HOUR_MICROSECOND);`).Check(
  5254  		testkit.Events("2007-03-28 22:08:27.800000"))
  5255  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2 DAY_MICROSECOND);`).Check(
  5256  		testkit.Events("2007-03-28 22:08:27.800000"))
  5257  
  5258  	// For Decimal
  5259  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 HOUR_MINUTE);`).Check(
  5260  		testkit.Events("2007-03-29 00:10:28"))
  5261  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 MINUTE_SECOND);`).Check(
  5262  		testkit.Events("2007-03-28 22:10:30"))
  5263  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 YEAR_MONTH);`).Check(
  5264  		testkit.Events("2009-05-28 22:08:28"))
  5265  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 DAY_HOUR);`).Check(
  5266  		testkit.Events("2007-03-31 00:08:28"))
  5267  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 DAY_MINUTE);`).Check(
  5268  		testkit.Events("2007-03-29 00:10:28"))
  5269  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 DAY_SECOND);`).Check(
  5270  		testkit.Events("2007-03-28 22:10:30"))
  5271  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 HOUR_SECOND);`).Check(
  5272  		testkit.Events("2007-03-28 22:10:30"))
  5273  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 SECOND);`).Check(
  5274  		testkit.Events("2007-03-28 22:08:30.200000"))
  5275  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 YEAR);`).Check(
  5276  		testkit.Events("2009-03-28 22:08:28"))
  5277  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 QUARTER);`).Check(
  5278  		testkit.Events("2007-09-28 22:08:28"))
  5279  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 MONTH);`).Check(
  5280  		testkit.Events("2007-05-28 22:08:28"))
  5281  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 WEEK);`).Check(
  5282  		testkit.Events("2007-04-11 22:08:28"))
  5283  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 DAY);`).Check(
  5284  		testkit.Events("2007-03-30 22:08:28"))
  5285  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 HOUR);`).Check(
  5286  		testkit.Events("2007-03-29 00:08:28"))
  5287  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 MINUTE);`).Check(
  5288  		testkit.Events("2007-03-28 22:10:28"))
  5289  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 MICROSECOND);`).Check(
  5290  		testkit.Events("2007-03-28 22:08:28.000002"))
  5291  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 HOUR_MINUTE);`).Check(
  5292  		testkit.Events("2007-03-28 20:06:28"))
  5293  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 MINUTE_SECOND);`).Check(
  5294  		testkit.Events("2007-03-28 22:06:26"))
  5295  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 YEAR_MONTH);`).Check(
  5296  		testkit.Events("2005-01-28 22:08:28"))
  5297  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 DAY_HOUR);`).Check(
  5298  		testkit.Events("2007-03-26 20:08:28"))
  5299  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 DAY_MINUTE);`).Check(
  5300  		testkit.Events("2007-03-28 20:06:28"))
  5301  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 DAY_SECOND);`).Check(
  5302  		testkit.Events("2007-03-28 22:06:26"))
  5303  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 HOUR_SECOND);`).Check(
  5304  		testkit.Events("2007-03-28 22:06:26"))
  5305  	//	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 SECOND);`).Check(
  5306  	//		testkit.Events("2007-03-28 22:08:25.800000"))
  5307  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 YEAR);`).Check(
  5308  		testkit.Events("2005-03-28 22:08:28"))
  5309  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 QUARTER);`).Check(
  5310  		testkit.Events("2006-09-28 22:08:28"))
  5311  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 MONTH);`).Check(
  5312  		testkit.Events("2007-01-28 22:08:28"))
  5313  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 WEEK);`).Check(
  5314  		testkit.Events("2007-03-14 22:08:28"))
  5315  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 DAY);`).Check(
  5316  		testkit.Events("2007-03-26 22:08:28"))
  5317  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 HOUR);`).Check(
  5318  		testkit.Events("2007-03-28 20:08:28"))
  5319  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 MINUTE);`).Check(
  5320  		testkit.Events("2007-03-28 22:06:28"))
  5321  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 MICROSECOND);`).Check(
  5322  		testkit.Events("2007-03-28 22:08:27.999998"))
  5323  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" HOUR_MINUTE);`).Check(
  5324  		testkit.Events("2007-03-28 20:06:28"))
  5325  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" MINUTE_SECOND);`).Check(
  5326  		testkit.Events("2007-03-28 22:06:26"))
  5327  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" YEAR_MONTH);`).Check(
  5328  		testkit.Events("2005-01-28 22:08:28"))
  5329  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" DAY_HOUR);`).Check(
  5330  		testkit.Events("2007-03-26 20:08:28"))
  5331  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" DAY_MINUTE);`).Check(
  5332  		testkit.Events("2007-03-28 20:06:28"))
  5333  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" DAY_SECOND);`).Check(
  5334  		testkit.Events("2007-03-28 22:06:26"))
  5335  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" HOUR_SECOND);`).Check(
  5336  		testkit.Events("2007-03-28 22:06:26"))
  5337  	//	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" SECOND);`).Check(
  5338  	//		testkit.Events("2007-03-28 22:08:25.800000"))
  5339  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" YEAR);`).Check(
  5340  		testkit.Events("2005-03-28 22:08:28"))
  5341  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" QUARTER);`).Check(
  5342  		testkit.Events("2006-09-28 22:08:28"))
  5343  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" MONTH);`).Check(
  5344  		testkit.Events("2007-01-28 22:08:28"))
  5345  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" WEEK);`).Check(
  5346  		testkit.Events("2007-03-14 22:08:28"))
  5347  	//	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" DAY);`).Check(
  5348  	//		testkit.Events("2007-03-26 22:08:28"))
  5349  	//	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" HOUR);`).Check(
  5350  	//		testkit.Events("2007-03-28 20:08:28"))
  5351  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" MINUTE);`).Check(
  5352  		testkit.Events("2007-03-28 22:06:28"))
  5353  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.2" MICROSECOND);`).Check(
  5354  		testkit.Events("2007-03-28 22:08:27.999998"))
  5355  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" HOUR_MINUTE);`).Check(
  5356  		testkit.Events("2007-03-28 20:06:28"))
  5357  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" MINUTE_SECOND);`).Check(
  5358  		testkit.Events("2007-03-28 22:06:26"))
  5359  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" YEAR_MONTH);`).Check(
  5360  		testkit.Events("2005-01-28 22:08:28"))
  5361  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" DAY_HOUR);`).Check(
  5362  		testkit.Events("2007-03-26 20:08:28"))
  5363  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" DAY_MINUTE);`).Check(
  5364  		testkit.Events("2007-03-28 20:06:28"))
  5365  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" DAY_SECOND);`).Check(
  5366  		testkit.Events("2007-03-28 22:06:26"))
  5367  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" HOUR_SECOND);`).Check(
  5368  		testkit.Events("2007-03-28 22:06:26"))
  5369  	//	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" SECOND);`).Check(
  5370  	//		testkit.Events("2007-03-28 22:08:26"))
  5371  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" YEAR);`).Check(
  5372  		testkit.Events("2005-03-28 22:08:28"))
  5373  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" QUARTER);`).Check(
  5374  		testkit.Events("2006-09-28 22:08:28"))
  5375  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" MONTH);`).Check(
  5376  		testkit.Events("2007-01-28 22:08:28"))
  5377  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" WEEK);`).Check(
  5378  		testkit.Events("2007-03-14 22:08:28"))
  5379  	//	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" DAY);`).Check(
  5380  	//		testkit.Events("2007-03-26 22:08:28"))
  5381  	//	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" HOUR);`).Check(
  5382  	//		testkit.Events("2007-03-28 20:08:28"))
  5383  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" MINUTE);`).Check(
  5384  		testkit.Events("2007-03-28 22:06:28"))
  5385  	tk.MustQuery(`select DATE_ADD('2007-03-28 22:08:28',INTERVAL "-2.-2" MICROSECOND);`).Check(
  5386  		testkit.Events("2007-03-28 22:08:27.999998"))
  5387  }
  5388  
  5389  func (s *testIntegrationSuite) TestFuncCaseWithLeftJoin(c *C) {
  5390  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
  5391  
  5392  	tk.MustInterDirc("create causet kankan1(id int, name text)")
  5393  	tk.MustInterDirc("insert into kankan1 values(1, 'a')")
  5394  	tk.MustInterDirc("insert into kankan1 values(2, 'a')")
  5395  
  5396  	tk.MustInterDirc("create causet kankan2(id int, h1 text)")
  5397  	tk.MustInterDirc("insert into kankan2 values(2, 'z')")
  5398  
  5399  	tk.MustQuery("select t1.id from kankan1 t1 left join kankan2 t2 on t1.id = t2.id where (case  when t1.name='b' then 'case2' when t1.name='a' then 'case1' else NULL end) = 'case1' order by t1.id").Check(testkit.Events("1", "2"))
  5400  }
  5401  
  5402  func (s *testIntegrationSuite) TestIssue11594(c *C) {
  5403  	tk := testkit.NewTestKit(c, s.causetstore)
  5404  	tk.MustInterDirc("use test")
  5405  	tk.MustInterDirc(`drop causet if exists t1;`)
  5406  	tk.MustInterDirc("CREATE TABLE t1 (v bigint(20) UNSIGNED NOT NULL);")
  5407  	tk.MustInterDirc("INSERT INTO t1 VALUES (1), (2);")
  5408  	tk.MustQuery("SELECT SUM(IF(v > 1, v, -v)) FROM t1;").Check(testkit.Events("1"))
  5409  	tk.MustQuery("SELECT sum(IFNULL(cast(null+rand() as unsigned), -v)) FROM t1;").Check(testkit.Events("-3"))
  5410  	tk.MustQuery("SELECT sum(COALESCE(cast(null+rand() as unsigned), -v)) FROM t1;").Check(testkit.Events("-3"))
  5411  	tk.MustQuery("SELECT sum(COALESCE(cast(null+rand() as unsigned), v)) FROM t1;").Check(testkit.Events("3"))
  5412  }
  5413  
  5414  func (s *testIntegrationSuite) TestDefEnableVectorizedEvaluation(c *C) {
  5415  	tk := testkit.NewTestKit(c, s.causetstore)
  5416  	tk.MustInterDirc("use allegrosql")
  5417  	tk.MustQuery(`select @@milevadb_enable_vectorized_memex`).Check(testkit.Events("1"))
  5418  }
  5419  
  5420  func (s *testIntegrationSuite) TestIssue11309And11319(c *C) {
  5421  	tk := testkit.NewTestKit(c, s.causetstore)
  5422  	tk.MustInterDirc("use test")
  5423  	tk.MustInterDirc(`drop causet if exists t;`)
  5424  	tk.MustInterDirc(`CREATE TABLE t (a decimal(6,3),b double(6,3),c float(6,3));`)
  5425  	tk.MustInterDirc(`INSERT INTO t VALUES (1.100,1.100,1.100);`)
  5426  	tk.MustQuery(`SELECT DATE_ADD('2003-11-18 07:25:13',INTERVAL a MINUTE_SECOND) FROM t`).Check(testkit.Events(`2003-11-18 07:27:53`))
  5427  	tk.MustQuery(`SELECT DATE_ADD('2003-11-18 07:25:13',INTERVAL b MINUTE_SECOND) FROM t`).Check(testkit.Events(`2003-11-18 07:27:53`))
  5428  	tk.MustQuery(`SELECT DATE_ADD('2003-11-18 07:25:13',INTERVAL c MINUTE_SECOND) FROM t`).Check(testkit.Events(`2003-11-18 07:27:53`))
  5429  	tk.MustInterDirc(`drop causet if exists t;`)
  5430  	tk.MustInterDirc(`CREATE TABLE t (a decimal(11,7),b double(11,7),c float(11,7));`)
  5431  	tk.MustInterDirc(`INSERT INTO t VALUES (123.9999999,123.9999999,123.9999999),(-123.9999999,-123.9999999,-123.9999999);`)
  5432  	tk.MustQuery(`SELECT DATE_ADD('2003-11-18 07:25:13',INTERVAL a MINUTE_SECOND) FROM t`).Check(testkit.Events(`2004-03-13 03:14:52`, `2003-07-25 11:35:34`))
  5433  	tk.MustQuery(`SELECT DATE_ADD('2003-11-18 07:25:13',INTERVAL b MINUTE_SECOND) FROM t`).Check(testkit.Events(`2004-03-13 03:14:52`, `2003-07-25 11:35:34`))
  5434  	tk.MustQuery(`SELECT DATE_ADD('2003-11-18 07:25:13',INTERVAL c MINUTE_SECOND) FROM t`).Check(testkit.Events(`2003-11-18 09:29:13`, `2003-11-18 05:21:13`))
  5435  	tk.MustInterDirc(`drop causet if exists t;`)
  5436  
  5437  	// for https://github.com/whtcorpsinc/milevadb/issues/11319
  5438  	tk.MustQuery(`SELECT DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 MINUTE_MICROSECOND)`).Check(testkit.Events("2007-03-28 22:08:25.800000"))
  5439  	tk.MustQuery(`SELECT DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 SECOND_MICROSECOND)`).Check(testkit.Events("2007-03-28 22:08:25.800000"))
  5440  	tk.MustQuery(`SELECT DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 HOUR_MICROSECOND)`).Check(testkit.Events("2007-03-28 22:08:25.800000"))
  5441  	tk.MustQuery(`SELECT DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 DAY_MICROSECOND)`).Check(testkit.Events("2007-03-28 22:08:25.800000"))
  5442  	tk.MustQuery(`SELECT DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 SECOND)`).Check(testkit.Events("2007-03-28 22:08:25.800000"))
  5443  	tk.MustQuery(`SELECT DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 HOUR_SECOND)`).Check(testkit.Events("2007-03-28 22:06:26"))
  5444  	tk.MustQuery(`SELECT DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 DAY_SECOND)`).Check(testkit.Events("2007-03-28 22:06:26"))
  5445  	tk.MustQuery(`SELECT DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 MINUTE_SECOND)`).Check(testkit.Events("2007-03-28 22:06:26"))
  5446  	tk.MustQuery(`SELECT DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 MINUTE)`).Check(testkit.Events("2007-03-28 22:06:28"))
  5447  	tk.MustQuery(`SELECT DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 DAY_MINUTE)`).Check(testkit.Events("2007-03-28 20:06:28"))
  5448  	tk.MustQuery(`SELECT DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 HOUR_MINUTE)`).Check(testkit.Events("2007-03-28 20:06:28"))
  5449  	tk.MustQuery(`SELECT DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 DAY_HOUR)`).Check(testkit.Events("2007-03-26 20:08:28"))
  5450  	tk.MustQuery(`SELECT DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 YEAR_MONTH)`).Check(testkit.Events("2005-01-28 22:08:28"))
  5451  
  5452  	tk.MustQuery(`SELECT DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 MINUTE_MICROSECOND)`).Check(testkit.Events("2007-03-28 22:08:30.200000"))
  5453  	tk.MustQuery(`SELECT DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 SECOND_MICROSECOND)`).Check(testkit.Events("2007-03-28 22:08:30.200000"))
  5454  	tk.MustQuery(`SELECT DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 HOUR_MICROSECOND)`).Check(testkit.Events("2007-03-28 22:08:30.200000"))
  5455  	tk.MustQuery(`SELECT DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 DAY_MICROSECOND)`).Check(testkit.Events("2007-03-28 22:08:30.200000"))
  5456  	tk.MustQuery(`SELECT DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 SECOND)`).Check(testkit.Events("2007-03-28 22:08:30.200000"))
  5457  	tk.MustQuery(`SELECT DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 HOUR_SECOND)`).Check(testkit.Events("2007-03-28 22:10:30"))
  5458  	tk.MustQuery(`SELECT DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 DAY_SECOND)`).Check(testkit.Events("2007-03-28 22:10:30"))
  5459  	tk.MustQuery(`SELECT DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 MINUTE_SECOND)`).Check(testkit.Events("2007-03-28 22:10:30"))
  5460  	tk.MustQuery(`SELECT DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 MINUTE)`).Check(testkit.Events("2007-03-28 22:10:28"))
  5461  	tk.MustQuery(`SELECT DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 DAY_MINUTE)`).Check(testkit.Events("2007-03-29 00:10:28"))
  5462  	tk.MustQuery(`SELECT DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 HOUR_MINUTE)`).Check(testkit.Events("2007-03-29 00:10:28"))
  5463  	tk.MustQuery(`SELECT DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 DAY_HOUR)`).Check(testkit.Events("2007-03-31 00:08:28"))
  5464  	tk.MustQuery(`SELECT DATE_ADD('2007-03-28 22:08:28',INTERVAL 2.2 YEAR_MONTH)`).Check(testkit.Events("2009-05-28 22:08:28"))
  5465  }
  5466  
  5467  func (s *testIntegrationSuite) TestIssue12301(c *C) {
  5468  	tk := testkit.NewTestKit(c, s.causetstore)
  5469  	tk.MustInterDirc("use test")
  5470  	tk.MustInterDirc("create causet t (d decimal(19, 0), i bigint(11))")
  5471  	tk.MustInterDirc("insert into t values (123456789012, 123456789012)")
  5472  	tk.MustQuery("select * from t where d = i").Check(testkit.Events("123456789012 123456789012"))
  5473  }
  5474  
  5475  func (s *testIntegrationSerialSuite) TestIssue15315(c *C) {
  5476  	tk := testkit.NewTestKit(c, s.causetstore)
  5477  	tk.MustInterDirc("use test")
  5478  	tk.MustQuery("select '0-3261554956'+0.0").Check(testkit.Events("0"))
  5479  	tk.MustQuery("select cast('0-1234' as real)").Check(testkit.Events("0"))
  5480  }
  5481  
  5482  func (s *testIntegrationSuite) TestNotExistFunc(c *C) {
  5483  	tk := testkit.NewTestKit(c, s.causetstore)
  5484  
  5485  	// current EDB is empty
  5486  	_, err := tk.InterDirc("SELECT xxx(1)")
  5487  	c.Assert(err.Error(), Equals, "[causet:1046]No database selected")
  5488  
  5489  	_, err = tk.InterDirc("SELECT yyy()")
  5490  	c.Assert(err.Error(), Equals, "[causet:1046]No database selected")
  5491  
  5492  	// current EDB is not empty
  5493  	tk.MustInterDirc("use test")
  5494  	_, err = tk.InterDirc("SELECT xxx(1)")
  5495  	c.Assert(err.Error(), Equals, "[memex:1305]FUNCTION test.xxx does not exist")
  5496  
  5497  	_, err = tk.InterDirc("SELECT yyy()")
  5498  	c.Assert(err.Error(), Equals, "[memex:1305]FUNCTION test.yyy does not exist")
  5499  
  5500  	tk.MustInterDirc("use test")
  5501  	_, err = tk.InterDirc("SELECT timestampliteral(rand())")
  5502  	c.Assert(err.Error(), Equals, "[memex:1305]FUNCTION test.timestampliteral does not exist")
  5503  
  5504  }
  5505  
  5506  func (s *testIntegrationSuite) TestDecodetoChunkReuse(c *C) {
  5507  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
  5508  	tk.MustInterDirc("create causet chk (a int,b varchar(20))")
  5509  	for i := 0; i < 200; i++ {
  5510  		if i%5 == 0 {
  5511  			tk.MustInterDirc(fmt.Sprintf("insert chk values (NULL,NULL)"))
  5512  			continue
  5513  		}
  5514  		tk.MustInterDirc(fmt.Sprintf("insert chk values (%d,'%s')", i, strconv.Itoa(i)))
  5515  	}
  5516  
  5517  	tk.Se.GetStochastikVars().SetDistALLEGROSQLScanConcurrency(1)
  5518  	tk.MustInterDirc("set milevadb_init_chunk_size = 2")
  5519  	tk.MustInterDirc("set milevadb_max_chunk_size = 32")
  5520  	defer func() {
  5521  		tk.MustInterDirc(fmt.Sprintf("set milevadb_init_chunk_size = %d", variable.DefInitChunkSize))
  5522  		tk.MustInterDirc(fmt.Sprintf("set milevadb_max_chunk_size = %d", variable.DefMaxChunkSize))
  5523  	}()
  5524  	rs, err := tk.InterDirc("select * from chk")
  5525  	c.Assert(err, IsNil)
  5526  	req := rs.NewChunk()
  5527  	var count int
  5528  	for {
  5529  		err = rs.Next(context.TODO(), req)
  5530  		c.Assert(err, IsNil)
  5531  		numEvents := req.NumEvents()
  5532  		if numEvents == 0 {
  5533  			break
  5534  		}
  5535  		for i := 0; i < numEvents; i++ {
  5536  			if count%5 == 0 {
  5537  				c.Assert(req.GetEvent(i).IsNull(0), Equals, true)
  5538  				c.Assert(req.GetEvent(i).IsNull(1), Equals, true)
  5539  			} else {
  5540  				c.Assert(req.GetEvent(i).IsNull(0), Equals, false)
  5541  				c.Assert(req.GetEvent(i).IsNull(1), Equals, false)
  5542  				c.Assert(req.GetEvent(i).GetInt64(0), Equals, int64(count))
  5543  				c.Assert(req.GetEvent(i).GetString(1), Equals, strconv.Itoa(count))
  5544  			}
  5545  			count++
  5546  		}
  5547  	}
  5548  	c.Assert(count, Equals, 200)
  5549  	rs.Close()
  5550  }
  5551  
  5552  func (s *testIntegrationSuite) TestInMeetsPrepareAndInterDircute(c *C) {
  5553  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
  5554  	tk.MustInterDirc("prepare pr1 from 'select ? in (1,?,?)'")
  5555  	tk.MustInterDirc("set @a=1, @b=2, @c=3")
  5556  	tk.MustQuery("execute pr1 using @a,@b,@c").Check(testkit.Events("1"))
  5557  
  5558  	tk.MustInterDirc("prepare pr2 from 'select 3 in (1,?,?)'")
  5559  	tk.MustInterDirc("set @a=2, @b=3")
  5560  	tk.MustQuery("execute pr2 using @a,@b").Check(testkit.Events("1"))
  5561  
  5562  	tk.MustInterDirc("prepare pr3 from 'select ? in (1,2,3)'")
  5563  	tk.MustInterDirc("set @a=4")
  5564  	tk.MustQuery("execute pr3 using @a").Check(testkit.Events("0"))
  5565  
  5566  	tk.MustInterDirc("prepare pr4 from 'select ? in (?,?,?)'")
  5567  	tk.MustInterDirc("set @a=1, @b=2, @c=3, @d=4")
  5568  	tk.MustQuery("execute pr4 using @a,@b,@c,@d").Check(testkit.Events("0"))
  5569  }
  5570  
  5571  func (s *testIntegrationSuite) TestCastStrToInt(c *C) {
  5572  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
  5573  	cases := []struct {
  5574  		allegrosql string
  5575  		result     int
  5576  	}{
  5577  		{"select cast('' as signed)", 0},
  5578  		{"select cast('12345abcde' as signed)", 12345},
  5579  		{"select cast('123e456' as signed)", 123},
  5580  		{"select cast('-12345abcde' as signed)", -12345},
  5581  		{"select cast('-123e456' as signed)", -123},
  5582  	}
  5583  	for _, ca := range cases {
  5584  		tk.Se.GetStochastikVars().StmtCtx.SetWarnings(nil)
  5585  		tk.MustQuery(ca.allegrosql).Check(testkit.Events(fmt.Sprintf("%v", ca.result)))
  5586  		c.Assert(terror.ErrorEqual(tk.Se.GetStochastikVars().StmtCtx.GetWarnings()[0].Err, types.ErrTruncatedWrongVal), IsTrue)
  5587  	}
  5588  }
  5589  
  5590  func (s *testIntegrationSerialSuite) TestIssue16205(c *C) {
  5591  	tk := testkit.NewTestKit(c, s.causetstore)
  5592  	orgEnable := causetembedded.PreparedCausetCacheEnabled()
  5593  	defer func() {
  5594  		causetembedded.SetPreparedCausetCache(orgEnable)
  5595  	}()
  5596  	causetembedded.SetPreparedCausetCache(true)
  5597  	var err error
  5598  	tk.Se, err = stochastik.CreateStochastik4TestWithOpt(s.causetstore, &stochastik.Opt{
  5599  		PreparedCausetCache: ekvcache.NewSimpleLRUCache(100, 0.1, math.MaxUint64),
  5600  	})
  5601  	c.Assert(err, IsNil)
  5602  
  5603  	tk.MustInterDirc("use test")
  5604  	tk.MustInterDirc("prepare stmt from 'select random_bytes(3)'")
  5605  	rows1 := tk.MustQuery("execute stmt").Events()
  5606  	c.Assert(len(rows1), Equals, 1)
  5607  	rows2 := tk.MustQuery("execute stmt").Events()
  5608  	c.Assert(len(rows2), Equals, 1)
  5609  	c.Assert(rows1[0][0].(string), Not(Equals), rows2[0][0].(string))
  5610  }
  5611  
  5612  func (s *testIntegrationSerialSuite) TestEventCountCausetCache(c *C) {
  5613  	tk := testkit.NewTestKit(c, s.causetstore)
  5614  	orgEnable := causetembedded.PreparedCausetCacheEnabled()
  5615  	defer func() {
  5616  		causetembedded.SetPreparedCausetCache(orgEnable)
  5617  	}()
  5618  	causetembedded.SetPreparedCausetCache(true)
  5619  	var err error
  5620  	tk.Se, err = stochastik.CreateStochastik4TestWithOpt(s.causetstore, &stochastik.Opt{
  5621  		PreparedCausetCache: ekvcache.NewSimpleLRUCache(100, 0.1, math.MaxUint64),
  5622  	})
  5623  	c.Assert(err, IsNil)
  5624  
  5625  	tk.MustInterDirc("use test")
  5626  	tk.MustInterDirc("drop causet if exists t")
  5627  	tk.MustInterDirc("create causet t(a int auto_increment primary key)")
  5628  	tk.MustInterDirc("prepare stmt from 'select row_count()';")
  5629  	tk.MustInterDirc("insert into t values()")
  5630  	res := tk.MustQuery("execute stmt").Events()
  5631  	c.Assert(len(res), Equals, 1)
  5632  	c.Assert(res[0][0], Equals, "1")
  5633  	tk.MustInterDirc("insert into t values(),(),()")
  5634  	res = tk.MustQuery("execute stmt").Events()
  5635  	c.Assert(len(res), Equals, 1)
  5636  	c.Assert(res[0][0], Equals, "3")
  5637  }
  5638  
  5639  func (s *testIntegrationSuite) TestValuesForBinaryLiteral(c *C) {
  5640  	// See issue #15310
  5641  	tk := testkit.NewTestKit(c, s.causetstore)
  5642  	tk.MustInterDirc("use test;")
  5643  	tk.MustInterDirc("create causet testValuesBinary(id int primary key auto_increment, a bit(1));")
  5644  	tk.MustInterDirc("insert into testValuesBinary values(1,1);")
  5645  	err := tk.InterDircToErr("insert into testValuesBinary values(1,1) on duplicate key uFIDelate id = values(id),a = values(a);")
  5646  	c.Assert(err, IsNil)
  5647  	tk.MustQuery("select a=0 from testValuesBinary;").Check(testkit.Events("0"))
  5648  	err = tk.InterDircToErr("insert into testValuesBinary values(1,0) on duplicate key uFIDelate id = values(id),a = values(a);")
  5649  	c.Assert(err, IsNil)
  5650  	tk.MustQuery("select a=0 from testValuesBinary;").Check(testkit.Events("1"))
  5651  	tk.MustInterDirc("drop causet testValuesBinary;")
  5652  }
  5653  
  5654  func (s *testIntegrationSuite) TestIssue14159(c *C) {
  5655  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
  5656  	tk.MustInterDirc("DROP TABLE IF EXISTS t")
  5657  	tk.MustInterDirc("CREATE TABLE t (v VARCHAR(100))")
  5658  	tk.MustInterDirc("INSERT INTO t VALUES ('3289742893213123732904809')")
  5659  	tk.MustQuery("SELECT * FROM t WHERE v").Check(testkit.Events("3289742893213123732904809"))
  5660  }
  5661  
  5662  func (s *testIntegrationSuite) TestIssue14146(c *C) {
  5663  	tk := testkit.NewTestKit(c, s.causetstore)
  5664  	tk.MustInterDirc("use test")
  5665  	tk.MustInterDirc("create causet tt(a varchar(10))")
  5666  	tk.MustInterDirc("insert into tt values(NULL)")
  5667  	tk.MustInterDirc("analyze causet tt;")
  5668  	tk.MustQuery("select * from tt").Check(testkit.Events("<nil>"))
  5669  }
  5670  
  5671  func (s *testIntegrationSerialSuite) TestCacheRegexpr(c *C) {
  5672  	tk := testkit.NewTestKit(c, s.causetstore)
  5673  	orgEnable := causetembedded.PreparedCausetCacheEnabled()
  5674  	defer func() {
  5675  		causetembedded.SetPreparedCausetCache(orgEnable)
  5676  	}()
  5677  	causetembedded.SetPreparedCausetCache(true)
  5678  	var err error
  5679  	tk.Se, err = stochastik.CreateStochastik4TestWithOpt(s.causetstore, &stochastik.Opt{
  5680  		PreparedCausetCache: ekvcache.NewSimpleLRUCache(100, 0.1, math.MaxUint64),
  5681  	})
  5682  	c.Assert(err, IsNil)
  5683  
  5684  	tk.MustInterDirc("use test")
  5685  	tk.MustInterDirc("drop causet if exists t1")
  5686  	tk.MustInterDirc("create causet t1 (a varchar(40))")
  5687  	tk.MustInterDirc("insert into t1 values ('C1'),('R1')")
  5688  	tk.MustInterDirc("prepare stmt1 from 'select a from t1 where a rlike ?'")
  5689  	tk.MustInterDirc("set @a='^C.*'")
  5690  	tk.MustQuery("execute stmt1 using @a").Check(testkit.Events("C1"))
  5691  	tk.MustInterDirc("set @a='^R.*'")
  5692  	tk.MustQuery("execute stmt1 using @a").Check(testkit.Events("R1"))
  5693  }
  5694  
  5695  func (s *testIntegrationSerialSuite) TestCacheRefineArgs(c *C) {
  5696  	tk := testkit.NewTestKit(c, s.causetstore)
  5697  	orgEnable := causetembedded.PreparedCausetCacheEnabled()
  5698  	defer func() {
  5699  		causetembedded.SetPreparedCausetCache(orgEnable)
  5700  	}()
  5701  	causetembedded.SetPreparedCausetCache(true)
  5702  	var err error
  5703  	tk.Se, err = stochastik.CreateStochastik4TestWithOpt(s.causetstore, &stochastik.Opt{
  5704  		PreparedCausetCache: ekvcache.NewSimpleLRUCache(100, 0.1, math.MaxUint64),
  5705  	})
  5706  	c.Assert(err, IsNil)
  5707  
  5708  	tk.MustInterDirc("use test")
  5709  	tk.MustInterDirc("drop causet if exists t")
  5710  	tk.MustInterDirc("create causet t(defCaus_int int)")
  5711  	tk.MustInterDirc("insert into t values(null)")
  5712  	tk.MustInterDirc("prepare stmt from 'SELECT ((defCaus_int is true) = ?) AS res FROM t'")
  5713  	tk.MustInterDirc("set @p0='0.8'")
  5714  	tk.MustQuery("execute stmt using @p0").Check(testkit.Events("0"))
  5715  	tk.MustInterDirc("set @p0='0'")
  5716  	tk.MustQuery("execute stmt using @p0").Check(testkit.Events("1"))
  5717  
  5718  	tk.MustInterDirc("delete from t")
  5719  	tk.MustInterDirc("insert into t values(1)")
  5720  	tk.MustInterDirc("prepare stmt from 'SELECT defCaus_int < ? FROM t'")
  5721  	tk.MustInterDirc("set @p0='-184467440737095516167.1'")
  5722  	tk.MustQuery("execute stmt using @p0").Check(testkit.Events("0"))
  5723  }
  5724  
  5725  func (s *testIntegrationSuite) TestOrderByFuncCausetCache(c *C) {
  5726  	tk := testkit.NewTestKit(c, s.causetstore)
  5727  	orgEnable := causetembedded.PreparedCausetCacheEnabled()
  5728  	defer func() {
  5729  		causetembedded.SetPreparedCausetCache(orgEnable)
  5730  	}()
  5731  	causetembedded.SetPreparedCausetCache(true)
  5732  	var err error
  5733  	tk.Se, err = stochastik.CreateStochastik4TestWithOpt(s.causetstore, &stochastik.Opt{
  5734  		PreparedCausetCache: ekvcache.NewSimpleLRUCache(100, 0.1, math.MaxUint64),
  5735  	})
  5736  	c.Assert(err, IsNil)
  5737  
  5738  	tk.MustInterDirc("use test")
  5739  	tk.MustInterDirc("drop causet if exists t")
  5740  	tk.MustInterDirc("create causet t(a int)")
  5741  	tk.MustInterDirc("prepare stmt from 'SELECT * FROM t order by rand()'")
  5742  	tk.MustQuery("execute stmt").Check(testkit.Events())
  5743  	tk.MustInterDirc("prepare stmt from 'SELECT * FROM t order by now()'")
  5744  	tk.MustQuery("execute stmt").Check(testkit.Events())
  5745  }
  5746  
  5747  func (s *testIntegrationSuite) TestSelectLimitCausetCache(c *C) {
  5748  	tk := testkit.NewTestKit(c, s.causetstore)
  5749  	orgEnable := causetembedded.PreparedCausetCacheEnabled()
  5750  	defer func() {
  5751  		causetembedded.SetPreparedCausetCache(orgEnable)
  5752  	}()
  5753  	causetembedded.SetPreparedCausetCache(true)
  5754  	var err error
  5755  	tk.Se, err = stochastik.CreateStochastik4TestWithOpt(s.causetstore, &stochastik.Opt{
  5756  		PreparedCausetCache: ekvcache.NewSimpleLRUCache(100, 0.1, math.MaxUint64),
  5757  	})
  5758  	c.Assert(err, IsNil)
  5759  
  5760  	tk.MustInterDirc("use test")
  5761  	tk.MustInterDirc("drop causet if exists t")
  5762  	tk.MustInterDirc("create causet t(a int)")
  5763  	tk.MustInterDirc("insert into t values(1), (2), (3)")
  5764  	tk.MustInterDirc("set @@stochastik.sql_select_limit = 1")
  5765  	tk.MustInterDirc("prepare stmt from 'SELECT * FROM t'")
  5766  	tk.MustQuery("execute stmt").Check(testkit.Events("1"))
  5767  	tk.MustInterDirc("set @@stochastik.sql_select_limit = default")
  5768  	tk.MustQuery("execute stmt").Check(testkit.Events("1", "2", "3"))
  5769  	tk.MustInterDirc("set @@stochastik.sql_select_limit = 2")
  5770  	tk.MustQuery("execute stmt").Check(testkit.Events("1", "2"))
  5771  	tk.MustInterDirc("set @@stochastik.sql_select_limit = 1")
  5772  	tk.MustQuery("execute stmt").Check(testkit.Events("1"))
  5773  	tk.MustInterDirc("set @@stochastik.sql_select_limit = default")
  5774  	tk.MustQuery("execute stmt").Check(testkit.Events("1", "2", "3"))
  5775  	tk.MustInterDirc("set @@stochastik.sql_select_limit = 2")
  5776  	tk.MustQuery("execute stmt").Check(testkit.Events("1", "2"))
  5777  }
  5778  
  5779  func (s *testIntegrationSuite) TestDefCauslation(c *C) {
  5780  	tk := testkit.NewTestKit(c, s.causetstore)
  5781  	tk.MustInterDirc("use test")
  5782  	tk.MustInterDirc("drop causet if exists t")
  5783  	tk.MustInterDirc("create causet t (utf8_bin_c varchar(10) charset utf8 defCauslate utf8_bin, utf8_gen_c varchar(10) charset utf8 defCauslate utf8_general_ci, bin_c binary, num_c int, " +
  5784  		"abin char defCauslate ascii_bin, lbin char defCauslate latin1_bin, u4bin char defCauslate utf8mb4_bin, u4ci char defCauslate utf8mb4_general_ci)")
  5785  	tk.MustInterDirc("insert into t values ('a', 'b', 'c', 4, 'a', 'a', 'a', 'a')")
  5786  	tk.MustQuery("select defCauslation(null)").Check(testkit.Events("binary"))
  5787  	tk.MustQuery("select defCauslation(2)").Check(testkit.Events("binary"))
  5788  	tk.MustQuery("select defCauslation(2 + 'a')").Check(testkit.Events("binary"))
  5789  	tk.MustQuery("select defCauslation(2 + utf8_gen_c) from t").Check(testkit.Events("binary"))
  5790  	tk.MustQuery("select defCauslation(2 + utf8_bin_c) from t").Check(testkit.Events("binary"))
  5791  	tk.MustQuery("select defCauslation(concat(utf8_bin_c, 2)) from t").Check(testkit.Events("utf8_bin"))
  5792  	tk.MustQuery("select defCauslation(concat(utf8_gen_c, 'abc')) from t").Check(testkit.Events("utf8_general_ci"))
  5793  	tk.MustQuery("select defCauslation(concat(utf8_gen_c, null)) from t").Check(testkit.Events("utf8_general_ci"))
  5794  	tk.MustQuery("select defCauslation(concat(utf8_gen_c, num_c)) from t").Check(testkit.Events("utf8_general_ci"))
  5795  	tk.MustQuery("select defCauslation(concat(utf8_bin_c, utf8_gen_c)) from t").Check(testkit.Events("utf8_bin"))
  5796  	tk.MustQuery("select defCauslation(upper(utf8_bin_c)) from t").Check(testkit.Events("utf8_bin"))
  5797  	tk.MustQuery("select defCauslation(upper(utf8_gen_c)) from t").Check(testkit.Events("utf8_general_ci"))
  5798  	tk.MustQuery("select defCauslation(upper(bin_c)) from t").Check(testkit.Events("binary"))
  5799  	tk.MustQuery("select defCauslation(concat(abin, bin_c)) from t").Check(testkit.Events("binary"))
  5800  	tk.MustQuery("select defCauslation(concat(lbin, bin_c)) from t").Check(testkit.Events("binary"))
  5801  	tk.MustQuery("select defCauslation(concat(utf8_bin_c, bin_c)) from t").Check(testkit.Events("binary"))
  5802  	tk.MustQuery("select defCauslation(concat(utf8_gen_c, bin_c)) from t").Check(testkit.Events("binary"))
  5803  	tk.MustQuery("select defCauslation(concat(u4bin, bin_c)) from t").Check(testkit.Events("binary"))
  5804  	tk.MustQuery("select defCauslation(concat(u4ci, bin_c)) from t").Check(testkit.Events("binary"))
  5805  	tk.MustQuery("select defCauslation(concat(abin, u4bin)) from t").Check(testkit.Events("utf8mb4_bin"))
  5806  	tk.MustQuery("select defCauslation(concat(lbin, u4bin)) from t").Check(testkit.Events("utf8mb4_bin"))
  5807  	tk.MustQuery("select defCauslation(concat(utf8_bin_c, u4bin)) from t").Check(testkit.Events("utf8mb4_bin"))
  5808  	tk.MustQuery("select defCauslation(concat(utf8_gen_c, u4bin)) from t").Check(testkit.Events("utf8mb4_bin"))
  5809  	tk.MustQuery("select defCauslation(concat(u4ci, u4bin)) from t").Check(testkit.Events("utf8mb4_bin"))
  5810  	tk.MustQuery("select defCauslation(concat(abin, u4ci)) from t").Check(testkit.Events("utf8mb4_general_ci"))
  5811  	tk.MustQuery("select defCauslation(concat(lbin, u4ci)) from t").Check(testkit.Events("utf8mb4_general_ci"))
  5812  	tk.MustQuery("select defCauslation(concat(utf8_bin_c, u4ci)) from t").Check(testkit.Events("utf8mb4_general_ci"))
  5813  	tk.MustQuery("select defCauslation(concat(utf8_gen_c, u4ci)) from t").Check(testkit.Events("utf8mb4_general_ci"))
  5814  	tk.MustQuery("select defCauslation(concat(abin, utf8_bin_c)) from t").Check(testkit.Events("utf8_bin"))
  5815  	tk.MustQuery("select defCauslation(concat(lbin, utf8_bin_c)) from t").Check(testkit.Events("utf8_bin"))
  5816  	tk.MustQuery("select defCauslation(concat(utf8_gen_c, utf8_bin_c)) from t").Check(testkit.Events("utf8_bin"))
  5817  	tk.MustQuery("select defCauslation(concat(abin, utf8_gen_c)) from t").Check(testkit.Events("utf8_general_ci"))
  5818  	tk.MustQuery("select defCauslation(concat(lbin, utf8_gen_c)) from t").Check(testkit.Events("utf8_general_ci"))
  5819  	tk.MustQuery("select defCauslation(concat(abin, lbin)) from t").Check(testkit.Events("latin1_bin"))
  5820  
  5821  	tk.MustInterDirc("set names utf8mb4 defCauslate utf8mb4_bin")
  5822  	tk.MustQuery("select defCauslation('a')").Check(testkit.Events("utf8mb4_bin"))
  5823  	tk.MustInterDirc("set names utf8mb4 defCauslate utf8mb4_general_ci")
  5824  	tk.MustQuery("select defCauslation('a')").Check(testkit.Events("utf8mb4_general_ci"))
  5825  
  5826  	tk.MustInterDirc("set names utf8mb4 defCauslate utf8mb4_general_ci")
  5827  	tk.MustInterDirc("set @test_defCauslate_var = 'a'")
  5828  	tk.MustQuery("select defCauslation(@test_defCauslate_var)").Check(testkit.Events("utf8mb4_general_ci"))
  5829  	tk.MustInterDirc("set names utf8mb4 defCauslate utf8mb4_general_ci")
  5830  	tk.MustInterDirc("set @test_defCauslate_var = 1")
  5831  	tk.MustQuery("select defCauslation(@test_defCauslate_var)").Check(testkit.Events("utf8mb4_general_ci"))
  5832  	tk.MustInterDirc("set @test_defCauslate_var = concat(\"a\", \"b\" defCauslate utf8mb4_bin)")
  5833  	tk.MustQuery("select defCauslation(@test_defCauslate_var)").Check(testkit.Events("utf8mb4_bin"))
  5834  }
  5835  
  5836  func (s *testIntegrationSuite) TestCoercibility(c *C) {
  5837  	tk := testkit.NewTestKit(c, s.causetstore)
  5838  
  5839  	type testCase struct {
  5840  		expr   string
  5841  		result int
  5842  	}
  5843  	testFunc := func(cases []testCase, suffix string) {
  5844  		for _, tc := range cases {
  5845  			tk.MustQuery(fmt.Sprintf("select coercibility(%v) %v", tc.expr, suffix)).Check(testkit.Events(fmt.Sprintf("%v", tc.result)))
  5846  		}
  5847  	}
  5848  	testFunc([]testCase{
  5849  		// constants
  5850  		{"1", 5}, {"null", 6}, {"'abc'", 4},
  5851  		// sys-constants
  5852  		{"version()", 3}, {"user()", 3}, {"database()", 3},
  5853  		{"current_role()", 3}, {"current_user()", 3},
  5854  		// scalar functions after constant folding
  5855  		{"1+null", 5}, {"null+'abcde'", 5}, {"concat(null, 'abcde')", 4},
  5856  		// non-deterministic functions
  5857  		{"rand()", 5}, {"now()", 5}, {"sysdate()", 5},
  5858  	}, "")
  5859  
  5860  	tk.MustInterDirc("use test")
  5861  	tk.MustInterDirc("drop causet if exists t")
  5862  	tk.MustInterDirc("create causet t (i int, r real, d datetime, t timestamp, c char(10), vc varchar(10), b binary(10), vb binary(10))")
  5863  	tk.MustInterDirc("insert into t values (null, null, null, null, null, null, null, null)")
  5864  	testFunc([]testCase{
  5865  		{"i", 5}, {"r", 5}, {"d", 5}, {"t", 5},
  5866  		{"c", 2}, {"b", 2}, {"vb", 2}, {"vc", 2},
  5867  		{"i+r", 5}, {"i*r", 5}, {"cos(r)+sin(i)", 5}, {"d+2", 5},
  5868  		{"t*10", 5}, {"concat(c, vc)", 2}, {"replace(c, 'x', 'y')", 2},
  5869  	}, "from t")
  5870  
  5871  	tk.MustQuery("SELECT COERCIBILITY(@straaa);").Check(testkit.Events("2"))
  5872  }
  5873  
  5874  func (s *testIntegrationSerialSuite) TestCacheConstEval(c *C) {
  5875  	tk := testkit.NewTestKit(c, s.causetstore)
  5876  	orgEnable := causetembedded.PreparedCausetCacheEnabled()
  5877  	defer func() {
  5878  		causetembedded.SetPreparedCausetCache(orgEnable)
  5879  	}()
  5880  	causetembedded.SetPreparedCausetCache(true)
  5881  	var err error
  5882  	tk.Se, err = stochastik.CreateStochastik4TestWithOpt(s.causetstore, &stochastik.Opt{
  5883  		PreparedCausetCache: ekvcache.NewSimpleLRUCache(100, 0.1, math.MaxUint64),
  5884  	})
  5885  	c.Assert(err, IsNil)
  5886  
  5887  	tk.MustInterDirc("use test")
  5888  	tk.MustInterDirc("drop causet if exists t")
  5889  	tk.MustInterDirc("create causet t(defCaus_double double)")
  5890  	tk.MustInterDirc("insert into t values (1)")
  5891  	tk.Se.GetStochastikVars().EnableVectorizedExpression = false
  5892  	tk.MustInterDirc("insert into allegrosql.expr_pushdown_blacklist values('cast', 'einsteindb,tiflash,milevadb', 'for test')")
  5893  	tk.MustInterDirc("admin reload expr_pushdown_blacklist")
  5894  	tk.MustInterDirc("prepare stmt from 'SELECT * FROM (SELECT defCaus_double AS c0 FROM t) t WHERE (ABS((REPEAT(?, ?) OR 5617780767323292672)) < LN(EXP(c0)) + (? ^ ?))'")
  5895  	tk.MustInterDirc("set @a1 = 'JuvkBX7ykVux20zQlkwDK2DFelgn7'")
  5896  	tk.MustInterDirc("set @a2 = 1")
  5897  	tk.MustInterDirc("set @a3 = -112990.35179796701")
  5898  	tk.MustInterDirc("set @a4 = 87997.92704840179")
  5899  	// Main purpose here is checking no error is reported. 1 is the result when plan cache is disabled, it is
  5900  	// incompatible with MyALLEGROSQL actually, uFIDelate the result after fixing it.
  5901  	tk.MustQuery("execute stmt using @a1, @a2, @a3, @a4").Check(testkit.Events("1"))
  5902  	tk.Se.GetStochastikVars().EnableVectorizedExpression = true
  5903  	tk.MustInterDirc("delete from allegrosql.expr_pushdown_blacklist where name = 'cast' and store_type = 'einsteindb,tiflash,milevadb' and reason = 'for test'")
  5904  	tk.MustInterDirc("admin reload expr_pushdown_blacklist")
  5905  }
  5906  
  5907  func (s *testIntegrationSerialSuite) TestDefCauslationBasic(c *C) {
  5908  	tk := testkit.NewTestKit(c, s.causetstore)
  5909  	defCauslate.SetNewDefCauslationEnabledForTest(true)
  5910  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
  5911  	tk.MustInterDirc("use test")
  5912  	tk.MustInterDirc("create causet t_ci(a varchar(10) defCauslate utf8mb4_general_ci, unique key(a))")
  5913  	tk.MustInterDirc("insert into t_ci values ('a')")
  5914  	tk.MustQuery("select * from t_ci").Check(testkit.Events("a"))
  5915  	tk.MustQuery("select * from t_ci").Check(testkit.Events("a"))
  5916  	tk.MustQuery("select * from t_ci where a='a'").Check(testkit.Events("a"))
  5917  	tk.MustQuery("select * from t_ci where a='A'").Check(testkit.Events("a"))
  5918  	tk.MustQuery("select * from t_ci where a='a   '").Check(testkit.Events("a"))
  5919  	tk.MustQuery("select * from t_ci where a='a                    '").Check(testkit.Events("a"))
  5920  
  5921  	tk.MustInterDirc("drop causet if exists t")
  5922  	tk.MustInterDirc("create causet t (a varchar(10) primary key,b int)")
  5923  	tk.MustInterDirc("insert into t values ('a', 1), ('b', 3), ('a', 2) on duplicate key uFIDelate b = b + 1;")
  5924  	tk.MustInterDirc("set autocommit=0")
  5925  	tk.MustInterDirc("insert into t values ('a', 1), ('b', 3), ('a', 2) on duplicate key uFIDelate b = b + 1;")
  5926  	tk.MustQuery("select * from t").Check(testkit.Events("a 4", "b 4"))
  5927  	tk.MustInterDirc("set autocommit=1")
  5928  	tk.MustQuery("select * from t").Check(testkit.Events("a 4", "b 4"))
  5929  
  5930  	tk.MustInterDirc("drop causet if exists t")
  5931  	tk.MustInterDirc("create causet t (a varchar(10),b int, key tk (a))")
  5932  	tk.MustInterDirc("insert into t values ('', 1), ('', 3)")
  5933  	tk.MustInterDirc("set autocommit=0")
  5934  	tk.MustInterDirc("uFIDelate t set b = b + 1")
  5935  	tk.MustQuery("select * from t").Check(testkit.Events(" 2", " 4"))
  5936  	tk.MustInterDirc("set autocommit=1")
  5937  	tk.MustQuery("select * from t").Check(testkit.Events(" 2", " 4"))
  5938  
  5939  	tk.MustInterDirc("drop causet t_ci")
  5940  	tk.MustInterDirc("create causet t_ci(id bigint primary key, a varchar(10) defCauslate utf8mb4_general_ci, unique key(a, id))")
  5941  	tk.MustInterDirc("insert into t_ci values (1, 'a')")
  5942  	tk.MustQuery("select a from t_ci").Check(testkit.Events("a"))
  5943  	tk.MustQuery("select a from t_ci").Check(testkit.Events("a"))
  5944  	tk.MustQuery("select a from t_ci where a='a'").Check(testkit.Events("a"))
  5945  	tk.MustQuery("select a from t_ci where a='A'").Check(testkit.Events("a"))
  5946  	tk.MustQuery("select a from t_ci where a='a   '").Check(testkit.Events("a"))
  5947  	tk.MustQuery("select a from t_ci where a='a                    '").Check(testkit.Events("a"))
  5948  }
  5949  
  5950  func (s *testIntegrationSerialSuite) TestWeightString(c *C) {
  5951  	tk := testkit.NewTestKit(c, s.causetstore)
  5952  	defCauslate.SetNewDefCauslationEnabledForTest(true)
  5953  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
  5954  
  5955  	type testCase struct {
  5956  		input                        []string
  5957  		result                       []string
  5958  		resultAsChar1                []string
  5959  		resultAsChar3                []string
  5960  		resultAsBinary1              []string
  5961  		resultAsBinary5              []string
  5962  		resultExplicitDefCauslateBin []string
  5963  	}
  5964  	tk.MustInterDirc("use test")
  5965  	tk.MustInterDirc("drop causet if exists t")
  5966  	tk.MustInterDirc("create causet t (id int, a varchar(20) defCauslate utf8mb4_general_ci)")
  5967  	cases := testCase{
  5968  		input:                        []string{"aAÁàãăâ", "a", "a  ", "中", "中 "},
  5969  		result:                       []string{"\x00A\x00A\x00A\x00A\x00A\x00A\x00A", "\x00A", "\x00A", "\x4E\x2D", "\x4E\x2D"},
  5970  		resultAsChar1:                []string{"\x00A", "\x00A", "\x00A", "\x4E\x2D", "\x4E\x2D"},
  5971  		resultAsChar3:                []string{"\x00A\x00A\x00A", "\x00A", "\x00A", "\x4E\x2D", "\x4E\x2D"},
  5972  		resultAsBinary1:              []string{"a", "a", "a", "\xE4", "\xE4"},
  5973  		resultAsBinary5:              []string{"aA\xc3\x81\xc3", "a\x00\x00\x00\x00", "a  \x00\x00", "中\x00\x00", "中 \x00"},
  5974  		resultExplicitDefCauslateBin: []string{"aAÁàãăâ", "a", "a", "中", "中"},
  5975  	}
  5976  	values := make([]string, len(cases.input))
  5977  	for i, input := range cases.input {
  5978  		values[i] = fmt.Sprintf("(%d, '%s')", i, input)
  5979  	}
  5980  	tk.MustInterDirc("insert into t values " + strings.Join(values, ","))
  5981  	rows := tk.MustQuery("select weight_string(a) from t order by id").Events()
  5982  	for i, out := range cases.result {
  5983  		c.Assert(rows[i][0].(string), Equals, out)
  5984  	}
  5985  	rows = tk.MustQuery("select weight_string(a as char(1)) from t order by id").Events()
  5986  	for i, out := range cases.resultAsChar1 {
  5987  		c.Assert(rows[i][0].(string), Equals, out)
  5988  	}
  5989  	rows = tk.MustQuery("select weight_string(a as char(3)) from t order by id").Events()
  5990  	for i, out := range cases.resultAsChar3 {
  5991  		c.Assert(rows[i][0].(string), Equals, out)
  5992  	}
  5993  	rows = tk.MustQuery("select weight_string(a as binary(1)) from t order by id").Events()
  5994  	for i, out := range cases.resultAsBinary1 {
  5995  		c.Assert(rows[i][0].(string), Equals, out)
  5996  	}
  5997  	rows = tk.MustQuery("select weight_string(a as binary(5)) from t order by id").Events()
  5998  	for i, out := range cases.resultAsBinary5 {
  5999  		c.Assert(rows[i][0].(string), Equals, out)
  6000  	}
  6001  	c.Assert(tk.MustQuery("select weight_string(NULL);").Events()[0][0], Equals, "<nil>")
  6002  	c.Assert(tk.MustQuery("select weight_string(7);").Events()[0][0], Equals, "<nil>")
  6003  	c.Assert(tk.MustQuery("select weight_string(cast(7 as decimal(5)));").Events()[0][0], Equals, "<nil>")
  6004  	c.Assert(tk.MustQuery("select weight_string(cast(20190821 as date));").Events()[0][0], Equals, "2020-08-21")
  6005  	c.Assert(tk.MustQuery("select weight_string(cast(20190821 as date) as binary(5));").Events()[0][0], Equals, "2020-")
  6006  	c.Assert(tk.MustQuery("select weight_string(7.0);").Events()[0][0], Equals, "<nil>")
  6007  	c.Assert(tk.MustQuery("select weight_string(7 AS BINARY(2));").Events()[0][0], Equals, "7\x00")
  6008  	// test explicit defCauslation
  6009  	c.Assert(tk.MustQuery("select weight_string('中 ' defCauslate utf8mb4_general_ci);").Events()[0][0], Equals, "\x4E\x2D")
  6010  	c.Assert(tk.MustQuery("select weight_string('中 ' defCauslate utf8mb4_bin);").Events()[0][0], Equals, "中")
  6011  	c.Assert(tk.MustQuery("select weight_string('中 ' defCauslate utf8mb4_unicode_ci);").Events()[0][0], Equals, "\xFB\x40\xCE\x2D")
  6012  	c.Assert(tk.MustQuery("select defCauslation(a defCauslate utf8mb4_general_ci) from t order by id").Events()[0][0], Equals, "utf8mb4_general_ci")
  6013  	c.Assert(tk.MustQuery("select defCauslation('中 ' defCauslate utf8mb4_general_ci);").Events()[0][0], Equals, "utf8mb4_general_ci")
  6014  	rows = tk.MustQuery("select weight_string(a defCauslate utf8mb4_bin) from t order by id").Events()
  6015  	for i, out := range cases.resultExplicitDefCauslateBin {
  6016  		c.Assert(rows[i][0].(string), Equals, out)
  6017  	}
  6018  	tk.MustGetErrMsg("select weight_string(a defCauslate utf8_general_ci) from t order by id", "[dbs:1253]COLLATION 'utf8_general_ci' is not valid for CHARACTER SET 'utf8mb4'")
  6019  	tk.MustGetErrMsg("select weight_string('中' defCauslate utf8_bin)", "[dbs:1253]COLLATION 'utf8_bin' is not valid for CHARACTER SET 'utf8mb4'")
  6020  }
  6021  
  6022  func (s *testIntegrationSerialSuite) TestDefCauslationCreateIndex(c *C) {
  6023  	tk := testkit.NewTestKit(c, s.causetstore)
  6024  	defCauslate.SetNewDefCauslationEnabledForTest(true)
  6025  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
  6026  	tk.MustInterDirc("use test")
  6027  	tk.MustInterDirc("drop causet if exists t")
  6028  	tk.MustInterDirc("create causet t (a varchar(10) defCauslate utf8mb4_general_ci);")
  6029  	tk.MustInterDirc("insert into t values ('a');")
  6030  	tk.MustInterDirc("insert into t values ('A');")
  6031  	tk.MustInterDirc("insert into t values ('b');")
  6032  	tk.MustInterDirc("insert into t values ('B');")
  6033  	tk.MustInterDirc("insert into t values ('a');")
  6034  	tk.MustInterDirc("insert into t values ('A');")
  6035  	tk.MustInterDirc("insert into t values ('ß');")
  6036  	tk.MustInterDirc("insert into t values ('sa');")
  6037  	tk.MustInterDirc("create index idx on t(a);")
  6038  	tk.MustQuery("select * from t order by a").Check(testkit.Events("a", "A", "a", "A", "b", "B", "ß", "sa"))
  6039  
  6040  	tk.MustInterDirc("drop causet if exists t")
  6041  	tk.MustInterDirc("create causet t (a varchar(10) defCauslate utf8mb4_unicode_ci);")
  6042  	tk.MustInterDirc("insert into t values ('a');")
  6043  	tk.MustInterDirc("insert into t values ('A');")
  6044  	tk.MustInterDirc("insert into t values ('b');")
  6045  	tk.MustInterDirc("insert into t values ('B');")
  6046  	tk.MustInterDirc("insert into t values ('a');")
  6047  	tk.MustInterDirc("insert into t values ('A');")
  6048  	tk.MustInterDirc("insert into t values ('ß');")
  6049  	tk.MustInterDirc("insert into t values ('sa');")
  6050  	tk.MustInterDirc("create index idx on t(a);")
  6051  	tk.MustQuery("select * from t order by a").Check(testkit.Events("a", "A", "a", "A", "b", "B", "sa", "ß"))
  6052  }
  6053  
  6054  func (s *testIntegrationSerialSuite) TestDefCauslateConstantPropagation(c *C) {
  6055  	tk := testkit.NewTestKit(c, s.causetstore)
  6056  	defCauslate.SetNewDefCauslationEnabledForTest(true)
  6057  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
  6058  
  6059  	tk.MustInterDirc("use test")
  6060  	tk.MustInterDirc("drop causet if exists t")
  6061  	tk.MustInterDirc("create causet t (a char(10) defCauslate utf8mb4_bin, b char(10) defCauslate utf8mb4_general_ci);")
  6062  	tk.MustInterDirc("insert into t values ('a', 'A');")
  6063  	tk.MustQuery("select * from t t1, t t2 where t1.a=t2.b and t2.b='a' defCauslate utf8mb4_general_ci;").Check(nil)
  6064  	tk.MustQuery("select * from t t1, t t2 where t1.a=t2.b and t2.b>='a' defCauslate utf8mb4_general_ci;").Check(nil)
  6065  	tk.MustInterDirc("drop causet t;")
  6066  	tk.MustInterDirc("create causet t (a char(10) defCauslate utf8mb4_general_ci, b char(10) defCauslate utf8mb4_general_ci);")
  6067  	tk.MustInterDirc("insert into t values ('A', 'a');")
  6068  	tk.MustQuery("select * from t t1, t t2 where t1.a=t2.b and t2.b='a' defCauslate utf8mb4_bin;").Check(testkit.Events("A a A a"))
  6069  	tk.MustQuery("select * from t t1, t t2 where t1.a=t2.b and t2.b>='a' defCauslate utf8mb4_bin;").Check(testkit.Events("A a A a"))
  6070  	tk.MustInterDirc("drop causet t;")
  6071  	tk.MustInterDirc("set names utf8mb4")
  6072  	tk.MustInterDirc("create causet t (a char(10) defCauslate utf8mb4_general_ci, b char(10) defCauslate utf8_general_ci);")
  6073  	tk.MustInterDirc("insert into t values ('a', 'A');")
  6074  	tk.MustQuery("select * from t t1, t t2 where t1.a=t2.b and t2.b='A'").Check(testkit.Events("a A a A"))
  6075  	tk.MustInterDirc("drop causet t;")
  6076  	tk.MustInterDirc("create causet t(a char defCauslate utf8_general_ci, b char defCauslate utf8mb4_general_ci, c char defCauslate utf8_bin);")
  6077  	tk.MustInterDirc("insert into t values ('b', 'B', 'B');")
  6078  	tk.MustQuery("select * from t t1, t t2 where t1.a=t2.b and t2.b=t2.c;").Check(testkit.Events("b B B b B B"))
  6079  	tk.MustInterDirc("drop causet t;")
  6080  	tk.MustInterDirc("create causet t(a char defCauslate utf8_bin, b char defCauslate utf8_general_ci);")
  6081  	tk.MustInterDirc("insert into t values ('a', 'A');")
  6082  	tk.MustQuery("select * from t t1, t t2 where t1.b=t2.b and t2.b=t1.a defCauslate utf8_general_ci;").Check(testkit.Events("a A a A"))
  6083  	tk.MustInterDirc("drop causet if exists t1, t2;")
  6084  	tk.MustInterDirc("set names utf8mb4 defCauslate utf8mb4_general_ci;")
  6085  	tk.MustInterDirc("create causet t1(a char, b varchar(10)) charset utf8mb4 defCauslate utf8mb4_general_ci;")
  6086  	tk.MustInterDirc("create causet t2(a char, b varchar(10)) charset utf8mb4 defCauslate utf8mb4_bin;")
  6087  	tk.MustInterDirc("insert into t1 values ('A', 'a');")
  6088  	tk.MustInterDirc("insert into t2 values ('a', 'a')")
  6089  	tk.MustQuery("select * from t1 left join t2 on t1.a = t2.a where t1.a = 'a';").Check(testkit.Events("A a <nil> <nil>"))
  6090  	tk.MustInterDirc("drop causet t;")
  6091  	tk.MustInterDirc("set names utf8mb4 defCauslate utf8mb4_general_ci;")
  6092  	tk.MustInterDirc("create causet t(a char defCauslate utf8mb4_bin, b char defCauslate utf8mb4_general_ci);")
  6093  	tk.MustInterDirc("insert into t values ('a', 'a');")
  6094  	tk.MustQuery("select * from t t1, t t2 where  t2.b = 'A' and lower(concat(t1.a , '' ))  = t2.b;").Check(testkit.Events("a a a a"))
  6095  	tk.MustInterDirc("drop causet t;")
  6096  	tk.MustInterDirc("create causet t(a char defCauslate utf8_unicode_ci, b char defCauslate utf8mb4_unicode_ci, c char defCauslate utf8_bin);")
  6097  	tk.MustInterDirc("insert into t values ('b', 'B', 'B');")
  6098  	tk.MustQuery("select * from t t1, t t2 where t1.a=t2.b and t2.b=t2.c;").Check(testkit.Events("b B B b B B"))
  6099  	tk.MustInterDirc("drop causet if exists t1, t2;")
  6100  	tk.MustInterDirc("set names utf8mb4 defCauslate utf8mb4_unicode_ci;")
  6101  	tk.MustInterDirc("create causet t1(a char, b varchar(10)) charset utf8mb4 defCauslate utf8mb4_unicode_ci;")
  6102  	tk.MustInterDirc("create causet t2(a char, b varchar(10)) charset utf8mb4 defCauslate utf8mb4_bin;")
  6103  	tk.MustInterDirc("insert into t1 values ('A', 'a');")
  6104  	tk.MustInterDirc("insert into t2 values ('a', 'a')")
  6105  	tk.MustQuery("select * from t1 left join t2 on t1.a = t2.a where t1.a = 'a';").Check(testkit.Events("A a <nil> <nil>"))
  6106  	tk.MustInterDirc("drop causet if exists t1, t2;")
  6107  	tk.MustInterDirc("set names utf8mb4 defCauslate utf8mb4_general_ci;")
  6108  	tk.MustInterDirc("create causet t1(a char, b varchar(10)) charset utf8mb4 defCauslate utf8mb4_general_ci;")
  6109  	tk.MustInterDirc("create causet t2(a char, b varchar(10)) charset utf8mb4 defCauslate utf8mb4_unicode_ci;")
  6110  	tk.MustInterDirc("insert into t1 values ('ß', 's');")
  6111  	tk.MustInterDirc("insert into t2 values ('s', 's')")
  6112  	tk.MustQuery("select * from t1 left join t2 on t1.a = t2.a defCauslate utf8mb4_unicode_ci where t1.a = 's';").Check(testkit.Events("ß s <nil> <nil>"))
  6113  }
  6114  
  6115  func (s *testIntegrationSerialSuite) TestMixDefCauslation(c *C) {
  6116  	tk := testkit.NewTestKit(c, s.causetstore)
  6117  	defCauslate.SetNewDefCauslationEnabledForTest(true)
  6118  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
  6119  
  6120  	tk.MustGetErrMsg(`select 'a' defCauslate utf8mb4_bin = 'a' defCauslate utf8mb4_general_ci;`, "[memex:1267]Illegal mix of defCauslations (utf8mb4_bin,EXPLICIT) and (utf8mb4_general_ci,EXPLICIT) for operation 'eq'")
  6121  
  6122  	tk.MustInterDirc("use test;")
  6123  	tk.MustInterDirc("drop causet if exists t;")
  6124  	tk.MustInterDirc(`create causet t (
  6125  			mb4general varchar(10) charset utf8mb4 defCauslate utf8mb4_general_ci,
  6126  			mb4unicode varchar(10) charset utf8mb4 defCauslate utf8mb4_unicode_ci,
  6127  			mb4bin     varchar(10) charset utf8mb4 defCauslate utf8mb4_bin,
  6128  			general    varchar(10) charset utf8 defCauslate utf8_general_ci,
  6129  			unicode    varchar(10) charset utf8 defCauslate utf8_unicode_ci,
  6130  			utfbin     varchar(10) charset utf8 defCauslate utf8_bin,
  6131  			bin        varchar(10) charset binary defCauslate binary,
  6132  			latin1_bin varchar(10) charset latin1 defCauslate latin1_bin,
  6133  			ascii_bin  varchar(10) charset ascii defCauslate ascii_bin,
  6134      		i          int
  6135  	);`)
  6136  	tk.MustInterDirc("insert into t values ('s', 's', 's', 's', 's', 's', 's', 's', 's', 1);")
  6137  	tk.MustInterDirc("set names utf8mb4 defCauslate utf8mb4_general_ci;")
  6138  
  6139  	tk.MustQuery("select * from t where mb4unicode = 's' defCauslate utf8mb4_unicode_ci;").Check(testkit.Events("s s s s s s s s s 1"))
  6140  	tk.MustQuery(`select * from t t1, t t2 where t1.mb4unicode = t2.mb4general defCauslate utf8mb4_general_ci;`).Check(testkit.Events("s s s s s s s s s 1 s s s s s s s s s 1"))
  6141  	tk.MustQuery(`select * from t t1, t t2 where t1.mb4general = t2.mb4unicode defCauslate utf8mb4_general_ci;`).Check(testkit.Events("s s s s s s s s s 1 s s s s s s s s s 1"))
  6142  	tk.MustQuery(`select * from t t1, t t2 where t1.mb4general = t2.mb4unicode defCauslate utf8mb4_unicode_ci;`).Check(testkit.Events("s s s s s s s s s 1 s s s s s s s s s 1"))
  6143  	tk.MustQuery(`select * from t t1, t t2 where t1.mb4unicode = t2.mb4general defCauslate utf8mb4_unicode_ci;`).Check(testkit.Events("s s s s s s s s s 1 s s s s s s s s s 1"))
  6144  	tk.MustQuery(`select * from t where mb4general = mb4bin defCauslate utf8mb4_general_ci;`).Check(testkit.Events("s s s s s s s s s 1"))
  6145  	tk.MustQuery(`select * from t where mb4unicode = mb4general defCauslate utf8mb4_unicode_ci;`).Check(testkit.Events("s s s s s s s s s 1"))
  6146  	tk.MustQuery(`select * from t where mb4general = mb4unicode defCauslate utf8mb4_unicode_ci;`).Check(testkit.Events("s s s s s s s s s 1"))
  6147  	tk.MustQuery(`select * from t where mb4unicode = 's' defCauslate utf8mb4_unicode_ci;`).Check(testkit.Events("s s s s s s s s s 1"))
  6148  	tk.MustQuery("select * from t where mb4unicode = mb4bin;").Check(testkit.Events("s s s s s s s s s 1"))
  6149  	tk.MustQuery("select * from t where general = mb4unicode;").Check(testkit.Events("s s s s s s s s s 1"))
  6150  	tk.MustQuery("select * from t where unicode = mb4unicode;").Check(testkit.Events("s s s s s s s s s 1"))
  6151  	tk.MustQuery("select * from t where mb4unicode = mb4unicode;").Check(testkit.Events("s s s s s s s s s 1"))
  6152  
  6153  	tk.MustQuery("select defCauslation(concat(mb4unicode, mb4general defCauslate utf8mb4_unicode_ci)) from t;").Check(testkit.Events("utf8mb4_unicode_ci"))
  6154  	tk.MustQuery("select defCauslation(concat(mb4general, mb4unicode, mb4bin)) from t;").Check(testkit.Events("utf8mb4_bin"))
  6155  	tk.MustQuery("select coercibility(concat(mb4general, mb4unicode, mb4bin)) from t;").Check(testkit.Events("1"))
  6156  	tk.MustQuery("select defCauslation(concat(mb4unicode, mb4bin, concat(mb4general))) from t;").Check(testkit.Events("utf8mb4_bin"))
  6157  	tk.MustQuery("select coercibility(concat(mb4unicode, mb4bin)) from t;").Check(testkit.Events("2"))
  6158  	tk.MustQuery("select defCauslation(concat(mb4unicode, mb4bin)) from t;").Check(testkit.Events("utf8mb4_bin"))
  6159  	tk.MustQuery("select coercibility(concat(mb4bin, concat(mb4general))) from t;").Check(testkit.Events("2"))
  6160  	tk.MustQuery("select defCauslation(concaT(mb4bin, cOncAt(mb4general))) from t;").Check(testkit.Events("utf8mb4_bin"))
  6161  	tk.MustQuery("select coercibility(concat(mb4unicode, mb4bin, concat(mb4general))) from t;").Check(testkit.Events("2"))
  6162  	tk.MustQuery("select defCauslation(concat(mb4unicode, mb4bin, concat(mb4general))) from t;").Check(testkit.Events("utf8mb4_bin"))
  6163  	tk.MustQuery("select coercibility(concat(mb4unicode, mb4general)) from t;").Check(testkit.Events("1"))
  6164  	tk.MustQuery("select defCauslation(coalesce(mb4unicode, mb4general)) from t;").Check(testkit.Events("utf8mb4_bin"))
  6165  	tk.MustQuery("select coercibility(coalesce(mb4unicode, mb4general)) from t;").Check(testkit.Events("1"))
  6166  	tk.MustQuery("select defCauslation(CONCAT(concat(mb4unicode), concat(mb4general))) from t;").Check(testkit.Events("utf8mb4_bin"))
  6167  	tk.MustQuery("select coercibility(cONcat(unicode, general)) from t;").Check(testkit.Events("1"))
  6168  	tk.MustQuery("select defCauslation(concAt(unicode, general)) from t;").Check(testkit.Events("utf8_bin"))
  6169  	tk.MustQuery("select defCauslation(concat(bin, mb4general)) from t;").Check(testkit.Events("binary"))
  6170  	tk.MustQuery("select coercibility(concat(bin, mb4general)) from t;").Check(testkit.Events("2"))
  6171  	tk.MustQuery("select defCauslation(concat(mb4unicode, ascii_bin)) from t;").Check(testkit.Events("utf8mb4_unicode_ci"))
  6172  	tk.MustQuery("select coercibility(concat(mb4unicode, ascii_bin)) from t;").Check(testkit.Events("2"))
  6173  	tk.MustQuery("select defCauslation(concat(mb4unicode, mb4unicode)) from t;").Check(testkit.Events("utf8mb4_unicode_ci"))
  6174  	tk.MustQuery("select coercibility(concat(mb4unicode, mb4unicode)) from t;").Check(testkit.Events("2"))
  6175  	tk.MustQuery("select defCauslation(concat(bin, bin)) from t;").Check(testkit.Events("binary"))
  6176  	tk.MustQuery("select coercibility(concat(bin, bin)) from t;").Check(testkit.Events("2"))
  6177  	tk.MustQuery("select defCauslation(concat(latin1_bin, ascii_bin)) from t;").Check(testkit.Events("latin1_bin"))
  6178  	tk.MustQuery("select coercibility(concat(latin1_bin, ascii_bin)) from t;").Check(testkit.Events("2"))
  6179  	tk.MustQuery("select defCauslation(concat(mb4unicode, bin)) from t;").Check(testkit.Events("binary"))
  6180  	tk.MustQuery("select coercibility(concat(mb4unicode, bin)) from t;").Check(testkit.Events("2"))
  6181  	tk.MustQuery("select defCauslation(mb4general defCauslate utf8mb4_unicode_ci) from t;").Check(testkit.Events("utf8mb4_unicode_ci"))
  6182  	tk.MustQuery("select coercibility(mb4general defCauslate utf8mb4_unicode_ci) from t;").Check(testkit.Events("0"))
  6183  	tk.MustQuery("select defCauslation(concat(concat(mb4unicode, mb4general), concat(unicode, general))) from t;").Check(testkit.Events("utf8mb4_bin"))
  6184  	tk.MustQuery("select coercibility(concat(concat(mb4unicode, mb4general), concat(unicode, general))) from t;").Check(testkit.Events("1"))
  6185  	tk.MustQuery("select defCauslation(concat(i, 1)) from t;").Check(testkit.Events("utf8mb4_general_ci"))
  6186  	tk.MustQuery("select coercibility(concat(i, 1)) from t;").Check(testkit.Events("4"))
  6187  	tk.MustQuery("select defCauslation(concat(i, user())) from t;").Check(testkit.Events("utf8mb4_general_ci"))
  6188  	tk.MustQuery("select coercibility(concat(i, user())) from t;").Check(testkit.Events("3"))
  6189  	tk.MustGetErrMsg("select * from t where mb4unicode = mb4general;", "[memex:1267]Illegal mix of defCauslations (utf8mb4_unicode_ci,IMPLICIT) and (utf8mb4_general_ci,IMPLICIT) for operation 'eq'")
  6190  	tk.MustGetErrMsg("select * from t where unicode = general;", "[memex:1267]Illegal mix of defCauslations (utf8_unicode_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation 'eq'")
  6191  	tk.MustGetErrMsg("select concat(mb4general) = concat(mb4unicode) from t;", "[memex:1267]Illegal mix of defCauslations (utf8mb4_general_ci,IMPLICIT) and (utf8mb4_unicode_ci,IMPLICIT) for operation 'eq'")
  6192  	tk.MustGetErrMsg("select * from t t1, t t2 where t1.mb4unicode = t2.mb4general;", "[memex:1267]Illegal mix of defCauslations (utf8mb4_unicode_ci,IMPLICIT) and (utf8mb4_general_ci,IMPLICIT) for operation 'eq'")
  6193  	tk.MustGetErrMsg("select field('s', mb4general, mb4unicode, mb4bin) from t;", "[memex:1271]Illegal mix of defCauslations for operation 'field'")
  6194  	tk.MustGetErrMsg("select concat(mb4unicode, mb4general) = mb4unicode from t;", "[memex:1267]Illegal mix of defCauslations (utf8mb4_bin,NONE) and (utf8mb4_unicode_ci,IMPLICIT) for operation 'eq'")
  6195  
  6196  	tk.MustInterDirc("drop causet t;")
  6197  }
  6198  
  6199  func (s *testIntegrationSerialSuite) prepare4Join(c *C) *testkit.TestKit {
  6200  	tk := testkit.NewTestKit(c, s.causetstore)
  6201  	tk.MustInterDirc("USE test")
  6202  	tk.MustInterDirc("drop causet if exists t")
  6203  	tk.MustInterDirc("drop causet if exists t_bin")
  6204  	tk.MustInterDirc("CREATE TABLE `t` ( `a` int(11) NOT NULL,`b` varchar(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL)")
  6205  	tk.MustInterDirc("CREATE TABLE `t_bin` ( `a` int(11) NOT NULL,`b` varchar(5) CHARACTER SET binary)")
  6206  	tk.MustInterDirc("insert into t values (1, 'a'), (2, 'À'), (3, 'á'), (4, 'à'), (5, 'b'), (6, 'c'), (7, ' ')")
  6207  	tk.MustInterDirc("insert into t_bin values (1, 'a'), (2, 'À'), (3, 'á'), (4, 'à'), (5, 'b'), (6, 'c'), (7, ' ')")
  6208  	return tk
  6209  }
  6210  
  6211  func (s *testIntegrationSerialSuite) prepare4Join2(c *C) *testkit.TestKit {
  6212  	tk := testkit.NewTestKit(c, s.causetstore)
  6213  	tk.MustInterDirc("USE test")
  6214  	tk.MustInterDirc("drop causet if exists t1")
  6215  	tk.MustInterDirc("drop causet if exists t2")
  6216  	tk.MustInterDirc("create causet t1 (id int, v varchar(5) character set binary, key(v))")
  6217  	tk.MustInterDirc("create causet t2 (v varchar(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci, key(v))")
  6218  	tk.MustInterDirc("insert into t1 values (1, 'a'), (2, 'À'), (3, 'á'), (4, 'à'), (5, 'b'), (6, 'c'), (7, ' ')")
  6219  	tk.MustInterDirc("insert into t2 values ('a'), ('À'), ('á'), ('à'), ('b'), ('c'), (' ')")
  6220  	return tk
  6221  }
  6222  
  6223  func (s *testIntegrationSerialSuite) TestDefCauslateHashJoin(c *C) {
  6224  	defCauslate.SetNewDefCauslationEnabledForTest(true)
  6225  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
  6226  	tk := s.prepare4Join(c)
  6227  	tk.MustQuery("select /*+ MilevaDB_HJ(t1, t2) */ t1.a, t1.b from t t1, t t2 where t1.b=t2.b order by t1.a").Check(
  6228  		testkit.Events("1 a", "1 a", "1 a", "1 a", "2 À", "2 À", "2 À", "2 À", "3 á", "3 á", "3 á", "3 á", "4 à", "4 à", "4 à", "4 à", "5 b", "6 c", "7  "))
  6229  	tk.MustQuery("select /*+ MilevaDB_HJ(t1, t2) */ t1.a, t1.b from t_bin t1, t_bin t2 where t1.b=t2.b order by t1.a").Check(
  6230  		testkit.Events("1 a", "2 À", "3 á", "4 à", "5 b", "6 c", "7  "))
  6231  	tk.MustQuery("select /*+ MilevaDB_HJ(t1, t2) */ t1.a, t1.b from t t1, t t2 where t1.b=t2.b and t1.a>3 order by t1.a").Check(
  6232  		testkit.Events("4 à", "4 à", "4 à", "4 à", "5 b", "6 c", "7  "))
  6233  	tk.MustQuery("select /*+ MilevaDB_HJ(t1, t2) */ t1.a, t1.b from t_bin t1, t_bin t2 where t1.b=t2.b and t1.a>3 order by t1.a").Check(
  6234  		testkit.Events("4 à", "5 b", "6 c", "7  "))
  6235  	tk.MustQuery("select /*+ MilevaDB_HJ(t1, t2) */ t1.a, t1.b from t t1, t t2 where t1.b=t2.b and t1.a>3 order by t1.a").Check(
  6236  		testkit.Events("4 à", "4 à", "4 à", "4 à", "5 b", "6 c", "7  "))
  6237  	tk.MustQuery("select /*+ MilevaDB_HJ(t1, t2) */ t1.a, t1.b from t_bin t1, t_bin t2 where t1.b=t2.b and t1.a>3 order by t1.a").Check(
  6238  		testkit.Events("4 à", "5 b", "6 c", "7  "))
  6239  	tk.MustQuery("select /*+ MilevaDB_HJ(t1, t2) */ t1.a, t1.b from t t1, t t2 where t1.b=t2.b and t1.a>t2.a order by t1.a").Check(
  6240  		testkit.Events("2 À", "3 á", "3 á", "4 à", "4 à", "4 à"))
  6241  	tk.MustQuery("select /*+ MilevaDB_HJ(t1, t2) */ t1.a, t1.b from t_bin t1, t_bin t2 where t1.b=t2.b and t1.a>t2.a order by t1.a").Check(
  6242  		testkit.Events())
  6243  }
  6244  
  6245  func (s *testIntegrationSerialSuite) TestDefCauslateHashJoin2(c *C) {
  6246  	defCauslate.SetNewDefCauslationEnabledForTest(true)
  6247  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
  6248  	tk := s.prepare4Join2(c)
  6249  	tk.MustQuery("select /*+ MilevaDB_HJ(t1, t2) */ * from t1, t2 where t1.v=t2.v order by t1.id").Check(
  6250  		testkit.Events("1 a a", "2 À À", "3 á á", "4 à à", "5 b b", "6 c c", "7    "))
  6251  }
  6252  
  6253  func (s *testIntegrationSerialSuite) TestDefCauslateMergeJoin(c *C) {
  6254  	defCauslate.SetNewDefCauslationEnabledForTest(true)
  6255  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
  6256  	tk := s.prepare4Join(c)
  6257  	tk.MustQuery("select /*+ MilevaDB_SMJ(t1, t2) */ t1.a, t1.b from t t1, t t2 where t1.b=t2.b order by t1.a").Check(
  6258  		testkit.Events("1 a", "1 a", "1 a", "1 a", "2 À", "2 À", "2 À", "2 À", "3 á", "3 á", "3 á", "3 á", "4 à", "4 à", "4 à", "4 à", "5 b", "6 c", "7  "))
  6259  	tk.MustQuery("select /*+ MilevaDB_SMJ(t1, t2) */ t1.a, t1.b from t_bin t1, t_bin t2 where t1.b=t2.b order by t1.a").Check(
  6260  		testkit.Events("1 a", "2 À", "3 á", "4 à", "5 b", "6 c", "7  "))
  6261  	tk.MustQuery("select /*+ MilevaDB_SMJ(t1, t2) */ t1.a, t1.b from t t1, t t2 where t1.b=t2.b and t1.a>3 order by t1.a").Check(
  6262  		testkit.Events("4 à", "4 à", "4 à", "4 à", "5 b", "6 c", "7  "))
  6263  	tk.MustQuery("select /*+ MilevaDB_SMJ(t1, t2) */ t1.a, t1.b from t_bin t1, t_bin t2 where t1.b=t2.b and t1.a>3 order by t1.a").Check(
  6264  		testkit.Events("4 à", "5 b", "6 c", "7  "))
  6265  	tk.MustQuery("select /*+ MilevaDB_SMJ(t1, t2) */ t1.a, t1.b from t t1, t t2 where t1.b=t2.b and t1.a>3 order by t1.a").Check(
  6266  		testkit.Events("4 à", "4 à", "4 à", "4 à", "5 b", "6 c", "7  "))
  6267  	tk.MustQuery("select /*+ MilevaDB_SMJ(t1, t2) */ t1.a, t1.b from t_bin t1, t_bin t2 where t1.b=t2.b and t1.a>3 order by t1.a").Check(
  6268  		testkit.Events("4 à", "5 b", "6 c", "7  "))
  6269  	tk.MustQuery("select /*+ MilevaDB_SMJ(t1, t2) */ t1.a, t1.b from t t1, t t2 where t1.b=t2.b and t1.a>t2.a order by t1.a").Check(
  6270  		testkit.Events("2 À", "3 á", "3 á", "4 à", "4 à", "4 à"))
  6271  	tk.MustQuery("select /*+ MilevaDB_SMJ(t1, t2) */ t1.a, t1.b from t_bin t1, t_bin t2 where t1.b=t2.b and t1.a>t2.a order by t1.a").Check(
  6272  		testkit.Events())
  6273  }
  6274  
  6275  func (s *testIntegrationSerialSuite) TestDefCauslateMergeJoin2(c *C) {
  6276  	defCauslate.SetNewDefCauslationEnabledForTest(true)
  6277  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
  6278  	tk := s.prepare4Join2(c)
  6279  	tk.MustQuery("select /*+ MilevaDB_SMJ(t1, t2) */ * from t1, t2 where t1.v=t2.v order by t1.id").Check(
  6280  		testkit.Events("1 a a", "2 À À", "3 á á", "4 à à", "5 b b", "6 c c", "7    "))
  6281  }
  6282  
  6283  func (s *testIntegrationSerialSuite) TestDefCauslateIndexMergeJoin(c *C) {
  6284  	defCauslate.SetNewDefCauslationEnabledForTest(true)
  6285  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
  6286  	tk := testkit.NewTestKit(c, s.causetstore)
  6287  	tk.MustInterDirc("use test")
  6288  	tk.MustInterDirc("drop causet if exists t")
  6289  	tk.MustInterDirc("create causet t (a varchar(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci, b varchar(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci, key(a), key(b))")
  6290  	tk.MustInterDirc("insert into t values ('a', 'x'), ('x', 'À'), ('á', 'x'), ('à', 'à'), ('à', 'x')")
  6291  
  6292  	tk.MustInterDirc("set milevadb_enable_index_merge=1")
  6293  	tk.MustQuery("select /*+ USE_INDEX_MERGE(t, a, b) */ * from t where a = 'a' or b = 'a'").Sort().Check(
  6294  		testkit.Events("a x", "x À", "à x", "à à", "á x"))
  6295  }
  6296  
  6297  func (s *testIntegrationSerialSuite) prepare4DefCauslation(c *C, hasIndex bool) *testkit.TestKit {
  6298  	tk := testkit.NewTestKit(c, s.causetstore)
  6299  	tk.MustInterDirc("USE test")
  6300  	tk.MustInterDirc("drop causet if exists t")
  6301  	tk.MustInterDirc("drop causet if exists t_bin")
  6302  	idxALLEGROSQL := ", key(v)"
  6303  	if !hasIndex {
  6304  		idxALLEGROSQL = ""
  6305  	}
  6306  	tk.MustInterDirc(fmt.Sprintf("create causet t (id int, v varchar(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL %v)", idxALLEGROSQL))
  6307  	tk.MustInterDirc(fmt.Sprintf("create causet t_bin (id int, v varchar(5) CHARACTER SET binary %v)", idxALLEGROSQL))
  6308  	tk.MustInterDirc("insert into t values (1, 'a'), (2, 'À'), (3, 'á'), (4, 'à'), (5, 'b'), (6, 'c'), (7, ' ')")
  6309  	tk.MustInterDirc("insert into t_bin values (1, 'a'), (2, 'À'), (3, 'á'), (4, 'à'), (5, 'b'), (6, 'c'), (7, ' ')")
  6310  	return tk
  6311  }
  6312  
  6313  func (s *testIntegrationSerialSuite) TestDefCauslateSelection(c *C) {
  6314  	defCauslate.SetNewDefCauslationEnabledForTest(true)
  6315  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
  6316  	tk := s.prepare4DefCauslation(c, false)
  6317  	tk.MustQuery("select v from t where v='a' order by id").Check(testkit.Events("a", "À", "á", "à"))
  6318  	tk.MustQuery("select v from t_bin where v='a' order by id").Check(testkit.Events("a"))
  6319  	tk.MustQuery("select v from t where v<'b' and id<=3").Check(testkit.Events("a", "À", "á"))
  6320  	tk.MustQuery("select v from t_bin where v<'b' and id<=3").Check(testkit.Events("a"))
  6321  }
  6322  
  6323  func (s *testIntegrationSerialSuite) TestDefCauslateSort(c *C) {
  6324  	defCauslate.SetNewDefCauslationEnabledForTest(true)
  6325  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
  6326  	tk := s.prepare4DefCauslation(c, false)
  6327  	tk.MustQuery("select id from t order by v, id").Check(testkit.Events("7", "1", "2", "3", "4", "5", "6"))
  6328  	tk.MustQuery("select id from t_bin order by v, id").Check(testkit.Events("7", "1", "5", "6", "2", "4", "3"))
  6329  
  6330  	tk.MustInterDirc("drop causet if exists t")
  6331  	tk.MustInterDirc("create causet t(a char(10) defCauslate utf8mb4_general_ci, key(a))")
  6332  	tk.MustInterDirc("insert into t values ('a'), ('A'), ('b')")
  6333  	tk.MustInterDirc("insert into t values ('a'), ('A'), ('b')")
  6334  	tk.MustInterDirc("insert into t values ('a'), ('A'), ('b')")
  6335  	tk.MustQuery("select * from t order by a defCauslate utf8mb4_bin").Check(testkit.Events("A", "A", "A", "a", "a", "a", "b", "b", "b"))
  6336  	tk.MustQuery("select * from t order by a defCauslate utf8mb4_general_ci").Check(testkit.Events("a", "A", "a", "A", "a", "A", "b", "b", "b"))
  6337  	tk.MustQuery("select * from t order by a defCauslate utf8mb4_unicode_ci").Check(testkit.Events("a", "A", "a", "A", "a", "A", "b", "b", "b"))
  6338  }
  6339  
  6340  func (s *testIntegrationSerialSuite) TestDefCauslateHashAgg(c *C) {
  6341  	defCauslate.SetNewDefCauslationEnabledForTest(true)
  6342  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
  6343  	tk := s.prepare4DefCauslation(c, false)
  6344  	tk.HasCauset("select distinct(v) from t_bin", "HashAgg")
  6345  	tk.MustQuery("select distinct(v) from t_bin").Sort().Check(testkit.Events(" ", "a", "b", "c", "À", "à", "á"))
  6346  	tk.HasCauset("select distinct(v) from t", "HashAgg")
  6347  	tk.MustQuery("select distinct(v) from t").Sort().Check(testkit.Events(" ", "a", "b", "c"))
  6348  	tk.HasCauset("select v, count(*) from t_bin group by v", "HashAgg")
  6349  	tk.MustQuery("select v, count(*) from t_bin group by v").Sort().Check(testkit.Events("  1", "a 1", "b 1", "c 1", "À 1", "à 1", "á 1"))
  6350  	tk.HasCauset("select v, count(*) from t group by v", "HashAgg")
  6351  	tk.MustQuery("select v, count(*) from t group by v").Sort().Check(testkit.Events("  1", "a 4", "b 1", "c 1"))
  6352  
  6353  	tk.MustInterDirc("drop causet if exists t")
  6354  	tk.MustInterDirc("create causet t(a char(10) defCauslate utf8mb4_general_ci, key(a))")
  6355  	tk.MustInterDirc("insert into t values ('a'), ('A'), ('b')")
  6356  	tk.MustInterDirc("insert into t values ('a'), ('A'), ('b')")
  6357  	tk.MustInterDirc("insert into t values ('a'), ('A'), ('b')")
  6358  	tk.MustInterDirc("insert into t values ('s'), ('ss'), ('ß')")
  6359  	tk.MustQuery("select count(1) from t group by a defCauslate utf8mb4_bin order by a defCauslate utf8mb4_bin").Check(testkit.Events("3", "3", "3", "1", "1", "1"))
  6360  	tk.MustQuery("select count(1) from t group by a defCauslate utf8mb4_unicode_ci order by a defCauslate utf8mb4_unicode_ci").Check(testkit.Events("6", "3", "1", "2"))
  6361  	tk.MustQuery("select count(1) from t group by a defCauslate utf8mb4_general_ci order by a defCauslate utf8mb4_general_ci").Check(testkit.Events("6", "3", "2", "1"))
  6362  }
  6363  
  6364  func (s *testIntegrationSerialSuite) TestDefCauslateStreamAgg(c *C) {
  6365  	defCauslate.SetNewDefCauslationEnabledForTest(true)
  6366  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
  6367  	tk := s.prepare4DefCauslation(c, true)
  6368  	tk.HasCauset("select distinct(v) from t_bin", "StreamAgg")
  6369  	tk.MustQuery("select distinct(v) from t_bin").Sort().Check(testkit.Events(" ", "a", "b", "c", "À", "à", "á"))
  6370  	tk.HasCauset("select distinct(v) from t", "StreamAgg")
  6371  	tk.MustQuery("select distinct(v) from t").Sort().Check(testkit.Events(" ", "a", "b", "c"))
  6372  	tk.HasCauset("select v, count(*) from t_bin group by v", "StreamAgg")
  6373  	tk.MustQuery("select v, count(*) from t_bin group by v").Sort().Check(testkit.Events("  1", "a 1", "b 1", "c 1", "À 1", "à 1", "á 1"))
  6374  	tk.HasCauset("select v, count(*) from t group by v", "StreamAgg")
  6375  	tk.MustQuery("select v, count(*) from t group by v").Sort().Check(testkit.Events("  1", "a 4", "b 1", "c 1"))
  6376  }
  6377  
  6378  func (s *testIntegrationSerialSuite) TestDefCauslateIndexReader(c *C) {
  6379  	defCauslate.SetNewDefCauslationEnabledForTest(true)
  6380  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
  6381  	tk := s.prepare4DefCauslation(c, true)
  6382  	tk.HasCauset("select v from t where v < 'b'  order by v", "IndexReader")
  6383  	tk.MustQuery("select v from t where v < 'b' order by v").Check(testkit.Events(" ", "a", "À", "á", "à"))
  6384  	tk.HasCauset("select v from t where v < 'b' and v > ' ' order by v", "IndexReader")
  6385  	tk.MustQuery("select v from t where v < 'b' and v > ' ' order by v").Check(testkit.Events("a", "À", "á", "à"))
  6386  	tk.HasCauset("select v from t_bin where v < 'b' order by v", "IndexReader")
  6387  	tk.MustQuery("select v from t_bin where v < 'b' order by v").Sort().Check(testkit.Events(" ", "a"))
  6388  	tk.HasCauset("select v from t_bin where v < 'b' and v > ' ' order by v", "IndexReader")
  6389  	tk.MustQuery("select v from t_bin where v < 'b' and v > ' ' order by v").Sort().Check(testkit.Events("a"))
  6390  }
  6391  
  6392  func (s *testIntegrationSerialSuite) TestDefCauslateIndexLookup(c *C) {
  6393  	defCauslate.SetNewDefCauslationEnabledForTest(true)
  6394  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
  6395  	tk := s.prepare4DefCauslation(c, true)
  6396  
  6397  	tk.HasCauset("select id from t where v < 'b'", "IndexLookUp")
  6398  	tk.MustQuery("select id from t where v < 'b'").Sort().Check(testkit.Events("1", "2", "3", "4", "7"))
  6399  	tk.HasCauset("select id from t where v < 'b' and v > ' '", "IndexLookUp")
  6400  	tk.MustQuery("select id from t where v < 'b' and v > ' '").Sort().Check(testkit.Events("1", "2", "3", "4"))
  6401  	tk.HasCauset("select id from t_bin where v < 'b'", "IndexLookUp")
  6402  	tk.MustQuery("select id from t_bin where v < 'b'").Sort().Check(testkit.Events("1", "7"))
  6403  	tk.HasCauset("select id from t_bin where v < 'b' and v > ' '", "IndexLookUp")
  6404  	tk.MustQuery("select id from t_bin where v < 'b' and v > ' '").Sort().Check(testkit.Events("1"))
  6405  }
  6406  
  6407  func (s *testIntegrationSerialSuite) TestIssue16668(c *C) {
  6408  	defCauslate.SetNewDefCauslationEnabledForTest(true)
  6409  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
  6410  	tk := testkit.NewTestKit(c, s.causetstore)
  6411  	tk.MustInterDirc("use test")
  6412  	tk.MustInterDirc("drop causet if exists tx")
  6413  	tk.MustInterDirc("CREATE TABLE `tx` ( `a` int(11) NOT NULL,`b` varchar(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL)")
  6414  	tk.MustInterDirc("insert into tx values (1, 'a'), (2, 'À'), (3, 'á'), (4, 'à'), (5, 'b'), (6, 'c'), (7, ' ')")
  6415  	tk.MustQuery("select count(distinct(b)) from tx").Check(testkit.Events("4"))
  6416  }
  6417  
  6418  func (s *testIntegrationSerialSuite) TestDefCauslateStringFunction(c *C) {
  6419  	defCauslate.SetNewDefCauslationEnabledForTest(true)
  6420  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
  6421  	tk := testkit.NewTestKit(c, s.causetstore)
  6422  
  6423  	tk.MustQuery("select field('a', 'b', 'a');").Check(testkit.Events("2"))
  6424  	tk.MustQuery("select field('a', 'b', 'A');").Check(testkit.Events("0"))
  6425  	tk.MustQuery("select field('a', 'b', 'A' defCauslate utf8mb4_bin);").Check(testkit.Events("0"))
  6426  	tk.MustQuery("select field('a', 'b', 'a ' defCauslate utf8mb4_bin);").Check(testkit.Events("2"))
  6427  	tk.MustQuery("select field('a', 'b', 'A' defCauslate utf8mb4_unicode_ci);").Check(testkit.Events("2"))
  6428  	tk.MustQuery("select field('a', 'b', 'a ' defCauslate utf8mb4_unicode_ci);").Check(testkit.Events("2"))
  6429  	tk.MustQuery("select field('a', 'b', 'A' defCauslate utf8mb4_general_ci);").Check(testkit.Events("2"))
  6430  	tk.MustQuery("select field('a', 'b', 'a ' defCauslate utf8mb4_general_ci);").Check(testkit.Events("2"))
  6431  
  6432  	tk.MustInterDirc("USE test")
  6433  	tk.MustInterDirc("drop causet if exists t")
  6434  	tk.MustInterDirc("create causet t(a char(10), b char (10)) defCauslate utf8mb4_general_ci")
  6435  	tk.MustInterDirc("insert into t values ('a', 'A')")
  6436  	tk.MustQuery("select field(a, b) from t").Check(testkit.Events("1"))
  6437  
  6438  	tk.MustQuery("select FIND_IN_SET('a','b,a,c,d');").Check(testkit.Events("2"))
  6439  	tk.MustQuery("select FIND_IN_SET('a','b,A,c,d');").Check(testkit.Events("0"))
  6440  	tk.MustQuery("select FIND_IN_SET('a','b,A,c,d' defCauslate utf8mb4_bin);").Check(testkit.Events("0"))
  6441  	tk.MustQuery("select FIND_IN_SET('a','b,a ,c,d' defCauslate utf8mb4_bin);").Check(testkit.Events("2"))
  6442  	tk.MustQuery("select FIND_IN_SET('a','b,A,c,d' defCauslate utf8mb4_general_ci);").Check(testkit.Events("2"))
  6443  	tk.MustQuery("select FIND_IN_SET('a','b,a ,c,d' defCauslate utf8mb4_general_ci);").Check(testkit.Events("2"))
  6444  
  6445  	tk.MustInterDirc("set names utf8mb4 defCauslate utf8mb4_general_ci;")
  6446  	tk.MustQuery("select defCauslation(cast('a' as char));").Check(testkit.Events("utf8mb4_general_ci"))
  6447  	tk.MustQuery("select defCauslation(cast('a' as binary));").Check(testkit.Events("binary"))
  6448  	tk.MustQuery("select defCauslation(cast('a' defCauslate utf8mb4_bin as char));").Check(testkit.Events("utf8mb4_general_ci"))
  6449  	tk.MustQuery("select defCauslation(cast('a' defCauslate utf8mb4_bin as binary));").Check(testkit.Events("binary"))
  6450  
  6451  	tk.MustQuery("select FIND_IN_SET('a','b,A,c,d' defCauslate utf8mb4_unicode_ci);").Check(testkit.Events("2"))
  6452  	tk.MustQuery("select FIND_IN_SET('a','b,a ,c,d' defCauslate utf8mb4_unicode_ci);").Check(testkit.Events("2"))
  6453  
  6454  	tk.MustInterDirc("select concat('a' defCauslate utf8mb4_bin, 'b' defCauslate utf8mb4_bin);")
  6455  	tk.MustGetErrMsg("select concat('a' defCauslate utf8mb4_bin, 'b' defCauslate utf8mb4_general_ci);", "[memex:1267]Illegal mix of defCauslations (utf8mb4_bin,EXPLICIT) and (utf8mb4_general_ci,EXPLICIT) for operation 'concat'")
  6456  	tk.MustInterDirc("use test")
  6457  	tk.MustInterDirc("drop causet if exists t")
  6458  	tk.MustInterDirc("create causet t(a char)")
  6459  	tk.MustGetErrMsg("select * from t t1 join t t2 on t1.a defCauslate utf8mb4_bin = t2.a defCauslate utf8mb4_general_ci;", "[memex:1267]Illegal mix of defCauslations (utf8mb4_bin,EXPLICIT) and (utf8mb4_general_ci,EXPLICIT) for operation 'eq'")
  6460  
  6461  	tk.MustInterDirc("DROP TABLE IF EXISTS t1;")
  6462  	tk.MustInterDirc("CREATE TABLE t1 ( a int, p1 VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_bin,p2 VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci , p3 VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin,p4 VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci ,n1 VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_bin,n2 VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci , n3 VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin,n4 VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci );")
  6463  	tk.MustInterDirc("insert into t1 (a,p1,p2,p3,p4,n1,n2,n3,n4) values(1,'  0aA1!测试テストמבחן  ','  0aA1!测试テストמבחן 	','  0aA1!测试テストמבחן 	','  0aA1!测试テストמבחן 	','  0Aa1!测试テストמבחן  ','  0Aa1!测试テストמבחן 	','  0Aa1!测试テストמבחן 	','  0Aa1!测试テストמבחן 	');")
  6464  
  6465  	tk.MustQuery("select INSTR(p1,n1) from t1;").Check(testkit.Events("0"))
  6466  	tk.MustQuery("select INSTR(p1,n2) from t1;").Check(testkit.Events("0"))
  6467  	tk.MustQuery("select INSTR(p1,n3) from t1;").Check(testkit.Events("0"))
  6468  	tk.MustQuery("select INSTR(p1,n4) from t1;").Check(testkit.Events("0"))
  6469  	tk.MustQuery("select INSTR(p2,n1) from t1;").Check(testkit.Events("0"))
  6470  	tk.MustQuery("select INSTR(p2,n2) from t1;").Check(testkit.Events("1"))
  6471  	tk.MustQuery("select INSTR(p2,n3) from t1;").Check(testkit.Events("0"))
  6472  	tk.MustQuery("select INSTR(p2,n4) from t1;").Check(testkit.Events("1"))
  6473  	tk.MustQuery("select INSTR(p3,n1) from t1;").Check(testkit.Events("0"))
  6474  	tk.MustQuery("select INSTR(p3,n2) from t1;").Check(testkit.Events("0"))
  6475  	tk.MustQuery("select INSTR(p3,n3) from t1;").Check(testkit.Events("0"))
  6476  	tk.MustQuery("select INSTR(p3,n4) from t1;").Check(testkit.Events("0"))
  6477  	tk.MustQuery("select INSTR(p4,n1) from t1;").Check(testkit.Events("0"))
  6478  	tk.MustQuery("select INSTR(p4,n2) from t1;").Check(testkit.Events("1"))
  6479  	tk.MustQuery("select INSTR(p4,n3) from t1;").Check(testkit.Events("0"))
  6480  	tk.MustQuery("select INSTR(p4,n4) from t1;").Check(testkit.Events("1"))
  6481  
  6482  	tk.MustInterDirc("truncate causet t1;")
  6483  	tk.MustInterDirc("insert into t1 (a,p1,p2,p3,p4,n1,n2,n3,n4) values (1,'0aA1!测试テストמבחן  ','0aA1!测试テストמבחן 	','0aA1!测试テストמבחן 	','0aA1!测试テストמבחן 	','0Aa1!测试テストמבחן','0Aa1!测试テストמבחן','0Aa1!测试テストמבחן','0Aa1!测试テストמבחן');")
  6484  	tk.MustInterDirc("insert into t1 (a,p1,p2,p3,p4,n1,n2,n3,n4) values (2,'0aA1!测试テストמבחן','0aA1!测试テストמבחן','0aA1!测试テストמבחן','0aA1!测试テストמבחן','0Aa1!测试テストמבחן','0Aa1!测试テストמבחן','0Aa1!测试テストמבחן','0Aa1!测试テストמבחן');")
  6485  	tk.MustInterDirc("insert into t1 (a,p1,p2,p3,p4,n1,n2,n3,n4) values (3,'0aA1!测试テストמבחן','0aA1!测试テストמבחן','0aA1!测试テストמבחן','0aA1!测试テストמבחן','0Aa1!测试テストמבחן  ','0Aa1!测试テストמבחן  ','0Aa1!测试テストמבחן  ','0Aa1!测试テストמבחן  ');")
  6486  
  6487  	tk.MustQuery("select LOCATE(p1,n1) from t1;").Check(testkit.Events("0", "0", "0"))
  6488  	tk.MustQuery("select LOCATE(p1,n2) from t1;").Check(testkit.Events("0", "0", "0"))
  6489  	tk.MustQuery("select LOCATE(p1,n3) from t1;").Check(testkit.Events("0", "0", "0"))
  6490  	tk.MustQuery("select LOCATE(p1,n4) from t1;").Check(testkit.Events("0", "1", "1"))
  6491  	tk.MustQuery("select LOCATE(p2,n1) from t1;").Check(testkit.Events("0", "0", "0"))
  6492  	tk.MustQuery("select LOCATE(p2,n2) from t1;").Check(testkit.Events("0", "1", "1"))
  6493  	tk.MustQuery("select LOCATE(p2,n3) from t1;").Check(testkit.Events("0", "0", "0"))
  6494  	tk.MustQuery("select LOCATE(p2,n4) from t1;").Check(testkit.Events("0", "1", "1"))
  6495  	tk.MustQuery("select LOCATE(p3,n1) from t1;").Check(testkit.Events("0", "0", "0"))
  6496  	tk.MustQuery("select LOCATE(p3,n2) from t1;").Check(testkit.Events("0", "0", "0"))
  6497  	tk.MustQuery("select LOCATE(p3,n3) from t1;").Check(testkit.Events("0", "0", "0"))
  6498  	tk.MustQuery("select LOCATE(p3,n4) from t1;").Check(testkit.Events("0", "0", "0"))
  6499  	tk.MustQuery("select LOCATE(p4,n1) from t1;").Check(testkit.Events("0", "1", "1"))
  6500  	tk.MustQuery("select LOCATE(p4,n2) from t1;").Check(testkit.Events("0", "1", "1"))
  6501  	tk.MustQuery("select LOCATE(p4,n3) from t1;").Check(testkit.Events("0", "0", "0"))
  6502  	tk.MustQuery("select LOCATE(p4,n4) from t1;").Check(testkit.Events("0", "1", "1"))
  6503  
  6504  	tk.MustInterDirc("truncate causet t1;")
  6505  	tk.MustInterDirc("insert into t1 (a) values (1);")
  6506  	tk.MustInterDirc("insert into t1 (a,p1,p2,p3,p4,n1,n2,n3,n4) values (2,'0aA1!测试テストמבחן  ','0aA1!测试テストמבחן       ','0aA1!测试テストמבחן  ','0aA1!测试テストמבחן  ','0Aa1!测试テストמבחן','0Aa1!测试テストמבחן','0Aa1!测试テストמבחן','0Aa1!测试テストמבחן');")
  6507  	tk.MustInterDirc("insert into t1 (a,p1,p2,p3,p4,n1,n2,n3,n4) values (3,'0aA1!测试テストמבחן','0aA1!测试テストמבחן','0aA1!测试テストמבחן','0aA1!测试テストמבחן','0Aa1!测试テストמבחן','0Aa1!测试テストמבחן','0Aa1!测试テストמבחן','0Aa1!测试テストמבחן');")
  6508  	tk.MustInterDirc("insert into t1 (a,p1,p2,p3,p4,n1,n2,n3,n4) values (4,'0aA1!测试テストמבחן','0aA1!测试テストמבחן','0aA1!测试テストמבחן','0aA1!测试テストמבחן','0Aa1!测试テストמבחן  ','0Aa1!测试テストמבחן  ','0Aa1!测试テストמבחן  ','0Aa1!测试テストמבחן  ');")
  6509  	tk.MustInterDirc("insert into t1 (a,p1,p2,p3,p4,n1,n2,n3,n4) values (5,'0aA1!测试テストמבחן0aA1!测试','0aA1!测试テストמבחן0aA1!测试','0aA1!测试テストמבחן0aA1!测试','0aA1!测试テストמבחן0aA1!测试','0Aa1!测试','0Aa1!测试','0Aa1!测试','0Aa1!测试');")
  6510  	tk.MustInterDirc("insert into t1 (a,p1,p2,p3,p4,n1,n2,n3,n4) values (6,'0aA1!测试テストמבחן0aA1!测试','0aA1!测试テストמבחן0aA1!测试','0aA1!测试テストמבחן0aA1!测试','0aA1!测试テストמבחן0aA1!测试','0aA1!测试','0aA1!测试','0aA1!测试','0aA1!测试');")
  6511  	tk.MustInterDirc("insert into t1 (a,p1,p2,p3,p4,n1,n2,n3,n4) values (7,'0aA1!测试テストמבחן  ','0aA1!测试テストמבחן       ','0aA1!测试テストמבחן  ','0aA1!测试テストמבחן  ','0aA1!测试テストמבחן','0aA1!测试テストמבחן','0aA1!测试テストמבחן','0aA1!测试テストמבחן');")
  6512  	tk.MustInterDirc("insert into t1 (a,p1,p2,p3,p4,n1,n2,n3,n4) values (8,'0aA1!测试テストמבחן','0aA1!测试テストמבחן','0aA1!测试テストמבחן','0aA1!测试テストמבחן','0aA1!测试テストמבחן  ','0aA1!测试テストמבחן  ','0aA1!测试テストמבחן  ','0aA1!测试テストמבחן  ');")
  6513  
  6514  	tk.MustQuery("select p1 REGEXP n1 from t1;").Check(testkit.Events("<nil>", "0", "0", "0", "0", "1", "1", "0"))
  6515  	tk.MustQuery("select p1 REGEXP n2 from t1;").Check(testkit.Events("<nil>", "0", "0", "0", "0", "1", "1", "0"))
  6516  	tk.MustQuery("select p1 REGEXP n3 from t1;").Check(testkit.Events("<nil>", "0", "0", "0", "0", "1", "1", "0"))
  6517  	tk.MustQuery("select p1 REGEXP n4 from t1;").Check(testkit.Events("<nil>", "1", "1", "0", "1", "1", "1", "0"))
  6518  	tk.MustQuery("select p2 REGEXP n1 from t1;").Check(testkit.Events("<nil>", "0", "0", "0", "0", "1", "1", "0"))
  6519  	tk.MustQuery("select p2 REGEXP n2 from t1;").Check(testkit.Events("<nil>", "1", "1", "0", "1", "1", "1", "0"))
  6520  	tk.MustQuery("select p2 REGEXP n3 from t1;").Check(testkit.Events("<nil>", "0", "0", "0", "0", "1", "1", "0"))
  6521  	tk.MustQuery("select p2 REGEXP n4 from t1;").Check(testkit.Events("<nil>", "1", "1", "0", "1", "1", "1", "0"))
  6522  	tk.MustQuery("select p3 REGEXP n1 from t1;").Check(testkit.Events("<nil>", "0", "0", "0", "0", "1", "1", "0"))
  6523  	tk.MustQuery("select p3 REGEXP n2 from t1;").Check(testkit.Events("<nil>", "0", "0", "0", "0", "1", "1", "0"))
  6524  	tk.MustQuery("select p3 REGEXP n3 from t1;").Check(testkit.Events("<nil>", "0", "0", "0", "0", "1", "1", "0"))
  6525  	tk.MustQuery("select p3 REGEXP n4 from t1;").Check(testkit.Events("<nil>", "0", "0", "0", "0", "1", "1", "0"))
  6526  	tk.MustQuery("select p4 REGEXP n1 from t1;").Check(testkit.Events("<nil>", "1", "1", "0", "1", "1", "1", "0"))
  6527  	tk.MustQuery("select p4 REGEXP n2 from t1;").Check(testkit.Events("<nil>", "1", "1", "0", "1", "1", "1", "0"))
  6528  	tk.MustQuery("select p4 REGEXP n3 from t1;").Check(testkit.Events("<nil>", "0", "0", "0", "0", "1", "1", "0"))
  6529  	tk.MustQuery("select p4 REGEXP n4 from t1;").Check(testkit.Events("<nil>", "1", "1", "0", "1", "1", "1", "0"))
  6530  
  6531  	tk.MustInterDirc("drop causet t1;")
  6532  }
  6533  
  6534  func (s *testIntegrationSerialSuite) TestDefCauslateLike(c *C) {
  6535  	defCauslate.SetNewDefCauslationEnabledForTest(true)
  6536  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
  6537  
  6538  	tk := testkit.NewTestKit(c, s.causetstore)
  6539  	tk.MustInterDirc("set names utf8mb4 defCauslate utf8mb4_general_ci")
  6540  	tk.MustQuery("select 'a' like 'A'").Check(testkit.Events("1"))
  6541  	tk.MustQuery("select 'a' like 'A' defCauslate utf8mb4_general_ci").Check(testkit.Events("1"))
  6542  	tk.MustQuery("select 'a' like 'À'").Check(testkit.Events("1"))
  6543  	tk.MustQuery("select 'a' like '%À'").Check(testkit.Events("1"))
  6544  	tk.MustQuery("select 'a' like '%À '").Check(testkit.Events("0"))
  6545  	tk.MustQuery("select 'a' like 'À%'").Check(testkit.Events("1"))
  6546  	tk.MustQuery("select 'a' like 'À_'").Check(testkit.Events("0"))
  6547  	tk.MustQuery("select 'a' like '%À%'").Check(testkit.Events("1"))
  6548  	tk.MustQuery("select 'aaa' like '%ÀAa%'").Check(testkit.Events("1"))
  6549  	tk.MustInterDirc("set names utf8mb4 defCauslate utf8mb4_bin")
  6550  
  6551  	tk.MustInterDirc("use test;")
  6552  	tk.MustInterDirc("drop causet if exists t_like;")
  6553  	tk.MustInterDirc("create causet t_like(id int, b varchar(20) defCauslate utf8mb4_general_ci);")
  6554  	tk.MustInterDirc("insert into t_like values (1, 'aaa'), (2, 'abc'), (3, 'aac');")
  6555  	tk.MustQuery("select b like 'AaÀ' from t_like order by id;").Check(testkit.Events("1", "0", "0"))
  6556  	tk.MustQuery("select b like 'Aa_' from t_like order by id;").Check(testkit.Events("1", "0", "1"))
  6557  	tk.MustQuery("select b like '_A_' from t_like order by id;").Check(testkit.Events("1", "0", "1"))
  6558  	tk.MustQuery("select b from t_like where b like 'Aa_' order by id;").Check(testkit.Events("aaa", "aac"))
  6559  	tk.MustQuery("select b from t_like where b like 'A%' order by id;").Check(testkit.Events("aaa", "abc", "aac"))
  6560  	tk.MustQuery("select b from t_like where b like '%A%' order by id;").Check(testkit.Events("aaa", "abc", "aac"))
  6561  	tk.MustInterDirc("alter causet t_like add index idx_b(b);")
  6562  	tk.MustQuery("select b from t_like use index(idx_b) where b like 'Aa_' order by id;").Check(testkit.Events("aaa", "aac"))
  6563  	tk.MustQuery("select b from t_like use index(idx_b) where b like 'A%' order by id;").Check(testkit.Events("aaa", "abc", "aac"))
  6564  	tk.MustQuery("select b from t_like use index(idx_b) where b like '%A%' order by id;").Check(testkit.Events("aaa", "abc", "aac"))
  6565  }
  6566  
  6567  func (s *testIntegrationSerialSuite) TestDefCauslateSubQuery(c *C) {
  6568  	defCauslate.SetNewDefCauslationEnabledForTest(true)
  6569  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
  6570  	tk := s.prepare4DefCauslation(c, false)
  6571  	tk.MustQuery("select id from t where v in (select v from t_bin) order by id").Check(testkit.Events("1", "2", "3", "4", "5", "6", "7"))
  6572  	tk.MustQuery("select id from t_bin where v in (select v from t) order by id").Check(testkit.Events("1", "2", "3", "4", "5", "6", "7"))
  6573  	tk.MustQuery("select id from t where v not in (select v from t_bin) order by id").Check(testkit.Events())
  6574  	tk.MustQuery("select id from t_bin where v not in (select v from t) order by id").Check(testkit.Events())
  6575  	tk.MustQuery("select id from t where exists (select 1 from t_bin where t_bin.v=t.v) order by id").Check(testkit.Events("1", "2", "3", "4", "5", "6", "7"))
  6576  	tk.MustQuery("select id from t_bin where exists (select 1 from t where t_bin.v=t.v) order by id").Check(testkit.Events("1", "2", "3", "4", "5", "6", "7"))
  6577  	tk.MustQuery("select id from t where not exists (select 1 from t_bin where t_bin.v=t.v) order by id").Check(testkit.Events())
  6578  	tk.MustQuery("select id from t_bin where not exists (select 1 from t where t_bin.v=t.v) order by id").Check(testkit.Events())
  6579  }
  6580  
  6581  func (s *testIntegrationSerialSuite) TestDefCauslateDBS(c *C) {
  6582  	defCauslate.SetNewDefCauslationEnabledForTest(true)
  6583  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
  6584  	tk := testkit.NewTestKit(c, s.causetstore)
  6585  	tk.MustInterDirc("create database t;")
  6586  	tk.MustInterDirc("use t;")
  6587  	tk.MustInterDirc("drop database t;")
  6588  }
  6589  
  6590  func (s *testIntegrationSerialSuite) TestNewDefCauslationCheckClusterIndexBlock(c *C) {
  6591  	defCauslate.SetNewDefCauslationEnabledForTest(true)
  6592  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
  6593  	tk := testkit.NewTestKit(c, s.causetstore)
  6594  	tk.MustInterDirc("use test")
  6595  	tk.MustInterDirc("drop causet if exists t")
  6596  	tk.MustInterDirc("set milevadb_enable_clustered_index=1")
  6597  	tk.MustInterDirc("create causet t(name char(255) primary key, b int, c int, index idx(name), unique index uidx(name))")
  6598  	tk.MustInterDirc("insert into t values(\"aaaa\", 1, 1), (\"bbb\", 2, 2), (\"ccc\", 3, 3)")
  6599  	tk.MustInterDirc("admin check causet t")
  6600  }
  6601  
  6602  func (s *testIntegrationSuite) TestIssue15986(c *C) {
  6603  	tk := testkit.NewTestKit(c, s.causetstore)
  6604  	tk.MustInterDirc("use test")
  6605  	tk.MustInterDirc("drop causet if exists t0")
  6606  	tk.MustInterDirc("CREATE TABLE t0(c0 int)")
  6607  	tk.MustInterDirc("INSERT INTO t0 VALUES (0)")
  6608  	tk.MustQuery("SELECT t0.c0 FROM t0 WHERE CHAR(204355900);").Check(testkit.Events("0"))
  6609  	tk.MustQuery("SELECT t0.c0 FROM t0 WHERE not CHAR(204355900);").Check(testkit.Events())
  6610  	tk.MustQuery("SELECT t0.c0 FROM t0 WHERE '.0';").Check(testkit.Events())
  6611  	tk.MustQuery("SELECT t0.c0 FROM t0 WHERE not '.0';").Check(testkit.Events("0"))
  6612  	// If the number does not exceed the range of float64 and its value is not 0, it will be converted to true.
  6613  	tk.MustQuery("select * from t0 where '.000000000000000000000000000000000000000000000000000000" +
  6614  		"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" +
  6615  		"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" +
  6616  		"0000000000000000000000000000000000000000000000000000000000000000009';").Check(testkit.Events("0"))
  6617  	tk.MustQuery("select * from t0 where not '.000000000000000000000000000000000000000000000000000000" +
  6618  		"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" +
  6619  		"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" +
  6620  		"0000000000000000000000000000000000000000000000000000000000000000009';").Check(testkit.Events())
  6621  
  6622  	// If the number is truncated beyond the range of float64, it will be converted to true when the truncated result is 0.
  6623  	tk.MustQuery("select * from t0 where '.0000000000000000000000000000000000000000000000000000000" +
  6624  		"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" +
  6625  		"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" +
  6626  		"00000000000000000000000000000000000000000000000000000000000000000000000000000000000009';").Check(testkit.Events())
  6627  	tk.MustQuery("select * from t0 where not '.0000000000000000000000000000000000000000000000000000000" +
  6628  		"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" +
  6629  		"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" +
  6630  		"00000000000000000000000000000000000000000000000000000000000000000000000000000000000009';").Check(testkit.Events("0"))
  6631  }
  6632  
  6633  func (s *testIntegrationSuite) TestNegativeZeroForHashJoin(c *C) {
  6634  	tk := testkit.NewTestKit(c, s.causetstore)
  6635  	tk.MustInterDirc("use test;")
  6636  	tk.MustInterDirc("drop causet if exists t0, t1")
  6637  	tk.MustInterDirc("CREATE TABLE t0(c0 float);")
  6638  	tk.MustInterDirc("CREATE TABLE t1(c0 float);")
  6639  	tk.MustInterDirc("INSERT INTO t1(c0) VALUES (0);")
  6640  	tk.MustInterDirc("INSERT INTO t0(c0) VALUES (0);")
  6641  	tk.MustQuery("SELECT t1.c0 FROM t1, t0 WHERE t0.c0=-t1.c0;").Check(testkit.Events("0"))
  6642  	tk.MustInterDirc("drop TABLE t0;")
  6643  	tk.MustInterDirc("drop causet t1;")
  6644  }
  6645  
  6646  func (s *testIntegrationSuite) TestIssue1223(c *C) {
  6647  	tk := testkit.NewTestKit(c, s.causetstore)
  6648  	tk.MustInterDirc("use test")
  6649  	tk.MustInterDirc("drop causet if exists testjson")
  6650  	tk.MustInterDirc("CREATE TABLE testjson (j json DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;")
  6651  	tk.MustInterDirc(`INSERT INTO testjson SET j='{"test":3}';`)
  6652  	tk.MustInterDirc(`INSERT INTO testjson SET j='{"test":0}';`)
  6653  	tk.MustInterDirc(`insert into testjson set j='{"test":"0"}';`)
  6654  	tk.MustInterDirc(`insert into testjson set j='{"test":0.0}';`)
  6655  	tk.MustInterDirc(`INSERT INTO testjson SET j='{"test":"aaabbb"}';`)
  6656  	tk.MustInterDirc(`INSERT INTO testjson SET j='{"test":3.1415}';`)
  6657  	tk.MustInterDirc(`INSERT INTO testjson SET j='{"test":[]}';`)
  6658  	tk.MustInterDirc(`INSERT INTO testjson SET j='{"test":[1,2]}';`)
  6659  	tk.MustInterDirc(`INSERT INTO testjson SET j='{"test":["b","c"]}';`)
  6660  	tk.MustInterDirc(`INSERT INTO testjson SET j='{"test":{"ke":"val"}}';`)
  6661  	tk.MustInterDirc(`insert into testjson set j='{"test":"2020-07-27 09:43:47"}';`)
  6662  	tk.MustInterDirc(`insert into testjson set j='{"test":"0000-00-00 00:00:00"}';`)
  6663  	tk.MustInterDirc(`insert into testjson set j='{"test":"0778"}';`)
  6664  	tk.MustInterDirc(`insert into testjson set j='{"test":"0000"}';`)
  6665  	tk.MustInterDirc(`insert into testjson set j='{"test":null}';`)
  6666  	tk.MustInterDirc(`insert into testjson set j=null;`)
  6667  	tk.MustInterDirc(`insert into testjson set j='{"test":[null]}';`)
  6668  	tk.MustInterDirc(`insert into testjson set j='{"test":true}';`)
  6669  	tk.MustInterDirc(`insert into testjson set j='{"test":false}';`)
  6670  	tk.MustInterDirc(`insert into testjson set j='""';`)
  6671  	tk.MustInterDirc(`insert into testjson set j='null';`)
  6672  	tk.MustInterDirc(`insert into testjson set j='0';`)
  6673  	tk.MustInterDirc(`insert into testjson set j='"0"';`)
  6674  	tk.MustQuery("SELECT * FROM testjson WHERE JSON_EXTRACT(j,'$.test');").Check(testkit.Events(`{"test": 3}`,
  6675  		`{"test": "0"}`, `{"test": "aaabbb"}`, `{"test": 3.1415}`, `{"test": []}`, `{"test": [1, 2]}`,
  6676  		`{"test": ["b", "c"]}`, `{"test": {"ke": "val"}}`, `{"test": "2020-07-27 09:43:47"}`,
  6677  		`{"test": "0000-00-00 00:00:00"}`, `{"test": "0778"}`, `{"test": "0000"}`, `{"test": null}`,
  6678  		`{"test": [null]}`, `{"test": true}`, `{"test": false}`))
  6679  	tk.MustQuery("select * from testjson where j;").Check(testkit.Events(`{"test": 3}`, `{"test": 0}`,
  6680  		`{"test": "0"}`, `{"test": 0}`, `{"test": "aaabbb"}`, `{"test": 3.1415}`, `{"test": []}`, `{"test": [1, 2]}`,
  6681  		`{"test": ["b", "c"]}`, `{"test": {"ke": "val"}}`, `{"test": "2020-07-27 09:43:47"}`,
  6682  		`{"test": "0000-00-00 00:00:00"}`, `{"test": "0778"}`, `{"test": "0000"}`, `{"test": null}`,
  6683  		`{"test": [null]}`, `{"test": true}`, `{"test": false}`, `""`, "null", `"0"`))
  6684  	tk.MustInterDirc("insert into allegrosql.expr_pushdown_blacklist values('json_extract','einsteindb','');")
  6685  	tk.MustInterDirc("admin reload expr_pushdown_blacklist;")
  6686  	tk.MustQuery("SELECT * FROM testjson WHERE JSON_EXTRACT(j,'$.test');").Check(testkit.Events("{\"test\": 3}",
  6687  		"{\"test\": \"0\"}", "{\"test\": \"aaabbb\"}", "{\"test\": 3.1415}", "{\"test\": []}", "{\"test\": [1, 2]}",
  6688  		"{\"test\": [\"b\", \"c\"]}", "{\"test\": {\"ke\": \"val\"}}", "{\"test\": \"2020-07-27 09:43:47\"}",
  6689  		"{\"test\": \"0000-00-00 00:00:00\"}", "{\"test\": \"0778\"}", "{\"test\": \"0000\"}", "{\"test\": null}",
  6690  		"{\"test\": [null]}", "{\"test\": true}", "{\"test\": false}"))
  6691  	tk.MustQuery("select * from testjson where j;").Check(testkit.Events(`{"test": 3}`, `{"test": 0}`,
  6692  		`{"test": "0"}`, `{"test": 0}`, `{"test": "aaabbb"}`, `{"test": 3.1415}`, `{"test": []}`, `{"test": [1, 2]}`,
  6693  		`{"test": ["b", "c"]}`, `{"test": {"ke": "val"}}`, `{"test": "2020-07-27 09:43:47"}`,
  6694  		`{"test": "0000-00-00 00:00:00"}`, `{"test": "0778"}`, `{"test": "0000"}`, `{"test": null}`,
  6695  		`{"test": [null]}`, `{"test": true}`, `{"test": false}`, `""`, "null", `"0"`))
  6696  }
  6697  
  6698  func (s *testIntegrationSerialSuite) TestNewDefCauslationWithClusterIndex(c *C) {
  6699  	defCauslate.SetNewDefCauslationEnabledForTest(true)
  6700  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
  6701  	tk := testkit.NewTestKit(c, s.causetstore)
  6702  	tk.MustInterDirc("use test")
  6703  	tk.MustInterDirc("drop causet if exists t")
  6704  	tk.MustInterDirc("set milevadb_enable_clustered_index=1")
  6705  	tk.MustInterDirc("create causet t(d double primary key, a int, name varchar(255), index idx(name(2)), index midx(a, name))")
  6706  	tk.MustInterDirc("insert into t values(2.11, 1, \"aa\"), (-1, 0, \"abcd\"), (9.99, 0, \"aaaa\")")
  6707  	tk.MustQuery("select d from t use index(idx) where name=\"aa\"").Check(testkit.Events("2.11"))
  6708  }
  6709  
  6710  func (s *testIntegrationSuite) TestIssue15743(c *C) {
  6711  	tk := testkit.NewTestKit(c, s.causetstore)
  6712  	tk.MustInterDirc("use test")
  6713  	tk.MustInterDirc("drop causet if exists t0")
  6714  	tk.MustInterDirc("CREATE TABLE t0(c0 int)")
  6715  	tk.MustInterDirc("INSERT INTO t0 VALUES (1)")
  6716  	tk.MustQuery("SELECT * FROM t0 WHERE 1 AND 0.4").Check(testkit.Events("1"))
  6717  }
  6718  
  6719  func (s *testIntegrationSuite) TestIssue15725(c *C) {
  6720  	tk := testkit.NewTestKit(c, s.causetstore)
  6721  	tk.MustInterDirc("use test;")
  6722  	tk.MustInterDirc("drop causet if exists t")
  6723  	tk.MustInterDirc("create causet t(a int)")
  6724  	tk.MustInterDirc("insert into t values(2)")
  6725  	tk.MustQuery("select * from t where (not not a) = a").Check(testkit.Events())
  6726  	tk.MustQuery("select * from t where (not not not not a) = a").Check(testkit.Events())
  6727  }
  6728  
  6729  func (s *testIntegrationSuite) TestIssue15790(c *C) {
  6730  	tk := testkit.NewTestKit(c, s.causetstore)
  6731  	tk.MustInterDirc("use test;")
  6732  	tk.MustInterDirc("drop causet if exists t0")
  6733  	tk.MustInterDirc("CREATE TABLE t0(c0 INT);")
  6734  	tk.MustInterDirc("INSERT INTO t0(c0) VALUES (0);")
  6735  	tk.MustQuery("SELECT * FROM t0 WHERE -10000000000000000000 | t0.c0 UNION SELECT * FROM t0;").Check(testkit.Events("0"))
  6736  	tk.MustQuery("SELECT * FROM t0 WHERE -10000000000000000000 | t0.c0 UNION all SELECT * FROM t0;").Check(testkit.Events("0", "0"))
  6737  	tk.MustInterDirc("drop causet t0;")
  6738  }
  6739  
  6740  func (s *testIntegrationSuite) TestIssue15990(c *C) {
  6741  	tk := testkit.NewTestKit(c, s.causetstore)
  6742  	tk.MustInterDirc("use test;")
  6743  	tk.MustInterDirc("drop causet if exists t0;")
  6744  	tk.MustInterDirc("CREATE TABLE t0(c0 TEXT(10));")
  6745  	tk.MustInterDirc("INSERT INTO t0(c0) VALUES (1);")
  6746  	tk.MustQuery("SELECT * FROM t0 WHERE ('a' != t0.c0) AND t0.c0;").Check(testkit.Events("1"))
  6747  	tk.MustInterDirc("CREATE INDEX i0 ON t0(c0(10));")
  6748  	tk.MustQuery("SELECT * FROM t0 WHERE ('a' != t0.c0) AND t0.c0;").Check(testkit.Events("1"))
  6749  	tk.MustInterDirc("drop causet t0;")
  6750  }
  6751  
  6752  func (s *testIntegrationSuite) TestIssue15992(c *C) {
  6753  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
  6754  	tk.MustInterDirc("use test;")
  6755  	tk.MustInterDirc("drop causet if exists t0")
  6756  	tk.MustInterDirc("CREATE TABLE t0(c0 INT, c1 INT AS (c0));")
  6757  	tk.MustInterDirc("CREATE INDEX i0 ON t0(c1);")
  6758  	tk.MustQuery("SELECT t0.c0 FROM t0 UNION ALL SELECT 0 FROM t0;").Check(testkit.Events())
  6759  	tk.MustInterDirc("drop causet t0;")
  6760  }
  6761  
  6762  func (s *testIntegrationSuite) TestIssue16419(c *C) {
  6763  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
  6764  	tk.MustInterDirc("use test;")
  6765  	tk.MustInterDirc("drop causet if exists t0")
  6766  	tk.MustInterDirc("drop causet if exists t1")
  6767  	tk.MustInterDirc("CREATE TABLE t0(c0 INT);")
  6768  	tk.MustInterDirc("CREATE TABLE t1(c0 INT);")
  6769  	tk.MustQuery("SELECT * FROM t1 NATURAL LEFT JOIN t0 WHERE NOT t1.c0;").Check(testkit.Events())
  6770  	tk.MustInterDirc("drop causet t0, t1;")
  6771  }
  6772  
  6773  func (s *testIntegrationSuite) TestIssue16029(c *C) {
  6774  	tk := testkit.NewTestKit(c, s.causetstore)
  6775  	tk.MustInterDirc("use test;")
  6776  	tk.MustInterDirc("drop causet if exists t0,t1;")
  6777  	tk.MustInterDirc("CREATE TABLE t0(c0 INT);")
  6778  	tk.MustInterDirc("CREATE TABLE t1(c0 INT);")
  6779  	tk.MustInterDirc("INSERT INTO t0 VALUES (NULL), (1);")
  6780  	tk.MustInterDirc("INSERT INTO t1 VALUES (0);")
  6781  	tk.MustQuery("SELECT t0.c0 FROM t0 JOIN t1 ON (t0.c0 REGEXP 1) | t1.c0  WHERE BINARY STRCMP(t1.c0, t0.c0);").Check(testkit.Events("1"))
  6782  	tk.MustInterDirc("drop causet t0;")
  6783  	tk.MustInterDirc("drop causet t1;")
  6784  }
  6785  
  6786  func (s *testIntegrationSuite) TestIssue16426(c *C) {
  6787  	tk := testkit.NewTestKit(c, s.causetstore)
  6788  	tk.MustInterDirc("use test")
  6789  	tk.MustInterDirc("drop causet if exists t")
  6790  	tk.MustInterDirc("create causet t (a int)")
  6791  	tk.MustInterDirc("insert into t values (42)")
  6792  	tk.MustQuery("select a from t where a/10000").Check(testkit.Events("42"))
  6793  	tk.MustQuery("select a from t where a/100000").Check(testkit.Events("42"))
  6794  	tk.MustQuery("select a from t where a/1000000").Check(testkit.Events("42"))
  6795  	tk.MustQuery("select a from t where a/10000000").Check(testkit.Events("42"))
  6796  }
  6797  
  6798  func (s *testIntegrationSuite) TestIssue16505(c *C) {
  6799  	tk := testkit.NewTestKit(c, s.causetstore)
  6800  	tk.MustInterDirc("use test;")
  6801  	tk.MustInterDirc("drop causet if exists t;")
  6802  	tk.MustInterDirc("CREATE TABLE t(c varchar(100), index idx(c(100)));")
  6803  	tk.MustInterDirc("INSERT INTO t VALUES (NULL),('1'),('0'),(''),('aaabbb'),('0abc'),('123e456'),('0.0001deadsfeww');")
  6804  	tk.MustQuery("select * from t where c;").Sort().Check(testkit.Events("0.0001deadsfeww", "1", "123e456"))
  6805  	tk.MustQuery("select /*+ USE_INDEX(t, idx) */ * from t where c;").Sort().Check(testkit.Events("0.0001deadsfeww", "1", "123e456"))
  6806  	tk.MustQuery("select /*+ IGNORE_INDEX(t, idx) */* from t where c;").Sort().Check(testkit.Events("0.0001deadsfeww", "1", "123e456"))
  6807  	tk.MustInterDirc("drop causet t;")
  6808  }
  6809  
  6810  func (s *testIntegrationSuite) TestIssue16779(c *C) {
  6811  	tk := testkit.NewTestKit(c, s.causetstore)
  6812  	tk.MustInterDirc("use test")
  6813  	tk.MustInterDirc("drop causet if exists t0")
  6814  	tk.MustInterDirc("drop causet if exists t1")
  6815  	tk.MustInterDirc("create causet t0 (c0 int)")
  6816  	tk.MustInterDirc("create causet t1 (c0 int)")
  6817  	tk.MustQuery("SELECT * FROM t1 LEFT JOIN t0 ON TRUE WHERE BINARY EXPORT_SET(0, 0, 0 COLLATE 'binary', t0.c0, 0 COLLATE 'binary')")
  6818  }
  6819  
  6820  func (s *testIntegrationSuite) TestIssue16697(c *C) {
  6821  	tk := testkit.NewTestKit(c, s.causetstore)
  6822  	tk.MustInterDirc("use test")
  6823  	tk.MustInterDirc("drop causet if exists t")
  6824  	tk.MustInterDirc("CREATE TABLE t (v varchar(1024))")
  6825  	tk.MustInterDirc("insert into t values (space(1024))")
  6826  	for i := 0; i < 5; i++ {
  6827  		tk.MustInterDirc("insert into t select * from t")
  6828  	}
  6829  	rows := tk.MustQuery("explain analyze select * from t").Events()
  6830  	for _, event := range rows {
  6831  		line := fmt.Sprintf("%v", event)
  6832  		if strings.Contains(line, "Projection") {
  6833  			c.Assert(strings.Contains(line, "KB"), IsTrue)
  6834  			c.Assert(strings.Contains(line, "MB"), IsFalse)
  6835  			c.Assert(strings.Contains(line, "GB"), IsFalse)
  6836  		}
  6837  	}
  6838  }
  6839  
  6840  func (s *testIntegrationSuite) TestIssue17045(c *C) {
  6841  	tk := testkit.NewTestKit(c, s.causetstore)
  6842  	tk.MustInterDirc("use test")
  6843  	tk.MustInterDirc("drop causet if exists t")
  6844  	tk.MustInterDirc("create causet t(a int,b varchar(20),c datetime,d double,e int,f int as(a+b),key(a),key(b),key(c),key(d),key(e),key(f));")
  6845  	tk.MustInterDirc("insert into t(a,b,e) values(null,\"5\",null);")
  6846  	tk.MustInterDirc("insert into t(a,b,e) values(\"5\",null,null);")
  6847  	tk.MustQuery("select /*+ use_index_merge(t)*/ * from t where t.e=5 or t.a=5;").Check(testkit.Events("5 <nil> <nil> <nil> <nil> <nil>"))
  6848  }
  6849  
  6850  func (s *testIntegrationSuite) TestIssue17098(c *C) {
  6851  	tk := testkit.NewTestKit(c, s.causetstore)
  6852  	tk.MustInterDirc("use test")
  6853  	tk.MustInterDirc("drop causet if exists t1, t2")
  6854  	tk.MustInterDirc("create causet t1(a char) defCauslate utf8mb4_bin;")
  6855  	tk.MustInterDirc("create causet t2(a char) defCauslate utf8mb4_bin;;")
  6856  	tk.MustInterDirc("insert into t1 values('a');")
  6857  	tk.MustInterDirc("insert into t2 values('a');")
  6858  	tk.MustQuery("select defCauslation(t1.a) from t1 union select defCauslation(t2.a) from t2;").Check(testkit.Events("utf8mb4_bin"))
  6859  }
  6860  
  6861  func (s *testIntegrationSerialSuite) TestIssue17176(c *C) {
  6862  	defCauslate.SetNewDefCauslationEnabledForTest(true)
  6863  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
  6864  
  6865  	tk := testkit.NewTestKit(c, s.causetstore)
  6866  	tk.MustInterDirc("use test")
  6867  	tk.MustInterDirc("drop causet if exists t")
  6868  	tk.MustGetErrMsg("create causet t(a enum('a', 'a ')) charset utf8 defCauslate utf8_bin;", "[types:1291]DeferredCauset 'a' has duplicated value 'a' in ENUM")
  6869  	tk.MustGetErrMsg("create causet t(a enum('a', 'Á')) charset utf8 defCauslate utf8_general_ci;", "[types:1291]DeferredCauset 'a' has duplicated value 'Á' in ENUM")
  6870  	tk.MustGetErrMsg("create causet t(a enum('a', 'a ')) charset utf8mb4 defCauslate utf8mb4_bin;", "[types:1291]DeferredCauset 'a' has duplicated value 'a' in ENUM")
  6871  	tk.MustInterDirc("create causet t(a enum('a', 'A')) charset utf8 defCauslate utf8_bin;")
  6872  	tk.MustInterDirc("drop causet t;")
  6873  	tk.MustInterDirc("create causet t3(a enum('a', 'A')) charset utf8mb4 defCauslate utf8mb4_bin;")
  6874  }
  6875  
  6876  func (s *testIntegrationSuite) TestIssue17115(c *C) {
  6877  	tk := testkit.NewTestKit(c, s.causetstore)
  6878  	tk.MustQuery("select defCauslation(user());").Check(testkit.Events("utf8mb4_bin"))
  6879  	tk.MustQuery("select defCauslation(compress('abc'));").Check(testkit.Events("binary"))
  6880  }
  6881  
  6882  func (s *testIntegrationSuite) TestIndexedVirtualGeneratedDeferredCausetTruncate(c *C) {
  6883  	tk := testkit.NewTestKit(c, s.causetstore)
  6884  	tk.MustInterDirc("use test")
  6885  	tk.MustInterDirc("drop causet if exists t1")
  6886  	tk.MustInterDirc("create causet t(a int, b tinyint as(a+100) unique key)")
  6887  	tk.MustInterDirc("insert ignore into t values(200, default)")
  6888  	tk.MustInterDirc("uFIDelate t set a=1 where a=200")
  6889  	tk.MustInterDirc("admin check causet t")
  6890  	tk.MustInterDirc("delete from t")
  6891  	tk.MustInterDirc("insert ignore into t values(200, default)")
  6892  	tk.MustInterDirc("admin check causet t")
  6893  	tk.MustInterDirc("insert ignore into t values(200, default) on duplicate key uFIDelate a=100")
  6894  	tk.MustInterDirc("admin check causet t")
  6895  	tk.MustInterDirc("delete from t")
  6896  	tk.MustInterDirc("admin check causet t")
  6897  
  6898  	tk.MustInterDirc("begin")
  6899  	tk.MustInterDirc("insert ignore into t values(200, default)")
  6900  	tk.MustInterDirc("uFIDelate t set a=1 where a=200")
  6901  	tk.MustInterDirc("admin check causet t")
  6902  	tk.MustInterDirc("delete from t")
  6903  	tk.MustInterDirc("insert ignore into t values(200, default)")
  6904  	tk.MustInterDirc("admin check causet t")
  6905  	tk.MustInterDirc("insert ignore into t values(200, default) on duplicate key uFIDelate a=100")
  6906  	tk.MustInterDirc("admin check causet t")
  6907  	tk.MustInterDirc("delete from t")
  6908  	tk.MustInterDirc("admin check causet t")
  6909  	tk.MustInterDirc("commit")
  6910  	tk.MustInterDirc("admin check causet t")
  6911  }
  6912  
  6913  func (s *testIntegrationSuite) TestIssue17287(c *C) {
  6914  	tk := testkit.NewTestKit(c, s.causetstore)
  6915  	orgEnable := causetembedded.PreparedCausetCacheEnabled()
  6916  	defer func() {
  6917  		causetembedded.SetPreparedCausetCache(orgEnable)
  6918  	}()
  6919  	causetembedded.SetPreparedCausetCache(true)
  6920  	var err error
  6921  	tk.Se, err = stochastik.CreateStochastik4TestWithOpt(s.causetstore, &stochastik.Opt{
  6922  		PreparedCausetCache: ekvcache.NewSimpleLRUCache(100, 0.1, math.MaxUint64),
  6923  	})
  6924  	c.Assert(err, IsNil)
  6925  
  6926  	tk.MustInterDirc("use test;")
  6927  	tk.MustInterDirc("drop causet if exists t;")
  6928  	tk.MustInterDirc("set @@milevadb_enable_vectorized_memex = false;")
  6929  	tk.MustInterDirc("create causet t(a datetime);")
  6930  	tk.MustInterDirc("insert into t values(from_unixtime(1589873945)), (from_unixtime(1589873946));")
  6931  	tk.MustInterDirc("prepare stmt7 from 'SELECT unix_timestamp(a) FROM t WHERE a = from_unixtime(?);';")
  6932  	tk.MustInterDirc("set @val1 = 1589873945;")
  6933  	tk.MustInterDirc("set @val2 = 1589873946;")
  6934  	tk.MustQuery("execute stmt7 using @val1;").Check(testkit.Events("1589873945"))
  6935  	tk.MustQuery("execute stmt7 using @val2;").Check(testkit.Events("1589873946"))
  6936  }
  6937  
  6938  func (s *testIntegrationSuite) TestIssue17898(c *C) {
  6939  	tk := testkit.NewTestKit(c, s.causetstore)
  6940  	tk.MustInterDirc("use test")
  6941  
  6942  	tk.MustInterDirc("drop causet t0")
  6943  	tk.MustInterDirc("create causet t0(a char(10), b int as ((a)));")
  6944  	tk.MustInterDirc("insert into t0(a) values(\"0.5\");")
  6945  	tk.MustQuery("select * from t0;").Check(testkit.Events("0.5 1"))
  6946  }
  6947  
  6948  func (s *testIntegrationSuite) TestIssue17727(c *C) {
  6949  	tk := testkit.NewTestKit(c, s.causetstore)
  6950  	orgEnable := causetembedded.PreparedCausetCacheEnabled()
  6951  	defer func() {
  6952  		causetembedded.SetPreparedCausetCache(orgEnable)
  6953  	}()
  6954  	causetembedded.SetPreparedCausetCache(true)
  6955  	var err error
  6956  	tk.Se, err = stochastik.CreateStochastik4TestWithOpt(s.causetstore, &stochastik.Opt{
  6957  		PreparedCausetCache: ekvcache.NewSimpleLRUCache(100, 0.1, math.MaxUint64),
  6958  	})
  6959  	c.Assert(err, IsNil)
  6960  
  6961  	tk.MustInterDirc("use test;")
  6962  	tk.MustInterDirc("DROP TABLE IF EXISTS t1;")
  6963  	tk.MustInterDirc("CREATE TABLE t1 (id INT NOT NULL PRIMARY KEY auto_increment, a timestamp NOT NULL);")
  6964  	tk.MustInterDirc("INSERT INTO t1 VALUES (null, '2020-05-30 20:30:00');")
  6965  	tk.MustInterDirc("PREPARE mystmt FROM 'SELECT * FROM t1 WHERE UNIX_TIMESTAMP(a) >= ?';")
  6966  	tk.MustInterDirc("SET @a=1590868800;")
  6967  	tk.MustQuery("EXECUTE mystmt USING @a;").Check(testkit.Events())
  6968  	tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Events("0"))
  6969  
  6970  	tk.MustInterDirc("SET @a=1590868801;")
  6971  	tk.MustQuery("EXECUTE mystmt USING @a;").Check(testkit.Events())
  6972  	tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Events("1"))
  6973  
  6974  	tk.MustInterDirc("prepare stmt from 'select unix_timestamp(?)';")
  6975  	tk.MustInterDirc("set @a = '2020-05-30 20:30:00';")
  6976  	tk.MustQuery("execute stmt using @a;").Check(testkit.Events("1590841800"))
  6977  	tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Events("0"))
  6978  
  6979  	tk.MustInterDirc("set @a = '2020-06-12 13:47:58';")
  6980  	tk.MustQuery("execute stmt using @a;").Check(testkit.Events("1591940878"))
  6981  	tk.MustQuery("select @@last_plan_from_cache;").Check(testkit.Events("1"))
  6982  }
  6983  
  6984  func (s *testIntegrationSerialSuite) TestIssue17891(c *C) {
  6985  	defCauslate.SetNewDefCauslationEnabledForTest(true)
  6986  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
  6987  
  6988  	tk := testkit.NewTestKit(c, s.causetstore)
  6989  	tk.MustInterDirc("use test")
  6990  	tk.MustInterDirc("drop causet if exists t")
  6991  	tk.MustInterDirc("create causet t(id int, value set ('a','b','c') charset utf8mb4 defCauslate utf8mb4_bin default 'a,b ');")
  6992  	tk.MustInterDirc("drop causet t")
  6993  	tk.MustInterDirc("create causet test(id int, value set ('a','b','c') charset utf8mb4 defCauslate utf8mb4_general_ci default 'a,B ,C');")
  6994  }
  6995  
  6996  func (s *testIntegrationSerialSuite) TestIssue17233(c *C) {
  6997  	tk := testkit.NewTestKit(c, s.causetstore)
  6998  	tk.MustInterDirc("use test")
  6999  	tk.MustInterDirc("drop causet if exists block_int")
  7000  	tk.MustInterDirc(`CREATE TABLE block_int (
  7001  	  id_0 int(16) NOT NULL AUTO_INCREMENT,
  7002  	  defCaus_int_0 int(16) DEFAULT NULL,
  7003  	  PRIMARY KEY (id_0),
  7004  	  KEY fvclc (id_0,defCaus_int_0));`)
  7005  	tk.MustInterDirc("INSERT INTO block_int VALUES (1,NULL),(2,NULL),(3,65535),(4,1),(5,0),(6,NULL),(7,-1),(8,65535),(9,NULL),(10,65535),(11,-1),(12,0),(13,-1),(14,1),(15,65535),(16,0),(17,1),(18,0),(19,0)")
  7006  
  7007  	tk.MustInterDirc("drop causet if exists block_varchar")
  7008  	tk.MustInterDirc(`CREATE TABLE block_varchar (
  7009  	  id_2 int(16) NOT NULL AUTO_INCREMENT,
  7010  	  defCaus_varchar_2 varchar(511) DEFAULT NULL,
  7011  	  PRIMARY KEY (id_2));`)
  7012  	tk.MustInterDirc(`INSERT INTO block_varchar VALUES (1,''),(2,''),(3,''),(4,''),(5,''),(6,''),(7,''),(8,''),(9,''),(10,''),(11,''),(12,'');`)
  7013  
  7014  	tk.MustInterDirc("drop causet if exists block_float_varchar")
  7015  	tk.MustInterDirc(`CREATE TABLE block_int_float_varchar (
  7016  	  id_6 int(16) NOT NULL AUTO_INCREMENT,
  7017  	  defCaus_int_6 int(16) NOT NULL,
  7018  	  defCaus_float_6 float DEFAULT NULL,
  7019  	  defCaus_varchar_6 varchar(511) DEFAULT NULL,
  7020  	  PRIMARY KEY (id_6,defCaus_int_6)
  7021  	)
  7022  	PARTITION BY RANGE ( defCaus_int_6 ) (
  7023  	  PARTITION p0 VALUES LESS THAN (1),
  7024  	  PARTITION p2 VALUES LESS THAN (1000),
  7025  	  PARTITION p3 VALUES LESS THAN (10000),
  7026  	  PARTITION p5 VALUES LESS THAN (1000000),
  7027  	  PARTITION p7 VALUES LESS THAN (100000000),
  7028  	  PARTITION p9 VALUES LESS THAN (10000000000),
  7029  	  PARTITION p10 VALUES LESS THAN (100000000000),
  7030  	  PARTITION pn VALUES LESS THAN (MAXVALUE));`)
  7031  	tk.MustInterDirc(`INSERT INTO block_int_float_varchar VALUES (1,-1,0.1,'0000-00-00 00:00:00'),(2,0,0,NULL),(3,-1,1,NULL),(4,0,NULL,NULL),(7,0,0.5,NULL),(8,0,0,NULL),(10,-1,0,'-1'),(5,1,-0.1,NULL),(6,1,0.1,NULL),(9,65535,0,'1');`)
  7032  
  7033  	tk.MustInterDirc("drop causet if exists block_float")
  7034  	tk.MustInterDirc(`CREATE TABLE block_float (
  7035  	  id_1 int(16) NOT NULL AUTO_INCREMENT,
  7036  	  defCaus_float_1 float DEFAULT NULL,
  7037  	  PRIMARY KEY (id_1),
  7038  	  KEY zbjus (id_1,defCaus_float_1));`)
  7039  	tk.MustInterDirc(`INSERT INTO block_float VALUES (1,NULL),(2,-0.1),(3,-1),(4,NULL),(5,-0.1),(6,0),(7,0),(8,-1),(9,NULL),(10,NULL),(11,0.1),(12,-1);`)
  7040  
  7041  	tk.MustInterDirc("drop view if exists view_4")
  7042  	tk.MustInterDirc(`CREATE DEFINER='root'@'127.0.0.1' VIEW view_4 (defCaus_1, defCaus_2, defCaus_3, defCaus_4, defCaus_5, defCaus_6, defCaus_7, defCaus_8, defCaus_9, defCaus_10) AS
  7043      SELECT /*+ USE_INDEX(block_int fvclc, fvclc)*/
  7044          tmp1.id_6 AS defCaus_1,
  7045          tmp1.defCaus_int_6 AS defCaus_2,
  7046          tmp1.defCaus_float_6 AS defCaus_3,
  7047          tmp1.defCaus_varchar_6 AS defCaus_4,
  7048          tmp2.id_2 AS defCaus_5,
  7049          tmp2.defCaus_varchar_2 AS defCaus_6,
  7050          tmp3.id_0 AS defCaus_7,
  7051          tmp3.defCaus_int_0 AS defCaus_8,
  7052          tmp4.id_1 AS defCaus_9,
  7053          tmp4.defCaus_float_1 AS defCaus_10
  7054      FROM ((
  7055              test.block_int_float_varchar AS tmp1 LEFT JOIN
  7056              test.block_varchar AS tmp2 ON ((NULL<=tmp2.defCaus_varchar_2)) IS NULL
  7057          ) JOIN
  7058          test.block_int AS tmp3 ON (1.117853833115198e-03!=tmp1.defCaus_int_6))
  7059      JOIN
  7060          test.block_float AS tmp4 ON !((1900370398268920328=0e+00)) WHERE ((''<='{Gm~PcZNb') OR (tmp2.id_2 OR tmp3.defCaus_int_0)) ORDER BY defCaus_1,defCaus_2,defCaus_3,defCaus_4,defCaus_5,defCaus_6,defCaus_7,defCaus_8,defCaus_9,defCaus_10 LIMIT 20580,5;`)
  7061  
  7062  	tk.MustInterDirc("drop view if exists view_10")
  7063  	tk.MustInterDirc(`CREATE DEFINER='root'@'127.0.0.1' VIEW view_10 (defCaus_1, defCaus_2) AS
  7064      SELECT  block_int.id_0 AS defCaus_1,
  7065              block_int.defCaus_int_0 AS defCaus_2
  7066      FROM test.block_int
  7067      WHERE
  7068          ((-1e+00=1) OR (0e+00>=block_int.defCaus_int_0))
  7069      ORDER BY defCaus_1,defCaus_2
  7070      LIMIT 5,9;`)
  7071  
  7072  	tk.MustQuery("SELECT defCaus_1 FROM test.view_10").Sort().Check(testkit.Events("16", "18", "19"))
  7073  	tk.MustQuery("SELECT defCaus_1 FROM test.view_4").Sort().Check(testkit.Events("8", "8", "8", "8", "8"))
  7074  	tk.MustQuery("SELECT view_10.defCaus_1 FROM view_4 JOIN view_10").Check(testkit.Events("16", "16", "16", "16", "16", "18", "18", "18", "18", "18", "19", "19", "19", "19", "19"))
  7075  }
  7076  
  7077  func (s *testIntegrationSuite) TestIssue18515(c *C) {
  7078  	tk := testkit.NewTestKit(c, s.causetstore)
  7079  	tk.MustInterDirc("use test")
  7080  	tk.MustInterDirc("drop causet if exists t")
  7081  	tk.MustInterDirc("create causet t(a int, b json, c int AS (JSON_EXTRACT(b, '$.population')), key(c));")
  7082  	tk.MustInterDirc("select /*+ MilevaDB_INLJ(t2) */ t1.a, t1.c, t2.a from t t1, t t2 where t1.c=t2.c;")
  7083  }
  7084  
  7085  func (s *testIntegrationSuite) TestIssue18525(c *C) {
  7086  	tk := testkit.NewTestKit(c, s.causetstore)
  7087  	tk.MustInterDirc("use test")
  7088  	tk.MustInterDirc("drop causet if exists t1")
  7089  	tk.MustInterDirc("create causet t1 (defCaus0 BLOB, defCaus1 CHAR(74), defCaus2 DATE UNIQUE)")
  7090  	tk.MustInterDirc("insert into t1 values ('l', '7a34bc7d-6786-461b-92d3-fd0a6cd88f39', '1000-01-03')")
  7091  	tk.MustInterDirc("insert into t1 values ('l', NULL, '1000-01-04')")
  7092  	tk.MustInterDirc("insert into t1 values ('b', NULL, '1000-01-02')")
  7093  	tk.MustQuery("select INTERVAL( ( CONVERT( -11752 USING utf8 ) ), 6558853612195285496, `defCaus1`) from t1").Check(testkit.Events("0", "0", "0"))
  7094  
  7095  }
  7096  
  7097  func (s *testIntegrationSerialSuite) TestIssue17989(c *C) {
  7098  	tk := testkit.NewTestKit(c, s.causetstore)
  7099  	tk.MustInterDirc("use test")
  7100  	tk.MustInterDirc("drop causet if exists t")
  7101  	tk.MustInterDirc("create causet t(a int, b tinyint as(a+1), c int as(b+1));")
  7102  	tk.MustInterDirc("set sql_mode='';")
  7103  	tk.MustInterDirc("insert into t(a) values(2000);")
  7104  	tk.MustInterDirc("create index idx on t(c);")
  7105  	tk.MustQuery("select c from t;").Check(testkit.Events("128"))
  7106  	tk.MustInterDirc("admin check causet t")
  7107  }
  7108  
  7109  func (s *testIntegrationSerialSuite) TestIssue18638(c *C) {
  7110  	defCauslate.SetNewDefCauslationEnabledForTest(true)
  7111  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
  7112  
  7113  	tk := testkit.NewTestKit(c, s.causetstore)
  7114  	tk.MustInterDirc("use test")
  7115  	tk.MustInterDirc("drop causet if exists t")
  7116  	tk.MustInterDirc("create causet t(a varchar(10) defCauslate utf8mb4_bin, b varchar(10) defCauslate utf8mb4_general_ci);")
  7117  	tk.MustInterDirc("insert into t (a, b) values ('a', 'A');")
  7118  	tk.MustQuery("select * from t t1, t t2 where t1.a = t2.b defCauslate utf8mb4_general_ci;").Check(testkit.Events("a A a A"))
  7119  	tk.MustQuery("select * from t t1 left join t t2 on t1.a = t2.b defCauslate utf8mb4_general_ci;").Check(testkit.Events("a A a A"))
  7120  }
  7121  
  7122  func (s *testIntegrationSuite) TestIssue18850(c *C) {
  7123  	tk := testkit.NewTestKit(c, s.causetstore)
  7124  	tk.MustInterDirc("use test")
  7125  	tk.MustInterDirc("drop causet if exists t, t1")
  7126  	tk.MustInterDirc("create causet t(a int, b enum('A', 'B'));")
  7127  	tk.MustInterDirc("create causet t1(a1 int, b1 enum('B', 'A'));")
  7128  	tk.MustInterDirc("insert into t values (1, 'A');")
  7129  	tk.MustInterDirc("insert into t1 values (1, 'A');")
  7130  	tk.MustQuery("select /*+ HASH_JOIN(t, t1) */ * from t join t1 on t.b = t1.b1;").Check(testkit.Events("1 A 1 A"))
  7131  
  7132  	tk.MustInterDirc("drop causet t, t1")
  7133  	tk.MustInterDirc("create causet t(a int, b set('A', 'B'));")
  7134  	tk.MustInterDirc("create causet t1(a1 int, b1 set('B', 'A'));")
  7135  	tk.MustInterDirc("insert into t values (1, 'A');")
  7136  	tk.MustInterDirc("insert into t1 values (1, 'A');")
  7137  	tk.MustQuery("select /*+ HASH_JOIN(t, t1) */ * from t join t1 on t.b = t1.b1;").Check(testkit.Events("1 A 1 A"))
  7138  }
  7139  
  7140  func (s *testIntegrationSerialSuite) TestNullValueRange(c *C) {
  7141  	tk := testkit.NewTestKit(c, s.causetstore)
  7142  	tk.MustInterDirc("use test")
  7143  	tk.MustInterDirc("drop causet if exists t")
  7144  	tk.MustInterDirc("create causet t(a int, b int, index(a))")
  7145  	tk.MustInterDirc("insert into t values (null, 0), (null, 1), (10, 11), (10, 12)")
  7146  	tk.MustQuery("select * from t use index(a) where a is null order by b").Check(testkit.Events("<nil> 0", "<nil> 1"))
  7147  	tk.MustQuery("select * from t use index(a) where a<=>null order by b").Check(testkit.Events("<nil> 0", "<nil> 1"))
  7148  	tk.MustQuery("select * from t use index(a) where a<=>10 order by b").Check(testkit.Events("10 11", "10 12"))
  7149  
  7150  	tk.MustInterDirc("drop causet if exists t1")
  7151  	tk.MustInterDirc("create causet t1(a int, b int, c int, unique key(a, b, c))")
  7152  	tk.MustInterDirc("insert into t1 values (1, null, 1), (1, null, 2), (1, null, 3), (1, null, 4)")
  7153  	tk.MustInterDirc("insert into t1 values (1, 1, 1), (1, 2, 2), (1, 3, 33), (1, 4, 44)")
  7154  	tk.MustQuery("select c from t1 where a=1 and b<=>null and c>2 order by c").Check(testkit.Events("3", "4"))
  7155  	tk.MustQuery("select c from t1 where a=1 and b is null and c>2 order by c").Check(testkit.Events("3", "4"))
  7156  	tk.MustQuery("select c from t1 where a=1 and b is not null and c>2 order by c").Check(testkit.Events("33", "44"))
  7157  }
  7158  
  7159  func (s *testIntegrationSerialSuite) TestIssue18652(c *C) {
  7160  	tk := testkit.NewTestKit(c, s.causetstore)
  7161  	tk.MustInterDirc("use test")
  7162  	tk.MustInterDirc("DROP TABLE IF EXISTS t1")
  7163  	tk.MustInterDirc("CREATE TABLE t1 ( `pk` int not null primary key auto_increment, `defCaus_smallint_key_signed` smallint  , key (`defCaus_smallint_key_signed`))")
  7164  	tk.MustInterDirc("INSERT INTO `t1` VALUES (1,0),(2,NULL),(3,NULL),(4,0),(5,0),(6,NULL),(7,NULL),(8,0),(9,0),(10,0)")
  7165  	tk.MustQuery("SELECT * FROM t1 WHERE ( LOG( `defCaus_smallint_key_signed`, -8297584758403770424 ) ) DIV 1").Check(testkit.Events())
  7166  }
  7167  
  7168  func (s *testIntegrationSerialSuite) TestIssue18662(c *C) {
  7169  	defCauslate.SetNewDefCauslationEnabledForTest(true)
  7170  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
  7171  
  7172  	tk := testkit.NewTestKit(c, s.causetstore)
  7173  	tk.MustInterDirc("use test")
  7174  	tk.MustInterDirc("drop causet if exists t")
  7175  	tk.MustInterDirc("create causet t(a varchar(10) defCauslate utf8mb4_bin, b varchar(10) defCauslate utf8mb4_general_ci);")
  7176  	tk.MustInterDirc("insert into t (a, b) values ('a', 'A');")
  7177  	tk.MustQuery("select * from t where field('A', a defCauslate utf8mb4_general_ci, b) > 1;").Check(testkit.Events())
  7178  	tk.MustQuery("select * from t where field('A', a, b defCauslate utf8mb4_general_ci) > 1;").Check(testkit.Events())
  7179  	tk.MustQuery("select * from t where field('A' defCauslate utf8mb4_general_ci, a, b) > 1;").Check(testkit.Events())
  7180  	tk.MustQuery("select * from t where field('A', a, b) > 1;").Check(testkit.Events("a A"))
  7181  }
  7182  
  7183  func (s *testIntegrationSerialSuite) TestIssue19045(c *C) {
  7184  	tk := testkit.NewTestKit(c, s.causetstore)
  7185  	tk.MustInterDirc("use test")
  7186  	tk.MustInterDirc("drop causet if exists t, t1, t2")
  7187  	tk.MustInterDirc(`CREATE TABLE t (
  7188    id int(11) NOT NULL AUTO_INCREMENT,
  7189    a char(10) DEFAULT NULL,
  7190    PRIMARY KEY (id)
  7191  );`)
  7192  	tk.MustInterDirc(`CREATE TABLE t1 (
  7193    id int(11) NOT NULL AUTO_INCREMENT,
  7194    a char(10) DEFAULT NULL,
  7195    b char(10) DEFAULT NULL,
  7196    c char(10) DEFAULT NULL,
  7197    PRIMARY KEY (id)
  7198  );`)
  7199  	tk.MustInterDirc(`CREATE TABLE t2 (
  7200    id int(11) NOT NULL AUTO_INCREMENT,
  7201    a char(10) DEFAULT NULL,
  7202    b char(10) DEFAULT NULL,
  7203    PRIMARY KEY (id),
  7204    UNIQUE KEY b (b)
  7205  );`)
  7206  	tk.MustInterDirc(`insert into t1(a,b,c) values('hs4_0004', "04", "101"), ('a01', "01", "101"),('a011', "02", "101");`)
  7207  	tk.MustInterDirc(`insert into t2(a,b) values("02","03");`)
  7208  	tk.MustInterDirc(`insert into t(a) values('101'),('101');`)
  7209  	tk.MustQuery(`select  ( SELECT t1.a FROM  t1,  t2 WHERE t1.b = t2.a AND  t2.b = '03' AND t1.c = a.a) invode from t a ;`).Check(testkit.Events("a011", "a011"))
  7210  }
  7211  
  7212  func (s *testIntegrationSerialSuite) TestIssue19116(c *C) {
  7213  	defCauslate.SetNewDefCauslationEnabledForTest(true)
  7214  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
  7215  
  7216  	tk := testkit.NewTestKit(c, s.causetstore)
  7217  	tk.MustInterDirc("set names utf8mb4 defCauslate utf8mb4_general_ci;")
  7218  	tk.MustQuery("select defCauslation(concat(1 defCauslate `binary`));").Check(testkit.Events("binary"))
  7219  	tk.MustQuery("select coercibility(concat(1 defCauslate `binary`));").Check(testkit.Events("0"))
  7220  	tk.MustQuery("select defCauslation(concat(NULL,NULL));").Check(testkit.Events("binary"))
  7221  	tk.MustQuery("select coercibility(concat(NULL,NULL));").Check(testkit.Events("6"))
  7222  	tk.MustQuery("select defCauslation(concat(1,1));").Check(testkit.Events("utf8mb4_general_ci"))
  7223  	tk.MustQuery("select coercibility(concat(1,1));").Check(testkit.Events("4"))
  7224  	tk.MustQuery("select defCauslation(1);").Check(testkit.Events("binary"))
  7225  	tk.MustQuery("select coercibility(1);").Check(testkit.Events("5"))
  7226  	tk.MustQuery("select coercibility(1=1);").Check(testkit.Events("5"))
  7227  }
  7228  
  7229  func (s *testIntegrationSerialSuite) TestIssue14448and19383(c *C) {
  7230  	tk := testkit.NewTestKit(c, s.causetstore)
  7231  	tk.MustInterDirc("use test")
  7232  	tk.MustInterDirc("DROP TABLE IF EXISTS t1")
  7233  	tk.MustInterDirc("CREATE TABLE t1 (a INT NOT NULL PRIMARY KEY)")
  7234  	tk.MustInterDirc("INSERT INTO t1 VALUES (1),(2),(3)")
  7235  	_, err := tk.InterDirc("SELECT ALLEGROSQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1")
  7236  	message := `function ALLEGROSQL_CALC_FOUND_ROWS has only noop implementation in milevadb now, use milevadb_enable_noop_functions to enable these functions`
  7237  	c.Assert(strings.Contains(err.Error(), message), IsTrue)
  7238  	_, err = tk.InterDirc("SELECT * FROM t1 LOCK IN SHARE MODE")
  7239  	message = `function LOCK IN SHARE MODE has only noop implementation in milevadb now, use milevadb_enable_noop_functions to enable these functions`
  7240  	c.Assert(strings.Contains(err.Error(), message), IsTrue)
  7241  	tk.MustInterDirc("SET milevadb_enable_noop_functions=1")
  7242  	tk.MustInterDirc("SELECT ALLEGROSQL_CALC_FOUND_ROWS * FROM t1 LIMIT 1")
  7243  	tk.MustInterDirc("SELECT * FROM t1 LOCK IN SHARE MODE")
  7244  }
  7245  
  7246  func (s *testIntegrationSerialSuite) TestIssue19315(c *C) {
  7247  	tk := testkit.NewTestKit(c, s.causetstore)
  7248  	tk.MustInterDirc("use test")
  7249  	tk.MustInterDirc("drop causet if exists t")
  7250  	tk.MustInterDirc("drop causet if exists t1")
  7251  	tk.MustInterDirc("CREATE TABLE `t` (`a` bit(10) DEFAULT NULL,`b` int(11) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")
  7252  	tk.MustInterDirc("INSERT INTO `t` VALUES (_binary '\\0',1),(_binary '\\0',2),(_binary '\\0',5),(_binary '\\0',4),(_binary '\\0',2),(_binary '\\0	',4)")
  7253  	tk.MustInterDirc("CREATE TABLE `t1` (`a` int(11) DEFAULT NULL, `b` int(11) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")
  7254  	tk.MustInterDirc("INSERT INTO `t1` VALUES (1,1),(1,5),(2,3),(2,4),(3,3)")
  7255  	err := tk.QueryToErr("select * from t where t.b > (select min(t1.b) from t1 where t1.a > t.a)")
  7256  	c.Assert(err, IsNil)
  7257  }
  7258  
  7259  func (s *testIntegrationSerialSuite) TestIssue18674(c *C) {
  7260  	tk := testkit.NewTestKit(c, s.causetstore)
  7261  	tk.MustQuery("select -1.0 % -1.0").Check(testkit.Events("0.0"))
  7262  	tk.MustInterDirc("use test")
  7263  	tk.MustInterDirc("drop causet if exists t1")
  7264  	tk.MustInterDirc("create causet t1(`pk` int primary key,`defCaus_float_key_signed` float  ,key (`defCaus_float_key_signed`))")
  7265  	tk.MustInterDirc("insert into t1 values (0, null), (1, 0), (2, -0), (3, 1), (-1,-1)")
  7266  	tk.MustQuery("select * from t1 where ( `defCaus_float_key_signed` % `defCaus_float_key_signed`) IS FALSE").Sort().Check(testkit.Events("-1 -1", "3 1"))
  7267  	tk.MustQuery("select  `defCaus_float_key_signed` , `defCaus_float_key_signed` % `defCaus_float_key_signed` from t1").Sort().Check(testkit.Events(
  7268  		"-1 -0", "0 <nil>", "0 <nil>", "1 0", "<nil> <nil>"))
  7269  	tk.MustQuery("select  `defCaus_float_key_signed` , (`defCaus_float_key_signed` % `defCaus_float_key_signed`) IS FALSE from t1").Sort().Check(testkit.Events(
  7270  		"-1 1", "0 0", "0 0", "1 1", "<nil> 0"))
  7271  }
  7272  
  7273  func (s *testIntegrationSerialSuite) TestIssue17063(c *C) {
  7274  	defCauslate.SetNewDefCauslationEnabledForTest(true)
  7275  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
  7276  
  7277  	tk := testkit.NewTestKit(c, s.causetstore)
  7278  	tk.MustInterDirc(`use test;`)
  7279  	tk.MustInterDirc(`drop causet if exists t;`)
  7280  	tk.MustInterDirc("create causet t(a char, b char) defCauslate utf8mb4_general_ci;")
  7281  	tk.MustInterDirc(`insert into t values('a', 'b');`)
  7282  	tk.MustInterDirc(`insert into t values('a', 'B');`)
  7283  	tk.MustQuery(`select * from t where if(a='x', a, b) = 'b';`).Check(testkit.Events("a b", "a B"))
  7284  	tk.MustQuery(`select defCauslation(if(a='x', a, b)) from t;`).Check(testkit.Events("utf8mb4_general_ci", "utf8mb4_general_ci"))
  7285  	tk.MustQuery(`select coercibility(if(a='x', a, b)) from t;`).Check(testkit.Events("2", "2"))
  7286  	tk.MustQuery(`select defCauslation(lag(b, 1, 'B') over w) from t window w as (order by b);`).Check(testkit.Events("utf8mb4_general_ci", "utf8mb4_general_ci"))
  7287  	tk.MustQuery(`select coercibility(lag(b, 1, 'B') over w) from t window w as (order by b);`).Check(testkit.Events("2", "2"))
  7288  }
  7289  
  7290  func (s *testIntegrationSuite) TestIssue19504(c *C) {
  7291  	tk := testkit.NewTestKit(c, s.causetstore)
  7292  	tk.MustInterDirc("use test")
  7293  	tk.MustInterDirc("drop causet if exists t1;")
  7294  	tk.MustInterDirc("create causet t1 (c_int int, primary key (c_int));")
  7295  	tk.MustInterDirc("insert into t1 values (1), (2), (3);")
  7296  	tk.MustInterDirc("drop causet if exists t2;")
  7297  	tk.MustInterDirc("create causet t2 (c_int int, primary key (c_int));")
  7298  	tk.MustInterDirc("insert into t2 values (1);")
  7299  	tk.MustQuery("select (select count(c_int) from t2 where c_int = t1.c_int) c1, (select count(1) from t2 where c_int = t1.c_int) c2 from t1;").
  7300  		Check(testkit.Events("1 1", "0 0", "0 0"))
  7301  }
  7302  
  7303  func (s *testIntegrationSerialSuite) TestIssue19804(c *C) {
  7304  	defCauslate.SetNewDefCauslationEnabledForTest(true)
  7305  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
  7306  
  7307  	tk := testkit.NewTestKit(c, s.causetstore)
  7308  	tk.MustInterDirc(`use test;`)
  7309  	tk.MustInterDirc(`drop causet if exists t;`)
  7310  	tk.MustInterDirc(`create causet t(a set('a', 'b', 'c'));`)
  7311  	tk.MustGetErrMsg("alter causet t change a a set('a', 'b', 'c', 'c');", "[types:1291]DeferredCauset 'a' has duplicated value 'c' in SET")
  7312  	tk.MustInterDirc(`drop causet if exists t;`)
  7313  	tk.MustInterDirc(`create causet t(a enum('a', 'b', 'c'));`)
  7314  	tk.MustGetErrMsg("alter causet t change a a enum('a', 'b', 'c', 'c');", "[types:1291]DeferredCauset 'a' has duplicated value 'c' in ENUM")
  7315  	tk.MustInterDirc(`drop causet if exists t;`)
  7316  	tk.MustInterDirc(`create causet t(a set('a', 'b', 'c'));`)
  7317  	tk.MustInterDirc(`alter causet t change a a set('a', 'b', 'c', 'd');`)
  7318  	tk.MustGetErrMsg(`alter causet t change a a set('a', 'b', 'c', 'e', 'f');`, "[dbs:8200]Unsupported modify defCausumn: cannot modify set defCausumn value d to e")
  7319  }
  7320  
  7321  func (s *testIntegrationSerialSuite) TestIssue18949(c *C) {
  7322  	defCauslate.SetNewDefCauslationEnabledForTest(true)
  7323  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
  7324  
  7325  	tk := testkit.NewTestKit(c, s.causetstore)
  7326  	tk.MustInterDirc(`use test;`)
  7327  	tk.MustInterDirc(`drop causet if exists t;`)
  7328  	tk.MustInterDirc(`create causet t(a enum('a ', 'b\t', ' c '), b set('a ', 'b\t', ' c '));`)
  7329  	result := tk.MustQuery("show create causet t").Events()[0][1]
  7330  	c.Assert(result, Matches, `(?s).*enum\('a','b	',' c'\).*set\('a','b	',' c'\).*`)
  7331  	tk.MustInterDirc(`alter causet t change a aa enum('a   ', 'b\t', ' c ');`)
  7332  	result = tk.MustQuery("show create causet t").Events()[0][1]
  7333  	c.Assert(result, Matches, `(?s).*enum\('a','b	',' c'\).*set\('a','b	',' c'\).*`)
  7334  }
  7335  
  7336  func (s *testIntegrationSuite) TestIssue19596(c *C) {
  7337  	tk := testkit.NewTestKit(c, s.causetstore)
  7338  	tk.MustInterDirc("use test")
  7339  	tk.MustInterDirc("drop causet if exists t;")
  7340  	tk.MustInterDirc("create causet t (a int) partition by range(a) (PARTITION p0 VALUES LESS THAN (10));")
  7341  	tk.MustGetErrMsg("alter causet t add partition (partition p1 values less than (a));", "[memex:1054]Unknown defCausumn 'a' in 'memex'")
  7342  	tk.MustQuery("select * from t;")
  7343  	tk.MustInterDirc("drop causet if exists t;")
  7344  	tk.MustGetErrMsg("create causet t (a int) partition by range(a) (PARTITION p0 VALUES LESS THAN (a));", "[memex:1054]Unknown defCausumn 'a' in 'memex'")
  7345  }
  7346  
  7347  func (s *testIntegrationSuite) TestIssue17476(c *C) {
  7348  	tk := testkit.NewTestKit(c, s.causetstore)
  7349  	tk.MustInterDirc("use test")
  7350  	tk.MustInterDirc("DROP TABLE IF EXISTS `block_float`;")
  7351  	tk.MustInterDirc("DROP TABLE IF EXISTS `block_int_float_varchar`;")
  7352  	tk.MustInterDirc("CREATE TABLE `block_float` (`id_1` int(16) NOT NULL AUTO_INCREMENT,`defCaus_float_1` float DEFAULT NULL,PRIMARY KEY (`id_1`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=97635;")
  7353  	tk.MustInterDirc("CREATE TABLE `block_int_float_varchar` " +
  7354  		"(`id_6` int(16) NOT NULL AUTO_INCREMENT," +
  7355  		"`defCaus_int_6` int(16) DEFAULT NULL,`defCaus_float_6` float DEFAULT NULL," +
  7356  		"`defCaus_varchar_6` varchar(511) DEFAULT NULL,PRIMARY KEY (`id_6`)," +
  7357  		"KEY `vhyen` (`id_6`,`defCaus_int_6`,`defCaus_float_6`,`defCaus_varchar_6`(1))," +
  7358  		"KEY `zzylq` (`id_6`,`defCaus_int_6`,`defCaus_float_6`,`defCaus_varchar_6`(1))) " +
  7359  		"ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=90818;")
  7360  
  7361  	tk.MustInterDirc("INSERT INTO `block_float` VALUES (1,NULL),(2,0.1),(3,0),(4,-0.1),(5,-0.1),(6,NULL),(7,0.5),(8,0),(9,0),(10,NULL),(11,1),(12,1.5),(13,NULL),(14,NULL);")
  7362  	tk.MustInterDirc("INSERT INTO `block_int_float_varchar` VALUES (1,0,0.1,'true'),(2,-1,1.5,'2020-02-02 02:02:00'),(3,NULL,1.5,NULL),(4,65535,0.1,'true'),(5,NULL,0.1,'1'),(6,-1,1.5,'2020-02-02 02:02:00'),(7,-1,NULL,''),(8,NULL,-0.1,NULL),(9,NULL,-0.1,'1'),(10,-1,NULL,''),(11,NULL,1.5,'false'),(12,-1,0,NULL),(13,0,-0.1,NULL),(14,-1,NULL,'-0'),(15,65535,-1,'1'),(16,NULL,0.5,NULL),(17,-1,NULL,NULL);")
  7363  	tk.MustQuery(`select count(*) from block_float
  7364   JOIN block_int_float_varchar AS tmp3 ON (tmp3.defCaus_varchar_6 AND NULL)
  7365   IS NULL WHERE defCaus_int_6=0;`).Check(testkit.Events("14"))
  7366  	tk.MustQuery(`SELECT count(*) FROM (block_float JOIN block_int_float_varchar AS tmp3 ON (tmp3.defCaus_varchar_6 AND NULL) IS NULL);`).Check(testkit.Events("154"))
  7367  	tk.MustQuery(`SELECT * FROM (block_int_float_varchar AS tmp3) WHERE (defCaus_varchar_6 AND NULL) IS NULL AND defCaus_int_6=0;`).Check(testkit.Events("13 0 -0.1 <nil>"))
  7368  }