github.com/dolthub/go-mysql-server@v0.18.0/enginetest/sqllogictest/testdata/join/join.txt (about)

     1  # Copyright 2023 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  # TEST ADAPTED FROM COCKROACHDB; HEAVILY MODIFIED TO WORK WITH MYSQL
    16  # SOURCE https://github.com/cockroachdb/cockroach/blob/7a2796a8d45e4a4031c1861747483e5390fbff6c/pkg/sql/logictest/testdata/logic_test/join
    17  
    18  # The join condition logic is tricky to get right with NULL
    19  # values. Simple implementations can deal well with NULLs on the first
    20  # or last row but fail to handle them in the middle. So the test table
    21  # must contain at least 3 rows with a null in the middle. This test
    22  # table also contains the pair 44/42 so that a test with a non-trivial
    23  # ON condition can be written.
    24  statement ok
    25  CREATE TABLE onecolumn (x INT)
    26  
    27  statement ok
    28  INSERT INTO onecolumn(x) VALUES (44), (NULL), (42)
    29  
    30  query II nosort
    31  SELECT a.x AS x, b.x AS y FROM onecolumn AS a CROSS JOIN onecolumn AS b ORDER BY x, y
    32  ----
    33  NULL
    34  NULL
    35  NULL
    36  42
    37  NULL
    38  44
    39  42
    40  NULL
    41  42
    42  42
    43  42
    44  44
    45  44
    46  NULL
    47  44
    48  42
    49  44
    50  44
    51  
    52  # Check that name resolution chokes on ambiguity when it needs to.
    53  statement error
    54  SELECT x FROM onecolumn AS a, onecolumn AS b
    55  
    56  query II nosort
    57  SELECT a.x AS x, b.x AS y FROM onecolumn AS a JOIN onecolumn AS b ON a.x = b.x
    58  ----
    59  44
    60  44
    61  42
    62  42
    63  
    64  query I nosort
    65  SELECT * FROM onecolumn AS a JOIN onecolumn as b USING(x) ORDER BY x
    66  ----
    67  42
    68  44
    69  
    70  query I nosort
    71  SELECT * FROM onecolumn AS a NATURAL JOIN onecolumn as b
    72  ----
    73  44
    74  42
    75  
    76  query II nosort
    77  SELECT a.x AS x, b.x AS y FROM onecolumn AS a LEFT OUTER JOIN onecolumn AS b ON a.x = b.x
    78  ----
    79  44
    80  44
    81  NULL
    82  NULL
    83  42
    84  42
    85  
    86  query I nosort
    87  SELECT * FROM onecolumn AS a LEFT OUTER JOIN onecolumn AS b USING(x) ORDER BY x
    88  ----
    89  NULL
    90  42
    91  44
    92  
    93  # Check that ORDER BY chokes on ambiguity if no table less columns
    94  # were introduced by USING. (#12239)
    95  statement error
    96  SELECT * FROM onecolumn AS a, onecolumn AS b ORDER BY x
    97  
    98  query I nosort
    99  SELECT * FROM onecolumn AS a NATURAL LEFT OUTER JOIN onecolumn AS b
   100  ----
   101  44
   102  NULL
   103  42
   104  
   105  query II nosort
   106  SELECT a.x AS x, b.x AS y FROM onecolumn AS a RIGHT OUTER JOIN onecolumn AS b ON a.x = b.x ORDER BY x, y
   107  ----
   108  NULL
   109  NULL
   110  42
   111  42
   112  44
   113  44
   114  
   115  query I nosort
   116  SELECT * FROM onecolumn AS a RIGHT OUTER JOIN onecolumn AS b USING(x) ORDER BY x
   117  ----
   118  NULL
   119  42
   120  44
   121  
   122  query I nosort
   123  SELECT * FROM onecolumn AS a NATURAL RIGHT OUTER JOIN onecolumn AS b ORDER BY x
   124  ----
   125  NULL
   126  42
   127  44
   128  
   129  statement ok
   130  CREATE TABLE onecolumn_w(w INT)
   131  
   132  statement ok
   133  INSERT INTO onecolumn_w(w) VALUES (42),(43)
   134  
   135  query II nosort
   136  SELECT * FROM onecolumn AS a NATURAL JOIN onecolumn_w as b ORDER BY x, w
   137  ----
   138  NULL
   139  42
   140  NULL
   141  43
   142  42
   143  42
   144  42
   145  43
   146  44
   147  42
   148  44
   149  43
   150  
   151  statement ok
   152  CREATE TABLE othercolumn (x INT)
   153  
   154  statement ok
   155  INSERT INTO othercolumn(x) VALUES (43),(42),(16)
   156  
   157  # Check that the source columns can be selected separately from the
   158  # USING column (#12033).
   159  # Check that a limit on the JOIN's result do not cause rows from the
   160  # JOIN operands to become invisible to the JOIN.
   161  query I nosort
   162  SELECT * FROM (SELECT x FROM onecolumn ORDER BY x DESC) sq NATURAL JOIN (SELECT column_0 as x from (VALUES ROW(42)) v) AS v LIMIT 1
   163  ----
   164  42
   165  
   166  statement ok
   167  CREATE TABLE `empty` (x INT)
   168  
   169  query II nosort
   170  SELECT a.x AS x, b.x as y FROM onecolumn AS a CROSS JOIN `empty` AS b
   171  ----
   172  
   173  query II nosort
   174  SELECT * FROM `empty` AS a CROSS JOIN onecolumn AS b
   175  ----
   176  
   177  query II nosort
   178  SELECT a.x AS x, b.x as y FROM onecolumn AS a JOIN `empty` AS b ON a.x = b.x
   179  ----
   180  
   181  query I nosort
   182  SELECT * FROM onecolumn AS a JOIN `empty` AS b USING(x)
   183  ----
   184  
   185  query II nosort
   186  SELECT a.x AS x, b.x AS y FROM `empty` AS a JOIN onecolumn AS b ON a.x = b.x
   187  ----
   188  
   189  query I nosort
   190  SELECT * FROM `empty` AS a JOIN onecolumn AS b USING(x)
   191  ----
   192  
   193  query II nosort
   194  SELECT a.x AS x, b.x AS y FROM onecolumn AS a LEFT OUTER JOIN `empty` AS b ON a.x = b.x ORDER BY a.x
   195  ----
   196  NULL
   197  NULL
   198  42
   199  NULL
   200  44
   201  NULL
   202  
   203  query I nosort
   204  SELECT * FROM onecolumn AS a LEFT OUTER JOIN `empty` AS b USING(x) ORDER BY x
   205  ----
   206  NULL
   207  42
   208  44
   209  
   210  query II nosort
   211  SELECT a.x AS x, b.x AS y FROM `empty` AS a LEFT OUTER JOIN onecolumn AS b ON a.x = b.x
   212  ----
   213  
   214  query I nosort
   215  SELECT * FROM `empty` AS a LEFT OUTER JOIN onecolumn AS b USING(x)
   216  ----
   217  
   218  query II nosort
   219  SELECT a.x AS x, b.x AS y FROM onecolumn AS a RIGHT OUTER JOIN `empty` AS b ON a.x = b.x
   220  ----
   221  
   222  query I nosort
   223  SELECT * FROM onecolumn AS a RIGHT OUTER JOIN `empty` AS b USING(x)
   224  ----
   225  
   226  statement ok
   227  CREATE TABLE twocolumn (x INT, y INT)
   228  
   229  statement ok
   230  INSERT INTO twocolumn(x, y) VALUES (44,51), (NULL,52), (42,53), (45,45)
   231  
   232  # Natural joins with partial match
   233  query II nosort
   234  SELECT * FROM onecolumn NATURAL JOIN twocolumn
   235  ----
   236  44
   237  51
   238  42
   239  53
   240  
   241  query IIII nosort
   242  SELECT * FROM twocolumn AS a JOIN twocolumn AS b ON a.x = a.y
   243  ----
   244  45
   245  45
   246  44
   247  51
   248  45
   249  45
   250  NULL
   251  52
   252  45
   253  45
   254  42
   255  53
   256  45
   257  45
   258  45
   259  45
   260  
   261  # Inner join with filter predicate
   262  query II nosort
   263  SELECT o.x, t.y FROM onecolumn o INNER JOIN twocolumn t ON (o.x=t.x AND t.y=53)
   264  ----
   265  42
   266  53
   267  
   268  # Outer joins with filter predicate
   269  query II nosort
   270  SELECT o.x, t.y FROM onecolumn o LEFT OUTER JOIN twocolumn t ON (o.x=t.x AND t.y=53)
   271  ----
   272  44
   273  NULL
   274  NULL
   275  NULL
   276  42
   277  53
   278  
   279  query II nosort
   280  SELECT o.x, t.y FROM onecolumn o LEFT OUTER JOIN twocolumn t ON (o.x=t.x AND o.x=44)
   281  ----
   282  44
   283  51
   284  NULL
   285  NULL
   286  42
   287  NULL
   288  
   289  query II nosort
   290  SELECT o.x, t.y FROM onecolumn o LEFT OUTER JOIN twocolumn t ON (o.x=t.x AND t.x=44)
   291  ----
   292  44
   293  51
   294  NULL
   295  NULL
   296  42
   297  NULL
   298  
   299  # Computed columns with NATURAL FULL JOIN.
   300  ## Simple test cases for inner, left, right, and outer joins
   301  
   302  statement ok
   303  CREATE TABLE a (i int)
   304  
   305  statement ok
   306  INSERT INTO a VALUES (1), (2), (3)
   307  
   308  statement ok
   309  CREATE TABLE b (i int, b bool)
   310  
   311  statement ok
   312  INSERT INTO b VALUES (2, true), (3, true), (4, false)
   313  
   314  query IIB nosort
   315  SELECT * FROM a INNER JOIN b ON a.i = b.i ORDER BY a.i, b.i, b.b
   316  ----
   317  2
   318  2
   319  1
   320  3
   321  3
   322  1
   323  
   324  query IIB nosort
   325  SELECT * FROM a LEFT OUTER JOIN b ON a.i = b.i ORDER BY a.i, b.i, b.b
   326  ----
   327  1
   328  NULL
   329  NULL
   330  2
   331  2
   332  1
   333  3
   334  3
   335  1
   336  
   337  query IIB nosort
   338  SELECT * FROM a RIGHT OUTER JOIN b ON a.i = b.i ORDER BY a.i, b.i, b.b
   339  ----
   340  NULL
   341  4
   342  0
   343  2
   344  2
   345  1
   346  3
   347  3
   348  1
   349  
   350  # Full outer join with filter predicate
   351  # Duplicate right matches for a single left row
   352  statement ok
   353  INSERT INTO b VALUES (3, false)
   354  
   355  query IIB nosort
   356  SELECT * FROM a RIGHT OUTER JOIN b ON a.i=b.i ORDER BY b.i, b.b
   357  ----
   358  2
   359  2
   360  1
   361  3
   362  3
   363  0
   364  3
   365  3
   366  1
   367  NULL
   368  4
   369  0
   370  
   371  
   372  # Check column orders and names.
   373  query IIIIII nosort
   374  SELECT * FROM (onecolumn CROSS JOIN twocolumn JOIN (SELECT a.x AS b FROM onecolumn AS a) a ON a.b=twocolumn.x JOIN (SELECT c.x as d, c.y as e FROM twocolumn AS c) c ON a.b=c.d AND c.d=onecolumn.x) ORDER BY 1 LIMIT 1
   375  ----
   376  42
   377  42
   378  53
   379  42
   380  42
   381  53
   382  
   383  # Check sub-queries in ON conditions.
   384  query III nosort
   385  SELECT * FROM onecolumn JOIN twocolumn ON twocolumn.x = onecolumn.x AND onecolumn.x IN (SELECT x FROM twocolumn WHERE y >= 52)
   386  ----
   387  42
   388  42
   389  53
   390  
   391  # Check sub-queries as data sources.
   392  query I nosort
   393  SELECT * FROM onecolumn JOIN (SELECT column_0 as x FROM (VALUES ROW(41), ROW(42), ROW(43)) a) AS a USING(x)
   394  ----
   395  42
   396  
   397  query I nosort
   398  SELECT * FROM onecolumn JOIN (SELECT x + 2 AS x FROM onecolumn) sq USING(x)
   399  ----
   400  44
   401  
   402  # Check that a single column can have multiple table aliases.
   403  query IIII nosort
   404  SELECT * FROM (twocolumn AS a JOIN twocolumn AS b USING(x) JOIN twocolumn AS c USING(x)) ORDER BY x LIMIT 1
   405  ----
   406  42
   407  53
   408  53
   409  53
   410  
   411  query IIIIII nosort
   412  SELECT a.x AS s, b.x, c.x, a.y, b.y, c.y FROM (twocolumn AS a JOIN twocolumn AS b USING(x) JOIN twocolumn AS c USING(x)) ORDER BY s
   413  ----
   414  42
   415  42
   416  42
   417  53
   418  53
   419  53
   420  44
   421  44
   422  44
   423  51
   424  51
   425  51
   426  45
   427  45
   428  45
   429  45
   430  45
   431  45
   432  
   433  statement error
   434  SELECT * FROM (onecolumn AS a JOIN onecolumn AS b USING(y))
   435  
   436  # This is valid in MySQL
   437  # statement error
   438  # SELECT * FROM (onecolumn AS a JOIN onecolumn AS b USING(x, x))
   439  
   440  statement ok
   441  CREATE TABLE othertype (x TEXT)
   442  
   443  # This is valid in MySQL
   444  # statement error
   445  # SELECT * FROM (onecolumn AS a JOIN othertype AS b USING(x))
   446  
   447  statement error
   448  SELECT * FROM (onecolumn JOIN onecolumn USING(x))
   449  
   450  statement error
   451  SELECT * FROM (onecolumn JOIN twocolumn USING(x) JOIN onecolumn USING(x))
   452  
   453  # Check that star expansion works across anonymous sources.
   454  query II nosort
   455  SELECT * FROM (SELECT * FROM onecolumn) sq1, (SELECT * FROM onecolumn) sq2 ORDER BY sq1.x, sq2.x
   456  ----
   457  NULL
   458  NULL
   459  NULL
   460  42
   461  NULL
   462  44
   463  42
   464  NULL
   465  42
   466  42
   467  42
   468  44
   469  44
   470  NULL
   471  44
   472  42
   473  44
   474  44
   475  
   476  # Check that anonymous sources are properly looked up without ambiguity.
   477  query I nosort
   478  SELECT x FROM (onecolumn JOIN othercolumn USING (x)) JOIN (onecolumn AS a JOIN othercolumn AS b USING(x)) USING(x)
   479  ----
   480  42
   481  
   482  # Check that multiple anonymous sources cause proper ambiguity errors.
   483  statement error
   484  SELECT x FROM (SELECT * FROM onecolumn), (SELECT * FROM onecolumn)
   485  
   486  statement error
   487  SELECT * FROM (onecolumn AS a JOIN onecolumn AS b ON x > 32)
   488  
   489  statement error
   490  SELECT * FROM (onecolumn AS a JOIN onecolumn AS b ON a.y > y)
   491  
   492  statement ok
   493  CREATE TABLE s(x INT)
   494  
   495  statement ok
   496  INSERT INTO s(x) VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10)
   497  
   498  # Ensure that large cross-joins are optimized somehow (#10633)
   499  statement ok
   500  CREATE TABLE customers(id INT PRIMARY KEY NOT NULL)
   501  
   502  statement ok
   503  CREATE TABLE orders(id INT, cust INT REFERENCES customers(id))
   504  
   505  # No way am I going to convert this
   506  # query TTTTTTTTIIITTI nosort
   507  # SELECT     NULL::text  AS pktable_cat,
   508  #        pkn.nspname AS pktable_schem,
   509  #        pkc.relname AS pktable_name,
   510  #        pka.attname AS pkcolumn_name,
   511  #        NULL::text  AS fktable_cat,
   512  #        fkn.nspname AS fktable_schem,
   513  #        fkc.relname AS fktable_name,
   514  #        fka.attname AS fkcolumn_name,
   515  #        pos.n       AS key_seq,
   516  #        CASE con.confupdtype
   517  #             WHEN 'c' THEN 0
   518  #             WHEN 'n' THEN 2
   519  #             WHEN 'd' THEN 4
   520  #             WHEN 'r' THEN 1
   521  #             WHEN 'a' THEN 3
   522  #             ELSE NULL
   523  #        END AS update_rule,
   524  #        CASE con.confdeltype
   525  #             WHEN 'c' THEN 0
   526  #             WHEN 'n' THEN 2
   527  #             WHEN 'd' THEN 4
   528  #             WHEN 'r' THEN 1
   529  #             WHEN 'a' THEN 3
   530  #             ELSE NULL
   531  #        END          AS delete_rule,
   532  #        con.conname  AS fk_name,
   533  #        pkic.relname AS pk_name,
   534  #        CASE
   535  #             WHEN con.condeferrable
   536  #             AND      con.condeferred THEN 5
   537  #             WHEN con.condeferrable THEN 6
   538  #             ELSE 7
   539  #        END AS deferrability
   540  #   FROM     pg_catalog.pg_namespace pkn,
   541  #        pg_catalog.pg_class pkc,
   542  #        pg_catalog.pg_attribute pka,
   543  #        pg_catalog.pg_namespace fkn,
   544  #        pg_catalog.pg_class fkc,
   545  #        pg_catalog.pg_attribute fka,
   546  #        pg_catalog.pg_constraint con,
   547  #        pg_catalog.generate_series(1, 32) pos(n),
   548  #        pg_catalog.pg_depend dep,
   549  #        pg_catalog.pg_class pkic
   550  #   WHERE    pkn.oid = pkc.relnamespace
   551  #   AND      pkc.oid = pka.attrelid
   552  #   AND      pka.attnum = con.confkey[pos.n]
   553  #   AND      con.confrelid = pkc.oid
   554  #   AND      fkn.oid = fkc.relnamespace
   555  #   AND      fkc.oid = fka.attrelid
   556  #   AND      fka.attnum = con.conkey[pos.n]
   557  #   AND      con.conrelid = fkc.oid
   558  #   AND      con.contype = 'f'
   559  #   AND      con.oid = dep.objid
   560  #   AND      pkic.oid = dep.refobjid
   561  #   AND      pkic.relkind = 'i'
   562  #   AND      fkn.nspname = 'public'
   563  #   AND      fkc.relname = 'orders'
   564  #   ORDER BY pkn.nspname,
   565  #        pkc.relname,
   566  #        con.conname,
   567  #        pos.n
   568  # ----
   569  # NULL
   570  # public
   571  # customers
   572  # id
   573  # NULL
   574  # public
   575  # orders
   576  # cust
   577  # 1
   578  # 3
   579  # 3
   580  # orders_cust_fkey
   581  # customers_pkey
   582  # 7
   583  
   584  
   585  # Tests for filter propagation through joins.
   586  
   587  statement ok
   588  CREATE TABLE square (n INT PRIMARY KEY, sq INT)
   589  
   590  statement ok
   591  INSERT INTO square VALUES (1,1), (2,4), (3,9), (4,16), (5,25), (6,36)
   592  
   593  statement ok
   594  CREATE TABLE pairs (a INT, b INT)
   595  
   596  statement ok
   597  INSERT INTO pairs VALUES (1,1), (1,2), (1,3), (1,4), (1,5), (1,6), (2,3), (2,4), (2,5), (2,6), (3,4), (3,5), (3,6), (4,5), (4,6)
   598  
   599  query IIII nosort
   600  SELECT * FROM pairs, square WHERE pairs.b = square.n
   601  ----
   602  1
   603  1
   604  1
   605  1
   606  1
   607  2
   608  2
   609  4
   610  1
   611  3
   612  3
   613  9
   614  1
   615  4
   616  4
   617  16
   618  1
   619  5
   620  5
   621  25
   622  1
   623  6
   624  6
   625  36
   626  2
   627  3
   628  3
   629  9
   630  2
   631  4
   632  4
   633  16
   634  2
   635  5
   636  5
   637  25
   638  2
   639  6
   640  6
   641  36
   642  3
   643  4
   644  4
   645  16
   646  3
   647  5
   648  5
   649  25
   650  3
   651  6
   652  6
   653  36
   654  4
   655  5
   656  5
   657  25
   658  4
   659  6
   660  6
   661  36
   662  
   663  query IIII nosort
   664  SELECT * FROM pairs, square WHERE pairs.a + pairs.b = square.sq
   665  ----
   666  1
   667  3
   668  2
   669  4
   670  3
   671  6
   672  3
   673  9
   674  4
   675  5
   676  3
   677  9
   678  
   679  query IIII nosort
   680  SELECT a, b, n, sq FROM (SELECT a, b, a * b / 2 AS `div`, n, sq FROM pairs, square) sqa WHERE `div` = sq ORDER BY a, b, n, sq
   681  ----
   682  1
   683  2
   684  1
   685  1
   686  2
   687  4
   688  2
   689  4
   690  3
   691  6
   692  3
   693  9
   694  
   695  # Filter propagation through outer joins.
   696  
   697  query IIII nosort
   698  SELECT *  FROM (SELECT * FROM pairs LEFT JOIN square ON b = sq AND a > 1 AND n < 6) sqa WHERE b > 1 AND (n IS NULL OR n > 1) AND (n IS NULL OR a  < sq)
   699  ----
   700  1
   701  2
   702  NULL
   703  NULL
   704  1
   705  3
   706  NULL
   707  NULL
   708  1
   709  4
   710  NULL
   711  NULL
   712  1
   713  5
   714  NULL
   715  NULL
   716  1
   717  6
   718  NULL
   719  NULL
   720  2
   721  3
   722  NULL
   723  NULL
   724  2
   725  4
   726  2
   727  4
   728  2
   729  5
   730  NULL
   731  NULL
   732  2
   733  6
   734  NULL
   735  NULL
   736  3
   737  4
   738  2
   739  4
   740  3
   741  5
   742  NULL
   743  NULL
   744  3
   745  6
   746  NULL
   747  NULL
   748  4
   749  5
   750  NULL
   751  NULL
   752  4
   753  6
   754  NULL
   755  NULL
   756  
   757  query IIII nosort
   758  SELECT *  FROM (SELECT * FROM pairs RIGHT JOIN square ON b = sq AND a > 1 AND n < 6) sqa WHERE (a IS NULL OR a > 2) AND n > 1 AND (a IS NULL OR a < sq)
   759  ----
   760  3
   761  4
   762  2
   763  4
   764  NULL
   765  NULL
   766  3
   767  9
   768  NULL
   769  NULL
   770  4
   771  16
   772  NULL
   773  NULL
   774  5
   775  25
   776  NULL
   777  NULL
   778  6
   779  36
   780  
   781  
   782  statement ok
   783  CREATE TABLE t1 (col1 INT, x INT, col2 INT, y INT)
   784  
   785  statement ok
   786  CREATE TABLE t2 (col3 INT, y INT, x INT, col4 INT)
   787  
   788  statement ok
   789  INSERT INTO t1 VALUES (10, 1, 11, 1), (20, 2, 21, 1), (30, 3, 31, 1)
   790  
   791  statement ok
   792  INSERT INTO t2 VALUES (100, 1, 1, 101), (200, 1, 201, 2), (400, 1, 401, 4)
   793  
   794  query IIIIIII nosort
   795  SELECT * FROM t1 JOIN t2 USING(x)
   796  ----
   797  1
   798  10
   799  11
   800  1
   801  100
   802  1
   803  101
   804  
   805  query IIIIII nosort
   806  SELECT * FROM t1 NATURAL JOIN t2
   807  ----
   808  1
   809  1
   810  10
   811  11
   812  100
   813  101
   814  
   815  query IIIIIIII nosort
   816  SELECT * FROM t1 JOIN t2 ON t2.x=t1.x
   817  ----
   818  10
   819  1
   820  11
   821  1
   822  100
   823  1
   824  1
   825  101
   826  
   827  query III nosort
   828  SELECT t2.x, t1.x, x FROM t1 JOIN t2 USING(x)
   829  ----
   830  1
   831  1
   832  1
   833  
   834  # Test for #19536.
   835  query I nosort
   836  SELECT x FROM t1 NATURAL JOIN (SELECT * FROM t2) sqa
   837  ----
   838  1
   839  
   840  # Tests for merge join ordering information.
   841  statement ok
   842  CREATE TABLE pkBA (a INT, b INT, c INT, d INT, PRIMARY KEY(b,a))
   843  
   844  statement ok
   845  CREATE TABLE pkBC (a INT, b INT, c INT, d INT, PRIMARY KEY(b,c))
   846  
   847  statement ok
   848  CREATE TABLE pkBAC (a INT, b INT, c INT, d INT, PRIMARY KEY(b,a,c))
   849  
   850  statement ok
   851  CREATE TABLE pkBAD (a INT, b INT, c INT, d INT, PRIMARY KEY(b,a,d))
   852  
   853  # Tests with joins with merged columns of collated string type.
   854  statement ok
   855  CREATE TABLE str1 (a INT PRIMARY KEY, s TEXT COLLATE utf8mb4_0900_ai_ci)
   856  
   857  statement ok
   858  INSERT INTO str1 VALUES (1, 'a' COLLATE utf8mb4_0900_ai_ci), (2, 'A' COLLATE utf8mb4_0900_ai_ci), (3, 'c' COLLATE utf8mb4_0900_ai_ci), (4, 'D' COLLATE utf8mb4_0900_ai_ci)
   859  
   860  statement ok
   861  CREATE TABLE str2 (a INT PRIMARY KEY, s TEXT COLLATE utf8mb4_0900_ai_ci)
   862  
   863  statement ok
   864  INSERT INTO str2 VALUES (1, 'A' COLLATE utf8mb4_0900_ai_ci), (2, 'B' COLLATE utf8mb4_0900_ai_ci), (3, 'C' COLLATE utf8mb4_0900_ai_ci), (4, 'E' COLLATE utf8mb4_0900_ai_ci)
   865  
   866  query TTT nosort
   867  SELECT s, str1.s, str2.s FROM str1 INNER JOIN str2 USING(s)
   868  ----
   869  A
   870  A
   871  A
   872  a
   873  a
   874  A
   875  c
   876  c
   877  C
   878  
   879  query TTT nosort
   880  SELECT s, str1.s, str2.s FROM str1 LEFT OUTER JOIN str2 USING(s)
   881  ----
   882  a
   883  a
   884  A
   885  A
   886  A
   887  A
   888  c
   889  c
   890  C
   891  D
   892  D
   893  NULL
   894  
   895  query TTT nosort
   896  SELECT s, str1.s, str2.s FROM str1 RIGHT OUTER JOIN str2 USING(s)
   897  ----
   898  A
   899  A
   900  A
   901  A
   902  a
   903  A
   904  B
   905  NULL
   906  B
   907  C
   908  c
   909  C
   910  E
   911  NULL
   912  E
   913  
   914  
   915  statement ok
   916  CREATE TABLE xyu (x INT, y INT, u INT, PRIMARY KEY(x,y,u))
   917  
   918  statement ok
   919  INSERT INTO xyu VALUES (0, 0, 0), (1, 1, 1), (3, 1, 31), (3, 2, 32), (4, 4, 44)
   920  
   921  statement ok
   922  CREATE TABLE xyv (x INT, y INT, v INT, PRIMARY KEY(x,y,v))
   923  
   924  statement ok
   925  INSERT INTO xyv VALUES (1, 1, 1), (2, 2, 2), (3, 1, 31), (3, 3, 33), (5, 5, 55)
   926  
   927  query IIII nosort
   928  SELECT * FROM xyu INNER JOIN xyv USING(x, y) WHERE x > 2
   929  ----
   930  3
   931  1
   932  31
   933  31
   934  
   935  query IIII nosort
   936  SELECT * FROM xyu LEFT OUTER JOIN xyv USING(x, y) WHERE x > 2
   937  ----
   938  3
   939  1
   940  31
   941  31
   942  3
   943  2
   944  32
   945  NULL
   946  4
   947  4
   948  44
   949  NULL
   950  
   951  query IIII nosort
   952  SELECT x, y, u, v FROM xyu RIGHT OUTER JOIN xyv USING(x, y) WHERE x > 2 ORDER BY x, y, u, v
   953  ----
   954  3
   955  1
   956  31
   957  31
   958  3
   959  3
   960  NULL
   961  33
   962  5
   963  5
   964  NULL
   965  55
   966  
   967  query IIIIII nosort
   968  SELECT * FROM xyu INNER JOIN xyv ON xyu.x = xyv.x AND xyu.y = xyv.y WHERE xyu.x = 1 AND xyu.y < 10
   969  ----
   970  1
   971  1
   972  1
   973  1
   974  1
   975  1
   976  
   977  query IIIIII nosort
   978  SELECT * FROM xyu INNER JOIN xyv ON xyu.x = xyv.x AND xyu.y = xyv.y AND xyu.x = 1 AND xyu.y < 10
   979  ----
   980  1
   981  1
   982  1
   983  1
   984  1
   985  1
   986  
   987  query IIIIII nosort
   988  SELECT * FROM xyu LEFT OUTER JOIN xyv ON xyu.x = xyv.x AND xyu.y = xyv.y AND xyu.x = 1 AND xyu.y < 10
   989  ----
   990  0
   991  0
   992  0
   993  NULL
   994  NULL
   995  NULL
   996  1
   997  1
   998  1
   999  1
  1000  1
  1001  1
  1002  3
  1003  1
  1004  31
  1005  NULL
  1006  NULL
  1007  NULL
  1008  3
  1009  2
  1010  32
  1011  NULL
  1012  NULL
  1013  NULL
  1014  4
  1015  4
  1016  44
  1017  NULL
  1018  NULL
  1019  NULL
  1020  
  1021  query IIIIII nosort
  1022  SELECT xyv.x, xyv.y, xyv.v, xyu.x, xyu.y, xyu.u FROM xyu RIGHT OUTER JOIN xyv ON xyu.x = xyv.x AND xyu.y = xyv.y AND xyu.x = 1 AND xyu.y < 10 ORDER BY xyv.x, xyv.y, xyv.v, xyu.x, xyu.y, xyu.u
  1023  ----
  1024  1
  1025  1
  1026  1
  1027  1
  1028  1
  1029  1
  1030  2
  1031  2
  1032  2
  1033  NULL
  1034  NULL
  1035  NULL
  1036  3
  1037  1
  1038  31
  1039  NULL
  1040  NULL
  1041  NULL
  1042  3
  1043  3
  1044  33
  1045  NULL
  1046  NULL
  1047  NULL
  1048  5
  1049  5
  1050  55
  1051  NULL
  1052  NULL
  1053  NULL
  1054  
  1055  
  1056  # Test OUTER joins that are run in the distSQL merge joiner
  1057  
  1058  query IIII nosort
  1059  SELECT * FROM (SELECT * FROM xyu ORDER BY x, y) AS xyu LEFT OUTER JOIN (SELECT * FROM xyv ORDER BY x, y) AS xyv USING(x, y) WHERE x > 2
  1060  ----
  1061  3
  1062  1
  1063  31
  1064  31
  1065  3
  1066  2
  1067  32
  1068  NULL
  1069  4
  1070  4
  1071  44
  1072  NULL
  1073  
  1074  query IIII nosort
  1075  SELECT x, y, v, u FROM (SELECT * FROM xyu ORDER BY x, y) AS xyu RIGHT OUTER JOIN (SELECT * FROM xyv ORDER BY x, y) AS xyv USING(x, y) WHERE x > 2
  1076  ----
  1077  3
  1078  1
  1079  31
  1080  31
  1081  3
  1082  3
  1083  33
  1084  NULL
  1085  5
  1086  5
  1087  55
  1088  NULL
  1089  
  1090  query IIIIII nosort
  1091  SELECT * FROM (SELECT * FROM xyu ORDER BY x, y) AS xyu LEFT OUTER JOIN (SELECT * FROM xyv ORDER BY x, y) AS xyv ON xyu.x = xyv.x AND xyu.y = xyv.y AND xyu.x = 1 AND xyu.y < 10
  1092  ----
  1093  0
  1094  0
  1095  0
  1096  NULL
  1097  NULL
  1098  NULL
  1099  1
  1100  1
  1101  1
  1102  1
  1103  1
  1104  1
  1105  3
  1106  1
  1107  31
  1108  NULL
  1109  NULL
  1110  NULL
  1111  3
  1112  2
  1113  32
  1114  NULL
  1115  NULL
  1116  NULL
  1117  4
  1118  4
  1119  44
  1120  NULL
  1121  NULL
  1122  NULL
  1123  
  1124  query IIIIII nosort
  1125  SELECT * FROM xyu RIGHT OUTER JOIN (SELECT * FROM xyv ORDER BY x, y) AS xyv ON xyu.x = xyv.x AND xyu.y = xyv.y AND xyu.x = 1 AND xyu.y < 10
  1126  ----
  1127  1
  1128  1
  1129  1
  1130  1
  1131  1
  1132  1
  1133  NULL
  1134  NULL
  1135  NULL
  1136  2
  1137  2
  1138  2
  1139  NULL
  1140  NULL
  1141  NULL
  1142  3
  1143  1
  1144  31
  1145  NULL
  1146  NULL
  1147  NULL
  1148  3
  1149  3
  1150  33
  1151  NULL
  1152  NULL
  1153  NULL
  1154  5
  1155  5
  1156  55
  1157  
  1158  
  1159  # Regression test for #20858.
  1160  
  1161  statement ok
  1162  CREATE TABLE l (a INT PRIMARY KEY, b1 INT)
  1163  
  1164  statement ok
  1165  CREATE TABLE r (a INT PRIMARY KEY, b2 INT)
  1166  
  1167  statement ok
  1168  INSERT INTO l VALUES (1, 1), (2, 1), (3, 1)
  1169  
  1170  statement ok
  1171  INSERT INTO r VALUES (2, 1), (3, 1), (4, 1)
  1172  
  1173  query III nosort
  1174  SELECT * FROM l LEFT OUTER JOIN r USING(a) WHERE a = 1
  1175  ----
  1176  1
  1177  1
  1178  NULL
  1179  
  1180  query III nosort
  1181  SELECT * FROM l LEFT OUTER JOIN r USING(a) WHERE a = 2
  1182  ----
  1183  2
  1184  1
  1185  1
  1186  
  1187  query III nosort
  1188  SELECT * FROM l RIGHT OUTER JOIN r USING(a) WHERE a = 3
  1189  ----
  1190  3
  1191  1
  1192  1
  1193  
  1194  query III nosort
  1195  SELECT a, b1, b2 FROM l RIGHT OUTER JOIN r USING(a) WHERE a = 4
  1196  ----
  1197  4
  1198  NULL
  1199  1
  1200  
  1201  
  1202  # Regression tests for mixed-type equality columns (#22514).
  1203  statement ok
  1204  CREATE TABLE foo (  a INT,  b INT,  c FLOAT,  d FLOAT)
  1205  
  1206  statement ok
  1207  INSERT INTO foo VALUES  (1, 1, 1, 1),  (2, 2, 2, 2),  (3, 3, 3, 3)
  1208  
  1209  statement ok
  1210  CREATE TABLE bar (  a INT,  b FLOAT,  c FLOAT,  d INT)
  1211  
  1212  statement ok
  1213  INSERT INTO bar VALUES  (1, 1, 1, 1),  (2, 2, 2, 2),  (3, 3, 3, 3)
  1214  
  1215  query IIRR nosort
  1216  SELECT * FROM foo NATURAL JOIN bar
  1217  ----
  1218  1
  1219  1
  1220  1
  1221  1
  1222  2
  1223  2
  1224  2
  1225  2
  1226  3
  1227  3
  1228  3
  1229  3
  1230  
  1231  query IIRRIRI nosort
  1232  SELECT * FROM foo JOIN bar USING (b)
  1233  ----
  1234  1
  1235  1
  1236  1
  1237  1
  1238  1
  1239  1
  1240  1
  1241  2
  1242  2
  1243  2
  1244  2
  1245  2
  1246  2
  1247  2
  1248  3
  1249  3
  1250  3
  1251  3
  1252  3
  1253  3
  1254  3
  1255  
  1256  query IIRRRI nosort
  1257  SELECT * FROM foo JOIN bar USING (a, b)
  1258  ----
  1259  1
  1260  1
  1261  1
  1262  1
  1263  1
  1264  1
  1265  2
  1266  2
  1267  2
  1268  2
  1269  2
  1270  2
  1271  3
  1272  3
  1273  3
  1274  3
  1275  3
  1276  3
  1277  
  1278  query IIRRI nosort
  1279  SELECT * FROM foo JOIN bar USING (a, b, c)
  1280  ----
  1281  1
  1282  1
  1283  1
  1284  1
  1285  1
  1286  2
  1287  2
  1288  2
  1289  2
  1290  2
  1291  3
  1292  3
  1293  3
  1294  3
  1295  3
  1296  
  1297  query IIRRIRRI nosort
  1298  SELECT * FROM foo JOIN bar ON foo.b = bar.b
  1299  ----
  1300  1
  1301  1
  1302  1
  1303  1
  1304  1
  1305  1
  1306  1
  1307  1
  1308  2
  1309  2
  1310  2
  1311  2
  1312  2
  1313  2
  1314  2
  1315  2
  1316  3
  1317  3
  1318  3
  1319  3
  1320  3
  1321  3
  1322  3
  1323  3
  1324  
  1325  query IIRRIRRI nosort
  1326  SELECT * FROM foo JOIN bar ON foo.a = bar.a AND foo.b = bar.b
  1327  ----
  1328  1
  1329  1
  1330  1
  1331  1
  1332  1
  1333  1
  1334  1
  1335  1
  1336  2
  1337  2
  1338  2
  1339  2
  1340  2
  1341  2
  1342  2
  1343  2
  1344  3
  1345  3
  1346  3
  1347  3
  1348  3
  1349  3
  1350  3
  1351  3
  1352  
  1353  query IIRRIRRI nosort
  1354  SELECT * FROM foo, bar WHERE foo.b = bar.b
  1355  ----
  1356  1
  1357  1
  1358  1
  1359  1
  1360  1
  1361  1
  1362  1
  1363  1
  1364  2
  1365  2
  1366  2
  1367  2
  1368  2
  1369  2
  1370  2
  1371  2
  1372  3
  1373  3
  1374  3
  1375  3
  1376  3
  1377  3
  1378  3
  1379  3
  1380  
  1381  query IIRRIRRI nosort
  1382  SELECT * FROM foo, bar WHERE foo.a = bar.a AND foo.b = bar.b
  1383  ----
  1384  1
  1385  1
  1386  1
  1387  1
  1388  1
  1389  1
  1390  1
  1391  1
  1392  2
  1393  2
  1394  2
  1395  2
  1396  2
  1397  2
  1398  2
  1399  2
  1400  3
  1401  3
  1402  3
  1403  3
  1404  3
  1405  3
  1406  3
  1407  3
  1408  
  1409  query IIRRRI nosort
  1410  SELECT * FROM foo JOIN bar USING (a, b) WHERE foo.c = bar.c AND foo.d = bar.d
  1411  ----
  1412  1
  1413  1
  1414  1
  1415  1
  1416  1
  1417  1
  1418  2
  1419  2
  1420  2
  1421  2
  1422  2
  1423  2
  1424  3
  1425  3
  1426  3
  1427  3
  1428  3
  1429  3
  1430  
  1431  # Regression test for 23664.
  1432  query III nosort
  1433  SELECT * FROM onecolumn AS a RIGHT JOIN twocolumn ON false
  1434  ----
  1435  NULL
  1436  44
  1437  51
  1438  NULL
  1439  NULL
  1440  52
  1441  NULL
  1442  42
  1443  53
  1444  NULL
  1445  45
  1446  45
  1447  
  1448  # Regression test for #23609: make sure that the type of the merged column
  1449  # is int (not unknown).
  1450  # Regression test for #28817. Do not allow special functions in ON clause.
  1451  statement error
  1452  SELECT * FROM foo JOIN bar ON generate_series(0, 1) < 2
  1453  
  1454  statement error
  1455  SELECT * FROM foo JOIN bar ON max(foo.c) < 2
  1456  
  1457  # Regression test for #44029 (outer join on two single-row clauses, with two
  1458  # results).
  1459  # Regression test for #44746 (internal error for particular condition).
  1460  statement ok
  1461  CREATE TABLE t44746_0(c0 INT)
  1462  
  1463  statement ok
  1464  CREATE TABLE t44746_1(c1 INT)
  1465  
  1466  # Note: an "error parsing regexp" would also be acceptable here.
  1467  statement ok
  1468  SELECT * FROM t44746_0 FULL JOIN t44746_1 ON (SUBSTRING('', ')') = '') = (c1 > 0)
  1469  
  1470  # Regression test for #49630.
  1471  statement ok
  1472  DROP TABLE `empty`;
  1473  
  1474  statement ok
  1475  CREATE TABLE xy (x INT PRIMARY KEY, y INT);
  1476  
  1477  statement ok
  1478  CREATE TABLE fk_ref (r INT NOT NULL REFERENCES xy (x));
  1479  
  1480  statement ok
  1481  CREATE TABLE `empty` (v INT);
  1482  
  1483  statement ok
  1484  INSERT INTO xy (VALUES ROW(1, 1));
  1485  
  1486  statement ok
  1487  INSERT INTO fk_ref (VALUES ROW(1));
  1488  
  1489  query IIII nosort
  1490  SELECT * FROM fk_ref LEFT JOIN (SELECT * FROM xy INNER JOIN `empty` ON True) sqa ON r = x
  1491  ----
  1492  1
  1493  NULL
  1494  NULL
  1495  NULL
  1496  
  1497  statement ok
  1498  DROP TABLE `empty`;
  1499  
  1500  statement ok
  1501  DROP TABLE fk_ref;
  1502  
  1503  statement ok
  1504  DROP TABLE xy;
  1505  
  1506  statement ok
  1507  CREATE TABLE abcd (a INT, b INT, c INT, d INT)
  1508  
  1509  statement ok
  1510  INSERT INTO abcd VALUES (1, 1, 1, 1), (2, 2, 2, 2)
  1511  
  1512  statement ok
  1513  CREATE TABLE dxby (d INT, x INT, b INT, y INT)
  1514  
  1515  statement ok
  1516  INSERT INTO dxby VALUES (2, 2, 2, 2), (3, 3, 3, 3)
  1517  
  1518  # Test that qualified stars expand to all table columns (even those that aren't
  1519  # directly visible); see #66123.
  1520  query IIIIIIII nosort
  1521  SELECT abcd.*, dxby.* FROM abcd INNER JOIN dxby USING (d, b)
  1522  ----
  1523  2
  1524  2
  1525  2
  1526  2
  1527  2
  1528  2
  1529  2
  1530  2
  1531