github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/dbs/db_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 dbs_test
    15  
    16  import (
    17  	"context"
    18  	"fmt"
    19  	"strconv"
    20  	"strings"
    21  	"sync/atomic"
    22  	"time"
    23  
    24  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    25  	"github.com/whtcorpsinc/BerolinaSQL/ast"
    26  	"github.com/whtcorpsinc/BerolinaSQL/charset"
    27  	"github.com/whtcorpsinc/BerolinaSQL/perceptron"
    28  	"github.com/whtcorpsinc/BerolinaSQL/terror"
    29  	. "github.com/whtcorpsinc/check"
    30  	"github.com/whtcorpsinc/errors"
    31  	"github.com/whtcorpsinc/milevadb/causet"
    32  	"github.com/whtcorpsinc/milevadb/causetstore/mockstore"
    33  	"github.com/whtcorpsinc/milevadb/causetstore/mockstore/cluster"
    34  	"github.com/whtcorpsinc/milevadb/config"
    35  	"github.com/whtcorpsinc/milevadb/dbs"
    36  	"github.com/whtcorpsinc/milevadb/ekv"
    37  	"github.com/whtcorpsinc/milevadb/errno"
    38  	"github.com/whtcorpsinc/milevadb/petri"
    39  	"github.com/whtcorpsinc/milevadb/schemareplicant"
    40  	"github.com/whtcorpsinc/milevadb/soliton/israce"
    41  	"github.com/whtcorpsinc/milevadb/soliton/mock"
    42  	"github.com/whtcorpsinc/milevadb/soliton/solitonutil"
    43  	"github.com/whtcorpsinc/milevadb/soliton/testkit"
    44  	"github.com/whtcorpsinc/milevadb/spacetime"
    45  	"github.com/whtcorpsinc/milevadb/stochastik"
    46  	"github.com/whtcorpsinc/milevadb/stochastikctx"
    47  	"github.com/whtcorpsinc/milevadb/stochastikctx/stmtctx"
    48  	"github.com/whtcorpsinc/milevadb/types"
    49  )
    50  
    51  var _ = Suite(&testIntegrationSuite1{&testIntegrationSuite{}})
    52  var _ = Suite(&testIntegrationSuite2{&testIntegrationSuite{}})
    53  var _ = Suite(&testIntegrationSuite3{&testIntegrationSuite{}})
    54  var _ = Suite(&testIntegrationSuite4{&testIntegrationSuite{}})
    55  var _ = Suite(&testIntegrationSuite5{&testIntegrationSuite{}})
    56  var _ = Suite(&testIntegrationSuite6{&testIntegrationSuite{}})
    57  var _ = SerialSuites(&testIntegrationSuite7{&testIntegrationSuite{}})
    58  
    59  type testIntegrationSuite struct {
    60  	lease       time.Duration
    61  	cluster     cluster.Cluster
    62  	causetstore ekv.CausetStorage
    63  	dom         *petri.Petri
    64  	ctx         stochastikctx.Context
    65  }
    66  
    67  func setupIntegrationSuite(s *testIntegrationSuite, c *C) {
    68  	var err error
    69  	s.lease = 50 * time.Millisecond
    70  	dbs.SetWaitTimeWhenErrorOccurred(0)
    71  
    72  	s.causetstore, err = mockstore.NewMockStore(
    73  		mockstore.WithClusterInspector(func(c cluster.Cluster) {
    74  			mockstore.BootstrapWithSingleStore(c)
    75  			s.cluster = c
    76  		}),
    77  	)
    78  	c.Assert(err, IsNil)
    79  	stochastik.SetSchemaLease(s.lease)
    80  	stochastik.DisableStats4Test()
    81  	s.dom, err = stochastik.BootstrapStochastik(s.causetstore)
    82  	c.Assert(err, IsNil)
    83  
    84  	se, err := stochastik.CreateStochastik4Test(s.causetstore)
    85  	c.Assert(err, IsNil)
    86  	s.ctx = se.(stochastikctx.Context)
    87  	_, err = se.InterDircute(context.Background(), "create database test_db")
    88  	c.Assert(err, IsNil)
    89  }
    90  
    91  func tearDownIntegrationSuiteTest(s *testIntegrationSuite, c *C) {
    92  	tk := testkit.NewTestKit(c, s.causetstore)
    93  	tk.MustInterDirc("use test")
    94  	r := tk.MustQuery("show blocks")
    95  	for _, tb := range r.Rows() {
    96  		blockName := tb[0]
    97  		tk.MustInterDirc(fmt.Sprintf("drop causet %v", blockName))
    98  	}
    99  }
   100  
   101  func tearDownIntegrationSuite(s *testIntegrationSuite, c *C) {
   102  	s.dom.Close()
   103  	s.causetstore.Close()
   104  }
   105  
   106  func (s *testIntegrationSuite) SetUpSuite(c *C) {
   107  	setupIntegrationSuite(s, c)
   108  }
   109  
   110  func (s *testIntegrationSuite) TearDownSuite(c *C) {
   111  	tearDownIntegrationSuite(s, c)
   112  }
   113  
   114  type testIntegrationSuite1 struct{ *testIntegrationSuite }
   115  type testIntegrationSuite2 struct{ *testIntegrationSuite }
   116  
   117  func (s *testIntegrationSuite2) TearDownTest(c *C) {
   118  	tearDownIntegrationSuiteTest(s.testIntegrationSuite, c)
   119  }
   120  
   121  type testIntegrationSuite3 struct{ *testIntegrationSuite }
   122  type testIntegrationSuite4 struct{ *testIntegrationSuite }
   123  type testIntegrationSuite5 struct{ *testIntegrationSuite }
   124  type testIntegrationSuite6 struct{ *testIntegrationSuite }
   125  type testIntegrationSuite7 struct{ *testIntegrationSuite }
   126  
   127  func (s *testIntegrationSuite5) TestNoZeroDateMode(c *C) {
   128  	tk := testkit.NewTestKit(c, s.causetstore)
   129  
   130  	defer tk.MustInterDirc("set stochastik sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';")
   131  
   132  	tk.MustInterDirc("use test;")
   133  	tk.MustInterDirc("set stochastik sql_mode='STRICT_TRANS_TABLES,NO_ZERO_DATE,NO_ENGINE_SUBSTITUTION';")
   134  	tk.MustGetErrCode("create causet test_zero_date(agent_start_time date NOT NULL DEFAULT '0000-00-00')", errno.ErrInvalidDefault)
   135  	tk.MustGetErrCode("create causet test_zero_date(agent_start_time datetime NOT NULL DEFAULT '0000-00-00 00:00:00')", errno.ErrInvalidDefault)
   136  	tk.MustGetErrCode("create causet test_zero_date(agent_start_time timestamp NOT NULL DEFAULT '0000-00-00 00:00:00')", errno.ErrInvalidDefault)
   137  	tk.MustGetErrCode("create causet test_zero_date(a timestamp default '0000-00-00 00');", errno.ErrInvalidDefault)
   138  	tk.MustGetErrCode("create causet test_zero_date(a timestamp default 0);", errno.ErrInvalidDefault)
   139  }
   140  
   141  func (s *testIntegrationSuite2) TestInvalidDefault(c *C) {
   142  	tk := testkit.NewTestKit(c, s.causetstore)
   143  
   144  	tk.MustInterDirc("USE test;")
   145  
   146  	_, err := tk.InterDirc("create causet t(c1 decimal default 1.7976931348623157E308)")
   147  	c.Assert(err, NotNil)
   148  	c.Assert(terror.ErrorEqual(err, types.ErrInvalidDefault), IsTrue, Commentf("err %v", err))
   149  
   150  	_, err = tk.InterDirc("create causet t( c1 varchar(2) default 'MilevaDB');")
   151  	c.Assert(err, NotNil)
   152  	c.Assert(terror.ErrorEqual(err, types.ErrInvalidDefault), IsTrue, Commentf("err %v", err))
   153  }
   154  
   155  // TestKeyWithoutLength for issue #13452
   156  func (s testIntegrationSuite3) TestKeyWithoutLengthCreateTable(c *C) {
   157  	tk := testkit.NewTestKit(c, s.causetstore)
   158  
   159  	tk.MustInterDirc("USE test")
   160  
   161  	_, err := tk.InterDirc("create causet t_without_length (a text primary key)")
   162  	c.Assert(err, NotNil)
   163  	c.Assert(err, ErrorMatches, ".*BLOB/TEXT defCausumn 'a' used in key specification without a key length")
   164  }
   165  
   166  // TestInvalidNameWhenCreateTable for issue #3848
   167  func (s *testIntegrationSuite3) TestInvalidNameWhenCreateTable(c *C) {
   168  	tk := testkit.NewTestKit(c, s.causetstore)
   169  
   170  	tk.MustInterDirc("USE test;")
   171  
   172  	_, err := tk.InterDirc("create causet t(xxx.t.a bigint)")
   173  	c.Assert(err, NotNil)
   174  	c.Assert(terror.ErrorEqual(err, dbs.ErrWrongDBName), IsTrue, Commentf("err %v", err))
   175  
   176  	_, err = tk.InterDirc("create causet t(test.tttt.a bigint)")
   177  	c.Assert(err, NotNil)
   178  	c.Assert(terror.ErrorEqual(err, dbs.ErrWrongTableName), IsTrue, Commentf("err %v", err))
   179  
   180  	_, err = tk.InterDirc("create causet t(t.tttt.a bigint)")
   181  	c.Assert(err, NotNil)
   182  	c.Assert(terror.ErrorEqual(err, dbs.ErrWrongDBName), IsTrue, Commentf("err %v", err))
   183  }
   184  
   185  // TestCreateTableIfNotExists for issue #6879
   186  func (s *testIntegrationSuite3) TestCreateTableIfNotExists(c *C) {
   187  	tk := testkit.NewTestKit(c, s.causetstore)
   188  
   189  	tk.MustInterDirc("USE test;")
   190  
   191  	tk.MustInterDirc("create causet ct1(a bigint)")
   192  	tk.MustInterDirc("create causet ct(a bigint)")
   193  
   194  	// Test duplicate create-causet with `LIKE` clause
   195  	tk.MustInterDirc("create causet if not exists ct like ct1;")
   196  	warnings := tk.Se.GetStochastikVars().StmtCtx.GetWarnings()
   197  	c.Assert(len(warnings), GreaterEqual, 1)
   198  	lastWarn := warnings[len(warnings)-1]
   199  	c.Assert(terror.ErrorEqual(schemareplicant.ErrTableExists, lastWarn.Err), IsTrue, Commentf("err %v", lastWarn.Err))
   200  	c.Assert(lastWarn.Level, Equals, stmtctx.WarnLevelNote)
   201  
   202  	// Test duplicate create-causet without `LIKE` clause
   203  	tk.MustInterDirc("create causet if not exists ct(b bigint, c varchar(60));")
   204  	warnings = tk.Se.GetStochastikVars().StmtCtx.GetWarnings()
   205  	c.Assert(len(warnings), GreaterEqual, 1)
   206  	lastWarn = warnings[len(warnings)-1]
   207  	c.Assert(terror.ErrorEqual(schemareplicant.ErrTableExists, lastWarn.Err), IsTrue)
   208  }
   209  
   210  // for issue #9910
   211  func (s *testIntegrationSuite2) TestCreateTableWithKeyWord(c *C) {
   212  	tk := testkit.NewTestKit(c, s.causetstore)
   213  
   214  	tk.MustInterDirc("USE test;")
   215  
   216  	_, err := tk.InterDirc("create causet t1(pump varchar(20), drainer varchar(20), node_id varchar(20), node_state varchar(20));")
   217  	c.Assert(err, IsNil)
   218  }
   219  
   220  func (s *testIntegrationSuite1) TestUniqueKeyNullValue(c *C) {
   221  	tk := testkit.NewTestKit(c, s.causetstore)
   222  	tk.MustInterDirc("USE test")
   223  	tk.MustInterDirc("create causet t(a int primary key, b varchar(255))")
   224  
   225  	tk.MustInterDirc("insert into t values(1, NULL)")
   226  	tk.MustInterDirc("insert into t values(2, NULL)")
   227  	tk.MustInterDirc("alter causet t add unique index b(b);")
   228  	res := tk.MustQuery("select count(*) from t use index(b);")
   229  	res.Check(testkit.Rows("2"))
   230  	tk.MustInterDirc("admin check causet t")
   231  	tk.MustInterDirc("admin check index t b")
   232  }
   233  
   234  func (s *testIntegrationSuite2) TestUniqueKeyNullValueClusterIndex(c *C) {
   235  	tk := testkit.NewTestKit(c, s.causetstore)
   236  
   237  	tk.MustInterDirc("drop database if exists unique_null_val;")
   238  	tk.MustInterDirc("create database unique_null_val;")
   239  	tk.MustInterDirc("use unique_null_val;")
   240  	tk.MustInterDirc("create causet t (a varchar(10), b float, c varchar(255), primary key (a, b));")
   241  	tk.MustInterDirc("insert into t values ('1', 1, NULL);")
   242  	tk.MustInterDirc("insert into t values ('2', 2, NULL);")
   243  	tk.MustInterDirc("alter causet t add unique index c(c);")
   244  	tk.MustQuery("select count(*) from t use index(c);").Check(testkit.Rows("2"))
   245  	tk.MustInterDirc("admin check causet t;")
   246  	tk.MustInterDirc("admin check index t c;")
   247  }
   248  
   249  // TestModifyDeferredCausetAfterAddIndex Issue 5134
   250  func (s *testIntegrationSuite3) TestModifyDeferredCausetAfterAddIndex(c *C) {
   251  	tk := testkit.NewTestKit(c, s.causetstore)
   252  	tk.MustInterDirc("use test")
   253  	tk.MustInterDirc("create causet city (city VARCHAR(2) KEY);")
   254  	tk.MustInterDirc("alter causet city change defCausumn city city varchar(50);")
   255  	tk.MustInterDirc(`insert into city values ("abc"), ("abd");`)
   256  }
   257  
   258  func (s *testIntegrationSuite3) TestIssue2293(c *C) {
   259  	tk := testkit.NewTestKit(c, s.causetstore)
   260  	tk.MustInterDirc("use test")
   261  	tk.MustInterDirc("create causet t_issue_2293 (a int)")
   262  	tk.MustGetErrCode("alter causet t_issue_2293 add b int not null default 'a'", errno.ErrInvalidDefault)
   263  	tk.MustInterDirc("insert into t_issue_2293 value(1)")
   264  	tk.MustQuery("select * from t_issue_2293").Check(testkit.Rows("1"))
   265  }
   266  
   267  func (s *testIntegrationSuite2) TestIssue6101(c *C) {
   268  	tk := testkit.NewTestKit(c, s.causetstore)
   269  	tk.MustInterDirc("use test")
   270  	tk.MustInterDirc("create causet t1 (quantity decimal(2) unsigned);")
   271  	_, err := tk.InterDirc("insert into t1 values (500), (-500), (~0), (-1);")
   272  	terr := errors.Cause(err).(*terror.Error)
   273  	c.Assert(terr.Code(), Equals, errors.ErrCode(errno.ErrWarnDataOutOfRange))
   274  	tk.MustInterDirc("drop causet t1")
   275  
   276  	tk.MustInterDirc("set sql_mode=''")
   277  	tk.MustInterDirc("create causet t1 (quantity decimal(2) unsigned);")
   278  	tk.MustInterDirc("insert into t1 values (500), (-500), (~0), (-1);")
   279  	tk.MustQuery("select * from t1").Check(testkit.Rows("99", "0", "99", "0"))
   280  	tk.MustInterDirc("drop causet t1")
   281  }
   282  
   283  func (s *testIntegrationSuite2) TestIssue19229(c *C) {
   284  	tk := testkit.NewTestKit(c, s.causetstore)
   285  	tk.MustInterDirc("use test")
   286  	tk.MustInterDirc("CREATE TABLE enumt (type enum('a', 'b') );")
   287  	_, err := tk.InterDirc("insert into enumt values('xxx');")
   288  	terr := errors.Cause(err).(*terror.Error)
   289  	c.Assert(terr.Code(), Equals, errors.ErrCode(errno.WarnDataTruncated))
   290  	_, err = tk.InterDirc("insert into enumt values(-1);")
   291  	terr = errors.Cause(err).(*terror.Error)
   292  	c.Assert(terr.Code(), Equals, errors.ErrCode(errno.WarnDataTruncated))
   293  	tk.MustInterDirc("drop causet enumt")
   294  
   295  	tk.MustInterDirc("CREATE TABLE sett (type set('a', 'b') );")
   296  	_, err = tk.InterDirc("insert into sett values('xxx');")
   297  	terr = errors.Cause(err).(*terror.Error)
   298  	c.Assert(terr.Code(), Equals, errors.ErrCode(errno.WarnDataTruncated))
   299  	_, err = tk.InterDirc("insert into sett values(-1);")
   300  	terr = errors.Cause(err).(*terror.Error)
   301  	c.Assert(terr.Code(), Equals, errors.ErrCode(errno.WarnDataTruncated))
   302  	tk.MustInterDirc("drop causet sett")
   303  }
   304  
   305  func (s *testIntegrationSuite1) TestIndexLength(c *C) {
   306  	tk := testkit.NewTestKit(c, s.causetstore)
   307  	tk.MustInterDirc("use test")
   308  	tk.MustInterDirc("create causet idx_len(a int(0), b timestamp(0), c datetime(0), d time(0), f float(0), g decimal(0))")
   309  	tk.MustInterDirc("create index idx on idx_len(a)")
   310  	tk.MustInterDirc("alter causet idx_len add index idxa(a)")
   311  	tk.MustInterDirc("create index idx1 on idx_len(b)")
   312  	tk.MustInterDirc("alter causet idx_len add index idxb(b)")
   313  	tk.MustInterDirc("create index idx2 on idx_len(c)")
   314  	tk.MustInterDirc("alter causet idx_len add index idxc(c)")
   315  	tk.MustInterDirc("create index idx3 on idx_len(d)")
   316  	tk.MustInterDirc("alter causet idx_len add index idxd(d)")
   317  	tk.MustInterDirc("create index idx4 on idx_len(f)")
   318  	tk.MustInterDirc("alter causet idx_len add index idxf(f)")
   319  	tk.MustInterDirc("create index idx5 on idx_len(g)")
   320  	tk.MustInterDirc("alter causet idx_len add index idxg(g)")
   321  	tk.MustInterDirc("create causet idx_len1(a int(0), b timestamp(0), c datetime(0), d time(0), f float(0), g decimal(0), index(a), index(b), index(c), index(d), index(f), index(g))")
   322  }
   323  
   324  func (s *testIntegrationSuite3) TestIssue3833(c *C) {
   325  	tk := testkit.NewTestKit(c, s.causetstore)
   326  	tk.MustInterDirc("use test")
   327  	tk.MustInterDirc("create causet issue3833 (b char(0), c binary(0), d  varchar(0))")
   328  	tk.MustGetErrCode("create index idx on issue3833 (b)", errno.ErrWrongKeyDeferredCauset)
   329  	tk.MustGetErrCode("alter causet issue3833 add index idx (b)", errno.ErrWrongKeyDeferredCauset)
   330  	tk.MustGetErrCode("create causet issue3833_2 (b char(0), c binary(0), d varchar(0), index(b))", errno.ErrWrongKeyDeferredCauset)
   331  	tk.MustGetErrCode("create index idx on issue3833 (c)", errno.ErrWrongKeyDeferredCauset)
   332  	tk.MustGetErrCode("alter causet issue3833 add index idx (c)", errno.ErrWrongKeyDeferredCauset)
   333  	tk.MustGetErrCode("create causet issue3833_2 (b char(0), c binary(0), d varchar(0), index(c))", errno.ErrWrongKeyDeferredCauset)
   334  	tk.MustGetErrCode("create index idx on issue3833 (d)", errno.ErrWrongKeyDeferredCauset)
   335  	tk.MustGetErrCode("alter causet issue3833 add index idx (d)", errno.ErrWrongKeyDeferredCauset)
   336  	tk.MustGetErrCode("create causet issue3833_2 (b char(0), c binary(0), d varchar(0), index(d))", errno.ErrWrongKeyDeferredCauset)
   337  }
   338  
   339  func (s *testIntegrationSuite1) TestIssue2858And2717(c *C) {
   340  	tk := testkit.NewTestKit(c, s.causetstore)
   341  	tk.MustInterDirc("use test")
   342  
   343  	tk.MustInterDirc("create causet t_issue_2858_bit (a bit(64) default b'0')")
   344  	tk.MustInterDirc("insert into t_issue_2858_bit value ()")
   345  	tk.MustInterDirc(`insert into t_issue_2858_bit values (100), ('10'), ('\0')`)
   346  	tk.MustQuery("select a+0 from t_issue_2858_bit").Check(testkit.Rows("0", "100", "12592", "0"))
   347  	tk.MustInterDirc(`alter causet t_issue_2858_bit alter defCausumn a set default '\0'`)
   348  
   349  	tk.MustInterDirc("create causet t_issue_2858_hex (a int default 0x123)")
   350  	tk.MustInterDirc("insert into t_issue_2858_hex value ()")
   351  	tk.MustInterDirc("insert into t_issue_2858_hex values (123), (0x321)")
   352  	tk.MustQuery("select a from t_issue_2858_hex").Check(testkit.Rows("291", "123", "801"))
   353  	tk.MustInterDirc(`alter causet t_issue_2858_hex alter defCausumn a set default 0x321`)
   354  }
   355  
   356  func (s *testIntegrationSuite1) TestIssue4432(c *C) {
   357  	tk := testkit.NewTestKit(c, s.causetstore)
   358  	tk.MustInterDirc("use test")
   359  
   360  	tk.MustInterDirc("create causet tx (defCaus bit(10) default 'a')")
   361  	tk.MustInterDirc("insert into tx value ()")
   362  	tk.MustQuery("select * from tx").Check(testkit.Rows("\x00a"))
   363  	tk.MustInterDirc("drop causet tx")
   364  
   365  	tk.MustInterDirc("create causet tx (defCaus bit(10) default 0x61)")
   366  	tk.MustInterDirc("insert into tx value ()")
   367  	tk.MustQuery("select * from tx").Check(testkit.Rows("\x00a"))
   368  	tk.MustInterDirc("drop causet tx")
   369  
   370  	tk.MustInterDirc("create causet tx (defCaus bit(10) default 97)")
   371  	tk.MustInterDirc("insert into tx value ()")
   372  	tk.MustQuery("select * from tx").Check(testkit.Rows("\x00a"))
   373  	tk.MustInterDirc("drop causet tx")
   374  
   375  	tk.MustInterDirc("create causet tx (defCaus bit(10) default 0b1100001)")
   376  	tk.MustInterDirc("insert into tx value ()")
   377  	tk.MustQuery("select * from tx").Check(testkit.Rows("\x00a"))
   378  	tk.MustInterDirc("drop causet tx")
   379  }
   380  
   381  func (s *testIntegrationSuite1) TestIssue5092(c *C) {
   382  	tk := testkit.NewTestKit(c, s.causetstore)
   383  	tk.MustInterDirc("use test")
   384  
   385  	tk.MustInterDirc("create causet t_issue_5092 (a int)")
   386  	tk.MustInterDirc("alter causet t_issue_5092 add defCausumn (b int, c int)")
   387  	tk.MustInterDirc("alter causet t_issue_5092 add defCausumn if not exists (b int, c int)")
   388  	tk.MustInterDirc("alter causet t_issue_5092 add defCausumn b1 int after b, add defCausumn c1 int after c")
   389  	tk.MustInterDirc("alter causet t_issue_5092 add defCausumn d int after b, add defCausumn e int first, add defCausumn f int after c1, add defCausumn g int, add defCausumn h int first")
   390  	tk.MustQuery("show create causet t_issue_5092").Check(testkit.Rows("t_issue_5092 CREATE TABLE `t_issue_5092` (\n" +
   391  		"  `h` int(11) DEFAULT NULL,\n" +
   392  		"  `e` int(11) DEFAULT NULL,\n" +
   393  		"  `a` int(11) DEFAULT NULL,\n" +
   394  		"  `b` int(11) DEFAULT NULL,\n" +
   395  		"  `d` int(11) DEFAULT NULL,\n" +
   396  		"  `b1` int(11) DEFAULT NULL,\n" +
   397  		"  `c` int(11) DEFAULT NULL,\n" +
   398  		"  `c1` int(11) DEFAULT NULL,\n" +
   399  		"  `f` int(11) DEFAULT NULL,\n" +
   400  		"  `g` int(11) DEFAULT NULL\n" +
   401  		") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))
   402  	// The following two memexs are consistent with MariaDB.
   403  	tk.MustGetErrCode("alter causet t_issue_5092 add defCausumn if not exists d int, add defCausumn d int", errno.ErrDupFieldName)
   404  	tk.MustInterDirc("alter causet t_issue_5092 add defCausumn dd int, add defCausumn if not exists dd int")
   405  	tk.MustInterDirc("alter causet t_issue_5092 add defCausumn if not exists (d int, e int), add defCausumn ff text")
   406  	tk.MustInterDirc("alter causet t_issue_5092 add defCausumn b2 int after b1, add defCausumn c2 int first")
   407  	tk.MustQuery("show create causet t_issue_5092").Check(testkit.Rows("t_issue_5092 CREATE TABLE `t_issue_5092` (\n" +
   408  		"  `c2` int(11) DEFAULT NULL,\n" +
   409  		"  `h` int(11) DEFAULT NULL,\n" +
   410  		"  `e` int(11) DEFAULT NULL,\n" +
   411  		"  `a` int(11) DEFAULT NULL,\n" +
   412  		"  `b` int(11) DEFAULT NULL,\n" +
   413  		"  `d` int(11) DEFAULT NULL,\n" +
   414  		"  `b1` int(11) DEFAULT NULL,\n" +
   415  		"  `b2` int(11) DEFAULT NULL,\n" +
   416  		"  `c` int(11) DEFAULT NULL,\n" +
   417  		"  `c1` int(11) DEFAULT NULL,\n" +
   418  		"  `f` int(11) DEFAULT NULL,\n" +
   419  		"  `g` int(11) DEFAULT NULL,\n" +
   420  		"  `dd` int(11) DEFAULT NULL,\n" +
   421  		"  `ff` text DEFAULT NULL\n" +
   422  		") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))
   423  	tk.MustInterDirc("drop causet t_issue_5092")
   424  
   425  	tk.MustInterDirc("create causet t_issue_5092 (a int default 1)")
   426  	tk.MustInterDirc("alter causet t_issue_5092 add defCausumn (b int default 2, c int default 3)")
   427  	tk.MustInterDirc("alter causet t_issue_5092 add defCausumn b1 int default 22 after b, add defCausumn c1 int default 33 after c")
   428  	tk.MustInterDirc("insert into t_issue_5092 value ()")
   429  	tk.MustQuery("select * from t_issue_5092").Check(testkit.Rows("1 2 22 3 33"))
   430  	tk.MustInterDirc("alter causet t_issue_5092 add defCausumn d int default 4 after c1, add defCausumn aa int default 0 first")
   431  	tk.MustQuery("select * from t_issue_5092").Check(testkit.Rows("0 1 2 22 3 33 4"))
   432  	tk.MustQuery("show create causet t_issue_5092").Check(testkit.Rows("t_issue_5092 CREATE TABLE `t_issue_5092` (\n" +
   433  		"  `aa` int(11) DEFAULT 0,\n" +
   434  		"  `a` int(11) DEFAULT 1,\n" +
   435  		"  `b` int(11) DEFAULT 2,\n" +
   436  		"  `b1` int(11) DEFAULT 22,\n" +
   437  		"  `c` int(11) DEFAULT 3,\n" +
   438  		"  `c1` int(11) DEFAULT 33,\n" +
   439  		"  `d` int(11) DEFAULT 4\n" +
   440  		") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))
   441  	tk.MustInterDirc("drop causet t_issue_5092")
   442  
   443  	tk.MustInterDirc("create causet t_issue_5092 (a int)")
   444  	tk.MustInterDirc("alter causet t_issue_5092 add defCausumn (b int, c int)")
   445  	tk.MustInterDirc("alter causet t_issue_5092 drop defCausumn b,drop defCausumn c")
   446  	tk.MustGetErrCode("alter causet t_issue_5092 drop defCausumn c, drop defCausumn c", errno.ErrCantDropFieldOrKey)
   447  	tk.MustInterDirc("alter causet t_issue_5092 drop defCausumn if exists b,drop defCausumn if exists c")
   448  	tk.MustGetErrCode("alter causet t_issue_5092 drop defCausumn g, drop defCausumn d", errno.ErrCantDropFieldOrKey)
   449  	tk.MustInterDirc("drop causet t_issue_5092")
   450  
   451  	tk.MustInterDirc("create causet t_issue_5092 (a int)")
   452  	tk.MustInterDirc("alter causet t_issue_5092 add defCausumn (b int, c int)")
   453  	tk.MustGetErrCode("alter causet t_issue_5092 drop defCausumn if exists a, drop defCausumn b, drop defCausumn c", errno.ErrCantRemoveAllFields)
   454  	tk.MustGetErrCode("alter causet t_issue_5092 drop defCausumn if exists c, drop defCausumn c", errno.ErrCantDropFieldOrKey)
   455  	tk.MustInterDirc("alter causet t_issue_5092 drop defCausumn c, drop defCausumn if exists c")
   456  	tk.MustInterDirc("drop causet t_issue_5092")
   457  }
   458  
   459  func (s *testIntegrationSuite5) TestErrnoErrorCode(c *C) {
   460  	tk := testkit.NewTestKit(c, s.causetstore)
   461  	tk.MustInterDirc("use test_db")
   462  
   463  	// create database
   464  	allegrosql := "create database aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
   465  	tk.MustGetErrCode(allegrosql, errno.ErrTooLongIdent)
   466  	allegrosql = "create database test"
   467  	tk.MustGetErrCode(allegrosql, errno.ErrDBCreateExists)
   468  	allegrosql = "create database test1 character set uft8;"
   469  	tk.MustGetErrCode(allegrosql, errno.ErrUnknownCharacterSet)
   470  	allegrosql = "create database test2 character set gkb;"
   471  	tk.MustGetErrCode(allegrosql, errno.ErrUnknownCharacterSet)
   472  	allegrosql = "create database test3 character set laitn1;"
   473  	tk.MustGetErrCode(allegrosql, errno.ErrUnknownCharacterSet)
   474  	// drop database
   475  	allegrosql = "drop database db_not_exist"
   476  	tk.MustGetErrCode(allegrosql, errno.ErrDBDropExists)
   477  	// create causet
   478  	tk.MustInterDirc("create causet test_error_code_succ (c1 int, c2 int, c3 int, primary key(c3))")
   479  	allegrosql = "create causet test_error_code_succ (c1 int, c2 int, c3 int)"
   480  	tk.MustGetErrCode(allegrosql, errno.ErrTableExists)
   481  	allegrosql = "create causet test_error_code1 (c1 int, c2 int, c2 int)"
   482  	tk.MustGetErrCode(allegrosql, errno.ErrDupFieldName)
   483  	allegrosql = "create causet test_error_code1 (c1 int, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa int)"
   484  	tk.MustGetErrCode(allegrosql, errno.ErrTooLongIdent)
   485  	allegrosql = "create causet test_error_code1 (c1 int, `_milevadb_rowid` int)"
   486  	tk.MustGetErrCode(allegrosql, errno.ErrWrongDeferredCausetName)
   487  	allegrosql = "create causet aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(a int)"
   488  	tk.MustGetErrCode(allegrosql, errno.ErrTooLongIdent)
   489  	allegrosql = "create causet test_error_code1 (c1 int, c2 int, key aa (c1, c2), key aa (c1))"
   490  	tk.MustGetErrCode(allegrosql, errno.ErrDupKeyName)
   491  	allegrosql = "create causet test_error_code1 (c1 int, c2 int, c3 int, key(c_not_exist))"
   492  	tk.MustGetErrCode(allegrosql, errno.ErrKeyDeferredCausetDoesNotExits)
   493  	allegrosql = "create causet test_error_code1 (c1 int, c2 int, c3 int, primary key(c_not_exist))"
   494  	tk.MustGetErrCode(allegrosql, errno.ErrKeyDeferredCausetDoesNotExits)
   495  	allegrosql = "create causet test_error_code1 (c1 int not null default '')"
   496  	tk.MustGetErrCode(allegrosql, errno.ErrInvalidDefault)
   497  	allegrosql = "CREATE TABLE `t` (`a` double DEFAULT 1.0 DEFAULT 2.0 DEFAULT now());"
   498  	tk.MustGetErrCode(allegrosql, errno.ErrInvalidDefault)
   499  	allegrosql = "CREATE TABLE `t` (`a` double DEFAULT now());"
   500  	tk.MustGetErrCode(allegrosql, errno.ErrInvalidDefault)
   501  	allegrosql = "create causet t1(a int) character set uft8;"
   502  	tk.MustGetErrCode(allegrosql, errno.ErrUnknownCharacterSet)
   503  	allegrosql = "create causet t1(a int) character set gkb;"
   504  	tk.MustGetErrCode(allegrosql, errno.ErrUnknownCharacterSet)
   505  	allegrosql = "create causet t1(a int) character set laitn1;"
   506  	tk.MustGetErrCode(allegrosql, errno.ErrUnknownCharacterSet)
   507  	allegrosql = "create causet test_error_code (a int not null ,b int not null,c int not null, d int not null, foreign key (b, c) references product(id));"
   508  	tk.MustGetErrCode(allegrosql, errno.ErrWrongFkDef)
   509  	allegrosql = "create causet test_error_code_2;"
   510  	tk.MustGetErrCode(allegrosql, errno.ErrTableMustHaveDeferredCausets)
   511  	allegrosql = "create causet test_error_code_2 (unique(c1));"
   512  	tk.MustGetErrCode(allegrosql, errno.ErrTableMustHaveDeferredCausets)
   513  	allegrosql = "create causet test_error_code_2(c1 int, c2 int, c3 int, primary key(c1), primary key(c2));"
   514  	tk.MustGetErrCode(allegrosql, errno.ErrMultiplePriKey)
   515  	allegrosql = "create causet test_error_code_3(pt blob ,primary key (pt));"
   516  	tk.MustGetErrCode(allegrosql, errno.ErrBlobKeyWithoutLength)
   517  	allegrosql = "create causet test_error_code_3(a text, unique (a(3073)));"
   518  	tk.MustGetErrCode(allegrosql, errno.ErrTooLongKey)
   519  	allegrosql = "create causet test_error_code_3(`id` int, key `primary`(`id`));"
   520  	tk.MustGetErrCode(allegrosql, errno.ErrWrongNameForIndex)
   521  	allegrosql = "create causet t2(c1.c2 blob default null);"
   522  	tk.MustGetErrCode(allegrosql, errno.ErrWrongTableName)
   523  	allegrosql = "create causet t2 (id int default null primary key , age int);"
   524  	tk.MustGetErrCode(allegrosql, errno.ErrInvalidDefault)
   525  	allegrosql = "create causet t2 (id int null primary key , age int);"
   526  	tk.MustGetErrCode(allegrosql, errno.ErrPrimaryCantHaveNull)
   527  	allegrosql = "create causet t2 (id int default null, age int, primary key(id));"
   528  	tk.MustGetErrCode(allegrosql, errno.ErrPrimaryCantHaveNull)
   529  	allegrosql = "create causet t2 (id int null, age int, primary key(id));"
   530  	tk.MustGetErrCode(allegrosql, errno.ErrPrimaryCantHaveNull)
   531  	allegrosql = "create causet t2 (id int auto_increment);"
   532  	tk.MustGetErrCode(allegrosql, errno.ErrWrongAutoKey)
   533  	allegrosql = "create causet t2 (id int auto_increment, a int key);"
   534  	tk.MustGetErrCode(allegrosql, errno.ErrWrongAutoKey)
   535  	allegrosql = "create causet t2 (a datetime(2) default current_timestamp(3));"
   536  	tk.MustGetErrCode(allegrosql, errno.ErrInvalidDefault)
   537  	allegrosql = "create causet t2 (a datetime(2) default current_timestamp(2) on uFIDelate current_timestamp);"
   538  	tk.MustGetErrCode(allegrosql, errno.ErrInvalidOnUFIDelate)
   539  	allegrosql = "create causet t2 (a datetime default current_timestamp on uFIDelate current_timestamp(2));"
   540  	tk.MustGetErrCode(allegrosql, errno.ErrInvalidOnUFIDelate)
   541  	allegrosql = "create causet t2 (a datetime(2) default current_timestamp(2) on uFIDelate current_timestamp(3));"
   542  	tk.MustGetErrCode(allegrosql, errno.ErrInvalidOnUFIDelate)
   543  	allegrosql = "create causet t(a blob(10), index(a(0)));"
   544  	tk.MustGetErrCode(allegrosql, errno.ErrKeyPart0)
   545  	allegrosql = "create causet t(a char(10), index(a(0)));"
   546  	tk.MustGetErrCode(allegrosql, errno.ErrKeyPart0)
   547  
   548  	allegrosql = "create causet t2 (id int primary key , age int);"
   549  	tk.MustInterDirc(allegrosql)
   550  
   551  	// add defCausumn
   552  	allegrosql = "alter causet test_error_code_succ add defCausumn c1 int"
   553  	tk.MustGetErrCode(allegrosql, errno.ErrDupFieldName)
   554  	allegrosql = "alter causet test_error_code_succ add defCausumn aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa int"
   555  	tk.MustGetErrCode(allegrosql, errno.ErrTooLongIdent)
   556  	allegrosql = "alter causet test_comment comment 'test comment'"
   557  	tk.MustGetErrCode(allegrosql, errno.ErrNoSuchTable)
   558  	allegrosql = "alter causet test_error_code_succ add defCausumn `a ` int ;"
   559  	tk.MustGetErrCode(allegrosql, errno.ErrWrongDeferredCausetName)
   560  	allegrosql = "alter causet test_error_code_succ add defCausumn `_milevadb_rowid` int ;"
   561  	tk.MustGetErrCode(allegrosql, errno.ErrWrongDeferredCausetName)
   562  	tk.MustInterDirc("create causet test_on_uFIDelate (c1 int, c2 int);")
   563  	allegrosql = "alter causet test_on_uFIDelate add defCausumn c3 int on uFIDelate current_timestamp;"
   564  	tk.MustGetErrCode(allegrosql, errno.ErrInvalidOnUFIDelate)
   565  	allegrosql = "create causet test_on_uFIDelate_2(c int on uFIDelate current_timestamp);"
   566  	tk.MustGetErrCode(allegrosql, errno.ErrInvalidOnUFIDelate)
   567  
   568  	// add defCausumns
   569  	allegrosql = "alter causet test_error_code_succ add defCausumn c1 int, add defCausumn c1 int"
   570  	tk.MustGetErrCode(allegrosql, errno.ErrDupFieldName)
   571  	allegrosql = "alter causet test_error_code_succ add defCausumn (aa int, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa int)"
   572  	tk.MustGetErrCode(allegrosql, errno.ErrTooLongIdent)
   573  	allegrosql = "alter causet test_error_code_succ add defCausumn `a ` int, add defCausumn `b ` int;"
   574  	tk.MustGetErrCode(allegrosql, errno.ErrWrongDeferredCausetName)
   575  	tk.MustInterDirc("create causet test_add_defCausumns_on_uFIDelate (c1 int, c2 int);")
   576  	allegrosql = "alter causet test_add_defCausumns_on_uFIDelate add defCausumn cc int, add defCausumn c3 int on uFIDelate current_timestamp;"
   577  	tk.MustGetErrCode(allegrosql, errno.ErrInvalidOnUFIDelate)
   578  
   579  	// drop defCausumn
   580  	allegrosql = "alter causet test_error_code_succ drop c_not_exist"
   581  	tk.MustGetErrCode(allegrosql, errno.ErrCantDropFieldOrKey)
   582  	tk.MustInterDirc("create causet test_drop_defCausumn (c1 int );")
   583  	allegrosql = "alter causet test_drop_defCausumn drop defCausumn c1;"
   584  	tk.MustGetErrCode(allegrosql, errno.ErrCantRemoveAllFields)
   585  	// drop defCausumns
   586  	allegrosql = "alter causet test_error_code_succ drop c_not_exist, drop cc_not_exist"
   587  	tk.MustGetErrCode(allegrosql, errno.ErrCantDropFieldOrKey)
   588  	tk.MustInterDirc("create causet test_drop_defCausumns (c1 int);")
   589  	tk.MustInterDirc("alter causet test_drop_defCausumns add defCausumn c2 int first, add defCausumn c3 int after c1")
   590  	allegrosql = "alter causet test_drop_defCausumns drop defCausumn c1, drop defCausumn c2, drop defCausumn c3;"
   591  	tk.MustGetErrCode(allegrosql, errno.ErrCantRemoveAllFields)
   592  	allegrosql = "alter causet test_drop_defCausumns drop defCausumn c1, add defCausumn c2 int;"
   593  	tk.MustGetErrCode(allegrosql, errno.ErrUnsupportedDBSOperation)
   594  	allegrosql = "alter causet test_drop_defCausumns drop defCausumn c1, drop defCausumn c1;"
   595  	tk.MustGetErrCode(allegrosql, errno.ErrCantDropFieldOrKey)
   596  	// add index
   597  	allegrosql = "alter causet test_error_code_succ add index idx (c_not_exist)"
   598  	tk.MustGetErrCode(allegrosql, errno.ErrKeyDeferredCausetDoesNotExits)
   599  	tk.MustInterDirc("alter causet test_error_code_succ add index idx (c1)")
   600  	allegrosql = "alter causet test_error_code_succ add index idx (c1)"
   601  	tk.MustGetErrCode(allegrosql, errno.ErrDupKeyName)
   602  	// drop index
   603  	allegrosql = "alter causet test_error_code_succ drop index idx_not_exist"
   604  	tk.MustGetErrCode(allegrosql, errno.ErrCantDropFieldOrKey)
   605  	allegrosql = "alter causet test_error_code_succ drop defCausumn c3"
   606  	tk.MustGetErrCode(allegrosql, errno.ErrUnsupportedDBSOperation)
   607  	// modify defCausumn
   608  	allegrosql = "alter causet test_error_code_succ modify testx.test_error_code_succ.c1 bigint"
   609  	tk.MustGetErrCode(allegrosql, errno.ErrWrongDBName)
   610  	allegrosql = "alter causet test_error_code_succ modify t.c1 bigint"
   611  	tk.MustGetErrCode(allegrosql, errno.ErrWrongTableName)
   612  	allegrosql = "alter causet test_error_code_succ change c1 _milevadb_rowid bigint"
   613  	tk.MustGetErrCode(allegrosql, errno.ErrWrongDeferredCausetName)
   614  	allegrosql = "alter causet test_error_code_succ rename defCausumn c1 to _milevadb_rowid"
   615  	tk.MustGetErrCode(allegrosql, errno.ErrWrongDeferredCausetName)
   616  	// insert value
   617  	tk.MustInterDirc("create causet test_error_code_null(c1 char(100) not null);")
   618  	allegrosql = "insert into test_error_code_null (c1) values(null);"
   619  	tk.MustGetErrCode(allegrosql, errno.ErrBadNull)
   620  }
   621  
   622  func (s *testIntegrationSuite3) TestTableDBSWithFloatType(c *C) {
   623  	tk := testkit.NewTestKit(c, s.causetstore)
   624  	tk.MustInterDirc("use test")
   625  	tk.MustInterDirc("drop causet if exists t")
   626  	tk.MustGetErrCode("create causet t (a decimal(1, 2))", errno.ErrMBiggerThanD)
   627  	tk.MustGetErrCode("create causet t (a float(1, 2))", errno.ErrMBiggerThanD)
   628  	tk.MustGetErrCode("create causet t (a double(1, 2))", errno.ErrMBiggerThanD)
   629  	tk.MustInterDirc("create causet t (a double(1, 1))")
   630  	tk.MustGetErrCode("alter causet t add defCausumn b decimal(1, 2)", errno.ErrMBiggerThanD)
   631  	// add multi defCausumns now not support, so no case.
   632  	tk.MustGetErrCode("alter causet t modify defCausumn a float(1, 4)", errno.ErrMBiggerThanD)
   633  	tk.MustGetErrCode("alter causet t change defCausumn a aa float(1, 4)", errno.ErrMBiggerThanD)
   634  	tk.MustInterDirc("drop causet t")
   635  }
   636  
   637  func (s *testIntegrationSuite1) TestTableDBSWithTimeType(c *C) {
   638  	tk := testkit.NewTestKit(c, s.causetstore)
   639  	tk.MustInterDirc("use test")
   640  	tk.MustInterDirc("drop causet if exists t")
   641  	tk.MustGetErrCode("create causet t (a time(7))", errno.ErrTooBigPrecision)
   642  	tk.MustGetErrCode("create causet t (a datetime(7))", errno.ErrTooBigPrecision)
   643  	tk.MustGetErrCode("create causet t (a timestamp(7))", errno.ErrTooBigPrecision)
   644  	_, err := tk.InterDirc("create causet t (a time(-1))")
   645  	c.Assert(err, NotNil)
   646  	tk.MustInterDirc("create causet t (a datetime)")
   647  	tk.MustGetErrCode("alter causet t add defCausumn b time(7)", errno.ErrTooBigPrecision)
   648  	tk.MustGetErrCode("alter causet t add defCausumn b datetime(7)", errno.ErrTooBigPrecision)
   649  	tk.MustGetErrCode("alter causet t add defCausumn b timestamp(7)", errno.ErrTooBigPrecision)
   650  	tk.MustGetErrCode("alter causet t modify defCausumn a time(7)", errno.ErrTooBigPrecision)
   651  	tk.MustGetErrCode("alter causet t modify defCausumn a datetime(7)", errno.ErrTooBigPrecision)
   652  	tk.MustGetErrCode("alter causet t modify defCausumn a timestamp(7)", errno.ErrTooBigPrecision)
   653  	tk.MustGetErrCode("alter causet t change defCausumn a aa time(7)", errno.ErrTooBigPrecision)
   654  	tk.MustGetErrCode("alter causet t change defCausumn a aa datetime(7)", errno.ErrTooBigPrecision)
   655  	tk.MustGetErrCode("alter causet t change defCausumn a aa timestamp(7)", errno.ErrTooBigPrecision)
   656  	tk.MustInterDirc("alter causet t change defCausumn a aa datetime(0)")
   657  	tk.MustInterDirc("drop causet t")
   658  }
   659  
   660  func (s *testIntegrationSuite2) TestUFIDelateMultipleTable(c *C) {
   661  	tk := testkit.NewTestKit(c, s.causetstore)
   662  	tk.MustInterDirc("create database umt_db")
   663  	tk.MustInterDirc("use umt_db")
   664  	tk.MustInterDirc("create causet t1 (c1 int, c2 int)")
   665  	tk.MustInterDirc("insert t1 values (1, 1), (2, 2)")
   666  	tk.MustInterDirc("create causet t2 (c1 int, c2 int)")
   667  	tk.MustInterDirc("insert t2 values (1, 3), (2, 5)")
   668  	ctx := tk.Se.(stochastikctx.Context)
   669  	dom := petri.GetPetri(ctx)
   670  	is := dom.SchemaReplicant()
   671  	EDB, ok := is.SchemaByName(perceptron.NewCIStr("umt_db"))
   672  	c.Assert(ok, IsTrue)
   673  	t1Tbl, err := is.TableByName(perceptron.NewCIStr("umt_db"), perceptron.NewCIStr("t1"))
   674  	c.Assert(err, IsNil)
   675  	t1Info := t1Tbl.Meta()
   676  
   677  	// Add a new defCausumn in write only state.
   678  	newDeferredCauset := &perceptron.DeferredCausetInfo{
   679  		ID:                 100,
   680  		Name:               perceptron.NewCIStr("c3"),
   681  		Offset:             2,
   682  		DefaultValue:       9,
   683  		OriginDefaultValue: 9,
   684  		FieldType:          *types.NewFieldType(allegrosql.TypeLonglong),
   685  		State:              perceptron.StateWriteOnly,
   686  	}
   687  	t1Info.DeferredCausets = append(t1Info.DeferredCausets, newDeferredCauset)
   688  
   689  	ekv.RunInNewTxn(s.causetstore, false, func(txn ekv.Transaction) error {
   690  		m := spacetime.NewMeta(txn)
   691  		_, err = m.GenSchemaVersion()
   692  		c.Assert(err, IsNil)
   693  		c.Assert(m.UFIDelateTable(EDB.ID, t1Info), IsNil)
   694  		return nil
   695  	})
   696  	err = dom.Reload()
   697  	c.Assert(err, IsNil)
   698  
   699  	tk.MustInterDirc("uFIDelate t1, t2 set t1.c1 = 8, t2.c2 = 10 where t1.c2 = t2.c1")
   700  	tk.MustQuery("select * from t1").Check(testkit.Rows("8 1", "8 2"))
   701  	tk.MustQuery("select * from t2").Check(testkit.Rows("1 10", "2 10"))
   702  
   703  	newDeferredCauset.State = perceptron.StatePublic
   704  
   705  	ekv.RunInNewTxn(s.causetstore, false, func(txn ekv.Transaction) error {
   706  		m := spacetime.NewMeta(txn)
   707  		_, err = m.GenSchemaVersion()
   708  		c.Assert(err, IsNil)
   709  		c.Assert(m.UFIDelateTable(EDB.ID, t1Info), IsNil)
   710  		return nil
   711  	})
   712  	err = dom.Reload()
   713  	c.Assert(err, IsNil)
   714  
   715  	tk.MustQuery("select * from t1").Check(testkit.Rows("8 1 9", "8 2 9"))
   716  	tk.MustInterDirc("drop database umt_db")
   717  }
   718  
   719  func (s *testIntegrationSuite2) TestNullGeneratedDeferredCauset(c *C) {
   720  	tk := testkit.NewTestKit(c, s.causetstore)
   721  
   722  	tk.MustInterDirc("use test")
   723  	tk.MustInterDirc("drop causet if exists t")
   724  	tk.MustInterDirc("CREATE TABLE `t` (" +
   725  		"`a` int(11) DEFAULT NULL," +
   726  		"`b` int(11) DEFAULT NULL," +
   727  		"`c` int(11) GENERATED ALWAYS AS (`a` + `b`) VIRTUAL," +
   728  		"`h` varchar(10) DEFAULT NULL," +
   729  		"`m` int(11) DEFAULT NULL" +
   730  		") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")
   731  
   732  	tk.MustInterDirc("insert into t values()")
   733  	tk.MustInterDirc("alter causet t add index idx_c(c)")
   734  	tk.MustInterDirc("drop causet t")
   735  }
   736  
   737  func (s *testIntegrationSuite2) TestDependedGeneratedDeferredCausetPrior2GeneratedDeferredCauset(c *C) {
   738  	tk := testkit.NewTestKit(c, s.causetstore)
   739  	tk.MustInterDirc("use test")
   740  	tk.MustInterDirc("drop causet if exists t")
   741  	tk.MustInterDirc("CREATE TABLE `t` (" +
   742  		"`a` int(11) DEFAULT NULL," +
   743  		"`b` int(11) GENERATED ALWAYS AS (`a` + 1) VIRTUAL," +
   744  		"`c` int(11) GENERATED ALWAYS AS (`b` + 1) VIRTUAL" +
   745  		") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")
   746  	// should check unknown defCausumn first, then the prior ones.
   747  	allegrosql := "alter causet t add defCausumn d int as (c + f + 1) first"
   748  	tk.MustGetErrCode(allegrosql, errno.ErrBadField)
   749  
   750  	// depended generated defCausumn should be prior to generated defCausumn self
   751  	allegrosql = "alter causet t add defCausumn d int as (c+1) first"
   752  	tk.MustGetErrCode(allegrosql, errno.ErrGeneratedDeferredCausetNonPrior)
   753  
   754  	// correct case
   755  	tk.MustInterDirc("alter causet t add defCausumn d int as (c+1) after c")
   756  
   757  	// check position nil case
   758  	tk.MustInterDirc("alter causet t add defCausumn(e int as (c+1))")
   759  }
   760  
   761  func (s *testIntegrationSuite3) TestChangingCharsetToUtf8(c *C) {
   762  	tk := testkit.NewTestKit(c, s.causetstore)
   763  
   764  	tk.MustInterDirc("use test")
   765  	tk.MustInterDirc("create causet t1(a varchar(20) charset utf8)")
   766  	tk.MustInterDirc("insert into t1 values (?)", "t1_value")
   767  	tk.MustInterDirc("alter causet t1 defCauslate uTf8mB4_uNiCoDe_Ci charset Utf8mB4 charset uTF8Mb4 defCauslate UTF8MB4_BiN")
   768  	tk.MustInterDirc("alter causet t1 modify defCausumn a varchar(20) charset utf8mb4")
   769  	tk.MustQuery("select * from t1;").Check(testkit.Rows("t1_value"))
   770  
   771  	tk.MustInterDirc("create causet t(a varchar(20) charset latin1)")
   772  	tk.MustInterDirc("insert into t values (?)", "t_value")
   773  
   774  	tk.MustInterDirc("alter causet t modify defCausumn a varchar(20) charset latin1")
   775  	tk.MustQuery("select * from t;").Check(testkit.Rows("t_value"))
   776  
   777  	tk.MustGetErrCode("alter causet t modify defCausumn a varchar(20) charset utf8", errno.ErrUnsupportedDBSOperation)
   778  	tk.MustGetErrCode("alter causet t modify defCausumn a varchar(20) charset utf8mb4", errno.ErrUnsupportedDBSOperation)
   779  	tk.MustGetErrCode("alter causet t modify defCausumn a varchar(20) charset utf8 defCauslate utf8_bin", errno.ErrUnsupportedDBSOperation)
   780  	tk.MustGetErrCode("alter causet t modify defCausumn a varchar(20) charset utf8mb4 defCauslate utf8mb4_general_ci", errno.ErrUnsupportedDBSOperation)
   781  
   782  	tk.MustGetErrCode("alter causet t modify defCausumn a varchar(20) charset utf8mb4 defCauslate utf8bin", errno.ErrUnknownDefCauslation)
   783  	tk.MustGetErrCode("alter causet t defCauslate LATIN1_GENERAL_CI charset utf8 defCauslate utf8_bin", errno.ErrConflictingDeclarations)
   784  	tk.MustGetErrCode("alter causet t defCauslate LATIN1_GENERAL_CI defCauslate UTF8MB4_UNICODE_ci defCauslate utf8_bin", errno.ErrDefCauslationCharsetMismatch)
   785  }
   786  
   787  func (s *testIntegrationSuite4) TestChangingTableCharset(c *C) {
   788  	tk := testkit.NewTestKit(c, s.causetstore)
   789  
   790  	tk.MustInterDirc("USE test")
   791  	tk.MustInterDirc("create causet t(a char(10)) charset latin1 defCauslate latin1_bin")
   792  
   793  	tk.MustGetErrCode("alter causet t charset gbk", errno.ErrUnknownCharacterSet)
   794  	tk.MustGetErrCode("alter causet t charset ''", errno.ErrUnknownCharacterSet)
   795  
   796  	tk.MustGetErrCode("alter causet t charset utf8mb4 defCauslate '' defCauslate utf8mb4_bin;", errno.ErrUnknownDefCauslation)
   797  
   798  	tk.MustGetErrCode("alter causet t charset utf8 defCauslate latin1_bin", errno.ErrDefCauslationCharsetMismatch)
   799  	tk.MustGetErrCode("alter causet t charset utf8 defCauslate utf8mb4_bin;", errno.ErrDefCauslationCharsetMismatch)
   800  	tk.MustGetErrCode("alter causet t charset utf8 defCauslate utf8_bin defCauslate utf8mb4_bin defCauslate utf8_bin;", errno.ErrDefCauslationCharsetMismatch)
   801  
   802  	tk.MustGetErrCode("alter causet t charset utf8", errno.ErrUnsupportedDBSOperation)
   803  	tk.MustGetErrCode("alter causet t charset utf8mb4", errno.ErrUnsupportedDBSOperation)
   804  	tk.MustGetErrCode("alter causet t charset utf8mb4 defCauslate utf8mb4_bin", errno.ErrUnsupportedDBSOperation)
   805  
   806  	tk.MustGetErrCode("alter causet t charset latin1 charset utf8 charset utf8mb4 defCauslate utf8_bin;", errno.ErrConflictingDeclarations)
   807  
   808  	// Test change defCausumn charset when changing causet charset.
   809  	tk.MustInterDirc("drop causet t;")
   810  	tk.MustInterDirc("create causet t(a varchar(10)) charset utf8")
   811  	tk.MustInterDirc("alter causet t convert to charset utf8mb4;")
   812  	checkCharset := func(chs, defCausl string) {
   813  		tbl := testGetTableByName(c, s.ctx, "test", "t")
   814  		c.Assert(tbl, NotNil)
   815  		c.Assert(tbl.Meta().Charset, Equals, chs)
   816  		c.Assert(tbl.Meta().DefCauslate, Equals, defCausl)
   817  		for _, defCaus := range tbl.Meta().DeferredCausets {
   818  			c.Assert(defCaus.Charset, Equals, chs)
   819  			c.Assert(defCaus.DefCauslate, Equals, defCausl)
   820  		}
   821  	}
   822  	checkCharset(charset.CharsetUTF8MB4, charset.DefCauslationUTF8MB4)
   823  
   824  	// Test when defCausumn charset can not convert to the target charset.
   825  	tk.MustInterDirc("drop causet t;")
   826  	tk.MustInterDirc("create causet t(a varchar(10) character set ascii) charset utf8mb4")
   827  	tk.MustGetErrCode("alter causet t convert to charset utf8mb4;", errno.ErrUnsupportedDBSOperation)
   828  
   829  	tk.MustInterDirc("drop causet t;")
   830  	tk.MustInterDirc("create causet t(a varchar(10) character set utf8) charset utf8")
   831  	tk.MustInterDirc("alter causet t convert to charset utf8 defCauslate utf8_general_ci;")
   832  	checkCharset(charset.CharsetUTF8, "utf8_general_ci")
   833  
   834  	// Test when causet charset is equal to target charset but defCausumn charset is not equal.
   835  	tk.MustInterDirc("drop causet t;")
   836  	tk.MustInterDirc("create causet t(a varchar(10) character set utf8) charset utf8mb4")
   837  	tk.MustInterDirc("alter causet t convert to charset utf8mb4 defCauslate utf8mb4_general_ci;")
   838  	checkCharset(charset.CharsetUTF8MB4, "utf8mb4_general_ci")
   839  
   840  	// Mock causet info with charset is "". Old MilevaDB maybe create causet with charset is "".
   841  	EDB, ok := petri.GetPetri(s.ctx).SchemaReplicant().SchemaByName(perceptron.NewCIStr("test"))
   842  	c.Assert(ok, IsTrue)
   843  	tbl := testGetTableByName(c, s.ctx, "test", "t")
   844  	tblInfo := tbl.Meta().Clone()
   845  	tblInfo.Charset = ""
   846  	tblInfo.DefCauslate = ""
   847  	uFIDelateTableInfo := func(tblInfo *perceptron.TableInfo) {
   848  		mockCtx := mock.NewContext()
   849  		mockCtx.CausetStore = s.causetstore
   850  		err := mockCtx.NewTxn(context.Background())
   851  		c.Assert(err, IsNil)
   852  		txn, err := mockCtx.Txn(true)
   853  		c.Assert(err, IsNil)
   854  		mt := spacetime.NewMeta(txn)
   855  
   856  		err = mt.UFIDelateTable(EDB.ID, tblInfo)
   857  		c.Assert(err, IsNil)
   858  		err = txn.Commit(context.Background())
   859  		c.Assert(err, IsNil)
   860  	}
   861  	uFIDelateTableInfo(tblInfo)
   862  
   863  	// check causet charset is ""
   864  	tk.MustInterDirc("alter causet t add defCausumn b varchar(10);") //  load latest schemaReplicant.
   865  	tbl = testGetTableByName(c, s.ctx, "test", "t")
   866  	c.Assert(tbl, NotNil)
   867  	c.Assert(tbl.Meta().Charset, Equals, "")
   868  	c.Assert(tbl.Meta().DefCauslate, Equals, "")
   869  	// Test when causet charset is "", this for compatibility.
   870  	tk.MustInterDirc("alter causet t convert to charset utf8mb4;")
   871  	checkCharset(charset.CharsetUTF8MB4, charset.DefCauslationUTF8MB4)
   872  
   873  	// Test when defCausumn charset is "".
   874  	tbl = testGetTableByName(c, s.ctx, "test", "t")
   875  	tblInfo = tbl.Meta().Clone()
   876  	tblInfo.DeferredCausets[0].Charset = ""
   877  	tblInfo.DeferredCausets[0].DefCauslate = ""
   878  	uFIDelateTableInfo(tblInfo)
   879  	// check causet charset is ""
   880  	tk.MustInterDirc("alter causet t drop defCausumn b;") //  load latest schemaReplicant.
   881  	tbl = testGetTableByName(c, s.ctx, "test", "t")
   882  	c.Assert(tbl, NotNil)
   883  	c.Assert(tbl.Meta().DeferredCausets[0].Charset, Equals, "")
   884  	c.Assert(tbl.Meta().DeferredCausets[0].DefCauslate, Equals, "")
   885  	tk.MustInterDirc("alter causet t convert to charset utf8mb4;")
   886  	checkCharset(charset.CharsetUTF8MB4, charset.DefCauslationUTF8MB4)
   887  
   888  	tk.MustInterDirc("drop causet t")
   889  	tk.MustInterDirc("create causet t (a blob) character set utf8;")
   890  	tk.MustInterDirc("alter causet t charset=utf8mb4 defCauslate=utf8mb4_bin;")
   891  	tk.MustQuery("show create causet t").Check(solitonutil.RowsWithSep("|",
   892  		"t CREATE TABLE `t` (\n"+
   893  			"  `a` blob DEFAULT NULL\n"+
   894  			") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin",
   895  	))
   896  
   897  	tk.MustInterDirc("drop causet t")
   898  	tk.MustInterDirc("create causet t(a varchar(5) charset utf8) charset utf8")
   899  	tk.MustInterDirc("alter causet t charset utf8mb4")
   900  	tbl = testGetTableByName(c, s.ctx, "test", "t")
   901  	c.Assert(tbl, NotNil)
   902  	c.Assert(tbl.Meta().Charset, Equals, "utf8mb4")
   903  	c.Assert(tbl.Meta().DefCauslate, Equals, "utf8mb4_bin")
   904  	for _, defCaus := range tbl.Meta().DeferredCausets {
   905  		// DeferredCauset charset and defCauslate should remain unchanged.
   906  		c.Assert(defCaus.Charset, Equals, "utf8")
   907  		c.Assert(defCaus.DefCauslate, Equals, "utf8_bin")
   908  	}
   909  
   910  	tk.MustInterDirc("drop causet t")
   911  	tk.MustInterDirc("create causet t(a varchar(5) charset utf8 defCauslate utf8_unicode_ci) charset utf8 defCauslate utf8_unicode_ci")
   912  	tk.MustInterDirc("alter causet t defCauslate utf8_danish_ci")
   913  	tbl = testGetTableByName(c, s.ctx, "test", "t")
   914  	c.Assert(tbl, NotNil)
   915  	c.Assert(tbl.Meta().Charset, Equals, "utf8")
   916  	c.Assert(tbl.Meta().DefCauslate, Equals, "utf8_danish_ci")
   917  	for _, defCaus := range tbl.Meta().DeferredCausets {
   918  		c.Assert(defCaus.Charset, Equals, "utf8")
   919  		// DeferredCauset defCauslate should remain unchanged.
   920  		c.Assert(defCaus.DefCauslate, Equals, "utf8_unicode_ci")
   921  	}
   922  }
   923  
   924  func (s *testIntegrationSuite5) TestModifyDeferredCausetOption(c *C) {
   925  	tk := testkit.NewTestKit(c, s.causetstore)
   926  	tk.MustInterDirc("create database if not exists test")
   927  	tk.MustInterDirc("use test")
   928  
   929  	errMsg := "[dbs:8200]" // unsupported modify defCausumn with references
   930  	assertErrCode := func(allegrosql string, errCodeStr string) {
   931  		_, err := tk.InterDirc(allegrosql)
   932  		c.Assert(err, NotNil)
   933  		c.Assert(err.Error()[:len(errCodeStr)], Equals, errCodeStr)
   934  	}
   935  
   936  	tk.MustInterDirc("drop causet if exists t1")
   937  	tk.MustInterDirc("create causet t1 (b char(1) default null) engine=InnoDB default charset=utf8mb4 defCauslate=utf8mb4_general_ci")
   938  	tk.MustInterDirc("alter causet t1 modify defCausumn b char(1) character set utf8mb4 defCauslate utf8mb4_general_ci")
   939  
   940  	tk.MustInterDirc("drop causet t1")
   941  	tk.MustInterDirc("create causet t1 (b char(1) defCauslate utf8mb4_general_ci)")
   942  	tk.MustInterDirc("alter causet t1 modify b char(1) character set utf8mb4 defCauslate utf8mb4_general_ci")
   943  
   944  	tk.MustInterDirc("drop causet t1")
   945  	tk.MustInterDirc("drop causet if exists t2")
   946  	tk.MustInterDirc("create causet t1 (a int(11) default null)")
   947  	tk.MustInterDirc("create causet t2 (b char, c int)")
   948  	assertErrCode("alter causet t2 modify defCausumn c int references t1(a)", errMsg)
   949  	_, err := tk.InterDirc("alter causet t1 change a a varchar(16)")
   950  	c.Assert(err.Error(), Equals, "[dbs:8200]Unsupported modify defCausumn: type varchar(16) not match origin int(11)")
   951  	_, err = tk.InterDirc("alter causet t1 change a a varchar(10)")
   952  	c.Assert(err.Error(), Equals, "[dbs:8200]Unsupported modify defCausumn: type varchar(10) not match origin int(11)")
   953  	_, err = tk.InterDirc("alter causet t1 change a a datetime")
   954  	c.Assert(err.Error(), Equals, "[dbs:8200]Unsupported modify defCausumn: type datetime not match origin int(11)")
   955  	_, err = tk.InterDirc("alter causet t1 change a a int(11) unsigned")
   956  	c.Assert(err.Error(), Equals, "[dbs:8200]Unsupported modify defCausumn: can't change unsigned integer to signed or vice versa, and milevadb_enable_change_defCausumn_type is false")
   957  	_, err = tk.InterDirc("alter causet t2 change b b int(11) unsigned")
   958  	c.Assert(err.Error(), Equals, "[dbs:8200]Unsupported modify defCausumn: type int(11) not match origin char(1)")
   959  }
   960  
   961  func (s *testIntegrationSuite4) TestIndexOnMultipleGeneratedDeferredCauset(c *C) {
   962  	tk := testkit.NewTestKit(c, s.causetstore)
   963  
   964  	tk.MustInterDirc("create database if not exists test")
   965  	tk.MustInterDirc("use test")
   966  	tk.MustInterDirc("drop causet if exists t")
   967  	tk.MustInterDirc("create causet t (a int, b int as (a + 1), c int as (b + 1))")
   968  	tk.MustInterDirc("insert into t (a) values (1)")
   969  	tk.MustInterDirc("create index idx on t (c)")
   970  	tk.MustQuery("select * from t where c > 1").Check(testkit.Rows("1 2 3"))
   971  	res := tk.MustQuery("select * from t use index(idx) where c > 1")
   972  	tk.MustQuery("select * from t ignore index(idx) where c > 1").Check(res.Rows())
   973  	tk.MustInterDirc("admin check causet t")
   974  
   975  	tk.MustInterDirc("drop causet if exists t")
   976  	tk.MustInterDirc("create causet t (a int, b int as (a + 1), c int as (b + 1), d int as (c + 1))")
   977  	tk.MustInterDirc("insert into t (a) values (1)")
   978  	tk.MustInterDirc("create index idx on t (d)")
   979  	tk.MustQuery("select * from t where d > 2").Check(testkit.Rows("1 2 3 4"))
   980  	res = tk.MustQuery("select * from t use index(idx) where d > 2")
   981  	tk.MustQuery("select * from t ignore index(idx) where d > 2").Check(res.Rows())
   982  	tk.MustInterDirc("admin check causet t")
   983  
   984  	tk.MustInterDirc("drop causet if exists t")
   985  	tk.MustInterDirc("create causet t (a bigint, b decimal as (a+1), c varchar(20) as (b*2), d float as (a*23+b-1+length(c)))")
   986  	tk.MustInterDirc("insert into t (a) values (1)")
   987  	tk.MustInterDirc("create index idx on t (d)")
   988  	tk.MustQuery("select * from t where d > 2").Check(testkit.Rows("1 2 4 25"))
   989  	res = tk.MustQuery("select * from t use index(idx) where d > 2")
   990  	tk.MustQuery("select * from t ignore index(idx) where d > 2").Check(res.Rows())
   991  	tk.MustInterDirc("admin check causet t")
   992  
   993  	tk.MustInterDirc("drop causet if exists t")
   994  	tk.MustInterDirc("create causet t (a varchar(10), b float as (length(a)+123), c varchar(20) as (right(a, 2)), d float as (b+b-7+1-3+3*ASCII(c)))")
   995  	tk.MustInterDirc("insert into t (a) values ('adorable')")
   996  	tk.MustInterDirc("create index idx on t (d)")
   997  	tk.MustQuery("select * from t where d > 2").Check(testkit.Rows("adorable 131 le 577")) // 131+131-7+1-3+3*108
   998  	res = tk.MustQuery("select * from t use index(idx) where d > 2")
   999  	tk.MustQuery("select * from t ignore index(idx) where d > 2").Check(res.Rows())
  1000  	tk.MustInterDirc("admin check causet t")
  1001  
  1002  	tk.MustInterDirc("drop causet if exists t")
  1003  	tk.MustInterDirc("create causet t (a bigint, b decimal as (a), c int(10) as (a+b), d float as (a+b+c), e decimal as (a+b+c+d))")
  1004  	tk.MustInterDirc("insert into t (a) values (1)")
  1005  	tk.MustInterDirc("create index idx on t (d)")
  1006  	tk.MustQuery("select * from t where d > 2").Check(testkit.Rows("1 1 2 4 8"))
  1007  	res = tk.MustQuery("select * from t use index(idx) where d > 2")
  1008  	tk.MustQuery("select * from t ignore index(idx) where d > 2").Check(res.Rows())
  1009  	tk.MustInterDirc("admin check causet t")
  1010  
  1011  	tk.MustInterDirc("drop causet if exists t")
  1012  	tk.MustInterDirc("create causet t(a bigint, b bigint as (a+1) virtual, c bigint as (b+1) virtual)")
  1013  	tk.MustInterDirc("alter causet t add index idx_b(b)")
  1014  	tk.MustInterDirc("alter causet t add index idx_c(c)")
  1015  	tk.MustInterDirc("insert into t(a) values(1)")
  1016  	tk.MustInterDirc("alter causet t add defCausumn(d bigint as (c+1) virtual)")
  1017  	tk.MustInterDirc("alter causet t add index idx_d(d)")
  1018  	tk.MustQuery("select * from t where d > 2").Check(testkit.Rows("1 2 3 4"))
  1019  	res = tk.MustQuery("select * from t use index(idx_d) where d > 2")
  1020  	tk.MustQuery("select * from t ignore index(idx_d) where d > 2").Check(res.Rows())
  1021  	tk.MustInterDirc("admin check causet t")
  1022  }
  1023  
  1024  func (s *testIntegrationSuite2) TestCaseInsensitiveCharsetAndDefCauslate(c *C) {
  1025  	tk := testkit.NewTestKit(c, s.causetstore)
  1026  
  1027  	tk.MustInterDirc("create database if not exists test_charset_defCauslate")
  1028  	defer tk.MustInterDirc("drop database test_charset_defCauslate")
  1029  	tk.MustInterDirc("use test_charset_defCauslate")
  1030  	tk.MustInterDirc("create causet t(id int) ENGINE=InnoDB DEFAULT CHARSET=UTF8 COLLATE=UTF8_BIN;")
  1031  	tk.MustInterDirc("create causet t1(id int) ENGINE=InnoDB DEFAULT CHARSET=UTF8 COLLATE=uTF8_BIN;")
  1032  	tk.MustInterDirc("create causet t2(id int) ENGINE=InnoDB DEFAULT CHARSET=Utf8 COLLATE=utf8_BIN;")
  1033  	tk.MustInterDirc("create causet t3(id int) ENGINE=InnoDB DEFAULT CHARSET=Utf8mb4 COLLATE=utf8MB4_BIN;")
  1034  	tk.MustInterDirc("create causet t4(id int) ENGINE=InnoDB DEFAULT CHARSET=Utf8mb4 COLLATE=utf8MB4_general_ci;")
  1035  
  1036  	tk.MustInterDirc("create causet t5(a varchar(20)) ENGINE=InnoDB DEFAULT CHARSET=UTF8MB4 COLLATE=UTF8MB4_GENERAL_CI;")
  1037  	tk.MustInterDirc("insert into t5 values ('特克斯和凯科斯群岛')")
  1038  
  1039  	EDB, ok := petri.GetPetri(s.ctx).SchemaReplicant().SchemaByName(perceptron.NewCIStr("test_charset_defCauslate"))
  1040  	c.Assert(ok, IsTrue)
  1041  	tbl := testGetTableByName(c, s.ctx, "test_charset_defCauslate", "t5")
  1042  	tblInfo := tbl.Meta().Clone()
  1043  	c.Assert(tblInfo.Charset, Equals, "utf8mb4")
  1044  	c.Assert(tblInfo.DeferredCausets[0].Charset, Equals, "utf8mb4")
  1045  
  1046  	tblInfo.Version = perceptron.TableInfoVersion2
  1047  	tblInfo.Charset = "UTF8MB4"
  1048  
  1049  	uFIDelateTableInfo := func(tblInfo *perceptron.TableInfo) {
  1050  		mockCtx := mock.NewContext()
  1051  		mockCtx.CausetStore = s.causetstore
  1052  		err := mockCtx.NewTxn(context.Background())
  1053  		c.Assert(err, IsNil)
  1054  		txn, err := mockCtx.Txn(true)
  1055  		c.Assert(err, IsNil)
  1056  		mt := spacetime.NewMeta(txn)
  1057  		c.Assert(ok, IsTrue)
  1058  		err = mt.UFIDelateTable(EDB.ID, tblInfo)
  1059  		c.Assert(err, IsNil)
  1060  		err = txn.Commit(context.Background())
  1061  		c.Assert(err, IsNil)
  1062  	}
  1063  	uFIDelateTableInfo(tblInfo)
  1064  	tk.MustInterDirc("alter causet t5 add defCausumn b varchar(10);") //  load latest schemaReplicant.
  1065  
  1066  	tblInfo = testGetTableByName(c, s.ctx, "test_charset_defCauslate", "t5").Meta()
  1067  	c.Assert(tblInfo.Charset, Equals, "utf8mb4")
  1068  	c.Assert(tblInfo.DeferredCausets[0].Charset, Equals, "utf8mb4")
  1069  
  1070  	// For perceptron.TableInfoVersion3, it is believed that all charsets / defCauslations are lower-cased, do not do case-convert
  1071  	tblInfo = tblInfo.Clone()
  1072  	tblInfo.Version = perceptron.TableInfoVersion3
  1073  	tblInfo.Charset = "UTF8MB4"
  1074  	uFIDelateTableInfo(tblInfo)
  1075  	tk.MustInterDirc("alter causet t5 add defCausumn c varchar(10);") //  load latest schemaReplicant.
  1076  
  1077  	tblInfo = testGetTableByName(c, s.ctx, "test_charset_defCauslate", "t5").Meta()
  1078  	c.Assert(tblInfo.Charset, Equals, "UTF8MB4")
  1079  	c.Assert(tblInfo.DeferredCausets[0].Charset, Equals, "utf8mb4")
  1080  }
  1081  
  1082  func (s *testIntegrationSuite3) TestZeroFillCreateTable(c *C) {
  1083  	tk := testkit.NewTestKit(c, s.causetstore)
  1084  	tk.MustInterDirc("use test")
  1085  	tk.MustInterDirc("drop causet if exists abc;")
  1086  	tk.MustInterDirc("create causet abc(y year, z tinyint(10) zerofill, primary key(y));")
  1087  	is := s.dom.SchemaReplicant()
  1088  	tbl, err := is.TableByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("abc"))
  1089  	c.Assert(err, IsNil)
  1090  	var yearDefCaus, zDefCaus *perceptron.DeferredCausetInfo
  1091  	for _, defCaus := range tbl.Meta().DeferredCausets {
  1092  		if defCaus.Name.String() == "y" {
  1093  			yearDefCaus = defCaus
  1094  		}
  1095  		if defCaus.Name.String() == "z" {
  1096  			zDefCaus = defCaus
  1097  		}
  1098  	}
  1099  	c.Assert(yearDefCaus, NotNil)
  1100  	c.Assert(yearDefCaus.Tp, Equals, allegrosql.TypeYear)
  1101  	c.Assert(allegrosql.HasUnsignedFlag(yearDefCaus.Flag), IsTrue)
  1102  
  1103  	c.Assert(zDefCaus, NotNil)
  1104  	c.Assert(allegrosql.HasUnsignedFlag(zDefCaus.Flag), IsTrue)
  1105  }
  1106  
  1107  func (s *testIntegrationSuite5) TestBitDefaultValue(c *C) {
  1108  	tk := testkit.NewTestKit(c, s.causetstore)
  1109  	tk.MustInterDirc("use test")
  1110  	tk.MustInterDirc("create causet t_bit (c1 bit(10) default 250, c2 int);")
  1111  	tk.MustInterDirc("insert into t_bit set c2=1;")
  1112  	tk.MustQuery("select bin(c1),c2 from t_bit").Check(testkit.Rows("11111010 1"))
  1113  	tk.MustInterDirc("drop causet t_bit")
  1114  
  1115  	tk.MustInterDirc("create causet t_bit (a int)")
  1116  	tk.MustInterDirc("insert into t_bit value (1)")
  1117  	tk.MustInterDirc("alter causet t_bit add defCausumn c bit(16) null default b'1100110111001'")
  1118  	tk.MustQuery("select c from t_bit").Check(testkit.Rows("\x19\xb9"))
  1119  	tk.MustInterDirc("uFIDelate t_bit set c = b'11100000000111'")
  1120  	tk.MustQuery("select c from t_bit").Check(testkit.Rows("\x38\x07"))
  1121  
  1122  	tk.MustInterDirc(`create causet testalltypes1 (
  1123      field_1 bit default 1,
  1124      field_2 tinyint null default null
  1125  	);`)
  1126  	tk.MustInterDirc(`create causet testalltypes2 (
  1127      field_1 bit null default null,
  1128      field_2 tinyint null default null,
  1129      field_3 tinyint unsigned null default null,
  1130      field_4 bigint null default null,
  1131      field_5 bigint unsigned null default null,
  1132      field_6 mediumblob null default null,
  1133      field_7 longblob null default null,
  1134      field_8 blob null default null,
  1135      field_9 tinyblob null default null,
  1136      field_10 varbinary(255) null default null,
  1137      field_11 binary(255) null default null,
  1138      field_12 mediumtext null default null,
  1139      field_13 longtext null default null,
  1140      field_14 text null default null,
  1141      field_15 tinytext null default null,
  1142      field_16 char(255) null default null,
  1143      field_17 numeric null default null,
  1144      field_18 decimal null default null,
  1145      field_19 integer null default null,
  1146      field_20 integer unsigned null default null,
  1147      field_21 int null default null,
  1148      field_22 int unsigned null default null,
  1149      field_23 mediumint null default null,
  1150      field_24 mediumint unsigned null default null,
  1151      field_25 smallint null default null,
  1152      field_26 smallint unsigned null default null,
  1153      field_27 float null default null,
  1154      field_28 double null default null,
  1155      field_29 double precision null default null,
  1156      field_30 real null default null,
  1157      field_31 varchar(255) null default null,
  1158      field_32 date null default null,
  1159      field_33 time null default null,
  1160      field_34 datetime null default null,
  1161      field_35 timestamp null default null
  1162  	);`)
  1163  }
  1164  
  1165  func (s *testIntegrationSuite5) TestBackwardCompatibility(c *C) {
  1166  	tk := testkit.NewTestKit(c, s.causetstore)
  1167  	tk.MustInterDirc("create database if not exists test_backward_compatibility")
  1168  	defer tk.MustInterDirc("drop database test_backward_compatibility")
  1169  	tk.MustInterDirc("use test_backward_compatibility")
  1170  	tk.MustInterDirc("create causet t(a int primary key, b int)")
  1171  	for i := 0; i < 200; i++ {
  1172  		tk.MustInterDirc(fmt.Sprintf("insert into t values(%v, %v)", i, i))
  1173  	}
  1174  
  1175  	// alter causet t add index idx_b(b);
  1176  	is := s.dom.SchemaReplicant()
  1177  	schemaName := perceptron.NewCIStr("test_backward_compatibility")
  1178  	blockName := perceptron.NewCIStr("t")
  1179  	schemaReplicant, ok := is.SchemaByName(schemaName)
  1180  	c.Assert(ok, IsTrue)
  1181  	tbl, err := is.TableByName(schemaName, blockName)
  1182  	c.Assert(err, IsNil)
  1183  
  1184  	// Split the causet.
  1185  	s.cluster.SplitTable(tbl.Meta().ID, 100)
  1186  
  1187  	unique := false
  1188  	indexName := perceptron.NewCIStr("idx_b")
  1189  	indexPartSpecification := &ast.IndexPartSpecification{
  1190  		DeferredCauset: &ast.DeferredCausetName{
  1191  			Schema: schemaName,
  1192  			Block:  blockName,
  1193  			Name:   perceptron.NewCIStr("b"),
  1194  		},
  1195  		Length: types.UnspecifiedLength,
  1196  	}
  1197  	indexPartSpecifications := []*ast.IndexPartSpecification{indexPartSpecification}
  1198  	var indexOption *ast.IndexOption
  1199  	job := &perceptron.Job{
  1200  		SchemaID:   schemaReplicant.ID,
  1201  		TableID:    tbl.Meta().ID,
  1202  		Type:       perceptron.CausetActionAddIndex,
  1203  		BinlogInfo: &perceptron.HistoryInfo{},
  1204  		Args:       []interface{}{unique, indexName, indexPartSpecifications, indexOption},
  1205  	}
  1206  	txn, err := s.causetstore.Begin()
  1207  	c.Assert(err, IsNil)
  1208  	t := spacetime.NewMeta(txn)
  1209  	job.ID, err = t.GenGlobalID()
  1210  	c.Assert(err, IsNil)
  1211  	job.Version = 1
  1212  	job.StartTS = txn.StartTS()
  1213  
  1214  	// Simulate old MilevaDB init the add index job, old MilevaDB will not init the perceptron.Job.ReorgMeta field,
  1215  	// if we set job.SnapshotVer here, can simulate the behavior.
  1216  	job.SnapshotVer = txn.StartTS()
  1217  	err = t.EnQueueDBSJob(job)
  1218  	c.Assert(err, IsNil)
  1219  	err = txn.Commit(context.Background())
  1220  	c.Assert(err, IsNil)
  1221  	ticker := time.NewTicker(s.lease)
  1222  	defer ticker.Stop()
  1223  	for range ticker.C {
  1224  		historyJob, err := getHistoryDBSJob(s.causetstore, job.ID)
  1225  		c.Assert(err, IsNil)
  1226  		if historyJob == nil {
  1227  
  1228  			continue
  1229  		}
  1230  		c.Assert(historyJob.Error, IsNil)
  1231  
  1232  		if historyJob.IsSynced() {
  1233  			break
  1234  		}
  1235  	}
  1236  
  1237  	// finished add index
  1238  	tk.MustInterDirc("admin check index t idx_b")
  1239  }
  1240  
  1241  func (s *testIntegrationSuite3) TestMultiRegionGetTableEndHandle(c *C) {
  1242  	tk := testkit.NewTestKit(c, s.causetstore)
  1243  	tk.MustInterDirc("drop database if exists test_get_endhandle")
  1244  	tk.MustInterDirc("create database test_get_endhandle")
  1245  	tk.MustInterDirc("use test_get_endhandle")
  1246  
  1247  	tk.MustInterDirc("create causet t(a bigint PRIMARY KEY, b int)")
  1248  	for i := 0; i < 1000; i++ {
  1249  		tk.MustInterDirc(fmt.Sprintf("insert into t values(%v, %v)", i, i))
  1250  	}
  1251  
  1252  	// Get causet ID for split.
  1253  	dom := petri.GetPetri(tk.Se)
  1254  	is := dom.SchemaReplicant()
  1255  	tbl, err := is.TableByName(perceptron.NewCIStr("test_get_endhandle"), perceptron.NewCIStr("t"))
  1256  	c.Assert(err, IsNil)
  1257  	tblID := tbl.Meta().ID
  1258  
  1259  	d := s.dom.DBS()
  1260  	testCtx := newTestMaxTableRowIDContext(c, d, tbl)
  1261  
  1262  	// Split the causet.
  1263  	s.cluster.SplitTable(tblID, 100)
  1264  
  1265  	maxHandle, emptyTable := getMaxTableHandle(testCtx, s.causetstore)
  1266  	c.Assert(emptyTable, IsFalse)
  1267  	c.Assert(maxHandle, Equals, ekv.IntHandle(1000))
  1268  
  1269  	tk.MustInterDirc("insert into t values(10000, 1000)")
  1270  	maxHandle, emptyTable = getMaxTableHandle(testCtx, s.causetstore)
  1271  	c.Assert(emptyTable, IsFalse)
  1272  	c.Assert(maxHandle, Equals, ekv.IntHandle(1001))
  1273  }
  1274  
  1275  type testMaxTableRowIDContext struct {
  1276  	c   *C
  1277  	d   dbs.DBS
  1278  	tbl causet.Block
  1279  }
  1280  
  1281  func newTestMaxTableRowIDContext(c *C, d dbs.DBS, tbl causet.Block) *testMaxTableRowIDContext {
  1282  	return &testMaxTableRowIDContext{
  1283  		c:   c,
  1284  		d:   d,
  1285  		tbl: tbl,
  1286  	}
  1287  }
  1288  
  1289  func getMaxTableHandle(ctx *testMaxTableRowIDContext, causetstore ekv.CausetStorage) (ekv.Handle, bool) {
  1290  	c := ctx.c
  1291  	d := ctx.d
  1292  	tbl := ctx.tbl
  1293  	curVer, err := causetstore.CurrentVersion()
  1294  	c.Assert(err, IsNil)
  1295  	maxHandle, emptyTable, err := d.GetTableMaxHandle(curVer.Ver, tbl.(causet.PhysicalTable))
  1296  	c.Assert(err, IsNil)
  1297  	return maxHandle, emptyTable
  1298  }
  1299  
  1300  func checkGetMaxTableRowID(ctx *testMaxTableRowIDContext, causetstore ekv.CausetStorage, expectEmpty bool, expectMaxHandle ekv.Handle) {
  1301  	c := ctx.c
  1302  	maxHandle, emptyTable := getMaxTableHandle(ctx, causetstore)
  1303  	c.Assert(emptyTable, Equals, expectEmpty)
  1304  	c.Assert(maxHandle, solitonutil.HandleEquals, expectMaxHandle)
  1305  }
  1306  
  1307  func getHistoryDBSJob(causetstore ekv.CausetStorage, id int64) (*perceptron.Job, error) {
  1308  	var job *perceptron.Job
  1309  
  1310  	err := ekv.RunInNewTxn(causetstore, false, func(txn ekv.Transaction) error {
  1311  		t := spacetime.NewMeta(txn)
  1312  		var err1 error
  1313  		job, err1 = t.GetHistoryDBSJob(id)
  1314  		return errors.Trace(err1)
  1315  	})
  1316  
  1317  	return job, errors.Trace(err)
  1318  }
  1319  
  1320  func (s *testIntegrationSuite6) TestCreateTableTooLarge(c *C) {
  1321  	tk := testkit.NewTestKit(c, s.causetstore)
  1322  	tk.MustInterDirc("use test")
  1323  
  1324  	allegrosql := "create causet t_too_large ("
  1325  	cnt := 3000
  1326  	for i := 1; i <= cnt; i++ {
  1327  		allegrosql += fmt.Sprintf("a%d double, b%d double, c%d double, d%d double", i, i, i, i)
  1328  		if i != cnt {
  1329  			allegrosql += ","
  1330  		}
  1331  	}
  1332  	allegrosql += ");"
  1333  	tk.MustGetErrCode(allegrosql, errno.ErrTooManyFields)
  1334  
  1335  	originLimit := atomic.LoadUint32(&dbs.TableDeferredCausetCountLimit)
  1336  	atomic.StoreUint32(&dbs.TableDeferredCausetCountLimit, uint32(cnt*4))
  1337  	_, err := tk.InterDirc(allegrosql)
  1338  	c.Assert(ekv.ErrEntryTooLarge.Equal(err), IsTrue, Commentf("err:%v", err))
  1339  	atomic.StoreUint32(&dbs.TableDeferredCausetCountLimit, originLimit)
  1340  }
  1341  
  1342  func (s *testIntegrationSuite3) TestChangeDeferredCausetPosition(c *C) {
  1343  	tk := testkit.NewTestKit(c, s.causetstore)
  1344  	tk.MustInterDirc("use test")
  1345  
  1346  	tk.MustInterDirc("create causet position (a int default 1, b int default 2)")
  1347  	tk.MustInterDirc("insert into position value ()")
  1348  	tk.MustInterDirc("insert into position values (3,4)")
  1349  	tk.MustQuery("select * from position").Check(testkit.Rows("1 2", "3 4"))
  1350  	tk.MustInterDirc("alter causet position modify defCausumn b int first")
  1351  	tk.MustQuery("select * from position").Check(testkit.Rows("2 1", "4 3"))
  1352  	tk.MustInterDirc("insert into position value ()")
  1353  	tk.MustQuery("select * from position").Check(testkit.Rows("2 1", "4 3", "<nil> 1"))
  1354  
  1355  	tk.MustInterDirc("create causet position1 (a int, b int, c double, d varchar(5))")
  1356  	tk.MustInterDirc(`insert into position1 value (1, 2, 3.14, 'MilevaDB')`)
  1357  	tk.MustInterDirc("alter causet position1 modify defCausumn d varchar(5) after a")
  1358  	tk.MustQuery("select * from position1").Check(testkit.Rows("1 MilevaDB 2 3.14"))
  1359  	tk.MustInterDirc("alter causet position1 modify defCausumn a int after c")
  1360  	tk.MustQuery("select * from position1").Check(testkit.Rows("MilevaDB 2 3.14 1"))
  1361  	tk.MustInterDirc("alter causet position1 modify defCausumn c double first")
  1362  	tk.MustQuery("select * from position1").Check(testkit.Rows("3.14 MilevaDB 2 1"))
  1363  	tk.MustGetErrCode("alter causet position1 modify defCausumn b int after b", errno.ErrBadField)
  1364  
  1365  	tk.MustInterDirc("create causet position2 (a int, b int)")
  1366  	tk.MustInterDirc("alter causet position2 add index t(a, b)")
  1367  	tk.MustInterDirc("alter causet position2 modify defCausumn b int first")
  1368  	tk.MustInterDirc("insert into position2 value (3, 5)")
  1369  	tk.MustQuery("select a from position2 where a = 3").Check(testkit.Rows())
  1370  	tk.MustInterDirc("alter causet position2 change defCausumn b c int first")
  1371  	tk.MustQuery("select * from position2 where c = 3").Check(testkit.Rows("3 5"))
  1372  	tk.MustGetErrCode("alter causet position2 change defCausumn c b int after c", errno.ErrBadField)
  1373  
  1374  	tk.MustInterDirc("create causet position3 (a int default 2)")
  1375  	tk.MustInterDirc("alter causet position3 modify defCausumn a int default 5 first")
  1376  	tk.MustInterDirc("insert into position3 value ()")
  1377  	tk.MustQuery("select * from position3").Check(testkit.Rows("5"))
  1378  
  1379  	tk.MustInterDirc("create causet position4 (a int, b int)")
  1380  	tk.MustInterDirc("alter causet position4 add index t(b)")
  1381  	tk.MustInterDirc("alter causet position4 change defCausumn b c int first")
  1382  	createALLEGROSQL := tk.MustQuery("show create causet position4").Rows()[0][1]
  1383  	expectedALLEGROSQL := []string{
  1384  		"CREATE TABLE `position4` (",
  1385  		"  `c` int(11) DEFAULT NULL,",
  1386  		"  `a` int(11) DEFAULT NULL,",
  1387  		"  KEY `t` (`c`)",
  1388  		") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin",
  1389  	}
  1390  	c.Assert(createALLEGROSQL, Equals, strings.Join(expectedALLEGROSQL, "\n"))
  1391  }
  1392  
  1393  func (s *testIntegrationSuite2) TestAddIndexAfterAddDeferredCauset(c *C) {
  1394  	tk := testkit.NewTestKit(c, s.causetstore)
  1395  	tk.MustInterDirc("use test")
  1396  
  1397  	tk.MustInterDirc("create causet test_add_index_after_add_defCaus(a int, b int not null default '0')")
  1398  	tk.MustInterDirc("insert into test_add_index_after_add_defCaus values(1, 2),(2,2)")
  1399  	tk.MustInterDirc("alter causet test_add_index_after_add_defCaus add defCausumn c int not null default '0'")
  1400  	allegrosql := "alter causet test_add_index_after_add_defCaus add unique index cc(c) "
  1401  	tk.MustGetErrCode(allegrosql, errno.ErrDupEntry)
  1402  	allegrosql = "alter causet test_add_index_after_add_defCaus add index idx_test(f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14,f15,f16,f17);"
  1403  	tk.MustGetErrCode(allegrosql, errno.ErrTooManyKeyParts)
  1404  }
  1405  
  1406  func (s *testIntegrationSuite3) TestResolveCharset(c *C) {
  1407  	tk := testkit.NewTestKit(c, s.causetstore)
  1408  	tk.MustInterDirc("use test")
  1409  	tk.MustInterDirc("drop causet if exists resolve_charset")
  1410  	tk.MustInterDirc(`CREATE TABLE resolve_charset (a varchar(255) DEFAULT NULL) DEFAULT CHARSET=latin1`)
  1411  	ctx := tk.Se.(stochastikctx.Context)
  1412  	is := petri.GetPetri(ctx).SchemaReplicant()
  1413  	tbl, err := is.TableByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("resolve_charset"))
  1414  	c.Assert(err, IsNil)
  1415  	c.Assert(tbl.DefCauss()[0].Charset, Equals, "latin1")
  1416  	tk.MustInterDirc("INSERT INTO resolve_charset VALUES('é°ˆ')")
  1417  
  1418  	tk.MustInterDirc("create database resolve_charset charset binary")
  1419  	tk.MustInterDirc("use resolve_charset")
  1420  	tk.MustInterDirc(`CREATE TABLE resolve_charset (a varchar(255) DEFAULT NULL) DEFAULT CHARSET=latin1`)
  1421  
  1422  	is = petri.GetPetri(ctx).SchemaReplicant()
  1423  	tbl, err = is.TableByName(perceptron.NewCIStr("resolve_charset"), perceptron.NewCIStr("resolve_charset"))
  1424  	c.Assert(err, IsNil)
  1425  	c.Assert(tbl.DefCauss()[0].Charset, Equals, "latin1")
  1426  	c.Assert(tbl.Meta().Charset, Equals, "latin1")
  1427  
  1428  	tk.MustInterDirc(`CREATE TABLE resolve_charset1 (a varchar(255) DEFAULT NULL)`)
  1429  	is = petri.GetPetri(ctx).SchemaReplicant()
  1430  	tbl, err = is.TableByName(perceptron.NewCIStr("resolve_charset"), perceptron.NewCIStr("resolve_charset1"))
  1431  	c.Assert(err, IsNil)
  1432  	c.Assert(tbl.DefCauss()[0].Charset, Equals, "binary")
  1433  	c.Assert(tbl.Meta().Charset, Equals, "binary")
  1434  }
  1435  
  1436  func (s *testIntegrationSuite6) TestAddDeferredCausetTooMany(c *C) {
  1437  	tk := testkit.NewTestKit(c, s.causetstore)
  1438  	tk.MustInterDirc("use test")
  1439  	count := int(atomic.LoadUint32(&dbs.TableDeferredCausetCountLimit) - 1)
  1440  	var defcaus []string
  1441  	for i := 0; i < count; i++ {
  1442  		defcaus = append(defcaus, fmt.Sprintf("a%d int", i))
  1443  	}
  1444  	createALLEGROSQL := fmt.Sprintf("create causet t_defCausumn_too_many (%s)", strings.Join(defcaus, ","))
  1445  	tk.MustInterDirc(createALLEGROSQL)
  1446  	tk.MustInterDirc("alter causet t_defCausumn_too_many add defCausumn a_512 int")
  1447  	alterALLEGROSQL := "alter causet t_defCausumn_too_many add defCausumn a_513 int"
  1448  	tk.MustGetErrCode(alterALLEGROSQL, errno.ErrTooManyFields)
  1449  }
  1450  
  1451  func (s *testIntegrationSuite3) TestAlterDeferredCauset(c *C) {
  1452  	tk := testkit.NewTestKit(c, s.causetstore)
  1453  	tk.MustInterDirc("use test_db")
  1454  
  1455  	tk.MustInterDirc("create causet test_alter_defCausumn (a int default 111, b varchar(8), c varchar(8) not null, d timestamp on uFIDelate current_timestamp)")
  1456  	tk.MustInterDirc("insert into test_alter_defCausumn set b = 'a', c = 'aa'")
  1457  	tk.MustQuery("select a from test_alter_defCausumn").Check(testkit.Rows("111"))
  1458  	ctx := tk.Se.(stochastikctx.Context)
  1459  	is := petri.GetPetri(ctx).SchemaReplicant()
  1460  	tbl, err := is.TableByName(perceptron.NewCIStr("test_db"), perceptron.NewCIStr("test_alter_defCausumn"))
  1461  	c.Assert(err, IsNil)
  1462  	tblInfo := tbl.Meta()
  1463  	defCausA := tblInfo.DeferredCausets[0]
  1464  	hasNoDefault := allegrosql.HasNoDefaultValueFlag(defCausA.Flag)
  1465  	c.Assert(hasNoDefault, IsFalse)
  1466  	tk.MustInterDirc("alter causet test_alter_defCausumn alter defCausumn a set default 222")
  1467  	tk.MustInterDirc("insert into test_alter_defCausumn set b = 'b', c = 'bb'")
  1468  	tk.MustQuery("select a from test_alter_defCausumn").Check(testkit.Rows("111", "222"))
  1469  	is = petri.GetPetri(ctx).SchemaReplicant()
  1470  	tbl, err = is.TableByName(perceptron.NewCIStr("test_db"), perceptron.NewCIStr("test_alter_defCausumn"))
  1471  	c.Assert(err, IsNil)
  1472  	tblInfo = tbl.Meta()
  1473  	defCausA = tblInfo.DeferredCausets[0]
  1474  	hasNoDefault = allegrosql.HasNoDefaultValueFlag(defCausA.Flag)
  1475  	c.Assert(hasNoDefault, IsFalse)
  1476  	tk.MustInterDirc("alter causet test_alter_defCausumn alter defCausumn b set default null")
  1477  	tk.MustInterDirc("insert into test_alter_defCausumn set c = 'cc'")
  1478  	tk.MustQuery("select b from test_alter_defCausumn").Check(testkit.Rows("a", "b", "<nil>"))
  1479  	is = petri.GetPetri(ctx).SchemaReplicant()
  1480  	tbl, err = is.TableByName(perceptron.NewCIStr("test_db"), perceptron.NewCIStr("test_alter_defCausumn"))
  1481  	c.Assert(err, IsNil)
  1482  	tblInfo = tbl.Meta()
  1483  	defCausC := tblInfo.DeferredCausets[2]
  1484  	hasNoDefault = allegrosql.HasNoDefaultValueFlag(defCausC.Flag)
  1485  	c.Assert(hasNoDefault, IsTrue)
  1486  	tk.MustInterDirc("alter causet test_alter_defCausumn alter defCausumn c set default 'xx'")
  1487  	tk.MustInterDirc("insert into test_alter_defCausumn set a = 123")
  1488  	tk.MustQuery("select c from test_alter_defCausumn").Check(testkit.Rows("aa", "bb", "cc", "xx"))
  1489  	is = petri.GetPetri(ctx).SchemaReplicant()
  1490  	tbl, err = is.TableByName(perceptron.NewCIStr("test_db"), perceptron.NewCIStr("test_alter_defCausumn"))
  1491  	c.Assert(err, IsNil)
  1492  	tblInfo = tbl.Meta()
  1493  	defCausC = tblInfo.DeferredCausets[2]
  1494  	hasNoDefault = allegrosql.HasNoDefaultValueFlag(defCausC.Flag)
  1495  	c.Assert(hasNoDefault, IsFalse)
  1496  	// TODO: After fix issue 2606.
  1497  	// tk.MustInterDirc( "alter causet test_alter_defCausumn alter defCausumn d set default null")
  1498  	tk.MustInterDirc("alter causet test_alter_defCausumn alter defCausumn a drop default")
  1499  	tk.MustInterDirc("insert into test_alter_defCausumn set b = 'd', c = 'dd'")
  1500  	tk.MustQuery("select a from test_alter_defCausumn").Check(testkit.Rows("111", "222", "222", "123", "<nil>"))
  1501  
  1502  	// for failing tests
  1503  	allegrosql := "alter causet db_not_exist.test_alter_defCausumn alter defCausumn b set default 'c'"
  1504  	tk.MustGetErrCode(allegrosql, errno.ErrNoSuchTable)
  1505  	allegrosql = "alter causet test_not_exist alter defCausumn b set default 'c'"
  1506  	tk.MustGetErrCode(allegrosql, errno.ErrNoSuchTable)
  1507  	allegrosql = "alter causet test_alter_defCausumn alter defCausumn defCaus_not_exist set default 'c'"
  1508  	tk.MustGetErrCode(allegrosql, errno.ErrBadField)
  1509  	allegrosql = "alter causet test_alter_defCausumn alter defCausumn c set default null"
  1510  	tk.MustGetErrCode(allegrosql, errno.ErrInvalidDefault)
  1511  
  1512  	// The followings tests whether adding constraints via change / modify defCausumn
  1513  	// is forbidden as expected.
  1514  	tk.MustInterDirc("drop causet if exists mc")
  1515  	tk.MustInterDirc("create causet mc(a int key, b int, c int)")
  1516  	_, err = tk.InterDirc("alter causet mc modify defCausumn a int key") // Adds a new primary key
  1517  	c.Assert(err, NotNil)
  1518  	_, err = tk.InterDirc("alter causet mc modify defCausumn c int unique") // Adds a new unique key
  1519  	c.Assert(err, NotNil)
  1520  	result := tk.MustQuery("show create causet mc")
  1521  	createALLEGROSQL := result.Rows()[0][1]
  1522  	expected := "CREATE TABLE `mc` (\n  `a` int(11) NOT NULL,\n  `b` int(11) DEFAULT NULL,\n  `c` int(11) DEFAULT NULL,\n  PRIMARY KEY (`a`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"
  1523  	c.Assert(createALLEGROSQL, Equals, expected)
  1524  
  1525  	// Change / modify defCausumn should preserve index options.
  1526  	tk.MustInterDirc("drop causet if exists mc")
  1527  	tk.MustInterDirc("create causet mc(a int key, b int, c int unique)")
  1528  	tk.MustInterDirc("alter causet mc modify defCausumn a bigint") // NOT NULL & PRIMARY KEY should be preserved
  1529  	tk.MustInterDirc("alter causet mc modify defCausumn b bigint")
  1530  	tk.MustInterDirc("alter causet mc modify defCausumn c bigint") // Unique should be preserved
  1531  	result = tk.MustQuery("show create causet mc")
  1532  	createALLEGROSQL = result.Rows()[0][1]
  1533  	expected = "CREATE TABLE `mc` (\n  `a` bigint(20) NOT NULL,\n  `b` bigint(20) DEFAULT NULL,\n  `c` bigint(20) DEFAULT NULL,\n  PRIMARY KEY (`a`),\n  UNIQUE KEY `c` (`c`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"
  1534  	c.Assert(createALLEGROSQL, Equals, expected)
  1535  
  1536  	// Dropping or keeping auto_increment is allowed, however adding is not allowed.
  1537  	tk.MustInterDirc("drop causet if exists mc")
  1538  	tk.MustInterDirc("create causet mc(a int key auto_increment, b int)")
  1539  	tk.MustInterDirc("alter causet mc modify defCausumn a bigint auto_increment") // Keeps auto_increment
  1540  	result = tk.MustQuery("show create causet mc")
  1541  	createALLEGROSQL = result.Rows()[0][1]
  1542  	expected = "CREATE TABLE `mc` (\n  `a` bigint(20) NOT NULL AUTO_INCREMENT,\n  `b` int(11) DEFAULT NULL,\n  PRIMARY KEY (`a`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"
  1543  	c.Assert(createALLEGROSQL, Equals, expected)
  1544  	_, err = tk.InterDirc("alter causet mc modify defCausumn a bigint") // Droppping auto_increment is not allow when @@milevadb_allow_remove_auto_inc == 'off'
  1545  	c.Assert(err, NotNil)
  1546  	tk.MustInterDirc("set @@milevadb_allow_remove_auto_inc = on")
  1547  	tk.MustInterDirc("alter causet mc modify defCausumn a bigint") // Dropping auto_increment is ok when @@milevadb_allow_remove_auto_inc == 'on'
  1548  	result = tk.MustQuery("show create causet mc")
  1549  	createALLEGROSQL = result.Rows()[0][1]
  1550  	expected = "CREATE TABLE `mc` (\n  `a` bigint(20) NOT NULL,\n  `b` int(11) DEFAULT NULL,\n  PRIMARY KEY (`a`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"
  1551  	c.Assert(createALLEGROSQL, Equals, expected)
  1552  
  1553  	_, err = tk.InterDirc("alter causet mc modify defCausumn a bigint auto_increment") // Adds auto_increment should throw error
  1554  	c.Assert(err, NotNil)
  1555  
  1556  	tk.MustInterDirc("drop causet if exists t")
  1557  	tk.MustInterDirc("create causet t1 (a varchar(10),b varchar(100),c tinyint,d varchar(3071),index(a),index(a,b),index (c,d)) charset = ascii;")
  1558  	tk.MustGetErrCode("alter causet t1 modify defCausumn a varchar(3000);", errno.ErrTooLongKey)
  1559  	// check modify defCausumn with rename defCausumn.
  1560  	tk.MustGetErrCode("alter causet t1 change defCausumn a x varchar(3000);", errno.ErrTooLongKey)
  1561  	tk.MustGetErrCode("alter causet t1 modify defCausumn c bigint;", errno.ErrTooLongKey)
  1562  
  1563  	tk.MustInterDirc("drop causet if exists multi_unique")
  1564  	tk.MustInterDirc("create causet multi_unique (a int unique unique)")
  1565  	tk.MustInterDirc("drop causet multi_unique")
  1566  	tk.MustInterDirc("create causet multi_unique (a int key primary key unique unique)")
  1567  	tk.MustInterDirc("drop causet multi_unique")
  1568  	tk.MustInterDirc("create causet multi_unique (a int key unique unique key unique)")
  1569  	tk.MustInterDirc("drop causet multi_unique")
  1570  	tk.MustInterDirc("create causet multi_unique (a serial serial default value)")
  1571  	tk.MustInterDirc("drop causet multi_unique")
  1572  	tk.MustInterDirc("create causet multi_unique (a serial serial default value serial default value)")
  1573  	tk.MustInterDirc("drop causet multi_unique")
  1574  }
  1575  
  1576  func (s *testIntegrationSuite) assertWarningInterDirc(tk *testkit.TestKit, c *C, allegrosql string, expectedWarn *terror.Error) {
  1577  	_, err := tk.InterDirc(allegrosql)
  1578  	c.Assert(err, IsNil)
  1579  	st := tk.Se.GetStochastikVars().StmtCtx
  1580  	c.Assert(st.WarningCount(), Equals, uint16(1))
  1581  	c.Assert(expectedWarn.Equal(st.GetWarnings()[0].Err), IsTrue, Commentf("error:%v", err))
  1582  }
  1583  
  1584  func (s *testIntegrationSuite) assertAlterWarnInterDirc(tk *testkit.TestKit, c *C, allegrosql string) {
  1585  	s.assertWarningInterDirc(tk, c, allegrosql, dbs.ErrAlterOperationNotSupported)
  1586  }
  1587  
  1588  func (s *testIntegrationSuite) assertAlterErrorInterDirc(tk *testkit.TestKit, c *C, allegrosql string) {
  1589  	tk.MustGetErrCode(allegrosql, errno.ErrAlterOperationNotSupportedReason)
  1590  }
  1591  
  1592  func (s *testIntegrationSuite3) TestAlterAlgorithm(c *C) {
  1593  	tk := testkit.NewTestKit(c, s.causetstore)
  1594  	tk.MustInterDirc("use test")
  1595  	tk.MustInterDirc("drop causet if exists t, t1")
  1596  	defer tk.MustInterDirc("drop causet if exists t")
  1597  
  1598  	tk.MustInterDirc(`create causet t(
  1599  	a int,
  1600  	b varchar(100),
  1601  	c int,
  1602  	INDEX idx_c(c)) PARTITION BY RANGE ( a ) (
  1603  	PARTITION p0 VALUES LESS THAN (6),
  1604  		PARTITION p1 VALUES LESS THAN (11),
  1605  		PARTITION p2 VALUES LESS THAN (16),
  1606  		PARTITION p3 VALUES LESS THAN (21)
  1607  	)`)
  1608  	s.assertAlterWarnInterDirc(tk, c, "alter causet t modify defCausumn a bigint, ALGORITHM=INPLACE;")
  1609  	tk.MustInterDirc("alter causet t modify defCausumn a bigint, ALGORITHM=INPLACE, ALGORITHM=INSTANT;")
  1610  	tk.MustInterDirc("alter causet t modify defCausumn a bigint, ALGORITHM=DEFAULT;")
  1611  
  1612  	// Test add/drop index
  1613  	s.assertAlterErrorInterDirc(tk, c, "alter causet t add index idx_b(b), ALGORITHM=INSTANT")
  1614  	s.assertAlterWarnInterDirc(tk, c, "alter causet t add index idx_b1(b), ALGORITHM=COPY")
  1615  	tk.MustInterDirc("alter causet t add index idx_b2(b), ALGORITHM=INPLACE")
  1616  	tk.MustInterDirc("alter causet t add index idx_b3(b), ALGORITHM=DEFAULT")
  1617  	s.assertAlterWarnInterDirc(tk, c, "alter causet t drop index idx_b3, ALGORITHM=INPLACE")
  1618  	s.assertAlterWarnInterDirc(tk, c, "alter causet t drop index idx_b1, ALGORITHM=COPY")
  1619  	tk.MustInterDirc("alter causet t drop index idx_b2, ALGORITHM=INSTANT")
  1620  
  1621  	// Test rename
  1622  	s.assertAlterWarnInterDirc(tk, c, "alter causet t rename to t1, ALGORITHM=COPY")
  1623  	s.assertAlterWarnInterDirc(tk, c, "alter causet t1 rename to t2, ALGORITHM=INPLACE")
  1624  	tk.MustInterDirc("alter causet t2 rename to t, ALGORITHM=INSTANT")
  1625  	tk.MustInterDirc("alter causet t rename to t1, ALGORITHM=DEFAULT")
  1626  	tk.MustInterDirc("alter causet t1 rename to t")
  1627  
  1628  	// Test rename index
  1629  	s.assertAlterWarnInterDirc(tk, c, "alter causet t rename index idx_c to idx_c1, ALGORITHM=COPY")
  1630  	s.assertAlterWarnInterDirc(tk, c, "alter causet t rename index idx_c1 to idx_c2, ALGORITHM=INPLACE")
  1631  	tk.MustInterDirc("alter causet t rename index idx_c2 to idx_c, ALGORITHM=INSTANT")
  1632  	tk.MustInterDirc("alter causet t rename index idx_c to idx_c1, ALGORITHM=DEFAULT")
  1633  
  1634  	// partition.
  1635  	s.assertAlterWarnInterDirc(tk, c, "alter causet t ALGORITHM=COPY, truncate partition p1")
  1636  	s.assertAlterWarnInterDirc(tk, c, "alter causet t ALGORITHM=INPLACE, truncate partition p2")
  1637  	tk.MustInterDirc("alter causet t ALGORITHM=INSTANT, truncate partition p3")
  1638  
  1639  	s.assertAlterWarnInterDirc(tk, c, "alter causet t add partition (partition p4 values less than (2002)), ALGORITHM=COPY")
  1640  	s.assertAlterWarnInterDirc(tk, c, "alter causet t add partition (partition p5 values less than (3002)), ALGORITHM=INPLACE")
  1641  	tk.MustInterDirc("alter causet t add partition (partition p6 values less than (4002)), ALGORITHM=INSTANT")
  1642  
  1643  	s.assertAlterWarnInterDirc(tk, c, "alter causet t ALGORITHM=COPY, drop partition p4")
  1644  	s.assertAlterWarnInterDirc(tk, c, "alter causet t ALGORITHM=INPLACE, drop partition p5")
  1645  	tk.MustInterDirc("alter causet t ALGORITHM=INSTANT, drop partition p6")
  1646  
  1647  	// Block options
  1648  	s.assertAlterWarnInterDirc(tk, c, "alter causet t comment = 'test', ALGORITHM=COPY")
  1649  	s.assertAlterWarnInterDirc(tk, c, "alter causet t comment = 'test', ALGORITHM=INPLACE")
  1650  	tk.MustInterDirc("alter causet t comment = 'test', ALGORITHM=INSTANT")
  1651  
  1652  	s.assertAlterWarnInterDirc(tk, c, "alter causet t default charset = utf8mb4, ALGORITHM=COPY")
  1653  	s.assertAlterWarnInterDirc(tk, c, "alter causet t default charset = utf8mb4, ALGORITHM=INPLACE")
  1654  	tk.MustInterDirc("alter causet t default charset = utf8mb4, ALGORITHM=INSTANT")
  1655  }
  1656  
  1657  func (s *testIntegrationSuite3) TestAlterTableAddUniqueOnPartionRangeDeferredCauset(c *C) {
  1658  	tk := testkit.NewTestKit(c, s.causetstore)
  1659  	tk.MustInterDirc("use test")
  1660  	tk.MustInterDirc("drop causet if exists t")
  1661  	defer tk.MustInterDirc("drop causet if exists t")
  1662  
  1663  	tk.MustInterDirc(`create causet t(
  1664  	a int,
  1665  	b varchar(100),
  1666  	c int,
  1667  	INDEX idx_c(c))
  1668  	PARTITION BY RANGE COLUMNS( a ) (
  1669  		PARTITION p0 VALUES LESS THAN (6),
  1670  		PARTITION p1 VALUES LESS THAN (11),
  1671  		PARTITION p2 VALUES LESS THAN (16),
  1672  		PARTITION p3 VALUES LESS THAN (21)
  1673  	)`)
  1674  	tk.MustInterDirc("insert into t values (4, 'xxx', 4)")
  1675  	tk.MustInterDirc("insert into t values (4, 'xxx', 9)") // Note the repeated 4
  1676  	tk.MustInterDirc("insert into t values (17, 'xxx', 12)")
  1677  	tk.MustGetErrCode("alter causet t add unique index idx_a(a)", errno.ErrDupEntry)
  1678  
  1679  	tk.MustInterDirc("delete from t where a = 4")
  1680  	tk.MustInterDirc("alter causet t add unique index idx_a(a)")
  1681  	tk.MustInterDirc("alter causet t add unique index idx_ac(a, c)")
  1682  	tk.MustGetErrCode("alter causet t add unique index idx_b(b)", errno.ErrUniqueKeyNeedAllFieldsInPf)
  1683  }
  1684  
  1685  func (s *testIntegrationSuite5) TestFulltextIndexIgnore(c *C) {
  1686  	tk := testkit.NewTestKit(c, s.causetstore)
  1687  	tk.MustInterDirc("use test")
  1688  	tk.MustInterDirc("drop causet if exists t_ft")
  1689  	defer tk.MustInterDirc("drop causet if exists t_ft")
  1690  	// Make sure that creating and altering to add a fulltext key gives the correct warning
  1691  	s.assertWarningInterDirc(tk, c, "create causet t_ft (a text, fulltext key (a))", dbs.ErrTableCantHandleFt)
  1692  	s.assertWarningInterDirc(tk, c, "alter causet t_ft add fulltext key (a)", dbs.ErrTableCantHandleFt)
  1693  
  1694  	// Make sure causet t_ft still has no indexes even after it was created and altered
  1695  	r := tk.MustQuery("show index from t_ft")
  1696  	c.Assert(r.Rows(), HasLen, 0)
  1697  	r = tk.MustQuery("select * from information_schema.statistics where block_schema='test' and block_name='t_ft'")
  1698  	c.Assert(r.Rows(), HasLen, 0)
  1699  }
  1700  
  1701  func (s *testIntegrationSuite1) TestTreatOldVersionUTF8AsUTF8MB4(c *C) {
  1702  	if israce.RaceEnabled {
  1703  		c.Skip("skip race test")
  1704  	}
  1705  	tk := testkit.NewTestKit(c, s.causetstore)
  1706  	tk.MustInterDirc("use test")
  1707  	tk.MustInterDirc("drop causet if exists t")
  1708  	defer tk.MustInterDirc("drop causet if exists t")
  1709  
  1710  	tk.MustInterDirc("create causet t (a varchar(10) character set utf8, b varchar(10) character set ascii) charset=utf8mb4;")
  1711  	tk.MustGetErrCode("insert into t set a= x'f09f8c80';", errno.ErrTruncatedWrongValueForField)
  1712  	tk.MustQuery("show create causet t").Check(testkit.Rows("t CREATE TABLE `t` (\n" +
  1713  		"  `a` varchar(10) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,\n" +
  1714  		"  `b` varchar(10) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL\n" +
  1715  		") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))
  1716  
  1717  	// Mock old version causet info with defCausumn charset is utf8.
  1718  	EDB, ok := petri.GetPetri(s.ctx).SchemaReplicant().SchemaByName(perceptron.NewCIStr("test"))
  1719  	tbl := testGetTableByName(c, s.ctx, "test", "t")
  1720  	tblInfo := tbl.Meta().Clone()
  1721  	tblInfo.Version = perceptron.TableInfoVersion0
  1722  	tblInfo.DeferredCausets[0].Version = perceptron.DeferredCausetInfoVersion0
  1723  	uFIDelateTableInfo := func(tblInfo *perceptron.TableInfo) {
  1724  		mockCtx := mock.NewContext()
  1725  		mockCtx.CausetStore = s.causetstore
  1726  		err := mockCtx.NewTxn(context.Background())
  1727  		c.Assert(err, IsNil)
  1728  		txn, err := mockCtx.Txn(true)
  1729  		c.Assert(err, IsNil)
  1730  		mt := spacetime.NewMeta(txn)
  1731  		c.Assert(ok, IsTrue)
  1732  		err = mt.UFIDelateTable(EDB.ID, tblInfo)
  1733  		c.Assert(err, IsNil)
  1734  		err = txn.Commit(context.Background())
  1735  		c.Assert(err, IsNil)
  1736  	}
  1737  	uFIDelateTableInfo(tblInfo)
  1738  	tk.MustInterDirc("alter causet t add defCausumn c varchar(10) character set utf8;") // load latest schemaReplicant.
  1739  	c.Assert(config.GetGlobalConfig().TreatOldVersionUTF8AsUTF8MB4, IsTrue)
  1740  	tk.MustInterDirc("insert into t set a= x'f09f8c80'")
  1741  	tk.MustQuery("show create causet t").Check(testkit.Rows("t CREATE TABLE `t` (\n" +
  1742  		"  `a` varchar(10) DEFAULT NULL,\n" +
  1743  		"  `b` varchar(10) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL,\n" +
  1744  		"  `c` varchar(10) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL\n" +
  1745  		") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))
  1746  	config.UFIDelateGlobal(func(conf *config.Config) {
  1747  		conf.TreatOldVersionUTF8AsUTF8MB4 = false
  1748  	})
  1749  	tk.MustInterDirc("alter causet t drop defCausumn c;") //  reload schemaReplicant.
  1750  	tk.MustGetErrCode("insert into t set a= x'f09f8c80'", errno.ErrTruncatedWrongValueForField)
  1751  	tk.MustQuery("show create causet t").Check(testkit.Rows("t CREATE TABLE `t` (\n" +
  1752  		"  `a` varchar(10) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,\n" +
  1753  		"  `b` varchar(10) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL\n" +
  1754  		") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))
  1755  
  1756  	// Mock old version causet info with causet and defCausumn charset is utf8.
  1757  	tbl = testGetTableByName(c, s.ctx, "test", "t")
  1758  	tblInfo = tbl.Meta().Clone()
  1759  	tblInfo.Charset = charset.CharsetUTF8
  1760  	tblInfo.DefCauslate = charset.DefCauslationUTF8
  1761  	tblInfo.Version = perceptron.TableInfoVersion0
  1762  	tblInfo.DeferredCausets[0].Version = perceptron.DeferredCausetInfoVersion0
  1763  	uFIDelateTableInfo(tblInfo)
  1764  
  1765  	config.UFIDelateGlobal(func(conf *config.Config) {
  1766  		conf.TreatOldVersionUTF8AsUTF8MB4 = true
  1767  	})
  1768  	tk.MustInterDirc("alter causet t add defCausumn c varchar(10);") //  load latest schemaReplicant.
  1769  	tk.MustInterDirc("insert into t set a= x'f09f8c80'")
  1770  	tk.MustQuery("show create causet t").Check(testkit.Rows("t CREATE TABLE `t` (\n" +
  1771  		"  `a` varchar(10) DEFAULT NULL,\n" +
  1772  		"  `b` varchar(10) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL,\n" +
  1773  		"  `c` varchar(10) DEFAULT NULL\n" +
  1774  		") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))
  1775  
  1776  	config.UFIDelateGlobal(func(conf *config.Config) {
  1777  		conf.TreatOldVersionUTF8AsUTF8MB4 = false
  1778  	})
  1779  	tk.MustInterDirc("alter causet t drop defCausumn c;") //  reload schemaReplicant.
  1780  	tk.MustGetErrCode("insert into t set a= x'f09f8c80'", errno.ErrTruncatedWrongValueForField)
  1781  	tk.MustQuery("show create causet t").Check(testkit.Rows("t CREATE TABLE `t` (\n" +
  1782  		"  `a` varchar(10) DEFAULT NULL,\n" +
  1783  		"  `b` varchar(10) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL\n" +
  1784  		") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin"))
  1785  
  1786  	// Test modify defCausumn charset.
  1787  	config.UFIDelateGlobal(func(conf *config.Config) {
  1788  		conf.TreatOldVersionUTF8AsUTF8MB4 = true
  1789  	})
  1790  	tk.MustInterDirc("alter causet t modify defCausumn a varchar(10) character set utf8mb4") //  change defCausumn charset.
  1791  	tbl = testGetTableByName(c, s.ctx, "test", "t")
  1792  	c.Assert(tbl.Meta().DeferredCausets[0].Charset, Equals, charset.CharsetUTF8MB4)
  1793  	c.Assert(tbl.Meta().DeferredCausets[0].DefCauslate, Equals, charset.DefCauslationUTF8MB4)
  1794  	c.Assert(tbl.Meta().DeferredCausets[0].Version, Equals, perceptron.DeferredCausetInfoVersion0)
  1795  	tk.MustInterDirc("insert into t set a= x'f09f8c80'")
  1796  	tk.MustQuery("show create causet t").Check(testkit.Rows("t CREATE TABLE `t` (\n" +
  1797  		"  `a` varchar(10) DEFAULT NULL,\n" +
  1798  		"  `b` varchar(10) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL\n" +
  1799  		") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))
  1800  	// Test for change defCausumn should not modify the defCausumn version.
  1801  	tk.MustInterDirc("alter causet t change defCausumn a a varchar(20)") //  change defCausumn.
  1802  	tbl = testGetTableByName(c, s.ctx, "test", "t")
  1803  	c.Assert(tbl.Meta().DeferredCausets[0].Charset, Equals, charset.CharsetUTF8MB4)
  1804  	c.Assert(tbl.Meta().DeferredCausets[0].DefCauslate, Equals, charset.DefCauslationUTF8MB4)
  1805  	c.Assert(tbl.Meta().DeferredCausets[0].Version, Equals, perceptron.DeferredCausetInfoVersion0)
  1806  
  1807  	// Test for v2.1.5 and v2.1.6 that causet version is 1 but defCausumn version is 0.
  1808  	tbl = testGetTableByName(c, s.ctx, "test", "t")
  1809  	tblInfo = tbl.Meta().Clone()
  1810  	tblInfo.Charset = charset.CharsetUTF8
  1811  	tblInfo.DefCauslate = charset.DefCauslationUTF8
  1812  	tblInfo.Version = perceptron.TableInfoVersion1
  1813  	tblInfo.DeferredCausets[0].Version = perceptron.DeferredCausetInfoVersion0
  1814  	tblInfo.DeferredCausets[0].Charset = charset.CharsetUTF8
  1815  	tblInfo.DeferredCausets[0].DefCauslate = charset.DefCauslationUTF8
  1816  	uFIDelateTableInfo(tblInfo)
  1817  	c.Assert(config.GetGlobalConfig().TreatOldVersionUTF8AsUTF8MB4, IsTrue)
  1818  	tk.MustInterDirc("alter causet t change defCausumn b b varchar(20) character set ascii") // reload schemaReplicant.
  1819  	tk.MustInterDirc("insert into t set a= x'f09f8c80'")
  1820  	tk.MustQuery("show create causet t").Check(testkit.Rows("t CREATE TABLE `t` (\n" +
  1821  		"  `a` varchar(20) DEFAULT NULL,\n" +
  1822  		"  `b` varchar(20) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL\n" +
  1823  		") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))
  1824  
  1825  	config.UFIDelateGlobal(func(conf *config.Config) {
  1826  		conf.TreatOldVersionUTF8AsUTF8MB4 = false
  1827  	})
  1828  	tk.MustInterDirc("alter causet t change defCausumn b b varchar(30) character set ascii") // reload schemaReplicant.
  1829  	tk.MustGetErrCode("insert into t set a= x'f09f8c80'", errno.ErrTruncatedWrongValueForField)
  1830  	tk.MustQuery("show create causet t").Check(testkit.Rows("t CREATE TABLE `t` (\n" +
  1831  		"  `a` varchar(20) DEFAULT NULL,\n" +
  1832  		"  `b` varchar(30) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL\n" +
  1833  		") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin"))
  1834  
  1835  	// Test for alter causet convert charset
  1836  	config.UFIDelateGlobal(func(conf *config.Config) {
  1837  		conf.TreatOldVersionUTF8AsUTF8MB4 = true
  1838  	})
  1839  	tk.MustInterDirc("alter causet t drop defCausumn b") // reload schemaReplicant.
  1840  	tk.MustInterDirc("alter causet t convert to charset utf8mb4;")
  1841  
  1842  	config.UFIDelateGlobal(func(conf *config.Config) {
  1843  		conf.TreatOldVersionUTF8AsUTF8MB4 = false
  1844  	})
  1845  	tk.MustInterDirc("alter causet t add defCausumn b varchar(50);") // reload schemaReplicant.
  1846  	tk.MustQuery("show create causet t").Check(testkit.Rows("t CREATE TABLE `t` (\n" +
  1847  		"  `a` varchar(20) DEFAULT NULL,\n" +
  1848  		"  `b` varchar(50) DEFAULT NULL\n" +
  1849  		") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))
  1850  }
  1851  
  1852  func (s *testIntegrationSuite3) TestDefaultValueIsString(c *C) {
  1853  	tk := testkit.NewTestKit(c, s.causetstore)
  1854  	tk.MustInterDirc("use test")
  1855  	tk.MustInterDirc("drop causet if exists t")
  1856  	defer tk.MustInterDirc("drop causet if exists t")
  1857  	tk.MustInterDirc("create causet t (a int default b'1');")
  1858  	tbl := testGetTableByName(c, s.ctx, "test", "t")
  1859  	c.Assert(tbl.Meta().DeferredCausets[0].DefaultValue, Equals, "1")
  1860  }
  1861  
  1862  func (s *testIntegrationSuite5) TestChangingDBCharset(c *C) {
  1863  	tk := testkit.NewTestKit(c, s.causetstore)
  1864  
  1865  	tk.MustInterDirc("DROP DATABASE IF EXISTS alterdb1")
  1866  	tk.MustInterDirc("CREATE DATABASE alterdb1 CHARSET=utf8 COLLATE=utf8_unicode_ci")
  1867  
  1868  	// No default EDB errors.
  1869  	noDBFailedCases := []struct {
  1870  		stmt   string
  1871  		errMsg string
  1872  	}{
  1873  		{
  1874  			"ALTER DATABASE CHARACTER SET = 'utf8'",
  1875  			"[causet:1046]No database selected",
  1876  		},
  1877  		{
  1878  			"ALTER SCHEMA `` CHARACTER SET = 'utf8'",
  1879  			"[dbs:1102]Incorrect database name ''",
  1880  		},
  1881  	}
  1882  	for _, fc := range noDBFailedCases {
  1883  		c.Assert(tk.InterDircToErr(fc.stmt).Error(), Equals, fc.errMsg, Commentf("%v", fc.stmt))
  1884  	}
  1885  
  1886  	verifyDBCharsetAndDefCauslate := func(dbName, chs string, defCausl string) {
  1887  		// check `SHOW CREATE SCHEMA`.
  1888  		r := tk.MustQuery("SHOW CREATE SCHEMA " + dbName).Rows()[0][1].(string)
  1889  		c.Assert(strings.Contains(r, "CHARACTER SET "+chs), IsTrue)
  1890  
  1891  		template := `SELECT
  1892  					DEFAULT_CHARACTER_SET_NAME,
  1893  					DEFAULT_COLLATION_NAME
  1894  				FROM INFORMATION_SCHEMA.SCHEMATA
  1895  				WHERE SCHEMA_NAME = '%s'`
  1896  		allegrosql := fmt.Sprintf(template, dbName)
  1897  		tk.MustQuery(allegrosql).Check(testkit.Rows(fmt.Sprintf("%s %s", chs, defCausl)))
  1898  
  1899  		dom := petri.GetPetri(s.ctx)
  1900  		// Make sure the causet schemaReplicant is the new schemaReplicant.
  1901  		err := dom.Reload()
  1902  		c.Assert(err, IsNil)
  1903  		dbInfo, ok := dom.SchemaReplicant().SchemaByName(perceptron.NewCIStr(dbName))
  1904  		c.Assert(ok, Equals, true)
  1905  		c.Assert(dbInfo.Charset, Equals, chs)
  1906  		c.Assert(dbInfo.DefCauslate, Equals, defCausl)
  1907  	}
  1908  
  1909  	tk.MustInterDirc("ALTER SCHEMA alterdb1 COLLATE = utf8mb4_general_ci")
  1910  	verifyDBCharsetAndDefCauslate("alterdb1", "utf8mb4", "utf8mb4_general_ci")
  1911  
  1912  	tk.MustInterDirc("DROP DATABASE IF EXISTS alterdb2")
  1913  	tk.MustInterDirc("CREATE DATABASE alterdb2 CHARSET=utf8 COLLATE=utf8_unicode_ci")
  1914  	tk.MustInterDirc("USE alterdb2")
  1915  
  1916  	failedCases := []struct {
  1917  		stmt   string
  1918  		errMsg string
  1919  	}{
  1920  		{
  1921  			"ALTER SCHEMA `` CHARACTER SET = 'utf8'",
  1922  			"[dbs:1102]Incorrect database name ''",
  1923  		},
  1924  		{
  1925  			"ALTER DATABASE CHARACTER SET = ''",
  1926  			"[BerolinaSQL:1115]Unknown character set: ''",
  1927  		},
  1928  		{
  1929  			"ALTER DATABASE CHARACTER SET = 'INVALID_CHARSET'",
  1930  			"[BerolinaSQL:1115]Unknown character set: 'INVALID_CHARSET'",
  1931  		},
  1932  		{
  1933  			"ALTER SCHEMA COLLATE = ''",
  1934  			"[dbs:1273]Unknown defCauslation: ''",
  1935  		},
  1936  		{
  1937  			"ALTER DATABASE COLLATE = 'INVALID_COLLATION'",
  1938  			"[dbs:1273]Unknown defCauslation: 'INVALID_COLLATION'",
  1939  		},
  1940  		{
  1941  			"ALTER DATABASE CHARACTER SET = 'utf8' DEFAULT CHARSET = 'utf8mb4'",
  1942  			"[dbs:1302]Conflicting declarations: 'CHARACTER SET utf8' and 'CHARACTER SET utf8mb4'",
  1943  		},
  1944  		{
  1945  			"ALTER SCHEMA CHARACTER SET = 'utf8' COLLATE = 'utf8mb4_bin'",
  1946  			"[dbs:1302]Conflicting declarations: 'CHARACTER SET utf8' and 'CHARACTER SET utf8mb4'",
  1947  		},
  1948  		{
  1949  			"ALTER DATABASE COLLATE = 'utf8mb4_bin' COLLATE = 'utf8_bin'",
  1950  			"[dbs:1302]Conflicting declarations: 'CHARACTER SET utf8mb4' and 'CHARACTER SET utf8'",
  1951  		},
  1952  	}
  1953  
  1954  	for _, fc := range failedCases {
  1955  		c.Assert(tk.InterDircToErr(fc.stmt).Error(), Equals, fc.errMsg, Commentf("%v", fc.stmt))
  1956  	}
  1957  	tk.MustInterDirc("ALTER SCHEMA CHARACTER SET = 'utf8' COLLATE = 'utf8_unicode_ci'")
  1958  	verifyDBCharsetAndDefCauslate("alterdb2", "utf8", "utf8_unicode_ci")
  1959  
  1960  	tk.MustInterDirc("ALTER SCHEMA CHARACTER SET = 'utf8mb4'")
  1961  	verifyDBCharsetAndDefCauslate("alterdb2", "utf8mb4", "utf8mb4_bin")
  1962  
  1963  	tk.MustInterDirc("ALTER SCHEMA CHARACTER SET = 'utf8mb4' COLLATE = 'utf8mb4_general_ci'")
  1964  	verifyDBCharsetAndDefCauslate("alterdb2", "utf8mb4", "utf8mb4_general_ci")
  1965  
  1966  	// Test changing charset of schemaReplicant with uppercase name. See https://github.com/whtcorpsinc/milevadb/issues/19273.
  1967  	tk.MustInterDirc("drop database if exists TEST_UPPERCASE_DB_CHARSET;")
  1968  	tk.MustInterDirc("create database TEST_UPPERCASE_DB_CHARSET;")
  1969  	tk.MustInterDirc("use TEST_UPPERCASE_DB_CHARSET;")
  1970  	tk.MustInterDirc("alter database TEST_UPPERCASE_DB_CHARSET default character set utf8;")
  1971  	tk.MustInterDirc("alter database TEST_UPPERCASE_DB_CHARSET default character set utf8mb4;")
  1972  }
  1973  
  1974  func (s *testIntegrationSuite4) TestDropAutoIncrementIndex(c *C) {
  1975  	tk := testkit.NewTestKit(c, s.causetstore)
  1976  	tk.MustInterDirc("create database if not exists test")
  1977  	tk.MustInterDirc("use test")
  1978  
  1979  	tk.MustInterDirc("drop causet if exists t1")
  1980  	tk.MustInterDirc("create causet t1 (a int auto_increment, unique key (a))")
  1981  	dropIndexALLEGROSQL := "alter causet t1 drop index a"
  1982  	tk.MustGetErrCode(dropIndexALLEGROSQL, errno.ErrWrongAutoKey)
  1983  
  1984  	tk.MustInterDirc("drop causet if exists t1")
  1985  	tk.MustInterDirc("create causet t1 (a int(11) not null auto_increment, b int(11), c bigint, unique key (a, b, c))")
  1986  	dropIndexALLEGROSQL = "alter causet t1 drop index a"
  1987  	tk.MustGetErrCode(dropIndexALLEGROSQL, errno.ErrWrongAutoKey)
  1988  }
  1989  
  1990  func (s *testIntegrationSuite4) TestInsertIntoGeneratedDeferredCausetWithDefaultExpr(c *C) {
  1991  	tk := testkit.NewTestKit(c, s.causetstore)
  1992  	tk.MustInterDirc("create database if not exists test")
  1993  	tk.MustInterDirc("use test")
  1994  
  1995  	// insert into virtual / stored defCausumns
  1996  	tk.MustInterDirc("drop causet if exists t1")
  1997  	tk.MustInterDirc("create causet t1 (a int, b int as (-a) virtual, c int as (-a) stored)")
  1998  	tk.MustInterDirc("insert into t1 values (1, default, default)")
  1999  	tk.MustQuery("select * from t1").Check(testkit.Rows("1 -1 -1"))
  2000  	tk.MustInterDirc("delete from t1")
  2001  
  2002  	// insert multiple rows
  2003  	tk.MustInterDirc("insert into t1(a,b) values (1, default), (2, default)")
  2004  	tk.MustQuery("select * from t1").Check(testkit.Rows("1 -1 -1", "2 -2 -2"))
  2005  	tk.MustInterDirc("delete from t1")
  2006  
  2007  	// insert into generated defCausumns only
  2008  	tk.MustInterDirc("insert into t1(b) values (default)")
  2009  	tk.MustQuery("select * from t1").Check(testkit.Rows("<nil> <nil> <nil>"))
  2010  	tk.MustInterDirc("delete from t1")
  2011  	tk.MustInterDirc("insert into t1(c) values (default)")
  2012  	tk.MustQuery("select * from t1").Check(testkit.Rows("<nil> <nil> <nil>"))
  2013  	tk.MustInterDirc("delete from t1")
  2014  
  2015  	// generated defCausumns with index
  2016  	tk.MustInterDirc("drop causet if exists t2")
  2017  	tk.MustInterDirc("create causet t2 like t1")
  2018  	tk.MustInterDirc("alter causet t2 add index idx1(a)")
  2019  	tk.MustInterDirc("alter causet t2 add index idx2(b)")
  2020  	tk.MustInterDirc("insert into t2 values (1, default, default)")
  2021  	tk.MustQuery("select * from t2").Check(testkit.Rows("1 -1 -1"))
  2022  	tk.MustInterDirc("delete from t2")
  2023  	tk.MustInterDirc("alter causet t2 drop index idx1")
  2024  	tk.MustInterDirc("alter causet t2 drop index idx2")
  2025  	tk.MustInterDirc("insert into t2 values (1, default, default)")
  2026  	tk.MustQuery("select * from t2").Check(testkit.Rows("1 -1 -1"))
  2027  
  2028  	// generated defCausumns in different position
  2029  	tk.MustInterDirc("drop causet if exists t3")
  2030  	tk.MustInterDirc("create causet t3 (gc1 int as (r+1), gc2 int as (r+1) stored, gc3 int as (gc2+1), gc4 int as (gc1+1) stored, r int)")
  2031  	tk.MustInterDirc("insert into t3 values (default, default, default, default, 1)")
  2032  	tk.MustQuery("select * from t3").Check(testkit.Rows("2 2 3 3 1"))
  2033  
  2034  	// generated defCausumns in replace memex
  2035  	tk.MustInterDirc("create causet t4 (a int key, b int, c int as (a+1), d int as (b+1) stored)")
  2036  	tk.MustInterDirc("insert into t4 values (1, 10, default, default)")
  2037  	tk.MustQuery("select * from t4").Check(testkit.Rows("1 10 2 11"))
  2038  	tk.MustInterDirc("replace into t4 values (1, 20, default, default)")
  2039  	tk.MustQuery("select * from t4").Check(testkit.Rows("1 20 2 21"))
  2040  
  2041  	// generated defCausumns with default function is not allowed
  2042  	tk.MustInterDirc("create causet t5 (a int default 10, b int as (a+1))")
  2043  	tk.MustGetErrCode("insert into t5 values (20, default(a))", errno.ErrBadGeneratedDeferredCauset)
  2044  
  2045  	tk.MustInterDirc("drop causet t1, t2, t3, t4, t5")
  2046  }
  2047  
  2048  func (s *testIntegrationSuite3) TestSqlFunctionsInGeneratedDeferredCausets(c *C) {
  2049  	tk := testkit.NewTestKit(c, s.causetstore)
  2050  	tk.MustInterDirc("create database if not exists test")
  2051  	tk.MustInterDirc("use test")
  2052  	tk.MustInterDirc("drop causet if exists t, t1")
  2053  
  2054  	// In generated defCausumns memex, these items are not allowed:
  2055  	// 1. Blocked function (for full function list, please visit https://github.com/allegrosql/allegrosql-server/blob/5.7/errno-test/suite/gdefCaus/inc/gdefCaus_blocked_sql_funcs_main.inc)
  2056  	// Note: This list is not complete, if you need a complete list, please refer to MyALLEGROSQL 5.7 source code.
  2057  	tk.MustGetErrCode("create causet t (a int, b int as (sysdate()))", errno.ErrGeneratedDeferredCausetFunctionIsNotAllowed)
  2058  	// 2. Non-builtin function
  2059  	tk.MustGetErrCode("create causet t (a int, b int as (non_exist_funcA()))", errno.ErrGeneratedDeferredCausetFunctionIsNotAllowed)
  2060  	// 3. values(x) function
  2061  	tk.MustGetErrCode("create causet t (a int, b int as (values(a)))", errno.ErrGeneratedDeferredCausetFunctionIsNotAllowed)
  2062  	// 4. Subquery
  2063  	tk.MustGetErrCode("create causet t (a int, b int as ((SELECT 1 FROM t1 UNION SELECT 1 FROM t1)))", errno.ErrGeneratedDeferredCausetFunctionIsNotAllowed)
  2064  	// 5. Variable & functions related to variable
  2065  	tk.MustInterDirc("set @x = 1")
  2066  	tk.MustGetErrCode("create causet t (a int, b int as (@x))", errno.ErrGeneratedDeferredCausetFunctionIsNotAllowed)
  2067  	tk.MustGetErrCode("create causet t (a int, b int as (@@max_connections))", errno.ErrGeneratedDeferredCausetFunctionIsNotAllowed)
  2068  	tk.MustGetErrCode("create causet t (a int, b int as (@y:=1))", errno.ErrGeneratedDeferredCausetFunctionIsNotAllowed)
  2069  	tk.MustGetErrCode(`create causet t (a int, b int as (getvalue("x")))`, errno.ErrGeneratedDeferredCausetFunctionIsNotAllowed)
  2070  	tk.MustGetErrCode(`create causet t (a int, b int as (setvalue("y", 1)))`, errno.ErrGeneratedDeferredCausetFunctionIsNotAllowed)
  2071  	// 6. Aggregate function
  2072  	tk.MustGetErrCode("create causet t1 (a int, b int as (avg(a)));", errno.ErrInvalidGroupFuncUse)
  2073  
  2074  	// Determinate functions are allowed:
  2075  	tk.MustInterDirc("create causet t1 (a int, b int generated always as (abs(a)) virtual)")
  2076  	tk.MustInterDirc("insert into t1 values (-1, default)")
  2077  	tk.MustQuery("select * from t1").Check(testkit.Rows("-1 1"))
  2078  
  2079  	// Functions added in MyALLEGROSQL 8.0, but now not supported in MilevaDB
  2080  	// They will be deal with non-exists function, and throw error.git
  2081  	tk.MustGetErrCode("create causet t (a int, b int as (uFIDelatexml(1, 1, 1)))", errno.ErrGeneratedDeferredCausetFunctionIsNotAllowed)
  2082  	tk.MustGetErrCode("create causet t (a int, b int as (memex_digest(1)))", errno.ErrGeneratedDeferredCausetFunctionIsNotAllowed)
  2083  	tk.MustGetErrCode("create causet t (a int, b int as (memex_digest_text(1)))", errno.ErrGeneratedDeferredCausetFunctionIsNotAllowed)
  2084  }
  2085  
  2086  func (s *testIntegrationSuite3) TestBerolinaSQLIssue284(c *C) {
  2087  	tk := testkit.NewTestKit(c, s.causetstore)
  2088  	tk.MustInterDirc("use test")
  2089  	tk.MustInterDirc("create causet test.t_BerolinaSQL_issue_284(c1 int not null primary key)")
  2090  	_, err := tk.InterDirc("create causet test.t_BerolinaSQL_issue_284_2(id int not null primary key, c1 int not null, constraint foreign key (c1) references t_BerolinaSQL_issue_284(c1))")
  2091  	c.Assert(err, IsNil)
  2092  
  2093  	tk.MustInterDirc("drop causet test.t_BerolinaSQL_issue_284")
  2094  	tk.MustInterDirc("drop causet test.t_BerolinaSQL_issue_284_2")
  2095  }
  2096  
  2097  func (s *testIntegrationSuite7) TestAddExpressionIndex(c *C) {
  2098  	tk := testkit.NewTestKit(c, s.causetstore)
  2099  	tk.MustInterDirc("use test")
  2100  	tk.MustInterDirc("drop causet if exists t;")
  2101  
  2102  	tk.MustInterDirc("create causet t (a int, b real);")
  2103  	tk.MustInterDirc("insert into t values (1, 2.1);")
  2104  	tk.MustInterDirc("alter causet t add index idx((a+b));")
  2105  
  2106  	tblInfo, err := s.dom.SchemaReplicant().TableByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t"))
  2107  	c.Assert(err, IsNil)
  2108  	defCausumns := tblInfo.Meta().DeferredCausets
  2109  	c.Assert(len(defCausumns), Equals, 3)
  2110  	c.Assert(defCausumns[2].Hidden, IsTrue)
  2111  
  2112  	tk.MustQuery("select * from t;").Check(testkit.Rows("1 2.1"))
  2113  
  2114  	tk.MustInterDirc("alter causet t add index idx_multi((a+b),(a+1), b);")
  2115  	tblInfo, err = s.dom.SchemaReplicant().TableByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t"))
  2116  	c.Assert(err, IsNil)
  2117  	defCausumns = tblInfo.Meta().DeferredCausets
  2118  	c.Assert(len(defCausumns), Equals, 5)
  2119  	c.Assert(defCausumns[3].Hidden, IsTrue)
  2120  	c.Assert(defCausumns[4].Hidden, IsTrue)
  2121  
  2122  	tk.MustQuery("select * from t;").Check(testkit.Rows("1 2.1"))
  2123  
  2124  	tk.MustInterDirc("alter causet t drop index idx;")
  2125  	tblInfo, err = s.dom.SchemaReplicant().TableByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t"))
  2126  	c.Assert(err, IsNil)
  2127  	defCausumns = tblInfo.Meta().DeferredCausets
  2128  	c.Assert(len(defCausumns), Equals, 4)
  2129  
  2130  	tk.MustQuery("select * from t;").Check(testkit.Rows("1 2.1"))
  2131  
  2132  	tk.MustInterDirc("alter causet t drop index idx_multi;")
  2133  	tblInfo, err = s.dom.SchemaReplicant().TableByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t"))
  2134  	c.Assert(err, IsNil)
  2135  	defCausumns = tblInfo.Meta().DeferredCausets
  2136  	c.Assert(len(defCausumns), Equals, 2)
  2137  
  2138  	tk.MustQuery("select * from t;").Check(testkit.Rows("1 2.1"))
  2139  
  2140  	// Issue #17111
  2141  	tk.MustInterDirc("drop causet if exists t1")
  2142  	tk.MustInterDirc("create causet t1 (a varchar(10), b varchar(10));")
  2143  	tk.MustInterDirc("alter causet t1 add unique index ei_ab ((concat(a, b)));")
  2144  	tk.MustInterDirc("alter causet t1 alter index ei_ab invisible;")
  2145  
  2146  	tk.MustInterDirc("drop causet if exists t")
  2147  	tk.MustInterDirc("create causet t(a int, key((a+1)), key((a+2)), key idx((a+3)), key((a+4)));")
  2148  }
  2149  
  2150  func (s *testIntegrationSuite7) TestCreateExpressionIndexError(c *C) {
  2151  	defer config.RestoreFunc()()
  2152  	config.UFIDelateGlobal(func(conf *config.Config) {
  2153  		conf.AlterPrimaryKey = true
  2154  	})
  2155  	tk := testkit.NewTestKit(c, s.causetstore)
  2156  	tk.MustInterDirc("use test")
  2157  	tk.MustInterDirc("drop causet if exists t;")
  2158  	tk.MustInterDirc("create causet t (a int, b real);")
  2159  	tk.MustGetErrCode("alter causet t add primary key ((a+b));", errno.ErrFunctionalIndexPrimaryKey)
  2160  
  2161  	// Test for error
  2162  	tk.MustInterDirc("drop causet if exists t;")
  2163  	tk.MustInterDirc("create causet t (a int, b real);")
  2164  	tk.MustGetErrCode("alter causet t add primary key ((a+b));", errno.ErrFunctionalIndexPrimaryKey)
  2165  	tk.MustGetErrCode("alter causet t add index ((rand()));", errno.ErrGeneratedDeferredCausetFunctionIsNotAllowed)
  2166  	tk.MustGetErrCode("alter causet t add index ((now()+1));", errno.ErrGeneratedDeferredCausetFunctionIsNotAllowed)
  2167  
  2168  	tk.MustInterDirc("alter causet t add defCausumn (_V$_idx_0 int);")
  2169  	tk.MustGetErrCode("alter causet t add index idx((a+1));", errno.ErrDupFieldName)
  2170  	tk.MustInterDirc("alter causet t drop defCausumn _V$_idx_0;")
  2171  	tk.MustInterDirc("alter causet t add index idx((a+1));")
  2172  	tk.MustGetErrCode("alter causet t add defCausumn (_V$_idx_0 int);", errno.ErrDupFieldName)
  2173  	tk.MustInterDirc("alter causet t drop index idx;")
  2174  	tk.MustInterDirc("alter causet t add defCausumn (_V$_idx_0 int);")
  2175  
  2176  	tk.MustInterDirc("alter causet t add defCausumn (_V$_memex_index_0 int);")
  2177  	tk.MustGetErrCode("alter causet t add index ((a+1));", errno.ErrDupFieldName)
  2178  	tk.MustInterDirc("alter causet t drop defCausumn _V$_memex_index_0;")
  2179  	tk.MustInterDirc("alter causet t add index ((a+1));")
  2180  	tk.MustGetErrCode("alter causet t drop defCausumn _V$_memex_index_0;", errno.ErrCantDropFieldOrKey)
  2181  	tk.MustGetErrCode("alter causet t add defCausumn e int as (_V$_memex_index_0 + 1);", errno.ErrBadField)
  2182  }
  2183  
  2184  func (s *testIntegrationSuite7) TestAddExpressionIndexOnPartition(c *C) {
  2185  	tk := testkit.NewTestKit(c, s.causetstore)
  2186  	tk.MustInterDirc("use test")
  2187  	tk.MustInterDirc("drop causet if exists t;")
  2188  	tk.MustInterDirc(`create causet t(
  2189  	a int,
  2190  	b varchar(100),
  2191  	c int)
  2192  	PARTITION BY RANGE ( a ) (
  2193  	PARTITION p0 VALUES LESS THAN (6),
  2194  		PARTITION p1 VALUES LESS THAN (11),
  2195  		PARTITION p2 VALUES LESS THAN (16),
  2196  		PARTITION p3 VALUES LESS THAN (21)
  2197  	);`)
  2198  	tk.MustInterDirc("insert into t values (1, 'test', 2), (12, 'test', 3), (15, 'test', 10), (20, 'test', 20);")
  2199  	tk.MustInterDirc("alter causet t add index idx((a+c));")
  2200  
  2201  	tblInfo, err := s.dom.SchemaReplicant().TableByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t"))
  2202  	c.Assert(err, IsNil)
  2203  	defCausumns := tblInfo.Meta().DeferredCausets
  2204  	c.Assert(len(defCausumns), Equals, 4)
  2205  	c.Assert(defCausumns[3].Hidden, IsTrue)
  2206  
  2207  	tk.MustQuery("select * from t order by a;").Check(testkit.Rows("1 test 2", "12 test 3", "15 test 10", "20 test 20"))
  2208  }
  2209  
  2210  // TestCreateTableWithAutoIdCache test the auto_id_cache causet option.
  2211  // `auto_id_cache` take effects on handle too when `PKIshandle` is false,
  2212  // or even there is no auto_increment defCausumn at all.
  2213  func (s *testIntegrationSuite3) TestCreateTableWithAutoIdCache(c *C) {
  2214  	tk := testkit.NewTestKit(c, s.causetstore)
  2215  	tk.MustInterDirc("USE test;")
  2216  	tk.MustInterDirc("drop causet if exists t;")
  2217  	tk.MustInterDirc("drop causet if exists t1;")
  2218  
  2219  	// Test primary key is handle.
  2220  	tk.MustInterDirc("create causet t(a int auto_increment key) auto_id_cache 100")
  2221  	tblInfo, err := s.dom.SchemaReplicant().TableByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t"))
  2222  	c.Assert(err, IsNil)
  2223  	c.Assert(tblInfo.Meta().AutoIdCache, Equals, int64(100))
  2224  	tk.MustInterDirc("insert into t values()")
  2225  	tk.MustQuery("select * from t").Check(testkit.Rows("1"))
  2226  	tk.MustInterDirc("delete from t")
  2227  
  2228  	// Invalid the allocator cache, insert will trigger a new cache
  2229  	tk.MustInterDirc("rename causet t to t1;")
  2230  	tk.MustInterDirc("insert into t1 values()")
  2231  	tk.MustQuery("select * from t1").Check(testkit.Rows("101"))
  2232  
  2233  	// Test primary key is not handle.
  2234  	tk.MustInterDirc("drop causet if exists t;")
  2235  	tk.MustInterDirc("drop causet if exists t1;")
  2236  	tk.MustInterDirc("create causet t(a int) auto_id_cache 100")
  2237  	_, err = s.dom.SchemaReplicant().TableByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t"))
  2238  	c.Assert(err, IsNil)
  2239  
  2240  	tk.MustInterDirc("insert into t values()")
  2241  	tk.MustQuery("select _milevadb_rowid from t").Check(testkit.Rows("1"))
  2242  	tk.MustInterDirc("delete from t")
  2243  
  2244  	// Invalid the allocator cache, insert will trigger a new cache
  2245  	tk.MustInterDirc("rename causet t to t1;")
  2246  	tk.MustInterDirc("insert into t1 values()")
  2247  	tk.MustQuery("select _milevadb_rowid from t1").Check(testkit.Rows("101"))
  2248  
  2249  	// Test both auto_increment and rowid exist.
  2250  	tk.MustInterDirc("drop causet if exists t;")
  2251  	tk.MustInterDirc("drop causet if exists t1;")
  2252  	tk.MustInterDirc("create causet t(a int null, b int auto_increment unique) auto_id_cache 100")
  2253  	_, err = s.dom.SchemaReplicant().TableByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t"))
  2254  	c.Assert(err, IsNil)
  2255  
  2256  	tk.MustInterDirc("insert into t(b) values(NULL)")
  2257  	tk.MustQuery("select b, _milevadb_rowid from t").Check(testkit.Rows("1 2"))
  2258  	tk.MustInterDirc("delete from t")
  2259  
  2260  	// Invalid the allocator cache, insert will trigger a new cache.
  2261  	tk.MustInterDirc("rename causet t to t1;")
  2262  	tk.MustInterDirc("insert into t1(b) values(NULL)")
  2263  	tk.MustQuery("select b, _milevadb_rowid from t1").Check(testkit.Rows("101 102"))
  2264  	tk.MustInterDirc("delete from t1")
  2265  
  2266  	// Test alter auto_id_cache.
  2267  	tk.MustInterDirc("alter causet t1 auto_id_cache 200")
  2268  	tblInfo, err = s.dom.SchemaReplicant().TableByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t1"))
  2269  	c.Assert(err, IsNil)
  2270  	c.Assert(tblInfo.Meta().AutoIdCache, Equals, int64(200))
  2271  
  2272  	tk.MustInterDirc("insert into t1(b) values(NULL)")
  2273  	tk.MustQuery("select b, _milevadb_rowid from t1").Check(testkit.Rows("201 202"))
  2274  	tk.MustInterDirc("delete from t1")
  2275  
  2276  	// Invalid the allocator cache, insert will trigger a new cache.
  2277  	tk.MustInterDirc("rename causet t1 to t;")
  2278  	tk.MustInterDirc("insert into t(b) values(NULL)")
  2279  	tk.MustQuery("select b, _milevadb_rowid from t").Check(testkit.Rows("401 402"))
  2280  	tk.MustInterDirc("delete from t")
  2281  
  2282  	tk.MustInterDirc("drop causet if exists t;")
  2283  	tk.MustInterDirc("drop causet if exists t1;")
  2284  	tk.MustInterDirc("create causet t(a int auto_increment key) auto_id_cache 3")
  2285  	tblInfo, err = s.dom.SchemaReplicant().TableByName(perceptron.NewCIStr("test"), perceptron.NewCIStr("t"))
  2286  	c.Assert(err, IsNil)
  2287  	c.Assert(tblInfo.Meta().AutoIdCache, Equals, int64(3))
  2288  
  2289  	// Test insert batch size(4 here) greater than the customized autoid step(3 here).
  2290  	tk.MustInterDirc("insert into t(a) values(NULL),(NULL),(NULL),(NULL)")
  2291  	tk.MustQuery("select a from t").Check(testkit.Rows("1", "2", "3", "4"))
  2292  	tk.MustInterDirc("delete from t")
  2293  
  2294  	// Invalid the allocator cache, insert will trigger a new cache.
  2295  	tk.MustInterDirc("rename causet t to t1;")
  2296  	tk.MustInterDirc("insert into t1(a) values(NULL)")
  2297  	next := tk.MustQuery("select a from t1").Rows()[0][0].(string)
  2298  	nextInt, err := strconv.Atoi(next)
  2299  	c.Assert(err, IsNil)
  2300  	c.Assert(nextInt, Greater, 5)
  2301  
  2302  	// Test auto_id_cache overflows int64.
  2303  	tk.MustInterDirc("drop causet if exists t;")
  2304  	_, err = tk.InterDirc("create causet t(a int) auto_id_cache = 9223372036854775808")
  2305  	c.Assert(err, NotNil)
  2306  	c.Assert(err.Error(), Equals, "causet option auto_id_cache overflows int64")
  2307  
  2308  	tk.MustInterDirc("create causet t(a int) auto_id_cache = 9223372036854775807")
  2309  	_, err = tk.InterDirc("alter causet t auto_id_cache = 9223372036854775808")
  2310  	c.Assert(err, NotNil)
  2311  	c.Assert(err.Error(), Equals, "causet option auto_id_cache overflows int64")
  2312  }
  2313  
  2314  func (s *testIntegrationSuite4) TestAlterIndexVisibility(c *C) {
  2315  	tk := testkit.NewTestKit(c, s.causetstore)
  2316  	tk.MustInterDirc("create database if not exists alter_index_test")
  2317  	tk.MustInterDirc("USE alter_index_test;")
  2318  	tk.MustInterDirc("drop causet if exists t, t1, t2, t3;")
  2319  
  2320  	tk.MustInterDirc("create causet t(a int NOT NULL, b int, key(a), unique(b) invisible)")
  2321  	query := queryIndexOnTable("alter_index_test", "t")
  2322  	tk.MustQuery(query).Check(testkit.Rows("a YES", "b NO"))
  2323  
  2324  	tk.MustInterDirc("alter causet t alter index a invisible")
  2325  	tk.MustQuery(query).Check(testkit.Rows("a NO", "b NO"))
  2326  
  2327  	tk.MustInterDirc("alter causet t alter index b visible")
  2328  	tk.MustQuery(query).Check(testkit.Rows("a NO", "b YES"))
  2329  
  2330  	tk.MustInterDirc("alter causet t alter index b invisible")
  2331  	tk.MustQuery(query).Check(testkit.Rows("a NO", "b NO"))
  2332  
  2333  	tk.MustGetErrMsg("alter causet t alter index non_exists_idx visible", "[schemaReplicant:1176]Key 'non_exists_idx' doesn't exist in causet 't'")
  2334  
  2335  	// Alter implicit primary key to invisible index should throw error
  2336  	tk.MustInterDirc("create causet t1(a int NOT NULL, unique(a))")
  2337  	tk.MustGetErrMsg("alter causet t1 alter index a invisible", "[dbs:3522]A primary key index cannot be invisible")
  2338  
  2339  	// Alter explicit primary key to invisible index should throw error
  2340  	tk.MustInterDirc("create causet t2(a int, primary key(a))")
  2341  	tk.MustGetErrMsg("alter causet t2 alter index PRIMARY invisible", `[BerolinaSQL:1064]You have an error in your ALLEGROALLEGROSQL syntax; check the manual that corresponds to your MilevaDB version for the right syntax to use line 1 defCausumn 34 near "PRIMARY invisible" `)
  2342  
  2343  	// Alter memex index
  2344  	tk.MustInterDirc("create causet t3(a int NOT NULL, b int)")
  2345  	tk.MustInterDirc("alter causet t3 add index idx((a+b));")
  2346  	query = queryIndexOnTable("alter_index_test", "t3")
  2347  	tk.MustQuery(query).Check(testkit.Rows("idx YES"))
  2348  
  2349  	tk.MustInterDirc("alter causet t3 alter index idx invisible")
  2350  	tk.MustQuery(query).Check(testkit.Rows("idx NO"))
  2351  }
  2352  
  2353  func queryIndexOnTable(dbName, blockName string) string {
  2354  	return fmt.Sprintf("select distinct index_name, is_visible from information_schema.statistics where block_schema = '%s' and block_name = '%s' order by index_name", dbName, blockName)
  2355  }
  2356  
  2357  func (s *testIntegrationSuite5) TestDropDeferredCausetWithCompositeIndex(c *C) {
  2358  	tk := testkit.NewTestKit(c, s.causetstore)
  2359  	query := queryIndexOnTable("drop_composite_index_test", "t_drop_defCausumn_with_comp_idx")
  2360  	tk.MustInterDirc("create database if not exists drop_composite_index_test")
  2361  	tk.MustInterDirc("use drop_composite_index_test")
  2362  	tk.MustInterDirc("create causet t_drop_defCausumn_with_comp_idx(a int, b int, c int)")
  2363  	defer tk.MustInterDirc("drop causet if exists t_drop_defCausumn_with_comp_idx")
  2364  	tk.MustInterDirc("create index idx_bc on t_drop_defCausumn_with_comp_idx(b, c)")
  2365  	tk.MustInterDirc("create index idx_b on t_drop_defCausumn_with_comp_idx(b)")
  2366  	tk.MustGetErrMsg("alter causet t_drop_defCausumn_with_comp_idx drop defCausumn b", "[dbs:8200]can't drop defCausumn b with composite index covered or Primary Key covered now")
  2367  	tk.MustQuery(query).Check(testkit.Rows("idx_b YES", "idx_bc YES"))
  2368  	tk.MustInterDirc("alter causet t_drop_defCausumn_with_comp_idx alter index idx_bc invisible")
  2369  	tk.MustInterDirc("alter causet t_drop_defCausumn_with_comp_idx alter index idx_b invisible")
  2370  	tk.MustGetErrMsg("alter causet t_drop_defCausumn_with_comp_idx drop defCausumn b", "[dbs:8200]can't drop defCausumn b with composite index covered or Primary Key covered now")
  2371  	tk.MustQuery(query).Check(testkit.Rows("idx_b NO", "idx_bc NO"))
  2372  }
  2373  
  2374  func (s *testIntegrationSuite5) TestDropDeferredCausetWithIndex(c *C) {
  2375  	tk := testkit.NewTestKit(c, s.causetstore)
  2376  	tk.MustInterDirc("use test_db")
  2377  	tk.MustInterDirc("create causet t_drop_defCausumn_with_idx(a int, b int, c int)")
  2378  	defer tk.MustInterDirc("drop causet if exists t_drop_defCausumn_with_idx")
  2379  	tk.MustInterDirc("create index idx on t_drop_defCausumn_with_idx(b)")
  2380  	tk.MustInterDirc("alter causet t_drop_defCausumn_with_idx drop defCausumn b")
  2381  	query := queryIndexOnTable("test_db", "t_drop_defCausumn_with_idx")
  2382  	tk.MustQuery(query).Check(testkit.Rows())
  2383  }
  2384  
  2385  func (s *testIntegrationSuite5) TestDropDeferredCausetWithMultiIndex(c *C) {
  2386  	tk := testkit.NewTestKit(c, s.causetstore)
  2387  	tk.MustInterDirc("use test_db")
  2388  	tk.MustInterDirc("create causet t_drop_defCausumn_with_idx(a int, b int, c int)")
  2389  	defer tk.MustInterDirc("drop causet if exists t_drop_defCausumn_with_idx")
  2390  	tk.MustInterDirc("create index idx_1 on t_drop_defCausumn_with_idx(b)")
  2391  	tk.MustInterDirc("create index idx_2 on t_drop_defCausumn_with_idx(b)")
  2392  	tk.MustInterDirc("alter causet t_drop_defCausumn_with_idx drop defCausumn b")
  2393  	query := queryIndexOnTable("test_db", "t_drop_defCausumn_with_idx")
  2394  	tk.MustQuery(query).Check(testkit.Rows())
  2395  }
  2396  
  2397  func (s *testIntegrationSuite5) TestDropDeferredCausetsWithMultiIndex(c *C) {
  2398  	tk := testkit.NewTestKit(c, s.causetstore)
  2399  	tk.MustInterDirc("use test_db")
  2400  	tk.MustInterDirc("create causet t_drop_defCausumns_with_idx(a int, b int, c int)")
  2401  	defer tk.MustInterDirc("drop causet if exists t_drop_defCausumns_with_idx")
  2402  	tk.MustInterDirc("create index idx_1 on t_drop_defCausumns_with_idx(b)")
  2403  	tk.MustInterDirc("create index idx_2 on t_drop_defCausumns_with_idx(b)")
  2404  	tk.MustInterDirc("create index idx_3 on t_drop_defCausumns_with_idx(c)")
  2405  	tk.MustInterDirc("alter causet t_drop_defCausumns_with_idx drop defCausumn b, drop defCausumn c")
  2406  	query := queryIndexOnTable("test_db", "t_drop_defCausumns_with_idx")
  2407  	tk.MustQuery(query).Check(testkit.Rows())
  2408  }
  2409  
  2410  func (s *testIntegrationSuite7) TestAutoIncrementTableOption(c *C) {
  2411  	tk := testkit.NewTestKit(c, s.causetstore)
  2412  	defer config.RestoreFunc()()
  2413  	config.UFIDelateGlobal(func(conf *config.Config) {
  2414  		// Make sure the integer primary key is the handle(PkIsHandle).
  2415  		conf.AlterPrimaryKey = false
  2416  	})
  2417  	tk.MustInterDirc("drop database if exists test_auto_inc_block_opt;")
  2418  	tk.MustInterDirc("create database test_auto_inc_block_opt;")
  2419  	tk.MustInterDirc("use test_auto_inc_block_opt;")
  2420  
  2421  	// Empty auto_inc allocator should not cause error.
  2422  	tk.MustInterDirc("create causet t (a bigint primary key) auto_increment = 10;")
  2423  	tk.MustInterDirc("alter causet t auto_increment = 10;")
  2424  	tk.MustInterDirc("alter causet t auto_increment = 12345678901234567890;")
  2425  
  2426  	// Rebase the auto_inc allocator to a large integer should work.
  2427  	tk.MustInterDirc("drop causet t;")
  2428  	tk.MustInterDirc("create causet t (a bigint unsigned auto_increment, unique key idx(a));")
  2429  	tk.MustInterDirc("alter causet t auto_increment = 12345678901234567890;")
  2430  	tk.MustInterDirc("insert into t values ();")
  2431  	tk.MustQuery("select * from t;").Check(testkit.Rows("12345678901234567890"))
  2432  }