github.com/dolthub/go-mysql-server@v0.18.0/enginetest/queries/join_queries.go (about)

     1  // Copyright 2022 Dolthub, 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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package queries
    16  
    17  import (
    18  	"github.com/dolthub/go-mysql-server/sql"
    19  )
    20  
    21  var JoinQueryTests = []QueryTest{
    22  	{
    23  		Query: "select ab.* from ab join pq on a = p where b = (select y from xy where y in (select v from uv where v = b)) order by a;",
    24  		Expected: []sql.Row{
    25  			{0, 2},
    26  			{1, 2},
    27  			{2, 2},
    28  			{3, 1},
    29  		},
    30  	},
    31  	{
    32  		Query: "select * from ab where b in (select y from xy where y in (select v from uv where v = b));",
    33  		Expected: []sql.Row{
    34  			{0, 2},
    35  			{1, 2},
    36  			{2, 2},
    37  			{3, 1},
    38  		},
    39  	},
    40  	{
    41  		Query: "select * from ab where a in (select y from xy where y in (select v from uv where v = a));",
    42  		Expected: []sql.Row{
    43  			{1, 2},
    44  			{2, 2},
    45  		},
    46  	},
    47  	{
    48  		Query: "select * from ab where a in (select x from xy where x in (select u from uv where u = a));",
    49  		Expected: []sql.Row{
    50  			{1, 2},
    51  			{2, 2},
    52  			{0, 2},
    53  			{3, 1},
    54  		},
    55  	},
    56  	{
    57  		// sqe index lookup must reference schema of outer scope after
    58  		// join planning reorders (lookup uv xy)
    59  		Query: `select y, (select 1 from uv where y = 1 and u = x) is_one from xy join uv on x = v order by y;`,
    60  		Expected: []sql.Row{
    61  			{0, nil},
    62  			{0, nil},
    63  			{1, 1},
    64  			{1, 1},
    65  		},
    66  	},
    67  	{
    68  		Query: `select y, (select 1 where y = 1) is_one from xy join uv on x = v order by y`,
    69  		Expected: []sql.Row{
    70  			{0, nil},
    71  			{0, nil},
    72  			{1, 1},
    73  			{1, 1},
    74  		},
    75  	},
    76  	{
    77  		Query: `select * from (select y, (select 1 where y = 1) is_one from xy join uv on x = v) sq order by y`,
    78  		Expected: []sql.Row{
    79  			{0, nil},
    80  			{0, nil},
    81  			{1, 1},
    82  			{1, 1},
    83  		},
    84  	},
    85  	//{
    86  	// TODO this is invalid, should error
    87  	//	Query:    `with cte1 as (select u, v from cte2 join ab on cte2.u = b), cte2 as (select u,v from uv join ab on u = b where u in (2,3)) select * from xy where (x) not in (select u from cte1) order by 1`,
    88  	//	Expected: []sql.Row{{0, 2}, {1, 0}, {3, 3}},
    89  	//},
    90  	{
    91  		Query:    `SELECT (SELECT 1 FROM (SELECT x FROM xy INNER JOIN uv ON (x = u OR y = v) LIMIT 1) r) AS s FROM xy`,
    92  		Expected: []sql.Row{{1}, {1}, {1}, {1}},
    93  	},
    94  	{
    95  		Query:    `select a from ab where exists (select 1 from xy where a =x)`,
    96  		Expected: []sql.Row{{0}, {1}, {2}, {3}},
    97  	},
    98  	{
    99  		Query:    "select a from ab where exists (select 1 from xy where a = x and b = 2 and y = 2);",
   100  		Expected: []sql.Row{{0}},
   101  	},
   102  	{
   103  		Query:    "select * from uv where exists (select 1, count(a) from ab where u = a group by a)",
   104  		Expected: []sql.Row{{0, 1}, {1, 1}, {2, 2}, {3, 2}},
   105  	},
   106  	{
   107  		Query: `
   108  select * from
   109  (
   110    select * from ab
   111    left join uv on a = u
   112    where exists (select * from pq where u = p)
   113  ) alias2
   114  inner join xy on a = x;`,
   115  		Expected: []sql.Row{
   116  			{0, 2, 0, 1, 0, 2},
   117  			{1, 2, 1, 1, 1, 0},
   118  			{2, 2, 2, 2, 2, 1},
   119  			{3, 1, 3, 2, 3, 3},
   120  		},
   121  	},
   122  	{
   123  		Query: `
   124  select * from ab
   125  where exists
   126  (
   127    select * from uv
   128    left join pq on u = p
   129    where a = u
   130  );`,
   131  		Expected: []sql.Row{
   132  			{0, 2},
   133  			{1, 2},
   134  			{2, 2},
   135  			{3, 1},
   136  		},
   137  	},
   138  	{
   139  		Query: `
   140  select * from
   141  (
   142    select * from ab
   143    where not exists (select * from uv where a = v)
   144  ) alias1
   145  where exists (select * from xy where a = x);`,
   146  		Expected: []sql.Row{
   147  			{0, 2},
   148  			{3, 1},
   149  		}},
   150  	{
   151  		Query: `
   152  select * from
   153  (
   154    select * from ab
   155    inner join xy on true
   156  ) alias1
   157  inner join uv on true
   158  inner join pq on true order by 1,2,3,4,5,6,7,8 limit 5;`,
   159  		Expected: []sql.Row{
   160  			{0, 2, 0, 2, 0, 1, 0, 0},
   161  			{0, 2, 0, 2, 0, 1, 1, 1},
   162  			{0, 2, 0, 2, 0, 1, 2, 2},
   163  			{0, 2, 0, 2, 0, 1, 3, 3},
   164  			{0, 2, 0, 2, 1, 1, 0, 0},
   165  		},
   166  	},
   167  	{
   168  		Query: `
   169  	select * from
   170  	(
   171  	 select * from ab
   172  	 where not exists (select * from xy where a = y+1)
   173  	) alias1
   174  	left join pq on alias1.a = p
   175  	where exists (select * from uv where a = u);`,
   176  		Expected: []sql.Row{
   177  			{0, 2, 0, 0},
   178  		}},
   179  	{
   180  		// Repro for: https://github.com/dolthub/dolt/issues/4183
   181  		Query: "SELECT mytable.i " +
   182  			"FROM mytable " +
   183  			"INNER JOIN othertable ON (mytable.i = othertable.i2) " +
   184  			"LEFT JOIN othertable T4 ON (mytable.i = T4.i2) " +
   185  			"ORDER BY othertable.i2, T4.s2",
   186  		Expected: []sql.Row{{1}, {2}, {3}},
   187  	},
   188  	{
   189  		// test cross join used as projected subquery expression
   190  		Query:    "select 1 as exprAlias, 2, 3, (select exprAlias + count(*) from one_pk_three_idx a cross join one_pk_three_idx b);",
   191  		Expected: []sql.Row{{1, 2, 3, 65}},
   192  	},
   193  	{
   194  		// test cross join used in an IndexedInFilter subquery expression
   195  		Query:    "select pk, v1, v2 from one_pk_three_idx where v1 in (select max(a.v1) from one_pk_three_idx a cross join (select 'foo' from dual) b);",
   196  		Expected: []sql.Row{{7, 4, 4}},
   197  	},
   198  	{
   199  		// test cross join used as subquery alias
   200  		Query: "select * from (select a.v1, b.v2 from one_pk_three_idx a cross join one_pk_three_idx b) dt order by 1 desc, 2 desc limit 5;",
   201  		Expected: []sql.Row{
   202  			{4, 4},
   203  			{4, 3},
   204  			{4, 2},
   205  			{4, 1},
   206  			{4, 0},
   207  		},
   208  	},
   209  	{
   210  		Query: "select a.pk, c.v2 from one_pk_three_idx a cross join one_pk_three_idx b left join one_pk_three_idx c on b.pk = c.v2 where b.pk = 0 and a.v2 = 1;",
   211  		Expected: []sql.Row{
   212  			{2, 0},
   213  			{2, 0},
   214  			{2, 0},
   215  			{2, 0},
   216  		},
   217  	},
   218  	{
   219  		Query: "select a.pk, c.v2 from one_pk_three_idx a cross join one_pk_three_idx b right join one_pk_three_idx c on b.pk = c.v3 where b.pk = 0 and c.v2 = 0 order by a.pk;",
   220  		Expected: []sql.Row{
   221  			{0, 0},
   222  			{0, 0},
   223  			{1, 0},
   224  			{1, 0},
   225  			{2, 0},
   226  			{2, 0},
   227  			{3, 0},
   228  			{3, 0},
   229  			{4, 0},
   230  			{4, 0},
   231  			{5, 0},
   232  			{5, 0},
   233  			{6, 0},
   234  			{6, 0},
   235  			{7, 0},
   236  			{7, 0},
   237  		},
   238  	},
   239  	{
   240  		Query: "select a.pk, c.v2 from one_pk_three_idx a cross join one_pk_three_idx b inner join (select * from one_pk_three_idx where v2 = 0) c on b.pk = c.v3 where b.pk = 0 and c.v2 = 0 order by a.pk;",
   241  		Expected: []sql.Row{
   242  			{0, 0},
   243  			{0, 0},
   244  			{1, 0},
   245  			{1, 0},
   246  			{2, 0},
   247  			{2, 0},
   248  			{3, 0},
   249  			{3, 0},
   250  			{4, 0},
   251  			{4, 0},
   252  			{5, 0},
   253  			{5, 0},
   254  			{6, 0},
   255  			{6, 0},
   256  			{7, 0},
   257  			{7, 0},
   258  		},
   259  	},
   260  	{
   261  		Query: "select a.pk, c.v2 from one_pk_three_idx a cross join one_pk_three_idx b left join one_pk_three_idx c on b.pk = c.v1+1 where b.pk = 0 order by a.pk;",
   262  		Expected: []sql.Row{
   263  			{0, nil},
   264  			{1, nil},
   265  			{2, nil},
   266  			{3, nil},
   267  			{4, nil},
   268  			{5, nil},
   269  			{6, nil},
   270  			{7, nil},
   271  		},
   272  	},
   273  	{
   274  		Query: "select a.pk, c.v2 from one_pk_three_idx a cross join one_pk_three_idx b right join one_pk_three_idx c on b.pk = c.v1 where b.pk = 0 and c.v2 = 0 order by a.pk;",
   275  		Expected: []sql.Row{
   276  			{0, 0},
   277  			{0, 0},
   278  			{1, 0},
   279  			{1, 0},
   280  			{2, 0},
   281  			{2, 0},
   282  			{3, 0},
   283  			{3, 0},
   284  			{4, 0},
   285  			{4, 0},
   286  			{5, 0},
   287  			{5, 0},
   288  			{6, 0},
   289  			{6, 0},
   290  			{7, 0},
   291  			{7, 0},
   292  		},
   293  	},
   294  	{
   295  		Query: "select * from mytable a CROSS JOIN mytable b RIGHT JOIN mytable c ON b.i = c.i + 1 order by 1,2,3,4,5,6;",
   296  		Expected: []sql.Row{
   297  			{nil, nil, nil, nil, 3, "third row"},
   298  			{1, "first row", 2, "second row", 1, "first row"},
   299  			{1, "first row", 3, "third row", 2, "second row"},
   300  			{2, "second row", 2, "second row", 1, "first row"},
   301  			{2, "second row", 3, "third row", 2, "second row"},
   302  			{3, "third row", 2, "second row", 1, "first row"},
   303  			{3, "third row", 3, "third row", 2, "second row"},
   304  		},
   305  	},
   306  	{
   307  		Query: "select * from mytable a CROSS JOIN mytable b LEFT JOIN mytable c ON b.i = c.i + 1 order by 1,2,3,4,5,6;",
   308  		Expected: []sql.Row{
   309  			{1, "first row", 1, "first row", nil, nil},
   310  			{1, "first row", 2, "second row", 1, "first row"},
   311  			{1, "first row", 3, "third row", 2, "second row"},
   312  			{2, "second row", 1, "first row", nil, nil},
   313  			{2, "second row", 2, "second row", 1, "first row"},
   314  			{2, "second row", 3, "third row", 2, "second row"},
   315  			{3, "third row", 1, "first row", nil, nil},
   316  			{3, "third row", 2, "second row", 1, "first row"},
   317  			{3, "third row", 3, "third row", 2, "second row"},
   318  		},
   319  	},
   320  	{
   321  		Query: "select a.i, b.i, c.i from mytable a CROSS JOIN mytable b LEFT JOIN mytable c ON b.i+1 = c.i order by 1,2,3;",
   322  		Expected: []sql.Row{
   323  			{1, 1, 2},
   324  			{1, 2, 3},
   325  			{1, 3, nil},
   326  			{2, 1, 2},
   327  			{2, 2, 3},
   328  			{2, 3, nil},
   329  			{3, 1, 2},
   330  			{3, 2, 3},
   331  			{3, 3, nil},
   332  		}},
   333  	{
   334  		Query: "select * from mytable a LEFT JOIN mytable b on a.i = b.i LEFT JOIN mytable c ON b.i = c.i + 1 order by 1,2,3,4,5,6;",
   335  		Expected: []sql.Row{
   336  			{1, "first row", 1, "first row", nil, nil},
   337  			{2, "second row", 2, "second row", 1, "first row"},
   338  			{3, "third row", 3, "third row", 2, "second row"},
   339  		},
   340  	},
   341  	{
   342  		Query: "select * from mytable a LEFT JOIN  mytable b on a.i = b.i RIGHT JOIN mytable c ON b.i = c.i + 1 order by 1,2,3,4,5,6;",
   343  		Expected: []sql.Row{
   344  			{nil, nil, nil, nil, 3, "third row"},
   345  			{2, "second row", 2, "second row", 1, "first row"},
   346  			{3, "third row", 3, "third row", 2, "second row"},
   347  		},
   348  	},
   349  	{
   350  		Query: "select * from mytable a RIGHT JOIN mytable b on a.i = b.i RIGHT JOIN mytable c ON b.i = c.i + 1 order by 1,2,3,4,5,6;",
   351  		Expected: []sql.Row{
   352  			{nil, nil, nil, nil, 3, "third row"},
   353  			{2, "second row", 2, "second row", 1, "first row"},
   354  			{3, "third row", 3, "third row", 2, "second row"},
   355  		},
   356  	},
   357  	{
   358  		Query: "select * from mytable a RIGHT JOIN mytable b on a.i = b.i LEFT JOIN mytable c ON b.i = c.i + 1;",
   359  		Expected: []sql.Row{
   360  			{1, "first row", 1, "first row", nil, nil},
   361  			{2, "second row", 2, "second row", 1, "first row"},
   362  			{3, "third row", 3, "third row", 2, "second row"},
   363  		},
   364  	},
   365  	{
   366  		Query: "select * from mytable a LEFT JOIN mytable b on a.i = b.i LEFT JOIN mytable c ON b.i+1 = c.i;",
   367  		Expected: []sql.Row{
   368  			{1, "first row", 1, "first row", 2, "second row"},
   369  			{2, "second row", 2, "second row", 3, "third row"},
   370  			{3, "third row", 3, "third row", nil, nil},
   371  		}},
   372  	{
   373  		Query: "select * from mytable a LEFT JOIN  mytable b on a.i = b.i RIGHT JOIN mytable c ON b.i+1 = c.i order by 1,2,3,4,5,6;",
   374  		Expected: []sql.Row{
   375  			{nil, nil, nil, nil, 1, "first row"},
   376  			{1, "first row", 1, "first row", 2, "second row"},
   377  			{2, "second row", 2, "second row", 3, "third row"},
   378  		}},
   379  	{
   380  		Query: "select * from mytable a RIGHT JOIN mytable b on a.i = b.i RIGHT JOIN mytable c ON b.i+1= c.i order by 1,2,3,4,5,6;",
   381  		Expected: []sql.Row{
   382  			{nil, nil, nil, nil, 1, "first row"},
   383  			{1, "first row", 1, "first row", 2, "second row"},
   384  			{2, "second row", 2, "second row", 3, "third row"},
   385  		}},
   386  	{
   387  		Query: "select * from mytable a RIGHT JOIN mytable b on a.i = b.i LEFT JOIN mytable c ON b.i+1 = c.i order by 1,2,3,4,5,6;",
   388  		Expected: []sql.Row{
   389  			{1, "first row", 1, "first row", 2, "second row"},
   390  			{2, "second row", 2, "second row", 3, "third row"},
   391  			{3, "third row", 3, "third row", nil, nil},
   392  		},
   393  	},
   394  	{
   395  		Query: "select * from mytable a CROSS JOIN mytable b RIGHT JOIN mytable c ON b.i+1 = c.i order by 1,2,3,4,5,6;",
   396  		Expected: []sql.Row{
   397  			{nil, nil, nil, nil, 1, "first row"},
   398  			{1, "first row", 1, "first row", 2, "second row"},
   399  			{1, "first row", 2, "second row", 3, "third row"},
   400  			{2, "second row", 1, "first row", 2, "second row"},
   401  			{2, "second row", 2, "second row", 3, "third row"},
   402  			{3, "third row", 1, "first row", 2, "second row"},
   403  			{3, "third row", 2, "second row", 3, "third row"},
   404  		},
   405  	},
   406  	{
   407  		Query: "with a as (select a.i, a.s from mytable a CROSS JOIN mytable b) select * from a RIGHT JOIN mytable c on a.i+1 = c.i-1;",
   408  		Expected: []sql.Row{
   409  			{nil, nil, 1, "first row"},
   410  			{nil, nil, 2, "second row"},
   411  			{1, "first row", 3, "third row"},
   412  			{1, "first row", 3, "third row"},
   413  			{1, "first row", 3, "third row"},
   414  		},
   415  	},
   416  	{
   417  		Query: "select a.* from mytable a RIGHT JOIN mytable b on a.i = b.i+1 LEFT JOIN mytable c on a.i = c.i-1 RIGHT JOIN mytable d on b.i = d.i;",
   418  		Expected: []sql.Row{
   419  			{2, "second row"},
   420  			{3, "third row"},
   421  			{nil, nil},
   422  		},
   423  	},
   424  	{
   425  		Query: "select a.*,b.* from mytable a RIGHT JOIN othertable b on a.i = b.i2+1 LEFT JOIN mytable c on a.i = c.i-1 LEFT JOIN othertable d on b.i2 = d.i2;",
   426  		Expected: []sql.Row{
   427  			{2, "second row", "third", 1},
   428  			{3, "third row", "second", 2},
   429  			{nil, nil, "first", 3},
   430  		},
   431  	},
   432  	{
   433  		Query: "select a.*,b.* from mytable a RIGHT JOIN othertable b on a.i = b.i2+1 RIGHT JOIN mytable c on a.i = c.i-1 LEFT JOIN othertable d on b.i2 = d.i2;",
   434  		Expected: []sql.Row{
   435  			{nil, nil, nil, nil},
   436  			{nil, nil, nil, nil},
   437  			{2, "second row", "third", 1},
   438  		},
   439  	},
   440  	{
   441  		Query:    "select i.pk, j.v3 from one_pk_two_idx i JOIN one_pk_three_idx j on i.v1 = j.pk;",
   442  		Expected: []sql.Row{{0, 0}, {1, 1}, {2, 0}, {3, 2}, {4, 0}, {5, 3}, {6, 0}, {7, 4}},
   443  	},
   444  	{
   445  		Query:    "select i.pk, j.v3, k.c1 from one_pk_two_idx i JOIN one_pk_three_idx j on i.v1 = j.pk JOIN one_pk k on j.v3 = k.pk;",
   446  		Expected: []sql.Row{{0, 0, 0}, {1, 1, 10}, {2, 0, 0}, {3, 2, 20}, {4, 0, 0}, {5, 3, 30}, {6, 0, 0}},
   447  	},
   448  	{
   449  		Query:    "select i.pk, j.v3 from (one_pk_two_idx i JOIN one_pk_three_idx j on((i.v1 = j.pk)));",
   450  		Expected: []sql.Row{{0, 0}, {1, 1}, {2, 0}, {3, 2}, {4, 0}, {5, 3}, {6, 0}, {7, 4}},
   451  	},
   452  	{
   453  		Query:    "select i.pk, j.v3, k.c1 from ((one_pk_two_idx i JOIN one_pk_three_idx j on ((i.v1 = j.pk))) JOIN one_pk k on((j.v3 = k.pk)));",
   454  		Expected: []sql.Row{{0, 0, 0}, {1, 1, 10}, {2, 0, 0}, {3, 2, 20}, {4, 0, 0}, {5, 3, 30}, {6, 0, 0}},
   455  	},
   456  	{
   457  		Query:    "select i.pk, j.v3, k.c1 from (one_pk_two_idx i JOIN one_pk_three_idx j on ((i.v1 = j.pk)) JOIN one_pk k on((j.v3 = k.pk)));",
   458  		Expected: []sql.Row{{0, 0, 0}, {1, 1, 10}, {2, 0, 0}, {3, 2, 20}, {4, 0, 0}, {5, 3, 30}, {6, 0, 0}},
   459  	},
   460  	{
   461  		Query: "select a.* from one_pk_two_idx a RIGHT JOIN (one_pk_two_idx i JOIN one_pk_three_idx j on i.v1 = j.pk) on a.pk = i.v1 LEFT JOIN (one_pk_two_idx k JOIN one_pk_three_idx l on k.v1 = l.pk) on a.pk = l.v2;",
   462  		Expected: []sql.Row{{0, 0, 0},
   463  			{0, 0, 0},
   464  			{0, 0, 0},
   465  			{0, 0, 0},
   466  			{1, 1, 1},
   467  			{2, 2, 2},
   468  			{3, 3, 3},
   469  			{4, 4, 4},
   470  			{5, 5, 5},
   471  			{6, 6, 6},
   472  			{7, 7, 7}},
   473  	},
   474  	{
   475  		Query: "select a.* from one_pk_two_idx a LEFT JOIN (one_pk_two_idx i JOIN one_pk_three_idx j on i.pk = j.v3) on a.pk = i.pk RIGHT JOIN (one_pk_two_idx k JOIN one_pk_three_idx l on k.v2 = l.v3) on a.v1 = l.v2;",
   476  		Expected: []sql.Row{{0, 0, 0},
   477  			{0, 0, 0},
   478  			{0, 0, 0},
   479  			{0, 0, 0},
   480  			{0, 0, 0},
   481  			{0, 0, 0},
   482  			{0, 0, 0},
   483  			{0, 0, 0},
   484  			{1, 1, 1},
   485  			{2, 2, 2},
   486  			{0, 0, 0},
   487  			{0, 0, 0},
   488  			{0, 0, 0},
   489  			{0, 0, 0},
   490  			{0, 0, 0},
   491  			{0, 0, 0},
   492  			{0, 0, 0},
   493  			{0, 0, 0},
   494  			{3, 3, 3},
   495  			{4, 4, 4},
   496  		},
   497  	},
   498  	{
   499  		Query: "select a.* from mytable a join mytable b on a.i = b.i and a.i > 2",
   500  		Expected: []sql.Row{
   501  			{3, "third row"},
   502  		},
   503  	},
   504  	{
   505  		Query: "select a.* from mytable a join mytable b on a.i = b.i and now() >= coalesce(NULL, NULL, now())",
   506  		Expected: []sql.Row{
   507  			{1, "first row"},
   508  			{2, "second row"},
   509  			{3, "third row"}},
   510  	},
   511  	{
   512  		Query: "select * from mytable a join niltable  b on a.i = b.i and b <=> NULL",
   513  		Expected: []sql.Row{
   514  			{1, "first row", 1, nil, nil, nil},
   515  		},
   516  	},
   517  	{
   518  		Query: "select * from mytable a join niltable  b on a.i = b.i and s IS NOT NULL",
   519  		Expected: []sql.Row{
   520  			{1, "first row", 1, nil, nil, nil},
   521  			{2, "second row", 2, 2, 1, nil},
   522  			{3, "third row", 3, nil, 0, nil},
   523  		},
   524  	},
   525  	{
   526  		Query: "select * from mytable a join niltable  b on a.i = b.i and b IS NOT NULL",
   527  		Expected: []sql.Row{
   528  			{2, "second row", 2, 2, 1, nil},
   529  			{3, "third row", 3, nil, 0, nil},
   530  		},
   531  	},
   532  	{
   533  		Query: "select * from mytable a join niltable  b on a.i = b.i and b != 0",
   534  		Expected: []sql.Row{
   535  			{2, "second row", 2, 2, 1, nil},
   536  		},
   537  	},
   538  	{
   539  		Query: "select * from mytable a join niltable  b on a.i <> b.i and b != 0;",
   540  		Expected: []sql.Row{
   541  			{3, "third row", 2, 2, 1, nil},
   542  			{1, "first row", 2, 2, 1, nil},
   543  			{3, "third row", 5, nil, 1, float64(5)},
   544  			{2, "second row", 5, nil, 1, float64(5)},
   545  			{1, "first row", 5, nil, 1, float64(5)},
   546  		},
   547  	},
   548  	{
   549  		Query: "select * from mytable a join niltable  b on a.i <> b.i;",
   550  		Expected: []sql.Row{
   551  			{3, "third row", 1, nil, nil, nil},
   552  			{2, "second row", 1, nil, nil, nil},
   553  			{3, "third row", 2, 2, 1, nil},
   554  			{1, "first row", 2, 2, 1, nil},
   555  			{2, "second row", 3, nil, 0, nil},
   556  			{1, "first row", 3, nil, 0, nil},
   557  			{3, "third row", 5, nil, 1, float64(5)},
   558  			{2, "second row", 5, nil, 1, float64(5)},
   559  			{1, "first row", 5, nil, 1, float64(5)},
   560  			{3, "third row", 4, 4, nil, float64(4)},
   561  			{2, "second row", 4, 4, nil, float64(4)},
   562  			{1, "first row", 4, 4, nil, float64(4)},
   563  			{3, "third row", 6, 6, 0, float64(6)},
   564  			{2, "second row", 6, 6, 0, float64(6)},
   565  			{1, "first row", 6, 6, 0, float64(6)},
   566  		},
   567  	},
   568  	{
   569  		//SkipPrepared: true,
   570  		Query: `SELECT pk as pk, nt.i  as i, nt2.i as i FROM one_pk
   571  						RIGHT JOIN niltable nt ON pk=nt.i
   572  						RIGHT JOIN niltable nt2 ON pk=nt2.i - 1
   573  						ORDER BY 3;`,
   574  		Expected: []sql.Row{
   575  			{nil, nil, 1},
   576  			{1, 1, 2},
   577  			{2, 2, 3},
   578  			{3, 3, 4},
   579  			{nil, nil, 5},
   580  			{nil, nil, 6},
   581  		},
   582  	},
   583  	{
   584  		Query: "select * from ab full join pq on a = p order by 1,2,3,4;",
   585  		Expected: []sql.Row{
   586  			{0, 2, 0, 0},
   587  			{1, 2, 1, 1},
   588  			{2, 2, 2, 2},
   589  			{3, 1, 3, 3},
   590  		},
   591  	},
   592  	{
   593  		Query: `
   594  	select * from ab
   595  	inner join uv on a = u
   596  	full join pq on a = p order by 1,2,3,4,5,6;`,
   597  		Expected: []sql.Row{
   598  			{0, 2, 0, 1, 0, 0},
   599  			{1, 2, 1, 1, 1, 1},
   600  			{2, 2, 2, 2, 2, 2},
   601  			{3, 1, 3, 2, 3, 3},
   602  		},
   603  	},
   604  	{
   605  		Query: `
   606  	select * from ab
   607  	full join pq on a = p
   608  	left join xy on a = x order by 1,2,3,4,5,6;`,
   609  		Expected: []sql.Row{
   610  			{0, 2, 0, 0, 0, 2},
   611  			{1, 2, 1, 1, 1, 0},
   612  			{2, 2, 2, 2, 2, 1},
   613  			{3, 1, 3, 3, 3, 3},
   614  		},
   615  	},
   616  	{
   617  		Query: `select * from (select a,v from ab join uv on a=u) av join (select x,q from xy join pq on x = p) xq on av.v = xq.x`,
   618  		Expected: []sql.Row{
   619  			{0, 1, 1, 1},
   620  			{1, 1, 1, 1},
   621  			{2, 2, 2, 2},
   622  			{3, 2, 2, 2},
   623  		},
   624  	},
   625  	{
   626  		Query:    "select x from xy join uv on y = v join ab on y = b and u = -1",
   627  		Expected: []sql.Row{},
   628  	},
   629  	{
   630  		Query: "select a.* from one_pk_two_idx a LEFT JOIN (one_pk_two_idx i JOIN one_pk_three_idx j on i.pk = j.v3) on a.pk = i.pk LEFT JOIN (one_pk_two_idx k JOIN one_pk_three_idx l on k.v2 = l.v3) on a.v1 = l.v2;",
   631  		Expected: []sql.Row{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0},
   632  			{0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {1, 1, 1}, {2, 2, 2}, {3, 3, 3}, {4, 4, 4}, {5, 5, 5}, {6, 6, 6}, {7, 7, 7},
   633  		},
   634  	},
   635  	{
   636  		Query:    "with recursive a(x,y) as (select i,i from mytable where i < 4 union select a.x, mytable.i from a join mytable on a.x+1 = mytable.i limit 2) select * from a;",
   637  		Expected: []sql.Row{{1, 1}, {2, 2}},
   638  	},
   639  	{
   640  		Query: `
   641  select * from (
   642      (ab JOIN pq ON (1 = p))
   643  	LEFT OUTER JOIN uv on (2 = u)
   644  );`,
   645  		Expected: []sql.Row{
   646  			{0, 2, 1, 1, 2, 2},
   647  			{1, 2, 1, 1, 2, 2},
   648  			{2, 2, 1, 1, 2, 2},
   649  			{3, 1, 1, 1, 2, 2},
   650  		},
   651  	},
   652  	{
   653  		Query: "select * from (ab JOIN pq ON (a = 1)) where a in (1,2,3)",
   654  		Expected: []sql.Row{
   655  			{1, 2, 0, 0},
   656  			{1, 2, 1, 1},
   657  			{1, 2, 2, 2},
   658  			{1, 2, 3, 3}},
   659  	},
   660  	{
   661  		Query: "select * from (ab JOIN pq ON (a = p)) where a in (select a from ab)",
   662  		Expected: []sql.Row{
   663  			{0, 2, 0, 0},
   664  			{1, 2, 1, 1},
   665  			{2, 2, 2, 2},
   666  			{3, 1, 3, 3}},
   667  	},
   668  	{
   669  		Query: "select * from (ab JOIN pq ON (a = 1)) where a in (select a from ab)",
   670  		Expected: []sql.Row{
   671  			{1, 2, 0, 0},
   672  			{1, 2, 1, 1},
   673  			{1, 2, 2, 2},
   674  			{1, 2, 3, 3}},
   675  	},
   676  	{
   677  		Query: "select * from (ab JOIN pq) where a in (select a from ab)",
   678  		Expected: []sql.Row{
   679  			{0, 2, 0, 0},
   680  			{0, 2, 1, 1},
   681  			{0, 2, 2, 2},
   682  			{0, 2, 3, 3},
   683  			{1, 2, 0, 0},
   684  			{1, 2, 1, 1},
   685  			{1, 2, 2, 2},
   686  			{1, 2, 3, 3},
   687  			{2, 2, 0, 0},
   688  			{2, 2, 1, 1},
   689  			{2, 2, 2, 2},
   690  			{2, 2, 3, 3},
   691  			{3, 1, 0, 0},
   692  			{3, 1, 1, 1},
   693  			{3, 1, 2, 2},
   694  			{3, 1, 3, 3}},
   695  	},
   696  	{
   697  		Query: "select * from (ab JOIN pq ON (a = 1)) where a in (1,2,3)",
   698  		Expected: []sql.Row{
   699  			{1, 2, 0, 0},
   700  			{1, 2, 1, 1},
   701  			{1, 2, 2, 2},
   702  			{1, 2, 3, 3}},
   703  	},
   704  	{
   705  		Query: "select * from (ab JOIN pq ON (a = 1)) where a in (select a from ab)",
   706  		Expected: []sql.Row{
   707  			{1, 2, 0, 0},
   708  			{1, 2, 1, 1},
   709  			{1, 2, 2, 2},
   710  			{1, 2, 3, 3}},
   711  	},
   712  	{
   713  		// verify this troublesome query from dolt with a syntactically similar query:
   714  		// SELECT count(*) from dolt_log('main') join dolt_diff(@Commit1, @Commit2, 't') where commit_hash = to_commit;
   715  		Query: `SELECT count(*)
   716  FROM
   717  JSON_TABLE(
   718  	'[{"a":1.5, "b":2.25},{"a":3.125, "b":4.0625}]',
   719  	'$[*]' COLUMNS(x float path '$.a', y float path '$.b')
   720  ) as t1
   721  join
   722  JSON_TABLE(
   723  	'[{"c":2, "d":3},{"c":4, "d":5}]',
   724  	'$[*]' COLUMNS(z float path '$.c', w float path '$.d')
   725  ) as t2
   726  on w = 0;`,
   727  		Expected: []sql.Row{{0}},
   728  	},
   729  	{
   730  		Query:    `SELECT * from xy_hasnull where y not in (SELECT b from ab_hasnull)`,
   731  		Expected: []sql.Row{},
   732  	},
   733  	{
   734  		Query:    `SELECT * from xy_hasnull where y not in (SELECT b from ab)`,
   735  		Expected: []sql.Row{{1, 0}},
   736  	},
   737  	{
   738  		Query:    `SELECT * from xy where y not in (SELECT b from ab_hasnull)`,
   739  		Expected: []sql.Row{},
   740  	},
   741  	{
   742  		Query:    `SELECT * from xy where null not in (SELECT b from ab)`,
   743  		Expected: []sql.Row{},
   744  	},
   745  	{
   746  		Query:    "select * from othertable join foo.othertable on othertable.s2 = 'third'",
   747  		Expected: []sql.Row{{"third", 1, "a", 4}, {"third", 1, "b", 2}, {"third", 1, "c", 0}},
   748  	},
   749  	{
   750  		Query:    "select * from othertable join foo.othertable on mydb.othertable.s2 = 'third'",
   751  		Expected: []sql.Row{{"third", 1, "a", 4}, {"third", 1, "b", 2}, {"third", 1, "c", 0}},
   752  	},
   753  	{
   754  		Query:    "select * from othertable join foo.othertable on foo.othertable.text = 'a'",
   755  		Expected: []sql.Row{{"third", 1, "a", 4}, {"second", 2, "a", 4}, {"first", 3, "a", 4}},
   756  	},
   757  	{
   758  		Query:    "select * from foo.othertable join othertable on othertable.s2 = 'third'",
   759  		Expected: []sql.Row{{"a", 4, "third", 1}, {"b", 2, "third", 1}, {"c", 0, "third", 1}},
   760  	},
   761  	{
   762  		Query:    "select * from foo.othertable join othertable on mydb.othertable.s2 = 'third'",
   763  		Expected: []sql.Row{{"a", 4, "third", 1}, {"b", 2, "third", 1}, {"c", 0, "third", 1}},
   764  	},
   765  	{
   766  		Query:    "select * from foo.othertable join othertable on foo.othertable.text = 'a'",
   767  		Expected: []sql.Row{{"a", 4, "third", 1}, {"a", 4, "second", 2}, {"a", 4, "first", 3}},
   768  	},
   769  	{
   770  		Query:    "select * from mydb.othertable join foo.othertable on othertable.s2 = 'third'",
   771  		Expected: []sql.Row{{"third", 1, "a", 4}, {"third", 1, "b", 2}, {"third", 1, "c", 0}},
   772  	},
   773  	{
   774  		Query:    "select * from mydb.othertable join foo.othertable on mydb.othertable.s2 = 'third'",
   775  		Expected: []sql.Row{{"third", 1, "a", 4}, {"third", 1, "b", 2}, {"third", 1, "c", 0}},
   776  	},
   777  	{
   778  		Query:    "select * from mydb.othertable join foo.othertable on foo.othertable.text = 'a'",
   779  		Expected: []sql.Row{{"third", 1, "a", 4}, {"second", 2, "a", 4}, {"first", 3, "a", 4}},
   780  	},
   781  	{
   782  		Query:    "select * from foo.othertable join mydb.othertable on othertable.s2 = 'third'",
   783  		Expected: []sql.Row{{"a", 4, "third", 1}, {"b", 2, "third", 1}, {"c", 0, "third", 1}},
   784  	},
   785  	{
   786  		Query:    "select * from foo.othertable join mydb.othertable on mydb.othertable.s2 = 'third'",
   787  		Expected: []sql.Row{{"a", 4, "third", 1}, {"b", 2, "third", 1}, {"c", 0, "third", 1}},
   788  	},
   789  	{
   790  		Query:    "select * from foo.othertable join mydb.othertable on foo.othertable.text = 'a'",
   791  		Expected: []sql.Row{{"a", 4, "third", 1}, {"a", 4, "second", 2}, {"a", 4, "first", 3}},
   792  	},
   793  }
   794  
   795  var JoinScriptTests = []ScriptTest{
   796  	{
   797  		Name:        "Simple join query",
   798  		SetUpScript: []string{},
   799  		Assertions: []ScriptTestAssertion{
   800  			{
   801  				Query:       "select x from xy, uv join ab on x = a and u = -1;",
   802  				ExpectedErr: sql.ErrColumnNotFound,
   803  			},
   804  		},
   805  	},
   806  	{
   807  		Name: "Complex join query with foreign key constraints",
   808  		SetUpScript: []string{
   809  			"CREATE TABLE `users` (`id` int NOT NULL AUTO_INCREMENT, `username` varchar(255) NOT NULL, PRIMARY KEY (`id`));",
   810  			"CREATE TABLE `tweet` ( `id` int NOT NULL AUTO_INCREMENT, `user_id` int NOT NULL, `content` text NOT NULL, `timestamp` bigint NOT NULL, PRIMARY KEY (`id`), KEY `tweet_user_id` (`user_id`), CONSTRAINT `0qpfesgd` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`));",
   811  			"INSERT INTO `users` (`id`,`username`) VALUES (1,'huey'), (2,'zaizee'), (3,'mickey')",
   812  			"INSERT INTO `tweet` (`id`,`user_id`,`content`,`timestamp`) VALUES (1,1,'meow',1647463727), (2,1,'purr',1647463727), (3,2,'hiss',1647463727), (4,3,'woof',1647463727)",
   813  		},
   814  		Assertions: []ScriptTestAssertion{
   815  			{
   816  				Query:    " SELECT `t1`.`username`, COUNT(`t1`.`id`) AS `ct` FROM ((SELECT `t2`.`id`, `t2`.`content`, `t3`.`username` FROM `tweet` AS `t2` INNER JOIN `users` AS `t3` ON (`t2`.`user_id` = `t3`.`id`) WHERE (`t3`.`username` = 'u3')) UNION (SELECT `t4`.`id`, `t4`.`content`, `t5`.`username` FROM `tweet` AS `t4` INNER JOIN `users` AS `t5` ON (`t4`.`user_id` = `t5`.`id`) WHERE (`t5`.`username` IN ('u2', 'u4')))) AS `t1` GROUP BY `t1`.`username` ORDER BY COUNT(`t1`.`id`) DESC;",
   817  				Expected: []sql.Row{},
   818  			},
   819  		},
   820  	},
   821  	{
   822  		Name: "USING join tests",
   823  		SetUpScript: []string{
   824  			"create table t1 (i int primary key, j int);",
   825  			"create table t2 (i int primary key, j int);",
   826  			"create table t3 (i int primary key, j int);",
   827  			"insert into t1 values (1, 10), (2, 20), (3, 30);",
   828  			"insert into t2 values (1, 30), (2, 20), (5, 50);",
   829  			"insert into t3 values (1, 200), (2, 20), (6, 600);",
   830  		},
   831  		Assertions: []ScriptTestAssertion{
   832  			// Basic tests
   833  			{
   834  				Query:       "select * from t1 join t2 using (badcol);",
   835  				ExpectedErr: sql.ErrUnknownColumn,
   836  			},
   837  			{
   838  				Query: "select i from t1 join t2 using (i);",
   839  				Expected: []sql.Row{
   840  					{1},
   841  					{2},
   842  				},
   843  			},
   844  			{
   845  				Query:       "select j from t1 join t2 using (i);",
   846  				ExpectedErr: sql.ErrAmbiguousColumnName,
   847  			},
   848  
   849  			{
   850  				Query: "select * from t1 join t2 using (i);",
   851  				Expected: []sql.Row{
   852  					{1, 10, 30},
   853  					{2, 20, 20},
   854  				},
   855  			},
   856  			{
   857  				Query: "select t1.i, t1.j, t2.i, t2.j from t1 join t2 using (i);",
   858  				Expected: []sql.Row{
   859  					{1, 10, 1, 30},
   860  					{2, 20, 2, 20},
   861  				},
   862  			},
   863  			{
   864  				Query: "select * from t1 join t2 using (j);",
   865  				Expected: []sql.Row{
   866  					{30, 3, 1},
   867  					{20, 2, 2},
   868  				},
   869  			},
   870  			{
   871  				Query: "select t1.i, t1.j, t2.i, t2.j from t1 join t2 using (j);",
   872  				Expected: []sql.Row{
   873  					{3, 30, 1, 30},
   874  					{2, 20, 2, 20},
   875  				},
   876  			},
   877  			{
   878  				Query: "select * from t1 join t2 using (i, j);",
   879  				Expected: []sql.Row{
   880  					{2, 20},
   881  				},
   882  			},
   883  			{
   884  				Query: "select * from t1 join t2 using (j, i);",
   885  				Expected: []sql.Row{
   886  					{2, 20},
   887  				},
   888  			},
   889  			{
   890  				Query: "select * from t1 natural join t2;",
   891  				Expected: []sql.Row{
   892  					{2, 20},
   893  				},
   894  			},
   895  			{
   896  				Query: "select t1.i, t1.j, t2.i, t2.j from t1 join t2 using (i, j);",
   897  				Expected: []sql.Row{
   898  					{2, 20, 2, 20},
   899  				},
   900  			},
   901  			{
   902  				Query: "select i, j, t1.*, t2.*, t1.i, t1.j, t2.i, t2.j from t1 join t2 using (i, j);",
   903  				Expected: []sql.Row{
   904  					{2, 20, 2, 20, 2, 20, 2, 20, 2, 20},
   905  				},
   906  			},
   907  			{
   908  				Query: "select i, j, t1.*, t2.*, t1.i, t1.j, t2.i, t2.j from t1 natural join t2;",
   909  				Expected: []sql.Row{
   910  					{2, 20, 2, 20, 2, 20, 2, 20, 2, 20},
   911  				},
   912  			},
   913  			{
   914  				Query: "select i, j, a.*, b.*, a.i, a.j, b.i, b.j from t1 a join t2 b using (i, j);",
   915  				Expected: []sql.Row{
   916  					{2, 20, 2, 20, 2, 20, 2, 20, 2, 20},
   917  				},
   918  			},
   919  			{
   920  				Query: "select i, j, a.*, b.*, a.i, a.j, b.i, b.j from t1 a natural join t2 b;",
   921  				Expected: []sql.Row{
   922  					{2, 20, 2, 20, 2, 20, 2, 20, 2, 20},
   923  				},
   924  			},
   925  
   926  			// Left Join
   927  			{
   928  				Query: "select * from t1 left join t2 using (i);",
   929  				Expected: []sql.Row{
   930  					{1, 10, 30},
   931  					{2, 20, 20},
   932  					{3, 30, nil},
   933  				},
   934  			},
   935  			{
   936  				Query: "select t1.i, t1.j, t2.i, t2.j from t1 left join t2 using (i);",
   937  				Expected: []sql.Row{
   938  					{1, 10, 1, 30},
   939  					{2, 20, 2, 20},
   940  					{3, 30, nil, nil},
   941  				},
   942  			},
   943  			{
   944  				Query: "select * from t1 left join t2 using (i, j);",
   945  				Expected: []sql.Row{
   946  					{1, 10},
   947  					{2, 20},
   948  					{3, 30},
   949  				},
   950  			},
   951  			{
   952  				Query: "select * from t1 natural left join t2;",
   953  				Expected: []sql.Row{
   954  					{1, 10},
   955  					{2, 20},
   956  					{3, 30},
   957  				},
   958  			},
   959  			{
   960  				Query: "select t1.i, t1.j, t2.i, t2.j from t1 left join t2 using (i, j);",
   961  				Expected: []sql.Row{
   962  					{1, 10, nil, nil},
   963  					{2, 20, 2, 20},
   964  					{3, 30, nil, nil},
   965  				},
   966  			},
   967  			{
   968  				Query: "select t1.i, t1.j, t2.i, t2.j from t1 natural left join t2;",
   969  				Expected: []sql.Row{
   970  					{1, 10, nil, nil},
   971  					{2, 20, 2, 20},
   972  					{3, 30, nil, nil},
   973  				},
   974  			},
   975  
   976  			// Right Join
   977  			{
   978  				Query: "select * from t1 right join t2 using (i);",
   979  				Expected: []sql.Row{
   980  					{1, 30, 10},
   981  					{2, 20, 20},
   982  					{5, 50, nil},
   983  				},
   984  			},
   985  			{
   986  				Query: "select t1.i, t1.j, t2.i, t2.j from t1 right join t2 using (i);",
   987  				Expected: []sql.Row{
   988  					{1, 10, 1, 30},
   989  					{2, 20, 2, 20},
   990  					{nil, nil, 5, 50},
   991  				},
   992  			},
   993  			{
   994  				Query: "select * from t1 right join t2 using (j);",
   995  				Expected: []sql.Row{
   996  					{30, 1, 3},
   997  					{20, 2, 2},
   998  					{50, 5, nil},
   999  				},
  1000  			},
  1001  			{
  1002  				Query: "select t1.i, t1.j, t2.i, t2.j from t1 right join t2 using (j);",
  1003  				Expected: []sql.Row{
  1004  					{3, 30, 1, 30},
  1005  					{2, 20, 2, 20},
  1006  					{nil, nil, 5, 50},
  1007  				},
  1008  			},
  1009  			{
  1010  				Query: "select * from t1 right join t2 using (i, j);",
  1011  				Expected: []sql.Row{
  1012  					{1, 30},
  1013  					{2, 20},
  1014  					{5, 50},
  1015  				},
  1016  			},
  1017  			{
  1018  				Query: "select * from t1 natural right join t2;",
  1019  				Expected: []sql.Row{
  1020  					{1, 30},
  1021  					{2, 20},
  1022  					{5, 50},
  1023  				},
  1024  			},
  1025  			{
  1026  				Query: "select t1.i, t1.j, t2.i, t2.j from t1 right join t2 using (i, j);",
  1027  				Expected: []sql.Row{
  1028  					{nil, nil, 1, 30},
  1029  					{2, 20, 2, 20},
  1030  					{nil, nil, 5, 50},
  1031  				},
  1032  			},
  1033  			{
  1034  				Query: "select t1.i, t1.j, t2.i, t2.j from t1 natural right join t2;",
  1035  				Expected: []sql.Row{
  1036  					{nil, nil, 1, 30},
  1037  					{2, 20, 2, 20},
  1038  					{nil, nil, 5, 50},
  1039  				},
  1040  			},
  1041  
  1042  			// Nested Join
  1043  			{
  1044  				Query: "select t1.i, t1.j, t2.i, t2.j, t3.i, t3.j from t1 join t2 using (i) join t3 on t1.i = t3.i;",
  1045  				Expected: []sql.Row{
  1046  					{1, 10, 1, 30, 1, 200},
  1047  					{2, 20, 2, 20, 2, 20},
  1048  				},
  1049  			},
  1050  			{
  1051  				Query:       "select t1.i, t1.j, t2.i, t2.j, t3.i, t3.j from t1 join t2 on t1.i = t2.i join t3 using (i);",
  1052  				ExpectedErr: sql.ErrAmbiguousColumnName,
  1053  			},
  1054  			{
  1055  				Query: "select t1.i, t1.j, t2.i, t2.j, t3.i, t3.j from t1 join t2 using (i) join t3 using (i);",
  1056  				Expected: []sql.Row{
  1057  					{1, 10, 1, 30, 1, 200},
  1058  					{2, 20, 2, 20, 2, 20},
  1059  				},
  1060  			},
  1061  			{
  1062  				Query: "select * from t1 join t2 using (i) join t3 using (i);",
  1063  				Expected: []sql.Row{
  1064  					{1, 10, 30, 200},
  1065  					{2, 20, 20, 20},
  1066  				},
  1067  			},
  1068  
  1069  			// Subquery Tests
  1070  			{
  1071  				Query: "select t1.i, t1.j, tt.i from t1 join (select 1 as i) tt using (i);",
  1072  				Expected: []sql.Row{
  1073  					{1, 10, 1},
  1074  				},
  1075  			},
  1076  			{
  1077  				Query: "select t1.i, t1.j, tt.i, tt.j from t1 join (select * from t2) tt using (i);",
  1078  				Expected: []sql.Row{
  1079  					{1, 10, 1, 30},
  1080  					{2, 20, 2, 20},
  1081  				},
  1082  			},
  1083  			{
  1084  				Query: "select tt1.i, tt1.j, tt2.i, tt2.j from (select * from t1) tt1 join (select * from t2) tt2 using (i);",
  1085  				Expected: []sql.Row{
  1086  					{1, 10, 1, 30},
  1087  					{2, 20, 2, 20},
  1088  				},
  1089  			},
  1090  
  1091  			// CTE Tests
  1092  			{
  1093  				Query: "with cte as (select * from t1) select cte.i, cte.j, t2.i, t2.j from cte join t2 using (i);",
  1094  				Expected: []sql.Row{
  1095  					{1, 10, 1, 30},
  1096  					{2, 20, 2, 20},
  1097  				},
  1098  			},
  1099  			{
  1100  				Query: "with cte1 as (select * from t1), cte2 as (select * from t2) select cte1.i, cte1.j, cte2.i, cte2.j from cte1 join cte2 using (i);",
  1101  				Expected: []sql.Row{
  1102  					{1, 10, 1, 30},
  1103  					{2, 20, 2, 20},
  1104  				},
  1105  			},
  1106  			{
  1107  				Query: "WITH cte(i, j) AS (SELECT 1, 1 UNION ALL SELECT i, j from t1) SELECT cte.i, cte.j, t2.i, t2.j from cte join t2 using (i);",
  1108  				Expected: []sql.Row{
  1109  					{1, 1, 1, 30},
  1110  					{1, 10, 1, 30},
  1111  					{2, 20, 2, 20},
  1112  				},
  1113  			},
  1114  			{
  1115  				Query: "with recursive cte(i, j) AS (select 1, 1 union all select i + 1, j * 10 from cte where i < 3) select cte.i, cte.j, t2.i, t2.j from cte join t2 using (i);",
  1116  				Expected: []sql.Row{
  1117  					{1, 1, 1, 30},
  1118  					{2, 10, 2, 20},
  1119  				},
  1120  			},
  1121  
  1122  			// Broken CTE tests
  1123  			{
  1124  				Skip:        true,
  1125  				Query:       "with cte as (select * from t1 join t2 using (i)) select * from cte;",
  1126  				ExpectedErr: sql.ErrDuplicateColumn,
  1127  			},
  1128  			{
  1129  				Skip:        true,
  1130  				Query:       "select * from (select t1.i, t1.j, t2.i, t2.j from t1 join t2 using (i)) tt;",
  1131  				ExpectedErr: sql.ErrDuplicateColumn,
  1132  			},
  1133  		},
  1134  	},
  1135  	{
  1136  		Name: "Join with truthy condition",
  1137  		SetUpScript: []string{
  1138  			"CREATE TABLE `a` (aa int);",
  1139  			"INSERT INTO `a` VALUES (1), (2);",
  1140  
  1141  			"CREATE TABLE `b` (bb int);",
  1142  			"INSERT INTO `b` VALUES (1), (2);",
  1143  		},
  1144  		Assertions: []ScriptTestAssertion{
  1145  			{
  1146  				Query: "SELECT * FROM a LEFT JOIN b ON 1;",
  1147  				Expected: []sql.Row{
  1148  					{1, 2},
  1149  					{1, 1},
  1150  					{2, 2},
  1151  					{2, 1},
  1152  				},
  1153  			},
  1154  			{
  1155  				Query: "SELECT * FROM a RIGHT JOIN b ON 8+9;",
  1156  				Expected: []sql.Row{
  1157  					{1, 2},
  1158  					{1, 1},
  1159  					{2, 2},
  1160  					{2, 1},
  1161  				},
  1162  			},
  1163  		},
  1164  	},
  1165  }
  1166  
  1167  var LateralJoinScriptTests = []ScriptTest{
  1168  	{
  1169  		Name: "basic lateral join test",
  1170  		SetUpScript: []string{
  1171  			"create table t (i int primary key)",
  1172  			"create table t1 (j int primary key)",
  1173  			"insert into t values (1), (2), (3)",
  1174  			"insert into t1 values (1), (4), (5)",
  1175  		},
  1176  		Assertions: []ScriptTestAssertion{
  1177  			// Lateral Cross Join
  1178  			{
  1179  				Query: "select * from t, lateral (select * from t1 where t.i = t1.j) as tt order by t.i, tt.j;",
  1180  				Expected: []sql.Row{
  1181  					{1, 1},
  1182  				},
  1183  			},
  1184  			{
  1185  				Query: "select * from t, lateral (select * from t1 where t.i != t1.j) as tt order by tt.j, t.i;",
  1186  				Expected: []sql.Row{
  1187  					{2, 1},
  1188  					{3, 1},
  1189  					{1, 4},
  1190  					{2, 4},
  1191  					{3, 4},
  1192  					{1, 5},
  1193  					{2, 5},
  1194  					{3, 5},
  1195  				},
  1196  			},
  1197  			{
  1198  				Query: "select * from t, t1, lateral (select * from t1 where t.i != t1.j) as tt where t.i > t1.j and t1.j = tt.j order by t.i, t1.j, tt.j;",
  1199  				Expected: []sql.Row{
  1200  					{2, 1, 1},
  1201  					{3, 1, 1},
  1202  				},
  1203  			},
  1204  			{
  1205  				Query: "select * from t, lateral (select * from t1 where t.i = t1.j) tt, lateral (select * from t1 where t.i != t1.j) as ttt order by t.i, tt.j, ttt.j;",
  1206  				Expected: []sql.Row{
  1207  					{1, 1, 4},
  1208  					{1, 1, 5},
  1209  				},
  1210  			},
  1211  			{
  1212  				Query: `WITH RECURSIVE cte(x) AS (SELECT 1 union all SELECT x + 1 from cte where x < 5) SELECT * FROM cte, lateral (select * from t where t.i = cte.x) tt;`,
  1213  				Expected: []sql.Row{
  1214  					{1, 1},
  1215  					{2, 2},
  1216  					{3, 3},
  1217  				},
  1218  			},
  1219  			{
  1220  				Query: "select * from (select * from t, lateral (select * from t1 where t.i = t1.j) as tt order by t.i, tt.j) ttt;",
  1221  				Expected: []sql.Row{
  1222  					{1, 1},
  1223  				},
  1224  			},
  1225  
  1226  			// Lateral Inner Join
  1227  			{
  1228  				Query: "select * from t inner join lateral (select * from t1 where t.i != t1.j) as tt on t.i > tt.j",
  1229  				Expected: []sql.Row{
  1230  					{2, 1},
  1231  					{3, 1},
  1232  				},
  1233  			},
  1234  			{
  1235  				Query: "select * from t inner join lateral (select * from t1 where t.i = t1.j) as tt on t.i = tt.j",
  1236  				Expected: []sql.Row{
  1237  					{1, 1},
  1238  				},
  1239  			},
  1240  			{
  1241  				Query:    "select * from t inner join lateral (select * from t1 where t.i = t1.j) as tt on t.i != tt.j",
  1242  				Expected: []sql.Row{},
  1243  			},
  1244  
  1245  			// Lateral Left Join
  1246  			{
  1247  				Query: "select * from t left join lateral (select * from t1 where t.i = t1.j) as tt on t.i = tt.j order by t.i, tt.j",
  1248  				Expected: []sql.Row{
  1249  					{1, 1},
  1250  					{2, nil},
  1251  					{3, nil},
  1252  				},
  1253  			},
  1254  			{
  1255  				Query: "select * from t left join lateral (select * from t1 where t.i != t1.j) as tt on t.i + 1 = tt.j or t.i + 2 = tt.j order by t.i, tt.j",
  1256  				Expected: []sql.Row{
  1257  					{1, nil},
  1258  					{2, 4},
  1259  					{3, 4},
  1260  					{3, 5},
  1261  				},
  1262  			},
  1263  
  1264  			// Lateral Right Join
  1265  			{
  1266  				Query:       "select * from t right join lateral (select * from t1 where t.i != t1.j) as tt on t.i > tt.j",
  1267  				ExpectedErr: sql.ErrTableNotFound,
  1268  			},
  1269  			{
  1270  				Query: "select * from t right join lateral (select * from t1) as tt on t.i > tt.j order by t.i, tt.j",
  1271  				Expected: []sql.Row{
  1272  					{nil, 4},
  1273  					{nil, 5},
  1274  					{2, 1},
  1275  					{3, 1},
  1276  				},
  1277  			},
  1278  		},
  1279  	},
  1280  	{
  1281  		Name: "multiple lateral joins with references to left tables",
  1282  		SetUpScript: []string{
  1283  			"create table students (id int primary key, name varchar(50), major int);",
  1284  			"create table classes (id int primary key, name varchar(50), department int);",
  1285  			"create table grades (grade float, student int, class int, primary key(class, student));",
  1286  			"create table majors (id int, name varchar(50), department int, primary key(name, department));",
  1287  			"create table departments (id int primary key, name varchar(50));",
  1288  			`insert into students values
  1289  					(1, 'Elle', 4), 
  1290  					(2, 'Latham', 2);`,
  1291  			`insert into classes values
  1292  					(1, 'Corporate Finance', 1),
  1293  					(2, 'ESG Studies', 1),
  1294  					(3, 'Late Bronze Age Collapse', 2),
  1295  					(4, 'Greek Mythology', 2);`,
  1296  			`insert into majors values
  1297  					(1, 'Roman Studies', 2),
  1298  					(2, 'Bronze Age Studies', 2),
  1299  					(3, 'Accounting', 1),
  1300  					(4, 'Finance', 1);`,
  1301  			`insert into departments values
  1302  					(1, 'Business'),
  1303  					(2, 'History');`,
  1304  			`insert into grades values 
  1305  					(94, 1, 1),
  1306  					(97, 1, 2),
  1307  					(85, 2, 3),
  1308  					(92, 2, 4);`,
  1309  		},
  1310  		Assertions: []ScriptTestAssertion{
  1311  			{
  1312  				Query: `
  1313  select name, class.class_name, grade.max_grade
  1314  from students,
  1315  LATERAL (
  1316  	select departments.id as did
  1317  	from majors
  1318  	join departments
  1319  	on majors.department = departments.id
  1320  	where majors.id = students.major
  1321  ) dept,
  1322  LATERAL (
  1323  	select
  1324  		grade as max_grade,
  1325  		classes.id as cid
  1326  	from grades
  1327  	join classes
  1328      on grades.class = classes.id
  1329  	where grades.student = students.id and classes.department = dept.did
  1330  	order by grade desc limit 1
  1331  ) grade,
  1332  LATERAL (
  1333  	select name as class_name from classes where grade.cid = classes.id
  1334  ) class
  1335  `,
  1336  				Expected: []sql.Row{
  1337  					{"Elle", "ESG Studies", 97.0},
  1338  					{"Latham", "Greek Mythology", 92.0},
  1339  				},
  1340  			},
  1341  		},
  1342  	},
  1343  	{
  1344  		Name: "lateral join with subquery",
  1345  		SetUpScript: []string{
  1346  			"create table xy (x int primary key, y int);",
  1347  			"create table uv (u int primary key, v int);",
  1348  			"insert into xy values (1, 0), (2, 1), (3, 2), (4, 3);",
  1349  			"insert into uv values (0, 0), (1, 1), (2, 2), (3, 3);",
  1350  		},
  1351  		Assertions: []ScriptTestAssertion{
  1352  			{
  1353  				Query: "select x, u from xy, lateral (select * from uv where y = u) uv;",
  1354  				Expected: []sql.Row{
  1355  					{1, 0},
  1356  					{2, 1},
  1357  					{3, 2},
  1358  					{4, 3},
  1359  				},
  1360  			},
  1361  		},
  1362  	},
  1363  }