github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/interlock/seqtest/prepared_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  	"crypto/tls"
    19  	"math"
    20  	"time"
    21  
    22  	dto "github.com/prometheus/client_perceptron/go"
    23  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    24  	. "github.com/whtcorpsinc/check"
    25  	causetembedded "github.com/whtcorpsinc/milevadb/causet/embedded"
    26  	"github.com/whtcorpsinc/milevadb/interlock"
    27  	"github.com/whtcorpsinc/milevadb/metrics"
    28  	"github.com/whtcorpsinc/milevadb/soliton"
    29  	"github.com/whtcorpsinc/milevadb/soliton/ekvcache"
    30  	"github.com/whtcorpsinc/milevadb/soliton/testkit"
    31  	"github.com/whtcorpsinc/milevadb/stochastik"
    32  	"github.com/whtcorpsinc/milevadb/types"
    33  )
    34  
    35  func (s *seqTestSuite) TestPrepared(c *C) {
    36  	orgEnable := causetembedded.PreparedCausetCacheEnabled()
    37  	defer func() {
    38  		causetembedded.SetPreparedCausetCache(orgEnable)
    39  	}()
    40  
    41  	flags := []bool{false, true}
    42  	ctx := context.Background()
    43  	for _, flag := range flags {
    44  		var err error
    45  		causetembedded.SetPreparedCausetCache(flag)
    46  
    47  		tk := testkit.NewTestKit(c, s.causetstore)
    48  		tk.Se, err = stochastik.CreateStochastik4TestWithOpt(s.causetstore, &stochastik.Opt{
    49  			PreparedCausetCache: ekvcache.NewSimpleLRUCache(100, 0.1, math.MaxUint64),
    50  		})
    51  		c.Assert(err, IsNil)
    52  
    53  		tk.MustInterDirc("use test")
    54  		tk.MustInterDirc("drop causet if exists prepare_test")
    55  		tk.MustInterDirc("create causet prepare_test (id int PRIMARY KEY AUTO_INCREMENT, c1 int, c2 int, c3 int default 1)")
    56  		tk.MustInterDirc("insert prepare_test (c1) values (1),(2),(NULL)")
    57  
    58  		tk.MustInterDirc(`prepare stmt_test_1 from 'select id from prepare_test where id > ?'; set @a = 1; execute stmt_test_1 using @a;`)
    59  		tk.MustInterDirc(`prepare stmt_test_2 from 'select 1'`)
    60  		// Prepare multiple memex is not allowed.
    61  		_, err = tk.InterDirc(`prepare stmt_test_3 from 'select id from prepare_test where id > ?;select id from prepare_test where id > ?;'`)
    62  		c.Assert(interlock.ErrPrepareMulti.Equal(err), IsTrue)
    63  		// The variable count does not match.
    64  		_, err = tk.InterDirc(`prepare stmt_test_4 from 'select id from prepare_test where id > ? and id < ?'; set @a = 1; execute stmt_test_4 using @a;`)
    65  		c.Assert(causetembedded.ErrWrongParamCount.Equal(err), IsTrue)
    66  		// Prepare and deallocate prepared memex immediately.
    67  		tk.MustInterDirc(`prepare stmt_test_5 from 'select id from prepare_test where id > ?'; deallocate prepare stmt_test_5;`)
    68  
    69  		// Statement not found.
    70  		_, err = tk.InterDirc("deallocate prepare stmt_test_5")
    71  		c.Assert(causetembedded.ErrStmtNotFound.Equal(err), IsTrue)
    72  
    73  		// incorrect ALLEGROSQLs in prepare. issue #3738, ALLEGROALLEGROSQL in prepare stmt is parsed in DoPrepare.
    74  		_, err = tk.InterDirc(`prepare p from "delete from t where a = 7 or 1=1/*' and b = 'p'";`)
    75  		c.Assert(err.Error(), Equals, `[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 near '/*' and b = 'p'' at line 1`)
    76  
    77  		// The `stmt_test5` should not be found.
    78  		_, err = tk.InterDirc(`set @a = 1; execute stmt_test_5 using @a;`)
    79  		c.Assert(causetembedded.ErrStmtNotFound.Equal(err), IsTrue)
    80  
    81  		// Use parameter marker with argument will run prepared memex.
    82  		result := tk.MustQuery("select distinct c1, c2 from prepare_test where c1 = ?", 1)
    83  		result.Check(testkit.Events("1 <nil>"))
    84  
    85  		// Call Stochastik PrepareStmt directly to get stmtID.
    86  		query := "select c1, c2 from prepare_test where c1 = ?"
    87  		stmtID, _, _, err := tk.Se.PrepareStmt(query)
    88  		c.Assert(err, IsNil)
    89  		rs, err := tk.Se.InterDircutePreparedStmt(ctx, stmtID, []types.Causet{types.NewCauset(1)})
    90  		c.Assert(err, IsNil)
    91  		tk.ResultSetToResult(rs, Commentf("%v", rs)).Check(testkit.Events("1 <nil>"))
    92  
    93  		tk.MustInterDirc("delete from prepare_test")
    94  		query = "select c1 from prepare_test where c1 = (select c1 from prepare_test where c1 = ?)"
    95  		stmtID, _, _, err = tk.Se.PrepareStmt(query)
    96  		c.Assert(err, IsNil)
    97  
    98  		tk1 := testkit.NewTestKit(c, s.causetstore)
    99  		tk1.Se, err = stochastik.CreateStochastik4TestWithOpt(s.causetstore, &stochastik.Opt{
   100  			PreparedCausetCache: ekvcache.NewSimpleLRUCache(100, 0.1, math.MaxUint64),
   101  		})
   102  		c.Assert(err, IsNil)
   103  
   104  		tk1.MustInterDirc("use test")
   105  		tk1.MustInterDirc("insert prepare_test (c1) values (3)")
   106  		rs, err = tk.Se.InterDircutePreparedStmt(ctx, stmtID, []types.Causet{types.NewCauset(3)})
   107  		c.Assert(err, IsNil)
   108  		tk.ResultSetToResult(rs, Commentf("%v", rs)).Check(testkit.Events("3"))
   109  
   110  		tk.MustInterDirc("delete from prepare_test")
   111  		query = "select c1 from prepare_test where c1 = (select c1 from prepare_test where c1 = ?)"
   112  		stmtID, _, _, err = tk.Se.PrepareStmt(query)
   113  		c.Assert(err, IsNil)
   114  		rs, err = tk.Se.InterDircutePreparedStmt(ctx, stmtID, []types.Causet{types.NewCauset(3)})
   115  		c.Assert(err, IsNil)
   116  		c.Assert(rs.Close(), IsNil)
   117  		tk1.MustInterDirc("insert prepare_test (c1) values (3)")
   118  		rs, err = tk.Se.InterDircutePreparedStmt(ctx, stmtID, []types.Causet{types.NewCauset(3)})
   119  		c.Assert(err, IsNil)
   120  		tk.ResultSetToResult(rs, Commentf("%v", rs)).Check(testkit.Events("3"))
   121  
   122  		tk.MustInterDirc("delete from prepare_test")
   123  		query = "select c1 from prepare_test where c1 in (select c1 from prepare_test where c1 = ?)"
   124  		stmtID, _, _, err = tk.Se.PrepareStmt(query)
   125  		c.Assert(err, IsNil)
   126  		rs, err = tk.Se.InterDircutePreparedStmt(ctx, stmtID, []types.Causet{types.NewCauset(3)})
   127  		c.Assert(err, IsNil)
   128  		c.Assert(rs.Close(), IsNil)
   129  		tk1.MustInterDirc("insert prepare_test (c1) values (3)")
   130  		rs, err = tk.Se.InterDircutePreparedStmt(ctx, stmtID, []types.Causet{types.NewCauset(3)})
   131  		c.Assert(err, IsNil)
   132  		tk.ResultSetToResult(rs, Commentf("%v", rs)).Check(testkit.Events("3"))
   133  
   134  		tk.MustInterDirc("begin")
   135  		tk.MustInterDirc("insert prepare_test (c1) values (4)")
   136  		query = "select c1, c2 from prepare_test where c1 = ?"
   137  		stmtID, _, _, err = tk.Se.PrepareStmt(query)
   138  		c.Assert(err, IsNil)
   139  		tk.MustInterDirc("rollback")
   140  		rs, err = tk.Se.InterDircutePreparedStmt(ctx, stmtID, []types.Causet{types.NewCauset(4)})
   141  		c.Assert(err, IsNil)
   142  		tk.ResultSetToResult(rs, Commentf("%v", rs)).Check(testkit.Events())
   143  
   144  		// Check that ast.Statement created by interlock.CompileInterDircutePreparedStmt has query text.
   145  		stmt, err := interlock.CompileInterDircutePreparedStmt(context.TODO(), tk.Se, stmtID, []types.Causet{types.NewCauset(1)})
   146  		c.Assert(err, IsNil)
   147  		c.Assert(stmt.OriginText(), Equals, query)
   148  
   149  		// Check that rebuild plan works.
   150  		tk.Se.PrepareTxnCtx(ctx)
   151  		_, err = stmt.RebuildCauset(ctx)
   152  		c.Assert(err, IsNil)
   153  		rs, err = stmt.InterDirc(ctx)
   154  		c.Assert(err, IsNil)
   155  		req := rs.NewChunk()
   156  		err = rs.Next(ctx, req)
   157  		c.Assert(err, IsNil)
   158  		c.Assert(rs.Close(), IsNil)
   159  
   160  		// Make schemaReplicant change.
   161  		tk.MustInterDirc("drop causet if exists prepare2")
   162  		tk.InterDirc("create causet prepare2 (a int)")
   163  
   164  		// Should success as the changed schemaReplicant do not affect the prepared memex.
   165  		_, err = tk.Se.InterDircutePreparedStmt(ctx, stmtID, []types.Causet{types.NewCauset(1)})
   166  		c.Assert(err, IsNil)
   167  
   168  		// Drop a defCausumn so the prepared memex become invalid.
   169  		query = "select c1, c2 from prepare_test where c1 = ?"
   170  		stmtID, _, _, err = tk.Se.PrepareStmt(query)
   171  		c.Assert(err, IsNil)
   172  		tk.MustInterDirc("alter causet prepare_test drop defCausumn c2")
   173  
   174  		_, err = tk.Se.InterDircutePreparedStmt(ctx, stmtID, []types.Causet{types.NewCauset(1)})
   175  		c.Assert(causetembedded.ErrUnknownDeferredCauset.Equal(err), IsTrue)
   176  
   177  		tk.MustInterDirc("drop causet prepare_test")
   178  		_, err = tk.Se.InterDircutePreparedStmt(ctx, stmtID, []types.Causet{types.NewCauset(1)})
   179  		c.Assert(causetembedded.ErrSchemaChanged.Equal(err), IsTrue)
   180  
   181  		// issue 3381
   182  		tk.MustInterDirc("drop causet if exists prepare3")
   183  		tk.MustInterDirc("create causet prepare3 (a decimal(1))")
   184  		tk.MustInterDirc("prepare stmt from 'insert into prepare3 value(123)'")
   185  		_, err = tk.InterDirc("execute stmt")
   186  		c.Assert(err, NotNil)
   187  
   188  		_, _, fields, err := tk.Se.PrepareStmt("select a from prepare3")
   189  		c.Assert(err, IsNil)
   190  		c.Assert(fields[0].DBName.L, Equals, "test")
   191  		c.Assert(fields[0].BlockAsName.L, Equals, "prepare3")
   192  		c.Assert(fields[0].DeferredCausetAsName.L, Equals, "a")
   193  
   194  		_, _, fields, err = tk.Se.PrepareStmt("select a from prepare3 where ?")
   195  		c.Assert(err, IsNil)
   196  		c.Assert(fields[0].DBName.L, Equals, "test")
   197  		c.Assert(fields[0].BlockAsName.L, Equals, "prepare3")
   198  		c.Assert(fields[0].DeferredCausetAsName.L, Equals, "a")
   199  
   200  		_, _, fields, err = tk.Se.PrepareStmt("select (1,1) in (select 1,1)")
   201  		c.Assert(err, IsNil)
   202  		c.Assert(fields[0].DBName.L, Equals, "")
   203  		c.Assert(fields[0].BlockAsName.L, Equals, "")
   204  		c.Assert(fields[0].DeferredCausetAsName.L, Equals, "(1,1) in (select 1,1)")
   205  
   206  		_, _, fields, err = tk.Se.PrepareStmt("select a from prepare3 where a = (" +
   207  			"select a from prepare2 where a = ?)")
   208  		c.Assert(err, IsNil)
   209  		c.Assert(fields[0].DBName.L, Equals, "test")
   210  		c.Assert(fields[0].BlockAsName.L, Equals, "prepare3")
   211  		c.Assert(fields[0].DeferredCausetAsName.L, Equals, "a")
   212  
   213  		_, _, fields, err = tk.Se.PrepareStmt("select * from prepare3 as t1 join prepare3 as t2")
   214  		c.Assert(err, IsNil)
   215  		c.Assert(fields[0].DBName.L, Equals, "test")
   216  		c.Assert(fields[0].BlockAsName.L, Equals, "t1")
   217  		c.Assert(fields[0].DeferredCausetAsName.L, Equals, "a")
   218  		c.Assert(fields[1].DBName.L, Equals, "test")
   219  		c.Assert(fields[1].BlockAsName.L, Equals, "t2")
   220  		c.Assert(fields[1].DeferredCausetAsName.L, Equals, "a")
   221  
   222  		_, _, fields, err = tk.Se.PrepareStmt("uFIDelate prepare3 set a = ?")
   223  		c.Assert(err, IsNil)
   224  		c.Assert(len(fields), Equals, 0)
   225  
   226  		// issue 8074
   227  		tk.MustInterDirc("drop causet if exists prepare1;")
   228  		tk.MustInterDirc("create causet prepare1 (a decimal(1))")
   229  		tk.MustInterDirc("insert into prepare1 values(1);")
   230  		_, err = tk.InterDirc("prepare stmt FROM @sql1")
   231  		c.Assert(err.Error(), Equals, "[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 4 near \"NULL\" ")
   232  		tk.MustInterDirc("SET @allegrosql = 'uFIDelate prepare1 set a=5 where a=?';")
   233  		_, err = tk.InterDirc("prepare stmt FROM @allegrosql")
   234  		c.Assert(err, IsNil)
   235  		tk.MustInterDirc("set @var=1;")
   236  		_, err = tk.InterDirc("execute stmt using @var")
   237  		c.Assert(err, IsNil)
   238  		tk.MustQuery("select a from prepare1;").Check(testkit.Events("5"))
   239  
   240  		// issue 19371
   241  		tk.MustInterDirc("SET @allegrosql = 'uFIDelate prepare1 set a=a+1';")
   242  		_, err = tk.InterDirc("prepare stmt FROM @ALLEGROALLEGROSQL")
   243  		c.Assert(err, IsNil)
   244  		_, err = tk.InterDirc("execute stmt")
   245  		c.Assert(err, IsNil)
   246  		tk.MustQuery("select a from prepare1;").Check(testkit.Events("6"))
   247  		_, err = tk.InterDirc("prepare stmt FROM @Sql")
   248  		c.Assert(err, IsNil)
   249  		_, err = tk.InterDirc("execute stmt")
   250  		c.Assert(err, IsNil)
   251  		tk.MustQuery("select a from prepare1;").Check(testkit.Events("7"))
   252  
   253  		// Coverage.
   254  		exec := &interlock.InterDircuteInterDirc{}
   255  		exec.Next(ctx, nil)
   256  		exec.Close()
   257  
   258  		// issue 8065
   259  		stmtID, _, _, err = tk.Se.PrepareStmt("select ? from dual")
   260  		c.Assert(err, IsNil)
   261  		_, err = tk.Se.InterDircutePreparedStmt(ctx, stmtID, []types.Causet{types.NewCauset(1)})
   262  		c.Assert(err, IsNil)
   263  		stmtID, _, _, err = tk.Se.PrepareStmt("uFIDelate prepare1 set a = ? where a = ?")
   264  		c.Assert(err, IsNil)
   265  		_, err = tk.Se.InterDircutePreparedStmt(ctx, stmtID, []types.Causet{types.NewCauset(1), types.NewCauset(1)})
   266  		c.Assert(err, IsNil)
   267  	}
   268  }
   269  
   270  func (s *seqTestSuite) TestPreparedLimitOffset(c *C) {
   271  	orgEnable := causetembedded.PreparedCausetCacheEnabled()
   272  	defer func() {
   273  		causetembedded.SetPreparedCausetCache(orgEnable)
   274  	}()
   275  	flags := []bool{false, true}
   276  	ctx := context.Background()
   277  	for _, flag := range flags {
   278  		var err error
   279  		causetembedded.SetPreparedCausetCache(flag)
   280  
   281  		tk := testkit.NewTestKit(c, s.causetstore)
   282  		tk.Se, err = stochastik.CreateStochastik4TestWithOpt(s.causetstore, &stochastik.Opt{
   283  			PreparedCausetCache: ekvcache.NewSimpleLRUCache(100, 0.1, math.MaxUint64),
   284  		})
   285  		c.Assert(err, IsNil)
   286  
   287  		tk.MustInterDirc("use test")
   288  		tk.MustInterDirc("drop causet if exists prepare_test")
   289  		tk.MustInterDirc("create causet prepare_test (id int PRIMARY KEY AUTO_INCREMENT, c1 int, c2 int, c3 int default 1)")
   290  		tk.MustInterDirc("insert prepare_test (c1) values (1),(2),(NULL)")
   291  		tk.MustInterDirc(`prepare stmt_test_1 from 'select id from prepare_test limit ? offset ?'; set @a = 1, @b=1;`)
   292  		r := tk.MustQuery(`execute stmt_test_1 using @a, @b;`)
   293  		r.Check(testkit.Events("2"))
   294  
   295  		tk.MustInterDirc(`set @a=1.1`)
   296  		r = tk.MustQuery(`execute stmt_test_1 using @a, @b;`)
   297  		r.Check(testkit.Events("2"))
   298  
   299  		tk.MustInterDirc(`set @c="-1"`)
   300  		_, err = tk.InterDirc("execute stmt_test_1 using @c, @c")
   301  		c.Assert(causetembedded.ErrWrongArguments.Equal(err), IsTrue)
   302  
   303  		stmtID, _, _, err := tk.Se.PrepareStmt("select id from prepare_test limit ?")
   304  		c.Assert(err, IsNil)
   305  		_, err = tk.Se.InterDircutePreparedStmt(ctx, stmtID, []types.Causet{types.NewCauset(1)})
   306  		c.Assert(err, IsNil)
   307  	}
   308  }
   309  
   310  func (s *seqTestSuite) TestPreparedNullParam(c *C) {
   311  	orgEnable := causetembedded.PreparedCausetCacheEnabled()
   312  	defer func() {
   313  		causetembedded.SetPreparedCausetCache(orgEnable)
   314  	}()
   315  	flags := []bool{false, true}
   316  	for _, flag := range flags {
   317  		causetembedded.SetPreparedCausetCache(flag)
   318  		tk := testkit.NewTestKit(c, s.causetstore)
   319  		var err error
   320  		tk.Se, err = stochastik.CreateStochastik4TestWithOpt(s.causetstore, &stochastik.Opt{
   321  			PreparedCausetCache: ekvcache.NewSimpleLRUCache(100, 0.1, math.MaxUint64),
   322  		})
   323  		c.Assert(err, IsNil)
   324  
   325  		tk.MustInterDirc("use test")
   326  		tk.MustInterDirc("drop causet if exists t")
   327  		tk.MustInterDirc("create causet t (id int, KEY id (id))")
   328  		tk.MustInterDirc("insert into t values (1), (2), (3)")
   329  		tk.MustInterDirc(`prepare stmt from 'select * from t use index(id) where id = ?'`)
   330  
   331  		r := tk.MustQuery(`execute stmt using @id;`)
   332  		r.Check(nil)
   333  
   334  		r = tk.MustQuery(`execute stmt using @id;`)
   335  		r.Check(nil)
   336  
   337  		tk.MustInterDirc(`set @id="1"`)
   338  		r = tk.MustQuery(`execute stmt using @id;`)
   339  		r.Check(testkit.Events("1"))
   340  
   341  		r = tk.MustQuery(`execute stmt using @id2;`)
   342  		r.Check(nil)
   343  
   344  		r = tk.MustQuery(`execute stmt using @id;`)
   345  		r.Check(testkit.Events("1"))
   346  	}
   347  }
   348  
   349  func (s *seqTestSuite) TestPrepareWithAggregation(c *C) {
   350  	orgEnable := causetembedded.PreparedCausetCacheEnabled()
   351  	defer func() {
   352  		causetembedded.SetPreparedCausetCache(orgEnable)
   353  	}()
   354  	flags := []bool{false, true}
   355  	for _, flag := range flags {
   356  		causetembedded.SetPreparedCausetCache(flag)
   357  		tk := testkit.NewTestKit(c, s.causetstore)
   358  		var err error
   359  		tk.Se, err = stochastik.CreateStochastik4TestWithOpt(s.causetstore, &stochastik.Opt{
   360  			PreparedCausetCache: ekvcache.NewSimpleLRUCache(100, 0.1, math.MaxUint64),
   361  		})
   362  		c.Assert(err, IsNil)
   363  
   364  		tk.MustInterDirc("use test")
   365  		tk.MustInterDirc("drop causet if exists t")
   366  		tk.MustInterDirc("create causet t (id int primary key)")
   367  		tk.MustInterDirc("insert into t values (1), (2), (3)")
   368  		tk.MustInterDirc(`prepare stmt from 'select sum(id) from t where id = ?'`)
   369  
   370  		tk.MustInterDirc(`set @id="1"`)
   371  		r := tk.MustQuery(`execute stmt using @id;`)
   372  		r.Check(testkit.Events("1"))
   373  
   374  		r = tk.MustQuery(`execute stmt using @id;`)
   375  		r.Check(testkit.Events("1"))
   376  	}
   377  }
   378  
   379  func (s *seqTestSuite) TestPreparedIssue7579(c *C) {
   380  	orgEnable := causetembedded.PreparedCausetCacheEnabled()
   381  	defer func() {
   382  		causetembedded.SetPreparedCausetCache(orgEnable)
   383  	}()
   384  	flags := []bool{false, true}
   385  	for _, flag := range flags {
   386  		causetembedded.SetPreparedCausetCache(flag)
   387  		tk := testkit.NewTestKit(c, s.causetstore)
   388  		var err error
   389  		tk.Se, err = stochastik.CreateStochastik4TestWithOpt(s.causetstore, &stochastik.Opt{
   390  			PreparedCausetCache: ekvcache.NewSimpleLRUCache(100, 0.1, math.MaxUint64),
   391  		})
   392  		c.Assert(err, IsNil)
   393  
   394  		tk.MustInterDirc("use test")
   395  		tk.MustInterDirc("drop causet if exists t")
   396  		tk.MustInterDirc("create causet t (a int, b int, index a_idx(a))")
   397  		tk.MustInterDirc("insert into t values (1,1), (2,2), (null,3)")
   398  
   399  		r := tk.MustQuery("select a, b from t order by b asc;")
   400  		r.Check(testkit.Events("1 1", "2 2", "<nil> 3"))
   401  
   402  		tk.MustInterDirc(`prepare stmt from 'select a, b from t where ? order by b asc'`)
   403  
   404  		r = tk.MustQuery(`execute stmt using @param;`)
   405  		r.Check(nil)
   406  
   407  		tk.MustInterDirc(`set @param = true`)
   408  		r = tk.MustQuery(`execute stmt using @param;`)
   409  		r.Check(testkit.Events("1 1", "2 2", "<nil> 3"))
   410  
   411  		tk.MustInterDirc(`set @param = false`)
   412  		r = tk.MustQuery(`execute stmt using @param;`)
   413  		r.Check(nil)
   414  
   415  		tk.MustInterDirc(`set @param = 1`)
   416  		r = tk.MustQuery(`execute stmt using @param;`)
   417  		r.Check(testkit.Events("1 1", "2 2", "<nil> 3"))
   418  
   419  		tk.MustInterDirc(`set @param = 0`)
   420  		r = tk.MustQuery(`execute stmt using @param;`)
   421  		r.Check(nil)
   422  	}
   423  }
   424  
   425  func (s *seqTestSuite) TestPreparedInsert(c *C) {
   426  	orgEnable := causetembedded.PreparedCausetCacheEnabled()
   427  	defer func() {
   428  		causetembedded.SetPreparedCausetCache(orgEnable)
   429  	}()
   430  	metrics.ResetblockCausetCacheCounterFortTest = true
   431  	metrics.CausetCacheCounter.Reset()
   432  	counter := metrics.CausetCacheCounter.WithLabelValues("prepare")
   433  	pb := &dto.Metric{}
   434  	flags := []bool{false, true}
   435  	for _, flag := range flags {
   436  		causetembedded.SetPreparedCausetCache(flag)
   437  		tk := testkit.NewTestKit(c, s.causetstore)
   438  		var err error
   439  		tk.Se, err = stochastik.CreateStochastik4TestWithOpt(s.causetstore, &stochastik.Opt{
   440  			PreparedCausetCache: ekvcache.NewSimpleLRUCache(100, 0.1, math.MaxUint64),
   441  		})
   442  		c.Assert(err, IsNil)
   443  
   444  		tk.MustInterDirc("use test")
   445  		tk.MustInterDirc("drop causet if exists prepare_test")
   446  		tk.MustInterDirc("create causet prepare_test (id int PRIMARY KEY, c1 int)")
   447  		tk.MustInterDirc(`prepare stmt_insert from 'insert into prepare_test values (?, ?)'`)
   448  		tk.MustInterDirc(`set @a=1,@b=1; execute stmt_insert using @a, @b;`)
   449  		if flag {
   450  			counter.Write(pb)
   451  			hit := pb.GetCounter().GetValue()
   452  			c.Check(hit, Equals, float64(0))
   453  		}
   454  		tk.MustInterDirc(`set @a=2,@b=2; execute stmt_insert using @a, @b;`)
   455  		if flag {
   456  			counter.Write(pb)
   457  			hit := pb.GetCounter().GetValue()
   458  			c.Check(hit, Equals, float64(1))
   459  		}
   460  		tk.MustInterDirc(`set @a=3,@b=3; execute stmt_insert using @a, @b;`)
   461  		if flag {
   462  			counter.Write(pb)
   463  			hit := pb.GetCounter().GetValue()
   464  			c.Check(hit, Equals, float64(2))
   465  		}
   466  
   467  		result := tk.MustQuery("select id, c1 from prepare_test where id = ?", 1)
   468  		result.Check(testkit.Events("1 1"))
   469  		result = tk.MustQuery("select id, c1 from prepare_test where id = ?", 2)
   470  		result.Check(testkit.Events("2 2"))
   471  		result = tk.MustQuery("select id, c1 from prepare_test where id = ?", 3)
   472  		result.Check(testkit.Events("3 3"))
   473  
   474  		tk.MustInterDirc(`prepare stmt_insert_select from 'insert into prepare_test (id, c1) select id + 100, c1 + 100 from prepare_test where id = ?'`)
   475  		tk.MustInterDirc(`set @a=1; execute stmt_insert_select using @a;`)
   476  		if flag {
   477  			counter.Write(pb)
   478  			hit := pb.GetCounter().GetValue()
   479  			c.Check(hit, Equals, float64(2))
   480  		}
   481  		tk.MustInterDirc(`set @a=2; execute stmt_insert_select using @a;`)
   482  		if flag {
   483  			counter.Write(pb)
   484  			hit := pb.GetCounter().GetValue()
   485  			c.Check(hit, Equals, float64(3))
   486  		}
   487  		tk.MustInterDirc(`set @a=3; execute stmt_insert_select using @a;`)
   488  		if flag {
   489  			counter.Write(pb)
   490  			hit := pb.GetCounter().GetValue()
   491  			c.Check(hit, Equals, float64(4))
   492  		}
   493  
   494  		result = tk.MustQuery("select id, c1 from prepare_test where id = ?", 101)
   495  		result.Check(testkit.Events("101 101"))
   496  		result = tk.MustQuery("select id, c1 from prepare_test where id = ?", 102)
   497  		result.Check(testkit.Events("102 102"))
   498  		result = tk.MustQuery("select id, c1 from prepare_test where id = ?", 103)
   499  		result.Check(testkit.Events("103 103"))
   500  	}
   501  }
   502  
   503  func (s *seqTestSuite) TestPreparedUFIDelate(c *C) {
   504  	orgEnable := causetembedded.PreparedCausetCacheEnabled()
   505  	defer func() {
   506  		causetembedded.SetPreparedCausetCache(orgEnable)
   507  	}()
   508  	metrics.ResetblockCausetCacheCounterFortTest = true
   509  	metrics.CausetCacheCounter.Reset()
   510  	counter := metrics.CausetCacheCounter.WithLabelValues("prepare")
   511  	pb := &dto.Metric{}
   512  	flags := []bool{false, true}
   513  	for _, flag := range flags {
   514  		causetembedded.SetPreparedCausetCache(flag)
   515  		tk := testkit.NewTestKit(c, s.causetstore)
   516  		var err error
   517  		tk.Se, err = stochastik.CreateStochastik4TestWithOpt(s.causetstore, &stochastik.Opt{
   518  			PreparedCausetCache: ekvcache.NewSimpleLRUCache(100, 0.1, math.MaxUint64),
   519  		})
   520  		c.Assert(err, IsNil)
   521  
   522  		tk.MustInterDirc("use test")
   523  		tk.MustInterDirc("drop causet if exists prepare_test")
   524  		tk.MustInterDirc("create causet prepare_test (id int PRIMARY KEY, c1 int)")
   525  		tk.MustInterDirc(`insert into prepare_test values (1, 1)`)
   526  		tk.MustInterDirc(`insert into prepare_test values (2, 2)`)
   527  		tk.MustInterDirc(`insert into prepare_test values (3, 3)`)
   528  
   529  		tk.MustInterDirc(`prepare stmt_uFIDelate from 'uFIDelate prepare_test set c1 = c1 + ? where id = ?'`)
   530  		tk.MustInterDirc(`set @a=1,@b=100; execute stmt_uFIDelate using @b,@a;`)
   531  		if flag {
   532  			counter.Write(pb)
   533  			hit := pb.GetCounter().GetValue()
   534  			c.Check(hit, Equals, float64(2))
   535  		}
   536  		tk.MustInterDirc(`set @a=2,@b=200; execute stmt_uFIDelate using @b,@a;`)
   537  		if flag {
   538  			counter.Write(pb)
   539  			hit := pb.GetCounter().GetValue()
   540  			c.Check(hit, Equals, float64(3))
   541  		}
   542  		tk.MustInterDirc(`set @a=3,@b=300; execute stmt_uFIDelate using @b,@a;`)
   543  		if flag {
   544  			counter.Write(pb)
   545  			hit := pb.GetCounter().GetValue()
   546  			c.Check(hit, Equals, float64(4))
   547  		}
   548  
   549  		result := tk.MustQuery("select id, c1 from prepare_test where id = ?", 1)
   550  		result.Check(testkit.Events("1 101"))
   551  		result = tk.MustQuery("select id, c1 from prepare_test where id = ?", 2)
   552  		result.Check(testkit.Events("2 202"))
   553  		result = tk.MustQuery("select id, c1 from prepare_test where id = ?", 3)
   554  		result.Check(testkit.Events("3 303"))
   555  	}
   556  }
   557  
   558  func (s *seqTestSuite) TestPreparedDelete(c *C) {
   559  	orgEnable := causetembedded.PreparedCausetCacheEnabled()
   560  	defer func() {
   561  		causetembedded.SetPreparedCausetCache(orgEnable)
   562  	}()
   563  	metrics.ResetblockCausetCacheCounterFortTest = true
   564  	metrics.CausetCacheCounter.Reset()
   565  	counter := metrics.CausetCacheCounter.WithLabelValues("prepare")
   566  	pb := &dto.Metric{}
   567  	flags := []bool{false, true}
   568  	for _, flag := range flags {
   569  		causetembedded.SetPreparedCausetCache(flag)
   570  		tk := testkit.NewTestKit(c, s.causetstore)
   571  		var err error
   572  		tk.Se, err = stochastik.CreateStochastik4TestWithOpt(s.causetstore, &stochastik.Opt{
   573  			PreparedCausetCache: ekvcache.NewSimpleLRUCache(100, 0.1, math.MaxUint64),
   574  		})
   575  		c.Assert(err, IsNil)
   576  
   577  		tk.MustInterDirc("use test")
   578  		tk.MustInterDirc("drop causet if exists prepare_test")
   579  		tk.MustInterDirc("create causet prepare_test (id int PRIMARY KEY, c1 int)")
   580  		tk.MustInterDirc(`insert into prepare_test values (1, 1)`)
   581  		tk.MustInterDirc(`insert into prepare_test values (2, 2)`)
   582  		tk.MustInterDirc(`insert into prepare_test values (3, 3)`)
   583  
   584  		tk.MustInterDirc(`prepare stmt_delete from 'delete from prepare_test where id = ?'`)
   585  		tk.MustInterDirc(`set @a=1; execute stmt_delete using @a;`)
   586  		if flag {
   587  			counter.Write(pb)
   588  			hit := pb.GetCounter().GetValue()
   589  			c.Check(hit, Equals, float64(0))
   590  		}
   591  		tk.MustInterDirc(`set @a=2; execute stmt_delete using @a;`)
   592  		if flag {
   593  			counter.Write(pb)
   594  			hit := pb.GetCounter().GetValue()
   595  			c.Check(hit, Equals, float64(1))
   596  		}
   597  		tk.MustInterDirc(`set @a=3; execute stmt_delete using @a;`)
   598  		if flag {
   599  			counter.Write(pb)
   600  			hit := pb.GetCounter().GetValue()
   601  			c.Check(hit, Equals, float64(2))
   602  		}
   603  
   604  		result := tk.MustQuery("select id, c1 from prepare_test where id = ?", 1)
   605  		result.Check(nil)
   606  		result = tk.MustQuery("select id, c1 from prepare_test where id = ?", 2)
   607  		result.Check(nil)
   608  		result = tk.MustQuery("select id, c1 from prepare_test where id = ?", 3)
   609  		result.Check(nil)
   610  	}
   611  }
   612  
   613  func (s *seqTestSuite) TestPrepareDealloc(c *C) {
   614  	orgEnable := causetembedded.PreparedCausetCacheEnabled()
   615  	defer func() {
   616  		causetembedded.SetPreparedCausetCache(orgEnable)
   617  	}()
   618  	causetembedded.SetPreparedCausetCache(true)
   619  
   620  	tk := testkit.NewTestKit(c, s.causetstore)
   621  	var err error
   622  	tk.Se, err = stochastik.CreateStochastik4TestWithOpt(s.causetstore, &stochastik.Opt{
   623  		PreparedCausetCache: ekvcache.NewSimpleLRUCache(3, 0.1, math.MaxUint64),
   624  	})
   625  	c.Assert(err, IsNil)
   626  
   627  	tk.MustInterDirc("use test")
   628  	tk.MustInterDirc("drop causet if exists prepare_test")
   629  	tk.MustInterDirc("create causet prepare_test (id int PRIMARY KEY, c1 int)")
   630  
   631  	c.Assert(tk.Se.PreparedCausetCache().Size(), Equals, 0)
   632  	tk.MustInterDirc(`prepare stmt1 from 'select * from prepare_test'`)
   633  	tk.MustInterDirc("execute stmt1")
   634  	tk.MustInterDirc(`prepare stmt2 from 'select * from prepare_test'`)
   635  	tk.MustInterDirc("execute stmt2")
   636  	tk.MustInterDirc(`prepare stmt3 from 'select * from prepare_test'`)
   637  	tk.MustInterDirc("execute stmt3")
   638  	tk.MustInterDirc(`prepare stmt4 from 'select * from prepare_test'`)
   639  	tk.MustInterDirc("execute stmt4")
   640  	c.Assert(tk.Se.PreparedCausetCache().Size(), Equals, 3)
   641  
   642  	tk.MustInterDirc("deallocate prepare stmt1")
   643  	c.Assert(tk.Se.PreparedCausetCache().Size(), Equals, 3)
   644  	tk.MustInterDirc("deallocate prepare stmt2")
   645  	tk.MustInterDirc("deallocate prepare stmt3")
   646  	tk.MustInterDirc("deallocate prepare stmt4")
   647  	c.Assert(tk.Se.PreparedCausetCache().Size(), Equals, 0)
   648  }
   649  
   650  func (s *seqTestSuite) TestPreparedIssue8153(c *C) {
   651  	orgEnable := causetembedded.PreparedCausetCacheEnabled()
   652  	defer func() {
   653  		causetembedded.SetPreparedCausetCache(orgEnable)
   654  	}()
   655  	flags := []bool{false, true}
   656  	for _, flag := range flags {
   657  		var err error
   658  		causetembedded.SetPreparedCausetCache(flag)
   659  		tk := testkit.NewTestKit(c, s.causetstore)
   660  		tk.Se, err = stochastik.CreateStochastik4TestWithOpt(s.causetstore, &stochastik.Opt{
   661  			PreparedCausetCache: ekvcache.NewSimpleLRUCache(100, 0.1, math.MaxUint64),
   662  		})
   663  		c.Assert(err, IsNil)
   664  
   665  		tk.MustInterDirc("use test")
   666  		tk.MustInterDirc("drop causet if exists t")
   667  		tk.MustInterDirc("create causet t (a int, b int)")
   668  		tk.MustInterDirc("insert into t (a, b) values (1,3), (2,2), (3,1)")
   669  
   670  		tk.MustInterDirc(`prepare stmt from 'select * from t order by ? asc'`)
   671  		r := tk.MustQuery(`execute stmt using @param;`)
   672  		r.Check(testkit.Events("1 3", "2 2", "3 1"))
   673  
   674  		tk.MustInterDirc(`set @param = 1`)
   675  		r = tk.MustQuery(`execute stmt using @param;`)
   676  		r.Check(testkit.Events("1 3", "2 2", "3 1"))
   677  
   678  		tk.MustInterDirc(`set @param = 2`)
   679  		r = tk.MustQuery(`execute stmt using @param;`)
   680  		r.Check(testkit.Events("3 1", "2 2", "1 3"))
   681  
   682  		tk.MustInterDirc(`set @param = 3`)
   683  		_, err = tk.InterDirc(`execute stmt using @param;`)
   684  		c.Assert(err.Error(), Equals, "[causet:1054]Unknown defCausumn '?' in 'order clause'")
   685  
   686  		tk.MustInterDirc(`set @param = '##'`)
   687  		r = tk.MustQuery(`execute stmt using @param;`)
   688  		r.Check(testkit.Events("1 3", "2 2", "3 1"))
   689  
   690  		tk.MustInterDirc("insert into t (a, b) values (1,1), (1,2), (2,1), (2,3), (3,2), (3,3)")
   691  		tk.MustInterDirc(`prepare stmt from 'select ?, sum(a) from t group by ?'`)
   692  
   693  		tk.MustInterDirc(`set @a=1,@b=1`)
   694  		r = tk.MustQuery(`execute stmt using @a,@b;`)
   695  		r.Check(testkit.Events("1 18"))
   696  
   697  		tk.MustInterDirc(`set @a=1,@b=2`)
   698  		_, err = tk.InterDirc(`execute stmt using @a,@b;`)
   699  		c.Assert(err.Error(), Equals, "[causet:1056]Can't group on 'sum(a)'")
   700  	}
   701  }
   702  
   703  func (s *seqTestSuite) TestPreparedIssue8644(c *C) {
   704  	orgEnable := causetembedded.PreparedCausetCacheEnabled()
   705  	defer func() {
   706  		causetembedded.SetPreparedCausetCache(orgEnable)
   707  	}()
   708  	flags := []bool{false, true}
   709  	for _, flag := range flags {
   710  		causetembedded.SetPreparedCausetCache(flag)
   711  		tk := testkit.NewTestKit(c, s.causetstore)
   712  		var err error
   713  		tk.Se, err = stochastik.CreateStochastik4TestWithOpt(s.causetstore, &stochastik.Opt{
   714  			PreparedCausetCache: ekvcache.NewSimpleLRUCache(100, 0.1, math.MaxUint64),
   715  		})
   716  		c.Assert(err, IsNil)
   717  
   718  		tk.MustInterDirc("use test")
   719  		tk.MustInterDirc("drop causet if exists t")
   720  		tk.MustInterDirc("create causet t(data mediumblob)")
   721  
   722  		tk.MustInterDirc(`prepare stmt1 from 'insert t (data) values (?)'`)
   723  
   724  		tk.MustInterDirc(`set @a = 'a'`)
   725  		tk.MustInterDirc(`execute stmt1 using @a;`)
   726  
   727  		tk.MustInterDirc(`set @b = 'aaaaaaaaaaaaaaaaaa'`)
   728  		tk.MustInterDirc(`execute stmt1 using @b;`)
   729  
   730  		r := tk.MustQuery(`select * from t`)
   731  		r.Check(testkit.Events("a", "aaaaaaaaaaaaaaaaaa"))
   732  	}
   733  }
   734  
   735  // mockStochastikManager is a mocked stochastik manager which is used for test.
   736  type mockStochastikManager1 struct {
   737  	Se stochastik.Stochastik
   738  }
   739  
   740  // ShowProcessList implements the StochastikManager.ShowProcessList interface.
   741  func (msm *mockStochastikManager1) ShowProcessList() map[uint64]*soliton.ProcessInfo {
   742  	ret := make(map[uint64]*soliton.ProcessInfo)
   743  	return ret
   744  }
   745  
   746  func (msm *mockStochastikManager1) GetProcessInfo(id uint64) (*soliton.ProcessInfo, bool) {
   747  	pi := msm.Se.ShowProcess()
   748  	return pi, true
   749  }
   750  
   751  // Kill implements the StochastikManager.Kill interface.
   752  func (msm *mockStochastikManager1) Kill(cid uint64, query bool) {}
   753  
   754  func (msm *mockStochastikManager1) UFIDelateTLSConfig(cfg *tls.Config) {}
   755  
   756  func (s *seqTestSuite) TestPreparedIssue17419(c *C) {
   757  	ctx := context.Background()
   758  	tk := testkit.NewTestKit(c, s.causetstore)
   759  
   760  	tk.MustInterDirc("use test")
   761  	tk.MustInterDirc("drop causet if exists t")
   762  	tk.MustInterDirc("create causet t (a int)")
   763  	tk.MustInterDirc("insert into t (a) values (1), (2), (3)")
   764  
   765  	tk1 := testkit.NewTestKit(c, s.causetstore)
   766  
   767  	var err error
   768  	tk1.Se, err = stochastik.CreateStochastik4Test(s.causetstore)
   769  	c.Assert(err, IsNil)
   770  	tk1.GetConnectionID()
   771  
   772  	query := "select * from test.t"
   773  	stmtID, _, _, err := tk1.Se.PrepareStmt(query)
   774  	c.Assert(err, IsNil)
   775  
   776  	sm := &mockStochastikManager1{
   777  		Se: tk1.Se,
   778  	}
   779  	tk1.Se.SetStochastikManager(sm)
   780  	s.petri.ExpensiveQueryHandle().SetStochastikManager(sm)
   781  
   782  	rs, err := tk1.Se.InterDircutePreparedStmt(ctx, stmtID, []types.Causet{})
   783  	c.Assert(err, IsNil)
   784  	tk1.ResultSetToResult(rs, Commentf("%v", rs)).Check(testkit.Events("1", "2", "3"))
   785  	tk1.Se.SetProcessInfo("", time.Now(), allegrosql.ComStmtInterDircute, 0)
   786  
   787  	s.petri.ExpensiveQueryHandle().LogOnQueryExceedMemQuota(tk.Se.GetStochastikVars().ConnectionID)
   788  
   789  	// After entirely fixing https://github.com/whtcorpsinc/milevadb/issues/17419
   790  	// c.Assert(tk1.Se.ShowProcess().Causet, NotNil)
   791  	// _, ok := tk1.Se.ShowProcess().Causet.(*causetembedded.InterDircute)
   792  	// c.Assert(ok, IsTrue)
   793  }