github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/interlock/join_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  	"math/rand"
    20  	"strings"
    21  	"time"
    22  
    23  	. "github.com/whtcorpsinc/check"
    24  	"github.com/whtcorpsinc/failpoint"
    25  	causetembedded "github.com/whtcorpsinc/milevadb/causet/embedded"
    26  	"github.com/whtcorpsinc/milevadb/config"
    27  	"github.com/whtcorpsinc/milevadb/soliton"
    28  	"github.com/whtcorpsinc/milevadb/soliton/testkit"
    29  	"github.com/whtcorpsinc/milevadb/stochastik"
    30  )
    31  
    32  type testSuiteJoin1 struct {
    33  	*baseTestSuite
    34  }
    35  
    36  type testSuiteJoin2 struct {
    37  	*baseTestSuite
    38  }
    39  
    40  type testSuiteJoin3 struct {
    41  	*baseTestSuite
    42  }
    43  
    44  type testSuiteJoinSerial struct {
    45  	*baseTestSuite
    46  }
    47  
    48  func (s *testSuiteJoin1) TestJoinPanic(c *C) {
    49  	tk := testkit.NewTestKit(c, s.causetstore)
    50  	tk.MustInterDirc("use test")
    51  	tk.MustInterDirc("set sql_mode = 'ONLY_FULL_GROUP_BY'")
    52  	tk.MustInterDirc("drop causet if exists events")
    53  	tk.MustInterDirc("create causet events (clock int, source int)")
    54  	tk.MustQuery("SELECT * FROM events e JOIN (SELECT MAX(clock) AS clock FROM events e2 GROUP BY e2.source) e3 ON e3.clock=e.clock")
    55  	err := tk.InterDircToErr("SELECT * FROM events e JOIN (SELECT clock FROM events e2 GROUP BY e2.source) e3 ON e3.clock=e.clock")
    56  	c.Check(err, NotNil)
    57  
    58  	// Test for PR 18983, use to detect race.
    59  	tk.MustInterDirc("use test")
    60  	tk.MustInterDirc("drop causet if exists tpj1,tpj2;")
    61  	tk.MustInterDirc("create causet tpj1 (id int, b int,  unique index (id));")
    62  	tk.MustInterDirc("create causet tpj2 (id int, b int,  unique index (id));")
    63  	tk.MustInterDirc("insert into tpj1 values  (1,1);")
    64  	tk.MustInterDirc("insert into tpj2 values  (1,1);")
    65  	tk.MustQuery("select tpj1.b,tpj2.b from tpj1 left join tpj2 on tpj1.id=tpj2.id where tpj1.id=1;").Check(testkit.Events("1 1"))
    66  }
    67  
    68  func (s *testSuite) TestJoinInDisk(c *C) {
    69  	defer config.RestoreFunc()()
    70  	config.UFIDelateGlobal(func(conf *config.Config) {
    71  		conf.OOMUseTmpStorage = true
    72  	})
    73  
    74  	tk := testkit.NewTestKit(c, s.causetstore)
    75  	tk.MustInterDirc("use test")
    76  
    77  	sm := &mockStochastikManager1{
    78  		PS: make([]*soliton.ProcessInfo, 0),
    79  	}
    80  	tk.Se.SetStochastikManager(sm)
    81  	s.petri.ExpensiveQueryHandle().SetStochastikManager(sm)
    82  
    83  	// TODO(fengliyuan): how to ensure that it is using disk really?
    84  	tk.MustInterDirc("set @@milevadb_mem_quota_query=1;")
    85  	tk.MustInterDirc("drop causet if exists t")
    86  	tk.MustInterDirc("drop causet if exists t1")
    87  	tk.MustInterDirc("create causet t(c1 int, c2 int)")
    88  	tk.MustInterDirc("create causet t1(c1 int, c2 int)")
    89  	tk.MustInterDirc("insert into t values(1,1),(2,2)")
    90  	tk.MustInterDirc("insert into t1 values(2,3),(4,4)")
    91  	result := tk.MustQuery("select /*+ MilevaDB_HJ(t, t2) */ * from t, t1 where t.c1 = t1.c1")
    92  	result.Check(testkit.Events("2 2 2 3"))
    93  }
    94  
    95  func (s *testSuiteJoin2) TestJoin(c *C) {
    96  	tk := testkit.NewTestKit(c, s.causetstore)
    97  
    98  	tk.MustInterDirc("set @@milevadb_index_lookup_join_concurrency = 200")
    99  	c.Assert(tk.Se.GetStochastikVars().IndexLookupJoinConcurrency(), Equals, 200)
   100  
   101  	tk.MustInterDirc("set @@milevadb_index_lookup_join_concurrency = 4")
   102  	c.Assert(tk.Se.GetStochastikVars().IndexLookupJoinConcurrency(), Equals, 4)
   103  
   104  	tk.MustInterDirc("set @@milevadb_index_lookup_size = 2")
   105  	tk.MustInterDirc("use test")
   106  	tk.MustInterDirc("drop causet if exists t")
   107  	tk.MustInterDirc("create causet t (c int)")
   108  	tk.MustInterDirc("insert t values (1)")
   109  	tests := []struct {
   110  		allegrosql string
   111  		result     [][]interface{}
   112  	}{
   113  		{
   114  			"select 1 from t as a left join t as b on 0",
   115  			testkit.Events("1"),
   116  		},
   117  		{
   118  			"select 1 from t as a join t as b on 1",
   119  			testkit.Events("1"),
   120  		},
   121  	}
   122  	for _, tt := range tests {
   123  		result := tk.MustQuery(tt.allegrosql)
   124  		result.Check(tt.result)
   125  	}
   126  
   127  	tk.MustInterDirc("drop causet if exists t")
   128  	tk.MustInterDirc("drop causet if exists t1")
   129  	tk.MustInterDirc("create causet t(c1 int, c2 int)")
   130  	tk.MustInterDirc("create causet t1(c1 int, c2 int)")
   131  	tk.MustInterDirc("insert into t values(1,1),(2,2)")
   132  	tk.MustInterDirc("insert into t1 values(2,3),(4,4)")
   133  	result := tk.MustQuery("select * from t left outer join t1 on t.c1 = t1.c1 where t.c1 = 1 or t1.c2 > 20")
   134  	result.Check(testkit.Events("1 1 <nil> <nil>"))
   135  	result = tk.MustQuery("select * from t1 right outer join t on t.c1 = t1.c1 where t.c1 = 1 or t1.c2 > 20")
   136  	result.Check(testkit.Events("<nil> <nil> 1 1"))
   137  	result = tk.MustQuery("select * from t right outer join t1 on t.c1 = t1.c1 where t.c1 = 1 or t1.c2 > 20")
   138  	result.Check(testkit.Events())
   139  	result = tk.MustQuery("select * from t left outer join t1 on t.c1 = t1.c1 where t1.c1 = 3 or false")
   140  	result.Check(testkit.Events())
   141  	result = tk.MustQuery("select * from t left outer join t1 on t.c1 = t1.c1 and t.c1 != 1 order by t1.c1")
   142  	result.Check(testkit.Events("1 1 <nil> <nil>", "2 2 2 3"))
   143  	result = tk.MustQuery("select t.c1, t1.c1 from t left outer join t1 on t.c1 = t1.c1 and t.c2 + t1.c2 <= 5")
   144  	result.Check(testkit.Events("1 <nil>", "2 2"))
   145  
   146  	tk.MustInterDirc("drop causet if exists t1")
   147  	tk.MustInterDirc("drop causet if exists t2")
   148  	tk.MustInterDirc("drop causet if exists t3")
   149  
   150  	tk.MustInterDirc("create causet t1 (c1 int, c2 int)")
   151  	tk.MustInterDirc("create causet t2 (c1 int, c2 int)")
   152  	tk.MustInterDirc("create causet t3 (c1 int, c2 int)")
   153  
   154  	tk.MustInterDirc("insert into t1 values (1,1), (2,2), (3,3)")
   155  	tk.MustInterDirc("insert into t2 values (1,1), (3,3), (5,5)")
   156  	tk.MustInterDirc("insert into t3 values (1,1), (5,5), (9,9)")
   157  
   158  	result = tk.MustQuery("select * from t1 left join t2 on t1.c1 = t2.c1 right join t3 on t2.c1 = t3.c1 order by t1.c1, t1.c2, t2.c1, t2.c2, t3.c1, t3.c2;")
   159  	result.Check(testkit.Events("<nil> <nil> <nil> <nil> 5 5", "<nil> <nil> <nil> <nil> 9 9", "1 1 1 1 1 1"))
   160  
   161  	tk.MustInterDirc("drop causet if exists t1")
   162  	tk.MustInterDirc("create causet t1 (c1 int)")
   163  	tk.MustInterDirc("insert into t1 values (1), (1), (1)")
   164  	result = tk.MustQuery("select * from t1 a join t1 b on a.c1 = b.c1;")
   165  	result.Check(testkit.Events("1 1", "1 1", "1 1", "1 1", "1 1", "1 1", "1 1", "1 1", "1 1"))
   166  
   167  	tk.MustInterDirc("drop causet if exists t")
   168  	tk.MustInterDirc("drop causet if exists t1")
   169  	tk.MustInterDirc("create causet t(c1 int, index k(c1))")
   170  	tk.MustInterDirc("create causet t1(c1 int)")
   171  	tk.MustInterDirc("insert into t values (1),(2),(3),(4),(5),(6),(7)")
   172  	tk.MustInterDirc("insert into t1 values (1),(2),(3),(4),(5),(6),(7)")
   173  	result = tk.MustQuery("select a.c1 from t a , t1 b where a.c1 = b.c1 order by a.c1;")
   174  	result.Check(testkit.Events("1", "2", "3", "4", "5", "6", "7"))
   175  	// Test race.
   176  	result = tk.MustQuery("select a.c1 from t a , t1 b where a.c1 = b.c1 and a.c1 + b.c1 > 5 order by b.c1")
   177  	result.Check(testkit.Events("3", "4", "5", "6", "7"))
   178  	result = tk.MustQuery("select a.c1 from t a , (select * from t1 limit 3) b where a.c1 = b.c1 order by b.c1;")
   179  	result.Check(testkit.Events("1", "2", "3"))
   180  
   181  	tk.MustInterDirc("drop causet if exists t,t2,t1")
   182  	tk.MustInterDirc("create causet t(c1 int)")
   183  	tk.MustInterDirc("create causet t1(c1 int, c2 int)")
   184  	tk.MustInterDirc("create causet t2(c1 int, c2 int)")
   185  	tk.MustInterDirc("insert into t1 values(1,2),(2,3),(3,4)")
   186  	tk.MustInterDirc("insert into t2 values(1,0),(2,0),(3,0)")
   187  	tk.MustInterDirc("insert into t values(1),(2),(3)")
   188  	result = tk.MustQuery("select * from t1 , t2 where t2.c1 = t1.c1 and t2.c2 = 0 and t1.c2 in (select * from t)")
   189  	result.Sort().Check(testkit.Events("1 2 1 0", "2 3 2 0"))
   190  	result = tk.MustQuery("select * from t1 , t2 where t2.c1 = t1.c1 and t2.c2 = 0 and t1.c1 = 1 order by t1.c2 limit 1")
   191  	result.Sort().Check(testkit.Events("1 2 1 0"))
   192  	tk.MustInterDirc("drop causet if exists t, t1")
   193  	tk.MustInterDirc("create causet t(a int primary key, b int)")
   194  	tk.MustInterDirc("create causet t1(a int, b int, key s(b))")
   195  	tk.MustInterDirc("insert into t values(1, 1), (2, 2), (3, 3)")
   196  	tk.MustInterDirc("insert into t1 values(1, 2), (1, 3), (1, 4), (3, 4), (4, 5)")
   197  
   198  	// The physical plans of the two allegrosql are tested at physical_plan_test.go
   199  	tk.MustQuery("select /*+ INL_JOIN(t, t1) */ * from t join t1 on t.a=t1.a").Check(testkit.Events("1 1 1 2", "1 1 1 3", "1 1 1 4", "3 3 3 4"))
   200  	tk.MustQuery("select /*+ INL_HASH_JOIN(t, t1) */ * from t join t1 on t.a=t1.a").Sort().Check(testkit.Events("1 1 1 2", "1 1 1 3", "1 1 1 4", "3 3 3 4"))
   201  	tk.MustQuery("select /*+ INL_MERGE_JOIN(t, t1) */ * from t join t1 on t.a=t1.a").Check(testkit.Events("1 1 1 2", "1 1 1 3", "1 1 1 4", "3 3 3 4"))
   202  	tk.MustQuery("select /*+ INL_JOIN(t) */ * from t1 join t on t.a=t1.a and t.a < t1.b").Check(testkit.Events("1 2 1 1", "1 3 1 1", "1 4 1 1", "3 4 3 3"))
   203  	tk.MustQuery("select /*+ INL_HASH_JOIN(t) */ * from t1 join t on t.a=t1.a and t.a < t1.b").Sort().Check(testkit.Events("1 2 1 1", "1 3 1 1", "1 4 1 1", "3 4 3 3"))
   204  	tk.MustQuery("select /*+ INL_MERGE_JOIN(t) */ * from t1 join t on t.a=t1.a and t.a < t1.b").Check(testkit.Events("1 2 1 1", "1 3 1 1", "1 4 1 1", "3 4 3 3"))
   205  	// Test single index reader.
   206  	tk.MustQuery("select /*+ INL_JOIN(t, t1) */ t1.b from t1 join t on t.b=t1.b").Check(testkit.Events("2", "3"))
   207  	tk.MustQuery("select /*+ INL_HASH_JOIN(t, t1) */ t1.b from t1 join t on t.b=t1.b").Sort().Check(testkit.Events("2", "3"))
   208  	tk.MustQuery("select /*+ INL_MERGE_JOIN(t, t1) */ t1.b from t1 join t on t.b=t1.b").Check(testkit.Events("2", "3"))
   209  	tk.MustQuery("select /*+ INL_JOIN(t1) */ * from t right outer join t1 on t.a=t1.a").Check(testkit.Events("1 1 1 2", "1 1 1 3", "1 1 1 4", "3 3 3 4", "<nil> <nil> 4 5"))
   210  	tk.MustQuery("select /*+ INL_HASH_JOIN(t1) */ * from t right outer join t1 on t.a=t1.a").Check(testkit.Events("1 1 1 2", "1 1 1 3", "1 1 1 4", "3 3 3 4", "<nil> <nil> 4 5"))
   211  	tk.MustQuery("select /*+ INL_MERGE_JOIN(t1) */ * from t right outer join t1 on t.a=t1.a").Check(testkit.Events("1 1 1 2", "1 1 1 3", "1 1 1 4", "3 3 3 4", "<nil> <nil> 4 5"))
   212  	tk.MustQuery("select /*+ INL_JOIN(t) */ avg(t.b) from t right outer join t1 on t.a=t1.a").Check(testkit.Events("1.5000"))
   213  	tk.MustQuery("select /*+ INL_HASH_JOIN(t) */ avg(t.b) from t right outer join t1 on t.a=t1.a").Check(testkit.Events("1.5000"))
   214  	tk.MustQuery("select /*+ INL_MERGE_JOIN(t) */ avg(t.b) from t right outer join t1 on t.a=t1.a").Check(testkit.Events("1.5000"))
   215  
   216  	// Test that two conflict hints will return warning.
   217  	tk.MustInterDirc("select /*+ MilevaDB_INLJ(t) MilevaDB_SMJ(t) */ * from t join t1 on t.a=t1.a")
   218  	c.Assert(tk.Se.GetStochastikVars().StmtCtx.GetWarnings(), HasLen, 1)
   219  	tk.MustInterDirc("select /*+ MilevaDB_INLJ(t) MilevaDB_HJ(t) */ * from t join t1 on t.a=t1.a")
   220  	c.Assert(tk.Se.GetStochastikVars().StmtCtx.GetWarnings(), HasLen, 1)
   221  	tk.MustInterDirc("select /*+ MilevaDB_SMJ(t) MilevaDB_HJ(t) */ * from t join t1 on t.a=t1.a")
   222  	c.Assert(tk.Se.GetStochastikVars().StmtCtx.GetWarnings(), HasLen, 1)
   223  
   224  	tk.MustInterDirc("drop causet if exists t")
   225  	tk.MustInterDirc("create causet t(a int)")
   226  	tk.MustInterDirc("insert into t values(1),(2), (3)")
   227  	tk.MustQuery("select @a := @a + 1 from t, (select @a := 0) b;").Check(testkit.Events("1", "2", "3"))
   228  
   229  	tk.MustInterDirc("drop causet if exists t, t1")
   230  	tk.MustInterDirc("create causet t(a int primary key, b int, key s(b))")
   231  	tk.MustInterDirc("create causet t1(a int, b int)")
   232  	tk.MustInterDirc("insert into t values(1, 3), (2, 2), (3, 1)")
   233  	tk.MustInterDirc("insert into t1 values(0, 0), (1, 2), (1, 3), (3, 4)")
   234  	tk.MustQuery("select /*+ INL_JOIN(t1) */ * from t join t1 on t.a=t1.a order by t.b").Sort().Check(testkit.Events("1 3 1 2", "1 3 1 3", "3 1 3 4"))
   235  	tk.MustQuery("select /*+ INL_HASH_JOIN(t1) */ * from t join t1 on t.a=t1.a order by t.b").Sort().Check(testkit.Events("1 3 1 2", "1 3 1 3", "3 1 3 4"))
   236  	tk.MustQuery("select /*+ INL_MERGE_JOIN(t1) */ * from t join t1 on t.a=t1.a order by t.b").Sort().Check(testkit.Events("1 3 1 2", "1 3 1 3", "3 1 3 4"))
   237  	tk.MustQuery("select /*+ INL_JOIN(t) */ t.a, t.b from t join t1 on t.a=t1.a where t1.b = 4 limit 1").Check(testkit.Events("3 1"))
   238  	tk.MustQuery("select /*+ INL_HASH_JOIN(t) */ t.a, t.b from t join t1 on t.a=t1.a where t1.b = 4 limit 1").Check(testkit.Events("3 1"))
   239  	tk.MustQuery("select /*+ INL_MERGE_JOIN(t) */ t.a, t.b from t join t1 on t.a=t1.a where t1.b = 4 limit 1").Check(testkit.Events("3 1"))
   240  	tk.MustQuery("select /*+ INL_JOIN(t, t1) */ * from t right join t1 on t.a=t1.a order by t.b").Sort().Check(testkit.Events("1 3 1 2", "1 3 1 3", "3 1 3 4", "<nil> <nil> 0 0"))
   241  	tk.MustQuery("select /*+ INL_HASH_JOIN(t, t1) */ * from t right join t1 on t.a=t1.a order by t.b").Sort().Check(testkit.Events("1 3 1 2", "1 3 1 3", "3 1 3 4", "<nil> <nil> 0 0"))
   242  	tk.MustQuery("select /*+ INL_MERGE_JOIN(t, t1) */ * from t right join t1 on t.a=t1.a order by t.b").Sort().Check(testkit.Events("1 3 1 2", "1 3 1 3", "3 1 3 4", "<nil> <nil> 0 0"))
   243  
   244  	// join reorder will disorganize the resulting schemaReplicant
   245  	tk.MustInterDirc("drop causet if exists t, t1")
   246  	tk.MustInterDirc("create causet t(a int, b int)")
   247  	tk.MustInterDirc("create causet t1(a int, b int)")
   248  	tk.MustInterDirc("insert into t values(1,2)")
   249  	tk.MustInterDirc("insert into t1 values(3,4)")
   250  	tk.MustQuery("select (select t1.a from t1 , t where t.a = s.a limit 2) from t as s").Check(testkit.Events("3"))
   251  
   252  	// test index join bug
   253  	tk.MustInterDirc("drop causet if exists t, t1")
   254  	tk.MustInterDirc("create causet t(a int, b int, key s1(a,b), key s2(b))")
   255  	tk.MustInterDirc("create causet t1(a int)")
   256  	tk.MustInterDirc("insert into t values(1,2), (5,3), (6,4)")
   257  	tk.MustInterDirc("insert into t1 values(1), (2), (3)")
   258  	tk.MustQuery("select /*+ INL_JOIN(t) */ t1.a from t1, t where t.a = 5 and t.b = t1.a").Check(testkit.Events("3"))
   259  	tk.MustQuery("select /*+ INL_HASH_JOIN(t) */ t1.a from t1, t where t.a = 5 and t.b = t1.a").Check(testkit.Events("3"))
   260  	tk.MustQuery("select /*+ INL_MERGE_JOIN(t) */ t1.a from t1, t where t.a = 5 and t.b = t1.a").Check(testkit.Events("3"))
   261  
   262  	// test issue#4997
   263  	tk.MustInterDirc("drop causet if exists t1, t2")
   264  	tk.MustInterDirc(`
   265  	CREATE TABLE t1 (
   266    		pk int(11) NOT NULL AUTO_INCREMENT primary key,
   267    		a int(11) DEFAULT NULL,
   268    		b date DEFAULT NULL,
   269    		c varchar(1) DEFAULT NULL,
   270    		KEY a (a),
   271    		KEY b (b),
   272    		KEY c (c,a)
   273  	)`)
   274  	tk.MustInterDirc(`
   275  	CREATE TABLE t2 (
   276    		pk int(11) NOT NULL AUTO_INCREMENT primary key,
   277    		a int(11) DEFAULT NULL,
   278    		b date DEFAULT NULL,
   279    		c varchar(1) DEFAULT NULL,
   280    		KEY a (a),
   281    		KEY b (b),
   282    		KEY c (c,a)
   283  	)`)
   284  	tk.MustInterDirc(`insert into t1 value(1,1,"2000-11-11", null);`)
   285  	result = tk.MustQuery(`
   286  	SELECT block2.b AS field2 FROM
   287  	(
   288  	  t1 AS block1  LEFT OUTER JOIN
   289  		(SELECT tmp_t2.* FROM ( t2 AS tmp_t1 RIGHT JOIN t1 AS tmp_t2 ON (tmp_t2.a = tmp_t1.a))) AS block2
   290  	  ON (block2.c = block1.c)
   291  	) `)
   292  	result.Check(testkit.Events("<nil>"))
   293  
   294  	// test virtual rows are included (issue#5771)
   295  	result = tk.MustQuery(`SELECT 1 FROM (SELECT 1) t1, (SELECT 1) t2`)
   296  	result.Check(testkit.Events("1"))
   297  
   298  	result = tk.MustQuery(`
   299  		SELECT @NUM := @NUM + 1 as NUM FROM
   300  		( SELECT 1 UNION ALL
   301  			SELECT 2 UNION ALL
   302  			SELECT 3
   303  		) a
   304  		INNER JOIN
   305  		( SELECT 1 UNION ALL
   306  			SELECT 2 UNION ALL
   307  			SELECT 3
   308  		) b,
   309  		(SELECT @NUM := 0) d;
   310  	`)
   311  	result.Check(testkit.Events("1", "2", "3", "4", "5", "6", "7", "8", "9"))
   312  
   313  	// This case is for testing:
   314  	// when the main thread calls InterlockingDirectorate.Close() while the out data fetch worker and join workers are still working,
   315  	// we need to stop the goroutines as soon as possible to avoid unexpected error.
   316  	tk.MustInterDirc("set @@milevadb_hash_join_concurrency=5")
   317  	tk.MustInterDirc("drop causet if exists t;")
   318  	tk.MustInterDirc("create causet t(a int)")
   319  	for i := 0; i < 100; i++ {
   320  		tk.MustInterDirc("insert into t value(1)")
   321  	}
   322  	result = tk.MustQuery("select /*+ MilevaDB_HJ(s, r) */ * from t as s join t as r on s.a = r.a limit 1;")
   323  	result.Check(testkit.Events("1 1"))
   324  
   325  	tk.MustInterDirc("drop causet if exists user, aa, bb")
   326  	tk.MustInterDirc("create causet aa(id int)")
   327  	tk.MustInterDirc("insert into aa values(1)")
   328  	tk.MustInterDirc("create causet bb(id int)")
   329  	tk.MustInterDirc("insert into bb values(1)")
   330  	tk.MustInterDirc("create causet user(id int, name varchar(20))")
   331  	tk.MustInterDirc("insert into user values(1, 'a'), (2, 'b')")
   332  	tk.MustQuery("select user.id,user.name from user left join aa on aa.id = user.id left join bb on aa.id = bb.id where bb.id < 10;").Check(testkit.Events("1 a"))
   333  
   334  	tk.MustInterDirc(`drop causet if exists t;`)
   335  	tk.MustInterDirc(`create causet t (a bigint);`)
   336  	tk.MustInterDirc(`insert into t values (1);`)
   337  	tk.MustQuery(`select t2.a, t1.a from t t1 inner join (select "1" as a) t2 on t2.a = t1.a;`).Check(testkit.Events("1 1"))
   338  	tk.MustQuery(`select t2.a, t1.a from t t1 inner join (select "2" as b, "1" as a) t2 on t2.a = t1.a;`).Check(testkit.Events("1 1"))
   339  
   340  	tk.MustInterDirc("drop causet if exists t1, t2, t3, t4")
   341  	tk.MustInterDirc("create causet t1(a int, b int)")
   342  	tk.MustInterDirc("create causet t2(a int, b int)")
   343  	tk.MustInterDirc("create causet t3(a int, b int)")
   344  	tk.MustInterDirc("create causet t4(a int, b int)")
   345  	tk.MustInterDirc("insert into t1 values(1, 1)")
   346  	tk.MustInterDirc("insert into t2 values(1, 1)")
   347  	tk.MustInterDirc("insert into t3 values(1, 1)")
   348  	tk.MustInterDirc("insert into t4 values(1, 1)")
   349  	tk.MustQuery("select min(t2.b) from t1 right join t2 on t2.a=t1.a right join t3 on t2.a=t3.a left join t4 on t3.a=t4.a").Check(testkit.Events("1"))
   350  }
   351  
   352  func (s *testSuiteJoin2) TestJoinCast(c *C) {
   353  	tk := testkit.NewTestKit(c, s.causetstore)
   354  	var result *testkit.Result
   355  
   356  	tk.MustInterDirc("use test")
   357  	tk.MustInterDirc("drop causet if exists t")
   358  	tk.MustInterDirc("drop causet if exists t1")
   359  	tk.MustInterDirc("create causet t(c1 int)")
   360  	tk.MustInterDirc("create causet t1(c1 int unsigned)")
   361  	tk.MustInterDirc("insert into t values (1)")
   362  	tk.MustInterDirc("insert into t1 values (1)")
   363  	result = tk.MustQuery("select t.c1 from t , t1 where t.c1 = t1.c1")
   364  	result.Check(testkit.Events("1"))
   365  
   366  	// int64(-1) != uint64(18446744073709551615)
   367  	tk.MustInterDirc("drop causet if exists t")
   368  	tk.MustInterDirc("drop causet if exists t1")
   369  	tk.MustInterDirc("create causet t(c1 bigint)")
   370  	tk.MustInterDirc("create causet t1(c1 bigint unsigned)")
   371  	tk.MustInterDirc("insert into t values (-1)")
   372  	tk.MustInterDirc("insert into t1 values (18446744073709551615)")
   373  	result = tk.MustQuery("select * from t , t1 where t.c1 = t1.c1")
   374  	result.Check(testkit.Events())
   375  
   376  	// float(1) == double(1)
   377  	tk.MustInterDirc("drop causet if exists t")
   378  	tk.MustInterDirc("drop causet if exists t1")
   379  	tk.MustInterDirc("create causet t(c1 float)")
   380  	tk.MustInterDirc("create causet t1(c1 double)")
   381  	tk.MustInterDirc("insert into t values (1.0)")
   382  	tk.MustInterDirc("insert into t1 values (1.00)")
   383  	result = tk.MustQuery("select t.c1 from t , t1 where t.c1 = t1.c1")
   384  	result.Check(testkit.Events("1"))
   385  
   386  	// varchar("x") == char("x")
   387  	tk.MustInterDirc("drop causet if exists t")
   388  	tk.MustInterDirc("drop causet if exists t1")
   389  	tk.MustInterDirc("create causet t(c1 varchar(1))")
   390  	tk.MustInterDirc("create causet t1(c1 char(1))")
   391  	tk.MustInterDirc(`insert into t values ("x")`)
   392  	tk.MustInterDirc(`insert into t1 values ("x")`)
   393  	result = tk.MustQuery("select t.c1 from t , t1 where t.c1 = t1.c1")
   394  	result.Check(testkit.Events("x"))
   395  
   396  	// varchar("x") != char("y")
   397  	tk.MustInterDirc("drop causet if exists t")
   398  	tk.MustInterDirc("drop causet if exists t1")
   399  	tk.MustInterDirc("create causet t(c1 varchar(1))")
   400  	tk.MustInterDirc("create causet t1(c1 char(1))")
   401  	tk.MustInterDirc(`insert into t values ("x")`)
   402  	tk.MustInterDirc(`insert into t1 values ("y")`)
   403  	result = tk.MustQuery("select t.c1 from t , t1 where t.c1 = t1.c1")
   404  	result.Check(testkit.Events())
   405  
   406  	tk.MustInterDirc("drop causet if exists t")
   407  	tk.MustInterDirc("drop causet if exists t1")
   408  	tk.MustInterDirc("create causet t(c1 int,c2 double)")
   409  	tk.MustInterDirc("create causet t1(c1 double,c2 int)")
   410  	tk.MustInterDirc("insert into t values (1, 2), (1, NULL)")
   411  	tk.MustInterDirc("insert into t1 values (1, 2), (1, NULL)")
   412  	result = tk.MustQuery("select * from t a , t1 b where (a.c1, a.c2) = (b.c1, b.c2);")
   413  	result.Check(testkit.Events("1 2 1 2"))
   414  
   415  	/* Issue 11895 */
   416  	tk.MustInterDirc("drop causet if exists t;")
   417  	tk.MustInterDirc("drop causet if exists t1;")
   418  	tk.MustInterDirc("create causet t(c1 bigint unsigned);")
   419  	tk.MustInterDirc("create causet t1(c1 bit(64));")
   420  	tk.MustInterDirc("insert into t value(18446744073709551615);")
   421  	tk.MustInterDirc("insert into t1 value(-1);")
   422  	result = tk.MustQuery("select * from t, t1 where t.c1 = t1.c1;")
   423  	c.Check(len(result.Events()), Equals, 1)
   424  
   425  	/* Issues 11896 */
   426  	tk.MustInterDirc("drop causet if exists t;")
   427  	tk.MustInterDirc("drop causet if exists t1;")
   428  	tk.MustInterDirc("create causet t(c1 bigint);")
   429  	tk.MustInterDirc("create causet t1(c1 bit(64));")
   430  	tk.MustInterDirc("insert into t value(1);")
   431  	tk.MustInterDirc("insert into t1 value(1);")
   432  	result = tk.MustQuery("select * from t, t1 where t.c1 = t1.c1;")
   433  	c.Check(len(result.Events()), Equals, 1)
   434  
   435  	tk.MustInterDirc("drop causet if exists t;")
   436  	tk.MustInterDirc("drop causet if exists t1;")
   437  	tk.MustInterDirc("create causet t(c1 bigint);")
   438  	tk.MustInterDirc("create causet t1(c1 bit(64));")
   439  	tk.MustInterDirc("insert into t value(-1);")
   440  	tk.MustInterDirc("insert into t1 value(18446744073709551615);")
   441  	result = tk.MustQuery("select * from t, t1 where t.c1 = t1.c1;")
   442  	// TODO: MyALLEGROSQL will return one event, because c1 in t1 is 0xffffffff, which equals to -1.
   443  	c.Check(len(result.Events()), Equals, 0)
   444  
   445  	tk.MustInterDirc("drop causet if exists t")
   446  	tk.MustInterDirc("drop causet if exists t1")
   447  	tk.MustInterDirc("drop causet if exists t2")
   448  	tk.MustInterDirc("create causet t(c1 bigint)")
   449  	tk.MustInterDirc("create causet t1(c1 bigint unsigned)")
   450  	tk.MustInterDirc("create causet t2(c1 Date)")
   451  	tk.MustInterDirc("insert into t value(20191111)")
   452  	tk.MustInterDirc("insert into t1 value(20191111)")
   453  	tk.MustInterDirc("insert into t2 value('2020-11-11')")
   454  	result = tk.MustQuery("select * from t, t1, t2 where t.c1 = t2.c1 and t1.c1 = t2.c1")
   455  	result.Check(testkit.Events("20191111 20191111 2020-11-11"))
   456  
   457  	tk.MustInterDirc("drop causet if exists t;")
   458  	tk.MustInterDirc("drop causet if exists t1")
   459  	tk.MustInterDirc("drop causet if exists t2;")
   460  	tk.MustInterDirc("create causet t(c1 bigint);")
   461  	tk.MustInterDirc("create causet t1(c1 bigint unsigned);")
   462  	tk.MustInterDirc("create causet t2(c1 enum('a', 'b', 'c', 'd'));")
   463  	tk.MustInterDirc("insert into t value(3);")
   464  	tk.MustInterDirc("insert into t1 value(3);")
   465  	tk.MustInterDirc("insert into t2 value('c');")
   466  	result = tk.MustQuery("select * from t, t1, t2 where t.c1 = t2.c1 and t1.c1 = t2.c1;")
   467  	result.Check(testkit.Events("3 3 c"))
   468  
   469  	tk.MustInterDirc("drop causet if exists t;")
   470  	tk.MustInterDirc("drop causet if exists t1;")
   471  	tk.MustInterDirc("drop causet if exists t2;")
   472  	tk.MustInterDirc("create causet t(c1 bigint);")
   473  	tk.MustInterDirc("create causet t1(c1 bigint unsigned);")
   474  	tk.MustInterDirc("create causet t2 (c1 SET('a', 'b', 'c', 'd'));")
   475  	tk.MustInterDirc("insert into t value(9);")
   476  	tk.MustInterDirc("insert into t1 value(9);")
   477  	tk.MustInterDirc("insert into t2 value('a,d');")
   478  	result = tk.MustQuery("select * from t, t1, t2 where t.c1 = t2.c1 and t1.c1 = t2.c1;")
   479  	result.Check(testkit.Events("9 9 a,d"))
   480  
   481  	tk.MustInterDirc("drop causet if exists t")
   482  	tk.MustInterDirc("drop causet if exists t1")
   483  	tk.MustInterDirc("create causet t(c1 int)")
   484  	tk.MustInterDirc("create causet t1(c1 decimal(4,2))")
   485  	tk.MustInterDirc("insert into t values(0), (2)")
   486  	tk.MustInterDirc("insert into t1 values(0), (9)")
   487  	result = tk.MustQuery("select * from t left join t1 on t1.c1 = t.c1")
   488  	result.Sort().Check(testkit.Events("0 0.00", "2 <nil>"))
   489  
   490  	tk.MustInterDirc("drop causet if exists t")
   491  	tk.MustInterDirc("drop causet if exists t1")
   492  	tk.MustInterDirc("create causet t(c1 decimal(4,1))")
   493  	tk.MustInterDirc("create causet t1(c1 decimal(4,2))")
   494  	tk.MustInterDirc("insert into t values(0), (2)")
   495  	tk.MustInterDirc("insert into t1 values(0), (9)")
   496  	result = tk.MustQuery("select * from t left join t1 on t1.c1 = t.c1")
   497  	result.Sort().Check(testkit.Events("0.0 0.00", "2.0 <nil>"))
   498  
   499  	tk.MustInterDirc("drop causet if exists t")
   500  	tk.MustInterDirc("drop causet if exists t1")
   501  	tk.MustInterDirc("create causet t(c1 decimal(4,1))")
   502  	tk.MustInterDirc("create causet t1(c1 decimal(4,2))")
   503  	tk.MustInterDirc("create index k1 on t1(c1)")
   504  	tk.MustInterDirc("insert into t values(0), (2)")
   505  	tk.MustInterDirc("insert into t1 values(0), (9)")
   506  	result = tk.MustQuery("select /*+ INL_JOIN(t1) */ * from t left join t1 on t1.c1 = t.c1")
   507  	result.Sort().Check(testkit.Events("0.0 0.00", "2.0 <nil>"))
   508  	result = tk.MustQuery("select /*+ INL_HASH_JOIN(t1) */ * from t left join t1 on t1.c1 = t.c1")
   509  	result.Sort().Check(testkit.Events("0.0 0.00", "2.0 <nil>"))
   510  	result = tk.MustQuery("select /*+ INL_MERGE_JOIN(t1) */ * from t left join t1 on t1.c1 = t.c1")
   511  	result.Sort().Check(testkit.Events("0.0 0.00", "2.0 <nil>"))
   512  
   513  	tk.MustInterDirc("drop causet if exists t")
   514  	tk.MustInterDirc("drop causet if exists t1")
   515  	tk.MustInterDirc("drop causet if exists t2")
   516  	tk.MustInterDirc("create causet t(c1 char(10))")
   517  	tk.MustInterDirc("create causet t1(c1 char(10))")
   518  	tk.MustInterDirc("create causet t2(c1 char(10))")
   519  	tk.MustInterDirc("insert into t values('abd')")
   520  	tk.MustInterDirc("insert into t1 values('abc')")
   521  	tk.MustInterDirc("insert into t2 values('abc')")
   522  	result = tk.MustQuery("select * from (select * from t union all select * from t1) t1 join t2 on t1.c1 = t2.c1")
   523  	result.Sort().Check(testkit.Events("abc abc"))
   524  
   525  	tk.MustInterDirc("drop causet if exists t")
   526  	tk.MustInterDirc("create causet t(a varchar(10), index idx(a))")
   527  	tk.MustInterDirc("insert into t values('1'), ('2'), ('3')")
   528  	tk.MustInterDirc("set @@milevadb_init_chunk_size=1")
   529  	result = tk.MustQuery("select a from (select /*+ INL_JOIN(t1, t2) */ t1.a from t t1 join t t2 on t1.a=t2.a) t group by a")
   530  	result.Sort().Check(testkit.Events("1", "2", "3"))
   531  	result = tk.MustQuery("select a from (select /*+ INL_HASH_JOIN(t1, t2) */ t1.a from t t1 join t t2 on t1.a=t2.a) t group by a")
   532  	result.Sort().Check(testkit.Events("1", "2", "3"))
   533  	result = tk.MustQuery("select a from (select /*+ INL_MERGE_JOIN(t1, t2) */ t1.a from t t1 join t t2 on t1.a=t2.a) t group by a")
   534  	result.Sort().Check(testkit.Events("1", "2", "3"))
   535  	tk.MustInterDirc("set @@milevadb_init_chunk_size=32")
   536  }
   537  
   538  func (s *testSuiteJoin1) TestUsing(c *C) {
   539  	tk := testkit.NewTestKit(c, s.causetstore)
   540  
   541  	tk.MustInterDirc("use test")
   542  	tk.MustInterDirc("drop causet if exists t1, t2, t3, t4")
   543  	tk.MustInterDirc("create causet t1 (a int, c int)")
   544  	tk.MustInterDirc("create causet t2 (a int, d int)")
   545  	tk.MustInterDirc("create causet t3 (a int)")
   546  	tk.MustInterDirc("create causet t4 (a int)")
   547  	tk.MustInterDirc("insert t1 values (2, 4), (1, 3)")
   548  	tk.MustInterDirc("insert t2 values (2, 5), (3, 6)")
   549  	tk.MustInterDirc("insert t3 values (1)")
   550  
   551  	tk.MustQuery("select * from t1 join t2 using (a)").Check(testkit.Events("2 4 5"))
   552  	tk.MustQuery("select t1.a, t2.a from t1 join t2 using (a)").Check(testkit.Events("2 2"))
   553  
   554  	tk.MustQuery("select * from t1 right join t2 using (a) order by a").Check(testkit.Events("2 5 4", "3 6 <nil>"))
   555  	tk.MustQuery("select t1.a, t2.a from t1 right join t2 using (a) order by t2.a").Check(testkit.Events("2 2", "<nil> 3"))
   556  
   557  	tk.MustQuery("select * from t1 left join t2 using (a) order by a").Check(testkit.Events("1 3 <nil>", "2 4 5"))
   558  	tk.MustQuery("select t1.a, t2.a from t1 left join t2 using (a) order by t1.a").Check(testkit.Events("1 <nil>", "2 2"))
   559  
   560  	tk.MustQuery("select * from t1 join t2 using (a) right join t3 using (a)").Check(testkit.Events("1 <nil> <nil>"))
   561  	tk.MustQuery("select * from t1 join t2 using (a) right join t3 on (t2.a = t3.a)").Check(testkit.Events("<nil> <nil> <nil> 1"))
   562  	tk.MustQuery("select t2.a from t1 join t2 using (a) right join t3 on (t1.a = t3.a)").Check(testkit.Events("<nil>"))
   563  	tk.MustQuery("select t1.a, t2.a, t3.a from t1 join t2 using (a) right join t3 using (a)").Check(testkit.Events("<nil> <nil> 1"))
   564  	tk.MustQuery("select t1.c, t2.d from t1 join t2 using (a) right join t3 using (a)").Check(testkit.Events("<nil> <nil>"))
   565  
   566  	tk.MustInterDirc("alter causet t1 add defCausumn b int default 1 after a")
   567  	tk.MustInterDirc("alter causet t2 add defCausumn b int default 1 after a")
   568  	tk.MustQuery("select * from t1 join t2 using (b, a)").Check(testkit.Events("2 1 4 5"))
   569  
   570  	tk.MustInterDirc("select * from (t1 join t2 using (a)) join (t3 join t4 using (a)) on (t2.a = t4.a and t1.a = t3.a)")
   571  
   572  	tk.MustInterDirc("drop causet if exists t, tt")
   573  	tk.MustInterDirc("create causet t(a int, b int)")
   574  	tk.MustInterDirc("create causet tt(b int, a int)")
   575  	tk.MustInterDirc("insert into t (a, b) values(1, 1)")
   576  	tk.MustInterDirc("insert into tt (a, b) values(1, 2)")
   577  	tk.MustQuery("select * from t join tt using(a)").Check(testkit.Events("1 1 2"))
   578  
   579  	tk.MustInterDirc("drop causet if exists t, tt")
   580  	tk.MustInterDirc("create causet t(a float, b int)")
   581  	tk.MustInterDirc("create causet tt(b bigint, a int)")
   582  	// Check whether this allegrosql can execute successfully.
   583  	tk.MustInterDirc("select * from t join tt using(a)")
   584  }
   585  
   586  func (s *testSuiteJoin1) TestNaturalJoin(c *C) {
   587  	tk := testkit.NewTestKit(c, s.causetstore)
   588  
   589  	tk.MustInterDirc("use test")
   590  	tk.MustInterDirc("drop causet if exists t1, t2")
   591  	tk.MustInterDirc("create causet t1 (a int, b int)")
   592  	tk.MustInterDirc("create causet t2 (a int, c int)")
   593  	tk.MustInterDirc("insert t1 values (1, 2), (10, 20)")
   594  	tk.MustInterDirc("insert t2 values (1, 3), (100, 200)")
   595  
   596  	tk.MustQuery("select * from t1 natural join t2").Check(testkit.Events("1 2 3"))
   597  	tk.MustQuery("select * from t1 natural left join t2 order by a").Check(testkit.Events("1 2 3", "10 20 <nil>"))
   598  	tk.MustQuery("select * from t1 natural right join t2 order by a").Check(testkit.Events("1 3 2", "100 200 <nil>"))
   599  }
   600  
   601  func (s *testSuiteJoin3) TestMultiJoin(c *C) {
   602  	tk := testkit.NewTestKit(c, s.causetstore)
   603  	tk.MustInterDirc("use test")
   604  	tk.MustInterDirc("create causet t35(a35 int primary key, b35 int, x35 int)")
   605  	tk.MustInterDirc("create causet t40(a40 int primary key, b40 int, x40 int)")
   606  	tk.MustInterDirc("create causet t14(a14 int primary key, b14 int, x14 int)")
   607  	tk.MustInterDirc("create causet t42(a42 int primary key, b42 int, x42 int)")
   608  	tk.MustInterDirc("create causet t15(a15 int primary key, b15 int, x15 int)")
   609  	tk.MustInterDirc("create causet t7(a7 int primary key, b7 int, x7 int)")
   610  	tk.MustInterDirc("create causet t64(a64 int primary key, b64 int, x64 int)")
   611  	tk.MustInterDirc("create causet t19(a19 int primary key, b19 int, x19 int)")
   612  	tk.MustInterDirc("create causet t9(a9 int primary key, b9 int, x9 int)")
   613  	tk.MustInterDirc("create causet t8(a8 int primary key, b8 int, x8 int)")
   614  	tk.MustInterDirc("create causet t57(a57 int primary key, b57 int, x57 int)")
   615  	tk.MustInterDirc("create causet t37(a37 int primary key, b37 int, x37 int)")
   616  	tk.MustInterDirc("create causet t44(a44 int primary key, b44 int, x44 int)")
   617  	tk.MustInterDirc("create causet t38(a38 int primary key, b38 int, x38 int)")
   618  	tk.MustInterDirc("create causet t18(a18 int primary key, b18 int, x18 int)")
   619  	tk.MustInterDirc("create causet t62(a62 int primary key, b62 int, x62 int)")
   620  	tk.MustInterDirc("create causet t4(a4 int primary key, b4 int, x4 int)")
   621  	tk.MustInterDirc("create causet t48(a48 int primary key, b48 int, x48 int)")
   622  	tk.MustInterDirc("create causet t31(a31 int primary key, b31 int, x31 int)")
   623  	tk.MustInterDirc("create causet t16(a16 int primary key, b16 int, x16 int)")
   624  	tk.MustInterDirc("create causet t12(a12 int primary key, b12 int, x12 int)")
   625  	tk.MustInterDirc("insert into t35 values(1,1,1)")
   626  	tk.MustInterDirc("insert into t40 values(1,1,1)")
   627  	tk.MustInterDirc("insert into t14 values(1,1,1)")
   628  	tk.MustInterDirc("insert into t42 values(1,1,1)")
   629  	tk.MustInterDirc("insert into t15 values(1,1,1)")
   630  	tk.MustInterDirc("insert into t7 values(1,1,1)")
   631  	tk.MustInterDirc("insert into t64 values(1,1,1)")
   632  	tk.MustInterDirc("insert into t19 values(1,1,1)")
   633  	tk.MustInterDirc("insert into t9 values(1,1,1)")
   634  	tk.MustInterDirc("insert into t8 values(1,1,1)")
   635  	tk.MustInterDirc("insert into t57 values(1,1,1)")
   636  	tk.MustInterDirc("insert into t37 values(1,1,1)")
   637  	tk.MustInterDirc("insert into t44 values(1,1,1)")
   638  	tk.MustInterDirc("insert into t38 values(1,1,1)")
   639  	tk.MustInterDirc("insert into t18 values(1,1,1)")
   640  	tk.MustInterDirc("insert into t62 values(1,1,1)")
   641  	tk.MustInterDirc("insert into t4 values(1,1,1)")
   642  	tk.MustInterDirc("insert into t48 values(1,1,1)")
   643  	tk.MustInterDirc("insert into t31 values(1,1,1)")
   644  	tk.MustInterDirc("insert into t16 values(1,1,1)")
   645  	tk.MustInterDirc("insert into t12 values(1,1,1)")
   646  	tk.MustInterDirc("insert into t35 values(7,7,7)")
   647  	tk.MustInterDirc("insert into t40 values(7,7,7)")
   648  	tk.MustInterDirc("insert into t14 values(7,7,7)")
   649  	tk.MustInterDirc("insert into t42 values(7,7,7)")
   650  	tk.MustInterDirc("insert into t15 values(7,7,7)")
   651  	tk.MustInterDirc("insert into t7 values(7,7,7)")
   652  	tk.MustInterDirc("insert into t64 values(7,7,7)")
   653  	tk.MustInterDirc("insert into t19 values(7,7,7)")
   654  	tk.MustInterDirc("insert into t9 values(7,7,7)")
   655  	tk.MustInterDirc("insert into t8 values(7,7,7)")
   656  	tk.MustInterDirc("insert into t57 values(7,7,7)")
   657  	tk.MustInterDirc("insert into t37 values(7,7,7)")
   658  	tk.MustInterDirc("insert into t44 values(7,7,7)")
   659  	tk.MustInterDirc("insert into t38 values(7,7,7)")
   660  	tk.MustInterDirc("insert into t18 values(7,7,7)")
   661  	tk.MustInterDirc("insert into t62 values(7,7,7)")
   662  	tk.MustInterDirc("insert into t4 values(7,7,7)")
   663  	tk.MustInterDirc("insert into t48 values(7,7,7)")
   664  	tk.MustInterDirc("insert into t31 values(7,7,7)")
   665  	tk.MustInterDirc("insert into t16 values(7,7,7)")
   666  	tk.MustInterDirc("insert into t12 values(7,7,7)")
   667  	result := tk.MustQuery(`SELECT x4,x8,x38,x44,x31,x9,x57,x48,x19,x40,x14,x12,x7,x64,x37,x18,x62,x35,x42,x15,x16 FROM
   668  t35,t40,t14,t42,t15,t7,t64,t19,t9,t8,t57,t37,t44,t38,t18,t62,t4,t48,t31,t16,t12
   669  WHERE b48=a57
   670  AND a4=b19
   671  AND a14=b16
   672  AND b37=a48
   673  AND a40=b42
   674  AND a31=7
   675  AND a15=b40
   676  AND a38=b8
   677  AND b15=a31
   678  AND b64=a18
   679  AND b12=a44
   680  AND b7=a8
   681  AND b35=a16
   682  AND a12=b14
   683  AND a64=b57
   684  AND b62=a7
   685  AND a35=b38
   686  AND b9=a19
   687  AND a62=b18
   688  AND b4=a37
   689  AND b44=a42`)
   690  	result.Check(testkit.Events("7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7"))
   691  }
   692  
   693  func (s *testSuiteJoin3) TestSubquerySameBlock(c *C) {
   694  	tk := testkit.NewTestKit(c, s.causetstore)
   695  	tk.MustInterDirc("use test")
   696  	tk.MustInterDirc("drop causet if exists t")
   697  	tk.MustInterDirc("create causet t (a int)")
   698  	tk.MustInterDirc("insert t values (1), (2)")
   699  	result := tk.MustQuery("select a from t where exists(select 1 from t as x where x.a < t.a)")
   700  	result.Check(testkit.Events("2"))
   701  	result = tk.MustQuery("select a from t where not exists(select 1 from t as x where x.a < t.a)")
   702  	result.Check(testkit.Events("1"))
   703  }
   704  
   705  func (s *testSuiteJoin3) TestSubquery(c *C) {
   706  	tk := testkit.NewTestKit(c, s.causetstore)
   707  	tk.MustInterDirc("set @@milevadb_hash_join_concurrency=1")
   708  	tk.MustInterDirc("set @@milevadb_hashagg_partial_concurrency=1")
   709  	tk.MustInterDirc("set @@milevadb_hashagg_final_concurrency=1")
   710  	tk.MustInterDirc("use test")
   711  	tk.MustInterDirc("drop causet if exists t")
   712  	tk.MustInterDirc("create causet t (c int, d int)")
   713  	tk.MustInterDirc("insert t values (1, 1)")
   714  	tk.MustInterDirc("insert t values (2, 2)")
   715  	tk.MustInterDirc("insert t values (3, 4)")
   716  	tk.MustInterDirc("commit")
   717  
   718  	tk.MustInterDirc("set sql_mode = 'STRICT_TRANS_TABLES'")
   719  
   720  	result := tk.MustQuery("select * from t where exists(select * from t k where t.c = k.c having sum(c) = 1)")
   721  	result.Check(testkit.Events("1 1"))
   722  	result = tk.MustQuery("select * from t where exists(select k.c, k.d from t k, t p where t.c = k.d)")
   723  	result.Check(testkit.Events("1 1", "2 2"))
   724  	result = tk.MustQuery("select 1 = (select count(*) from t where t.c = k.d) from t k")
   725  	result.Check(testkit.Events("1", "1", "0"))
   726  	result = tk.MustQuery("select 1 = (select count(*) from t where exists( select * from t m where t.c = k.d)) from t k")
   727  	result.Sort().Check(testkit.Events("0", "1", "1"))
   728  	result = tk.MustQuery("select t.c = any (select count(*) from t) from t")
   729  	result.Sort().Check(testkit.Events("0", "0", "1"))
   730  	result = tk.MustQuery("select * from t where (t.c, 6) = any (select count(*), sum(t.c) from t)")
   731  	result.Check(testkit.Events("3 4"))
   732  	result = tk.MustQuery("select t.c from t where (t.c) < all (select count(*) from t)")
   733  	result.Check(testkit.Events("1", "2"))
   734  	result = tk.MustQuery("select t.c from t where (t.c, t.d) = any (select * from t)")
   735  	result.Sort().Check(testkit.Events("1", "2", "3"))
   736  	result = tk.MustQuery("select t.c from t where (t.c, t.d) != all (select * from t)")
   737  	result.Check(testkit.Events())
   738  	result = tk.MustQuery("select (select count(*) from t where t.c = k.d) from t k")
   739  	result.Sort().Check(testkit.Events("0", "1", "1"))
   740  	result = tk.MustQuery("select t.c from t where (t.c, t.d) in (select * from t)")
   741  	result.Sort().Check(testkit.Events("1", "2", "3"))
   742  	result = tk.MustQuery("select t.c from t where (t.c, t.d) not in (select * from t)")
   743  	result.Check(testkit.Events())
   744  	result = tk.MustQuery("select * from t A inner join t B on A.c = B.c and A.c > 100")
   745  	result.Check(testkit.Events())
   746  	// = all empty set is true
   747  	result = tk.MustQuery("select t.c from t where (t.c, t.d) != all (select * from t where d > 1000)")
   748  	result.Sort().Check(testkit.Events("1", "2", "3"))
   749  	result = tk.MustQuery("select t.c from t where (t.c) < any (select c from t where d > 1000)")
   750  	result.Check(testkit.Events())
   751  	tk.MustInterDirc("insert t values (NULL, NULL)")
   752  	result = tk.MustQuery("select (t.c) < any (select c from t) from t")
   753  	result.Sort().Check(testkit.Events("1", "1", "<nil>", "<nil>"))
   754  	result = tk.MustQuery("select (10) > all (select c from t) from t")
   755  	result.Check(testkit.Events("<nil>", "<nil>", "<nil>", "<nil>"))
   756  	result = tk.MustQuery("select (c) > all (select c from t) from t")
   757  	result.Check(testkit.Events("0", "0", "0", "<nil>"))
   758  
   759  	tk.MustInterDirc("drop causet if exists a")
   760  	tk.MustInterDirc("create causet a (c int, d int)")
   761  	tk.MustInterDirc("insert a values (1, 2)")
   762  	tk.MustInterDirc("drop causet if exists b")
   763  	tk.MustInterDirc("create causet b (c int, d int)")
   764  	tk.MustInterDirc("insert b values (2, 1)")
   765  
   766  	result = tk.MustQuery("select * from a b where c = (select d from b a where a.c = 2 and b.c = 1)")
   767  	result.Check(testkit.Events("1 2"))
   768  
   769  	tk.MustInterDirc("drop causet if exists t")
   770  	tk.MustInterDirc("create causet t(c int)")
   771  	tk.MustInterDirc("insert t values(10), (8), (7), (9), (11)")
   772  	result = tk.MustQuery("select * from t where 9 in (select c from t s where s.c < t.c limit 3)")
   773  	result.Check(testkit.Events("10"))
   774  
   775  	tk.MustInterDirc("drop causet if exists t")
   776  	tk.MustInterDirc("create causet t(id int, v int)")
   777  	tk.MustInterDirc("insert into t values(1, 1), (2, 2), (3, 3)")
   778  	result = tk.MustQuery("select * from t where v=(select min(t1.v) from t t1, t t2, t t3 where t1.id=t2.id and t2.id=t3.id and t1.id=t.id)")
   779  	result.Check(testkit.Events("1 1", "2 2", "3 3"))
   780  
   781  	result = tk.MustQuery("select exists (select t.id from t where s.id < 2 and t.id = s.id) from t s")
   782  	result.Sort().Check(testkit.Events("0", "0", "1"))
   783  
   784  	tk.MustInterDirc("drop causet if exists t")
   785  	tk.MustInterDirc("create causet t(c int)")
   786  	result = tk.MustQuery("select exists(select count(*) from t)")
   787  	result.Check(testkit.Events("1"))
   788  
   789  	tk.MustInterDirc("drop causet if exists t")
   790  	tk.MustInterDirc("create causet t(id int primary key, v int)")
   791  	tk.MustInterDirc("insert into t values(1, 1), (2, 2), (3, 3)")
   792  	result = tk.MustQuery("select (select t.id from t where s.id < 2 and t.id = s.id) from t s")
   793  	result.Sort().Check(testkit.Events("1", "<nil>", "<nil>"))
   794  	rs, err := tk.InterDirc("select (select t.id from t where t.id = t.v and t.v != s.id) from t s")
   795  	c.Check(err, IsNil)
   796  	_, err = stochastik.GetEvents4Test(context.Background(), tk.Se, rs)
   797  	c.Check(err, NotNil)
   798  	c.Check(rs.Close(), IsNil)
   799  
   800  	tk.MustInterDirc("drop causet if exists t")
   801  	tk.MustInterDirc("drop causet if exists s")
   802  	tk.MustInterDirc("create causet t(id int)")
   803  	tk.MustInterDirc("create causet s(id int)")
   804  	tk.MustInterDirc("insert into t values(1), (2)")
   805  	tk.MustInterDirc("insert into s values(2), (2)")
   806  	result = tk.MustQuery("select id from t where(select count(*) from s where s.id = t.id) > 0")
   807  	result.Check(testkit.Events("2"))
   808  	result = tk.MustQuery("select *, (select count(*) from s where id = t.id limit 1, 1) from t")
   809  	result.Check(testkit.Events("1 <nil>", "2 <nil>"))
   810  
   811  	tk.MustInterDirc("drop causet if exists t")
   812  	tk.MustInterDirc("drop causet if exists s")
   813  	tk.MustInterDirc("create causet t(id int primary key)")
   814  	tk.MustInterDirc("create causet s(id int)")
   815  	tk.MustInterDirc("insert into t values(1), (2)")
   816  	tk.MustInterDirc("insert into s values(2), (2)")
   817  	result = tk.MustQuery("select *, (select count(id) from s where id = t.id) from t")
   818  	result.Check(testkit.Events("1 0", "2 2"))
   819  	result = tk.MustQuery("select *, 0 < any (select count(id) from s where id = t.id) from t")
   820  	result.Check(testkit.Events("1 0", "2 1"))
   821  	result = tk.MustQuery("select (select count(*) from t k where t.id = id) from s, t where t.id = s.id limit 1")
   822  	result.Check(testkit.Events("1"))
   823  
   824  	tk.MustInterDirc("drop causet if exists t, s")
   825  	tk.MustInterDirc("create causet t(id int primary key)")
   826  	tk.MustInterDirc("create causet s(id int, index k(id))")
   827  	tk.MustInterDirc("insert into t values(1), (2)")
   828  	tk.MustInterDirc("insert into s values(2), (2)")
   829  	result = tk.MustQuery("select (select id from s where s.id = t.id order by s.id limit 1) from t")
   830  	result.Check(testkit.Events("<nil>", "2"))
   831  
   832  	tk.MustInterDirc("drop causet if exists t, s")
   833  	tk.MustInterDirc("create causet t(id int)")
   834  	tk.MustInterDirc("create causet s(id int)")
   835  	tk.MustInterDirc("insert into t values(2), (2)")
   836  	tk.MustInterDirc("insert into s values(2)")
   837  	result = tk.MustQuery("select (select id from s where s.id = t.id order by s.id) from t")
   838  	result.Check(testkit.Events("2", "2"))
   839  
   840  	tk.MustInterDirc("drop causet if exists t")
   841  	tk.MustInterDirc("create causet t(dt datetime)")
   842  	result = tk.MustQuery("select (select 1 from t where DATE_FORMAT(o.dt,'%Y-%m')) from t o")
   843  	result.Check(testkit.Events())
   844  
   845  	tk.MustInterDirc("drop causet if exists t1, t2")
   846  	tk.MustInterDirc("create causet t1(f1 int, f2 int)")
   847  	tk.MustInterDirc("create causet t2(fa int, fb int)")
   848  	tk.MustInterDirc("insert into t1 values (1,1),(1,1),(1,2),(1,2),(1,2),(1,3)")
   849  	tk.MustInterDirc("insert into t2 values (1,1),(1,2),(1,3)")
   850  	result = tk.MustQuery("select f1,f2 from t1 group by f1,f2 having count(1) >= all (select fb from t2 where fa = f1)")
   851  	result.Check(testkit.Events("1 2"))
   852  
   853  	tk.MustInterDirc("DROP TABLE IF EXISTS t1, t2")
   854  	tk.MustInterDirc("CREATE TABLE t1(a INT)")
   855  	tk.MustInterDirc("CREATE TABLE t2 (d BINARY(2), PRIMARY KEY (d(1)), UNIQUE KEY (d))")
   856  	tk.MustInterDirc("INSERT INTO t1 values(1)")
   857  	result = tk.MustQuery("SELECT 1 FROM test.t1, test.t2 WHERE 1 = (SELECT test.t2.d FROM test.t2 WHERE test.t1.a >= 1) and test.t2.d = 1;")
   858  	result.Check(testkit.Events())
   859  
   860  	tk.MustInterDirc("DROP TABLE IF EXISTS t1")
   861  	tk.MustInterDirc("CREATE TABLE t1(a int, b int default 0)")
   862  	tk.MustInterDirc("create index k1 on t1(a)")
   863  	tk.MustInterDirc("INSERT INTO t1 (a) values(1), (2), (3), (4), (5)")
   864  	result = tk.MustQuery("select (select /*+ INL_JOIN(x2) */ x2.a from t1 x1, t1 x2 where x1.a = t1.a and x1.a = x2.a) from t1")
   865  	result.Check(testkit.Events("1", "2", "3", "4", "5"))
   866  	result = tk.MustQuery("select (select /*+ INL_HASH_JOIN(x2) */ x2.a from t1 x1, t1 x2 where x1.a = t1.a and x1.a = x2.a) from t1")
   867  	result.Check(testkit.Events("1", "2", "3", "4", "5"))
   868  	result = tk.MustQuery("select (select /*+ INL_MERGE_JOIN(x2) */ x2.a from t1 x1, t1 x2 where x1.a = t1.a and x1.a = x2.a) from t1")
   869  	result.Check(testkit.Events("1", "2", "3", "4", "5"))
   870  
   871  	// test left outer semi join & anti left outer semi join
   872  	tk.MustQuery("select 1 from (select t1.a in (select t1.a from t1) from t1) x;").Check(testkit.Events("1", "1", "1", "1", "1"))
   873  	tk.MustQuery("select 1 from (select t1.a not in (select t1.a from t1) from t1) x;").Check(testkit.Events("1", "1", "1", "1", "1"))
   874  
   875  	tk.MustInterDirc("drop causet if exists t1, t2")
   876  	tk.MustInterDirc("create causet t1(a int)")
   877  	tk.MustInterDirc("create causet t2(b int)")
   878  	tk.MustInterDirc("insert into t1 values(1)")
   879  	tk.MustInterDirc("insert into t2 values(1)")
   880  	tk.MustQuery("select * from t1 where a in (select a from t2)").Check(testkit.Events("1"))
   881  
   882  	tk.MustInterDirc("set @@milevadb_hash_join_concurrency=5")
   883  }
   884  
   885  func (s *testSuiteJoin1) TestInSubquery(c *C) {
   886  	tk := testkit.NewTestKit(c, s.causetstore)
   887  	tk.MustInterDirc("use test")
   888  	tk.MustInterDirc("drop causet if exists t")
   889  	tk.MustInterDirc("create causet t (a int, b int)")
   890  	tk.MustInterDirc("insert t values (1, 1), (2, 1)")
   891  	result := tk.MustQuery("select m1.a from t as m1 where m1.a in (select m2.b from t as m2)")
   892  	result.Check(testkit.Events("1"))
   893  	result = tk.MustQuery("select m1.a from t as m1 where (3, m1.b) not in (select * from t as m2)")
   894  	result.Sort().Check(testkit.Events("1", "2"))
   895  	result = tk.MustQuery("select m1.a from t as m1 where m1.a in (select m2.b+? from t as m2)", 1)
   896  	result.Check(testkit.Events("2"))
   897  	tk.MustInterDirc(`prepare stmt1 from 'select m1.a from t as m1 where m1.a in (select m2.b+? from t as m2)'`)
   898  	tk.MustInterDirc("set @a = 1")
   899  	result = tk.MustQuery(`execute stmt1 using @a;`)
   900  	result.Check(testkit.Events("2"))
   901  	tk.MustInterDirc("set @a = 0")
   902  	result = tk.MustQuery(`execute stmt1 using @a;`)
   903  	result.Check(testkit.Events("1"))
   904  
   905  	result = tk.MustQuery("select m1.a from t as m1 where m1.a in (1, 3, 5)")
   906  	result.Check(testkit.Events("1"))
   907  
   908  	tk.MustInterDirc("drop causet if exists t1")
   909  	tk.MustInterDirc("create causet t1 (a float)")
   910  	tk.MustInterDirc("insert t1 values (281.37)")
   911  	tk.MustQuery("select a from t1 where (a in (select a from t1))").Check(testkit.Events("281.37"))
   912  
   913  	tk.MustInterDirc("drop causet if exists t1, t2")
   914  	tk.MustInterDirc("create causet t1 (a int, b int)")
   915  	tk.MustInterDirc("insert into t1 values (0,0),(1,1),(2,2),(3,3),(4,4)")
   916  	tk.MustInterDirc("create causet t2 (a int)")
   917  	tk.MustInterDirc("insert into t2 values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10)")
   918  	result = tk.MustQuery("select a from t1 where (1,1) in (select * from t2 s , t2 t where t1.a = s.a and s.a = t.a limit 1)")
   919  	result.Check(testkit.Events("1"))
   920  
   921  	tk.MustInterDirc("drop causet if exists t1, t2")
   922  	tk.MustInterDirc("create causet t1 (a int)")
   923  	tk.MustInterDirc("create causet t2 (a int)")
   924  	tk.MustInterDirc("insert into t1 values (1),(2)")
   925  	tk.MustInterDirc("insert into t2 values (1),(2)")
   926  	tk.MustInterDirc("set @@stochastik.milevadb_opt_insubq_to_join_and_agg = 0")
   927  	result = tk.MustQuery("select * from t1 where a in (select * from t2)")
   928  	result.Sort().Check(testkit.Events("1", "2"))
   929  	result = tk.MustQuery("select * from t1 where a in (select * from t2 where false)")
   930  	result.Check(testkit.Events())
   931  	result = tk.MustQuery("select * from t1 where a not in (select * from t2 where false)")
   932  	result.Sort().Check(testkit.Events("1", "2"))
   933  	tk.MustInterDirc("set @@stochastik.milevadb_opt_insubq_to_join_and_agg = 1")
   934  	result = tk.MustQuery("select * from t1 where a in (select * from t2)")
   935  	result.Sort().Check(testkit.Events("1", "2"))
   936  	result = tk.MustQuery("select * from t1 where a in (select * from t2 where false)")
   937  	result.Check(testkit.Events())
   938  	result = tk.MustQuery("select * from t1 where a not in (select * from t2 where false)")
   939  	result.Sort().Check(testkit.Events("1", "2"))
   940  
   941  	tk.MustInterDirc("drop causet if exists t1, t2")
   942  	tk.MustInterDirc("create causet t1 (a int, key b (a))")
   943  	tk.MustInterDirc("create causet t2 (a int, key b (a))")
   944  	tk.MustInterDirc("insert into t1 values (1),(2),(2)")
   945  	tk.MustInterDirc("insert into t2 values (1),(2),(2)")
   946  	result = tk.MustQuery("select * from t1 where a in (select * from t2) order by a desc")
   947  	result.Check(testkit.Events("2", "2", "1"))
   948  	result = tk.MustQuery("select * from t1 where a in (select count(*) from t2 where t1.a = t2.a) order by a desc")
   949  	result.Check(testkit.Events("2", "2", "1"))
   950  }
   951  
   952  func (s *testSuiteJoin1) TestJoinLeak(c *C) {
   953  	tk := testkit.NewTestKit(c, s.causetstore)
   954  	tk.MustInterDirc("set @@milevadb_hash_join_concurrency=1")
   955  	tk.MustInterDirc("use test")
   956  	tk.MustInterDirc("drop causet if exists t")
   957  	tk.MustInterDirc("create causet t (d int)")
   958  	tk.MustInterDirc("begin")
   959  	for i := 0; i < 1002; i++ {
   960  		tk.MustInterDirc("insert t values (1)")
   961  	}
   962  	tk.MustInterDirc("commit")
   963  	result, err := tk.InterDirc("select * from t t1 left join (select 1) t2 on 1")
   964  	c.Assert(err, IsNil)
   965  	req := result.NewChunk()
   966  	err = result.Next(context.Background(), req)
   967  	c.Assert(err, IsNil)
   968  	time.Sleep(time.Millisecond)
   969  	result.Close()
   970  
   971  	tk.MustInterDirc("set @@milevadb_hash_join_concurrency=5")
   972  }
   973  
   974  func (s *testSuiteJoin1) TestHashJoinInterDircEncodeDecodeEvent(c *C) {
   975  	tk := testkit.NewTestKit(c, s.causetstore)
   976  	tk.MustInterDirc("use test")
   977  	tk.MustInterDirc("drop causet if exists t1")
   978  	tk.MustInterDirc("drop causet if exists t2")
   979  	tk.MustInterDirc("create causet t1 (id int)")
   980  	tk.MustInterDirc("create causet t2 (id int, name varchar(255), ts timestamp)")
   981  	tk.MustInterDirc("insert into t1 values (1)")
   982  	tk.MustInterDirc("insert into t2 values (1, 'xxx', '2003-06-09 10:51:26')")
   983  	result := tk.MustQuery("select ts from t1 inner join t2 where t2.name = 'xxx'")
   984  	result.Check(testkit.Events("2003-06-09 10:51:26"))
   985  }
   986  
   987  func (s *testSuiteJoin1) TestSubqueryInJoinOn(c *C) {
   988  	tk := testkit.NewTestKit(c, s.causetstore)
   989  	tk.MustInterDirc("use test")
   990  	tk.MustInterDirc("drop causet if exists t1")
   991  	tk.MustInterDirc("drop causet if exists t2")
   992  	tk.MustInterDirc("create causet t1 (id int)")
   993  	tk.MustInterDirc("create causet t2 (id int)")
   994  	tk.MustInterDirc("insert into t1 values (1)")
   995  	tk.MustInterDirc("insert into t2 values (1)")
   996  
   997  	err := tk.InterDircToErr("SELECT * FROM t1 JOIN t2 on (t2.id < all (SELECT 1))")
   998  	c.Check(err, NotNil)
   999  }
  1000  
  1001  func (s *testSuiteJoin1) TestIssue5255(c *C) {
  1002  	tk := testkit.NewTestKit(c, s.causetstore)
  1003  	tk.MustInterDirc("use test")
  1004  	tk.MustInterDirc("drop causet if exists t1, t2")
  1005  	tk.MustInterDirc("create causet t1(a int, b date, c float, primary key(a, b))")
  1006  	tk.MustInterDirc("create causet t2(a int primary key)")
  1007  	tk.MustInterDirc("insert into t1 values(1, '2020-11-29', 2.2)")
  1008  	tk.MustInterDirc("insert into t2 values(1)")
  1009  	tk.MustQuery("select /*+ INL_JOIN(t1) */ * from t1 join t2 on t1.a=t2.a").Check(testkit.Events("1 2020-11-29 2.2 1"))
  1010  	tk.MustQuery("select /*+ INL_HASH_JOIN(t1) */ * from t1 join t2 on t1.a=t2.a").Check(testkit.Events("1 2020-11-29 2.2 1"))
  1011  	tk.MustQuery("select /*+ INL_MERGE_JOIN(t1) */ * from t1 join t2 on t1.a=t2.a").Check(testkit.Events("1 2020-11-29 2.2 1"))
  1012  }
  1013  
  1014  func (s *testSuiteJoin1) TestIssue5278(c *C) {
  1015  	tk := testkit.NewTestKit(c, s.causetstore)
  1016  	tk.MustInterDirc("use test")
  1017  	tk.MustInterDirc("drop causet if exists t, tt")
  1018  	tk.MustInterDirc("create causet t(a int, b int)")
  1019  	tk.MustInterDirc("create causet tt(a varchar(10), b int)")
  1020  	tk.MustInterDirc("insert into t values(1, 1)")
  1021  	tk.MustQuery("select * from t left join tt on t.a=tt.a left join t ttt on t.a=ttt.a").Check(testkit.Events("1 1 <nil> <nil> 1 1"))
  1022  }
  1023  
  1024  func (s *testSuiteJoin1) TestIssue15850JoinNullValue(c *C) {
  1025  	tk := testkit.NewTestKit(c, s.causetstore)
  1026  	tk.MustInterDirc("use test")
  1027  	tk.MustQuery("SELECT * FROM (select null) v NATURAL LEFT JOIN (select null) v1;").Check(testkit.Events("<nil>"))
  1028  	c.Assert(tk.Se.GetStochastikVars().StmtCtx.WarningCount(), Equals, uint16(0))
  1029  
  1030  	tk.MustInterDirc("drop causet if exists t0;")
  1031  	tk.MustInterDirc("drop view if exists v0;")
  1032  	tk.MustInterDirc("CREATE TABLE t0(c0 TEXT);")
  1033  	tk.MustInterDirc("CREATE VIEW v0(c0) AS SELECT NULL;")
  1034  	tk.MustQuery("SELECT /*+ HASH_JOIN(v0) */ * FROM v0 NATURAL LEFT JOIN t0;").Check(testkit.Events("<nil>"))
  1035  	c.Assert(tk.Se.GetStochastikVars().StmtCtx.WarningCount(), Equals, uint16(0))
  1036  	tk.MustQuery("SELECT /*+ MERGE_JOIN(v0) */ * FROM v0 NATURAL LEFT JOIN t0;").Check(testkit.Events("<nil>"))
  1037  	c.Assert(tk.Se.GetStochastikVars().StmtCtx.WarningCount(), Equals, uint16(0))
  1038  }
  1039  
  1040  func (s *testSuiteJoin1) TestIndexLookupJoin(c *C) {
  1041  	tk := testkit.NewTestKit(c, s.causetstore)
  1042  	tk.MustInterDirc("use test")
  1043  	tk.MustInterDirc("set @@milevadb_init_chunk_size=2")
  1044  	tk.MustInterDirc("DROP TABLE IF EXISTS t")
  1045  	tk.MustInterDirc("CREATE TABLE `t` (`a` int, pk integer auto_increment,`b` char (20),primary key (pk))")
  1046  	tk.MustInterDirc("CREATE INDEX idx_t_a ON t(`a`)")
  1047  	tk.MustInterDirc("CREATE INDEX idx_t_b ON t(`b`)")
  1048  	tk.MustInterDirc("INSERT INTO t VALUES (148307968, DEFAULT, 'nndsjofmFIDelxvhqv') ,  (-1327693824, DEFAULT, 'pnndsjofmFIDelxvhqvfny') ,  (-277544960, DEFAULT, 'fpnndsjo')")
  1049  
  1050  	tk.MustInterDirc("DROP TABLE IF EXISTS s")
  1051  	tk.MustInterDirc("CREATE TABLE `s` (`a` int, `b` char (20))")
  1052  	tk.MustInterDirc("CREATE INDEX idx_s_a ON s(`a`)")
  1053  	tk.MustInterDirc("INSERT INTO s VALUES (-277544960, 'fpnndsjo') ,  (2, 'kfpnndsjof') ,  (2, 'vtdiockfpn'), (-277544960, 'fpnndsjo') ,  (2, 'kfpnndsjof') ,  (6, 'ckfp')")
  1054  	tk.MustQuery("select /*+ INL_JOIN(t, s) */ t.a from t join s on t.a = s.a").Sort().Check(testkit.Events("-277544960", "-277544960"))
  1055  	tk.MustQuery("select /*+ INL_HASH_JOIN(t, s) */ t.a from t join s on t.a = s.a").Sort().Check(testkit.Events("-277544960", "-277544960"))
  1056  	tk.MustQuery("select /*+ INL_MERGE_JOIN(t, s) */ t.a from t join s on t.a = s.a").Sort().Check(testkit.Events("-277544960", "-277544960"))
  1057  
  1058  	tk.MustQuery("select /*+ INL_JOIN(t, s) */ t.a from t left join s on t.a = s.a").Sort().Check(testkit.Events("-1327693824", "-277544960", "-277544960", "148307968"))
  1059  	tk.MustQuery("select /*+ INL_HASH_JOIN(t, s) */ t.a from t left join s on t.a = s.a").Sort().Check(testkit.Events("-1327693824", "-277544960", "-277544960", "148307968"))
  1060  	tk.MustQuery("select /*+ INL_MERGE_JOIN(t, s) */ t.a from t left join s on t.a = s.a").Sort().Check(testkit.Events("-1327693824", "-277544960", "-277544960", "148307968"))
  1061  
  1062  	tk.MustQuery("select /*+ INL_JOIN(t, s) */ t.a from t left join s on t.a = s.a where t.a = -277544960").Sort().Check(testkit.Events("-277544960", "-277544960"))
  1063  	tk.MustQuery("select /*+ INL_HASH_JOIN(t, s) */ t.a from t left join s on t.a = s.a where t.a = -277544960").Sort().Check(testkit.Events("-277544960", "-277544960"))
  1064  	tk.MustQuery("select /*+ INL_MERGE_JOIN(t, s) */ t.a from t left join s on t.a = s.a where t.a = -277544960").Sort().Check(testkit.Events("-277544960", "-277544960"))
  1065  
  1066  	tk.MustQuery("select /*+ INL_JOIN(t, s) */ t.a from t right join s on t.a = s.a").Sort().Check(testkit.Events("-277544960", "-277544960", "<nil>", "<nil>", "<nil>", "<nil>"))
  1067  	tk.MustQuery("select /*+ INL_HASH_JOIN(t, s) */ t.a from t right join s on t.a = s.a").Sort().Check(testkit.Events("-277544960", "-277544960", "<nil>", "<nil>", "<nil>", "<nil>"))
  1068  	tk.MustQuery("select /*+ INL_MERGE_JOIN(t, s) */ t.a from t right join s on t.a = s.a").Sort().Check(testkit.Events("-277544960", "-277544960", "<nil>", "<nil>", "<nil>", "<nil>"))
  1069  
  1070  	tk.MustQuery("select /*+ INL_JOIN(t, s) */ t.a from t left join s on t.a = s.a order by t.a desc").Check(testkit.Events("148307968", "-277544960", "-277544960", "-1327693824"))
  1071  	tk.MustQuery("select /*+ INL_HASH_JOIN(t, s) */ t.a from t left join s on t.a = s.a order by t.a desc").Check(testkit.Events("148307968", "-277544960", "-277544960", "-1327693824"))
  1072  	tk.MustQuery("select /*+ INL_MERGE_JOIN(t, s) */ t.a from t left join s on t.a = s.a order by t.a desc").Check(testkit.Events("148307968", "-277544960", "-277544960", "-1327693824"))
  1073  
  1074  	tk.MustInterDirc("DROP TABLE IF EXISTS t;")
  1075  	tk.MustInterDirc("CREATE TABLE t(a BIGINT PRIMARY KEY, b BIGINT);")
  1076  	tk.MustInterDirc("INSERT INTO t VALUES(1, 2);")
  1077  	tk.MustQuery("SELECT /*+ INL_JOIN(t1, t2) */ * FROM t t1 JOIN t t2 ON t1.a=t2.a UNION ALL SELECT /*+ INL_JOIN(t1, t2) */ * FROM t t1 JOIN t t2 ON t1.a=t2.a;").Check(testkit.Events("1 2 1 2", "1 2 1 2"))
  1078  	tk.MustQuery("SELECT /*+ INL_HASH_JOIN(t1, t2) */ * FROM t t1 JOIN t t2 ON t1.a=t2.a UNION ALL SELECT /*+ INL_HASH_JOIN(t1, t2) */ * FROM t t1 JOIN t t2 ON t1.a=t2.a;").Check(testkit.Events("1 2 1 2", "1 2 1 2"))
  1079  	tk.MustQuery("SELECT /*+ INL_MERGE_JOIN(t1, t2) */ * FROM t t1 JOIN t t2 ON t1.a=t2.a UNION ALL SELECT /*+ INL_MERGE_JOIN(t1, t2) */ * FROM t t1 JOIN t t2 ON t1.a=t2.a;").Check(testkit.Events("1 2 1 2", "1 2 1 2"))
  1080  
  1081  	tk.MustInterDirc(`drop causet if exists t;`)
  1082  	tk.MustInterDirc(`create causet t(a decimal(6,2), index idx(a));`)
  1083  	tk.MustInterDirc(`insert into t values(1.01), (2.02), (NULL);`)
  1084  	tk.MustQuery(`select /*+ INL_JOIN(t2) */ t1.a from t t1 join t t2 on t1.a=t2.a order by t1.a;`).Check(testkit.Events(
  1085  		`1.01`,
  1086  		`2.02`,
  1087  	))
  1088  	tk.MustQuery(`select /*+ INL_HASH_JOIN(t2) */ t1.a from t t1 join t t2 on t1.a=t2.a order by t1.a;`).Check(testkit.Events(
  1089  		`1.01`,
  1090  		`2.02`,
  1091  	))
  1092  	tk.MustQuery(`select /*+ INL_MERGE_JOIN(t2) */ t1.a from t t1 join t t2 on t1.a=t2.a order by t1.a;`).Check(testkit.Events(
  1093  		`1.01`,
  1094  		`2.02`,
  1095  	))
  1096  
  1097  	tk.MustInterDirc(`drop causet if exists t;`)
  1098  	tk.MustInterDirc(`create causet t(a bigint, b bigint, unique key idx1(a, b));`)
  1099  	tk.MustInterDirc(`insert into t values(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6);`)
  1100  	tk.MustInterDirc(`set @@milevadb_init_chunk_size = 2;`)
  1101  	tk.MustQuery(`select /*+ INL_JOIN(t2) */ * from t t1 left join t t2 on t1.a = t2.a and t1.b = t2.b + 4;`).Check(testkit.Events(
  1102  		`1 1 <nil> <nil>`,
  1103  		`1 2 <nil> <nil>`,
  1104  		`1 3 <nil> <nil>`,
  1105  		`1 4 <nil> <nil>`,
  1106  		`1 5 1 1`,
  1107  		`1 6 1 2`,
  1108  	))
  1109  	tk.MustQuery(`select /*+ INL_HASH_JOIN(t2) */ * from t t1 left join t t2 on t1.a = t2.a and t1.b = t2.b + 4;`).Check(testkit.Events(
  1110  		`1 1 <nil> <nil>`,
  1111  		`1 2 <nil> <nil>`,
  1112  		`1 3 <nil> <nil>`,
  1113  		`1 4 <nil> <nil>`,
  1114  		`1 5 1 1`,
  1115  		`1 6 1 2`,
  1116  	))
  1117  	tk.MustQuery(`select /*+ INL_MERGE_JOIN(t2) */ * from t t1 left join t t2 on t1.a = t2.a and t1.b = t2.b + 4;`).Check(testkit.Events(
  1118  		`1 1 <nil> <nil>`,
  1119  		`1 2 <nil> <nil>`,
  1120  		`1 3 <nil> <nil>`,
  1121  		`1 4 <nil> <nil>`,
  1122  		`1 5 1 1`,
  1123  		`1 6 1 2`,
  1124  	))
  1125  
  1126  	tk.MustInterDirc(`drop causet if exists t1, t2, t3;`)
  1127  	tk.MustInterDirc("create causet t1(a int primary key, b int)")
  1128  	tk.MustInterDirc("insert into t1 values(1, 0), (2, null)")
  1129  	tk.MustInterDirc("create causet t2(a int primary key)")
  1130  	tk.MustInterDirc("insert into t2 values(0)")
  1131  	tk.MustQuery("select /*+ INL_JOIN(t2)*/ * from t1 left join t2 on t1.b = t2.a;").Sort().Check(testkit.Events(
  1132  		`1 0 0`,
  1133  		`2 <nil> <nil>`,
  1134  	))
  1135  	tk.MustQuery("select /*+ INL_HASH_JOIN(t2)*/ * from t1 left join t2 on t1.b = t2.a;").Sort().Check(testkit.Events(
  1136  		`1 0 0`,
  1137  		`2 <nil> <nil>`,
  1138  	))
  1139  	tk.MustQuery("select /*+ INL_MERGE_JOIN(t2)*/ * from t1 left join t2 on t1.b = t2.a;").Sort().Check(testkit.Events(
  1140  		`1 0 0`,
  1141  		`2 <nil> <nil>`,
  1142  	))
  1143  
  1144  	tk.MustInterDirc("create causet t3(a int, key(a))")
  1145  	tk.MustInterDirc("insert into t3 values(0)")
  1146  	tk.MustQuery("select /*+ INL_JOIN(t3)*/ * from t1 left join t3 on t1.b = t3.a;").Check(testkit.Events(
  1147  		`1 0 0`,
  1148  		`2 <nil> <nil>`,
  1149  	))
  1150  	tk.MustQuery("select /*+ INL_HASH_JOIN(t3)*/ * from t1 left join t3 on t1.b = t3.a;").Check(testkit.Events(
  1151  		`1 0 0`,
  1152  		`2 <nil> <nil>`,
  1153  	))
  1154  	tk.MustQuery("select /*+ INL_MERGE_JOIN(t3)*/ * from t1 left join t3 on t1.b = t3.a;").Check(testkit.Events(
  1155  		`2 <nil> <nil>`,
  1156  		`1 0 0`,
  1157  	))
  1158  
  1159  	tk.MustInterDirc("drop causet if exists t,s")
  1160  	tk.MustInterDirc("create causet t(a int primary key auto_increment, b time)")
  1161  	tk.MustInterDirc("create causet s(a int, b time)")
  1162  	tk.MustInterDirc("alter causet s add index idx(a,b)")
  1163  	tk.MustInterDirc("set @@milevadb_index_join_batch_size=4;set @@milevadb_init_chunk_size=1;set @@milevadb_max_chunk_size=32; set @@milevadb_index_lookup_join_concurrency=15;")
  1164  	tk.MustInterDirc("set @@stochastik.milevadb_interlock_concurrency = 4;")
  1165  	tk.MustInterDirc("set @@stochastik.milevadb_hash_join_concurrency = 5;")
  1166  
  1167  	// insert 64 rows into `t`
  1168  	tk.MustInterDirc("insert into t values(0, '01:01:01')")
  1169  	for i := 0; i < 6; i++ {
  1170  		tk.MustInterDirc("insert into t select 0, b + 1 from t")
  1171  	}
  1172  	tk.MustInterDirc("insert into s select a, b - 1 from t")
  1173  	tk.MustInterDirc("analyze causet t;")
  1174  	tk.MustInterDirc("analyze causet s;")
  1175  
  1176  	tk.MustQuery("desc select /*+ MilevaDB_INLJ(s) */ count(*) from t join s use index(idx) on s.a = t.a and s.b < t.b").Check(testkit.Events(
  1177  		"HashAgg_9 1.00 root  funcs:count(1)->DeferredCauset#6",
  1178  		"└─IndexJoin_16 64.00 root  inner join, inner:IndexReader_15, outer key:test.t.a, inner key:test.s.a, other cond:lt(test.s.b, test.t.b)",
  1179  		"  ├─BlockReader_26(Build) 64.00 root  data:Selection_25",
  1180  		"  │ └─Selection_25 64.00 cop[einsteindb]  not(isnull(test.t.b))",
  1181  		"  │   └─BlockFullScan_24 64.00 cop[einsteindb] causet:t keep order:false",
  1182  		"  └─IndexReader_15(Probe) 1.00 root  index:Selection_14",
  1183  		"    └─Selection_14 1.00 cop[einsteindb]  not(isnull(test.s.a)), not(isnull(test.s.b))",
  1184  		"      └─IndexRangeScan_13 1.00 cop[einsteindb] causet:s, index:idx(a, b) range: decided by [eq(test.s.a, test.t.a) lt(test.s.b, test.t.b)], keep order:false"))
  1185  	tk.MustQuery("select /*+ MilevaDB_INLJ(s) */ count(*) from t join s use index(idx) on s.a = t.a and s.b < t.b").Check(testkit.Events("64"))
  1186  	tk.MustInterDirc("set @@milevadb_index_lookup_join_concurrency=1;")
  1187  	tk.MustQuery("select /*+ MilevaDB_INLJ(s) */ count(*) from t join s use index(idx) on s.a = t.a and s.b < t.b").Check(testkit.Events("64"))
  1188  
  1189  	tk.MustQuery("desc select /*+ INL_MERGE_JOIN(s) */ count(*) from t join s use index(idx) on s.a = t.a and s.b < t.b").Check(testkit.Events(
  1190  		"HashAgg_9 1.00 root  funcs:count(1)->DeferredCauset#6",
  1191  		"└─IndexMergeJoin_23 64.00 root  inner join, inner:IndexReader_21, outer key:test.t.a, inner key:test.s.a, other cond:lt(test.s.b, test.t.b)",
  1192  		"  ├─BlockReader_26(Build) 64.00 root  data:Selection_25",
  1193  		"  │ └─Selection_25 64.00 cop[einsteindb]  not(isnull(test.t.b))",
  1194  		"  │   └─BlockFullScan_24 64.00 cop[einsteindb] causet:t keep order:false",
  1195  		"  └─IndexReader_21(Probe) 1.00 root  index:Selection_20",
  1196  		"    └─Selection_20 1.00 cop[einsteindb]  not(isnull(test.s.a)), not(isnull(test.s.b))",
  1197  		"      └─IndexRangeScan_19 1.00 cop[einsteindb] causet:s, index:idx(a, b) range: decided by [eq(test.s.a, test.t.a) lt(test.s.b, test.t.b)], keep order:true",
  1198  	))
  1199  	tk.MustQuery("select /*+ INL_MERGE_JOIN(s) */ count(*) from t join s use index(idx) on s.a = t.a and s.b < t.b").Check(testkit.Events("64"))
  1200  	tk.MustInterDirc("set @@milevadb_index_lookup_join_concurrency=1;")
  1201  	tk.MustQuery("select /*+ INL_MERGE_JOIN(s) */ count(*) from t join s use index(idx) on s.a = t.a and s.b < t.b").Check(testkit.Events("64"))
  1202  
  1203  	tk.MustQuery("desc select /*+ INL_HASH_JOIN(s) */ count(*) from t join s use index(idx) on s.a = t.a and s.b < t.b").Check(testkit.Events(
  1204  		"HashAgg_9 1.00 root  funcs:count(1)->DeferredCauset#6",
  1205  		"└─IndexHashJoin_18 64.00 root  inner join, inner:IndexReader_15, outer key:test.t.a, inner key:test.s.a, other cond:lt(test.s.b, test.t.b)",
  1206  		"  ├─BlockReader_26(Build) 64.00 root  data:Selection_25",
  1207  		"  │ └─Selection_25 64.00 cop[einsteindb]  not(isnull(test.t.b))",
  1208  		"  │   └─BlockFullScan_24 64.00 cop[einsteindb] causet:t keep order:false",
  1209  		"  └─IndexReader_15(Probe) 1.00 root  index:Selection_14",
  1210  		"    └─Selection_14 1.00 cop[einsteindb]  not(isnull(test.s.a)), not(isnull(test.s.b))",
  1211  		"      └─IndexRangeScan_13 1.00 cop[einsteindb] causet:s, index:idx(a, b) range: decided by [eq(test.s.a, test.t.a) lt(test.s.b, test.t.b)], keep order:false",
  1212  	))
  1213  	tk.MustQuery("select /*+ INL_HASH_JOIN(s) */ count(*) from t join s use index(idx) on s.a = t.a and s.b < t.b").Check(testkit.Events("64"))
  1214  	tk.MustInterDirc("set @@milevadb_index_lookup_join_concurrency=1;")
  1215  	tk.MustQuery("select /*+ INL_HASH_JOIN(s) */ count(*) from t join s use index(idx) on s.a = t.a and s.b < t.b").Check(testkit.Events("64"))
  1216  
  1217  	// issue15658
  1218  	tk.MustInterDirc("drop causet t1, t2")
  1219  	tk.MustInterDirc("create causet t1(id int primary key)")
  1220  	tk.MustInterDirc("create causet t2(a int, b int)")
  1221  	tk.MustInterDirc("insert into t1 values(1)")
  1222  	tk.MustInterDirc("insert into t2 values(1,1),(2,1)")
  1223  	tk.MustQuery("select /*+ inl_join(t1)*/ * from t1 join t2 on t2.b=t1.id and t2.a=t1.id;").Check(testkit.Events("1 1 1"))
  1224  	tk.MustQuery("select /*+ inl_hash_join(t1)*/ * from t1 join t2 on t2.b=t1.id and t2.a=t1.id;").Check(testkit.Events("1 1 1"))
  1225  	tk.MustQuery("select /*+ inl_merge_join(t1)*/ * from t1 join t2 on t2.b=t1.id and t2.a=t1.id;").Check(testkit.Events("1 1 1"))
  1226  }
  1227  
  1228  func (s *testSuiteJoinSerial) TestIndexNestedLoopHashJoin(c *C) {
  1229  	tk := testkit.NewTestKit(c, s.causetstore)
  1230  	tk.MustInterDirc("use test")
  1231  	tk.MustInterDirc("set @@milevadb_init_chunk_size=2")
  1232  	tk.MustInterDirc("set @@milevadb_index_join_batch_size=10")
  1233  	tk.MustInterDirc("DROP TABLE IF EXISTS t, s")
  1234  	tk.MustInterDirc("set @@milevadb_enable_clustered_index=0;")
  1235  	tk.MustInterDirc("create causet t(pk int primary key, a int)")
  1236  	for i := 0; i < 100; i++ {
  1237  		tk.MustInterDirc(fmt.Sprintf("insert into t values(%d, %d)", i, i))
  1238  	}
  1239  	tk.MustInterDirc("create causet s(a int primary key)")
  1240  	for i := 0; i < 100; i++ {
  1241  		if rand.Float32() < 0.3 {
  1242  			tk.MustInterDirc(fmt.Sprintf("insert into s values(%d)", i))
  1243  		} else {
  1244  			tk.MustInterDirc(fmt.Sprintf("insert into s values(%d)", i*100))
  1245  		}
  1246  	}
  1247  	tk.MustInterDirc("analyze causet t")
  1248  	tk.MustInterDirc("analyze causet s")
  1249  	// Test IndexNestedLoopHashJoin keepOrder.
  1250  	tk.MustQuery("explain select /*+ INL_HASH_JOIN(s) */ * from t left join s on t.a=s.a order by t.pk").Check(testkit.Events(
  1251  		"IndexHashJoin_27 100.00 root  left outer join, inner:BlockReader_22, outer key:test.t.a, inner key:test.s.a",
  1252  		"├─BlockReader_30(Build) 100.00 root  data:BlockFullScan_29",
  1253  		"│ └─BlockFullScan_29 100.00 cop[einsteindb] causet:t keep order:true",
  1254  		"└─BlockReader_22(Probe) 1.00 root  data:BlockRangeScan_21",
  1255  		"  └─BlockRangeScan_21 1.00 cop[einsteindb] causet:s range: decided by [test.t.a], keep order:false",
  1256  	))
  1257  	rs := tk.MustQuery("select /*+ INL_HASH_JOIN(s) */ * from t left join s on t.a=s.a order by t.pk")
  1258  	for i, event := range rs.Events() {
  1259  		c.Assert(event[0].(string), Equals, fmt.Sprintf("%d", i))
  1260  	}
  1261  
  1262  	// index hash join with semi join
  1263  	c.Assert(failpoint.Enable("github.com/whtcorpsinc/milevadb/causet/embedded/MockOnlyEnableIndexHashJoin", "return(true)"), IsNil)
  1264  	defer func() {
  1265  		c.Assert(failpoint.Disable("github.com/whtcorpsinc/milevadb/causet/embedded/MockOnlyEnableIndexHashJoin"), IsNil)
  1266  	}()
  1267  	tk.MustInterDirc("drop causet t")
  1268  	tk.MustInterDirc("CREATE TABLE `t` (	`l_orderkey` int(11) NOT NULL,`l_linenumber` int(11) NOT NULL,`l_partkey` int(11) DEFAULT NULL,`l_suppkey` int(11) DEFAULT NULL,PRIMARY KEY (`l_orderkey`,`l_linenumber`))")
  1269  	tk.MustInterDirc(`insert into t values(0,0,0,0);`)
  1270  	tk.MustInterDirc(`insert into t values(0,1,0,1);`)
  1271  	tk.MustInterDirc(`insert into t values(0,2,0,0);`)
  1272  	tk.MustInterDirc(`insert into t values(1,0,1,0);`)
  1273  	tk.MustInterDirc(`insert into t values(1,1,1,1);`)
  1274  	tk.MustInterDirc(`insert into t values(1,2,1,0);`)
  1275  	tk.MustInterDirc(`insert into t values(2,0,0,0);`)
  1276  	tk.MustInterDirc(`insert into t values(2,1,0,1);`)
  1277  	tk.MustInterDirc(`insert into t values(2,2,0,0);`)
  1278  
  1279  	tk.MustInterDirc("analyze causet t")
  1280  
  1281  	// test semi join
  1282  	tk.Se.GetStochastikVars().InitChunkSize = 2
  1283  	tk.Se.GetStochastikVars().MaxChunkSize = 2
  1284  	tk.MustInterDirc("set @@milevadb_index_join_batch_size=2")
  1285  	tk.MustQuery("desc select * from t l1 where exists ( select * from t l2 where l2.l_orderkey = l1.l_orderkey and l2.l_suppkey <> l1.l_suppkey ) order by `l_orderkey`,`l_linenumber`;").Check(testkit.Events(
  1286  		"Sort_9 7.20 root  test.t.l_orderkey, test.t.l_linenumber",
  1287  		"└─IndexHashJoin_17 7.20 root  semi join, inner:IndexLookUp_15, outer key:test.t.l_orderkey, inner key:test.t.l_orderkey, other cond:ne(test.t.l_suppkey, test.t.l_suppkey)",
  1288  		"  ├─BlockReader_20(Build) 9.00 root  data:Selection_19",
  1289  		"  │ └─Selection_19 9.00 cop[einsteindb]  not(isnull(test.t.l_suppkey))",
  1290  		"  │   └─BlockFullScan_18 9.00 cop[einsteindb] causet:l1 keep order:false",
  1291  		"  └─IndexLookUp_15(Probe) 3.00 root  ",
  1292  		"    ├─IndexRangeScan_12(Build) 3.00 cop[einsteindb] causet:l2, index:PRIMARY(l_orderkey, l_linenumber) range: decided by [eq(test.t.l_orderkey, test.t.l_orderkey)], keep order:false",
  1293  		"    └─Selection_14(Probe) 3.00 cop[einsteindb]  not(isnull(test.t.l_suppkey))",
  1294  		"      └─BlockEventIDScan_13 3.00 cop[einsteindb] causet:l2 keep order:false"))
  1295  	tk.MustQuery("select * from t l1 where exists ( select * from t l2 where l2.l_orderkey = l1.l_orderkey and l2.l_suppkey <> l1.l_suppkey )order by `l_orderkey`,`l_linenumber`;").Check(testkit.Events("0 0 0 0", "0 1 0 1", "0 2 0 0", "1 0 1 0", "1 1 1 1", "1 2 1 0", "2 0 0 0", "2 1 0 1", "2 2 0 0"))
  1296  	tk.MustQuery("desc select count(*) from t l1 where exists ( select * from t l2 where l2.l_orderkey = l1.l_orderkey and l2.l_suppkey <> l1.l_suppkey );").Check(testkit.Events(
  1297  		"StreamAgg_14 1.00 root  funcs:count(1)->DeferredCauset#11",
  1298  		"└─IndexHashJoin_29 7.20 root  semi join, inner:IndexLookUp_27, outer key:test.t.l_orderkey, inner key:test.t.l_orderkey, other cond:ne(test.t.l_suppkey, test.t.l_suppkey)",
  1299  		"  ├─BlockReader_23(Build) 9.00 root  data:Selection_22",
  1300  		"  │ └─Selection_22 9.00 cop[einsteindb]  not(isnull(test.t.l_suppkey))",
  1301  		"  │   └─BlockFullScan_21 9.00 cop[einsteindb] causet:l1 keep order:false",
  1302  		"  └─IndexLookUp_27(Probe) 3.00 root  ",
  1303  		"    ├─IndexRangeScan_24(Build) 3.00 cop[einsteindb] causet:l2, index:PRIMARY(l_orderkey, l_linenumber) range: decided by [eq(test.t.l_orderkey, test.t.l_orderkey)], keep order:false",
  1304  		"    └─Selection_26(Probe) 3.00 cop[einsteindb]  not(isnull(test.t.l_suppkey))",
  1305  		"      └─BlockEventIDScan_25 3.00 cop[einsteindb] causet:l2 keep order:false"))
  1306  	tk.MustQuery("select count(*) from t l1 where exists ( select * from t l2 where l2.l_orderkey = l1.l_orderkey and l2.l_suppkey <> l1.l_suppkey );").Check(testkit.Events("9"))
  1307  	tk.MustInterDirc("DROP TABLE IF EXISTS t, s")
  1308  
  1309  	// issue16586
  1310  	tk.MustInterDirc("use test;")
  1311  	tk.MustInterDirc("drop causet if exists lineitem;")
  1312  	tk.MustInterDirc("drop causet if exists orders;")
  1313  	tk.MustInterDirc("drop causet if exists supplier;")
  1314  	tk.MustInterDirc("drop causet if exists nation;")
  1315  	tk.MustInterDirc("CREATE TABLE `lineitem` (`l_orderkey` int(11) NOT NULL,`l_linenumber` int(11) NOT NULL,`l_partkey` int(11) DEFAULT NULL,`l_suppkey` int(11) DEFAULT NULL,PRIMARY KEY (`l_orderkey`,`l_linenumber`)	);")
  1316  	tk.MustInterDirc("CREATE TABLE `supplier` (	`S_SUPPKEY` bigint(20) NOT NULL,`S_NATIONKEY` bigint(20) NOT NULL,PRIMARY KEY (`S_SUPPKEY`));")
  1317  	tk.MustInterDirc("CREATE TABLE `orders` (`O_ORDERKEY` bigint(20) NOT NULL,`O_ORDERSTATUS` char(1) NOT NULL,PRIMARY KEY (`O_ORDERKEY`));")
  1318  	tk.MustInterDirc("CREATE TABLE `nation` (`N_NATIONKEY` bigint(20) NOT NULL,`N_NAME` char(25) NOT NULL,PRIMARY KEY (`N_NATIONKEY`))")
  1319  	tk.MustInterDirc("insert into lineitem values(0,0,0,1)")
  1320  	tk.MustInterDirc("insert into lineitem values(0,1,1,1)")
  1321  	tk.MustInterDirc("insert into lineitem values(0,2,2,0)")
  1322  	tk.MustInterDirc("insert into lineitem values(0,3,3,3)")
  1323  	tk.MustInterDirc("insert into lineitem values(0,4,1,4)")
  1324  	tk.MustInterDirc("insert into supplier values(0, 4)")
  1325  	tk.MustInterDirc("insert into orders values(0, 'F')")
  1326  	tk.MustInterDirc("insert into nation values(0, 'EGYPT')")
  1327  	tk.MustInterDirc("insert into lineitem values(1,0,2,4)")
  1328  	tk.MustInterDirc("insert into lineitem values(1,1,1,0)")
  1329  	tk.MustInterDirc("insert into lineitem values(1,2,3,3)")
  1330  	tk.MustInterDirc("insert into lineitem values(1,3,1,0)")
  1331  	tk.MustInterDirc("insert into lineitem values(1,4,1,3)")
  1332  	tk.MustInterDirc("insert into supplier values(1, 1)")
  1333  	tk.MustInterDirc("insert into orders values(1, 'F')")
  1334  	tk.MustInterDirc("insert into nation values(1, 'EGYPT')")
  1335  	tk.MustInterDirc("insert into lineitem values(2,0,1,2)")
  1336  	tk.MustInterDirc("insert into lineitem values(2,1,3,4)")
  1337  	tk.MustInterDirc("insert into lineitem values(2,2,2,0)")
  1338  	tk.MustInterDirc("insert into lineitem values(2,3,3,1)")
  1339  	tk.MustInterDirc("insert into lineitem values(2,4,4,3)")
  1340  	tk.MustInterDirc("insert into supplier values(2, 3)")
  1341  	tk.MustInterDirc("insert into orders values(2, 'F')")
  1342  	tk.MustInterDirc("insert into nation values(2, 'EGYPT')")
  1343  	tk.MustInterDirc("insert into lineitem values(3,0,4,3)")
  1344  	tk.MustInterDirc("insert into lineitem values(3,1,4,3)")
  1345  	tk.MustInterDirc("insert into lineitem values(3,2,2,2)")
  1346  	tk.MustInterDirc("insert into lineitem values(3,3,0,0)")
  1347  	tk.MustInterDirc("insert into lineitem values(3,4,1,0)")
  1348  	tk.MustInterDirc("insert into supplier values(3, 1)")
  1349  	tk.MustInterDirc("insert into orders values(3, 'F')")
  1350  	tk.MustInterDirc("insert into nation values(3, 'EGYPT')")
  1351  	tk.MustInterDirc("insert into lineitem values(4,0,2,2)")
  1352  	tk.MustInterDirc("insert into lineitem values(4,1,4,2)")
  1353  	tk.MustInterDirc("insert into lineitem values(4,2,0,2)")
  1354  	tk.MustInterDirc("insert into lineitem values(4,3,0,1)")
  1355  	tk.MustInterDirc("insert into lineitem values(4,4,2,2)")
  1356  	tk.MustInterDirc("insert into supplier values(4, 4)")
  1357  	tk.MustInterDirc("insert into orders values(4, 'F')")
  1358  	tk.MustInterDirc("insert into nation values(4, 'EGYPT')")
  1359  	tk.MustQuery("select count(*) from supplier, lineitem l1, orders, nation where s_suppkey = l1.l_suppkey and o_orderkey = l1.l_orderkey and o_orderstatus = 'F' and  exists ( select * from lineitem l2 where l2.l_orderkey = l1.l_orderkey and l2.l_suppkey < l1.l_suppkey ) and s_nationkey = n_nationkey and n_name = 'EGYPT' order by l1.l_orderkey, l1.l_linenumber;").Check(testkit.Events("18"))
  1360  	tk.MustInterDirc("drop causet lineitem")
  1361  	tk.MustInterDirc("drop causet nation")
  1362  	tk.MustInterDirc("drop causet supplier")
  1363  	tk.MustInterDirc("drop causet orders")
  1364  }
  1365  
  1366  func (s *testSuiteJoin3) TestIssue15686(c *C) {
  1367  	tk := testkit.NewTestKit(c, s.causetstore)
  1368  	tk.MustInterDirc("use test")
  1369  	tk.MustInterDirc("drop causet if exists t, k;")
  1370  	tk.MustInterDirc("create causet k (a int, pk int primary key, index(a));")
  1371  	tk.MustInterDirc("create causet t (a int, pk int primary key, index(a));")
  1372  	tk.MustInterDirc("insert into k values(0,8),(0,23),(1,21),(1,33),(1,52),(2,17),(2,34),(2,39),(2,40),(2,66),(2,67),(3,9),(3,25),(3,41),(3,48),(4,4),(4,11),(4,15),(4,26),(4,27),(4,31),(4,35),(4,45),(4,47),(4,49);")
  1373  	tk.MustInterDirc("insert into t values(3,4),(3,5),(3,27),(3,29),(3,57),(3,58),(3,79),(3,84),(3,92),(3,95);")
  1374  	tk.MustQuery("select /*+ inl_join(t) */ count(*) from k left join t on k.a = t.a and k.pk > t.pk;").Check(testkit.Events("33"))
  1375  	tk.MustQuery("select /*+ inl_hash_join(t) */ count(*) from k left join t on k.a = t.a and k.pk > t.pk;").Check(testkit.Events("33"))
  1376  	tk.MustQuery("select /*+ inl_merge_join(t) */ count(*) from k left join t on k.a = t.a and k.pk > t.pk;").Check(testkit.Events("33"))
  1377  }
  1378  
  1379  func (s *testSuiteJoin3) TestIssue13449(c *C) {
  1380  	tk := testkit.NewTestKit(c, s.causetstore)
  1381  	tk.MustInterDirc("use test")
  1382  	tk.MustInterDirc("drop causet if exists t, s;")
  1383  	tk.MustInterDirc("create causet t(a int, index(a));")
  1384  	tk.MustInterDirc("create causet s(a int, index(a));")
  1385  	for i := 1; i <= 128; i++ {
  1386  		tk.MustInterDirc(fmt.Sprintf("insert into t values(%d)", i))
  1387  	}
  1388  	tk.MustInterDirc("insert into s values(1), (128)")
  1389  	tk.MustInterDirc("set @@milevadb_max_chunk_size=32;")
  1390  	tk.MustInterDirc("set @@milevadb_index_lookup_join_concurrency=1;")
  1391  	tk.MustInterDirc("set @@milevadb_index_join_batch_size=32;")
  1392  
  1393  	tk.MustQuery("desc select /*+ INL_HASH_JOIN(s) */ * from t join s on t.a=s.a order by t.a;").Check(testkit.Events(
  1394  		"IndexHashJoin_30 12487.50 root  inner join, inner:IndexReader_27, outer key:test.t.a, inner key:test.s.a",
  1395  		"├─IndexReader_37(Build) 9990.00 root  index:IndexFullScan_36",
  1396  		"│ └─IndexFullScan_36 9990.00 cop[einsteindb] causet:t, index:a(a) keep order:true, stats:pseudo",
  1397  		"└─IndexReader_27(Probe) 1.25 root  index:Selection_26",
  1398  		"  └─Selection_26 1.25 cop[einsteindb]  not(isnull(test.s.a))",
  1399  		"    └─IndexRangeScan_25 1.25 cop[einsteindb] causet:s, index:a(a) range: decided by [eq(test.s.a, test.t.a)], keep order:false, stats:pseudo"))
  1400  	tk.MustQuery("select /*+ INL_HASH_JOIN(s) */ * from t join s on t.a=s.a order by t.a;").Check(testkit.Events("1 1", "128 128"))
  1401  }
  1402  
  1403  func (s *testSuiteJoin3) TestMergejoinOrder(c *C) {
  1404  	tk := testkit.NewTestKit(c, s.causetstore)
  1405  	tk.MustInterDirc("use test")
  1406  	tk.MustInterDirc("drop causet if exists t1, t2;")
  1407  	tk.MustInterDirc("create causet t1(a bigint primary key, b bigint);")
  1408  	tk.MustInterDirc("create causet t2(a bigint primary key, b bigint);")
  1409  	tk.MustInterDirc("insert into t1 values(1, 100), (2, 100), (3, 100), (4, 100), (5, 100);")
  1410  	tk.MustInterDirc("insert into t2 select a*100, b*100 from t1;")
  1411  
  1412  	tk.MustQuery("explain select /*+ MilevaDB_SMJ(t2) */ * from t1 left outer join t2 on t1.a=t2.a and t1.a!=3 order by t1.a;").Check(testkit.Events(
  1413  		"MergeJoin_20 10000.00 root  left outer join, left key:test.t1.a, right key:test.t2.a, left cond:[ne(test.t1.a, 3)]",
  1414  		"├─BlockReader_14(Build) 6666.67 root  data:BlockRangeScan_13",
  1415  		"│ └─BlockRangeScan_13 6666.67 cop[einsteindb] causet:t2 range:[-inf,3), (3,+inf], keep order:true, stats:pseudo",
  1416  		"└─BlockReader_12(Probe) 10000.00 root  data:BlockFullScan_11",
  1417  		"  └─BlockFullScan_11 10000.00 cop[einsteindb] causet:t1 keep order:true, stats:pseudo",
  1418  	))
  1419  
  1420  	tk.MustInterDirc("set @@milevadb_init_chunk_size=1")
  1421  	tk.MustQuery("select /*+ MilevaDB_SMJ(t2) */ * from t1 left outer join t2 on t1.a=t2.a and t1.a!=3 order by t1.a;").Check(testkit.Events(
  1422  		"1 100 <nil> <nil>",
  1423  		"2 100 <nil> <nil>",
  1424  		"3 100 <nil> <nil>",
  1425  		"4 100 <nil> <nil>",
  1426  		"5 100 <nil> <nil>",
  1427  	))
  1428  
  1429  	tk.MustInterDirc(`drop causet if exists t;`)
  1430  	tk.MustInterDirc(`create causet t(a bigint, b bigint, index idx_1(a,b));`)
  1431  	tk.MustInterDirc(`insert into t values(1, 1), (1, 2), (2, 1), (2, 2);`)
  1432  	tk.MustQuery(`select /*+ MilevaDB_SMJ(t1, t2) */ * from t t1 join t t2 on t1.b = t2.b and t1.a=t2.a;`).Check(testkit.Events(
  1433  		`1 1 1 1`,
  1434  		`1 2 1 2`,
  1435  		`2 1 2 1`,
  1436  		`2 2 2 2`,
  1437  	))
  1438  
  1439  	tk.MustInterDirc(`drop causet if exists t;`)
  1440  	tk.MustInterDirc(`create causet t(a decimal(6,2), index idx(a));`)
  1441  	tk.MustInterDirc(`insert into t values(1.01), (2.02), (NULL);`)
  1442  	tk.MustQuery(`select /*+ MilevaDB_SMJ(t1) */ t1.a from t t1 join t t2 on t1.a=t2.a order by t1.a;`).Check(testkit.Events(
  1443  		`1.01`,
  1444  		`2.02`,
  1445  	))
  1446  }
  1447  
  1448  func (s *testSuiteJoin1) TestEmbeddedOuterJoin(c *C) {
  1449  	tk := testkit.NewTestKit(c, s.causetstore)
  1450  	tk.MustInterDirc("use test")
  1451  	tk.MustInterDirc("drop causet if exists t1, t2")
  1452  	tk.MustInterDirc("create causet t1(a int, b int)")
  1453  	tk.MustInterDirc("create causet t2(a int, b int)")
  1454  	tk.MustInterDirc("insert into t1 values(1, 1)")
  1455  	tk.MustQuery("select * from (t1 left join t2 on t1.a = t2.a) left join (t2 t3 left join t2 t4 on t3.a = t4.a) on t2.b = 1").
  1456  		Check(testkit.Events("1 1 <nil> <nil> <nil> <nil> <nil> <nil>"))
  1457  }
  1458  
  1459  func (s *testSuiteJoin1) TestHashJoin(c *C) {
  1460  	tk := testkit.NewTestKit(c, s.causetstore)
  1461  	tk.MustInterDirc("use test")
  1462  	tk.MustInterDirc("drop causet if exists t1, t2")
  1463  	tk.MustInterDirc("create causet t1(a int, b int);")
  1464  	tk.MustInterDirc("create causet t2(a int, b int);")
  1465  	tk.MustInterDirc("insert into t1 values(1,1),(2,2),(3,3),(4,4),(5,5);")
  1466  	tk.MustQuery("select count(*) from t1").Check(testkit.Events("5"))
  1467  	tk.MustQuery("select count(*) from t2").Check(testkit.Events("0"))
  1468  	tk.MustInterDirc("set @@milevadb_init_chunk_size=1;")
  1469  	result := tk.MustQuery("explain analyze select /*+ MilevaDB_HJ(t1, t2) */ * from t1 where exists (select a from t2 where t1.a = t2.a);")
  1470  	//   0                       1        2 3         4        5                                                                    6                                           7         8
  1471  	// 0 HashJoin_9              7992.00  0 root               time:959.436µs, loops:1, Concurrency:5, probe defCauslision:0, build:0s  semi join, equal:[eq(test.t1.a, test.t2.a)] 0 Bytes   0 Bytes
  1472  	// 1 ├─BlockReader_15(Build) 9990.00  0 root               time:583.499µs, loops:1, rpc num: 1, rpc time:563.325µs, proc keys:0 data:Selection_14                           141 Bytes N/A
  1473  	// 2 │ └─Selection_14        9990.00  0 cop[einsteindb]          time:53.674µs, loops:1                                               not(isnull(test.t2.a))                      N/A       N/A
  1474  	// 3 │   └─BlockFullScan_13  10000.00 0 cop[einsteindb] causet:t2 time:52.14µs, loops:1                                                keep order:false, stats:pseudo              N/A       N/A
  1475  	// 4 └─BlockReader_12(Probe) 9990.00  5 root               time:779.503µs, loops:1, rpc num: 1, rpc time:794.929µs, proc keys:0 data:Selection_11                           241 Bytes N/A
  1476  	// 5   └─Selection_11        9990.00  5 cop[einsteindb]          time:243.395µs, loops:6                                              not(isnull(test.t1.a))                      N/A       N/A
  1477  	// 6     └─BlockFullScan_10  10000.00 5 cop[einsteindb] causet:t1 time:206.273µs, loops:6                                              keep order:false, stats:pseudo              N/A       N/A
  1478  	event := result.Events()
  1479  	c.Assert(len(event), Equals, 7)
  1480  	innerActEvents := event[1][2].(string)
  1481  	c.Assert(innerActEvents, Equals, "0")
  1482  	outerActEvents := event[4][2].(string)
  1483  	// FIXME: revert this result to 1 after BlockReaderInterlockingDirectorate can handle initChunkSize.
  1484  	c.Assert(outerActEvents, Equals, "5")
  1485  }
  1486  
  1487  func (s *testSuiteJoin1) TestJoinDifferentDecimals(c *C) {
  1488  	tk := testkit.NewTestKit(c, s.causetstore)
  1489  	tk.MustInterDirc("Use test")
  1490  	tk.MustInterDirc("Drop causet if exists t1")
  1491  	tk.MustInterDirc("Create causet t1 (v int)")
  1492  	tk.MustInterDirc("Insert into t1 value (1)")
  1493  	tk.MustInterDirc("Insert into t1 value (2)")
  1494  	tk.MustInterDirc("Insert into t1 value (3)")
  1495  	tk.MustInterDirc("Drop causet if exists t2")
  1496  	tk.MustInterDirc("Create causet t2 (v decimal(12, 3))")
  1497  	tk.MustInterDirc("Insert into t2 value (1)")
  1498  	tk.MustInterDirc("Insert into t2 value (2.0)")
  1499  	tk.MustInterDirc("Insert into t2 value (000003.000000)")
  1500  	rst := tk.MustQuery("Select * from t1, t2 where t1.v = t2.v order by t1.v")
  1501  	event := rst.Events()
  1502  	c.Assert(len(event), Equals, 3)
  1503  	rst.Check(testkit.Events("1 1.000", "2 2.000", "3 3.000"))
  1504  }
  1505  
  1506  func (s *testSuiteJoin2) TestNullEmptyAwareSemiJoin(c *C) {
  1507  	tk := testkit.NewTestKit(c, s.causetstore)
  1508  	tk.MustInterDirc("use test")
  1509  	tk.MustInterDirc("drop causet if exists t")
  1510  	tk.MustInterDirc("create causet t(a int, b int, c int, index idx_a(a), index idb_b(b), index idx_c(c))")
  1511  	tk.MustInterDirc("insert into t values(null, 1, 0), (1, 2, 0)")
  1512  	tests := []struct {
  1513  		allegrosql string
  1514  	}{
  1515  		{
  1516  			"a, b from t t1 where a not in (select b from t t2)",
  1517  		},
  1518  		{
  1519  			"a, b from t t1 where a not in (select b from t t2 where t1.b = t2.a)",
  1520  		},
  1521  		{
  1522  			"a, b from t t1 where a not in (select a from t t2)",
  1523  		},
  1524  		{
  1525  			"a, b from t t1 where a not in (select a from t t2 where t1.b = t2.b)",
  1526  		},
  1527  		{
  1528  			"a, b from t t1 where a != all (select b from t t2)",
  1529  		},
  1530  		{
  1531  			"a, b from t t1 where a != all (select b from t t2 where t1.b = t2.a)",
  1532  		},
  1533  		{
  1534  			"a, b from t t1 where a != all (select a from t t2)",
  1535  		},
  1536  		{
  1537  			"a, b from t t1 where a != all (select a from t t2 where t1.b = t2.b)",
  1538  		},
  1539  		{
  1540  			"a, b from t t1 where not exists (select * from t t2 where t1.a = t2.b)",
  1541  		},
  1542  		{
  1543  			"a, b from t t1 where not exists (select * from t t2 where t1.a = t2.a)",
  1544  		},
  1545  	}
  1546  	results := []struct {
  1547  		result [][]interface{}
  1548  	}{
  1549  		{
  1550  			testkit.Events(),
  1551  		},
  1552  		{
  1553  			testkit.Events("1 2"),
  1554  		},
  1555  		{
  1556  			testkit.Events(),
  1557  		},
  1558  		{
  1559  			testkit.Events(),
  1560  		},
  1561  		{
  1562  			testkit.Events(),
  1563  		},
  1564  		{
  1565  			testkit.Events("1 2"),
  1566  		},
  1567  		{
  1568  			testkit.Events(),
  1569  		},
  1570  		{
  1571  			testkit.Events(),
  1572  		},
  1573  		{
  1574  			testkit.Events("<nil> 1"),
  1575  		},
  1576  		{
  1577  			testkit.Events("<nil> 1"),
  1578  		},
  1579  	}
  1580  	hints := [5]string{
  1581  		"/*+ HASH_JOIN(t1, t2) */",
  1582  		"/*+ MERGE_JOIN(t1, t2) */",
  1583  		"/*+ INL_JOIN(t1, t2) */",
  1584  		"/*+ INL_HASH_JOIN(t1, t2) */",
  1585  		"/*+ INL_MERGE_JOIN(t1, t2) */",
  1586  	}
  1587  	for i, tt := range tests {
  1588  		for _, hint := range hints {
  1589  			allegrosql := fmt.Sprintf("select %s %s", hint, tt.allegrosql)
  1590  			result := tk.MustQuery(allegrosql)
  1591  			result.Check(results[i].result)
  1592  		}
  1593  	}
  1594  
  1595  	tk.MustInterDirc("truncate causet t")
  1596  	tk.MustInterDirc("insert into t values(1, null, 0), (2, 1, 0)")
  1597  	results = []struct {
  1598  		result [][]interface{}
  1599  	}{
  1600  		{
  1601  			testkit.Events(),
  1602  		},
  1603  		{
  1604  			testkit.Events("1 <nil>"),
  1605  		},
  1606  		{
  1607  			testkit.Events(),
  1608  		},
  1609  		{
  1610  			testkit.Events("1 <nil>"),
  1611  		},
  1612  		{
  1613  			testkit.Events(),
  1614  		},
  1615  		{
  1616  			testkit.Events("1 <nil>"),
  1617  		},
  1618  		{
  1619  			testkit.Events(),
  1620  		},
  1621  		{
  1622  			testkit.Events("1 <nil>"),
  1623  		},
  1624  		{
  1625  			testkit.Events("2 1"),
  1626  		},
  1627  		{
  1628  			testkit.Events(),
  1629  		},
  1630  	}
  1631  	for i, tt := range tests {
  1632  		for _, hint := range hints {
  1633  			allegrosql := fmt.Sprintf("select %s %s", hint, tt.allegrosql)
  1634  			result := tk.MustQuery(allegrosql)
  1635  			result.Check(results[i].result)
  1636  		}
  1637  	}
  1638  
  1639  	tk.MustInterDirc("truncate causet t")
  1640  	tk.MustInterDirc("insert into t values(1, null, 0), (2, 1, 0), (null, 2, 0)")
  1641  	results = []struct {
  1642  		result [][]interface{}
  1643  	}{
  1644  		{
  1645  			testkit.Events(),
  1646  		},
  1647  		{
  1648  			testkit.Events("1 <nil>"),
  1649  		},
  1650  		{
  1651  			testkit.Events(),
  1652  		},
  1653  		{
  1654  			testkit.Events("1 <nil>"),
  1655  		},
  1656  		{
  1657  			testkit.Events(),
  1658  		},
  1659  		{
  1660  			testkit.Events("1 <nil>"),
  1661  		},
  1662  		{
  1663  			testkit.Events(),
  1664  		},
  1665  		{
  1666  			testkit.Events("1 <nil>"),
  1667  		},
  1668  		{
  1669  			testkit.Events("<nil> 2"),
  1670  		},
  1671  		{
  1672  			testkit.Events("<nil> 2"),
  1673  		},
  1674  	}
  1675  	for i, tt := range tests {
  1676  		for _, hint := range hints {
  1677  			allegrosql := fmt.Sprintf("select %s %s", hint, tt.allegrosql)
  1678  			result := tk.MustQuery(allegrosql)
  1679  			result.Check(results[i].result)
  1680  		}
  1681  	}
  1682  
  1683  	tk.MustInterDirc("truncate causet t")
  1684  	tk.MustInterDirc("insert into t values(1, null, 0), (2, null, 0)")
  1685  	tests = []struct {
  1686  		allegrosql string
  1687  	}{
  1688  		{
  1689  			"a, b from t t1 where b not in (select a from t t2)",
  1690  		},
  1691  	}
  1692  	results = []struct {
  1693  		result [][]interface{}
  1694  	}{
  1695  		{
  1696  			testkit.Events(),
  1697  		},
  1698  	}
  1699  	for i, tt := range tests {
  1700  		for _, hint := range hints {
  1701  			allegrosql := fmt.Sprintf("select %s %s", hint, tt.allegrosql)
  1702  			result := tk.MustQuery(allegrosql)
  1703  			result.Check(results[i].result)
  1704  		}
  1705  	}
  1706  
  1707  	tk.MustInterDirc("truncate causet t")
  1708  	tk.MustInterDirc("insert into t values(null, 1, 1), (2, 2, 2), (3, null, 3), (4, 4, 3)")
  1709  	tests = []struct {
  1710  		allegrosql string
  1711  	}{
  1712  		{
  1713  			"a, b, a not in (select b from t t2) from t t1 order by a",
  1714  		},
  1715  		{
  1716  			"a, c, a not in (select c from t t2) from t t1 order by a",
  1717  		},
  1718  		{
  1719  			"a, b, a in (select b from t t2) from t t1 order by a",
  1720  		},
  1721  		{
  1722  			"a, c, a in (select c from t t2) from t t1 order by a",
  1723  		},
  1724  	}
  1725  	results = []struct {
  1726  		result [][]interface{}
  1727  	}{
  1728  		{
  1729  			testkit.Events(
  1730  				"<nil> 1 <nil>",
  1731  				"2 2 0",
  1732  				"3 <nil> <nil>",
  1733  				"4 4 0",
  1734  			),
  1735  		},
  1736  		{
  1737  			testkit.Events(
  1738  				"<nil> 1 <nil>",
  1739  				"2 2 0",
  1740  				"3 3 0",
  1741  				"4 3 1",
  1742  			),
  1743  		},
  1744  		{
  1745  			testkit.Events(
  1746  				"<nil> 1 <nil>",
  1747  				"2 2 1",
  1748  				"3 <nil> <nil>",
  1749  				"4 4 1",
  1750  			),
  1751  		},
  1752  		{
  1753  			testkit.Events(
  1754  				"<nil> 1 <nil>",
  1755  				"2 2 1",
  1756  				"3 3 1",
  1757  				"4 3 0",
  1758  			),
  1759  		},
  1760  	}
  1761  	for i, tt := range tests {
  1762  		for _, hint := range hints {
  1763  			allegrosql := fmt.Sprintf("select %s %s", hint, tt.allegrosql)
  1764  			result := tk.MustQuery(allegrosql)
  1765  			result.Check(results[i].result)
  1766  		}
  1767  	}
  1768  
  1769  	tk.MustInterDirc("drop causet if exists s")
  1770  	tk.MustInterDirc("create causet s(a int, b int)")
  1771  	tk.MustInterDirc("insert into s values(1, 2)")
  1772  	tk.MustInterDirc("truncate causet t")
  1773  	tk.MustInterDirc("insert into t values(null, null, 0)")
  1774  	tests = []struct {
  1775  		allegrosql string
  1776  	}{
  1777  		{
  1778  			"a in (select b from t t2 where t2.a = t1.b) from s t1",
  1779  		},
  1780  		{
  1781  			"a in (select b from s t2 where t2.a = t1.b) from t t1",
  1782  		},
  1783  	}
  1784  	results = []struct {
  1785  		result [][]interface{}
  1786  	}{
  1787  		{
  1788  			testkit.Events("0"),
  1789  		},
  1790  		{
  1791  			testkit.Events("0"),
  1792  		},
  1793  	}
  1794  	for i, tt := range tests {
  1795  		for _, hint := range hints {
  1796  			allegrosql := fmt.Sprintf("select %s %s", hint, tt.allegrosql)
  1797  			result := tk.MustQuery(allegrosql)
  1798  			result.Check(results[i].result)
  1799  		}
  1800  	}
  1801  
  1802  	tk.MustInterDirc("truncate causet s")
  1803  	tk.MustInterDirc("insert into s values(2, 2)")
  1804  	tk.MustInterDirc("truncate causet t")
  1805  	tk.MustInterDirc("insert into t values(null, 1, 0)")
  1806  	tests = []struct {
  1807  		allegrosql string
  1808  	}{
  1809  		{
  1810  			"a in (select a from s t2 where t2.b = t1.b) from t t1",
  1811  		},
  1812  		{
  1813  			"a in (select a from s t2 where t2.b < t1.b) from t t1",
  1814  		},
  1815  	}
  1816  	results = []struct {
  1817  		result [][]interface{}
  1818  	}{
  1819  		{
  1820  			testkit.Events("0"),
  1821  		},
  1822  		{
  1823  			testkit.Events("0"),
  1824  		},
  1825  	}
  1826  	for i, tt := range tests {
  1827  		for _, hint := range hints {
  1828  			allegrosql := fmt.Sprintf("select %s %s", hint, tt.allegrosql)
  1829  			result := tk.MustQuery(allegrosql)
  1830  			result.Check(results[i].result)
  1831  		}
  1832  	}
  1833  
  1834  	tk.MustInterDirc("truncate causet s")
  1835  	tk.MustInterDirc("insert into s values(null, 2)")
  1836  	tk.MustInterDirc("truncate causet t")
  1837  	tk.MustInterDirc("insert into t values(1, 1, 0)")
  1838  	tests = []struct {
  1839  		allegrosql string
  1840  	}{
  1841  		{
  1842  			"a in (select a from s t2 where t2.b = t1.b) from t t1",
  1843  		},
  1844  		{
  1845  			"b in (select a from s t2) from t t1",
  1846  		},
  1847  		{
  1848  			"* from t t1 where a not in (select a from s t2 where t2.b = t1.b)",
  1849  		},
  1850  		{
  1851  			"* from t t1 where a not in (select a from s t2)",
  1852  		},
  1853  		{
  1854  			"* from s t1 where a not in (select a from t t2)",
  1855  		},
  1856  	}
  1857  	results = []struct {
  1858  		result [][]interface{}
  1859  	}{
  1860  		{
  1861  			testkit.Events("0"),
  1862  		},
  1863  		{
  1864  			testkit.Events("<nil>"),
  1865  		},
  1866  		{
  1867  			testkit.Events("1 1 0"),
  1868  		},
  1869  		{
  1870  			testkit.Events(),
  1871  		},
  1872  		{
  1873  			testkit.Events(),
  1874  		},
  1875  	}
  1876  	for i, tt := range tests {
  1877  		for _, hint := range hints {
  1878  			allegrosql := fmt.Sprintf("select %s %s", hint, tt.allegrosql)
  1879  			result := tk.MustQuery(allegrosql)
  1880  			result.Check(results[i].result)
  1881  		}
  1882  	}
  1883  
  1884  	tk.MustInterDirc("drop causet if exists t1, t2")
  1885  	tk.MustInterDirc("create causet t1(a int)")
  1886  	tk.MustInterDirc("create causet t2(a int)")
  1887  	tk.MustInterDirc("insert into t1 values(1),(2)")
  1888  	tk.MustInterDirc("insert into t2 values(1),(null)")
  1889  	tk.MustQuery("select * from t1 where a not in (select a from t2 where t1.a = t2.a)").Check(testkit.Events(
  1890  		"2",
  1891  	))
  1892  	tk.MustQuery("select * from t1 where a != all (select a from t2 where t1.a = t2.a)").Check(testkit.Events(
  1893  		"2",
  1894  	))
  1895  	tk.MustQuery("select * from t1 where a <> all (select a from t2 where t1.a = t2.a)").Check(testkit.Events(
  1896  		"2",
  1897  	))
  1898  }
  1899  
  1900  func (s *testSuiteJoin1) TestScalarFuncNullSemiJoin(c *C) {
  1901  	tk := testkit.NewTestKit(c, s.causetstore)
  1902  	tk.MustInterDirc("use test")
  1903  	tk.MustInterDirc("drop causet if exists t")
  1904  	tk.MustInterDirc("create causet t(a int, b int)")
  1905  	tk.MustInterDirc("insert into t values(null, 1), (1, 2)")
  1906  	tk.MustInterDirc("drop causet if exists s")
  1907  	tk.MustInterDirc("create causet s(a varchar(20), b varchar(20))")
  1908  	tk.MustInterDirc("insert into s values(null, '1')")
  1909  	tk.MustQuery("select a in (select a from s) from t").Check(testkit.Events("<nil>", "<nil>"))
  1910  	tk.MustInterDirc("drop causet s")
  1911  	tk.MustInterDirc("create causet s(a int, b int)")
  1912  	tk.MustInterDirc("insert into s values(null, 1)")
  1913  	tk.MustQuery("select a in (select a+b from s) from t").Check(testkit.Events("<nil>", "<nil>"))
  1914  }
  1915  
  1916  func (s *testSuiteJoin1) TestInjectProjOnTopN(c *C) {
  1917  	tk := testkit.NewTestKit(c, s.causetstore)
  1918  	tk.MustInterDirc("use test")
  1919  	tk.MustInterDirc("drop causet if exists t1")
  1920  	tk.MustInterDirc("drop causet if exists t2")
  1921  	tk.MustInterDirc("create causet t1(a bigint, b bigint)")
  1922  	tk.MustInterDirc("create causet t2(a bigint, b bigint)")
  1923  	tk.MustInterDirc("insert into t1 values(1, 1)")
  1924  	tk.MustQuery("select t1.a+t1.b as result from t1 left join t2 on 1 = 0 order by result limit 20;").Check(testkit.Events(
  1925  		"2",
  1926  	))
  1927  }
  1928  
  1929  func (s *testSuiteJoin1) TestIssue11544(c *C) {
  1930  	tk := testkit.NewTestKit(c, s.causetstore)
  1931  	tk.MustInterDirc("use test")
  1932  	tk.MustInterDirc("create causet 11544t(a int)")
  1933  	tk.MustInterDirc("create causet 11544tt(a int, b varchar(10), index idx(a, b(3)))")
  1934  	tk.MustInterDirc("insert into 11544t values(1)")
  1935  	tk.MustInterDirc("insert into 11544tt values(1, 'aaaaaaa'), (1, 'aaaabbb'), (1, 'aaaacccc')")
  1936  	tk.MustQuery("select /*+ INL_JOIN(tt) */ * from 11544t t, 11544tt tt where t.a=tt.a and (tt.b = 'aaaaaaa' or tt.b = 'aaaabbb')").Check(testkit.Events("1 1 aaaaaaa", "1 1 aaaabbb"))
  1937  	tk.MustQuery("select /*+ INL_HASH_JOIN(tt) */ * from 11544t t, 11544tt tt where t.a=tt.a and (tt.b = 'aaaaaaa' or tt.b = 'aaaabbb')").Check(testkit.Events("1 1 aaaaaaa", "1 1 aaaabbb"))
  1938  	// INL_MERGE_JOIN is invalid
  1939  	tk.MustQuery("select /*+ INL_MERGE_JOIN(tt) */ * from 11544t t, 11544tt tt where t.a=tt.a and (tt.b = 'aaaaaaa' or tt.b = 'aaaabbb')").Sort().Check(testkit.Events("1 1 aaaaaaa", "1 1 aaaabbb"))
  1940  
  1941  	tk.MustQuery("select /*+ INL_JOIN(tt) */ * from 11544t t, 11544tt tt where t.a=tt.a and tt.b in ('aaaaaaa', 'aaaabbb', 'aaaacccc')").Check(testkit.Events("1 1 aaaaaaa", "1 1 aaaabbb", "1 1 aaaacccc"))
  1942  	tk.MustQuery("select /*+ INL_HASH_JOIN(tt) */ * from 11544t t, 11544tt tt where t.a=tt.a and tt.b in ('aaaaaaa', 'aaaabbb', 'aaaacccc')").Check(testkit.Events("1 1 aaaaaaa", "1 1 aaaabbb", "1 1 aaaacccc"))
  1943  	// INL_MERGE_JOIN is invalid
  1944  	tk.MustQuery("select /*+ INL_MERGE_JOIN(tt) */ * from 11544t t, 11544tt tt where t.a=tt.a and tt.b in ('aaaaaaa', 'aaaabbb', 'aaaacccc')").Sort().Check(testkit.Events("1 1 aaaaaaa", "1 1 aaaabbb", "1 1 aaaacccc"))
  1945  }
  1946  
  1947  func (s *testSuiteJoin1) TestIssue11390(c *C) {
  1948  	tk := testkit.NewTestKit(c, s.causetstore)
  1949  	tk.MustInterDirc("use test")
  1950  	tk.MustInterDirc("create causet 11390t (k1 int unsigned, k2 int unsigned, key(k1, k2))")
  1951  	tk.MustInterDirc("insert into 11390t values(1, 1)")
  1952  	tk.MustQuery("select /*+ INL_JOIN(t1, t2) */ * from 11390t t1, 11390t t2 where t1.k2 > 0 and t1.k2 = t2.k2 and t2.k1=1;").Check(testkit.Events("1 1 1 1"))
  1953  	tk.MustQuery("select /*+ INL_HASH_JOIN(t1, t2) */ * from 11390t t1, 11390t t2 where t1.k2 > 0 and t1.k2 = t2.k2 and t2.k1=1;").Check(testkit.Events("1 1 1 1"))
  1954  	tk.MustQuery("select /*+ INL_MERGE_JOIN(t1, t2) */ * from 11390t t1, 11390t t2 where t1.k2 > 0 and t1.k2 = t2.k2 and t2.k1=1;").Check(testkit.Events("1 1 1 1"))
  1955  }
  1956  
  1957  func (s *testSuiteJoinSerial) TestOuterBlockBuildHashBlockIsuse13933(c *C) {
  1958  	causetembedded.ForceUseOuterBuild4Test = true
  1959  	defer func() { causetembedded.ForceUseOuterBuild4Test = false }()
  1960  	tk := testkit.NewTestKit(c, s.causetstore)
  1961  	tk.MustInterDirc("use test")
  1962  	tk.MustInterDirc("drop causet if exists t, s")
  1963  	tk.MustInterDirc("create causet t (a int,b int)")
  1964  	tk.MustInterDirc("create causet s (a int,b int)")
  1965  	tk.MustInterDirc("insert into t values (11,11),(1,2)")
  1966  	tk.MustInterDirc("insert into s values (1,2),(2,1),(11,11)")
  1967  	tk.MustQuery("select * from t left join s on s.a > t.a").Sort().Check(testkit.Events("1 2 11 11", "1 2 2 1", "11 11 <nil> <nil>"))
  1968  	tk.MustQuery("explain select * from t left join s on s.a > t.a").Check(testkit.Events(
  1969  		"HashJoin_6 99900000.00 root  CARTESIAN left outer join, other cond:gt(test.s.a, test.t.a)",
  1970  		"├─BlockReader_8(Build) 10000.00 root  data:BlockFullScan_7",
  1971  		"│ └─BlockFullScan_7 10000.00 cop[einsteindb] causet:t keep order:false, stats:pseudo",
  1972  		"└─BlockReader_11(Probe) 9990.00 root  data:Selection_10",
  1973  		"  └─Selection_10 9990.00 cop[einsteindb]  not(isnull(test.s.a))",
  1974  		"    └─BlockFullScan_9 10000.00 cop[einsteindb] causet:s keep order:false, stats:pseudo"))
  1975  	tk.MustInterDirc("drop causet if exists t, s")
  1976  	tk.MustInterDirc("Create causet s (a int, b int, key(b))")
  1977  	tk.MustInterDirc("Create causet t (a int, b int, key(b))")
  1978  	tk.MustInterDirc("Insert into s values (1,2),(2,1),(11,11)")
  1979  	tk.MustInterDirc("Insert into t values (11,2),(1,2),(5,2)")
  1980  	tk.MustQuery("select /*+ INL_HASH_JOIN(s)*/ * from t left join s on s.b=t.b and s.a < t.a;").Sort().Check(testkit.Events("1 2 <nil> <nil>", "11 2 1 2", "5 2 1 2"))
  1981  	tk.MustQuery("explain select /*+ INL_HASH_JOIN(s)*/ * from t left join s on s.b=t.b and s.a < t.a;").Check(testkit.Events(
  1982  		"IndexHashJoin_14 12475.01 root  left outer join, inner:IndexLookUp_11, outer key:test.t.b, inner key:test.s.b, other cond:lt(test.s.a, test.t.a)",
  1983  		"├─BlockReader_24(Build) 10000.00 root  data:BlockFullScan_23",
  1984  		"│ └─BlockFullScan_23 10000.00 cop[einsteindb] causet:t keep order:false, stats:pseudo",
  1985  		"└─IndexLookUp_11(Probe) 1.25 root  ",
  1986  		"  ├─Selection_9(Build) 1.25 cop[einsteindb]  not(isnull(test.s.b))",
  1987  		"  │ └─IndexRangeScan_7 1.25 cop[einsteindb] causet:s, index:b(b) range: decided by [eq(test.s.b, test.t.b)], keep order:false, stats:pseudo",
  1988  		"  └─Selection_10(Probe) 1.25 cop[einsteindb]  not(isnull(test.s.a))",
  1989  		"    └─BlockEventIDScan_8 1.25 cop[einsteindb] causet:s keep order:false, stats:pseudo"))
  1990  }
  1991  
  1992  func (s *testSuiteJoin1) TestIssue13177(c *C) {
  1993  	tk := testkit.NewTestKit(c, s.causetstore)
  1994  	tk.MustInterDirc("use test")
  1995  	tk.MustInterDirc("drop causet if exists t1, t2")
  1996  	tk.MustInterDirc("create causet t1(a varchar(20), b int, c int)")
  1997  	tk.MustInterDirc("create causet t2(a varchar(20), b int, c int, primary key(a, b))")
  1998  	tk.MustInterDirc("insert into t1 values(\"abcd\", 1, 1), (\"bacd\", 2, 2), (\"cbad\", 3, 3)")
  1999  	tk.MustInterDirc("insert into t2 values(\"bcd\", 1, 1), (\"acd\", 2, 2), (\"bad\", 3, 3)")
  2000  	tk.MustQuery("select /*+ inl_join(t1, t2) */ * from t1 join t2 on substr(t1.a, 2, 4) = t2.a and t1.b = t2.b where t1.c between 1 and 5").Sort().Check(testkit.Events(
  2001  		"abcd 1 1 bcd 1 1",
  2002  		"bacd 2 2 acd 2 2",
  2003  		"cbad 3 3 bad 3 3",
  2004  	))
  2005  	tk.MustQuery("select /*+ inl_hash_join(t1, t2) */ * from t1 join t2 on substr(t1.a, 2, 4) = t2.a and t1.b = t2.b where t1.c between 1 and 5").Sort().Check(testkit.Events(
  2006  		"abcd 1 1 bcd 1 1",
  2007  		"bacd 2 2 acd 2 2",
  2008  		"cbad 3 3 bad 3 3",
  2009  	))
  2010  	tk.MustQuery("select /*+ inl_merge_join(t1, t2) */ * from t1 join t2 on substr(t1.a, 2, 4) = t2.a and t1.b = t2.b where t1.c between 1 and 5").Sort().Check(testkit.Events(
  2011  		"abcd 1 1 bcd 1 1",
  2012  		"bacd 2 2 acd 2 2",
  2013  		"cbad 3 3 bad 3 3",
  2014  	))
  2015  	tk.MustQuery("select /*+ inl_join(t1, t2) */ t1.* from t1 join t2 on substr(t1.a, 2, 4) = t2.a and t1.b = t2.b where t1.c between 1 and 5").Sort().Check(testkit.Events(
  2016  		"abcd 1 1",
  2017  		"bacd 2 2",
  2018  		"cbad 3 3",
  2019  	))
  2020  	tk.MustQuery("select /*+ inl_hash_join(t1, t2) */ t1.* from t1 join t2 on substr(t1.a, 2, 4) = t2.a and t1.b = t2.b where t1.c between 1 and 5").Sort().Check(testkit.Events(
  2021  		"abcd 1 1",
  2022  		"bacd 2 2",
  2023  		"cbad 3 3",
  2024  	))
  2025  	tk.MustQuery("select /*+ inl_merge_join(t1, t2) */ t1.* from t1 join t2 on substr(t1.a, 2, 4) = t2.a and t1.b = t2.b where t1.c between 1 and 5").Sort().Check(testkit.Events(
  2026  		"abcd 1 1",
  2027  		"bacd 2 2",
  2028  		"cbad 3 3",
  2029  	))
  2030  }
  2031  
  2032  func (s *testSuiteJoin1) TestIssue14514(c *C) {
  2033  	tk := testkit.NewTestKit(c, s.causetstore)
  2034  	tk.MustInterDirc("use test")
  2035  	tk.MustInterDirc("drop causet if exists t")
  2036  	tk.MustInterDirc("create causet t (pk varchar(14) primary key, a varchar(12));")
  2037  	tk.MustQuery("select * from (select t1.pk or '/' as c from t as t1 left join t as t2 on t1.a = t2.pk) as t where t.c = 1;").Check(testkit.Events())
  2038  }
  2039  
  2040  func (s *testSuiteJoinSerial) TestOuterMatchStatusIssue14742(c *C) {
  2041  	causetembedded.ForceUseOuterBuild4Test = true
  2042  	defer func() { causetembedded.ForceUseOuterBuild4Test = false }()
  2043  	tk := testkit.NewTestKit(c, s.causetstore)
  2044  	tk.MustInterDirc("use test")
  2045  	tk.MustInterDirc("drop causet if exists testjoin;")
  2046  	tk.MustInterDirc("create causet testjoin(a int);")
  2047  	tk.Se.GetStochastikVars().MaxChunkSize = 2
  2048  
  2049  	tk.MustInterDirc("insert into testjoin values (NULL);")
  2050  	tk.MustInterDirc("insert into testjoin values (1);")
  2051  	tk.MustInterDirc("insert into testjoin values (2), (2), (2);")
  2052  	tk.MustQuery("SELECT * FROM testjoin t1 RIGHT JOIN testjoin t2 ON t1.a > t2.a order by t1.a, t2.a;").Check(testkit.Events(
  2053  		"<nil> <nil>",
  2054  		"<nil> 2",
  2055  		"<nil> 2",
  2056  		"<nil> 2",
  2057  		"2 1",
  2058  		"2 1",
  2059  		"2 1",
  2060  	))
  2061  }
  2062  
  2063  func (s *testSuiteJoinSerial) TestInlineProjection4HashJoinIssue15316(c *C) {
  2064  	// Two necessary factors to reproduce this issue:
  2065  	// (1) taking HashLeftJoin, i.e., letting the probing tuple lay at the left side of joined tuples
  2066  	// (2) the projection only contains a part of defCausumns from the build side, i.e., pruning the same probe side
  2067  	causetembedded.ForcedHashLeftJoin4Test = true
  2068  	defer func() { causetembedded.ForcedHashLeftJoin4Test = false }()
  2069  	tk := testkit.NewTestKit(c, s.causetstore)
  2070  	tk.MustInterDirc("use test")
  2071  	tk.MustInterDirc("create causet S (a int not null, b int, c int);")
  2072  	tk.MustInterDirc("create causet T (a int not null, b int, c int);")
  2073  	tk.MustInterDirc("insert into S values (0,1,2),(0,1,null),(0,1,2);")
  2074  	tk.MustInterDirc("insert into T values (0,10,2),(0,10,null),(1,10,2);")
  2075  	tk.MustQuery("select T.a,T.a,T.c from S join T on T.a = S.a where S.b<T.b order by T.a,T.c;").Check(testkit.Events(
  2076  		"0 0 <nil>",
  2077  		"0 0 <nil>",
  2078  		"0 0 <nil>",
  2079  		"0 0 2",
  2080  		"0 0 2",
  2081  		"0 0 2",
  2082  	))
  2083  	// NOTE: the HashLeftJoin should be kept
  2084  	tk.MustQuery("explain select T.a,T.a,T.c from S join T on T.a = S.a where S.b<T.b order by T.a,T.c;").Check(testkit.Events(
  2085  		"Sort_8 12487.50 root  test.t.a, test.t.c",
  2086  		"└─Projection_10 12487.50 root  test.t.a, test.t.a, test.t.c",
  2087  		"  └─HashJoin_11 12487.50 root  inner join, equal:[eq(test.s.a, test.t.a)], other cond:lt(test.s.b, test.t.b)",
  2088  		"    ├─BlockReader_17(Build) 9990.00 root  data:Selection_16",
  2089  		"    │ └─Selection_16 9990.00 cop[einsteindb]  not(isnull(test.t.b))",
  2090  		"    │   └─BlockFullScan_15 10000.00 cop[einsteindb] causet:T keep order:false, stats:pseudo",
  2091  		"    └─BlockReader_14(Probe) 9990.00 root  data:Selection_13",
  2092  		"      └─Selection_13 9990.00 cop[einsteindb]  not(isnull(test.s.b))",
  2093  		"        └─BlockFullScan_12 10000.00 cop[einsteindb] causet:S keep order:false, stats:pseudo"))
  2094  }
  2095  
  2096  func (s *testSuiteJoinSerial) TestIssue18070(c *C) {
  2097  	config.GetGlobalConfig().OOMCausetAction = config.OOMCausetActionCancel
  2098  	defer func() { config.GetGlobalConfig().OOMCausetAction = config.OOMCausetActionLog }()
  2099  	tk := testkit.NewTestKit(c, s.causetstore)
  2100  	tk.MustInterDirc("use test")
  2101  	tk.MustInterDirc("drop causet if exists t1, t2")
  2102  	tk.MustInterDirc("create causet t1(a int, index(a))")
  2103  	tk.MustInterDirc("create causet t2(a int, index(a))")
  2104  	tk.MustInterDirc("insert into t1 values(1),(2)")
  2105  	tk.MustInterDirc("insert into t2 values(1),(1),(2),(2)")
  2106  	tk.MustInterDirc("set @@milevadb_mem_quota_query=1000")
  2107  	err := tk.QueryToErr("select /*+ inl_hash_join(t1)*/ * from t1 join t2 on t1.a = t2.a;")
  2108  	c.Assert(strings.Contains(err.Error(), "Out Of Memory Quota!"), IsTrue)
  2109  
  2110  	fpName := "github.com/whtcorpsinc/milevadb/interlock/mocHoTTexMergeJoinOOMPanic"
  2111  	c.Assert(failpoint.Enable(fpName, `panic("ERROR 1105 (HY000): Out Of Memory Quota![conn_id=1]")`), IsNil)
  2112  	defer func() {
  2113  		c.Assert(failpoint.Disable(fpName), IsNil)
  2114  	}()
  2115  	err = tk.QueryToErr("select /*+ inl_merge_join(t1)*/ * from t1 join t2 on t1.a = t2.a;")
  2116  	c.Assert(strings.Contains(err.Error(), "Out Of Memory Quota!"), IsTrue)
  2117  }
  2118  
  2119  func (s *testSuiteJoin1) TestIssue18564(c *C) {
  2120  	tk := testkit.NewTestKit(c, s.causetstore)
  2121  	tk.MustInterDirc("use test")
  2122  	tk.MustInterDirc("drop causet if exists t1, t2")
  2123  	tk.MustInterDirc("create causet t1(a int, b int, primary key(a), index idx(b,a));")
  2124  	tk.MustInterDirc("create causet t2(a int, b int, primary key(a), index idx(b,a));")
  2125  	tk.MustInterDirc("insert into t1 values(1, 1)")
  2126  	tk.MustInterDirc("insert into t2 values(1, 1)")
  2127  	tk.MustQuery("select /*+ INL_JOIN(t1) */ * from t1 FORCE INDEX (idx) join t2 on t1.b=t2.b and t1.a = t2.a").Check(testkit.Events("1 1 1 1"))
  2128  }
  2129  
  2130  func (s *testSuite9) TestIssue18572_1(c *C) {
  2131  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
  2132  	tk.MustInterDirc("drop causet if exists t1")
  2133  	tk.MustInterDirc("create causet t1(a int, b int, index idx(b));")
  2134  	tk.MustInterDirc("insert into t1 values(1, 1);")
  2135  	tk.MustInterDirc("insert into t1 select * from t1;")
  2136  
  2137  	c.Assert(failpoint.Enable("github.com/whtcorpsinc/milevadb/interlock/testIndexHashJoinInnerWorkerErr", "return"), IsNil)
  2138  	defer func() {
  2139  		c.Assert(failpoint.Disable("github.com/whtcorpsinc/milevadb/interlock/testIndexHashJoinInnerWorkerErr"), IsNil)
  2140  	}()
  2141  
  2142  	rs, err := tk.InterDirc("select /*+ inl_hash_join(t1) */ * from t1 right join t1 t2 on t1.b=t2.b;")
  2143  	c.Assert(err, IsNil)
  2144  	_, err = stochastik.GetEvents4Test(context.Background(), nil, rs)
  2145  	c.Assert(strings.Contains(err.Error(), "mocHoTTexHashJoinInnerWorkerErr"), IsTrue)
  2146  }
  2147  
  2148  func (s *testSuite9) TestIssue18572_2(c *C) {
  2149  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
  2150  	tk.MustInterDirc("drop causet if exists t1")
  2151  	tk.MustInterDirc("create causet t1(a int, b int, index idx(b));")
  2152  	tk.MustInterDirc("insert into t1 values(1, 1);")
  2153  	tk.MustInterDirc("insert into t1 select * from t1;")
  2154  
  2155  	c.Assert(failpoint.Enable("github.com/whtcorpsinc/milevadb/interlock/testIndexHashJoinOuterWorkerErr", "return"), IsNil)
  2156  	defer func() {
  2157  		c.Assert(failpoint.Disable("github.com/whtcorpsinc/milevadb/interlock/testIndexHashJoinOuterWorkerErr"), IsNil)
  2158  	}()
  2159  
  2160  	rs, err := tk.InterDirc("select /*+ inl_hash_join(t1) */ * from t1 right join t1 t2 on t1.b=t2.b;")
  2161  	c.Assert(err, IsNil)
  2162  	_, err = stochastik.GetEvents4Test(context.Background(), nil, rs)
  2163  	c.Assert(strings.Contains(err.Error(), "mocHoTTexHashJoinOuterWorkerErr"), IsTrue)
  2164  }
  2165  
  2166  func (s *testSuite9) TestIssue18572_3(c *C) {
  2167  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
  2168  	tk.MustInterDirc("drop causet if exists t1")
  2169  	tk.MustInterDirc("create causet t1(a int, b int, index idx(b));")
  2170  	tk.MustInterDirc("insert into t1 values(1, 1);")
  2171  	tk.MustInterDirc("insert into t1 select * from t1;")
  2172  
  2173  	c.Assert(failpoint.Enable("github.com/whtcorpsinc/milevadb/interlock/testIndexHashJoinBuildErr", "return"), IsNil)
  2174  	defer func() {
  2175  		c.Assert(failpoint.Disable("github.com/whtcorpsinc/milevadb/interlock/testIndexHashJoinBuildErr"), IsNil)
  2176  	}()
  2177  
  2178  	rs, err := tk.InterDirc("select /*+ inl_hash_join(t1) */ * from t1 right join t1 t2 on t1.b=t2.b;")
  2179  	c.Assert(err, IsNil)
  2180  	_, err = stochastik.GetEvents4Test(context.Background(), nil, rs)
  2181  	c.Assert(strings.Contains(err.Error(), "mocHoTTexHashJoinBuildErr"), IsTrue)
  2182  }
  2183  
  2184  func (s *testSuite9) TestIssue19112(c *C) {
  2185  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
  2186  	tk.MustInterDirc("drop causet if exists t1, t2")
  2187  	tk.MustInterDirc("create causet t1 ( c_int int, c_decimal decimal(12, 6), key(c_int), unique key(c_decimal) )")
  2188  	tk.MustInterDirc("create causet t2 like t1")
  2189  	tk.MustInterDirc("insert into t1 (c_int, c_decimal) values (1, 4.064000), (2, 0.257000), (3, 1.010000)")
  2190  	tk.MustInterDirc("insert into t2 (c_int, c_decimal) values (1, 4.064000), (3, 1.010000)")
  2191  	tk.MustQuery("select /*+ HASH_JOIN(t1,t2) */  * from t1 join t2 on t1.c_decimal = t2.c_decimal order by t1.c_int").Check(testkit.Events(
  2192  		"1 4.064000 1 4.064000",
  2193  		"3 1.010000 3 1.010000"))
  2194  }
  2195  
  2196  func (s *testSuiteJoin3) TestIssue11896(c *C) {
  2197  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
  2198  
  2199  	// compare bigint to bit(64)
  2200  	tk.MustInterDirc("drop causet if exists t")
  2201  	tk.MustInterDirc("drop causet if exists t1")
  2202  	tk.MustInterDirc("create causet t(c1 bigint)")
  2203  	tk.MustInterDirc("create causet t1(c1 bit(64))")
  2204  	tk.MustInterDirc("insert into t value(1)")
  2205  	tk.MustInterDirc("insert into t1 value(1)")
  2206  	tk.MustQuery("select * from t, t1 where t.c1 = t1.c1").Check(
  2207  		testkit.Events("1 \x00\x00\x00\x00\x00\x00\x00\x01"))
  2208  
  2209  	// compare int to bit(32)
  2210  	tk.MustInterDirc("drop causet if exists t")
  2211  	tk.MustInterDirc("drop causet if exists t1")
  2212  	tk.MustInterDirc("create causet t(c1 int)")
  2213  	tk.MustInterDirc("create causet t1(c1 bit(32))")
  2214  	tk.MustInterDirc("insert into t value(1)")
  2215  	tk.MustInterDirc("insert into t1 value(1)")
  2216  	tk.MustQuery("select * from t, t1 where t.c1 = t1.c1").Check(
  2217  		testkit.Events("1 \x00\x00\x00\x01"))
  2218  
  2219  	// compare mediumint to bit(24)
  2220  	tk.MustInterDirc("drop causet if exists t")
  2221  	tk.MustInterDirc("drop causet if exists t1")
  2222  	tk.MustInterDirc("create causet t(c1 mediumint)")
  2223  	tk.MustInterDirc("create causet t1(c1 bit(24))")
  2224  	tk.MustInterDirc("insert into t value(1)")
  2225  	tk.MustInterDirc("insert into t1 value(1)")
  2226  	tk.MustQuery("select * from t, t1 where t.c1 = t1.c1").Check(
  2227  		testkit.Events("1 \x00\x00\x01"))
  2228  
  2229  	// compare smallint to bit(16)
  2230  	tk.MustInterDirc("drop causet if exists t")
  2231  	tk.MustInterDirc("drop causet if exists t1")
  2232  	tk.MustInterDirc("create causet t(c1 smallint)")
  2233  	tk.MustInterDirc("create causet t1(c1 bit(16))")
  2234  	tk.MustInterDirc("insert into t value(1)")
  2235  	tk.MustInterDirc("insert into t1 value(1)")
  2236  	tk.MustQuery("select * from t, t1 where t.c1 = t1.c1").Check(
  2237  		testkit.Events("1 \x00\x01"))
  2238  
  2239  	// compare tinyint to bit(8)
  2240  	tk.MustInterDirc("drop causet if exists t")
  2241  	tk.MustInterDirc("drop causet if exists t1")
  2242  	tk.MustInterDirc("create causet t(c1 tinyint)")
  2243  	tk.MustInterDirc("create causet t1(c1 bit(8))")
  2244  	tk.MustInterDirc("insert into t value(1)")
  2245  	tk.MustInterDirc("insert into t1 value(1)")
  2246  	tk.MustQuery("select * from t, t1 where t.c1 = t1.c1").Check(
  2247  		testkit.Events("1 \x01"))
  2248  }
  2249  
  2250  func (s *testSuiteJoin3) TestIssue19498(c *C) {
  2251  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
  2252  
  2253  	tk.MustInterDirc("drop causet if exists t1;")
  2254  	tk.MustInterDirc("create causet t1 (c_int int, primary key (c_int));")
  2255  	tk.MustInterDirc("insert into t1 values (1),(2),(3),(4)")
  2256  	tk.MustInterDirc("drop causet if exists t2;")
  2257  	tk.MustInterDirc("create causet t2 (c_str varchar(40));")
  2258  	tk.MustInterDirc("insert into t2 values ('zen sammet');")
  2259  	tk.MustInterDirc("insert into t2 values ('happy fermat');")
  2260  	tk.MustInterDirc("insert into t2 values ('happy archimedes');")
  2261  	tk.MustInterDirc("insert into t2 values ('happy hypatia');")
  2262  
  2263  	tk.MustInterDirc("drop causet if exists t3;")
  2264  	tk.MustInterDirc("create causet t3 (c_int int, c_str varchar(40), primary key (c_int), key (c_str));")
  2265  	tk.MustInterDirc("insert into t3 values (1, 'sweet hoover');")
  2266  	tk.MustInterDirc("insert into t3 values (2, 'awesome elion');")
  2267  	tk.MustInterDirc("insert into t3 values (3, 'hungry khayyam');")
  2268  	tk.MustInterDirc("insert into t3 values (4, 'objective kapitsa');")
  2269  
  2270  	rs := tk.MustQuery("select c_str, (select /*+ INL_JOIN(t1,t3) */ max(t1.c_int) from t1, t3 where t1.c_int = t3.c_int and t2.c_str > t3.c_str) q from t2 order by c_str;")
  2271  	rs.Check(testkit.Events("happy archimedes 2", "happy fermat 2", "happy hypatia 2", "zen sammet 4"))
  2272  
  2273  	rs = tk.MustQuery("select c_str, (select /*+ INL_HASH_JOIN(t1,t3) */ max(t1.c_int) from t1, t3 where t1.c_int = t3.c_int and t2.c_str > t3.c_str) q from t2 order by c_str;")
  2274  	rs.Check(testkit.Events("happy archimedes 2", "happy fermat 2", "happy hypatia 2", "zen sammet 4"))
  2275  
  2276  	rs = tk.MustQuery("select c_str, (select /*+ INL_MERGE_JOIN(t1,t3) */ max(t1.c_int) from t1, t3 where t1.c_int = t3.c_int and t2.c_str > t3.c_str) q from t2 order by c_str;")
  2277  	rs.Check(testkit.Events("happy archimedes 2", "happy fermat 2", "happy hypatia 2", "zen sammet 4"))
  2278  }
  2279  
  2280  func (s *testSuiteJoin3) TestIssue19500(c *C) {
  2281  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
  2282  	tk.MustInterDirc("drop causet if exists t1;")
  2283  	tk.MustInterDirc("create causet t1 (c_int int, primary key (c_int));")
  2284  	tk.MustInterDirc("insert into t1 values (1),(2),(3),(4),(5);")
  2285  	tk.MustInterDirc("drop causet if exists t2;")
  2286  	tk.MustInterDirc("create causet t2 (c_int int unsigned, c_str varchar(40), primary key (c_int), key (c_str));")
  2287  	tk.MustInterDirc("insert into t2 values (1, 'dazzling panini'),(2, 'infallible perlman'),(3, 'recursing cannon'),(4, 'vigorous satoshi'),(5, 'vigilant gauss'),(6, 'nervous jackson');\n")
  2288  	tk.MustInterDirc("drop causet if exists t3;")
  2289  	tk.MustInterDirc("create causet t3 (c_int int, c_str varchar(40), key (c_str));")
  2290  	tk.MustInterDirc("insert into t3 values (1, 'sweet morse'),(2, 'reverent golick'),(3, 'clever rubin'),(4, 'flamboyant morse');")
  2291  	tk.MustQuery("select (select (select sum(c_int) from t3 where t3.c_str > t2.c_str) from t2 where t2.c_int > t1.c_int order by c_int limit 1) q from t1 order by q;").
  2292  		Check(testkit.Events("<nil>", "<nil>", "3", "3", "3"))
  2293  }
  2294  
  2295  func (s *testSuiteJoinSerial) TestExplainAnalyzeJoin(c *C) {
  2296  	tk := testkit.NewTestKitWithInit(c, s.causetstore)
  2297  	tk.MustInterDirc("drop causet if exists t1,t2;")
  2298  	tk.MustInterDirc("create causet t1 (a int, b int, unique index (a));")
  2299  	tk.MustInterDirc("create causet t2 (a int, b int, unique index (a))")
  2300  	tk.MustInterDirc("insert into t1 values (1,1),(2,2),(3,3),(4,4),(5,5)")
  2301  	tk.MustInterDirc("insert into t2 values (1,1),(2,2),(3,3),(4,4),(5,5)")
  2302  	// Test for index lookup join.
  2303  	rows := tk.MustQuery("explain analyze select /*+ INL_JOIN(t1, t2) */ * from t1,t2 where t1.a=t2.a;").Events()
  2304  	c.Assert(len(rows), Equals, 8)
  2305  	c.Assert(rows[0][0], Matches, "IndexJoin_.*")
  2306  	c.Assert(rows[0][5], Matches, "time:.*, loops:.*, inner:{total:.*, concurrency:.*, task:.*, construct:.*, fetch:.*, build:.*}, probe:.*")
  2307  	// Test for index lookup hash join.
  2308  	rows = tk.MustQuery("explain analyze select /*+ INL_HASH_JOIN(t1, t2) */ * from t1,t2 where t1.a=t2.a;").Events()
  2309  	c.Assert(len(rows), Equals, 8)
  2310  	c.Assert(rows[0][0], Matches, "IndexHashJoin.*")
  2311  	c.Assert(rows[0][5], Matches, "time:.*, loops:.*, inner:{total:.*, concurrency:.*, task:.*, construct:.*, fetch:.*, build:.*, join:.*}")
  2312  	// Test for hash join.
  2313  	rows = tk.MustQuery("explain analyze select /*+ HASH_JOIN(t1, t2) */ * from t1,t2 where t1.a=t2.a;").Events()
  2314  	c.Assert(len(rows), Equals, 7)
  2315  	c.Assert(rows[0][0], Matches, "HashJoin.*")
  2316  	c.Assert(rows[0][5], Matches, "time:.*, loops:.*, build_hash_block:{total:.*, fetch:.*, build:.*}, probe:{concurrency:5, total:.*, max:.*, probe:.*, fetch:.*}")
  2317  }