github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/opt/optbuilder/testdata/union (about)

     1  # tests adapted from logictest -- union
     2  
     3  build
     4  VALUES (1), (1), (1), (2), (2) UNION VALUES (1), (3), (1)
     5  ----
     6  union
     7   ├── columns: column1:3!null
     8   ├── left columns: column1:1
     9   ├── right columns: column1:2
    10   ├── values
    11   │    ├── columns: column1:1!null
    12   │    ├── (1,)
    13   │    ├── (1,)
    14   │    ├── (1,)
    15   │    ├── (2,)
    16   │    └── (2,)
    17   └── values
    18        ├── columns: column1:2!null
    19        ├── (1,)
    20        ├── (3,)
    21        └── (1,)
    22  
    23  build
    24  VALUES (1), (1), (1), (2), (2) UNION ALL VALUES (1), (3), (1)
    25  ----
    26  union-all
    27   ├── columns: column1:3!null
    28   ├── left columns: column1:1
    29   ├── right columns: column1:2
    30   ├── values
    31   │    ├── columns: column1:1!null
    32   │    ├── (1,)
    33   │    ├── (1,)
    34   │    ├── (1,)
    35   │    ├── (2,)
    36   │    └── (2,)
    37   └── values
    38        ├── columns: column1:2!null
    39        ├── (1,)
    40        ├── (3,)
    41        └── (1,)
    42  
    43  build
    44  VALUES (1), (1), (1), (2), (2) INTERSECT VALUES (1), (3), (1)
    45  ----
    46  intersect
    47   ├── columns: column1:1!null
    48   ├── left columns: column1:1!null
    49   ├── right columns: column1:2
    50   ├── values
    51   │    ├── columns: column1:1!null
    52   │    ├── (1,)
    53   │    ├── (1,)
    54   │    ├── (1,)
    55   │    ├── (2,)
    56   │    └── (2,)
    57   └── values
    58        ├── columns: column1:2!null
    59        ├── (1,)
    60        ├── (3,)
    61        └── (1,)
    62  
    63  build
    64  VALUES (1), (1), (1), (2), (2) INTERSECT ALL VALUES (1), (3), (1)
    65  ----
    66  intersect-all
    67   ├── columns: column1:1!null
    68   ├── left columns: column1:1!null
    69   ├── right columns: column1:2
    70   ├── values
    71   │    ├── columns: column1:1!null
    72   │    ├── (1,)
    73   │    ├── (1,)
    74   │    ├── (1,)
    75   │    ├── (2,)
    76   │    └── (2,)
    77   └── values
    78        ├── columns: column1:2!null
    79        ├── (1,)
    80        ├── (3,)
    81        └── (1,)
    82  
    83  build
    84  VALUES (1), (1), (1), (2), (2) EXCEPT VALUES (1), (3), (1)
    85  ----
    86  except
    87   ├── columns: column1:1!null
    88   ├── left columns: column1:1!null
    89   ├── right columns: column1:2
    90   ├── values
    91   │    ├── columns: column1:1!null
    92   │    ├── (1,)
    93   │    ├── (1,)
    94   │    ├── (1,)
    95   │    ├── (2,)
    96   │    └── (2,)
    97   └── values
    98        ├── columns: column1:2!null
    99        ├── (1,)
   100        ├── (3,)
   101        └── (1,)
   102  
   103  build
   104  VALUES (1), (1), (1), (2), (2) EXCEPT ALL VALUES (1), (3), (1)
   105  ----
   106  except-all
   107   ├── columns: column1:1!null
   108   ├── left columns: column1:1!null
   109   ├── right columns: column1:2
   110   ├── values
   111   │    ├── columns: column1:1!null
   112   │    ├── (1,)
   113   │    ├── (1,)
   114   │    ├── (1,)
   115   │    ├── (2,)
   116   │    └── (2,)
   117   └── values
   118        ├── columns: column1:2!null
   119        ├── (1,)
   120        ├── (3,)
   121        └── (1,)
   122  
   123  build
   124  VALUES (1, 2), (1, 1), (1, 2), (2, 1), (2, 1) UNION VALUES (1, 3), (3, 4), (1, 1)
   125  ----
   126  union
   127   ├── columns: column1:5!null column2:6!null
   128   ├── left columns: column1:1 column2:2
   129   ├── right columns: column1:3 column2:4
   130   ├── values
   131   │    ├── columns: column1:1!null column2:2!null
   132   │    ├── (1, 2)
   133   │    ├── (1, 1)
   134   │    ├── (1, 2)
   135   │    ├── (2, 1)
   136   │    └── (2, 1)
   137   └── values
   138        ├── columns: column1:3!null column2:4!null
   139        ├── (1, 3)
   140        ├── (3, 4)
   141        └── (1, 1)
   142  
   143  build
   144  (VALUES (1), (1), (1), (2), (2) UNION ALL VALUES (1), (3), (1)) ORDER BY 1 DESC LIMIT 2
   145  ----
   146  limit
   147   ├── columns: column1:3!null
   148   ├── internal-ordering: -3
   149   ├── ordering: -3
   150   ├── sort
   151   │    ├── columns: column1:3!null
   152   │    ├── ordering: -3
   153   │    ├── limit hint: 2.00
   154   │    └── union-all
   155   │         ├── columns: column1:3!null
   156   │         ├── left columns: column1:1
   157   │         ├── right columns: column1:2
   158   │         ├── values
   159   │         │    ├── columns: column1:1!null
   160   │         │    ├── (1,)
   161   │         │    ├── (1,)
   162   │         │    ├── (1,)
   163   │         │    ├── (2,)
   164   │         │    └── (2,)
   165   │         └── values
   166   │              ├── columns: column1:2!null
   167   │              ├── (1,)
   168   │              ├── (3,)
   169   │              └── (1,)
   170   └── 2
   171  
   172  # The ORDER BY and LIMIT apply to the UNION, not the last VALUES.
   173  build
   174  VALUES (1), (1), (1), (2), (2) UNION ALL VALUES (1), (3), (1) ORDER BY 1 DESC LIMIT 2
   175  ----
   176  limit
   177   ├── columns: column1:3!null
   178   ├── internal-ordering: -3
   179   ├── ordering: -3
   180   ├── sort
   181   │    ├── columns: column1:3!null
   182   │    ├── ordering: -3
   183   │    ├── limit hint: 2.00
   184   │    └── union-all
   185   │         ├── columns: column1:3!null
   186   │         ├── left columns: column1:1
   187   │         ├── right columns: column1:2
   188   │         ├── values
   189   │         │    ├── columns: column1:1!null
   190   │         │    ├── (1,)
   191   │         │    ├── (1,)
   192   │         │    ├── (1,)
   193   │         │    ├── (2,)
   194   │         │    └── (2,)
   195   │         └── values
   196   │              ├── columns: column1:2!null
   197   │              ├── (1,)
   198   │              ├── (3,)
   199   │              └── (1,)
   200   └── 2
   201  
   202  # UNION with NULL columns in operands works.
   203  build
   204  VALUES (NULL) UNION ALL VALUES (1) ORDER BY 1
   205  ----
   206  sort
   207   ├── columns: column1:4
   208   ├── ordering: +4
   209   └── union-all
   210        ├── columns: column1:4
   211        ├── left columns: column1:3
   212        ├── right columns: column1:2
   213        ├── project
   214        │    ├── columns: column1:3
   215        │    ├── values
   216        │    │    ├── columns: column1:1
   217        │    │    └── (NULL,)
   218        │    └── projections
   219        │         └── column1:1::INT8 [as=column1:3]
   220        └── values
   221             ├── columns: column1:2!null
   222             └── (1,)
   223  
   224  build
   225  VALUES (NULL) UNION ALL VALUES (NULL)
   226  ----
   227  union-all
   228   ├── columns: column1:3
   229   ├── left columns: column1:1
   230   ├── right columns: column1:2
   231   ├── values
   232   │    ├── columns: column1:1
   233   │    └── (NULL,)
   234   └── values
   235        ├── columns: column1:2
   236        └── (NULL,)
   237  
   238  build
   239  SELECT x, pg_typeof(y) FROM (SELECT 1 AS a, NULL AS b UNION ALL SELECT 2 AS a, 4 AS b) AS t(x, y)
   240  ----
   241  project
   242   ├── columns: x:6!null pg_typeof:8
   243   ├── union-all
   244   │    ├── columns: a:6!null b:7
   245   │    ├── left columns: a:1 b:5
   246   │    ├── right columns: a:3 b:4
   247   │    ├── project
   248   │    │    ├── columns: b:5 a:1!null
   249   │    │    ├── project
   250   │    │    │    ├── columns: a:1!null b:2
   251   │    │    │    ├── values
   252   │    │    │    │    └── ()
   253   │    │    │    └── projections
   254   │    │    │         ├── 1 [as=a:1]
   255   │    │    │         └── NULL [as=b:2]
   256   │    │    └── projections
   257   │    │         └── b:2::INT8 [as=b:5]
   258   │    └── project
   259   │         ├── columns: a:3!null b:4!null
   260   │         ├── values
   261   │         │    └── ()
   262   │         └── projections
   263   │              ├── 2 [as=a:3]
   264   │              └── 4 [as=b:4]
   265   └── projections
   266        └── pg_typeof(b:7) [as=pg_typeof:8]
   267  
   268  build
   269  SELECT x, pg_typeof(y) FROM (SELECT 1 AS a, 3 AS b UNION ALL SELECT 2 AS a, NULL AS b) AS t(x, y)
   270  ----
   271  project
   272   ├── columns: x:5!null pg_typeof:7
   273   ├── union-all
   274   │    ├── columns: a:5!null b:6
   275   │    ├── left columns: a:1 b:2
   276   │    ├── right columns: a:3 b:4
   277   │    ├── project
   278   │    │    ├── columns: a:1!null b:2!null
   279   │    │    ├── values
   280   │    │    │    └── ()
   281   │    │    └── projections
   282   │    │         ├── 1 [as=a:1]
   283   │    │         └── 3 [as=b:2]
   284   │    └── project
   285   │         ├── columns: a:3!null b:4
   286   │         ├── values
   287   │         │    └── ()
   288   │         └── projections
   289   │              ├── 2 [as=a:3]
   290   │              └── NULL::INT8 [as=b:4]
   291   └── projections
   292        └── pg_typeof(b:6) [as=pg_typeof:7]
   293  
   294  exec-ddl
   295  CREATE TABLE uniontest (
   296    k INT,
   297    v INT
   298  )
   299  ----
   300  
   301  build
   302  SELECT v FROM uniontest WHERE k = 1 UNION SELECT v FROM uniontest WHERE k = 2
   303  ----
   304  union
   305   ├── columns: v:7
   306   ├── left columns: uniontest.v:2
   307   ├── right columns: uniontest.v:5
   308   ├── project
   309   │    ├── columns: uniontest.v:2
   310   │    └── select
   311   │         ├── columns: k:1!null uniontest.v:2 rowid:3!null
   312   │         ├── scan uniontest
   313   │         │    └── columns: k:1 uniontest.v:2 rowid:3!null
   314   │         └── filters
   315   │              └── k:1 = 1
   316   └── project
   317        ├── columns: uniontest.v:5
   318        └── select
   319             ├── columns: k:4!null uniontest.v:5 rowid:6!null
   320             ├── scan uniontest
   321             │    └── columns: k:4 uniontest.v:5 rowid:6!null
   322             └── filters
   323                  └── k:4 = 2
   324  
   325  build
   326  SELECT v FROM uniontest WHERE k = 1 UNION ALL SELECT v FROM uniontest WHERE k = 2
   327  ----
   328  union-all
   329   ├── columns: v:7
   330   ├── left columns: uniontest.v:2
   331   ├── right columns: uniontest.v:5
   332   ├── project
   333   │    ├── columns: uniontest.v:2
   334   │    └── select
   335   │         ├── columns: k:1!null uniontest.v:2 rowid:3!null
   336   │         ├── scan uniontest
   337   │         │    └── columns: k:1 uniontest.v:2 rowid:3!null
   338   │         └── filters
   339   │              └── k:1 = 1
   340   └── project
   341        ├── columns: uniontest.v:5
   342        └── select
   343             ├── columns: k:4!null uniontest.v:5 rowid:6!null
   344             ├── scan uniontest
   345             │    └── columns: k:4 uniontest.v:5 rowid:6!null
   346             └── filters
   347                  └── k:4 = 2
   348  
   349  build
   350  SELECT v FROM uniontest WHERE k = 1 INTERSECT SELECT v FROM uniontest WHERE k = 2
   351  ----
   352  intersect
   353   ├── columns: v:2
   354   ├── left columns: v:2
   355   ├── right columns: v:5
   356   ├── project
   357   │    ├── columns: v:2
   358   │    └── select
   359   │         ├── columns: k:1!null v:2 rowid:3!null
   360   │         ├── scan uniontest
   361   │         │    └── columns: k:1 v:2 rowid:3!null
   362   │         └── filters
   363   │              └── k:1 = 1
   364   └── project
   365        ├── columns: v:5
   366        └── select
   367             ├── columns: k:4!null v:5 rowid:6!null
   368             ├── scan uniontest
   369             │    └── columns: k:4 v:5 rowid:6!null
   370             └── filters
   371                  └── k:4 = 2
   372  
   373  build
   374  SELECT v FROM uniontest WHERE k = 1 INTERSECT ALL SELECT v FROM uniontest WHERE k = 2
   375  ----
   376  intersect-all
   377   ├── columns: v:2
   378   ├── left columns: v:2
   379   ├── right columns: v:5
   380   ├── project
   381   │    ├── columns: v:2
   382   │    └── select
   383   │         ├── columns: k:1!null v:2 rowid:3!null
   384   │         ├── scan uniontest
   385   │         │    └── columns: k:1 v:2 rowid:3!null
   386   │         └── filters
   387   │              └── k:1 = 1
   388   └── project
   389        ├── columns: v:5
   390        └── select
   391             ├── columns: k:4!null v:5 rowid:6!null
   392             ├── scan uniontest
   393             │    └── columns: k:4 v:5 rowid:6!null
   394             └── filters
   395                  └── k:4 = 2
   396  
   397  build
   398  SELECT v FROM uniontest WHERE k = 1 EXCEPT SELECT v FROM uniontest WHERE k = 2
   399  ----
   400  except
   401   ├── columns: v:2
   402   ├── left columns: v:2
   403   ├── right columns: v:5
   404   ├── project
   405   │    ├── columns: v:2
   406   │    └── select
   407   │         ├── columns: k:1!null v:2 rowid:3!null
   408   │         ├── scan uniontest
   409   │         │    └── columns: k:1 v:2 rowid:3!null
   410   │         └── filters
   411   │              └── k:1 = 1
   412   └── project
   413        ├── columns: v:5
   414        └── select
   415             ├── columns: k:4!null v:5 rowid:6!null
   416             ├── scan uniontest
   417             │    └── columns: k:4 v:5 rowid:6!null
   418             └── filters
   419                  └── k:4 = 2
   420  
   421  build
   422  SELECT v FROM uniontest WHERE k = 1 EXCEPT ALL SELECT v FROM uniontest WHERE k = 2
   423  ----
   424  except-all
   425   ├── columns: v:2
   426   ├── left columns: v:2
   427   ├── right columns: v:5
   428   ├── project
   429   │    ├── columns: v:2
   430   │    └── select
   431   │         ├── columns: k:1!null v:2 rowid:3!null
   432   │         ├── scan uniontest
   433   │         │    └── columns: k:1 v:2 rowid:3!null
   434   │         └── filters
   435   │              └── k:1 = 1
   436   └── project
   437        ├── columns: v:5
   438        └── select
   439             ├── columns: k:4!null v:5 rowid:6!null
   440             ├── scan uniontest
   441             │    └── columns: k:4 v:5 rowid:6!null
   442             └── filters
   443                  └── k:4 = 2
   444  
   445  build
   446  (SELECT v FROM uniontest WHERE k = 1 UNION ALL SELECT v FROM uniontest WHERE k = 2) ORDER BY 1 DESC LIMIT 2
   447  ----
   448  limit
   449   ├── columns: v:7
   450   ├── internal-ordering: -7
   451   ├── ordering: -7
   452   ├── sort
   453   │    ├── columns: v:7
   454   │    ├── ordering: -7
   455   │    ├── limit hint: 2.00
   456   │    └── union-all
   457   │         ├── columns: v:7
   458   │         ├── left columns: uniontest.v:2
   459   │         ├── right columns: uniontest.v:5
   460   │         ├── project
   461   │         │    ├── columns: uniontest.v:2
   462   │         │    └── select
   463   │         │         ├── columns: k:1!null uniontest.v:2 rowid:3!null
   464   │         │         ├── scan uniontest
   465   │         │         │    └── columns: k:1 uniontest.v:2 rowid:3!null
   466   │         │         └── filters
   467   │         │              └── k:1 = 1
   468   │         └── project
   469   │              ├── columns: uniontest.v:5
   470   │              └── select
   471   │                   ├── columns: k:4!null uniontest.v:5 rowid:6!null
   472   │                   ├── scan uniontest
   473   │                   │    └── columns: k:4 uniontest.v:5 rowid:6!null
   474   │                   └── filters
   475   │                        └── k:4 = 2
   476   └── 2
   477  
   478  # The ORDER BY and LIMIT apply to the UNION, not the last SELECT.
   479  build
   480  SELECT v FROM uniontest WHERE k = 1 UNION ALL SELECT v FROM uniontest WHERE k = 2 ORDER BY 1 DESC LIMIT 2
   481  ----
   482  limit
   483   ├── columns: v:7
   484   ├── internal-ordering: -7
   485   ├── ordering: -7
   486   ├── sort
   487   │    ├── columns: v:7
   488   │    ├── ordering: -7
   489   │    ├── limit hint: 2.00
   490   │    └── union-all
   491   │         ├── columns: v:7
   492   │         ├── left columns: uniontest.v:2
   493   │         ├── right columns: uniontest.v:5
   494   │         ├── project
   495   │         │    ├── columns: uniontest.v:2
   496   │         │    └── select
   497   │         │         ├── columns: k:1!null uniontest.v:2 rowid:3!null
   498   │         │         ├── scan uniontest
   499   │         │         │    └── columns: k:1 uniontest.v:2 rowid:3!null
   500   │         │         └── filters
   501   │         │              └── k:1 = 1
   502   │         └── project
   503   │              ├── columns: uniontest.v:5
   504   │              └── select
   505   │                   ├── columns: k:4!null uniontest.v:5 rowid:6!null
   506   │                   ├── scan uniontest
   507   │                   │    └── columns: k:4 uniontest.v:5 rowid:6!null
   508   │                   └── filters
   509   │                        └── k:4 = 2
   510   └── 2
   511  
   512  build
   513  SELECT v FROM uniontest UNION SELECT k FROM uniontest
   514  ----
   515  union
   516   ├── columns: v:7
   517   ├── left columns: uniontest.v:2
   518   ├── right columns: k:4
   519   ├── project
   520   │    ├── columns: uniontest.v:2
   521   │    └── scan uniontest
   522   │         └── columns: k:1 uniontest.v:2 rowid:3!null
   523   └── project
   524        ├── columns: k:4
   525        └── scan uniontest
   526             └── columns: k:4 uniontest.v:5 rowid:6!null
   527  
   528  build
   529  SELECT v FROM uniontest UNION ALL SELECT k FROM uniontest
   530  ----
   531  union-all
   532   ├── columns: v:7
   533   ├── left columns: uniontest.v:2
   534   ├── right columns: k:4
   535   ├── project
   536   │    ├── columns: uniontest.v:2
   537   │    └── scan uniontest
   538   │         └── columns: k:1 uniontest.v:2 rowid:3!null
   539   └── project
   540        ├── columns: k:4
   541        └── scan uniontest
   542             └── columns: k:4 uniontest.v:5 rowid:6!null
   543  
   544  build
   545  SELECT * FROM (SELECT * FROM (VALUES (1)) a LEFT JOIN (VALUES (1) UNION VALUES (2)) b on a.column1 = b.column1);
   546  ----
   547  left-join (hash)
   548   ├── columns: column1:1!null column1:4
   549   ├── values
   550   │    ├── columns: column1:1!null
   551   │    └── (1,)
   552   ├── union
   553   │    ├── columns: column1:4!null
   554   │    ├── left columns: column1:2
   555   │    ├── right columns: column1:3
   556   │    ├── values
   557   │    │    ├── columns: column1:2!null
   558   │    │    └── (1,)
   559   │    └── values
   560   │         ├── columns: column1:3!null
   561   │         └── (2,)
   562   └── filters
   563        └── column1:1 = column1:4
   564  
   565  build
   566  SELECT * FROM (VALUES (1)) a LEFT JOIN (VALUES (1) UNION VALUES (2)) b on a.column1 = b.column1;
   567  ----
   568  left-join (hash)
   569   ├── columns: column1:1!null column1:4
   570   ├── values
   571   │    ├── columns: column1:1!null
   572   │    └── (1,)
   573   ├── union
   574   │    ├── columns: column1:4!null
   575   │    ├── left columns: column1:2
   576   │    ├── right columns: column1:3
   577   │    ├── values
   578   │    │    ├── columns: column1:2!null
   579   │    │    └── (1,)
   580   │    └── values
   581   │         ├── columns: column1:3!null
   582   │         └── (2,)
   583   └── filters
   584        └── column1:1 = column1:4
   585  
   586  build
   587  SELECT 1, 2 UNION SELECT 3
   588  ----
   589  error (42601): each UNION query must have the same number of columns: 2 vs 1
   590  
   591  build
   592  SELECT 1, 2 INTERSECT SELECT 3
   593  ----
   594  error (42601): each INTERSECT query must have the same number of columns: 2 vs 1
   595  
   596  build
   597  SELECT 1, 2 EXCEPT SELECT 3
   598  ----
   599  error (42601): each EXCEPT query must have the same number of columns: 2 vs 1
   600  
   601  build
   602  SELECT 1 UNION SELECT '3'
   603  ----
   604  union
   605   ├── columns: "?column?":3!null
   606   ├── left columns: "?column?":1
   607   ├── right columns: "?column?":2
   608   ├── project
   609   │    ├── columns: "?column?":1!null
   610   │    ├── values
   611   │    │    └── ()
   612   │    └── projections
   613   │         └── 1 [as="?column?":1]
   614   └── project
   615        ├── columns: "?column?":2!null
   616        ├── values
   617        │    └── ()
   618        └── projections
   619             └── 3 [as="?column?":2]
   620  
   621  build
   622  SELECT 1 INTERSECT SELECT '3'
   623  ----
   624  intersect
   625   ├── columns: "?column?":1!null
   626   ├── left columns: "?column?":1!null
   627   ├── right columns: "?column?":2
   628   ├── project
   629   │    ├── columns: "?column?":1!null
   630   │    ├── values
   631   │    │    └── ()
   632   │    └── projections
   633   │         └── 1 [as="?column?":1]
   634   └── project
   635        ├── columns: "?column?":2!null
   636        ├── values
   637        │    └── ()
   638        └── projections
   639             └── 3 [as="?column?":2]
   640  
   641  build
   642  SELECT 1 EXCEPT SELECT '3'
   643  ----
   644  except
   645   ├── columns: "?column?":1!null
   646   ├── left columns: "?column?":1!null
   647   ├── right columns: "?column?":2
   648   ├── project
   649   │    ├── columns: "?column?":1!null
   650   │    ├── values
   651   │    │    └── ()
   652   │    └── projections
   653   │         └── 1 [as="?column?":1]
   654   └── project
   655        ├── columns: "?column?":2!null
   656        ├── values
   657        │    └── ()
   658        └── projections
   659             └── 3 [as="?column?":2]
   660  
   661  build
   662  SELECT 1 UNION SELECT 3 ORDER BY z
   663  ----
   664  error (42703): column "z" does not exist
   665  
   666  build
   667  SELECT ARRAY[1] UNION ALL SELECT ARRAY['foo']
   668  ----
   669  error (22P02): could not parse "foo" as type int: strconv.ParseInt: parsing "foo": invalid syntax
   670  
   671  build
   672  SELECT ARRAY['foo'] UNION ALL SELECT ARRAY[1]
   673  ----
   674  error (42804): UNION types string[] and int[] cannot be matched
   675  
   676  build
   677  SELECT ARRAY[1] UNION ALL SELECT ARRAY[1.2]
   678  ----
   679  error (42804): UNION types int[] and decimal[] cannot be matched
   680  
   681  exec-ddl
   682  CREATE TABLE t.xy (x STRING NOT NULL, y STRING NOT NULL)
   683  ----
   684  
   685  exec-ddl
   686  CREATE TABLE t.abc (
   687    a string,
   688    b string NOT NULL,
   689    c string NOT NULL
   690  )
   691  ----
   692  
   693  build
   694  (SELECT x, x, y FROM xy) UNION (SELECT a, b, c FROM abc)
   695  ----
   696  union
   697   ├── columns: x:8 x:9!null y:10!null
   698   ├── left columns: xy.x:1 xy.x:1 xy.y:2
   699   ├── right columns: a:4 b:5 c:6
   700   ├── project
   701   │    ├── columns: xy.x:1!null xy.y:2!null
   702   │    └── scan xy
   703   │         └── columns: xy.x:1!null xy.y:2!null xy.rowid:3!null
   704   └── project
   705        ├── columns: a:4 b:5!null c:6!null
   706        └── scan abc
   707             └── columns: a:4 b:5!null c:6!null abc.rowid:7!null
   708  
   709  build
   710  (SELECT a FROM abc ORDER BY b) UNION ALL (SELECT b FROM abc) ORDER BY a
   711  ----
   712  sort
   713   ├── columns: a:9
   714   ├── ordering: +9
   715   └── union-all
   716        ├── columns: a:9
   717        ├── left columns: abc.a:1
   718        ├── right columns: b:6
   719        ├── project
   720        │    ├── columns: abc.a:1 b:2!null
   721        │    └── scan abc
   722        │         └── columns: abc.a:1 b:2!null c:3!null rowid:4!null
   723        └── project
   724             ├── columns: b:6!null
   725             └── scan abc
   726                  └── columns: abc.a:5 b:6!null c:7!null rowid:8!null
   727  
   728  build
   729  (SELECT a FROM abc ORDER BY b) UNION ALL (SELECT a FROM abc ORDER BY c) ORDER BY a
   730  ----
   731  sort
   732   ├── columns: a:9
   733   ├── ordering: +9
   734   └── union-all
   735        ├── columns: a:9
   736        ├── left columns: abc.a:1
   737        ├── right columns: abc.a:5
   738        ├── project
   739        │    ├── columns: abc.a:1 b:2!null
   740        │    └── scan abc
   741        │         └── columns: abc.a:1 b:2!null c:3!null rowid:4!null
   742        └── project
   743             ├── columns: abc.a:5 c:7!null
   744             └── scan abc
   745                  └── columns: abc.a:5 b:6!null c:7!null rowid:8!null
   746  
   747  build
   748  (SELECT a FROM abc ORDER BY b) EXCEPT (SELECT b FROM abc ORDER BY c, b, a)
   749  ----
   750  except
   751   ├── columns: a:1
   752   ├── left columns: a:1
   753   ├── right columns: b:6
   754   ├── project
   755   │    ├── columns: a:1 b:2!null
   756   │    └── scan abc
   757   │         └── columns: a:1 b:2!null c:3!null rowid:4!null
   758   └── project
   759        ├── columns: a:5 b:6!null c:7!null
   760        └── scan abc
   761             └── columns: a:5 b:6!null c:7!null rowid:8!null
   762  
   763  # Tests for type propagation.
   764  
   765  build
   766  VALUES (NULL, NULL), (NULL, 'x') UNION VALUES (1, 'a'), (2, 'b')
   767  ----
   768  union
   769   ├── columns: column1:6 column2:7
   770   ├── left columns: column1:5 column2:2
   771   ├── right columns: column1:3 column2:4
   772   ├── project
   773   │    ├── columns: column1:5 column2:2
   774   │    ├── values
   775   │    │    ├── columns: column1:1 column2:2
   776   │    │    ├── (NULL, NULL::STRING)
   777   │    │    └── (NULL, 'x')
   778   │    └── projections
   779   │         └── column1:1::INT8 [as=column1:5]
   780   └── values
   781        ├── columns: column1:3!null column2:4!null
   782        ├── (1, 'a')
   783        └── (2, 'b')
   784  
   785  build
   786  VALUES (3, NULL), (NULL, 'x') INTERSECT VALUES (1, NULL), (2, NULL)
   787  ----
   788  intersect
   789   ├── columns: column1:1 column2:2
   790   ├── left columns: column1:1 column2:2
   791   ├── right columns: column1:3 column2:4
   792   ├── values
   793   │    ├── columns: column1:1 column2:2
   794   │    ├── (3, NULL::STRING)
   795   │    └── (NULL::INT8, 'x')
   796   └── values
   797        ├── columns: column1:3!null column2:4
   798        ├── (1, NULL::STRING)
   799        └── (2, NULL::STRING)
   800  
   801  build
   802  VALUES (NULL, NULL), (NULL, 'x') UNION ALL VALUES (1, NULL), (2, NULL)
   803  ----
   804  union-all
   805   ├── columns: column1:6 column2:7
   806   ├── left columns: column1:5 column2:2
   807   ├── right columns: column1:3 column2:4
   808   ├── project
   809   │    ├── columns: column1:5 column2:2
   810   │    ├── values
   811   │    │    ├── columns: column1:1 column2:2
   812   │    │    ├── (NULL, NULL::STRING)
   813   │    │    └── (NULL, 'x')
   814   │    └── projections
   815   │         └── column1:1::INT8 [as=column1:5]
   816   └── values
   817        ├── columns: column1:3!null column2:4
   818        ├── (1, NULL::STRING)
   819        └── (2, NULL::STRING)
   820  
   821  build
   822  VALUES (NULL, NULL), (NULL, NULL) UNION ALL VALUES (NULL, NULL), (NULL, NULL)
   823  ----
   824  union-all
   825   ├── columns: column1:5 column2:6
   826   ├── left columns: column1:1 column2:2
   827   ├── right columns: column1:3 column2:4
   828   ├── values
   829   │    ├── columns: column1:1 column2:2
   830   │    ├── (NULL, NULL)
   831   │    └── (NULL, NULL)
   832   └── values
   833        ├── columns: column1:3 column2:4
   834        ├── (NULL, NULL)
   835        └── (NULL, NULL)
   836  
   837  exec-ddl
   838  CREATE TABLE a (a INT PRIMARY KEY)
   839  ----
   840  
   841  # Regression test for #34524.
   842  build
   843  (SELECT NULL FROM a) EXCEPT (VALUES((SELECT 1 FROM a LIMIT 1)), (1))
   844  ----
   845  except
   846   ├── columns: "?column?":6
   847   ├── left columns: "?column?":6
   848   ├── right columns: column1:5
   849   ├── project
   850   │    ├── columns: "?column?":6
   851   │    ├── project
   852   │    │    ├── columns: "?column?":2
   853   │    │    ├── scan a
   854   │    │    │    └── columns: a:1!null
   855   │    │    └── projections
   856   │    │         └── NULL [as="?column?":2]
   857   │    └── projections
   858   │         └── "?column?":2::INT8 [as="?column?":6]
   859   └── values
   860        ├── columns: column1:5
   861        ├── tuple
   862        │    └── subquery
   863        │         └── max1-row
   864        │              ├── columns: "?column?":4!null
   865        │              └── limit
   866        │                   ├── columns: "?column?":4!null
   867        │                   ├── project
   868        │                   │    ├── columns: "?column?":4!null
   869        │                   │    ├── limit hint: 1.00
   870        │                   │    ├── scan a
   871        │                   │    │    ├── columns: a:3!null
   872        │                   │    │    └── limit hint: 1.00
   873        │                   │    └── projections
   874        │                   │         └── 1 [as="?column?":4]
   875        │                   └── 1
   876        └── (1,)