File: union.out

package info (click to toggle)
derby 10.14.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 79,056 kB
  • sloc: java: 691,961; sql: 42,686; xml: 20,512; sh: 3,373; sed: 96; makefile: 60
file content (1268 lines) | stat: -rw-r--r-- 57,065 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
ij> --
--   Licensed to the Apache Software Foundation (ASF) under one or more
--   contributor license agreements.  See the NOTICE file distributed with
--   this work for additional information regarding copyright ownership.
--   The ASF licenses this file to You under the Apache License, Version 2.0
--   (the "License"); you may not use this file except in compliance with
--   the License.  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--   Unless required by applicable law or agreed to in writing, software
--   distributed under the License is distributed on an "AS IS" BASIS,
--   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--   See the License for the specific language governing permissions and
--   limitations under the License.
--
--
-- this test shows union functionality
--

-- create the tables
create table t1 (i int, s smallint, d double precision, r real, c10 char(10),
				c30 char(30), vc10 varchar(10), vc30 varchar(30));
0 rows inserted/updated/deleted
ij> create table t2 (i int, s smallint, d double precision, r real, c10 char(10),
				c30 char(30), vc10 varchar(10), vc30 varchar(30));
0 rows inserted/updated/deleted
ij> create table dups (i int, s smallint, d double precision, r real, c10 char(10),
				c30 char(30), vc10 varchar(10), vc30 varchar(30));
0 rows inserted/updated/deleted
ij> -- populate the tables
insert into t1 values (null, null, null, null, null, null, null, null);
1 row inserted/updated/deleted
ij> insert into t1 values (1, 1, 1e1, 1e1, '11111', '11111     11', '11111',
	'11111      11');
1 row inserted/updated/deleted
ij> insert into t1 values (2, 2, 2e1, 2e1, '22222', '22222     22', '22222',
	'22222      22');
1 row inserted/updated/deleted
ij> insert into t2 values (null, null, null, null, null, null, null, null);
1 row inserted/updated/deleted
ij> insert into t2 values (3, 3, 3e1, 3e1, '33333', '33333     33', '33333',
	'33333      33');
1 row inserted/updated/deleted
ij> insert into t2 values (4, 4, 4e1, 4e1, '44444', '44444     44', '44444',
	'44444      44');
1 row inserted/updated/deleted
ij> insert into dups select * from t1 union all select * from t2;
6 rows inserted/updated/deleted
ij> -- simple cases
values (1, 2, 3, 4) union values (5, 6, 7, 8);
1          |2          |3          |4          
-----------------------------------------------
1          |2          |3          |4          
5          |6          |7          |8          
ij> values (1, 2, 3, 4) union values (1, 2, 3, 4);
1          |2          |3          |4          
-----------------------------------------------
1          |2          |3          |4          
ij> values (1, 2, 3, 4) union distinct values (5, 6, 7, 8);
1          |2          |3          |4          
-----------------------------------------------
1          |2          |3          |4          
5          |6          |7          |8          
ij> values (1, 2, 3, 4) union distinct values (1, 2, 3, 4);
1          |2          |3          |4          
-----------------------------------------------
1          |2          |3          |4          
ij> values (1, 2, 3, 4) union values (5, 6, 7, 8) union values (9, 10, 11, 12);
1          |2          |3          |4          
-----------------------------------------------
1          |2          |3          |4          
5          |6          |7          |8          
9          |10         |11         |12         
ij> values (1, 2, 3, 4) union values (1, 2, 3, 4) union values (1, 2, 3, 4);
1          |2          |3          |4          
-----------------------------------------------
1          |2          |3          |4          
ij> select * from t1 union select * from t2;
I          |S     |D                       |R              |C10       |C30                           |VC10      |VC30                          
-----------------------------------------------------------------------------------------------------------------------------------------------
1          |1     |10.0                    |10.0           |11111     |11111     11                  |11111     |11111      11                 
2          |2     |20.0                    |20.0           |22222     |22222     22                  |22222     |22222      22                 
3          |3     |30.0                    |30.0           |33333     |33333     33                  |33333     |33333      33                 
4          |4     |40.0                    |40.0           |44444     |44444     44                  |44444     |44444      44                 
NULL       |NULL  |NULL                    |NULL           |NULL      |NULL                          |NULL      |NULL                          
ij> select * from t1 union select * from t1;
I          |S     |D                       |R              |C10       |C30                           |VC10      |VC30                          
-----------------------------------------------------------------------------------------------------------------------------------------------
1          |1     |10.0                    |10.0           |11111     |11111     11                  |11111     |11111      11                 
2          |2     |20.0                    |20.0           |22222     |22222     22                  |22222     |22222      22                 
NULL       |NULL  |NULL                    |NULL           |NULL      |NULL                          |NULL      |NULL                          
ij> select * from t1 union select * from t2 union select * from dups;
I          |S     |D                       |R              |C10       |C30                           |VC10      |VC30                          
-----------------------------------------------------------------------------------------------------------------------------------------------
1          |1     |10.0                    |10.0           |11111     |11111     11                  |11111     |11111      11                 
2          |2     |20.0                    |20.0           |22222     |22222     22                  |22222     |22222      22                 
3          |3     |30.0                    |30.0           |33333     |33333     33                  |33333     |33333      33                 
4          |4     |40.0                    |40.0           |44444     |44444     44                  |44444     |44444      44                 
NULL       |NULL  |NULL                    |NULL           |NULL      |NULL                          |NULL      |NULL                          
ij> select * from t1 union select i, s, d, r, c10, c30, vc10, vc30 from t2;
I          |S     |D                       |R              |C10       |C30                           |VC10      |VC30                          
-----------------------------------------------------------------------------------------------------------------------------------------------
1          |1     |10.0                    |10.0           |11111     |11111     11                  |11111     |11111      11                 
2          |2     |20.0                    |20.0           |22222     |22222     22                  |22222     |22222      22                 
3          |3     |30.0                    |30.0           |33333     |33333     33                  |33333     |33333      33                 
4          |4     |40.0                    |40.0           |44444     |44444     44                  |44444     |44444      44                 
NULL       |NULL  |NULL                    |NULL           |NULL      |NULL                          |NULL      |NULL                          
ij> select * from t1 union select i, s, d, r, c10, c30, vc10, vc30 from t2
		union select * from dups;
I          |S     |D                       |R              |C10       |C30                           |VC10      |VC30                          
-----------------------------------------------------------------------------------------------------------------------------------------------
1          |1     |10.0                    |10.0           |11111     |11111     11                  |11111     |11111      11                 
2          |2     |20.0                    |20.0           |22222     |22222     22                  |22222     |22222      22                 
3          |3     |30.0                    |30.0           |33333     |33333     33                  |33333     |33333      33                 
4          |4     |40.0                    |40.0           |44444     |44444     44                  |44444     |44444      44                 
NULL       |NULL  |NULL                    |NULL           |NULL      |NULL                          |NULL      |NULL                          
ij> -- derived tables
select * from (values (1, 2, 3, 4) union values (5, 6, 7, 8)) a;
1          |2          |3          |4          
-----------------------------------------------
1          |2          |3          |4          
5          |6          |7          |8          
ij> select * from (values (1, 2, 3, 4) union values (5, 6, 7, 8) union
			   values (1, 2, 3, 4)) a;
1          |2          |3          |4          
-----------------------------------------------
1          |2          |3          |4          
5          |6          |7          |8          
ij> -- mix unions and union alls
select i from t1 union select i from t2 union all select i from dups;
I          
-----------
1          
2          
3          
4          
NULL       
NULL       
1          
2          
NULL       
3          
4          
ij> (select i from t1 union select i from t2) union all select i from dups;
I          
-----------
1          
2          
3          
4          
NULL       
NULL       
1          
2          
NULL       
3          
4          
ij> select i from t1 union (select i from t2 union all select i from dups);
I          
-----------
1          
2          
3          
4          
NULL       
ij> select i from t1 union all select i from t2 union select i from dups;
I          
-----------
1          
2          
3          
4          
NULL       
ij> (select i from t1 union all select i from t2) union select i from dups;
I          
-----------
1          
2          
3          
4          
NULL       
ij> select i from t1 union all (select i from t2 union select i from dups);
I          
-----------
NULL       
1          
2          
1          
2          
3          
4          
NULL       
ij> -- joins
select a.i, b.i from t1 a, t2 b union select b.i, a.i from t1 a, t2 b;
I          |I          
-----------------------
1          |3          
1          |4          
1          |NULL       
2          |3          
2          |4          
2          |NULL       
3          |1          
3          |2          
3          |NULL       
4          |1          
4          |2          
4          |NULL       
NULL       |1          
NULL       |2          
NULL       |3          
NULL       |4          
NULL       |NULL       
ij> values (9, 10) union 
	select a.i, b.i from t1 a, t2 b union select b.i, a.i from t1 a, t2 b;
1          |2          
-----------------------
1          |3          
1          |4          
1          |NULL       
2          |3          
2          |4          
2          |NULL       
3          |1          
3          |2          
3          |NULL       
4          |1          
4          |2          
4          |NULL       
9          |10         
NULL       |1          
NULL       |2          
NULL       |3          
NULL       |4          
NULL       |NULL       
ij> select a.i, b.i from t1 a, t2 b union 
	select b.i, a.i from t1 a, t2 b union values (9, 10);
1          |2          
-----------------------
1          |3          
1          |4          
1          |NULL       
2          |3          
2          |4          
2          |NULL       
3          |1          
3          |2          
3          |NULL       
4          |1          
4          |2          
4          |NULL       
9          |10         
NULL       |1          
NULL       |2          
NULL       |3          
NULL       |4          
NULL       |NULL       
ij> -- non-correlated subqueries

-- positive tests
select i from t1 where i = (values 1 union values 1);
I          
-----------
1          
ij> select i from t1 where i = (values 1 union values 1 union values 1);
I          
-----------
1          
ij> -- expression subquery
select i from t1 where i = (select 1 from t2 union values 1);
I          
-----------
1          
ij> -- in subquery
select i from t1 where i in (select i from t2 union values 1 union values 2);
I          
-----------
1          
2          
ij> select i from t1 where i in 
		(select a from (select i from t2 union values 1 union values 2) a (a));
I          
-----------
1          
2          
ij> -- not in subquery
select i from t1 where i not in (select i from t2 union values 1 union values 2);
I          
-----------
ij> select i from t1 where i not in (select i from t2 where i is not null union 
								 values 1 union values 22);
I          
-----------
2          
ij> select i from t1 where i not in 
		(select a from (select i from t2 where i is not null union 
						values 111 union values 2) a (a));
I          
-----------
1          
ij> -- correlated union subquery
select i from t1 a where i in (select i from t2 where 1 = 0 union 
							   select a.i from t2 where a.i < i);
I          
-----------
1          
2          
ij> select i from t1 a where i in (select a.i from t2 where a.i < i union 
							   select i from t2 where 1 < 0);
I          
-----------
1          
2          
ij> -- exists subquery
select i from t1 where exists (select * from t2 union select * from t2);
I          
-----------
NULL       
1          
2          
ij> select i from t1 where exists (select 1 from t2 union select 2 from t2);
I          
-----------
NULL       
1          
2          
ij> select i from t1 where exists (select 1 from t2 where 1 = 0 union 
							   select 2 from t2 where t1.i < i);
I          
-----------
1          
2          
ij> select i from t1 where exists (select i from t2 where t1.i < i union 
							    select i from t2 where 1 = 0 union 
							   select i from t2 where t1.i < i union 
							    select i from t2 where 1 = 0);
I          
-----------
1          
2          
ij> -- These next two should fail because left/right children do not have
-- the same number of result columns.
select i from t1 where exists (select 1 from t2 where 1 = 0 union 
							   select * from t2 where t1.i < i);
ERROR 42X58: The number of columns on the left and right sides of the UNION must be the same.
ij> select i from t1 where exists (select i from t2 where t1.i < i union 
							    select * from t2 where 1 = 0 union 
							   select * from t2 where t1.i < i union 
							    select i from t2 where 1 = 0);
ERROR 42X58: The number of columns on the left and right sides of the UNION must be the same.
ij> -- order by tests
select i from t1 union select i from dups order by i desc;
I          
-----------
NULL       
4          
3          
2          
1          
ij> select i, s from t1 union select s as i, 1 as s from dups order by s desc, i;
I          |S          
-----------------------
NULL       |NULL       
2          |2          
1          |1          
2          |1          
3          |1          
4          |1          
NULL       |1          
ij> -- insert tests
create table insert_test (i int, s smallint, d double precision, r real,
	c10 char(10), c30 char(30), vc10 varchar(10), vc30 varchar(30));
0 rows inserted/updated/deleted
ij> -- simple tests
insert into insert_test select * from t1 union select * from dups;
5 rows inserted/updated/deleted
ij> select * from insert_test;
I          |S     |D                       |R              |C10       |C30                           |VC10      |VC30                          
-----------------------------------------------------------------------------------------------------------------------------------------------
1          |1     |10.0                    |10.0           |11111     |11111     11                  |11111     |11111      11                 
2          |2     |20.0                    |20.0           |22222     |22222     22                  |22222     |22222      22                 
3          |3     |30.0                    |30.0           |33333     |33333     33                  |33333     |33333      33                 
4          |4     |40.0                    |40.0           |44444     |44444     44                  |44444     |44444      44                 
NULL       |NULL  |NULL                    |NULL           |NULL      |NULL                          |NULL      |NULL                          
ij> delete from insert_test;
5 rows inserted/updated/deleted
ij> insert into insert_test (s, i) values (2, 1) union values (4, 3);
2 rows inserted/updated/deleted
ij> select * from insert_test;
I          |S     |D                       |R              |C10       |C30                           |VC10      |VC30                          
-----------------------------------------------------------------------------------------------------------------------------------------------
1          |2     |NULL                    |NULL           |NULL      |NULL                          |NULL      |NULL                          
3          |4     |NULL                    |NULL           |NULL      |NULL                          |NULL      |NULL                          
ij> delete from insert_test;
2 rows inserted/updated/deleted
ij> -- test type dominance/length/nullability
insert into insert_test (vc30) select vc10 from t1 union select c30 from t2;
5 rows inserted/updated/deleted
ij> select * from insert_test;
I          |S     |D                       |R              |C10       |C30                           |VC10      |VC30                          
-----------------------------------------------------------------------------------------------------------------------------------------------
NULL       |NULL  |NULL                    |NULL           |NULL      |NULL                          |NULL      |11111                         
NULL       |NULL  |NULL                    |NULL           |NULL      |NULL                          |NULL      |22222                         
NULL       |NULL  |NULL                    |NULL           |NULL      |NULL                          |NULL      |33333     33                  
NULL       |NULL  |NULL                    |NULL           |NULL      |NULL                          |NULL      |44444     44                  
NULL       |NULL  |NULL                    |NULL           |NULL      |NULL                          |NULL      |NULL                          
ij> delete from insert_test;
5 rows inserted/updated/deleted
ij> insert into insert_test (c30)
	select vc10 from t1
	union
	select c30 from t2
	union
	select c10 from t1;
5 rows inserted/updated/deleted
ij> select * from insert_test;
I          |S     |D                       |R              |C10       |C30                           |VC10      |VC30                          
-----------------------------------------------------------------------------------------------------------------------------------------------
NULL       |NULL  |NULL                    |NULL           |NULL      |11111                         |NULL      |NULL                          
NULL       |NULL  |NULL                    |NULL           |NULL      |22222                         |NULL      |NULL                          
NULL       |NULL  |NULL                    |NULL           |NULL      |33333     33                  |NULL      |NULL                          
NULL       |NULL  |NULL                    |NULL           |NULL      |44444     44                  |NULL      |NULL                          
NULL       |NULL  |NULL                    |NULL           |NULL      |NULL                          |NULL      |NULL                          
ij> delete from insert_test;
5 rows inserted/updated/deleted
ij> -- test NormalizeResultSet generation
select i, d from t1 union select d, i from t2;
1                       |2                       
-------------------------------------------------
1.0                     |10.0                    
2.0                     |20.0                    
30.0                    |3.0                     
40.0                    |4.0                     
NULL                    |NULL                    
ij> select vc10, c30 from t1 union select c30, vc10 from t2;
1                             |2                             
-------------------------------------------------------------
11111                         |11111     11                  
22222                         |22222     22                  
33333     33                  |33333                         
44444     44                  |44444                         
NULL                          |NULL                          
ij> create table insert_test2 (s smallint not null, vc30 varchar(30) not null);
0 rows inserted/updated/deleted
ij> -- the following should fail due to null constraint
insert into insert_test2 select s, c10 from t1 union select s, c30 from t2;
ERROR 23502: Column 'S'  cannot accept a NULL value.
ij> select * from insert_test2;
S     |VC30                          
-------------------------------------
ij> -- negative tests
-- ? in select list of union
select ? from insert_test union select vc30 from insert_test;
ERROR 42X34: There is a ? parameter in the select list.  This is not allowed.
ij> select vc30 from insert_test union select ? from insert_test;
ERROR 42X34: There is a ? parameter in the select list.  This is not allowed.
ij> -- DB2 requires matching target and result column for insert
insert into insert_test values (1, 2) union values (3, 4);
ERROR 42802: The number of values assigned is not the same as the number of specified or implied columns.
ij> -- try some unions of different types.  
-- types should be ok if comparable.
values (1) union values (1.1);
1             
--------------
1.0           
1.1           
ij> values (1) union values (1.1e1);
1                       
------------------------
1.0                     
11.0                    
ij> values (1.1) union values (1);
1             
--------------
1.0           
1.1           
ij> values (1.1e1) union values (1);
1                       
------------------------
1.0                     
11.0                    
ij> -- negative cases
values (x'aa') union values (1);
ERROR 42X61: Types 'CHAR () FOR BIT DATA' and 'INTEGER' are not UNION compatible.
ij> -- drop the tables
drop table t1;
0 rows inserted/updated/deleted
ij> drop table t2;
0 rows inserted/updated/deleted
ij> drop table dups;
0 rows inserted/updated/deleted
ij> drop table insert_test;
0 rows inserted/updated/deleted
ij> drop table insert_test2;
0 rows inserted/updated/deleted
ij> --
-- this test shows the current supported union all functionality
--
-- RESOLVE - whats not tested
--	type compatability
--  nullability of result
--  type dominance
--  correlated subqueries
--  table constructors

-- create the tables
create table t1 (i int, s smallint, d double precision, r real, c10 char(10),
				 c30 char(30), vc10 varchar(10), vc30 varchar(30));
0 rows inserted/updated/deleted
ij> create table t2 (i int, s smallint, d double precision, r real, c10 char(10),
				 c30 char(30), vc10 varchar(10), vc30 varchar(30));
0 rows inserted/updated/deleted
ij> -- populate the tables
insert into t1 values (null, null, null, null, null, null, null, null);
1 row inserted/updated/deleted
ij> insert into t1 values (1, 1, 1e1, 1e1, '11111', '11111     11', '11111',
	'11111      11');
1 row inserted/updated/deleted
ij> insert into t1 values (2, 2, 2e1, 2e1, '22222', '22222     22', '22222',
	'22222      22');
1 row inserted/updated/deleted
ij> insert into t2 values (null, null, null, null, null, null, null, null);
1 row inserted/updated/deleted
ij> insert into t2 values (3, 3, 3e1, 3e1, '33333', '33333     33', '33333',
	'33333      33');
1 row inserted/updated/deleted
ij> insert into t2 values (4, 4, 4e1, 4e1, '44444', '44444     44', '44444',
	'44444      44');
1 row inserted/updated/deleted
ij> -- negative tests

-- non matching number of columns
select * from t1 union all select * from t1, t2;
ERROR 42X58: The number of columns on the left and right sides of the UNION must be the same.
ij> select * from t1 union all values (1, 2, 3, 4);
ERROR 42X58: The number of columns on the left and right sides of the UNION must be the same.
ij> values (1, 2, 3, 4) union all select * from t1;
ERROR 42X58: The number of columns on the left and right sides of the UNION must be the same.
ij> -- simple cases
values (1, 2, 3, 4) union all values (5, 6, 7, 8);
1          |2          |3          |4          
-----------------------------------------------
1          |2          |3          |4          
5          |6          |7          |8          
ij> values (1, 2, 3, 4) union all values (5, 6, 7, 8) union all values (9, 10, 11, 12);
1          |2          |3          |4          
-----------------------------------------------
1          |2          |3          |4          
5          |6          |7          |8          
9          |10         |11         |12         
ij> select * from t1 union all select * from t2;
I          |S     |D                       |R              |C10       |C30                           |VC10      |VC30                          
-----------------------------------------------------------------------------------------------------------------------------------------------
NULL       |NULL  |NULL                    |NULL           |NULL      |NULL                          |NULL      |NULL                          
1          |1     |10.0                    |10.0           |11111     |11111     11                  |11111     |11111      11                 
2          |2     |20.0                    |20.0           |22222     |22222     22                  |22222     |22222      22                 
NULL       |NULL  |NULL                    |NULL           |NULL      |NULL                          |NULL      |NULL                          
3          |3     |30.0                    |30.0           |33333     |33333     33                  |33333     |33333      33                 
4          |4     |40.0                    |40.0           |44444     |44444     44                  |44444     |44444      44                 
ij> select * from t1 union all select i, s, d, r, c10, c30, vc10, vc30 from t2;
I          |S     |D                       |R              |C10       |C30                           |VC10      |VC30                          
-----------------------------------------------------------------------------------------------------------------------------------------------
NULL       |NULL  |NULL                    |NULL           |NULL      |NULL                          |NULL      |NULL                          
1          |1     |10.0                    |10.0           |11111     |11111     11                  |11111     |11111      11                 
2          |2     |20.0                    |20.0           |22222     |22222     22                  |22222     |22222      22                 
NULL       |NULL  |NULL                    |NULL           |NULL      |NULL                          |NULL      |NULL                          
3          |3     |30.0                    |30.0           |33333     |33333     33                  |33333     |33333      33                 
4          |4     |40.0                    |40.0           |44444     |44444     44                  |44444     |44444      44                 
ij> -- derived tables
select * from (values (1, 2, 3, 4) union all values (5, 6, 7, 8)) a;
1          |2          |3          |4          
-----------------------------------------------
1          |2          |3          |4          
5          |6          |7          |8          
ij> select * from (values (1, 2, 3, 4) union all values (5, 6, 7, 8)) a (a, b, c, d);
A          |B          |C          |D          
-----------------------------------------------
1          |2          |3          |4          
5          |6          |7          |8          
ij> select b, d from (values (1, 2, 3, 4) union all values (5, 6, 7, 8)) a (a, b, c, d);
B          |D          
-----------------------
2          |4          
6          |8          
ij> select * from (select i, s, c10, vc10 from t1 union all select i, s, c10, vc10 from t2) a;
I          |S     |C10       |VC10      
----------------------------------------
NULL       |NULL  |NULL      |NULL      
1          |1     |11111     |11111     
2          |2     |22222     |22222     
NULL       |NULL  |NULL      |NULL      
3          |3     |33333     |33333     
4          |4     |44444     |44444     
ij> select * from (select i, s, c10, vc10 from t1 union all 
			   select i, s, c10, vc10 from t2) a (j, k, l, m), 
			   (select i, s, c10, vc10 from t1 union all 
			   select i, s, c10, vc10 from t2) b (j, k, l, m)
where a.j = b.j;
J          |K     |L         |M         |J          |K     |L         |M         
---------------------------------------------------------------------------------
1          |1     |11111     |11111     |1          |1     |11111     |11111     
2          |2     |22222     |22222     |2          |2     |22222     |22222     
3          |3     |33333     |33333     |3          |3     |33333     |33333     
4          |4     |44444     |44444     |4          |4     |44444     |44444     
ij> -- joins
select a.i, b.i from t1 a, t2 b union all select b.i, a.i from t1 a, t2 b;
I          |I          
-----------------------
NULL       |NULL       
NULL       |3          
NULL       |4          
1          |NULL       
1          |3          
1          |4          
2          |NULL       
2          |3          
2          |4          
NULL       |NULL       
3          |NULL       
4          |NULL       
NULL       |1          
3          |1          
4          |1          
NULL       |2          
3          |2          
4          |2          
ij> values (9, 10) union all 
	select a.i, b.i from t1 a, t2 b union all select b.i, a.i from t1 a, t2 b;
1          |2          
-----------------------
9          |10         
NULL       |NULL       
NULL       |3          
NULL       |4          
1          |NULL       
1          |3          
1          |4          
2          |NULL       
2          |3          
2          |4          
NULL       |NULL       
3          |NULL       
4          |NULL       
NULL       |1          
3          |1          
4          |1          
NULL       |2          
3          |2          
4          |2          
ij> select a.i, b.i from t1 a, t2 b union all 
	select b.i, a.i from t1 a, t2 b union all values (9, 10);
1          |2          
-----------------------
NULL       |NULL       
NULL       |3          
NULL       |4          
1          |NULL       
1          |3          
1          |4          
2          |NULL       
2          |3          
2          |4          
NULL       |NULL       
3          |NULL       
4          |NULL       
NULL       |1          
3          |1          
4          |1          
NULL       |2          
3          |2          
4          |2          
9          |10         
ij> -- incompatible types
select date('9999-11-11') from t1 union all select time('11:11:11') from t2;
ERROR 42X61: Types 'DATE' and 'TIME' are not UNION compatible.
ij> -- non-correlated subqueries

-- negative tests

-- select * in subquery
select i from t1 where i = (select * from t2 union all select 1 from t1);
ERROR 42X38: 'SELECT *' only allowed in EXISTS and NOT EXISTS subqueries.
ij> select i from t1 where i = (select 1 from t2 union all select * from t1);
ERROR 42X38: 'SELECT *' only allowed in EXISTS and NOT EXISTS subqueries.
ij> -- too many columns
select i from t1 where i = (values (1, 2, 3) union all values (1, 2, 3));
ERROR 42X39: Subquery is only allowed to return a single column.
ij> select i from t1 where i = (select i, s from t2 union all select i, s from t1);
ERROR 42X39: Subquery is only allowed to return a single column.
ij> -- cardinality violation
select i from t1 where i = (values 1 union all values 1);
ERROR 21000: Scalar subquery is only allowed to return a single row.
ij> -- both sides of union have same type, which is incompatible with LHS
select i from t1 where i in (select date('1999-02-04') from t2 union all select date('1999-03-08') from t2);
ERROR 42818: Comparisons between 'INTEGER' and 'DATE' are not supported. Types must be comparable. String types must also have matching collation. If collation does not match, a possible solution is to cast operands to force them to the default collation (e.g. SELECT tablename FROM sys.systables WHERE CAST(tablename AS VARCHAR(128)) = 'T1')
ij> -- positive tests

-- expression subquery
select i from t1 where i = (select i from t2 where 1 = 0 union all values 1);
I          
-----------
1          
ij> -- in subquery
select i from t1 where i in (select i from t2 union all values 1 union all values 2);
I          
-----------
1          
2          
ij> select i from t1 where i in 
		(select a from (select i from t2 union all values 1 union all values 2) a (a));
I          
-----------
1          
2          
ij> -- not in subquery
select i from t1 where i not in (select i from t2 union all values 1 union all values 2);
I          
-----------
ij> select i from t1 where i not in (select i from t2 where i is not null union all 
								 values 1 union all values 22);
I          
-----------
2          
ij> select i from t1 where i not in 
		(select a from (select i from t2 where i is not null union all 
						values 111 union all values 2) a (a));
I          
-----------
1          
ij> -- correlated union subquery
select i from t1 a where i in (select i from t2 where 1 = 0 union all
							   select a.i from t2 where a.i < i);
I          
-----------
1          
2          
ij> select i from t1 a where i in (select a.i from t2 where a.i < i union all
							   select i from t2 where 1 < 0);
I          
-----------
1          
2          
ij> -- exists subquery
select i from t1 where exists (select * from t2 union all select * from t2);
I          
-----------
NULL       
1          
2          
ij> select i from t1 where exists (select 1 from t2 union all select 2 from t2);
I          
-----------
NULL       
1          
2          
ij> select i from t1 where exists (select 1 from t2 where 1 = 0 union all
							   select 2 from t2 where t1.i < i);
I          
-----------
1          
2          
ij> select i from t1 where exists (select i from t2 where t1.i < i union all
							    select i from t2 where 1 = 0 union all
							   select i from t2 where t1.i < i union all
							    select i from t2 where 1 = 0);
I          
-----------
1          
2          
ij> -- These next two should fail because left/right children do not have
-- the same number of result columns.
select i from t1 where exists (select 1 from t2 where 1 = 0 union all
							   select * from t2 where t1.i < i);
ERROR 42X58: The number of columns on the left and right sides of the UNION must be the same.
ij> select i from t1 where exists (select i from t2 where t1.i < i union all
							    select * from t2 where 1 = 0 union all
							   select * from t2 where t1.i < i union all
							    select i from t2 where 1 = 0);
ERROR 42X58: The number of columns on the left and right sides of the UNION must be the same.
ij> -- insert tests
create table insert_test (i int, s smallint, d double precision, r real, c10 char(10),
				 c30 char(30), vc10 varchar(10), vc30 varchar(30));
0 rows inserted/updated/deleted
ij> -- simple tests
insert into insert_test select * from t1 union all select * from t2;
6 rows inserted/updated/deleted
ij> select * from insert_test;
I          |S     |D                       |R              |C10       |C30                           |VC10      |VC30                          
-----------------------------------------------------------------------------------------------------------------------------------------------
NULL       |NULL  |NULL                    |NULL           |NULL      |NULL                          |NULL      |NULL                          
1          |1     |10.0                    |10.0           |11111     |11111     11                  |11111     |11111      11                 
2          |2     |20.0                    |20.0           |22222     |22222     22                  |22222     |22222      22                 
NULL       |NULL  |NULL                    |NULL           |NULL      |NULL                          |NULL      |NULL                          
3          |3     |30.0                    |30.0           |33333     |33333     33                  |33333     |33333      33                 
4          |4     |40.0                    |40.0           |44444     |44444     44                  |44444     |44444      44                 
ij> delete from insert_test;
6 rows inserted/updated/deleted
ij> insert into insert_test (s, i) values (2, 1) union all values (4, 3);
2 rows inserted/updated/deleted
ij> select * from insert_test;
I          |S     |D                       |R              |C10       |C30                           |VC10      |VC30                          
-----------------------------------------------------------------------------------------------------------------------------------------------
1          |2     |NULL                    |NULL           |NULL      |NULL                          |NULL      |NULL                          
3          |4     |NULL                    |NULL           |NULL      |NULL                          |NULL      |NULL                          
ij> delete from insert_test;
2 rows inserted/updated/deleted
ij> -- type conversions between union all and target table
insert into insert_test select s, i, r, d, vc10, vc30, c10, c30 from t1 union all
						select s, i, r, d, vc10, vc30, c10, vc30 from t2;
6 rows inserted/updated/deleted
ij> select * from insert_test;
I          |S     |D                       |R              |C10       |C30                           |VC10      |VC30                          
-----------------------------------------------------------------------------------------------------------------------------------------------
NULL       |NULL  |NULL                    |NULL           |NULL      |NULL                          |NULL      |NULL                          
1          |1     |10.0                    |10.0           |11111     |11111      11                 |11111     |11111     11                  
2          |2     |20.0                    |20.0           |22222     |22222      22                 |22222     |22222     22                  
NULL       |NULL  |NULL                    |NULL           |NULL      |NULL                          |NULL      |NULL                          
3          |3     |30.0                    |30.0           |33333     |33333      33                 |33333     |33333      33                 
4          |4     |40.0                    |40.0           |44444     |44444      44                 |44444     |44444      44                 
ij> delete from insert_test;
6 rows inserted/updated/deleted
ij> -- test type dominance/length/nullability
select vc10 from t1 union all select c30 from t2;
1                             
------------------------------
NULL                          
11111                         
22222                         
NULL                          
33333     33                  
44444     44                  
ij> insert into insert_test (vc30) select vc10 from t1 union all select c30 from t2;
6 rows inserted/updated/deleted
ij> select * from insert_test;
I          |S     |D                       |R              |C10       |C30                           |VC10      |VC30                          
-----------------------------------------------------------------------------------------------------------------------------------------------
NULL       |NULL  |NULL                    |NULL           |NULL      |NULL                          |NULL      |NULL                          
NULL       |NULL  |NULL                    |NULL           |NULL      |NULL                          |NULL      |11111                         
NULL       |NULL  |NULL                    |NULL           |NULL      |NULL                          |NULL      |22222                         
NULL       |NULL  |NULL                    |NULL           |NULL      |NULL                          |NULL      |NULL                          
NULL       |NULL  |NULL                    |NULL           |NULL      |NULL                          |NULL      |33333     33                  
NULL       |NULL  |NULL                    |NULL           |NULL      |NULL                          |NULL      |44444     44                  
ij> delete from insert_test;
6 rows inserted/updated/deleted
ij> insert into insert_test (c30)
	select vc10 from t1
	union all
	select c30 from t2
	union all
	select c10 from t1;
9 rows inserted/updated/deleted
ij> select * from insert_test;
I          |S     |D                       |R              |C10       |C30                           |VC10      |VC30                          
-----------------------------------------------------------------------------------------------------------------------------------------------
NULL       |NULL  |NULL                    |NULL           |NULL      |NULL                          |NULL      |NULL                          
NULL       |NULL  |NULL                    |NULL           |NULL      |11111                         |NULL      |NULL                          
NULL       |NULL  |NULL                    |NULL           |NULL      |22222                         |NULL      |NULL                          
NULL       |NULL  |NULL                    |NULL           |NULL      |NULL                          |NULL      |NULL                          
NULL       |NULL  |NULL                    |NULL           |NULL      |33333     33                  |NULL      |NULL                          
NULL       |NULL  |NULL                    |NULL           |NULL      |44444     44                  |NULL      |NULL                          
NULL       |NULL  |NULL                    |NULL           |NULL      |NULL                          |NULL      |NULL                          
NULL       |NULL  |NULL                    |NULL           |NULL      |11111                         |NULL      |NULL                          
NULL       |NULL  |NULL                    |NULL           |NULL      |22222                         |NULL      |NULL                          
ij> delete from insert_test;
9 rows inserted/updated/deleted
ij> -- test NormalizeResultSet generation
select i, d from t1 union all select d, i from t2;
1                       |2                       
-------------------------------------------------
NULL                    |NULL                    
1.0                     |10.0                    
2.0                     |20.0                    
NULL                    |NULL                    
30.0                    |3.0                     
40.0                    |4.0                     
ij> select vc10, c30 from t1 union all select c30,  vc10 from t2;
1                             |2                             
-------------------------------------------------------------
NULL                          |NULL                          
11111                         |11111     11                  
22222                         |22222     22                  
NULL                          |NULL                          
33333     33                  |33333                         
44444     44                  |44444                         
ij> create table insert_test2 (s smallint not null, vc30 varchar(30) not null);
0 rows inserted/updated/deleted
ij> -- the following should fail due to null constraint
insert into insert_test2 select s, c10 from t1 union all select s, c30 from t2;
ERROR 23502: Column 'S'  cannot accept a NULL value.
ij> select * from insert_test2;
S     |VC30                          
-------------------------------------
ij> -- negative tests
-- ? in select list of union
select ? from insert_test union all select vc30 from insert_test;
ERROR 42X34: There is a ? parameter in the select list.  This is not allowed.
ij> select vc30 from insert_test union all select ? from insert_test;
ERROR 42X34: There is a ? parameter in the select list.  This is not allowed.
ij> -- DB2 requires matching target and result columns
insert into insert_test values (1, 2) union all values (3, 4);
ERROR 42802: The number of values assigned is not the same as the number of specified or implied columns.
ij> -- Beetle 4454 - test multiple union alls in a subquery
select vc10 from (select vc10 from t1 union all
select vc10 from t1 union all
select vc10 from t1 union all
select vc10 from t1 union all
select vc10 from t1 union all
select vc10 from t1 union all
select vc10 from t1) t;
VC10      
----------
NULL      
11111     
22222     
NULL      
11111     
22222     
NULL      
11111     
22222     
NULL      
11111     
22222     
NULL      
11111     
22222     
NULL      
11111     
22222     
NULL      
11111     
22222     
ij> -- force union all on right side
select vc10 from (select vc10 from t1 union all (select vc10 from t1 union all
select vc10 from t1)) t;
VC10      
----------
NULL      
11111     
22222     
NULL      
11111     
22222     
NULL      
11111     
22222     
ij> -- drop the tables
drop table t1;
0 rows inserted/updated/deleted
ij> drop table t2;
0 rows inserted/updated/deleted
ij> drop table insert_test;
0 rows inserted/updated/deleted
ij> drop table insert_test2;
0 rows inserted/updated/deleted
ij> -- DERBY-1967
-- NULLIF with UNION throws SQLSTATE 23502.

create table a (f1 varchar(10));
0 rows inserted/updated/deleted
ij> create table b (f2 varchar(10));
0 rows inserted/updated/deleted
ij> insert into b values('test');
1 row inserted/updated/deleted
ij> -- this used to throw 23502
select nullif('x','x') as f0, f1 from a
   union all
   select nullif('x','x') as f0, nullif('x','x') as f1 from b;
F0  |F1        
---------------
NULL|NULL      
ij> drop table a;
0 rows inserted/updated/deleted
ij> drop table b;
0 rows inserted/updated/deleted
ij> create table a (f1 int);
0 rows inserted/updated/deleted
ij> create table b (f2 int);
0 rows inserted/updated/deleted
ij> insert into b values(1);
1 row inserted/updated/deleted
ij> -- ok
select nullif('x','x') as f0, f1 from a
   union all
   select nullif('x','x') as f0, nullif(1,1) as f1 from b;
F0  |F1         
----------------
NULL|NULL       
ij> drop table a;
0 rows inserted/updated/deleted
ij> drop table b;
0 rows inserted/updated/deleted
ij> -- DERBY-681. Check union with group by/having
create table o (name varchar(20), ord int);
0 rows inserted/updated/deleted
ij> create table a (ord int, amount int);
0 rows inserted/updated/deleted
ij> create view v1 (vx, vy) 
as select name, sum(ord) from o where ord > 0 group by name, ord
    having ord <= ANY (select ord from a);
0 rows inserted/updated/deleted
ij> select vx, vy from v1
     union select vx, sum(vy) from v1 group by vx, vy having (vy / 2) > 15;
VX                  |2          
--------------------------------
ij> drop view v1;
0 rows inserted/updated/deleted
ij> drop table o;
0 rows inserted/updated/deleted
ij> drop table a;
0 rows inserted/updated/deleted
ij> -- DERBY-1852: Incorrect results when a UNION U1 (with no "ALL") appears
-- in the FROM list of a SELECT query, AND there are duplicate rows
-- across the left and/or right result sets of U1, AND U1 is the left or
-- right child of another set operator.

create table t1 (i int, j int);
0 rows inserted/updated/deleted
ij> create table t2 (i int, j int);
0 rows inserted/updated/deleted
ij> insert into t1 values (1, 2), (2, 4), (3, 6), (4, 8), (5, 10);
5 rows inserted/updated/deleted
ij> insert into t2 values (1, 2), (2, -4), (3, 6), (4, -8), (5, 10);
5 rows inserted/updated/deleted
ij> insert into t2 values (3, 6), (4, 8), (3, -6), (4, -8);
4 rows inserted/updated/deleted
ij> -- U1 is left child of another UNION; top-level query.
select * from t1 union select * from t2 union select * from t1;
I          |J          
-----------------------
1          |2          
2          |-4         
2          |4          
3          |-6         
3          |6          
4          |-8         
4          |8          
5          |10         
ij> -- U1 is left child of another UNION; subquery in FROM list.
select * from
  (select * from t1 union select * from t2 union select * from t1) x;
I          |J          
-----------------------
1          |2          
2          |-4         
2          |4          
3          |-6         
3          |6          
4          |-8         
4          |8          
5          |10         
ij> -- Same kind of thing, but in the form of a view (which is a
-- more likely use-ccase).
create view uv as
  select * from t1 union select * from t2 union select * from t1;
0 rows inserted/updated/deleted
ij> select * from uv;
I          |J          
-----------------------
1          |2          
2          |-4         
2          |4          
3          |-6         
3          |6          
4          |-8         
4          |8          
5          |10         
ij> drop view uv;
0 rows inserted/updated/deleted
ij> -- U1 is left child of a UNION ALL; top-level query.
select * from t1 union select * from t2 union all select * from t1;
I          |J          
-----------------------
1          |2          
2          |-4         
2          |4          
3          |-6         
3          |6          
4          |-8         
4          |8          
5          |10         
1          |2          
2          |4          
3          |6          
4          |8          
5          |10         
ij> -- U1 is left child of a UNION ALL; subquery in FROM list.
select * from
  (select * from t1 union select * from t2 union all select * from t1) x;
I          |J          
-----------------------
1          |2          
2          |-4         
2          |4          
3          |-6         
3          |6          
4          |-8         
4          |8          
5          |10         
1          |2          
2          |4          
3          |6          
4          |8          
5          |10         
ij> -- U1 is left child of an EXCEPT; top-level query.
select * from t1 union select * from t2 except select * from t1;
I          |J          
-----------------------
2          |-4         
3          |-6         
4          |-8         
ij> -- U1 is left child of an EXCEPT; subquery in FROM list.
select * from
  (select * from t1 union select * from t2 except select * from t1) x;
I          |J          
-----------------------
2          |-4         
3          |-6         
4          |-8         
ij> -- U1 is left child of an EXCEPT ALL; top-level query.
select * from t1 union select * from t2 except all select * from t1;
I          |J          
-----------------------
2          |-4         
3          |-6         
4          |-8         
ij> -- U1 is left child of an EXCEPT ALL; subquery in FROM list.
select * from
  (select * from t1 union select * from t2 except all select * from t1) x;
I          |J          
-----------------------
2          |-4         
3          |-6         
4          |-8         
ij> -- U1 is left child of an INTERSECT; top-level query.
-- Note: intersect has higher precedence than union so we have to use
-- quotes to force the UNION to be a child of the intersect.
(select * from t1 union select * from t2) intersect select * from t2;
I          |J          
-----------------------
1          |2          
2          |-4         
3          |-6         
3          |6          
4          |-8         
4          |8          
5          |10         
ij> -- U1 is left child of an INTERSECT; subquery in FROM list.
create view iv as
  (select * from t1 union select * from t2) intersect select * from t2;
0 rows inserted/updated/deleted
ij> select * from iv;
I          |J          
-----------------------
1          |2          
2          |-4         
3          |-6         
3          |6          
4          |-8         
4          |8          
5          |10         
ij> drop view iv;
0 rows inserted/updated/deleted
ij> -- U1 is left child of an INTERSECT ALL; top-level query.
(select * from t1 union select * from t2) intersect all select * from t2;
I          |J          
-----------------------
1          |2          
2          |-4         
3          |-6         
3          |6          
4          |-8         
4          |8          
5          |10         
ij> -- U1 is left child of an INTERSECT ALL; subquery in FROM list.
create view iv as
  (select * from t1 union select * from t2) intersect all select * from t2;
0 rows inserted/updated/deleted
ij> select * from iv;
I          |J          
-----------------------
1          |2          
2          |-4         
3          |-6         
3          |6          
4          |-8         
4          |8          
5          |10         
ij> drop view iv;
0 rows inserted/updated/deleted
ij> -- Just as a sanity check, make sure things work if U1 is a child of
-- an explicit JoinNode (since JoinNode is an instanceof TableOperatorNode
-- and TableOperatorNode is where the bug for DERBY-1852 was fixed).
select * from
  (select * from t1 union select * from t2) x2 left join t2 on x2.i = t2.i;
I          |J          |I          |J          
-----------------------------------------------
1          |2          |1          |2          
2          |-4         |2          |-4         
2          |4          |2          |-4         
3          |-6         |3          |6          
3          |-6         |3          |6          
3          |-6         |3          |-6         
3          |6          |3          |6          
3          |6          |3          |6          
3          |6          |3          |-6         
4          |-8         |4          |-8         
4          |-8         |4          |8          
4          |-8         |4          |-8         
4          |8          |4          |-8         
4          |8          |4          |8          
4          |8          |4          |-8         
5          |10         |5          |10         
ij> -- cleanup.
drop table t1;
0 rows inserted/updated/deleted
ij> drop table t2;
0 rows inserted/updated/deleted
ij> -- Regression test for DERBY-4391. These UNION queries used to throw a
-- NullPointerException during compilation. Now all of them should compile
-- successfully, but some of them fail during execution if their subqueries
-- return more than one row.
create table d4391(a int not null primary key, b int);
0 rows inserted/updated/deleted
ij> insert into d4391 values (0, 4), (1, 3), (2, 2), (3, 1), (4, 0);
5 rows inserted/updated/deleted
ij> select * from d4391 where a < (values 2 union values 2);
A          |B          
-----------------------
0          |4          
1          |3          
ij> select * from d4391 where a < (select 4 from d4391 union select b from d4391);
ERROR 21000: Scalar subquery is only allowed to return a single row.
ij> select * from d4391 where a < (select a+b from d4391 union select 4 from d4391);
A          |B          
-----------------------
0          |4          
1          |3          
2          |2          
3          |1          
ij> select * from d4391 where a < (select a+b from d4391 union select a from d4391);
ERROR 21000: Scalar subquery is only allowed to return a single row.
ij> select * from d4391 where a < (select sum(a) from d4391 union select sum(b) from d4391);
A          |B          
-----------------------
0          |4          
1          |3          
2          |2          
3          |1          
4          |0          
ij> drop table d4391;
0 rows inserted/updated/deleted
ij> -- Regression test for DERBY-4411. The predicate 1=0 used to be lost when the
-- SELECT statement was compiled, and the statement would fail with a message
-- saying that a scalar sub-query should return exactly one row.
create table d4411(a int primary key, b int);
0 rows inserted/updated/deleted
ij> insert into d4411 values (0, 4), (1, 3), (2, 2), (3, 1), (4, 0);
5 rows inserted/updated/deleted
ij> select * from d4411 where a < (values 2 union select b from d4411 where 1=0);
A          |B          
-----------------------
0          |4          
1          |3          
ij> drop table d4411;
0 rows inserted/updated/deleted
ij>