xref: /sqlite-3.40.0/test/select4.test (revision a276e3fd)
1# 2001 September 15
2#
3# The author disclaims copyright to this source code.  In place of
4# a legal notice, here is a blessing:
5#
6#    May you do good and not evil.
7#    May you find forgiveness for yourself and forgive others.
8#    May you share freely, never taking more than you give.
9#
10#***********************************************************************
11# This file implements regression tests for SQLite library.  The
12# focus of this file is testing UNION, INTERSECT and EXCEPT operators
13# in SELECT statements.
14#
15# $Id: select4.test,v 1.30 2009/04/16 00:24:24 drh Exp $
16
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19
20# Most tests in this file depend on compound-select. But there are a couple
21# right at the end that test DISTINCT, so we cannot omit the entire file.
22#
23ifcapable compound {
24
25# Build some test data
26#
27execsql {
28  CREATE TABLE t1(n int, log int);
29  BEGIN;
30}
31for {set i 1} {$i<32} {incr i} {
32  for {set j 0} {(1<<$j)<$i} {incr j} {}
33  execsql "INSERT INTO t1 VALUES($i,$j)"
34}
35execsql {
36  COMMIT;
37}
38
39do_test select4-1.0 {
40  execsql {SELECT DISTINCT log FROM t1 ORDER BY log}
41} {0 1 2 3 4 5}
42
43# Union All operator
44#
45do_test select4-1.1a {
46  lsort [execsql {SELECT DISTINCT log FROM t1}]
47} {0 1 2 3 4 5}
48do_test select4-1.1b {
49  lsort [execsql {SELECT n FROM t1 WHERE log=3}]
50} {5 6 7 8}
51do_test select4-1.1c {
52  execsql {
53    SELECT DISTINCT log FROM t1
54    UNION ALL
55    SELECT n FROM t1 WHERE log=3
56    ORDER BY log;
57  }
58} {0 1 2 3 4 5 5 6 7 8}
59do_test select4-1.1d {
60  execsql {
61    CREATE TABLE t2 AS
62      SELECT DISTINCT log FROM t1
63      UNION ALL
64      SELECT n FROM t1 WHERE log=3
65      ORDER BY log;
66    SELECT * FROM t2;
67  }
68} {0 1 2 3 4 5 5 6 7 8}
69execsql {DROP TABLE t2}
70do_test select4-1.1e {
71  execsql {
72    CREATE TABLE t2 AS
73      SELECT DISTINCT log FROM t1
74      UNION ALL
75      SELECT n FROM t1 WHERE log=3
76      ORDER BY log DESC;
77    SELECT * FROM t2;
78  }
79} {8 7 6 5 5 4 3 2 1 0}
80execsql {DROP TABLE t2}
81do_test select4-1.1f {
82  execsql {
83    SELECT DISTINCT log FROM t1
84    UNION ALL
85    SELECT n FROM t1 WHERE log=2
86  }
87} {0 1 2 3 4 5 3 4}
88do_test select4-1.1g {
89  execsql {
90    CREATE TABLE t2 AS
91      SELECT DISTINCT log FROM t1
92      UNION ALL
93      SELECT n FROM t1 WHERE log=2;
94    SELECT * FROM t2;
95  }
96} {0 1 2 3 4 5 3 4}
97execsql {DROP TABLE t2}
98ifcapable subquery {
99  do_test select4-1.2 {
100    execsql {
101      SELECT log FROM t1 WHERE n IN
102        (SELECT DISTINCT log FROM t1 UNION ALL
103         SELECT n FROM t1 WHERE log=3)
104      ORDER BY log;
105    }
106  } {0 1 2 2 3 3 3 3}
107}
108
109# EVIDENCE-OF: R-02644-22131 In a compound SELECT statement, only the
110# last or right-most simple SELECT may have an ORDER BY clause.
111#
112do_test select4-1.3 {
113  set v [catch {execsql {
114    SELECT DISTINCT log FROM t1 ORDER BY log
115    UNION ALL
116    SELECT n FROM t1 WHERE log=3
117    ORDER BY log;
118  }} msg]
119  lappend v $msg
120} {1 {ORDER BY clause should come after UNION ALL not before}}
121do_catchsql_test select4-1.4 {
122  SELECT (VALUES(0) INTERSECT SELECT(0) UNION SELECT(0) ORDER BY (0) UNION
123          SELECT 0 UNION SELECT 0 ORDER BY 1);
124} {1 {ORDER BY clause should come after UNION not before}}
125
126# Union operator
127#
128do_test select4-2.1 {
129  execsql {
130    SELECT DISTINCT log FROM t1
131    UNION
132    SELECT n FROM t1 WHERE log=3
133    ORDER BY log;
134  }
135} {0 1 2 3 4 5 6 7 8}
136ifcapable subquery {
137  do_test select4-2.2 {
138    execsql {
139      SELECT log FROM t1 WHERE n IN
140        (SELECT DISTINCT log FROM t1 UNION
141         SELECT n FROM t1 WHERE log=3)
142      ORDER BY log;
143    }
144  } {0 1 2 2 3 3 3 3}
145}
146do_test select4-2.3 {
147  set v [catch {execsql {
148    SELECT DISTINCT log FROM t1 ORDER BY log
149    UNION
150    SELECT n FROM t1 WHERE log=3
151    ORDER BY log;
152  }} msg]
153  lappend v $msg
154} {1 {ORDER BY clause should come after UNION not before}}
155
156# Except operator
157#
158do_test select4-3.1.1 {
159  execsql {
160    SELECT DISTINCT log FROM t1
161    EXCEPT
162    SELECT n FROM t1 WHERE log=3
163    ORDER BY log;
164  }
165} {0 1 2 3 4}
166do_test select4-3.1.2 {
167  execsql {
168    CREATE TABLE t2 AS
169      SELECT DISTINCT log FROM t1
170      EXCEPT
171      SELECT n FROM t1 WHERE log=3
172      ORDER BY log;
173    SELECT * FROM t2;
174  }
175} {0 1 2 3 4}
176execsql {DROP TABLE t2}
177do_test select4-3.1.3 {
178  execsql {
179    CREATE TABLE t2 AS
180      SELECT DISTINCT log FROM t1
181      EXCEPT
182      SELECT n FROM t1 WHERE log=3
183      ORDER BY log DESC;
184    SELECT * FROM t2;
185  }
186} {4 3 2 1 0}
187execsql {DROP TABLE t2}
188ifcapable subquery {
189  do_test select4-3.2 {
190    execsql {
191      SELECT log FROM t1 WHERE n IN
192        (SELECT DISTINCT log FROM t1 EXCEPT
193         SELECT n FROM t1 WHERE log=3)
194      ORDER BY log;
195    }
196  } {0 1 2 2}
197}
198do_test select4-3.3 {
199  set v [catch {execsql {
200    SELECT DISTINCT log FROM t1 ORDER BY log
201    EXCEPT
202    SELECT n FROM t1 WHERE log=3
203    ORDER BY log;
204  }} msg]
205  lappend v $msg
206} {1 {ORDER BY clause should come after EXCEPT not before}}
207
208# Intersect operator
209#
210do_test select4-4.1.1 {
211  execsql {
212    SELECT DISTINCT log FROM t1
213    INTERSECT
214    SELECT n FROM t1 WHERE log=3
215    ORDER BY log;
216  }
217} {5}
218
219do_test select4-4.1.2 {
220  execsql {
221    SELECT DISTINCT log FROM t1
222    UNION ALL
223    SELECT 6
224    INTERSECT
225    SELECT n FROM t1 WHERE log=3
226    ORDER BY t1.log;
227  }
228} {5 6}
229
230do_test select4-4.1.3 {
231  execsql {
232    CREATE TABLE t2 AS
233      SELECT DISTINCT log FROM t1 UNION ALL SELECT 6
234      INTERSECT
235      SELECT n FROM t1 WHERE log=3
236      ORDER BY log;
237    SELECT * FROM t2;
238  }
239} {5 6}
240execsql {DROP TABLE t2}
241do_test select4-4.1.4 {
242  execsql {
243    CREATE TABLE t2 AS
244      SELECT DISTINCT log FROM t1 UNION ALL SELECT 6
245      INTERSECT
246      SELECT n FROM t1 WHERE log=3
247      ORDER BY log DESC;
248    SELECT * FROM t2;
249  }
250} {6 5}
251execsql {DROP TABLE t2}
252ifcapable subquery {
253  do_test select4-4.2 {
254    execsql {
255      SELECT log FROM t1 WHERE n IN
256        (SELECT DISTINCT log FROM t1 INTERSECT
257         SELECT n FROM t1 WHERE log=3)
258      ORDER BY log;
259    }
260  } {3}
261}
262do_test select4-4.3 {
263  set v [catch {execsql {
264    SELECT DISTINCT log FROM t1 ORDER BY log
265    INTERSECT
266    SELECT n FROM t1 WHERE log=3
267    ORDER BY log;
268  }} msg]
269  lappend v $msg
270} {1 {ORDER BY clause should come after INTERSECT not before}}
271
272# Various error messages while processing UNION or INTERSECT
273#
274do_test select4-5.1 {
275  set v [catch {execsql {
276    SELECT DISTINCT log FROM t2
277    UNION ALL
278    SELECT n FROM t1 WHERE log=3
279    ORDER BY log;
280  }} msg]
281  lappend v $msg
282} {1 {no such table: t2}}
283do_test select4-5.2 {
284  set v [catch {execsql {
285    SELECT DISTINCT log AS "xyzzy" FROM t1
286    UNION ALL
287    SELECT n FROM t1 WHERE log=3
288    ORDER BY xyzzy;
289  }} msg]
290  lappend v $msg
291} {0 {0 1 2 3 4 5 5 6 7 8}}
292do_test select4-5.2b {
293  set v [catch {execsql {
294    SELECT DISTINCT log AS xyzzy FROM t1
295    UNION ALL
296    SELECT n FROM t1 WHERE log=3
297    ORDER BY "xyzzy";
298  }} msg]
299  lappend v $msg
300} {0 {0 1 2 3 4 5 5 6 7 8}}
301do_test select4-5.2c {
302  set v [catch {execsql {
303    SELECT DISTINCT log FROM t1
304    UNION ALL
305    SELECT n FROM t1 WHERE log=3
306    ORDER BY "xyzzy";
307  }} msg]
308  lappend v $msg
309} {1 {1st ORDER BY term does not match any column in the result set}}
310do_test select4-5.2d {
311  set v [catch {execsql {
312    SELECT DISTINCT log FROM t1
313    INTERSECT
314    SELECT n FROM t1 WHERE log=3
315    ORDER BY "xyzzy";
316  }} msg]
317  lappend v $msg
318} {1 {1st ORDER BY term does not match any column in the result set}}
319do_test select4-5.2e {
320  set v [catch {execsql {
321    SELECT DISTINCT log FROM t1
322    UNION ALL
323    SELECT n FROM t1 WHERE log=3
324    ORDER BY n;
325  }} msg]
326  lappend v $msg
327} {0 {0 1 2 3 4 5 5 6 7 8}}
328do_test select4-5.2f {
329  catchsql {
330    SELECT DISTINCT log FROM t1
331    UNION ALL
332    SELECT n FROM t1 WHERE log=3
333    ORDER BY log;
334  }
335} {0 {0 1 2 3 4 5 5 6 7 8}}
336do_test select4-5.2g {
337  catchsql {
338    SELECT DISTINCT log FROM t1
339    UNION ALL
340    SELECT n FROM t1 WHERE log=3
341    ORDER BY 1;
342  }
343} {0 {0 1 2 3 4 5 5 6 7 8}}
344do_test select4-5.2h {
345  catchsql {
346    SELECT DISTINCT log FROM t1
347    UNION ALL
348    SELECT n FROM t1 WHERE log=3
349    ORDER BY 2;
350  }
351} {1 {1st ORDER BY term out of range - should be between 1 and 1}}
352do_test select4-5.2i {
353  catchsql {
354    SELECT DISTINCT 1, log FROM t1
355    UNION ALL
356    SELECT 2, n FROM t1 WHERE log=3
357    ORDER BY 2, 1;
358  }
359} {0 {1 0 1 1 1 2 1 3 1 4 1 5 2 5 2 6 2 7 2 8}}
360do_test select4-5.2j {
361  catchsql {
362    SELECT DISTINCT 1, log FROM t1
363    UNION ALL
364    SELECT 2, n FROM t1 WHERE log=3
365    ORDER BY 1, 2 DESC;
366  }
367} {0 {1 5 1 4 1 3 1 2 1 1 1 0 2 8 2 7 2 6 2 5}}
368do_test select4-5.2k {
369  catchsql {
370    SELECT DISTINCT 1, log FROM t1
371    UNION ALL
372    SELECT 2, n FROM t1 WHERE log=3
373    ORDER BY n, 1;
374  }
375} {0 {1 0 1 1 1 2 1 3 1 4 1 5 2 5 2 6 2 7 2 8}}
376do_test select4-5.3 {
377  set v [catch {execsql {
378    SELECT DISTINCT log, n FROM t1
379    UNION ALL
380    SELECT n FROM t1 WHERE log=3
381    ORDER BY log;
382  }} msg]
383  lappend v $msg
384} {1 {SELECTs to the left and right of UNION ALL do not have the same number of result columns}}
385do_test select4-5.3-3807-1 {
386  catchsql {
387    SELECT 1 UNION SELECT 2, 3 UNION SELECT 4, 5 ORDER BY 1;
388  }
389} {1 {SELECTs to the left and right of UNION do not have the same number of result columns}}
390do_test select4-5.4 {
391  set v [catch {execsql {
392    SELECT log FROM t1 WHERE n=2
393    UNION ALL
394    SELECT log FROM t1 WHERE n=3
395    UNION ALL
396    SELECT log FROM t1 WHERE n=4
397    UNION ALL
398    SELECT log FROM t1 WHERE n=5
399    ORDER BY log;
400  }} msg]
401  lappend v $msg
402} {0 {1 2 2 3}}
403
404do_test select4-6.1 {
405  execsql {
406    SELECT log, count(*) as cnt FROM t1 GROUP BY log
407    UNION
408    SELECT log, n FROM t1 WHERE n=7
409    ORDER BY cnt, log;
410  }
411} {0 1 1 1 2 2 3 4 3 7 4 8 5 15}
412do_test select4-6.2 {
413  execsql {
414    SELECT log, count(*) FROM t1 GROUP BY log
415    UNION
416    SELECT log, n FROM t1 WHERE n=7
417    ORDER BY count(*), log;
418  }
419} {0 1 1 1 2 2 3 4 3 7 4 8 5 15}
420
421# NULLs are indistinct for the UNION operator.
422# Make sure the UNION operator recognizes this
423#
424do_test select4-6.3 {
425  execsql {
426    SELECT NULL UNION SELECT NULL UNION
427    SELECT 1 UNION SELECT 2 AS 'x'
428    ORDER BY x;
429  }
430} {{} 1 2}
431do_test select4-6.3.1 {
432  execsql {
433    SELECT NULL UNION ALL SELECT NULL UNION ALL
434    SELECT 1 UNION ALL SELECT 2 AS 'x'
435    ORDER BY x;
436  }
437} {{} {} 1 2}
438
439# Make sure the DISTINCT keyword treats NULLs as indistinct.
440#
441ifcapable subquery {
442  do_test select4-6.4 {
443    execsql {
444      SELECT * FROM (
445         SELECT NULL, 1 UNION ALL SELECT NULL, 1
446      );
447    }
448  } {{} 1 {} 1}
449  do_test select4-6.5 {
450    execsql {
451      SELECT DISTINCT * FROM (
452         SELECT NULL, 1 UNION ALL SELECT NULL, 1
453      );
454    }
455  } {{} 1}
456  do_test select4-6.6 {
457    execsql {
458      SELECT DISTINCT * FROM (
459         SELECT 1,2  UNION ALL SELECT 1,2
460      );
461    }
462  } {1 2}
463}
464
465# Test distinctness of NULL in other ways.
466#
467do_test select4-6.7 {
468  execsql {
469    SELECT NULL EXCEPT SELECT NULL
470  }
471} {}
472
473
474# Make sure column names are correct when a compound select appears as
475# an expression in the WHERE clause.
476#
477do_test select4-7.1 {
478  execsql {
479    CREATE TABLE t2 AS SELECT log AS 'x', count(*) AS 'y' FROM t1 GROUP BY log;
480    SELECT * FROM t2 ORDER BY x;
481  }
482} {0 1 1 1 2 2 3 4 4 8 5 15}
483ifcapable subquery {
484  do_test select4-7.2 {
485    execsql2 {
486      SELECT * FROM t1 WHERE n IN (SELECT n FROM t1 INTERSECT SELECT x FROM t2)
487      ORDER BY n
488    }
489  } {n 1 log 0 n 2 log 1 n 3 log 2 n 4 log 2 n 5 log 3}
490  do_test select4-7.3 {
491    execsql2 {
492      SELECT * FROM t1 WHERE n IN (SELECT n FROM t1 EXCEPT SELECT x FROM t2)
493      ORDER BY n LIMIT 2
494    }
495  } {n 6 log 3 n 7 log 3}
496  do_test select4-7.4 {
497    execsql2 {
498      SELECT * FROM t1 WHERE n IN (SELECT n FROM t1 UNION SELECT x FROM t2)
499      ORDER BY n LIMIT 2
500    }
501  } {n 1 log 0 n 2 log 1}
502} ;# ifcapable subquery
503
504} ;# ifcapable compound
505
506# Make sure DISTINCT works appropriately on TEXT and NUMERIC columns.
507do_test select4-8.1 {
508  execsql {
509    BEGIN;
510    CREATE TABLE t3(a text, b float, c text);
511    INSERT INTO t3 VALUES(1, 1.1, '1.1');
512    INSERT INTO t3 VALUES(2, 1.10, '1.10');
513    INSERT INTO t3 VALUES(3, 1.10, '1.1');
514    INSERT INTO t3 VALUES(4, 1.1, '1.10');
515    INSERT INTO t3 VALUES(5, 1.2, '1.2');
516    INSERT INTO t3 VALUES(6, 1.3, '1.3');
517    COMMIT;
518  }
519  execsql {
520    SELECT DISTINCT b FROM t3 ORDER BY c;
521  }
522} {1.1 1.2 1.3}
523do_test select4-8.2 {
524  execsql {
525    SELECT DISTINCT c FROM t3 ORDER BY c;
526  }
527} {1.1 1.10 1.2 1.3}
528
529# Make sure the names of columns are taken from the right-most subquery
530# right in a compound query.  Ticket #1721
531#
532ifcapable compound {
533
534do_test select4-9.1 {
535  execsql2 {
536    SELECT x, y FROM t2 UNION SELECT a, b FROM t3 ORDER BY x LIMIT 1
537  }
538} {x 0 y 1}
539do_test select4-9.2 {
540  execsql2 {
541    SELECT x, y FROM t2 UNION ALL SELECT a, b FROM t3 ORDER BY x LIMIT 1
542  }
543} {x 0 y 1}
544do_test select4-9.3 {
545  execsql2 {
546    SELECT x, y FROM t2 EXCEPT SELECT a, b FROM t3 ORDER BY x LIMIT 1
547  }
548} {x 0 y 1}
549do_test select4-9.4 {
550  execsql2 {
551    SELECT x, y FROM t2 INTERSECT SELECT 0 AS a, 1 AS b;
552  }
553} {x 0 y 1}
554do_test select4-9.5 {
555  execsql2 {
556    SELECT 0 AS x, 1 AS y
557    UNION
558    SELECT 2 AS p, 3 AS q
559    UNION
560    SELECT 4 AS a, 5 AS b
561    ORDER BY x LIMIT 1
562  }
563} {x 0 y 1}
564
565ifcapable subquery {
566do_test select4-9.6 {
567  execsql2 {
568    SELECT * FROM (
569      SELECT 0 AS x, 1 AS y
570      UNION
571      SELECT 2 AS p, 3 AS q
572      UNION
573      SELECT 4 AS a, 5 AS b
574    ) ORDER BY 1 LIMIT 1;
575  }
576} {x 0 y 1}
577do_test select4-9.7 {
578  execsql2 {
579    SELECT * FROM (
580      SELECT 0 AS x, 1 AS y
581      UNION
582      SELECT 2 AS p, 3 AS q
583      UNION
584      SELECT 4 AS a, 5 AS b
585    ) ORDER BY x LIMIT 1;
586  }
587} {x 0 y 1}
588} ;# ifcapable subquery
589
590do_test select4-9.8 {
591  execsql {
592    SELECT 0 AS x, 1 AS y
593    UNION
594    SELECT 2 AS y, -3 AS x
595    ORDER BY x LIMIT 1;
596  }
597} {0 1}
598
599do_test select4-9.9.1 {
600  execsql2 {
601    SELECT 1 AS a, 2 AS b UNION ALL SELECT 3 AS b, 4 AS a
602  }
603} {a 1 b 2 a 3 b 4}
604
605ifcapable subquery {
606do_test select4-9.9.2 {
607  execsql2 {
608    SELECT * FROM (SELECT 1 AS a, 2 AS b UNION ALL SELECT 3 AS b, 4 AS a)
609     WHERE b=3
610  }
611} {}
612do_test select4-9.10 {
613  execsql2 {
614    SELECT * FROM (SELECT 1 AS a, 2 AS b UNION ALL SELECT 3 AS b, 4 AS a)
615     WHERE b=2
616  }
617} {a 1 b 2}
618do_test select4-9.11 {
619  execsql2 {
620    SELECT * FROM (SELECT 1 AS a, 2 AS b UNION ALL SELECT 3 AS e, 4 AS b)
621     WHERE b=2
622  }
623} {a 1 b 2}
624do_test select4-9.12 {
625  execsql2 {
626    SELECT * FROM (SELECT 1 AS a, 2 AS b UNION ALL SELECT 3 AS e, 4 AS b)
627     WHERE b>0
628  }
629} {a 1 b 2 a 3 b 4}
630} ;# ifcapable subquery
631
632# Try combining DISTINCT, LIMIT, and OFFSET.  Make sure they all work
633# together.
634#
635do_test select4-10.1 {
636  execsql {
637    SELECT DISTINCT log FROM t1 ORDER BY log
638  }
639} {0 1 2 3 4 5}
640do_test select4-10.2 {
641  execsql {
642    SELECT DISTINCT log FROM t1 ORDER BY log LIMIT 4
643  }
644} {0 1 2 3}
645do_test select4-10.3 {
646  execsql {
647    SELECT DISTINCT log FROM t1 ORDER BY log LIMIT 0
648  }
649} {}
650do_test select4-10.4 {
651  execsql {
652    SELECT DISTINCT log FROM t1 ORDER BY log LIMIT -1
653  }
654} {0 1 2 3 4 5}
655do_test select4-10.5 {
656  execsql {
657    SELECT DISTINCT log FROM t1 ORDER BY log LIMIT -1 OFFSET 2
658  }
659} {2 3 4 5}
660do_test select4-10.6 {
661  execsql {
662    SELECT DISTINCT log FROM t1 ORDER BY log LIMIT 3 OFFSET 2
663  }
664} {2 3 4}
665do_test select4-10.7 {
666  execsql {
667    SELECT DISTINCT log FROM t1 ORDER BY +log LIMIT 3 OFFSET 20
668  }
669} {}
670do_test select4-10.8 {
671  execsql {
672    SELECT DISTINCT log FROM t1 ORDER BY log LIMIT 0 OFFSET 3
673  }
674} {}
675do_test select4-10.9 {
676  execsql {
677    SELECT DISTINCT max(n), log FROM t1 ORDER BY +log; -- LIMIT 2 OFFSET 1
678  }
679} {31 5}
680
681# Make sure compound SELECTs with wildly different numbers of columns
682# do not cause assertion faults due to register allocation issues.
683#
684do_test select4-11.1 {
685  catchsql {
686    SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2
687    UNION
688    SELECT x FROM t2
689  }
690} {1 {SELECTs to the left and right of UNION do not have the same number of result columns}}
691do_test select4-11.2 {
692  catchsql {
693    SELECT x FROM t2
694    UNION
695    SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2
696  }
697} {1 {SELECTs to the left and right of UNION do not have the same number of result columns}}
698do_test select4-11.3 {
699  catchsql {
700    SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2
701    UNION ALL
702    SELECT x FROM t2
703  }
704} {1 {SELECTs to the left and right of UNION ALL do not have the same number of result columns}}
705do_test select4-11.4 {
706  catchsql {
707    SELECT x FROM t2
708    UNION ALL
709    SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2
710  }
711} {1 {SELECTs to the left and right of UNION ALL do not have the same number of result columns}}
712do_test select4-11.5 {
713  catchsql {
714    SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2
715    EXCEPT
716    SELECT x FROM t2
717  }
718} {1 {SELECTs to the left and right of EXCEPT do not have the same number of result columns}}
719do_test select4-11.6 {
720  catchsql {
721    SELECT x FROM t2
722    EXCEPT
723    SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2
724  }
725} {1 {SELECTs to the left and right of EXCEPT do not have the same number of result columns}}
726do_test select4-11.7 {
727  catchsql {
728    SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2
729    INTERSECT
730    SELECT x FROM t2
731  }
732} {1 {SELECTs to the left and right of INTERSECT do not have the same number of result columns}}
733do_test select4-11.8 {
734  catchsql {
735    SELECT x FROM t2
736    INTERSECT
737    SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2
738  }
739} {1 {SELECTs to the left and right of INTERSECT do not have the same number of result columns}}
740
741do_test select4-11.11 {
742  catchsql {
743    SELECT x FROM t2
744    UNION
745    SELECT x FROM t2
746    UNION ALL
747    SELECT x FROM t2
748    EXCEPT
749    SELECT x FROM t2
750    INTERSECT
751    SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2
752  }
753} {1 {SELECTs to the left and right of INTERSECT do not have the same number of result columns}}
754do_test select4-11.12 {
755  catchsql {
756    SELECT x FROM t2
757    UNION
758    SELECT x FROM t2
759    UNION ALL
760    SELECT x FROM t2
761    EXCEPT
762    SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2
763    EXCEPT
764    SELECT x FROM t2
765  }
766} {1 {SELECTs to the left and right of EXCEPT do not have the same number of result columns}}
767do_test select4-11.13 {
768  catchsql {
769    SELECT x FROM t2
770    UNION
771    SELECT x FROM t2
772    UNION ALL
773    SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2
774    UNION ALL
775    SELECT x FROM t2
776    EXCEPT
777    SELECT x FROM t2
778  }
779} {1 {SELECTs to the left and right of UNION ALL do not have the same number of result columns}}
780do_test select4-11.14 {
781  catchsql {
782    SELECT x FROM t2
783    UNION
784    SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2
785    UNION
786    SELECT x FROM t2
787    UNION ALL
788    SELECT x FROM t2
789    EXCEPT
790    SELECT x FROM t2
791  }
792} {1 {SELECTs to the left and right of UNION do not have the same number of result columns}}
793do_test select4-11.15 {
794  catchsql {
795    SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2
796    UNION
797    SELECT x FROM t2
798    INTERSECT
799    SELECT x FROM t2
800    UNION ALL
801    SELECT x FROM t2
802    EXCEPT
803    SELECT x FROM t2
804  }
805} {1 {SELECTs to the left and right of UNION do not have the same number of result columns}}
806do_test select4-11.16 {
807  catchsql {
808    INSERT INTO t2(rowid) VALUES(2) UNION SELECT 3,4 UNION SELECT 5,6 ORDER BY 1;
809  }
810} {1 {SELECTs to the left and right of UNION do not have the same number of result columns}}
811
812do_test select4-12.1 {
813  sqlite3 db2 :memory:
814  catchsql {
815    SELECT 1 UNION SELECT 2,3 UNION SELECT 4,5 ORDER BY 1;
816  } db2
817} {1 {SELECTs to the left and right of UNION do not have the same number of result columns}}
818
819} ;# ifcapable compound
820
821
822# Ticket [3557ad65a076c] - Incorrect DISTINCT processing with an
823# indexed query using IN.
824#
825do_test select4-13.1 {
826  sqlite3 db test.db
827  db eval {
828    CREATE TABLE t13(a,b);
829    INSERT INTO t13 VALUES(1,1);
830    INSERT INTO t13 VALUES(2,1);
831    INSERT INTO t13 VALUES(3,1);
832    INSERT INTO t13 VALUES(2,2);
833    INSERT INTO t13 VALUES(3,2);
834    INSERT INTO t13 VALUES(4,2);
835    CREATE INDEX t13ab ON t13(a,b);
836    SELECT DISTINCT b from t13 WHERE a IN (1,2,3);
837  }
838} {1 2}
839
840# 2014-02-18: Make sure compound SELECTs work with VALUES clauses
841#
842do_execsql_test select4-14.1 {
843  CREATE TABLE t14(a,b,c);
844  INSERT INTO t14 VALUES(1,2,3),(4,5,6);
845  SELECT * FROM t14 INTERSECT VALUES(3,2,1),(2,3,1),(1,2,3),(2,1,3);
846} {1 2 3}
847do_execsql_test select4-14.2 {
848  SELECT * FROM t14 INTERSECT VALUES(1,2,3);
849} {1 2 3}
850do_execsql_test select4-14.3 {
851  SELECT * FROM t14
852   UNION VALUES(3,2,1),(2,3,1),(1,2,3),(7,8,9),(4,5,6)
853   UNION SELECT * FROM t14 ORDER BY 1, 2, 3
854} {1 2 3 2 3 1 3 2 1 4 5 6 7 8 9}
855do_execsql_test select4-14.4 {
856  SELECT * FROM t14
857   UNION VALUES(3,2,1)
858   UNION SELECT * FROM t14 ORDER BY 1, 2, 3
859} {1 2 3 3 2 1 4 5 6}
860do_execsql_test select4-14.5 {
861  SELECT * FROM t14 EXCEPT VALUES(3,2,1),(2,3,1),(1,2,3),(2,1,3);
862} {4 5 6}
863do_execsql_test select4-14.6 {
864  SELECT * FROM t14 EXCEPT VALUES(1,2,3)
865} {4 5 6}
866do_execsql_test select4-14.7 {
867  SELECT * FROM t14 EXCEPT VALUES(1,2,3) EXCEPT VALUES(4,5,6)
868} {}
869do_execsql_test select4-14.8 {
870  SELECT * FROM t14 EXCEPT VALUES('a','b','c') EXCEPT VALUES(4,5,6)
871} {1 2 3}
872do_execsql_test select4-14.9 {
873  SELECT * FROM t14 UNION ALL VALUES(3,2,1),(2,3,1),(1,2,3),(2,1,3);
874} {1 2 3 4 5 6 3 2 1 2 3 1 1 2 3 2 1 3}
875do_execsql_test select4-14.10 {
876  SELECT (VALUES(1),(2),(3),(4))
877} {1}
878do_execsql_test select4-14.11 {
879  SELECT (SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4)
880} {1}
881do_execsql_test select4-14.12 {
882  VALUES(1) UNION VALUES(2);
883} {1 2}
884do_execsql_test select4-14.13 {
885  VALUES(1),(2),(3) EXCEPT VALUES(2);
886} {1 3}
887do_execsql_test select4-14.14 {
888  VALUES(1),(2),(3) EXCEPT VALUES(1),(3);
889} {2}
890
891finish_test
892