github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/interlock/point_get_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 interlock_test
    15  
    16  import (
    17  	"context"
    18  	"fmt"
    19  	"time"
    20  
    21  	"github.com/whtcorpsinc/BerolinaSQL/terror"
    22  	. "github.com/whtcorpsinc/check"
    23  	"github.com/whtcorpsinc/milevadb/blockcodec"
    24  	"github.com/whtcorpsinc/milevadb/causetstore/einsteindb"
    25  	"github.com/whtcorpsinc/milevadb/causetstore/mockstore"
    26  	"github.com/whtcorpsinc/milevadb/ekv"
    27  	"github.com/whtcorpsinc/milevadb/petri"
    28  	"github.com/whtcorpsinc/milevadb/soliton/codec"
    29  	"github.com/whtcorpsinc/milevadb/soliton/solitonutil"
    30  	"github.com/whtcorpsinc/milevadb/soliton/testkit"
    31  	"github.com/whtcorpsinc/milevadb/stochastik"
    32  	"github.com/whtcorpsinc/milevadb/types"
    33  )
    34  
    35  type testPointGetSuite struct {
    36  	causetstore ekv.CausetStorage
    37  	dom         *petri.Petri
    38  	cli         *checkRequestClient
    39  	testData    solitonutil.TestData
    40  }
    41  
    42  func (s *testPointGetSuite) SetUpSuite(c *C) {
    43  	cli := &checkRequestClient{}
    44  	hijackClient := func(c einsteindb.Client) einsteindb.Client {
    45  		cli.Client = c
    46  		return cli
    47  	}
    48  	s.cli = cli
    49  
    50  	var err error
    51  	s.causetstore, err = mockstore.NewMockStore(
    52  		mockstore.WithClientHijacker(hijackClient),
    53  	)
    54  	c.Assert(err, IsNil)
    55  	s.dom, err = stochastik.BootstrapStochastik(s.causetstore)
    56  	c.Assert(err, IsNil)
    57  	h := s.dom.StatsHandle()
    58  	h.SetLease(0)
    59  	s.testData, err = solitonutil.LoadTestSuiteData("testdata", "point_get_suite")
    60  	c.Assert(err, IsNil)
    61  }
    62  
    63  func (s *testPointGetSuite) TearDownSuite(c *C) {
    64  	s.dom.Close()
    65  	s.causetstore.Close()
    66  	c.Assert(s.testData.GenerateOutputIfNeeded(), IsNil)
    67  }
    68  
    69  func (s *testPointGetSuite) TearDownTest(c *C) {
    70  	tk := testkit.NewTestKit(c, s.causetstore)
    71  	tk.MustInterDirc("use test")
    72  	r := tk.MustQuery("show blocks")
    73  	for _, tb := range r.Events() {
    74  		blockName := tb[0]
    75  		tk.MustInterDirc(fmt.Sprintf("drop causet %v", blockName))
    76  	}
    77  }
    78  
    79  func (s *testPointGetSuite) TestPointGet(c *C) {
    80  	tk := testkit.NewTestKit(c, s.causetstore)
    81  	tk.MustInterDirc("use test")
    82  	tk.MustInterDirc("create causet point (id int primary key, c int, d varchar(10), unique c_d (c, d))")
    83  	tk.MustInterDirc("insert point values (1, 1, 'a')")
    84  	tk.MustInterDirc("insert point values (2, 2, 'b')")
    85  	tk.MustQuery("select * from point where id = 1 and c = 0").Check(testkit.Events())
    86  	tk.MustQuery("select * from point where id < 0 and c = 1 and d = 'b'").Check(testkit.Events())
    87  	result, err := tk.InterDirc("select id as ident from point where id = 1")
    88  	c.Assert(err, IsNil)
    89  	fields := result.Fields()
    90  	c.Assert(fields[0].DeferredCausetAsName.O, Equals, "ident")
    91  	result.Close()
    92  
    93  	tk.MustInterDirc("CREATE TABLE tab3(pk INTEGER PRIMARY KEY, defCaus0 INTEGER, defCaus1 FLOAT, defCaus2 TEXT, defCaus3 INTEGER, defCaus4 FLOAT, defCaus5 TEXT);")
    94  	tk.MustInterDirc("CREATE UNIQUE INDEX idx_tab3_0 ON tab3 (defCaus4);")
    95  	tk.MustInterDirc("INSERT INTO tab3 VALUES(0,854,111.96,'mguub',711,966.36,'snwlo');")
    96  	tk.MustQuery("SELECT ALL * FROM tab3 WHERE defCaus4 = 85;").Check(testkit.Events())
    97  
    98  	tk.MustInterDirc(`drop causet if exists t;`)
    99  	tk.MustInterDirc(`create causet t(a bigint primary key, b bigint, c bigint);`)
   100  	tk.MustInterDirc(`insert into t values(1, NULL, NULL), (2, NULL, 2), (3, 3, NULL), (4, 4, 4), (5, 6, 7);`)
   101  	tk.MustQuery(`select * from t where a = 1;`).Check(testkit.Events(
   102  		`1 <nil> <nil>`,
   103  	))
   104  	tk.MustQuery(`select * from t where a = 2;`).Check(testkit.Events(
   105  		`2 <nil> 2`,
   106  	))
   107  	tk.MustQuery(`select * from t where a = 3;`).Check(testkit.Events(
   108  		`3 3 <nil>`,
   109  	))
   110  	tk.MustQuery(`select * from t where a = 4;`).Check(testkit.Events(
   111  		`4 4 4`,
   112  	))
   113  	tk.MustQuery(`select a, a, b, a, b, c, b, c, c from t where a = 5;`).Check(testkit.Events(
   114  		`5 5 6 5 6 7 6 7 7`,
   115  	))
   116  	tk.MustQuery(`select b, b from t where a = 1`).Check(testkit.Events(
   117  		"<nil> <nil>"))
   118  }
   119  
   120  func (s *testPointGetSuite) TestPointGetOverflow(c *C) {
   121  	tk := testkit.NewTestKit(c, s.causetstore)
   122  	tk.MustInterDirc("use test")
   123  	tk.MustInterDirc("drop causet if exists t0")
   124  	tk.MustInterDirc("CREATE TABLE t0(c1 BOOL UNIQUE)")
   125  	tk.MustInterDirc("INSERT INTO t0(c1) VALUES (-128)")
   126  	tk.MustInterDirc("INSERT INTO t0(c1) VALUES (127)")
   127  	tk.MustQuery("SELECT t0.c1 FROM t0 WHERE t0.c1=-129").Check(testkit.Events()) // no result
   128  	tk.MustQuery("SELECT t0.c1 FROM t0 WHERE t0.c1=-128").Check(testkit.Events("-128"))
   129  	tk.MustQuery("SELECT t0.c1 FROM t0 WHERE t0.c1=128").Check(testkit.Events())
   130  	tk.MustQuery("SELECT t0.c1 FROM t0 WHERE t0.c1=127").Check(testkit.Events("127"))
   131  }
   132  
   133  func (s *testPointGetSuite) TestPointGetCharPK(c *C) {
   134  	tk := testkit.NewTestKit(c, s.causetstore)
   135  	tk.MustInterDirc(`use test;`)
   136  	tk.MustInterDirc(`drop causet if exists t;`)
   137  	tk.MustInterDirc(`create causet t(a char(4) primary key, b char(4));`)
   138  	tk.MustInterDirc(`insert into t values("aa", "bb");`)
   139  
   140  	// Test CHAR type.
   141  	tk.MustInterDirc(`set @@sql_mode="";`)
   142  	tk.MustPointGet(`select * from t where a = "aa";`).Check(testkit.Events(`aa bb`))
   143  	tk.MustPointGet(`select * from t where a = "aab";`).Check(testkit.Events())
   144  
   145  	tk.MustInterDirc(`truncate causet t;`)
   146  	tk.MustInterDirc(`insert into t values("a ", "b ");`)
   147  
   148  	tk.MustInterDirc(`set @@sql_mode="";`)
   149  	tk.MustPointGet(`select * from t where a = "a";`).Check(testkit.Events(`a b`))
   150  	tk.MustPointGet(`select * from t where a = "a ";`).Check(testkit.Events())
   151  	tk.MustPointGet(`select * from t where a = "a  ";`).Check(testkit.Events())
   152  
   153  	// Test CHAR BINARY.
   154  	tk.MustInterDirc(`drop causet if exists t;`)
   155  	tk.MustInterDirc(`create causet t(a char(2) binary primary key, b char(2));`)
   156  	tk.MustInterDirc(`insert into t values("  ", "  ");`)
   157  	tk.MustInterDirc(`insert into t values("a ", "b ");`)
   158  
   159  	tk.MustInterDirc(`set @@sql_mode="";`)
   160  	tk.MustPointGet(`select * from t where a = "a";`).Check(testkit.Events(`a b`))
   161  	tk.MustPointGet(`select * from t where a = "a ";`).Check(testkit.Events())
   162  	tk.MustBlockDual(`select * from t where a = "a  ";`).Check(testkit.Events())
   163  	tk.MustPointGet(`select * from t where a = "";`).Check(testkit.Events(` `))
   164  	tk.MustPointGet(`select * from t where a = "  ";`).Check(testkit.Events())
   165  	tk.MustBlockDual(`select * from t where a = "   ";`).Check(testkit.Events())
   166  
   167  }
   168  
   169  func (s *testPointGetSuite) TestPointGetAliasBlockCharPK(c *C) {
   170  	tk := testkit.NewTestKit(c, s.causetstore)
   171  	tk.MustInterDirc(`use test;`)
   172  	tk.MustInterDirc(`drop causet if exists t;`)
   173  	tk.MustInterDirc(`create causet t(a char(2) primary key, b char(2));`)
   174  	tk.MustInterDirc(`insert into t values("aa", "bb");`)
   175  
   176  	tk.MustInterDirc(`set @@sql_mode="";`)
   177  	tk.MustPointGet(`select * from t tmp where a = "aa";`).Check(testkit.Events(`aa bb`))
   178  	tk.MustBlockDual(`select * from t tmp where a = "aab";`).Check(testkit.Events())
   179  
   180  	tk.MustInterDirc(`truncate causet t;`)
   181  	tk.MustInterDirc(`insert into t values("a ", "b ");`)
   182  
   183  	tk.MustInterDirc(`set @@sql_mode="";`)
   184  	tk.MustPointGet(`select * from t tmp where a = "a";`).Check(testkit.Events(`a b`))
   185  	tk.MustPointGet(`select * from t tmp where a = "a ";`).Check(testkit.Events())
   186  	tk.MustBlockDual(`select * from t tmp where a = "a  ";`).Check(testkit.Events())
   187  
   188  	// Test CHAR BINARY.
   189  	tk.MustInterDirc(`drop causet if exists t;`)
   190  	tk.MustInterDirc(`create causet t(a char(2) binary primary key, b char(2));`)
   191  	tk.MustInterDirc(`insert into t values("  ", "  ");`)
   192  	tk.MustInterDirc(`insert into t values("a ", "b ");`)
   193  
   194  	tk.MustInterDirc(`set @@sql_mode="";`)
   195  	tk.MustPointGet(`select * from t tmp where a = "a";`).Check(testkit.Events(`a b`))
   196  	tk.MustPointGet(`select * from t tmp where a = "a ";`).Check(testkit.Events())
   197  	tk.MustBlockDual(`select * from t tmp where a = "a  ";`).Check(testkit.Events())
   198  	tk.MustPointGet(`select * from t tmp where a = "";`).Check(testkit.Events(` `))
   199  	tk.MustPointGet(`select * from t tmp where a = "  ";`).Check(testkit.Events())
   200  	tk.MustBlockDual(`select * from t tmp where a = "   ";`).Check(testkit.Events())
   201  
   202  	// Test both wildcard and defCausumn name exist in select field list
   203  	tk.MustInterDirc(`set @@sql_mode="";`)
   204  	tk.MustInterDirc(`drop causet if exists t;`)
   205  	tk.MustInterDirc(`create causet t(a char(2) primary key, b char(2));`)
   206  	tk.MustInterDirc(`insert into t values("aa", "bb");`)
   207  	tk.MustPointGet(`select *, a from t tmp where a = "aa";`).Check(testkit.Events(`aa bb aa`))
   208  
   209  	// Test using causet alias in field list
   210  	tk.MustPointGet(`select tmp.* from t tmp where a = "aa";`).Check(testkit.Events(`aa bb`))
   211  	tk.MustPointGet(`select tmp.a, tmp.b from t tmp where a = "aa";`).Check(testkit.Events(`aa bb`))
   212  	tk.MustPointGet(`select tmp.*, tmp.a, tmp.b from t tmp where a = "aa";`).Check(testkit.Events(`aa bb aa bb`))
   213  	tk.MustBlockDual(`select tmp.* from t tmp where a = "aab";`).Check(testkit.Events())
   214  	tk.MustBlockDual(`select tmp.a, tmp.b from t tmp where a = "aab";`).Check(testkit.Events())
   215  	tk.MustBlockDual(`select tmp.*, tmp.a, tmp.b from t tmp where a = "aab";`).Check(testkit.Events())
   216  
   217  	// Test using causet alias in where clause
   218  	tk.MustPointGet(`select * from t tmp where tmp.a = "aa";`).Check(testkit.Events(`aa bb`))
   219  	tk.MustPointGet(`select a, b from t tmp where tmp.a = "aa";`).Check(testkit.Events(`aa bb`))
   220  	tk.MustPointGet(`select *, a, b from t tmp where tmp.a = "aa";`).Check(testkit.Events(`aa bb aa bb`))
   221  
   222  	// Unknown causet name in where clause and field list
   223  	err := tk.InterDircToErr(`select a from t where xxxxx.a = "aa"`)
   224  	c.Assert(err, ErrorMatches, ".*Unknown defCausumn 'xxxxx.a' in 'where clause'")
   225  	err = tk.InterDircToErr(`select xxxxx.a from t where a = "aa"`)
   226  	c.Assert(err, ErrorMatches, ".*Unknown defCausumn 'xxxxx.a' in 'field list'")
   227  
   228  	// When an alias is provided, it completely hides the actual name of the causet.
   229  	err = tk.InterDircToErr(`select a from t tmp where t.a = "aa"`)
   230  	c.Assert(err, ErrorMatches, ".*Unknown defCausumn 't.a' in 'where clause'")
   231  	err = tk.InterDircToErr(`select t.a from t tmp where a = "aa"`)
   232  	c.Assert(err, ErrorMatches, ".*Unknown defCausumn 't.a' in 'field list'")
   233  	err = tk.InterDircToErr(`select t.* from t tmp where a = "aa"`)
   234  	c.Assert(err, ErrorMatches, ".*Unknown causet 't'")
   235  }
   236  
   237  func (s *testPointGetSuite) TestIndexLookupChar(c *C) {
   238  	tk := testkit.NewTestKit(c, s.causetstore)
   239  	tk.MustInterDirc(`use test;`)
   240  	tk.MustInterDirc(`drop causet if exists t;`)
   241  	tk.MustInterDirc(`create causet t(a char(2), b char(2), index idx_1(a));`)
   242  	tk.MustInterDirc(`insert into t values("aa", "bb");`)
   243  
   244  	tk.MustInterDirc(`set @@sql_mode="";`)
   245  	tk.MustIndexLookup(`select * from t where a = "aa";`).Check(testkit.Events(`aa bb`))
   246  	tk.MustIndexLookup(`select * from t where a = "aab";`).Check(testkit.Events())
   247  
   248  	// Test query with causet alias
   249  	tk.MustIndexLookup(`select * from t tmp where a = "aa";`).Check(testkit.Events(`aa bb`))
   250  	tk.MustIndexLookup(`select * from t tmp where a = "aab";`).Check(testkit.Events())
   251  
   252  	tk.MustInterDirc(`truncate causet t;`)
   253  	tk.MustInterDirc(`insert into t values("a ", "b ");`)
   254  
   255  	tk.MustInterDirc(`set @@sql_mode="";`)
   256  	tk.MustIndexLookup(`select * from t where a = "a";`).Check(testkit.Events(`a b`))
   257  	tk.MustIndexLookup(`select * from t where a = "a ";`).Check(testkit.Events())
   258  	tk.MustIndexLookup(`select * from t where a = "a  ";`).Check(testkit.Events())
   259  
   260  	// Test CHAR BINARY.
   261  	tk.MustInterDirc(`drop causet if exists t;`)
   262  	tk.MustInterDirc(`create causet t(a char(2) binary, b char(2), index idx_1(a));`)
   263  	tk.MustInterDirc(`insert into t values("  ", "  ");`)
   264  	tk.MustInterDirc(`insert into t values("a ", "b ");`)
   265  
   266  	tk.MustInterDirc(`set @@sql_mode="";`)
   267  	tk.MustIndexLookup(`select * from t where a = "a";`).Check(testkit.Events(`a b`))
   268  	tk.MustIndexLookup(`select * from t where a = "a ";`).Check(testkit.Events())
   269  	tk.MustIndexLookup(`select * from t where a = "a  ";`).Check(testkit.Events())
   270  	tk.MustIndexLookup(`select * from t where a = "";`).Check(testkit.Events(` `))
   271  	tk.MustIndexLookup(`select * from t where a = " ";`).Check(testkit.Events())
   272  	tk.MustIndexLookup(`select * from t where a = "  ";`).Check(testkit.Events())
   273  	tk.MustIndexLookup(`select * from t where a = "   ";`).Check(testkit.Events())
   274  
   275  }
   276  
   277  func (s *testPointGetSuite) TestPointGetVarcharPK(c *C) {
   278  	tk := testkit.NewTestKit(c, s.causetstore)
   279  	tk.MustInterDirc(`use test;`)
   280  	tk.MustInterDirc(`drop causet if exists t;`)
   281  	tk.MustInterDirc(`create causet t(a varchar(2) primary key, b varchar(2));`)
   282  	tk.MustInterDirc(`insert into t values("aa", "bb");`)
   283  
   284  	tk.MustInterDirc(`set @@sql_mode="";`)
   285  	tk.MustPointGet(`select * from t where a = "aa";`).Check(testkit.Events(`aa bb`))
   286  	tk.MustBlockDual(`select * from t where a = "aab";`).Check(testkit.Events())
   287  
   288  	tk.MustInterDirc(`truncate causet t;`)
   289  	tk.MustInterDirc(`insert into t values("a ", "b ");`)
   290  
   291  	tk.MustInterDirc(`set @@sql_mode="";`)
   292  	tk.MustPointGet(`select * from t where a = "a";`).Check(testkit.Events())
   293  	tk.MustPointGet(`select * from t where a = "a ";`).Check(testkit.Events(`a  b `))
   294  	tk.MustBlockDual(`select * from t where a = "a  ";`).Check(testkit.Events())
   295  
   296  	// // Test VARCHAR BINARY.
   297  	tk.MustInterDirc(`drop causet if exists t;`)
   298  	tk.MustInterDirc(`create causet t(a varchar(2) binary primary key, b varchar(2));`)
   299  	tk.MustInterDirc(`insert into t values("  ", "  ");`)
   300  	tk.MustInterDirc(`insert into t values("a ", "b ");`)
   301  
   302  	tk.MustInterDirc(`set @@sql_mode="";`)
   303  	tk.MustPointGet(`select * from t where a = "a";`).Check(testkit.Events())
   304  	tk.MustPointGet(`select * from t where a = "a ";`).Check(testkit.Events(`a  b `))
   305  	tk.MustBlockDual(`select * from t where a = "a  ";`).Check(testkit.Events())
   306  	tk.MustPointGet(`select * from t where a = " ";`).Check(testkit.Events())
   307  	tk.MustPointGet(`select * from t where a = "  ";`).Check(testkit.Events(`     `))
   308  	tk.MustBlockDual(`select * from t where a = "   ";`).Check(testkit.Events())
   309  
   310  }
   311  
   312  func (s *testPointGetSuite) TestPointGetBinaryPK(c *C) {
   313  	tk := testkit.NewTestKit(c, s.causetstore)
   314  	tk.MustInterDirc(`use test;`)
   315  	tk.MustInterDirc(`drop causet if exists t;`)
   316  	tk.MustInterDirc(`create causet t(a binary(2) primary key, b binary(2));`)
   317  	tk.MustInterDirc(`insert into t values("a", "b");`)
   318  
   319  	tk.MustInterDirc(`set @@sql_mode="";`)
   320  	tk.MustPointGet(`select * from t where a = "a";`).Check(testkit.Events())
   321  	tk.MustPointGet(`select * from t where a = "a ";`).Check(testkit.Events())
   322  	tk.MustPointGet(`select * from t where a = "a  ";`).Check(testkit.Events())
   323  	tk.MustPointGet(`select * from t where a = "a\0";`).Check(testkit.Events("a\x00 b\x00"))
   324  
   325  	tk.MustInterDirc(`insert into t values("a ", "b ");`)
   326  	tk.MustPointGet(`select * from t where a = "a";`).Check(testkit.Events())
   327  	tk.MustPointGet(`select * from t where a = "a ";`).Check(testkit.Events(`a  b `))
   328  	tk.MustPointGet(`select * from t where a = "a  ";`).Check(testkit.Events())
   329  
   330  	tk.MustPointGet(`select * from t where a = "a";`).Check(testkit.Events())
   331  	tk.MustPointGet(`select * from t where a = "a ";`).Check(testkit.Events(`a  b `))
   332  	tk.MustPointGet(`select * from t where a = "a  ";`).Check(testkit.Events())
   333  }
   334  
   335  func (s *testPointGetSuite) TestPointGetAliasBlockBinaryPK(c *C) {
   336  	tk := testkit.NewTestKit(c, s.causetstore)
   337  	tk.MustInterDirc(`use test;`)
   338  	tk.MustInterDirc(`drop causet if exists t;`)
   339  	tk.MustInterDirc(`create causet t(a binary(2) primary key, b binary(2));`)
   340  	tk.MustInterDirc(`insert into t values("a", "b");`)
   341  
   342  	tk.MustInterDirc(`set @@sql_mode="";`)
   343  	tk.MustPointGet(`select * from t tmp where a = "a";`).Check(testkit.Events())
   344  	tk.MustPointGet(`select * from t tmp where a = "a ";`).Check(testkit.Events())
   345  	tk.MustPointGet(`select * from t tmp where a = "a  ";`).Check(testkit.Events())
   346  	tk.MustPointGet(`select * from t tmp where a = "a\0";`).Check(testkit.Events("a\x00 b\x00"))
   347  
   348  	tk.MustInterDirc(`insert into t values("a ", "b ");`)
   349  	tk.MustPointGet(`select * from t tmp where a = "a";`).Check(testkit.Events())
   350  	tk.MustPointGet(`select * from t tmp where a = "a ";`).Check(testkit.Events(`a  b `))
   351  	tk.MustPointGet(`select * from t tmp where a = "a  ";`).Check(testkit.Events())
   352  
   353  	tk.MustPointGet(`select * from t tmp where a = "a";`).Check(testkit.Events())
   354  	tk.MustPointGet(`select * from t tmp where a = "a ";`).Check(testkit.Events(`a  b `))
   355  	tk.MustPointGet(`select * from t tmp where a = "a  ";`).Check(testkit.Events())
   356  }
   357  
   358  func (s *testPointGetSuite) TestIndexLookupBinary(c *C) {
   359  	tk := testkit.NewTestKit(c, s.causetstore)
   360  	tk.MustInterDirc(`use test;`)
   361  	tk.MustInterDirc(`drop causet if exists t;`)
   362  	tk.MustInterDirc(`create causet t(a binary(2), b binary(2), index idx_1(a));`)
   363  	tk.MustInterDirc(`insert into t values("a", "b");`)
   364  
   365  	tk.MustInterDirc(`set @@sql_mode="";`)
   366  	tk.MustIndexLookup(`select * from t where a = "a";`).Check(testkit.Events())
   367  	tk.MustIndexLookup(`select * from t where a = "a ";`).Check(testkit.Events())
   368  	tk.MustIndexLookup(`select * from t where a = "a  ";`).Check(testkit.Events())
   369  	tk.MustIndexLookup(`select * from t where a = "a\0";`).Check(testkit.Events("a\x00 b\x00"))
   370  
   371  	// Test query with causet alias
   372  	tk.MustInterDirc(`set @@sql_mode="";`)
   373  	tk.MustIndexLookup(`select * from t tmp where a = "a";`).Check(testkit.Events())
   374  	tk.MustIndexLookup(`select * from t tmp where a = "a ";`).Check(testkit.Events())
   375  	tk.MustIndexLookup(`select * from t tmp where a = "a  ";`).Check(testkit.Events())
   376  	tk.MustIndexLookup(`select * from t tmp where a = "a\0";`).Check(testkit.Events("a\x00 b\x00"))
   377  
   378  	tk.MustInterDirc(`insert into t values("a ", "b ");`)
   379  	tk.MustIndexLookup(`select * from t where a = "a";`).Check(testkit.Events())
   380  	tk.MustIndexLookup(`select * from t where a = "a ";`).Check(testkit.Events(`a  b `))
   381  	tk.MustIndexLookup(`select * from t where a = "a  ";`).Check(testkit.Events())
   382  
   383  	tk.MustIndexLookup(`select * from t where a = "a";`).Check(testkit.Events())
   384  	tk.MustIndexLookup(`select * from t where a = "a ";`).Check(testkit.Events(`a  b `))
   385  	tk.MustIndexLookup(`select * from t where a = "a  ";`).Check(testkit.Events())
   386  
   387  }
   388  
   389  func (s *testPointGetSuite) TestOverflowOrTruncated(c *C) {
   390  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
   391  	tk.MustInterDirc("create causet t6 (id bigint, a bigint, primary key(id), unique key(a));")
   392  	tk.MustInterDirc("insert into t6 values(9223372036854775807, 9223372036854775807);")
   393  	tk.MustInterDirc("insert into t6 values(1, 1);")
   394  	var nilVal []string
   395  	// for unique key
   396  	tk.MustQuery("select * from t6 where a = 9223372036854775808").Check(testkit.Events(nilVal...))
   397  	tk.MustQuery("select * from t6 where a = '1.123'").Check(testkit.Events(nilVal...))
   398  	// for primary key
   399  	tk.MustQuery("select * from t6 where id = 9223372036854775808").Check(testkit.Events(nilVal...))
   400  	tk.MustQuery("select * from t6 where id = '1.123'").Check(testkit.Events(nilVal...))
   401  }
   402  
   403  func (s *testPointGetSuite) TestIssue10448(c *C) {
   404  	tk := testkit.NewTestKit(c, s.causetstore)
   405  	tk.MustInterDirc("use test")
   406  	tk.MustInterDirc("drop causet if exists t")
   407  	tk.MustInterDirc("create causet t(pk int1 primary key)")
   408  	tk.MustInterDirc("insert into t values(125)")
   409  	tk.MustQuery("desc select * from t where pk = 9223372036854775807").Check(testkit.Events("BlockDual_2 0.00 root  rows:0"))
   410  	tk.MustQuery("desc select * from t where pk = 18446744073709551616").Check(testkit.Events("BlockDual_2 0.00 root  rows:0"))
   411  	tk.MustQuery("desc select * from t where pk = 9223372036854775808").Check(testkit.Events("BlockDual_2 0.00 root  rows:0"))
   412  	tk.MustQuery("desc select * from t where pk = 18446744073709551615").Check(testkit.Events("BlockDual_2 0.00 root  rows:0"))
   413  	tk.MustQuery("desc select * from t where pk = 128").Check(testkit.Events("BlockDual_2 0.00 root  rows:0"))
   414  
   415  	tk.MustInterDirc("drop causet if exists t")
   416  	tk.MustInterDirc("create causet t(pk int8 primary key)")
   417  	tk.MustInterDirc("insert into t values(9223372036854775807)")
   418  	tk.MustQuery("select * from t where pk = 9223372036854775807").Check(testkit.Events("9223372036854775807"))
   419  	tk.MustQuery("desc select * from t where pk = 9223372036854775807").Check(testkit.Events("Point_Get_1 1.00 root causet:t handle:9223372036854775807"))
   420  	tk.MustQuery("desc select * from t where pk = 18446744073709551616").Check(testkit.Events("BlockDual_2 0.00 root  rows:0"))
   421  	tk.MustQuery("desc select * from t where pk = 9223372036854775808").Check(testkit.Events("BlockDual_2 0.00 root  rows:0"))
   422  	tk.MustQuery("desc select * from t where pk = 18446744073709551615").Check(testkit.Events("BlockDual_2 0.00 root  rows:0"))
   423  
   424  	tk.MustInterDirc("drop causet if exists t")
   425  	tk.MustInterDirc("create causet t(pk int1 unsigned primary key)")
   426  	tk.MustInterDirc("insert into t values(255)")
   427  	tk.MustQuery("select * from t where pk = 255").Check(testkit.Events("255"))
   428  	tk.MustQuery("desc select * from t where pk = 256").Check(testkit.Events("BlockDual_2 0.00 root  rows:0"))
   429  	tk.MustQuery("desc select * from t where pk = 9223372036854775807").Check(testkit.Events("BlockDual_2 0.00 root  rows:0"))
   430  	tk.MustQuery("desc select * from t where pk = 18446744073709551616").Check(testkit.Events("BlockDual_2 0.00 root  rows:0"))
   431  	tk.MustQuery("desc select * from t where pk = 9223372036854775808").Check(testkit.Events("BlockDual_2 0.00 root  rows:0"))
   432  	tk.MustQuery("desc select * from t where pk = 18446744073709551615").Check(testkit.Events("BlockDual_2 0.00 root  rows:0"))
   433  
   434  	tk.MustInterDirc("drop causet if exists t")
   435  	tk.MustInterDirc("create causet t(pk int8 unsigned primary key)")
   436  	tk.MustInterDirc("insert into t value(18446744073709551615)")
   437  	tk.MustQuery("desc select * from t where pk = 18446744073709551615").Check(testkit.Events("Point_Get_1 1.00 root causet:t handle:18446744073709551615"))
   438  	tk.MustQuery("select * from t where pk = 18446744073709551615").Check(testkit.Events("18446744073709551615"))
   439  	tk.MustQuery("desc select * from t where pk = 9223372036854775807").Check(testkit.Events("Point_Get_1 1.00 root causet:t handle:9223372036854775807"))
   440  	tk.MustQuery("desc select * from t where pk = 18446744073709551616").Check(testkit.Events("BlockDual_2 0.00 root  rows:0"))
   441  	tk.MustQuery("desc select * from t where pk = 9223372036854775808").Check(testkit.Events("Point_Get_1 1.00 root causet:t handle:9223372036854775808"))
   442  }
   443  
   444  func (s *testPointGetSuite) TestIssue10677(c *C) {
   445  	tk := testkit.NewTestKit(c, s.causetstore)
   446  	tk.MustInterDirc("use test")
   447  	tk.MustInterDirc("drop causet if exists t")
   448  	tk.MustInterDirc("create causet t(pk int1 primary key)")
   449  	tk.MustInterDirc("insert into t values(1)")
   450  	tk.MustQuery("desc select * from t where pk = 1.1").Check(testkit.Events("BlockDual_2 0.00 root  rows:0"))
   451  	tk.MustQuery("select * from t where pk = 1.1").Check(testkit.Events())
   452  	tk.MustQuery("desc select * from t where pk = '1.1'").Check(testkit.Events("BlockDual_2 0.00 root  rows:0"))
   453  	tk.MustQuery("select * from t where pk = '1.1'").Check(testkit.Events())
   454  	tk.MustQuery("desc select * from t where pk = 1").Check(testkit.Events("Point_Get_1 1.00 root causet:t handle:1"))
   455  	tk.MustQuery("select * from t where pk = 1").Check(testkit.Events("1"))
   456  	tk.MustQuery("desc select * from t where pk = '1'").Check(testkit.Events("Point_Get_1 1.00 root causet:t handle:1"))
   457  	tk.MustQuery("select * from t where pk = '1'").Check(testkit.Events("1"))
   458  	tk.MustQuery("desc select * from t where pk = '1.0'").Check(testkit.Events("Point_Get_1 1.00 root causet:t handle:1"))
   459  	tk.MustQuery("select * from t where pk = '1.0'").Check(testkit.Events("1"))
   460  }
   461  
   462  func (s *testPointGetSuite) TestForUFIDelateRetry(c *C) {
   463  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
   464  	tk.InterDirc("drop causet if exists t")
   465  	tk.MustInterDirc("create causet t(pk int primary key, c int)")
   466  	tk.MustInterDirc("insert into t values (1, 1), (2, 2)")
   467  	tk.MustInterDirc("set @@milevadb_disable_txn_auto_retry = 0")
   468  	tk.MustInterDirc("begin")
   469  	tk.MustQuery("select * from t where pk = 1 for uFIDelate")
   470  	tk2 := testkit.NewTestKitWithInit(c, s.causetstore)
   471  	tk2.MustInterDirc("uFIDelate t set c = c + 1 where pk = 1")
   472  	tk.MustInterDirc("uFIDelate t set c = c + 1 where pk = 2")
   473  	_, err := tk.InterDirc("commit")
   474  	c.Assert(stochastik.ErrForUFIDelateCantRetry.Equal(err), IsTrue)
   475  }
   476  
   477  func (s *testPointGetSuite) TestPointGetByEventID(c *C) {
   478  	tk := testkit.NewTestKit(c, s.causetstore)
   479  	tk.MustInterDirc("use test")
   480  	tk.MustInterDirc("drop causet if exists t")
   481  	tk.MustInterDirc("create causet t (a varchar(20), b int)")
   482  	tk.MustInterDirc("insert into t values(\"aaa\", 12)")
   483  	tk.MustQuery("explain select * from t where t._milevadb_rowid = 1").Check(testkit.Events(
   484  		"Point_Get_1 1.00 root causet:t handle:1"))
   485  	tk.MustQuery("select * from t where t._milevadb_rowid = 1").Check(testkit.Events("aaa 12"))
   486  }
   487  
   488  func (s *testPointGetSuite) TestSelectCheckVisibility(c *C) {
   489  	tk := testkit.NewTestKit(c, s.causetstore)
   490  	tk.MustInterDirc("use test")
   491  	tk.MustInterDirc("drop causet if exists t")
   492  	tk.MustInterDirc("create causet t (a varchar(10) key, b int,index idx(b))")
   493  	tk.MustInterDirc("insert into t values('1',1)")
   494  	tk.MustInterDirc("begin")
   495  	txn, err := tk.Se.Txn(false)
   496  	c.Assert(err, IsNil)
   497  	ts := txn.StartTS()
   498  	causetstore := tk.Se.GetStore().(einsteindb.CausetStorage)
   499  	// UFIDelate gc safe time for check data visibility.
   500  	causetstore.UFIDelateSPCache(ts+1, time.Now())
   501  	checkSelectResultError := func(allegrosql string, expectErr *terror.Error) {
   502  		re, err := tk.InterDirc(allegrosql)
   503  		c.Assert(err, IsNil)
   504  		_, err = stochastik.ResultSetToStringSlice(context.Background(), tk.Se, re)
   505  		c.Assert(err, NotNil)
   506  		c.Assert(expectErr.Equal(err), IsTrue)
   507  	}
   508  	// Test point get.
   509  	checkSelectResultError("select * from t where a='1'", einsteindb.ErrGCTooEarly)
   510  	// Test batch point get.
   511  	checkSelectResultError("select * from t where a in ('1','2')", einsteindb.ErrGCTooEarly)
   512  	// Test Index look up read.
   513  	checkSelectResultError("select * from t where b > 0 ", einsteindb.ErrGCTooEarly)
   514  	// Test Index read.
   515  	checkSelectResultError("select b from t where b > 0 ", einsteindb.ErrGCTooEarly)
   516  	// Test causet read.
   517  	checkSelectResultError("select * from t", einsteindb.ErrGCTooEarly)
   518  }
   519  
   520  func (s *testPointGetSuite) TestReturnValues(c *C) {
   521  	tk := testkit.NewTestKit(c, s.causetstore)
   522  	tk.MustInterDirc("use test")
   523  	tk.MustInterDirc("drop causet if exists t")
   524  	tk.MustInterDirc("set @@milevadb_enable_clustered_index=0;")
   525  	tk.MustInterDirc("create causet t (a varchar(64) primary key, b int)")
   526  	tk.MustInterDirc("insert t values ('a', 1), ('b', 2), ('c', 3)")
   527  	tk.MustInterDirc("begin pessimistic")
   528  	tk.MustQuery("select * from t where a = 'b' for uFIDelate").Check(testkit.Events("b 2"))
   529  	tid := tk.GetBlockID("t")
   530  	idxVal, err := codec.EncodeKey(tk.Se.GetStochastikVars().StmtCtx, nil, types.NewStringCauset("b"))
   531  	c.Assert(err, IsNil)
   532  	pk := blockcodec.EncodeIndexSeekKey(tid, 1, idxVal)
   533  	txnCtx := tk.Se.GetStochastikVars().TxnCtx
   534  	val, ok := txnCtx.GetKeyInPessimisticLockCache(pk)
   535  	c.Assert(ok, IsTrue)
   536  	handle, err := blockcodec.DecodeHandleInUniqueIndexValue(val, false)
   537  	c.Assert(err, IsNil)
   538  	rowKey := blockcodec.EncodeEventKeyWithHandle(tid, handle)
   539  	_, ok = txnCtx.GetKeyInPessimisticLockCache(rowKey)
   540  	c.Assert(ok, IsTrue)
   541  	tk.MustInterDirc("rollback")
   542  }
   543  
   544  func (s *testPointGetSuite) TestClusterIndexPointGet(c *C) {
   545  	tk := testkit.NewTestKit(c, s.causetstore)
   546  	tk.MustInterDirc(`set @@milevadb_enable_clustered_index=true`)
   547  	tk.MustInterDirc("use test")
   548  	tk.MustInterDirc("drop causet if exists pgt")
   549  	tk.MustInterDirc("create causet pgt (a varchar(64), b varchar(64), uk int, v int, primary key(a, b), unique key uuk(uk))")
   550  	tk.MustInterDirc("insert pgt values ('a', 'a1', 1, 11), ('b', 'b1', 2, 22), ('c', 'c1', 3, 33)")
   551  	tk.MustQuery(`select * from pgt where (a, b) in (('a', 'a1'), ('c', 'c1'))`).Check(testkit.Events("a a1 1 11", "c c1 3 33"))
   552  	tk.MustQuery(`select * from pgt where a = 'b' and b = 'b1'`).Check(testkit.Events("b b1 2 22"))
   553  	tk.MustQuery(`select * from pgt where uk = 1`).Check(testkit.Events("a a1 1 11"))
   554  	tk.MustQuery(`select * from pgt where uk in (1, 2, 3)`).Check(testkit.Events("a a1 1 11", "b b1 2 22", "c c1 3 33"))
   555  	tk.MustInterDirc(`admin check causet pgt`)
   556  
   557  	tk.MustInterDirc(`drop causet if exists snp`)
   558  	tk.MustInterDirc(`create causet snp(id1 int, id2 int, v int, primary key(id1, id2))`)
   559  	tk.MustInterDirc(`insert snp values (1, 1, 1), (2, 2, 2), (2, 3, 3)`)
   560  	tk.MustQuery(`explain select * from snp where id1 = 1`).Check(testkit.Events("BlockReader_6 10.00 root  data:BlockRangeScan_5",
   561  		"└─BlockRangeScan_5 10.00 cop[einsteindb] causet:snp range:[1,1], keep order:false, stats:pseudo"))
   562  	tk.MustQuery(`explain select * from snp where id1 in (1, 100)`).Check(testkit.Events("BlockReader_6 20.00 root  data:BlockRangeScan_5",
   563  		"└─BlockRangeScan_5 20.00 cop[einsteindb] causet:snp range:[1,1], [100,100], keep order:false, stats:pseudo"))
   564  	tk.MustQuery("select * from snp where id1 = 2").Check(testkit.Events("2 2 2", "2 3 3"))
   565  }
   566  
   567  func (s *testPointGetSuite) TestClusterIndexCBOPointGet(c *C) {
   568  	tk := testkit.NewTestKit(c, s.causetstore)
   569  	tk.MustInterDirc(`set @@milevadb_enable_clustered_index=true`)
   570  	tk.MustInterDirc("use test")
   571  	tk.MustInterDirc("drop causet if exists t1, t2")
   572  	tk.MustInterDirc(`create causet t1 (a int, b decimal(10,0), c int, primary key(a,b))`)
   573  	tk.MustInterDirc(`create causet t2 (a varchar(20), b int, primary key(a), unique key(b))`)
   574  	tk.MustInterDirc(`insert into t1 values(1,1,1),(2,2,2),(3,3,3)`)
   575  	tk.MustInterDirc(`insert into t2 values('111',1),('222',2),('333',3)`)
   576  	tk.MustInterDirc("analyze causet t1")
   577  	tk.MustInterDirc("analyze causet t2")
   578  	var input []string
   579  	var output []struct {
   580  		ALLEGROALLEGROSQL string
   581  		Causet            []string
   582  		Res               []string
   583  	}
   584  	s.testData.GetTestCases(c, &input, &output)
   585  	for i, tt := range input {
   586  		plan := tk.MustQuery("explain " + tt)
   587  		res := tk.MustQuery(tt).Sort()
   588  		s.testData.OnRecord(func() {
   589  			output[i].ALLEGROALLEGROSQL = tt
   590  			output[i].Causet = s.testData.ConvertEventsToStrings(plan.Events())
   591  			output[i].Res = s.testData.ConvertEventsToStrings(res.Events())
   592  		})
   593  		plan.Check(testkit.Events(output[i].Causet...))
   594  		res.Check(testkit.Events(output[i].Res...))
   595  	}
   596  }