000001  # 2010 July 16
000002  #
000003  # The author disclaims copyright to this source code.  In place of
000004  # a legal notice, here is a blessing:
000005  #
000006  #    May you do good and not evil.
000007  #    May you find forgiveness for yourself and forgive others.
000008  #    May you share freely, never taking more than you give.
000009  #
000010  #***********************************************************************
000011  #
000012  # This file implements tests to verify that the "testable statements" in 
000013  # the lang_select.html document are correct.
000014  #
000015  
000016  set testdir [file dirname $argv0]
000017  source $testdir/tester.tcl
000018  
000019  ifcapable !compound {
000020    finish_test
000021    return
000022  }
000023  
000024  do_execsql_test e_select-1.0 {
000025    CREATE TABLE t1(a, b);
000026    INSERT INTO t1 VALUES('a', 'one');
000027    INSERT INTO t1 VALUES('b', 'two');
000028    INSERT INTO t1 VALUES('c', 'three');
000029  
000030    CREATE TABLE t2(a, b);
000031    INSERT INTO t2 VALUES('a', 'I');
000032    INSERT INTO t2 VALUES('b', 'II');
000033    INSERT INTO t2 VALUES('c', 'III');
000034  
000035    CREATE TABLE t3(a, c);
000036    INSERT INTO t3 VALUES('a', 1);
000037    INSERT INTO t3 VALUES('b', 2);
000038  
000039    CREATE TABLE t4(a, c);
000040    INSERT INTO t4 VALUES('a', NULL);
000041    INSERT INTO t4 VALUES('b', 2);
000042  } {}
000043  set t1_cross_t2 [list                \
000044     a one   a I      a one   b II     \
000045     a one   c III    b two   a I      \
000046     b two   b II     b two   c III    \
000047     c three a I      c three b II     \
000048     c three c III                     \
000049  ]
000050  set t1_cross_t1 [list                  \
000051     a one   a one      a one   b two    \
000052     a one   c three    b two   a one    \
000053     b two   b two      b two   c three  \
000054     c three a one      c three b two    \
000055     c three c three                     \
000056  ]
000057  
000058  
000059  # This proc is a specialized version of [do_execsql_test].
000060  #
000061  # The second argument to this proc must be a SELECT statement that 
000062  # features a cross join of some time. Instead of the usual ",", 
000063  # "CROSS JOIN" or "INNER JOIN" join-op, the string %JOIN% must be 
000064  # substituted.
000065  #
000066  # This test runs the SELECT three times - once with:
000067  #
000068  #   * s/%JOIN%/,/
000069  #   * s/%JOIN%/JOIN/
000070  #   * s/%JOIN%/INNER JOIN/
000071  #   * s/%JOIN%/CROSS JOIN/
000072  #
000073  # and checks that each time the results of the SELECT are $res.
000074  #
000075  proc do_join_test {tn select res} {
000076    foreach {tn2 joinop} [list    1 ,    2 "CROSS JOIN"    3 "INNER JOIN"] {
000077      set S [string map [list %JOIN% $joinop] $select]
000078      uplevel do_execsql_test $tn.$tn2 [list $S] [list $res]
000079    }
000080  }
000081  
000082  #-------------------------------------------------------------------------
000083  # The following tests check that all paths on the syntax diagrams on
000084  # the lang_select.html page may be taken.
000085  #
000086  # -- syntax diagram join-constraint
000087  #
000088  do_join_test e_select-0.1.1 {
000089    SELECT count(*) FROM t1 %JOIN% t2 ON (t1.a=t2.a)
000090  } {3}
000091  do_join_test e_select-0.1.2 {
000092    SELECT count(*) FROM t1 %JOIN% t2 USING (a)
000093  } {3}
000094  do_join_test e_select-0.1.3 {
000095    SELECT count(*) FROM t1 %JOIN% t2
000096  } {9}
000097  do_catchsql_test e_select-0.1.4 {
000098    SELECT count(*) FROM t1, t2 ON (t1.a=t2.a) USING (a)
000099  } {1 {near "USING": syntax error}}
000100  do_catchsql_test e_select-0.1.5 {
000101    SELECT count(*) FROM t1, t2 USING (a) ON (t1.a=t2.a)
000102  } {1 {near "ON": syntax error}}
000103  
000104  # -- syntax diagram select-core
000105  #
000106  #   0: SELECT ...
000107  #   1: SELECT DISTINCT ...
000108  #   2: SELECT ALL ...
000109  #
000110  #   0: No FROM clause
000111  #   1: Has FROM clause
000112  #
000113  #   0: No WHERE clause
000114  #   1: Has WHERE clause
000115  #
000116  #   0: No GROUP BY clause
000117  #   1: Has GROUP BY clause
000118  #   2: Has GROUP BY and HAVING clauses
000119  #
000120  do_select_tests e_select-0.2 {
000121    0000.1  "SELECT 1, 2, 3 " {1 2 3}
000122    1000.1  "SELECT DISTINCT 1, 2, 3 " {1 2 3}
000123    2000.1  "SELECT ALL 1, 2, 3 " {1 2 3}
000124    
000125    0100.1  "SELECT a, b, a||b FROM t1 " {
000126      a one aone b two btwo c three cthree
000127    }
000128    1100.1  "SELECT DISTINCT a, b, a||b FROM t1 " {
000129      a one aone b two btwo c three cthree
000130    }
000131    1200.1  "SELECT ALL a, b, a||b FROM t1 " {
000132      a one aone b two btwo c three cthree
000133    }
000134  
000135    0010.1  "SELECT 1, 2, 3 WHERE 1 " {1 2 3}
000136    0010.2  "SELECT 1, 2, 3 WHERE 0 " {}
000137    0010.3  "SELECT 1, 2, 3 WHERE NULL " {}
000138  
000139    1010.1  "SELECT DISTINCT 1, 2, 3 WHERE 1 " {1 2 3}
000140  
000141    2010.1  "SELECT ALL 1, 2, 3 WHERE 1 " {1 2 3}
000142  
000143    0110.1  "SELECT a, b, a||b FROM t1 WHERE a!='x' " {
000144      a one aone b two btwo c three cthree
000145    }
000146    0110.2  "SELECT a, b, a||b FROM t1 WHERE a=='x'" {}
000147  
000148    1110.1  "SELECT DISTINCT a, b, a||b FROM t1 WHERE a!='x' " {
000149      a one aone b two btwo c three cthree
000150    }
000151  
000152    2110.0  "SELECT ALL a, b, a||b FROM t1 WHERE a=='x'" {}
000153  
000154    0001.1  "SELECT 1, 2, 3 GROUP BY 2" {1 2 3}
000155    0002.1  "SELECT 1, 2, 3 GROUP BY 2 HAVING count(*)=1" {1 2 3}
000156    0002.2  "SELECT 1, 2, 3 GROUP BY 2 HAVING count(*)>1" {}
000157  
000158    1001.1  "SELECT DISTINCT 1, 2, 3 GROUP BY 2" {1 2 3}
000159    1002.1  "SELECT DISTINCT 1, 2, 3 GROUP BY 2 HAVING count(*)=1" {1 2 3}
000160    1002.2  "SELECT DISTINCT 1, 2, 3 GROUP BY 2 HAVING count(*)>1" {}
000161  
000162    2001.1  "SELECT ALL 1, 2, 3 GROUP BY 2" {1 2 3}
000163    2002.1  "SELECT ALL 1, 2, 3 GROUP BY 2 HAVING count(*)=1" {1 2 3}
000164    2002.2  "SELECT ALL 1, 2, 3 GROUP BY 2 HAVING count(*)>1" {}
000165  
000166    0101.1  "SELECT count(*), max(a) FROM t1 GROUP BY b" {1 a 1 c 1 b}
000167    0102.1  "SELECT count(*), max(a) FROM t1 GROUP BY b HAVING count(*)=1" {
000168      1 a 1 c 1 b
000169    }
000170    0102.2  "SELECT count(*), max(a) FROM t1 GROUP BY b HAVING count(*)=2" {}
000171  
000172    1101.1  "SELECT DISTINCT count(*), max(a) FROM t1 GROUP BY b" {1 a 1 c 1 b}
000173    1102.1  "SELECT DISTINCT count(*), max(a) FROM t1 
000174             GROUP BY b HAVING count(*)=1" {
000175      1 a 1 c 1 b
000176    }
000177    1102.2  "SELECT DISTINCT count(*), max(a) FROM t1 
000178             GROUP BY b HAVING count(*)=2" {}
000179  
000180    2101.1  "SELECT ALL count(*), max(a) FROM t1 GROUP BY b" {1 a 1 c 1 b}
000181    2102.1  "SELECT ALL count(*), max(a) FROM t1 
000182             GROUP BY b HAVING count(*)=1" {
000183      1 a 1 c 1 b
000184    }
000185    2102.2  "SELECT ALL count(*), max(a) FROM t1 
000186             GROUP BY b HAVING count(*)=2" {}
000187  
000188    0011.1  "SELECT 1, 2, 3 WHERE 1 GROUP BY 2" {1 2 3}
000189    0012.1  "SELECT 1, 2, 3 WHERE 0 GROUP BY 2 HAVING count(*)=1" {}
000190    0012.2  "SELECT 1, 2, 3 WHERE 0 GROUP BY 2 HAVING count(*)>1" {}
000191  
000192    1011.1  "SELECT DISTINCT 1, 2, 3 WHERE 0 GROUP BY 2" {}
000193    1012.1  "SELECT DISTINCT 1, 2, 3 WHERE 1 GROUP BY 2 HAVING count(*)=1" 
000194            {1 2 3}
000195    1012.2  "SELECT DISTINCT 1, 2, 3 WHERE NULL GROUP BY 2 HAVING count(*)>1" {}
000196  
000197    2011.1  "SELECT ALL 1, 2, 3 WHERE 1 GROUP BY 2" {1 2 3}
000198    2012.1  "SELECT ALL 1, 2, 3 WHERE 0 GROUP BY 2 HAVING count(*)=1" {}
000199    2012.2  "SELECT ALL 1, 2, 3 WHERE 'abc' GROUP BY 2 HAVING count(*)>1" {}
000200  
000201    0111.1  "SELECT count(*), max(a) FROM t1 WHERE a='a' GROUP BY b" {1 a}
000202    0112.1  "SELECT count(*), max(a) FROM t1 
000203             WHERE a='c' GROUP BY b HAVING count(*)=1" {1 c}
000204    0112.2  "SELECT count(*), max(a) FROM t1 
000205             WHERE 0 GROUP BY b HAVING count(*)=2" {}
000206    1111.1  "SELECT DISTINCT count(*), max(a) FROM t1 WHERE a<'c' GROUP BY b" 
000207            {1 a 1 b}
000208    1112.1  "SELECT DISTINCT count(*), max(a) FROM t1 WHERE a>'a'
000209             GROUP BY b HAVING count(*)=1" {
000210      1 c 1 b
000211    }
000212    1112.2  "SELECT DISTINCT count(*), max(a) FROM t1 WHERE 0
000213             GROUP BY b HAVING count(*)=2" {}
000214  
000215    2111.1  "SELECT ALL count(*), max(a) FROM t1 WHERE b>'one' GROUP BY b" 
000216            {1 c 1 b}
000217    2112.1  "SELECT ALL count(*), max(a) FROM t1 WHERE a!='b'
000218             GROUP BY b HAVING count(*)=1" {
000219      1 a 1 c
000220    }
000221    2112.2  "SELECT ALL count(*), max(a) FROM t1 
000222             WHERE 0 GROUP BY b HAVING count(*)=2" {}
000223  }
000224  
000225  
000226  # -- syntax diagram result-column
000227  #
000228  do_select_tests e_select-0.3 {
000229    1  "SELECT * FROM t1" {a one b two c three}
000230    2  "SELECT t1.* FROM t1" {a one b two c three}
000231    3  "SELECT 'x'||a||'x' FROM t1" {xax xbx xcx}
000232    4  "SELECT 'x'||a||'x' alias FROM t1" {xax xbx xcx}
000233    5  "SELECT 'x'||a||'x' AS alias FROM t1" {xax xbx xcx}
000234  }
000235  
000236  # -- syntax diagram join-source
000237  #
000238  # -- syntax diagram join-op
000239  #
000240  do_select_tests e_select-0.4 {
000241    1  "SELECT t1.rowid FROM t1" {1 2 3}
000242    2  "SELECT t1.rowid FROM t1,t2" {1 1 1 2 2 2 3 3 3}
000243    3  "SELECT t1.rowid FROM t1,t2,t3" {1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3}
000244  
000245    4  "SELECT t1.rowid FROM t1" {1 2 3}
000246    5  "SELECT t1.rowid FROM t1 JOIN t2" {1 1 1 2 2 2 3 3 3}
000247    6  "SELECT t1.rowid FROM t1 JOIN t2 JOIN t3" 
000248       {1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3}
000249  
000250    7  "SELECT t1.rowid FROM t1 NATURAL JOIN t3" {1 2}
000251    8  "SELECT t1.rowid FROM t1 NATURAL LEFT OUTER JOIN t3" {1 2 3}
000252    9  "SELECT t1.rowid FROM t1 NATURAL LEFT JOIN t3" {1 2 3}
000253    10 "SELECT t1.rowid FROM t1 NATURAL INNER JOIN t3" {1 2}
000254    11 "SELECT t1.rowid FROM t1 NATURAL CROSS JOIN t3" {1 2}
000255  
000256    12 "SELECT t1.rowid FROM t1 JOIN t3" {1 1 2 2 3 3}
000257    13 "SELECT t1.rowid FROM t1 LEFT OUTER JOIN t3" {1 1 2 2 3 3}
000258    14 "SELECT t1.rowid FROM t1 LEFT JOIN t3" {1 1 2 2 3 3}
000259    15 "SELECT t1.rowid FROM t1 INNER JOIN t3" {1 1 2 2 3 3}
000260    16 "SELECT t1.rowid FROM t1 CROSS JOIN t3" {1 1 2 2 3 3}
000261  }
000262  
000263  # -- syntax diagram compound-operator
000264  #
000265  do_select_tests e_select-0.5 {
000266    1  "SELECT rowid FROM t1 UNION ALL SELECT rowid+2 FROM t4" {1 2 3 3 4}
000267    2  "SELECT rowid FROM t1 UNION     SELECT rowid+2 FROM t4" {1 2 3 4}
000268    3  "SELECT rowid FROM t1 INTERSECT SELECT rowid+2 FROM t4" {3}
000269    4  "SELECT rowid FROM t1 EXCEPT    SELECT rowid+2 FROM t4" {1 2}
000270  }
000271  
000272  # -- syntax diagram ordering-term
000273  #
000274  do_select_tests e_select-0.6 {
000275    1  "SELECT b||a FROM t1 ORDER BY b||a"                  {onea threec twob}
000276    2  "SELECT b||a FROM t1 ORDER BY (b||a) COLLATE nocase" {onea threec twob}
000277    3  "SELECT b||a FROM t1 ORDER BY (b||a) ASC"            {onea threec twob}
000278    4  "SELECT b||a FROM t1 ORDER BY (b||a) DESC"           {twob threec onea}
000279  }
000280  
000281  # -- syntax diagram select-stmt
000282  #
000283  do_select_tests e_select-0.7 {
000284    1  "SELECT * FROM t1" {a one b two c three}
000285    2  "SELECT * FROM t1 ORDER BY b" {a one c three b two}
000286    3  "SELECT * FROM t1 ORDER BY b, a" {a one c three b two}
000287  
000288    4  "SELECT * FROM t1 LIMIT 10" {a one b two c three}
000289    5  "SELECT * FROM t1 LIMIT 10 OFFSET 5" {}
000290    6  "SELECT * FROM t1 LIMIT 10, 5" {}
000291  
000292    7  "SELECT * FROM t1 ORDER BY a LIMIT 10" {a one b two c three}
000293    8  "SELECT * FROM t1 ORDER BY b LIMIT 10 OFFSET 5" {}
000294    9  "SELECT * FROM t1 ORDER BY a,b LIMIT 10, 5" {}
000295  
000296    10  "SELECT * FROM t1 UNION SELECT b, a FROM t1" 
000297       {a one b two c three one a three c two b}
000298    11  "SELECT * FROM t1 UNION SELECT b, a FROM t1 ORDER BY b" 
000299       {one a two b three c a one c three b two}
000300    12  "SELECT * FROM t1 UNION SELECT b, a FROM t1 ORDER BY b, a" 
000301       {one a two b three c a one c three b two}
000302    13  "SELECT * FROM t1 UNION SELECT b, a FROM t1 LIMIT 10" 
000303       {a one b two c three one a three c two b}
000304    14  "SELECT * FROM t1 UNION SELECT b, a FROM t1 LIMIT 10 OFFSET 5" 
000305       {two b}
000306    15  "SELECT * FROM t1 UNION SELECT b, a FROM t1 LIMIT 10, 5" 
000307       {}
000308    16  "SELECT * FROM t1 UNION SELECT b, a FROM t1 ORDER BY a LIMIT 10" 
000309       {a one b two c three one a three c two b}
000310    17  "SELECT * FROM t1 UNION SELECT b, a FROM t1 ORDER BY b LIMIT 10 OFFSET 5" 
000311       {b two}
000312    18  "SELECT * FROM t1 UNION SELECT b, a FROM t1 ORDER BY a,b LIMIT 10, 5" 
000313       {}
000314  }
000315  
000316  #-------------------------------------------------------------------------
000317  # The following tests focus on FROM clause (join) processing.
000318  #
000319  
000320  # EVIDENCE-OF: R-16074-54196 If the FROM clause is omitted from a simple
000321  # SELECT statement, then the input data is implicitly a single row zero
000322  # columns wide
000323  #
000324  do_select_tests e_select-1.1 {
000325    1 "SELECT 'abc'"            {abc}
000326    2 "SELECT 'abc' WHERE NULL" {}
000327    3 "SELECT NULL"             {{}}
000328    4 "SELECT count(*)"         {1}
000329    5 "SELECT count(*) WHERE 0" {0}
000330    6 "SELECT count(*) WHERE 1" {1}
000331  }
000332  
000333  # EVIDENCE-OF: R-45424-07352 If there is only a single table or subquery
000334  # in the FROM clause, then the input data used by the SELECT statement
000335  # is the contents of the named table.
000336  #
000337  #   The results of the SELECT queries suggest that they are operating on the
000338  #   contents of the table 'xx'.
000339  #
000340  do_execsql_test e_select-1.2.0 {
000341    CREATE TABLE xx(x, y);
000342    INSERT INTO xx VALUES('IiJlsIPepMuAhU', X'10B00B897A15BAA02E3F98DCE8F2');
000343    INSERT INTO xx VALUES(NULL, -16.87);
000344    INSERT INTO xx VALUES(-17.89, 'linguistically');
000345  } {}
000346  do_select_tests e_select-1.2 {
000347    1  "SELECT quote(x), quote(y) FROM xx" {
000348       'IiJlsIPepMuAhU' X'10B00B897A15BAA02E3F98DCE8F2' 
000349       NULL             -16.87                          
000350       -17.89           'linguistically'                
000351    }
000352  
000353    2  "SELECT count(*), count(x), count(y) FROM xx" {3 2 3}
000354    3  "SELECT sum(x), sum(y) FROM xx"               {-17.89 -16.87}
000355  }
000356  
000357  # EVIDENCE-OF: R-28355-09804 If there is more than one table or subquery
000358  # in FROM clause then the contents of all tables and/or subqueries are
000359  # joined into a single dataset for the simple SELECT statement to
000360  # operate on.
000361  #
000362  #   There are more detailed tests for subsequent requirements that add 
000363  #   more detail to this idea. We just add a single test that shows that
000364  #   data is coming from each of the three tables following the FROM clause
000365  #   here to show that the statement, vague as it is, is not incorrect.
000366  #
000367  do_select_tests e_select-1.3 {
000368    1 "SELECT * FROM t1, t2, t3" {
000369        a one a I a 1 a one a I b 2 a one b II a 1 
000370        a one b II b 2 a one c III a 1 a one c III b 2 
000371        b two a I a 1 b two a I b 2 b two b II a 1 
000372        b two b II b 2 b two c III a 1 b two c III b 2 
000373        c three a I a 1 c three a I b 2 c three b II a 1 
000374        c three b II b 2 c three c III a 1 c three c III b 2
000375    }
000376  }
000377  
000378  #
000379  # The following block of tests - e_select-1.4.* - test that the description
000380  # of cartesian joins in the SELECT documentation is consistent with SQLite.
000381  # In doing so, we test the following three requirements as a side-effect:
000382  #
000383  # EVIDENCE-OF: R-49872-03192 If the join-operator is "CROSS JOIN",
000384  # "INNER JOIN", "JOIN" or a comma (",") and there is no ON or USING
000385  # clause, then the result of the join is simply the cartesian product of
000386  # the left and right-hand datasets.
000387  #
000388  #    The tests are built on this assertion. Really, they test that the output
000389  #    of a CROSS JOIN, JOIN, INNER JOIN or "," join matches the expected result
000390  #    of calculating the cartesian product of the left and right-hand datasets. 
000391  #
000392  # EVIDENCE-OF: R-46256-57243 There is no difference between the "INNER
000393  # JOIN", "JOIN" and "," join operators.
000394  #
000395  # EVIDENCE-OF: R-25071-21202 The "CROSS JOIN" join operator produces the
000396  # same result as the "INNER JOIN", "JOIN" and "," operators
000397  #
000398  #    All tests are run 4 times, with the only difference in each run being
000399  #    which of the 4 equivalent cartesian product join operators are used.
000400  #    Since the output data is the same in all cases, we consider that this
000401  #    qualifies as testing the two statements above.
000402  #
000403  do_execsql_test e_select-1.4.0 {
000404    CREATE TABLE x1(a, b);
000405    CREATE TABLE x2(c, d, e);
000406    CREATE TABLE x3(f, g, h, i);
000407  
000408    -- x1: 3 rows, 2 columns
000409    INSERT INTO x1 VALUES(24, 'converging');
000410    INSERT INTO x1 VALUES(NULL, X'CB71');
000411    INSERT INTO x1 VALUES('blonds', 'proprietary');
000412  
000413    -- x2: 2 rows, 3 columns
000414    INSERT INTO x2 VALUES(-60.06, NULL, NULL);
000415    INSERT INTO x2 VALUES(-58, NULL, 1.21);
000416  
000417    -- x3: 5 rows, 4 columns
000418    INSERT INTO x3 VALUES(-39.24, NULL, 'encompass', -1);
000419    INSERT INTO x3 VALUES('presenting', 51, 'reformation', 'dignified');
000420    INSERT INTO x3 VALUES('conducting', -87.24, 37.56, NULL);
000421    INSERT INTO x3 VALUES('coldest', -96, 'dramatists', 82.3);
000422    INSERT INTO x3 VALUES('alerting', NULL, -93.79, NULL);
000423  } {}
000424  
000425  # EVIDENCE-OF: R-59089-25828 The columns of the cartesian product
000426  # dataset are, in order, all the columns of the left-hand dataset
000427  # followed by all the columns of the right-hand dataset.
000428  #
000429  do_join_test e_select-1.4.1.1 {
000430    SELECT * FROM x1 %JOIN% x2 LIMIT 1
000431  } [concat {24 converging} {-60.06 {} {}}]
000432  
000433  do_join_test e_select-1.4.1.2 {
000434    SELECT * FROM x2 %JOIN% x1 LIMIT 1
000435  } [concat {-60.06 {} {}} {24 converging}]
000436  
000437  do_join_test e_select-1.4.1.3 {
000438    SELECT * FROM x3 %JOIN% x2 LIMIT 1
000439  } [concat {-39.24 {} encompass -1} {-60.06 {} {}}]
000440  
000441  do_join_test e_select-1.4.1.4 {
000442    SELECT * FROM x2 %JOIN% x3 LIMIT 1
000443  } [concat {-60.06 {} {}} {-39.24 {} encompass -1}]
000444  
000445  # EVIDENCE-OF: R-44414-54710 There is a row in the cartesian product
000446  # dataset formed by combining each unique combination of a row from the
000447  # left-hand and right-hand datasets.
000448  #
000449  do_join_test e_select-1.4.2.1 {
000450    SELECT * FROM x2 %JOIN% x3 ORDER BY +c, +f
000451  } [list -60.06 {} {}      -39.24 {} encompass -1                 \
000452          -60.06 {} {}      alerting {} -93.79 {}                  \
000453          -60.06 {} {}      coldest -96 dramatists 82.3            \
000454          -60.06 {} {}      conducting -87.24 37.56 {}             \
000455          -60.06 {} {}      presenting 51 reformation dignified    \
000456          -58 {} 1.21       -39.24 {} encompass -1                 \
000457          -58 {} 1.21       alerting {} -93.79 {}                  \
000458          -58 {} 1.21       coldest -96 dramatists 82.3            \
000459          -58 {} 1.21       conducting -87.24 37.56 {}             \
000460          -58 {} 1.21       presenting 51 reformation dignified    \
000461  ]
000462  # TODO: Come back and add a few more like the above.
000463  
000464  # EVIDENCE-OF: R-18439-38548 In other words, if the left-hand dataset
000465  # consists of Nleft rows of Mleft columns, and the right-hand dataset of
000466  # Nright rows of Mright columns, then the cartesian product is a dataset
000467  # of Nleft&times;Nright rows, each containing Mleft+Mright columns.
000468  #
000469  # x1, x2    (Nlhs=3, Nrhs=2)   (Mlhs=2, Mrhs=3)
000470  do_join_test e_select-1.4.3.1 { 
000471    SELECT count(*) FROM x1 %JOIN% x2 
000472  } [expr 3*2]
000473  do_test e_select-1.4.3.2 { 
000474    expr {[llength [execsql {SELECT * FROM x1, x2}]] / 6}
000475  } [expr 2+3]
000476  
000477  # x2, x3    (Nlhs=2, Nrhs=5)   (Mlhs=3, Mrhs=4)
000478  do_join_test e_select-1.4.3.3 { 
000479    SELECT count(*) FROM x2 %JOIN% x3 
000480  } [expr 2*5]
000481  do_test e_select-1.4.3.4 { 
000482    expr {[llength [execsql {SELECT * FROM x2 JOIN x3}]] / 10}
000483  } [expr 3+4]
000484  
000485  # x3, x1    (Nlhs=5, Nrhs=3)   (Mlhs=4, Mrhs=2)
000486  do_join_test e_select-1.4.3.5 { 
000487    SELECT count(*) FROM x3 %JOIN% x1 
000488  } [expr 5*3]
000489  do_test e_select-1.4.3.6 { 
000490    expr {[llength [execsql {SELECT * FROM x3 CROSS JOIN x1}]] / 15}
000491  } [expr 4+2]
000492  
000493  # x3, x3    (Nlhs=5, Nrhs=5)   (Mlhs=4, Mrhs=4)
000494  do_join_test e_select-1.4.3.7 { 
000495    SELECT count(*) FROM x3 %JOIN% x3 
000496  } [expr 5*5]
000497  do_test e_select-1.4.3.8 { 
000498    expr {[llength [execsql {SELECT * FROM x3 INNER JOIN x3 AS x4}]] / 25}
000499  } [expr 4+4]
000500  
000501  # Some extra cartesian product tests using tables t1 and t2.
000502  #
000503  do_execsql_test e_select-1.4.4.1 { SELECT * FROM t1, t2 } $t1_cross_t2
000504  do_execsql_test e_select-1.4.4.2 { SELECT * FROM t1 AS x, t1 AS y} $t1_cross_t1
000505  
000506  do_select_tests e_select-1.4.5 [list                                   \
000507      1 { SELECT * FROM t1 CROSS JOIN t2 }           $t1_cross_t2        \
000508      2 { SELECT * FROM t1 AS y CROSS JOIN t1 AS x } $t1_cross_t1        \
000509      3 { SELECT * FROM t1 INNER JOIN t2 }           $t1_cross_t2        \
000510      4 { SELECT * FROM t1 AS y INNER JOIN t1 AS x } $t1_cross_t1        \
000511  ]
000512  
000513  # EVIDENCE-OF: R-38465-03616 If there is an ON clause then the ON
000514  # expression is evaluated for each row of the cartesian product as a
000515  # boolean expression. Only rows for which the expression evaluates to
000516  # true are included from the dataset.
000517  #
000518  foreach {tn select res} [list                                              \
000519      1 { SELECT * FROM t1 %JOIN% t2 ON (1) }       $t1_cross_t2             \
000520      2 { SELECT * FROM t1 %JOIN% t2 ON (0) }       [list]                   \
000521      3 { SELECT * FROM t1 %JOIN% t2 ON (NULL) }    [list]                   \
000522      4 { SELECT * FROM t1 %JOIN% t2 ON ('abc') }   [list]                   \
000523      5 { SELECT * FROM t1 %JOIN% t2 ON ('1ab') }   $t1_cross_t2             \
000524      6 { SELECT * FROM t1 %JOIN% t2 ON (0.9) }     $t1_cross_t2             \
000525      7 { SELECT * FROM t1 %JOIN% t2 ON ('0.9') }   $t1_cross_t2             \
000526      8 { SELECT * FROM t1 %JOIN% t2 ON (0.0) }     [list]                   \
000527                                                                             \
000528      9 { SELECT t1.b, t2.b FROM t1 %JOIN% t2 ON (t1.a = t2.a) }             \
000529        {one I two II three III}                                             \
000530     10 { SELECT t1.b, t2.b FROM t1 %JOIN% t2 ON (t1.a = 'a') }              \
000531        {one I one II one III}                                               \
000532     11 { SELECT t1.b, t2.b 
000533          FROM t1 %JOIN% t2 ON (CASE WHEN t1.a = 'a' THEN NULL ELSE 1 END) } \
000534        {two I two II two III three I three II three III}                    \
000535  ] {
000536    do_join_test e_select-1.3.$tn $select $res
000537  }
000538  
000539  # EVIDENCE-OF: R-49933-05137 If there is a USING clause then each of the
000540  # column names specified must exist in the datasets to both the left and
000541  # right of the join-operator.
000542  #
000543  do_select_tests e_select-1.4 -error {
000544    cannot join using column %s - column not present in both tables
000545  } {
000546    1 { SELECT * FROM t1, t3 USING (b) }   "b"
000547    2 { SELECT * FROM t3, t1 USING (c) }   "c"
000548    3 { SELECT * FROM t3, (SELECT a AS b, b AS c FROM t1) USING (a) }   "a"
000549  } 
000550  
000551  # EVIDENCE-OF: R-22776-52830 For each pair of named columns, the
000552  # expression "lhs.X = rhs.X" is evaluated for each row of the cartesian
000553  # product as a boolean expression. Only rows for which all such
000554  # expressions evaluates to true are included from the result set.
000555  #
000556  do_select_tests e_select-1.5 {
000557    1 { SELECT * FROM t1, t3 USING (a)   }  {a one 1 b two 2}
000558    2 { SELECT * FROM t3, t4 USING (a,c) }  {b 2}
000559  } 
000560  
000561  # EVIDENCE-OF: R-54046-48600 When comparing values as a result of a
000562  # USING clause, the normal rules for handling affinities, collation
000563  # sequences and NULL values in comparisons apply.
000564  #
000565  # EVIDENCE-OF: R-38422-04402 The column from the dataset on the
000566  # left-hand side of the join-operator is considered to be on the
000567  # left-hand side of the comparison operator (=) for the purposes of
000568  # collation sequence and affinity precedence.
000569  #
000570  do_execsql_test e_select-1.6.0 {
000571    CREATE TABLE t5(a COLLATE nocase, b COLLATE binary);
000572    INSERT INTO t5 VALUES('AA', 'cc');
000573    INSERT INTO t5 VALUES('BB', 'dd');
000574    INSERT INTO t5 VALUES(NULL, NULL);
000575    CREATE TABLE t6(a COLLATE binary, b COLLATE nocase);
000576    INSERT INTO t6 VALUES('aa', 'cc');
000577    INSERT INTO t6 VALUES('bb', 'DD');
000578    INSERT INTO t6 VALUES(NULL, NULL);
000579  } {}
000580  foreach {tn select res} {
000581    1 { SELECT * FROM t5 %JOIN% t6 USING (a) } {AA cc cc BB dd DD}
000582    2 { SELECT * FROM t6 %JOIN% t5 USING (a) } {}
000583    3 { SELECT * FROM (SELECT a COLLATE nocase, b FROM t6) %JOIN% t5 USING (a) } 
000584      {aa cc cc bb DD dd}
000585    4 { SELECT * FROM t5 %JOIN% t6 USING (a,b) } {AA cc}
000586    5 { SELECT * FROM t6 %JOIN% t5 USING (a,b) } {}
000587  } {
000588    do_join_test e_select-1.6.$tn $select $res
000589  }
000590  
000591  # EVIDENCE-OF: R-57047-10461 For each pair of columns identified by a
000592  # USING clause, the column from the right-hand dataset is omitted from
000593  # the joined dataset.
000594  #
000595  # EVIDENCE-OF: R-56132-15700 This is the only difference between a USING
000596  # clause and its equivalent ON constraint.
000597  #
000598  foreach {tn select res} {
000599    1a { SELECT * FROM t1 %JOIN% t2 USING (a)      } 
000600       {a one I b two II c three III}
000601    1b { SELECT * FROM t1 %JOIN% t2 ON (t1.a=t2.a) }
000602       {a one a I b two b II c three c III}
000603  
000604    2a { SELECT * FROM t3 %JOIN% t4 USING (a)      }  
000605       {a 1 {} b 2 2}
000606    2b { SELECT * FROM t3 %JOIN% t4 ON (t3.a=t4.a) } 
000607       {a 1 a {} b 2 b 2}
000608  
000609    3a { SELECT * FROM t3 %JOIN% t4 USING (a,c)                  } {b 2}
000610    3b { SELECT * FROM t3 %JOIN% t4 ON (t3.a=t4.a AND t3.c=t4.c) } {b 2 b 2}
000611  
000612    4a { SELECT * FROM (SELECT a COLLATE nocase, b FROM t6) AS x 
000613         %JOIN% t5 USING (a) } 
000614       {aa cc cc bb DD dd}
000615    4b { SELECT * FROM (SELECT a COLLATE nocase, b FROM t6) AS x
000616         %JOIN% t5 ON (x.a=t5.a) } 
000617       {aa cc AA cc bb DD BB dd}
000618  } {
000619    do_join_test e_select-1.7.$tn $select $res
000620  }
000621  
000622  # EVIDENCE-OF: R-24610-05866 If the join-operator is a "LEFT JOIN" or
000623  # "LEFT OUTER JOIN", then after the ON or USING filtering clauses have
000624  # been applied, an extra row is added to the output for each row in the
000625  # original left-hand input dataset that does not match any row in the
000626  # right-hand dataset.
000627  #
000628  do_execsql_test e_select-1.8.0 {
000629    CREATE TABLE t7(a, b, c);
000630    CREATE TABLE t8(a, d, e);
000631  
000632    INSERT INTO t7 VALUES('x', 'ex',  24);
000633    INSERT INTO t7 VALUES('y', 'why', 25);
000634  
000635    INSERT INTO t8 VALUES('x', 'abc', 24);
000636    INSERT INTO t8 VALUES('z', 'ghi', 26);
000637  } {}
000638  
000639  do_select_tests e_select-1.8 {
000640    1a "SELECT count(*) FROM t7 JOIN t8 ON (t7.a=t8.a)" {1}
000641    1b "SELECT count(*) FROM t7 LEFT JOIN t8 ON (t7.a=t8.a)" {2}
000642    2a "SELECT count(*) FROM t7 JOIN t8 USING (a)" {1}
000643    2b "SELECT count(*) FROM t7 LEFT JOIN t8 USING (a)" {2}
000644  }
000645  
000646  
000647  # EVIDENCE-OF: R-15607-52988 The added rows contain NULL values in the
000648  # columns that would normally contain values copied from the right-hand
000649  # input dataset.
000650  #
000651  do_select_tests e_select-1.9 {
000652    1a "SELECT * FROM t7 JOIN t8 ON (t7.a=t8.a)" {x ex 24 x abc 24}
000653    1b "SELECT * FROM t7 LEFT JOIN t8 ON (t7.a=t8.a)" 
000654       {x ex 24 x abc 24 y why 25 {} {} {}}
000655    2a "SELECT * FROM t7 JOIN t8 USING (a)" {x ex 24 abc 24}
000656    2b "SELECT * FROM t7 LEFT JOIN t8 USING (a)" {x ex 24 abc 24 y why 25 {} {}}
000657  }
000658  
000659  # EVIDENCE-OF: R-04932-55942 If the NATURAL keyword is in the
000660  # join-operator then an implicit USING clause is added to the
000661  # join-constraints. The implicit USING clause contains each of the
000662  # column names that appear in both the left and right-hand input
000663  # datasets.
000664  #
000665  do_select_tests e_select-1-10 {
000666    1a "SELECT * FROM t7 JOIN t8 USING (a)"        {x ex 24 abc 24}
000667    1b "SELECT * FROM t7 NATURAL JOIN t8"          {x ex 24 abc 24}
000668  
000669    2a "SELECT * FROM t8 JOIN t7 USING (a)"        {x abc 24 ex 24}
000670    2b "SELECT * FROM t8 NATURAL JOIN t7"          {x abc 24 ex 24}
000671  
000672    3a "SELECT * FROM t7 LEFT JOIN t8 USING (a)"   {x ex 24 abc 24 y why 25 {} {}}
000673    3b "SELECT * FROM t7 NATURAL LEFT JOIN t8"     {x ex 24 abc 24 y why 25 {} {}}
000674  
000675    4a "SELECT * FROM t8 LEFT JOIN t7 USING (a)"   {x abc 24 ex 24 z ghi 26 {} {}}
000676    4b "SELECT * FROM t8 NATURAL LEFT JOIN t7"     {x abc 24 ex 24 z ghi 26 {} {}}
000677  
000678    5a "SELECT * FROM t3 JOIN t4 USING (a,c)"      {b 2}
000679    5b "SELECT * FROM t3 NATURAL JOIN t4"          {b 2}
000680  
000681    6a "SELECT * FROM t3 LEFT JOIN t4 USING (a,c)" {a 1 b 2}
000682    6b "SELECT * FROM t3 NATURAL LEFT JOIN t4"     {a 1 b 2}
000683  } 
000684  
000685  # EVIDENCE-OF: R-49566-01570 If the left and right-hand input datasets
000686  # feature no common column names, then the NATURAL keyword has no effect
000687  # on the results of the join.
000688  #
000689  do_execsql_test e_select-1.11.0 {
000690    CREATE TABLE t10(x, y);
000691    INSERT INTO t10 VALUES(1, 'true');
000692    INSERT INTO t10 VALUES(0, 'false');
000693  } {}
000694  do_select_tests e_select-1-11 {
000695    1a "SELECT a, x FROM t1 CROSS JOIN t10" {a 1 a 0 b 1 b 0 c 1 c 0}
000696    1b "SELECT a, x FROM t1 NATURAL CROSS JOIN t10" {a 1 a 0 b 1 b 0 c 1 c 0}
000697  }
000698  
000699  # EVIDENCE-OF: R-39625-59133 A USING or ON clause may not be added to a
000700  # join that specifies the NATURAL keyword.
000701  #
000702  foreach {tn sql} {
000703    1 {SELECT * FROM t1 NATURAL LEFT JOIN t2 USING (a)}
000704    2 {SELECT * FROM t1 NATURAL LEFT JOIN t2 ON (t1.a=t2.a)}
000705    3 {SELECT * FROM t1 NATURAL LEFT JOIN t2 ON (45)}
000706  } {
000707    do_catchsql_test e_select-1.12.$tn "
000708      $sql
000709    " {1 {a NATURAL join may not have an ON or USING clause}}
000710  }
000711  
000712  #-------------------------------------------------------------------------
000713  # The next block of tests - e_select-3.* - concentrate on verifying 
000714  # statements made regarding WHERE clause processing.
000715  #
000716  drop_all_tables
000717  do_execsql_test e_select-3.0 {
000718    CREATE TABLE x1(k, x, y, z);
000719    INSERT INTO x1 VALUES(1, 'relinquished', 'aphasia', 78.43);
000720    INSERT INTO x1 VALUES(2, X'A8E8D66F',    X'07CF',   -81);
000721    INSERT INTO x1 VALUES(3, -22,            -27.57,    NULL);
000722    INSERT INTO x1 VALUES(4, NULL,           'bygone',  'picky');
000723    INSERT INTO x1 VALUES(5, NULL,           96.28,     NULL);
000724    INSERT INTO x1 VALUES(6, 0,              1,         2);
000725  
000726    CREATE TABLE x2(k, x, y2);
000727    INSERT INTO x2 VALUES(1, 50, X'B82838');
000728    INSERT INTO x2 VALUES(5, 84.79, 65.88);
000729    INSERT INTO x2 VALUES(3, -22, X'0E1BE452A393');
000730    INSERT INTO x2 VALUES(7, 'mistrusted', 'standardized');
000731  } {}
000732  
000733  # EVIDENCE-OF: R-60775-64916 If a WHERE clause is specified, the WHERE
000734  # expression is evaluated for each row in the input data as a boolean
000735  # expression. Only rows for which the WHERE clause expression evaluates
000736  # to true are included from the dataset before continuing.
000737  #
000738  do_execsql_test e_select-3.1.1 { SELECT k FROM x1 WHERE x }         {3}
000739  do_execsql_test e_select-3.1.2 { SELECT k FROM x1 WHERE y }         {3 5 6}
000740  do_execsql_test e_select-3.1.3 { SELECT k FROM x1 WHERE z }         {1 2 6}
000741  do_execsql_test e_select-3.1.4 { SELECT k FROM x1 WHERE '1'||z    } {1 2 4 6}
000742  do_execsql_test e_select-3.1.5 { SELECT k FROM x1 WHERE x IS NULL } {4 5}
000743  do_execsql_test e_select-3.1.6 { SELECT k FROM x1 WHERE z - 78.43 } {2 4 6}
000744  
000745  do_execsql_test e_select-3.2.1a {
000746    SELECT k FROM x1 LEFT JOIN x2 USING(k)
000747  } {1 2 3 4 5 6}
000748  do_execsql_test e_select-3.2.1b {
000749    SELECT k FROM x1 LEFT JOIN x2 USING(k) WHERE x2.k ORDER BY +k
000750  } {1 3 5}
000751  do_execsql_test e_select-3.2.2 {
000752    SELECT k FROM x1 LEFT JOIN x2 USING(k) WHERE x2.k IS NULL
000753  } {2 4 6}
000754  
000755  do_execsql_test e_select-3.2.3 {
000756    SELECT k FROM x1 NATURAL JOIN x2 WHERE x2.k
000757  } {3}
000758  do_execsql_test e_select-3.2.4 {
000759    SELECT k FROM x1 NATURAL JOIN x2 WHERE x2.k-3
000760  } {}
000761  
000762  #-------------------------------------------------------------------------
000763  # Tests below this point are focused on verifying the testable statements
000764  # related to caculating the result rows of a simple SELECT statement.
000765  #
000766  
000767  drop_all_tables
000768  do_execsql_test e_select-4.0 {
000769    CREATE TABLE z1(a, b, c);
000770    CREATE TABLE z2(d, e);
000771    CREATE TABLE z3(a, b);
000772  
000773    INSERT INTO z1 VALUES(51.65, -59.58, 'belfries');
000774    INSERT INTO z1 VALUES(-5, NULL, 75);
000775    INSERT INTO z1 VALUES(-2.2, -23.18, 'suiters');
000776    INSERT INTO z1 VALUES(NULL, 67, 'quartets');
000777    INSERT INTO z1 VALUES(-1.04, -32.3, 'aspen');
000778    INSERT INTO z1 VALUES(63, 'born', -26);
000779  
000780    INSERT INTO z2 VALUES(NULL, 21);
000781    INSERT INTO z2 VALUES(36, 6);
000782  
000783    INSERT INTO z3 VALUES('subsistence', 'gauze');
000784    INSERT INTO z3 VALUES(49.17, -67);
000785  } {}
000786  
000787  # EVIDENCE-OF: R-36327-17224 If a result expression is the special
000788  # expression "*" then all columns in the input data are substituted for
000789  # that one expression.
000790  #
000791  # EVIDENCE-OF: R-43693-30522 If the expression is the alias of a table
000792  # or subquery in the FROM clause followed by ".*" then all columns from
000793  # the named table or subquery are substituted for the single expression.
000794  #
000795  do_select_tests e_select-4.1 {
000796    1  "SELECT * FROM z1 LIMIT 1"             {51.65 -59.58 belfries}
000797    2  "SELECT * FROM z1,z2 LIMIT 1"          {51.65 -59.58 belfries {} 21}
000798    3  "SELECT z1.* FROM z1,z2 LIMIT 1"       {51.65 -59.58 belfries}
000799    4  "SELECT z2.* FROM z1,z2 LIMIT 1"       {{} 21}
000800    5  "SELECT z2.*, z1.* FROM z1,z2 LIMIT 1" {{} 21 51.65 -59.58 belfries}
000801  
000802    6  "SELECT count(*), * FROM z1"           {6 51.65 -59.58 belfries}
000803    7  "SELECT max(a), * FROM z1"             {63 63 born -26}
000804    8  "SELECT *, min(a) FROM z1"             {-5 {} 75 -5}
000805  
000806    9  "SELECT *,* FROM z1,z2 LIMIT 1" {        
000807       51.65 -59.58 belfries {} 21 51.65 -59.58 belfries {} 21
000808    }
000809    10 "SELECT z1.*,z1.* FROM z2,z1 LIMIT 1" {        
000810       51.65 -59.58 belfries 51.65 -59.58 belfries
000811    }
000812  }
000813  
000814  # EVIDENCE-OF: R-38023-18396 It is an error to use a "*" or "alias.*"
000815  # expression in any context other than a result expression list.
000816  #
000817  # EVIDENCE-OF: R-44324-41166 It is also an error to use a "*" or
000818  # "alias.*" expression in a simple SELECT query that does not have a
000819  # FROM clause.
000820  #
000821  foreach {tn select err} {
000822    1.1  "SELECT a, b, c FROM z1 WHERE *"    {near "*": syntax error}
000823    1.2  "SELECT a, b, c FROM z1 GROUP BY *" {near "*": syntax error}
000824    1.3  "SELECT 1 + * FROM z1"              {near "*": syntax error}
000825    1.4  "SELECT * + 1 FROM z1"              {near "+": syntax error}
000826  
000827    2.1 "SELECT *" {no tables specified}
000828    2.2 "SELECT * WHERE 1" {no tables specified}
000829    2.3 "SELECT * WHERE 0" {no tables specified}
000830    2.4 "SELECT count(*), *" {no tables specified}
000831  } {
000832    do_catchsql_test e_select-4.2.$tn $select [list 1 $err]
000833  }
000834  
000835  # EVIDENCE-OF: R-08669-22397 The number of columns in the rows returned
000836  # by a simple SELECT statement is equal to the number of expressions in
000837  # the result expression list after substitution of * and alias.*
000838  # expressions.
000839  #
000840  foreach {tn select nCol} {
000841    1   "SELECT * FROM z1"   3
000842    2   "SELECT * FROM z1 NATURAL JOIN z3"            3
000843    3   "SELECT z1.* FROM z1 NATURAL JOIN z3"         3
000844    4   "SELECT z3.* FROM z1 NATURAL JOIN z3"         2
000845    5   "SELECT z1.*, z3.* FROM z1 NATURAL JOIN z3"   5
000846    6   "SELECT 1, 2, z1.* FROM z1"                   5
000847    7   "SELECT a, *, b, c FROM z1"                   6
000848  } {
000849    set ::stmt [sqlite3_prepare_v2 db $select -1 DUMMY]
000850    do_test e_select-4.3.$tn { sqlite3_column_count $::stmt } $nCol
000851    sqlite3_finalize $::stmt
000852  }
000853  
000854  
000855  
000856  # In lang_select.html, a non-aggregate query is defined as any simple SELECT
000857  # that has no GROUP BY clause and no aggregate expressions in the result
000858  # expression list. Other queries are aggregate queries. Test cases
000859  # e_select-4.4.* through e_select-4.12.*, inclusive, which test the part of
000860  # simple SELECT that is different for aggregate and non-aggregate queries
000861  # verify (in a way) that these definitions are consistent:
000862  #
000863  # EVIDENCE-OF: R-20637-43463 A simple SELECT statement is an aggregate
000864  # query if it contains either a GROUP BY clause or one or more aggregate
000865  # functions in the result-set.
000866  #
000867  # EVIDENCE-OF: R-23155-55597 Otherwise, if a simple SELECT contains no
000868  # aggregate functions or a GROUP BY clause, it is a non-aggregate query.
000869  #
000870  
000871  # EVIDENCE-OF: R-44050-47362 If the SELECT statement is a non-aggregate
000872  # query, then each expression in the result expression list is evaluated
000873  # for each row in the dataset filtered by the WHERE clause.
000874  #
000875  do_select_tests e_select-4.4 {
000876    1 "SELECT a, b FROM z1"
000877      {51.65 -59.58 -5 {} -2.2 -23.18 {} 67 -1.04 -32.3 63 born}
000878  
000879    2 "SELECT a IS NULL, b+1, * FROM z1" {
000880          0 -58.58   51.65 -59.58 belfries
000881          0 {}       -5 {} 75            
000882          0 -22.18   -2.2 -23.18 suiters
000883          1 68       {} 67 quartets    
000884          0 -31.3    -1.04 -32.3 aspen
000885          0 1        63 born -26
000886    }
000887  
000888    3 "SELECT 32*32, d||e FROM z2" {1024 {} 1024 366}
000889  }
000890  
000891  
000892  # Test cases e_select-4.5.* and e_select-4.6.* together show that:
000893  #
000894  # EVIDENCE-OF: R-51988-01124 The single row of result-set data created
000895  # by evaluating the aggregate and non-aggregate expressions in the
000896  # result-set forms the result of an aggregate query without a GROUP BY
000897  # clause.
000898  #
000899  
000900  # EVIDENCE-OF: R-57629-25253 If the SELECT statement is an aggregate
000901  # query without a GROUP BY clause, then each aggregate expression in the
000902  # result-set is evaluated once across the entire dataset.
000903  #
000904  do_select_tests e_select-4.5 {
000905    1 "SELECT count(a), max(a), count(b), max(b) FROM z1"      {5 63 5 born}
000906    2 "SELECT count(*), max(1)"                                {1 1}
000907  
000908    3 "SELECT sum(b+1) FROM z1 NATURAL LEFT JOIN z3"           {-43.06}
000909    4 "SELECT sum(b+2) FROM z1 NATURAL LEFT JOIN z3"           {-38.06}
000910    5 "SELECT sum(b IS NOT NULL) FROM z1 NATURAL LEFT JOIN z3" {5}
000911  }
000912  
000913  # EVIDENCE-OF: R-26684-40576 Each non-aggregate expression in the
000914  # result-set is evaluated once for an arbitrarily selected row of the
000915  # dataset.
000916  #
000917  # EVIDENCE-OF: R-27994-60376 The same arbitrarily selected row is used
000918  # for each non-aggregate expression.
000919  #
000920  #   Note: The results of many of the queries in this block of tests are
000921  #   technically undefined, as the documentation does not specify which row
000922  #   SQLite will arbitrarily select to use for the evaluation of the
000923  #   non-aggregate expressions.
000924  #
000925  drop_all_tables
000926  do_execsql_test e_select-4.6.0 {
000927    CREATE TABLE a1(one PRIMARY KEY, two);
000928    INSERT INTO a1 VALUES(1, 1);
000929    INSERT INTO a1 VALUES(2, 3);
000930    INSERT INTO a1 VALUES(3, 6);
000931    INSERT INTO a1 VALUES(4, 10);
000932  
000933    CREATE TABLE a2(one PRIMARY KEY, three);
000934    INSERT INTO a2 VALUES(1, 1);
000935    INSERT INTO a2 VALUES(3, 2);
000936    INSERT INTO a2 VALUES(6, 3);
000937    INSERT INTO a2 VALUES(10, 4);
000938  } {}
000939  do_select_tests e_select-4.6 {
000940    1 "SELECT one, two, count(*) FROM a1"                        {1 1 4}
000941    2 "SELECT one, two, count(*) FROM a1 WHERE one<3"            {1 1 2}
000942    3 "SELECT one, two, count(*) FROM a1 WHERE one>3"            {4 10 1} 
000943    4 "SELECT *, count(*) FROM a1 JOIN a2"                       {1 1 1 1 16}
000944    5 "SELECT *, sum(three) FROM a1 NATURAL JOIN a2"             {1 1 1 3}
000945    6 "SELECT *, sum(three) FROM a1 NATURAL JOIN a2"             {1 1 1 3}
000946    7 "SELECT string_agg(three, ''), a1.* FROM a1 NATURAL JOIN a2" {12 1 1}
000947  }
000948  
000949  # EVIDENCE-OF: R-04486-07266 Or, if the dataset contains zero rows, then
000950  # each non-aggregate expression is evaluated against a row consisting
000951  # entirely of NULL values.
000952  #
000953  do_select_tests e_select-4.7 {
000954    1  "SELECT one, two, count(*) FROM a1 WHERE 0"           {{} {} 0}
000955    2  "SELECT sum(two), * FROM a1, a2 WHERE three>5"        {{} {} {} {} {}}
000956    3  "SELECT max(one) IS NULL, one IS NULL, two IS NULL FROM a1 WHERE two=7" {
000957      1 1 1
000958    }
000959  } 
000960  
000961  # EVIDENCE-OF: R-64138-28774 An aggregate query without a GROUP BY
000962  # clause always returns exactly one row of data, even if there are zero
000963  # rows of input data.
000964  #
000965  foreach {tn select} {
000966    8.1  "SELECT count(*) FROM a1"
000967    8.2  "SELECT count(*) FROM a1 WHERE 0"
000968    8.3  "SELECT count(*) FROM a1 WHERE 1"
000969    8.4  "SELECT max(a1.one)+min(two), a1.one, two, * FROM a1, a2 WHERE 1"
000970    8.5  "SELECT max(a1.one)+min(two), a1.one, two, * FROM a1, a2 WHERE 0"
000971  } {
000972    # Set $nRow to the number of rows returned by $select:
000973    set ::stmt [sqlite3_prepare_v2 db $select -1 DUMMY]
000974    set nRow 0
000975    while {"SQLITE_ROW" == [sqlite3_step $::stmt]} { incr nRow }
000976    set rc [sqlite3_finalize $::stmt]
000977  
000978    # Test that $nRow==1 and that statement execution was successful 
000979    # (rc==SQLITE_OK).
000980    do_test e_select-4.$tn [list list $rc $nRow] {SQLITE_OK 1}
000981  }
000982  
000983  drop_all_tables
000984  do_execsql_test e_select-4.9.0 {
000985    CREATE TABLE b1(one PRIMARY KEY, two);
000986    INSERT INTO b1 VALUES(1, 'o');
000987    INSERT INTO b1 VALUES(4, 'f');
000988    INSERT INTO b1 VALUES(3, 't');
000989    INSERT INTO b1 VALUES(2, 't');
000990    INSERT INTO b1 VALUES(5, 'f');
000991    INSERT INTO b1 VALUES(7, 's');
000992    INSERT INTO b1 VALUES(6, 's');
000993  
000994    CREATE TABLE b2(x, y);
000995    INSERT INTO b2 VALUES(NULL, 0);
000996    INSERT INTO b2 VALUES(NULL, 1);
000997    INSERT INTO b2 VALUES('xyz', 2);
000998    INSERT INTO b2 VALUES('abc', 3);
000999    INSERT INTO b2 VALUES('xyz', 4);
001000  
001001    CREATE TABLE b3(a COLLATE nocase, b COLLATE binary);
001002    INSERT INTO b3 VALUES('abc', 'abc');
001003    INSERT INTO b3 VALUES('aBC', 'aBC');
001004    INSERT INTO b3 VALUES('Def', 'Def');
001005    INSERT INTO b3 VALUES('dEF', 'dEF');
001006  } {}
001007  
001008  # EVIDENCE-OF: R-40855-36147 If the SELECT statement is an aggregate
001009  # query with a GROUP BY clause, then each of the expressions specified
001010  # as part of the GROUP BY clause is evaluated for each row of the
001011  # dataset according to the processing rules stated below for ORDER BY
001012  # expressions. Each row is then assigned to a "group" based on the
001013  # results; rows for which the results of evaluating the GROUP BY
001014  # expressions are the same get assigned to the same group.
001015  #
001016  #   These tests also show that the following is not untrue:
001017  #
001018  # EVIDENCE-OF: R-25883-55063 The expressions in the GROUP BY clause do
001019  # not have to be expressions that appear in the result.
001020  #
001021  do_select_tests e_select-4.9 {
001022    1  "SELECT group_concat(one), two FROM b1 GROUP BY two" {
001023      /#,# f   1 o   #,#   s #,# t/
001024    }
001025    2  "SELECT group_concat(one), sum(one) FROM b1 GROUP BY (one>4)" {
001026      1,2,3,4 10    5,6,7 18
001027    }
001028    3  "SELECT group_concat(one) FROM b1 GROUP BY (two>'o'), one%2" {
001029      4  1,5    2,6   3,7
001030    }
001031    4  "SELECT group_concat(one) FROM b1 GROUP BY (one==2 OR two=='o')" {
001032      4,3,5,7,6    1,2
001033    }
001034  }
001035  
001036  # EVIDENCE-OF: R-14926-50129 For the purposes of grouping rows, NULL
001037  # values are considered equal.
001038  #
001039  do_select_tests e_select-4.10 {
001040    1  "SELECT group_concat(y) FROM b2 GROUP BY x" {/#,#   3   #,#/}
001041    2  "SELECT count(*) FROM b2 GROUP BY CASE WHEN y<4 THEN NULL ELSE 0 END" {4 1}
001042  } 
001043  
001044  # EVIDENCE-OF: R-10470-30318 The usual rules for selecting a collation
001045  # sequence with which to compare text values apply when evaluating
001046  # expressions in a GROUP BY clause.
001047  #
001048  do_select_tests e_select-4.11 {
001049    1  "SELECT count(*) FROM b3 GROUP BY b"      {1 1 1 1}
001050    2  "SELECT count(*) FROM b3 GROUP BY a"      {2 2}
001051    3  "SELECT count(*) FROM b3 GROUP BY +b"     {1 1 1 1}
001052    4  "SELECT count(*) FROM b3 GROUP BY +a"     {2 2}
001053    5  "SELECT count(*) FROM b3 GROUP BY b||''"  {1 1 1 1}
001054    6  "SELECT count(*) FROM b3 GROUP BY a||''"  {1 1 1 1}
001055  }
001056  
001057  # EVIDENCE-OF: R-63573-50730 The expressions in a GROUP BY clause may
001058  # not be aggregate expressions.
001059  #
001060  foreach {tn select} {
001061    12.1  "SELECT * FROM b3 GROUP BY count(*)"
001062    12.2  "SELECT max(a) FROM b3 GROUP BY max(b)"
001063    12.3  "SELECT group_concat(a) FROM b3 GROUP BY a, max(b)"
001064  } {
001065    set res {1 {aggregate functions are not allowed in the GROUP BY clause}}
001066    do_catchsql_test e_select-4.$tn $select $res
001067  }
001068  
001069  # EVIDENCE-OF: R-31537-00101 If a HAVING clause is specified, it is
001070  # evaluated once for each group of rows as a boolean expression. If the
001071  # result of evaluating the HAVING clause is false, the group is
001072  # discarded.
001073  #
001074  #   This requirement is tested by all e_select-4.13.* tests.
001075  #
001076  # EVIDENCE-OF: R-04132-09474 If the HAVING clause is an aggregate
001077  # expression, it is evaluated across all rows in the group.
001078  #
001079  #   Tested by e_select-4.13.1.*
001080  #
001081  # EVIDENCE-OF: R-28262-47447 If a HAVING clause is a non-aggregate
001082  # expression, it is evaluated with respect to an arbitrarily selected
001083  # row from the group.
001084  #
001085  #   Tested by e_select-4.13.2.*
001086  #
001087  #   Tests in this block also show that this is not untrue:
001088  #
001089  # EVIDENCE-OF: R-55403-13450 The HAVING expression may refer to values,
001090  # even aggregate functions, that are not in the result.
001091  #
001092  do_execsql_test e_select-4.13.0 {
001093    CREATE TABLE c1(up, down);
001094    INSERT INTO c1 VALUES('x', 1);
001095    INSERT INTO c1 VALUES('x', 2);
001096    INSERT INTO c1 VALUES('x', 4);
001097    INSERT INTO c1 VALUES('x', 8);
001098    INSERT INTO c1 VALUES('y', 16);
001099    INSERT INTO c1 VALUES('y', 32);
001100  
001101    CREATE TABLE c2(i, j);
001102    INSERT INTO c2 VALUES(1, 0);
001103    INSERT INTO c2 VALUES(2, 1);
001104    INSERT INTO c2 VALUES(3, 3);
001105    INSERT INTO c2 VALUES(4, 6);
001106    INSERT INTO c2 VALUES(5, 10);
001107    INSERT INTO c2 VALUES(6, 15);
001108    INSERT INTO c2 VALUES(7, 21);
001109    INSERT INTO c2 VALUES(8, 28);
001110    INSERT INTO c2 VALUES(9, 36);
001111  
001112    CREATE TABLE c3(i PRIMARY KEY, k TEXT);
001113    INSERT INTO c3 VALUES(1,  'hydrogen');
001114    INSERT INTO c3 VALUES(2,  'helium');
001115    INSERT INTO c3 VALUES(3,  'lithium');
001116    INSERT INTO c3 VALUES(4,  'beryllium');
001117    INSERT INTO c3 VALUES(5,  'boron');
001118    INSERT INTO c3 VALUES(94, 'plutonium');
001119  } {}
001120  
001121  do_select_tests e_select-4.13 {
001122    1.1  "SELECT up FROM c1 GROUP BY up HAVING count(*)>3" {x}
001123    1.2  "SELECT up FROM c1 GROUP BY up HAVING sum(down)>16" {y}
001124    1.3  "SELECT up FROM c1 GROUP BY up HAVING sum(down)<16" {x}
001125    1.4  "SELECT up||down FROM c1 GROUP BY (down<5) HAVING max(down)<10" {x4}
001126  
001127    2.1  "SELECT up FROM c1 GROUP BY up HAVING down>10" {y}
001128    2.2  "SELECT up FROM c1 GROUP BY up HAVING up='y'"  {y}
001129  
001130    2.3  "SELECT i, j FROM c2 GROUP BY i>4 HAVING j>6"  {5 10}
001131  }
001132  
001133  # EVIDENCE-OF: R-23927-54081 Each expression in the result-set is then
001134  # evaluated once for each group of rows.
001135  #
001136  # EVIDENCE-OF: R-53735-47017 If the expression is an aggregate
001137  # expression, it is evaluated across all rows in the group.
001138  #
001139  do_select_tests e_select-4.15 {
001140    1  "SELECT sum(down) FROM c1 GROUP BY up" {15 48}
001141    2  "SELECT sum(j), max(j) FROM c2 GROUP BY (i%3)"     {54 36 27 21 39 28}
001142    3  "SELECT sum(j), max(j) FROM c2 GROUP BY (j%2)"     {80 36 40 21}
001143    4  "SELECT 1+sum(j), max(j)+1 FROM c2 GROUP BY (j%2)" {81 37 41 22}
001144    5  "SELECT count(*), round(avg(i),2) FROM c1, c2 ON (i=down) GROUP BY j%2"
001145          {3 4.33 1 2.0}
001146  } 
001147  
001148  # EVIDENCE-OF: R-62913-19830 Otherwise, it is evaluated against a single
001149  # arbitrarily chosen row from within the group.
001150  #
001151  # EVIDENCE-OF: R-53924-08809 If there is more than one non-aggregate
001152  # expression in the result-set, then all such expressions are evaluated
001153  # for the same row.
001154  #
001155  do_select_tests e_select-4.15 {
001156    1  "SELECT i, j FROM c2 GROUP BY i%2"             {2 1 1 0}
001157    2  "SELECT i, j FROM c2 GROUP BY i%2 HAVING j<30" {2 1 1 0}
001158    3  "SELECT i, j FROM c2 GROUP BY i%2 HAVING j>30" {}
001159    4  "SELECT i, j FROM c2 GROUP BY i%2 HAVING j>30" {}
001160    5  "SELECT count(*), i, k FROM c2 NATURAL JOIN c3 GROUP BY substr(k, 1, 1)"
001161          {2 4 beryllium 2 1 hydrogen 1 3 lithium}
001162  } 
001163  
001164  # EVIDENCE-OF: R-19334-12811 Each group of input dataset rows
001165  # contributes a single row to the set of result rows.
001166  #
001167  # EVIDENCE-OF: R-02223-49279 Subject to filtering associated with the
001168  # DISTINCT keyword, the number of rows returned by an aggregate query
001169  # with a GROUP BY clause is the same as the number of groups of rows
001170  # produced by applying the GROUP BY and HAVING clauses to the filtered
001171  # input dataset.
001172  #
001173  do_select_tests e_select.4.16 -count {
001174    1  "SELECT i, j FROM c2 GROUP BY i%2"          2
001175    2  "SELECT i, j FROM c2 GROUP BY i"            9
001176    3  "SELECT i, j FROM c2 GROUP BY i HAVING i<5" 4
001177  } 
001178  
001179  #-------------------------------------------------------------------------
001180  # The following tests attempt to verify statements made regarding the ALL
001181  # and DISTINCT keywords.
001182  #
001183  drop_all_tables
001184  do_execsql_test e_select-5.1.0 {
001185    CREATE TABLE h1(a, b);
001186    INSERT INTO h1 VALUES(1, 'one');
001187    INSERT INTO h1 VALUES(1, 'I');
001188    INSERT INTO h1 VALUES(1, 'i');
001189    INSERT INTO h1 VALUES(4, 'four');
001190    INSERT INTO h1 VALUES(4, 'IV');
001191    INSERT INTO h1 VALUES(4, 'iv');
001192  
001193    CREATE TABLE h2(x COLLATE nocase);
001194    INSERT INTO h2 VALUES('One');
001195    INSERT INTO h2 VALUES('Two');
001196    INSERT INTO h2 VALUES('Three');
001197    INSERT INTO h2 VALUES('Four');
001198    INSERT INTO h2 VALUES('one');
001199    INSERT INTO h2 VALUES('two');
001200    INSERT INTO h2 VALUES('three');
001201    INSERT INTO h2 VALUES('four');
001202  
001203    CREATE TABLE h3(c, d);
001204    INSERT INTO h3 VALUES(1, NULL);
001205    INSERT INTO h3 VALUES(2, NULL);
001206    INSERT INTO h3 VALUES(3, NULL);
001207    INSERT INTO h3 VALUES(4, '2');
001208    INSERT INTO h3 VALUES(5, NULL);
001209    INSERT INTO h3 VALUES(6, '2,3');
001210    INSERT INTO h3 VALUES(7, NULL);
001211    INSERT INTO h3 VALUES(8, '2,4');
001212    INSERT INTO h3 VALUES(9, '3');
001213  } {}
001214  
001215  # EVIDENCE-OF: R-60770-10612 One of the ALL or DISTINCT keywords may
001216  # follow the SELECT keyword in a simple SELECT statement.
001217  #
001218  do_select_tests e_select-5.1 {
001219    1   "SELECT ALL a FROM h1"      {1 1 1 4 4 4}
001220    2   "SELECT DISTINCT a FROM h1" {1 4}
001221  }
001222  
001223  # EVIDENCE-OF: R-08861-34280 If the simple SELECT is a SELECT ALL, then
001224  # the entire set of result rows are returned by the SELECT.
001225  #
001226  # EVIDENCE-OF: R-01256-01950 If neither ALL or DISTINCT are present,
001227  # then the behavior is as if ALL were specified.
001228  #
001229  # EVIDENCE-OF: R-14442-41305 If the simple SELECT is a SELECT DISTINCT,
001230  # then duplicate rows are removed from the set of result rows before it
001231  # is returned.
001232  #
001233  #   The three testable statements above are tested by e_select-5.2.*,
001234  #   5.3.* and 5.4.* respectively.
001235  #
001236  do_select_tests e_select-5 {
001237    3.1 "SELECT ALL x FROM h2" {One Two Three Four one two three four}
001238    3.2 "SELECT ALL x FROM h1, h2 ON (x=b)" {One one Four four}
001239  
001240    3.1 "SELECT x FROM h2" {One Two Three Four one two three four}
001241    3.2 "SELECT x FROM h1, h2 ON (x=b)" {One one Four four}
001242  
001243    4.1 "SELECT DISTINCT x FROM h2" {One Two Three Four}
001244    4.2 "SELECT DISTINCT x FROM h1, h2 ON (x=b)" {One Four}
001245  } 
001246  
001247  # EVIDENCE-OF: R-02054-15343 For the purposes of detecting duplicate
001248  # rows, two NULL values are considered to be equal.
001249  #
001250  do_select_tests e_select-5.5 {
001251    1  "SELECT DISTINCT d FROM h3" {{} 2 2,3 2,4 3}
001252  }
001253  
001254  # EVIDENCE-OF: R-47709-27231 The usual rules apply for selecting a
001255  # collation sequence to compare text values.
001256  #
001257  do_select_tests e_select-5.6 {
001258    1  "SELECT DISTINCT b FROM h1"                  {one I i four IV iv}
001259    2  "SELECT DISTINCT b COLLATE nocase FROM h1"   {one I four IV}
001260    3  "SELECT DISTINCT x FROM h2"                  {One Two Three Four}
001261    4  "SELECT DISTINCT x COLLATE binary FROM h2"   {
001262      One Two Three Four one two three four
001263    }
001264  }
001265  
001266  #-------------------------------------------------------------------------
001267  # The following tests - e_select-7.* - test that statements made to do
001268  # with compound SELECT statements are correct.
001269  #
001270  
001271  # EVIDENCE-OF: R-39368-64333 In a compound SELECT, all the constituent
001272  # SELECTs must return the same number of result columns.
001273  #
001274  #   All the other tests in this section use compound SELECTs created
001275  #   using component SELECTs that do return the same number of columns.
001276  #   So the tests here just show that it is an error to attempt otherwise.
001277  #
001278  drop_all_tables
001279  do_execsql_test e_select-7.1.0 {
001280    CREATE TABLE j1(a, b, c);
001281    CREATE TABLE j2(e, f);
001282    CREATE TABLE j3(g);
001283  } {}
001284  do_select_tests e_select-7.1 -error {
001285    SELECTs to the left and right of %s do not have the same number of result columns
001286  } {
001287    1   "SELECT a, b FROM j1    UNION ALL SELECT g FROM j3"    {{UNION ALL}}
001288    2   "SELECT *    FROM j1    UNION ALL SELECT * FROM j3"    {{UNION ALL}}
001289    3   "SELECT a, b FROM j1    UNION ALL SELECT g FROM j3"    {{UNION ALL}}
001290    4   "SELECT a, b FROM j1    UNION ALL SELECT * FROM j3,j2" {{UNION ALL}}
001291    5   "SELECT *    FROM j3,j2 UNION ALL SELECT a, b FROM j1" {{UNION ALL}}
001292  
001293    6   "SELECT a, b FROM j1    UNION SELECT g FROM j3"        {UNION}
001294    7   "SELECT *    FROM j1    UNION SELECT * FROM j3"        {UNION}
001295    8   "SELECT a, b FROM j1    UNION SELECT g FROM j3"        {UNION}
001296    9   "SELECT a, b FROM j1    UNION SELECT * FROM j3,j2"     {UNION}
001297    10  "SELECT *    FROM j3,j2 UNION SELECT a, b FROM j1"     {UNION}
001298  
001299    11  "SELECT a, b FROM j1    INTERSECT SELECT g FROM j3"    {INTERSECT}
001300    12  "SELECT *    FROM j1    INTERSECT SELECT * FROM j3"    {INTERSECT}
001301    13  "SELECT a, b FROM j1    INTERSECT SELECT g FROM j3"    {INTERSECT}
001302    14  "SELECT a, b FROM j1    INTERSECT SELECT * FROM j3,j2" {INTERSECT}
001303    15  "SELECT *    FROM j3,j2 INTERSECT SELECT a, b FROM j1" {INTERSECT}
001304  
001305    16  "SELECT a, b FROM j1    EXCEPT SELECT g FROM j3"       {EXCEPT}
001306    17  "SELECT *    FROM j1    EXCEPT SELECT * FROM j3"       {EXCEPT}
001307    18  "SELECT a, b FROM j1    EXCEPT SELECT g FROM j3"       {EXCEPT}
001308    19  "SELECT a, b FROM j1    EXCEPT SELECT * FROM j3,j2"    {EXCEPT}
001309    20  "SELECT *    FROM j3,j2 EXCEPT SELECT a, b FROM j1"    {EXCEPT}
001310  } 
001311  
001312  # EVIDENCE-OF: R-01450-11152 As the components of a compound SELECT must
001313  # be simple SELECT statements, they may not contain ORDER BY or LIMIT
001314  # clauses.
001315  # 
001316  foreach {tn select op1 op2} {
001317    1   "SELECT * FROM j1 ORDER BY a UNION ALL SELECT * FROM j2,j3" 
001318        {ORDER BY} {UNION ALL}
001319    2   "SELECT count(*) FROM j1 ORDER BY 1 UNION ALL SELECT max(e) FROM j2"
001320        {ORDER BY} {UNION ALL}
001321    3   "SELECT count(*), * FROM j1 ORDER BY 1,2,3 UNION ALL SELECT *,* FROM j2"
001322        {ORDER BY} {UNION ALL}
001323    4   "SELECT * FROM j1 LIMIT 10 UNION ALL SELECT * FROM j2,j3" 
001324        LIMIT {UNION ALL}
001325    5   "SELECT * FROM j1 LIMIT 10 OFFSET 5 UNION ALL SELECT * FROM j2,j3" 
001326        LIMIT {UNION ALL}
001327    6   "SELECT a FROM j1 LIMIT (SELECT e FROM j2) UNION ALL SELECT g FROM j2,j3" 
001328        LIMIT {UNION ALL}
001329  
001330    7   "SELECT * FROM j1 ORDER BY a UNION SELECT * FROM j2,j3" 
001331        {ORDER BY} {UNION}
001332    8   "SELECT count(*) FROM j1 ORDER BY 1 UNION SELECT max(e) FROM j2"
001333        {ORDER BY} {UNION}
001334    9   "SELECT count(*), * FROM j1 ORDER BY 1,2,3 UNION SELECT *,* FROM j2"
001335        {ORDER BY} {UNION}
001336    10  "SELECT * FROM j1 LIMIT 10 UNION SELECT * FROM j2,j3" 
001337        LIMIT {UNION}
001338    11  "SELECT * FROM j1 LIMIT 10 OFFSET 5 UNION SELECT * FROM j2,j3" 
001339        LIMIT {UNION}
001340    12  "SELECT a FROM j1 LIMIT (SELECT e FROM j2) UNION SELECT g FROM j2,j3" 
001341        LIMIT {UNION}
001342  
001343    13  "SELECT * FROM j1 ORDER BY a EXCEPT SELECT * FROM j2,j3" 
001344        {ORDER BY} {EXCEPT}
001345    14  "SELECT count(*) FROM j1 ORDER BY 1 EXCEPT SELECT max(e) FROM j2"
001346        {ORDER BY} {EXCEPT}
001347    15  "SELECT count(*), * FROM j1 ORDER BY 1,2,3 EXCEPT SELECT *,* FROM j2"
001348        {ORDER BY} {EXCEPT}
001349    16  "SELECT * FROM j1 LIMIT 10 EXCEPT SELECT * FROM j2,j3" 
001350        LIMIT {EXCEPT}
001351    17  "SELECT * FROM j1 LIMIT 10 OFFSET 5 EXCEPT SELECT * FROM j2,j3" 
001352        LIMIT {EXCEPT}
001353    18  "SELECT a FROM j1 LIMIT (SELECT e FROM j2) EXCEPT SELECT g FROM j2,j3" 
001354        LIMIT {EXCEPT}
001355  
001356    19  "SELECT * FROM j1 ORDER BY a INTERSECT SELECT * FROM j2,j3" 
001357        {ORDER BY} {INTERSECT}
001358    20  "SELECT count(*) FROM j1 ORDER BY 1 INTERSECT SELECT max(e) FROM j2"
001359        {ORDER BY} {INTERSECT}
001360    21  "SELECT count(*), * FROM j1 ORDER BY 1,2,3 INTERSECT SELECT *,* FROM j2"
001361        {ORDER BY} {INTERSECT}
001362    22  "SELECT * FROM j1 LIMIT 10 INTERSECT SELECT * FROM j2,j3" 
001363        LIMIT {INTERSECT}
001364    23  "SELECT * FROM j1 LIMIT 10 OFFSET 5 INTERSECT SELECT * FROM j2,j3" 
001365        LIMIT {INTERSECT}
001366    24  "SELECT a FROM j1 LIMIT (SELECT e FROM j2) INTERSECT SELECT g FROM j2,j3" 
001367        LIMIT {INTERSECT}
001368  } {
001369    set err "$op1 clause should come after $op2 not before"
001370    do_catchsql_test e_select-7.2.$tn $select [list 1 $err]
001371  }
001372  
001373  # EVIDENCE-OF: R-45440-25633 ORDER BY and LIMIT clauses may only occur
001374  # at the end of the entire compound SELECT, and then only if the final
001375  # element of the compound is not a VALUES clause.
001376  #
001377  foreach {tn select} {
001378    1   "SELECT * FROM j1 UNION ALL SELECT * FROM j2,j3 ORDER BY a"
001379    2   "SELECT count(*) FROM j1 UNION ALL SELECT max(e) FROM j2 ORDER BY 1"
001380    3   "SELECT count(*), * FROM j1 UNION ALL SELECT *,* FROM j2 ORDER BY 1,2,3"
001381    4   "SELECT * FROM j1 UNION ALL SELECT * FROM j2,j3 LIMIT 10" 
001382    5   "SELECT * FROM j1 UNION ALL SELECT * FROM j2,j3 LIMIT 10 OFFSET 5" 
001383    6   "SELECT a FROM j1 UNION ALL SELECT g FROM j2,j3 LIMIT (SELECT 10)" 
001384  
001385    7   "SELECT * FROM j1 UNION SELECT * FROM j2,j3 ORDER BY a"
001386    8   "SELECT count(*) FROM j1 UNION SELECT max(e) FROM j2 ORDER BY 1"
001387    8b  "VALUES('8b') UNION SELECT max(e) FROM j2 ORDER BY 1"
001388    9   "SELECT count(*), * FROM j1 UNION SELECT *,* FROM j2 ORDER BY 1,2,3"
001389    10  "SELECT * FROM j1 UNION SELECT * FROM j2,j3 LIMIT 10" 
001390    11  "SELECT * FROM j1 UNION SELECT * FROM j2,j3 LIMIT 10 OFFSET 5" 
001391    12  "SELECT a FROM j1 UNION SELECT g FROM j2,j3 LIMIT (SELECT 10)" 
001392  
001393    13  "SELECT * FROM j1 EXCEPT SELECT * FROM j2,j3 ORDER BY a"
001394    14  "SELECT count(*) FROM j1 EXCEPT SELECT max(e) FROM j2 ORDER BY 1"
001395    15  "SELECT count(*), * FROM j1 EXCEPT SELECT *,* FROM j2 ORDER BY 1,2,3"
001396    16  "SELECT * FROM j1 EXCEPT SELECT * FROM j2,j3 LIMIT 10" 
001397    17  "SELECT * FROM j1 EXCEPT SELECT * FROM j2,j3 LIMIT 10 OFFSET 5" 
001398    18  "SELECT a FROM j1 EXCEPT SELECT g FROM j2,j3 LIMIT (SELECT 10)" 
001399  
001400    19  "SELECT * FROM j1 INTERSECT SELECT * FROM j2,j3 ORDER BY a"
001401    20  "SELECT count(*) FROM j1 INTERSECT SELECT max(e) FROM j2 ORDER BY 1"
001402    21  "SELECT count(*), * FROM j1 INTERSECT SELECT *,* FROM j2 ORDER BY 1,2,3"
001403    22  "SELECT * FROM j1 INTERSECT SELECT * FROM j2,j3 LIMIT 10" 
001404    23  "SELECT * FROM j1 INTERSECT SELECT * FROM j2,j3 LIMIT 10 OFFSET 5" 
001405    24  "SELECT a FROM j1 INTERSECT SELECT g FROM j2,j3 LIMIT (SELECT 10)" 
001406  } {
001407    do_test e_select-7.3.$tn { catch {execsql $select} msg } 0
001408  }
001409  foreach {tn select} {
001410    50   "SELECT * FROM j1 ORDER BY 1 UNION ALL SELECT * FROM j2,j3"
001411    51   "SELECT * FROM j1 LIMIT 1 UNION ALL SELECT * FROM j2,j3"
001412    52   "SELECT count(*) FROM j1 UNION ALL VALUES(11) ORDER BY 1"
001413    53   "SELECT count(*) FROM j1 UNION ALL VALUES(11) LIMIT 1"
001414  } {
001415    do_test e_select-7.3.$tn { catch {execsql $select} msg } 1
001416  }
001417  
001418  # EVIDENCE-OF: R-08531-36543 A compound SELECT created using UNION ALL
001419  # operator returns all the rows from the SELECT to the left of the UNION
001420  # ALL operator, and all the rows from the SELECT to the right of it.
001421  #
001422  drop_all_tables
001423  do_execsql_test e_select-7.4.0 {
001424    CREATE TABLE q1(a TEXT, b INTEGER, c);
001425    CREATE TABLE q2(d NUMBER, e BLOB);
001426    CREATE TABLE q3(f REAL, g);
001427  
001428    INSERT INTO q1 VALUES(16, -87.66, NULL);
001429    INSERT INTO q1 VALUES('legible', 94, -42.47);
001430    INSERT INTO q1 VALUES('beauty', 36, NULL);
001431  
001432    INSERT INTO q2 VALUES('legible', 1);
001433    INSERT INTO q2 VALUES('beauty', 2);
001434    INSERT INTO q2 VALUES(-65.91, 4);
001435    INSERT INTO q2 VALUES('emanating', -16.56);
001436  
001437    INSERT INTO q3 VALUES('beauty', 2);
001438    INSERT INTO q3 VALUES('beauty', 2);
001439  } {}
001440  do_select_tests e_select-7.4 {
001441    1   {SELECT a FROM q1 UNION ALL SELECT d FROM q2}
001442        {16 legible beauty legible beauty -65.91 emanating}
001443  
001444    2   {SELECT * FROM q1 WHERE a=16 UNION ALL SELECT 'x', * FROM q2 WHERE oid=1}
001445        {16 -87.66 {} x legible 1}
001446  
001447    3   {SELECT count(*) FROM q1 UNION ALL SELECT min(e) FROM q2} 
001448        {3 -16.56}
001449  
001450    4   {SELECT * FROM q2 UNION ALL SELECT * FROM q3} 
001451        {legible 1 beauty 2 -65.91 4 emanating -16.56 beauty 2 beauty 2}
001452  } 
001453  
001454  # EVIDENCE-OF: R-20560-39162 The UNION operator works the same way as
001455  # UNION ALL, except that duplicate rows are removed from the final
001456  # result set.
001457  #
001458  do_select_tests e_select-7.5 {
001459    1   {SELECT a FROM q1 UNION SELECT d FROM q2}
001460        {-65.91 16 beauty emanating legible}
001461  
001462    2   {SELECT * FROM q1 WHERE a=16 UNION SELECT 'x', * FROM q2 WHERE oid=1}
001463        {16 -87.66 {} x legible 1}
001464  
001465    3   {SELECT count(*) FROM q1 UNION SELECT min(e) FROM q2} 
001466        {-16.56 3}
001467  
001468    4   {SELECT * FROM q2 UNION SELECT * FROM q3} 
001469        {-65.91 4 beauty 2 emanating -16.56 legible 1}
001470  } 
001471  
001472  # EVIDENCE-OF: R-45764-31737 The INTERSECT operator returns the
001473  # intersection of the results of the left and right SELECTs.
001474  #
001475  do_select_tests e_select-7.6 {
001476    1   {SELECT a FROM q1 INTERSECT SELECT d FROM q2} {beauty legible}
001477    2   {SELECT * FROM q2 INTERSECT SELECT * FROM q3} {beauty 2}
001478  }
001479  
001480  # EVIDENCE-OF: R-25787-28949 The EXCEPT operator returns the subset of
001481  # rows returned by the left SELECT that are not also returned by the
001482  # right-hand SELECT.
001483  #
001484  do_select_tests e_select-7.7 {
001485    1   {SELECT a FROM q1 EXCEPT SELECT d FROM q2} {16}
001486  
001487    2   {SELECT * FROM q2 EXCEPT SELECT * FROM q3} 
001488        {-65.91 4 emanating -16.56 legible 1}
001489  }
001490  
001491  # EVIDENCE-OF: R-40729-56447 Duplicate rows are removed from the results
001492  # of INTERSECT and EXCEPT operators before the result set is returned.
001493  #
001494  do_select_tests e_select-7.8 {
001495    0   {SELECT * FROM q3} {beauty 2 beauty 2}
001496  
001497    1   {SELECT * FROM q3 INTERSECT SELECT * FROM q3} {beauty 2}
001498    2   {SELECT * FROM q3 EXCEPT SELECT a,b FROM q1}  {beauty 2}
001499  }
001500  
001501  # EVIDENCE-OF: R-46765-43362 For the purposes of determining duplicate
001502  # rows for the results of compound SELECT operators, NULL values are
001503  # considered equal to other NULL values and distinct from all non-NULL
001504  # values.
001505  #
001506  db nullvalue null
001507  do_select_tests e_select-7.9 {
001508    1   {SELECT NULL UNION ALL SELECT NULL} {null null}
001509    2   {SELECT NULL UNION     SELECT NULL} {null}
001510    3   {SELECT NULL INTERSECT SELECT NULL} {null}
001511    4   {SELECT NULL EXCEPT    SELECT NULL} {}
001512  
001513    5   {SELECT NULL UNION ALL SELECT 'ab'} {null ab}
001514    6   {SELECT NULL UNION     SELECT 'ab'} {null ab}
001515    7   {SELECT NULL INTERSECT SELECT 'ab'} {}
001516    8   {SELECT NULL EXCEPT    SELECT 'ab'} {null}
001517  
001518    9   {SELECT NULL UNION ALL SELECT 0} {null 0}
001519    10  {SELECT NULL UNION     SELECT 0} {null 0}
001520    11  {SELECT NULL INTERSECT SELECT 0} {}
001521    12  {SELECT NULL EXCEPT    SELECT 0} {null}
001522  
001523    13  {SELECT c FROM q1 UNION ALL SELECT g FROM q3} {null -42.47 null 2 2}
001524    14  {SELECT c FROM q1 UNION     SELECT g FROM q3} {null -42.47 2}
001525    15  {SELECT c FROM q1 INTERSECT SELECT g FROM q3} {}
001526    16  {SELECT c FROM q1 EXCEPT    SELECT g FROM q3} {null -42.47}
001527  }
001528  db nullvalue {} 
001529  
001530  # EVIDENCE-OF: R-51232-50224 The collation sequence used to compare two
001531  # text values is determined as if the columns of the left and right-hand
001532  # SELECT statements were the left and right-hand operands of the equals
001533  # (=) operator, except that greater precedence is not assigned to a
001534  # collation sequence specified with the postfix COLLATE operator.
001535  #
001536  drop_all_tables
001537  do_execsql_test e_select-7.10.0 {
001538    CREATE TABLE y1(a COLLATE nocase, b COLLATE binary, c);
001539    INSERT INTO y1 VALUES('Abc', 'abc', 'aBC');
001540  } {}
001541  do_select_tests e_select-7.10 {
001542    1   {SELECT 'abc'                UNION SELECT 'ABC'} {ABC abc}
001543    2   {SELECT 'abc' COLLATE nocase UNION SELECT 'ABC'} {ABC}
001544    3   {SELECT 'abc'                UNION SELECT 'ABC' COLLATE nocase} {ABC}
001545    4   {SELECT 'abc' COLLATE binary UNION SELECT 'ABC' COLLATE nocase} {ABC abc}
001546    5   {SELECT 'abc' COLLATE nocase UNION SELECT 'ABC' COLLATE binary} {ABC}
001547  
001548    6   {SELECT a FROM y1 UNION SELECT b FROM y1}                {abc}
001549    7   {SELECT b FROM y1 UNION SELECT a FROM y1}                {Abc abc}
001550    8   {SELECT a FROM y1 UNION SELECT c FROM y1}                {aBC}
001551  
001552    9   {SELECT a FROM y1 UNION SELECT c COLLATE binary FROM y1} {aBC}
001553  }
001554  
001555  # EVIDENCE-OF: R-32706-07403 No affinity transformations are applied to
001556  # any values when comparing rows as part of a compound SELECT.
001557  #
001558  drop_all_tables
001559  do_execsql_test e_select-7.10.0 {
001560    CREATE TABLE w1(a TEXT, b NUMBER);
001561    CREATE TABLE w2(a, b TEXT);
001562  
001563    INSERT INTO w1 VALUES('1', 4.1);
001564    INSERT INTO w2 VALUES(1, 4.1);
001565  } {}
001566  
001567  do_select_tests e_select-7.11 {
001568    1  { SELECT a FROM w1 UNION SELECT a FROM w2 } {1 1}
001569    2  { SELECT a FROM w2 UNION SELECT a FROM w1 } {1 1}
001570    3  { SELECT b FROM w1 UNION SELECT b FROM w2 } {4.1 4.1}
001571    4  { SELECT b FROM w2 UNION SELECT b FROM w1 } {4.1 4.1}
001572  
001573    5  { SELECT a FROM w1 INTERSECT SELECT a FROM w2 } {}
001574    6  { SELECT a FROM w2 INTERSECT SELECT a FROM w1 } {}
001575    7  { SELECT b FROM w1 INTERSECT SELECT b FROM w2 } {}
001576    8  { SELECT b FROM w2 INTERSECT SELECT b FROM w1 } {}
001577  
001578    9  { SELECT a FROM w1 EXCEPT SELECT a FROM w2 } {1}
001579    10 { SELECT a FROM w2 EXCEPT SELECT a FROM w1 } {1}
001580    11 { SELECT b FROM w1 EXCEPT SELECT b FROM w2 } {4.1}
001581    12 { SELECT b FROM w2 EXCEPT SELECT b FROM w1 } {4.1}
001582  }
001583  
001584  
001585  # EVIDENCE-OF: R-32562-20566 When three or more simple SELECTs are
001586  # connected into a compound SELECT, they group from left to right. In
001587  # other words, if "A", "B" and "C" are all simple SELECT statements, (A
001588  # op B op C) is processed as ((A op B) op C).
001589  #
001590  #   e_select-7.12.1: Precedence of UNION vs. INTERSECT 
001591  #   e_select-7.12.2: Precedence of UNION vs. UNION ALL 
001592  #   e_select-7.12.3: Precedence of UNION vs. EXCEPT
001593  #   e_select-7.12.4: Precedence of INTERSECT vs. UNION ALL 
001594  #   e_select-7.12.5: Precedence of INTERSECT vs. EXCEPT
001595  #   e_select-7.12.6: Precedence of UNION ALL vs. EXCEPT
001596  #   e_select-7.12.7: Check that "a EXCEPT b EXCEPT c" is processed as 
001597  #                   "(a EXCEPT b) EXCEPT c".
001598  #
001599  # The INTERSECT and EXCEPT operations are mutually commutative. So
001600  # the e_select-7.12.5 test cases do not prove very much.
001601  #
001602  drop_all_tables
001603  do_execsql_test e_select-7.12.0 {
001604    CREATE TABLE t1(x);
001605    INSERT INTO t1 VALUES(1);
001606    INSERT INTO t1 VALUES(2);
001607    INSERT INTO t1 VALUES(3);
001608  } {}
001609  foreach {tn select res} {
001610    1a "(1,2) INTERSECT (1)   UNION     (3)"   {1 3}
001611    1b "(3)   UNION     (1,2) INTERSECT (1)"   {1}
001612  
001613    2a "(1,2) UNION     (3)   UNION ALL (1)"   {1 2 3 1}
001614    2b "(1)   UNION ALL (3)   UNION     (1,2)" {1 2 3}
001615  
001616    3a "(1,2) UNION     (3)   EXCEPT    (1)"   {2 3}
001617    3b "(1,2) EXCEPT    (3)   UNION     (1)"   {1 2}
001618  
001619    4a "(1,2) INTERSECT (1)   UNION ALL (3)"   {1 3}
001620    4b "(3)   UNION     (1,2) INTERSECT (1)"   {1}
001621  
001622    5a "(1,2) INTERSECT (2)   EXCEPT    (2)"   {}
001623    5b "(2,3) EXCEPT    (2)   INTERSECT (2)"   {}
001624  
001625    6a "(2)   UNION ALL (2)   EXCEPT    (2)"   {}
001626    6b "(2)   EXCEPT    (2)   UNION ALL (2)"   {2}
001627  
001628    7  "(2,3) EXCEPT    (2)   EXCEPT    (3)"   {}
001629  } {
001630    set select [string map {( {SELECT x FROM t1 WHERE x IN (}} $select]
001631    do_execsql_test e_select-7.12.$tn $select [list {*}$res]
001632  }
001633  
001634  
001635  #-------------------------------------------------------------------------
001636  # ORDER BY clauses
001637  #
001638  
001639  drop_all_tables
001640  do_execsql_test e_select-8.1.0 {
001641    CREATE TABLE d1(x, y, z);
001642  
001643    INSERT INTO d1 VALUES(1, 2, 3);
001644    INSERT INTO d1 VALUES(2, 5, -1);
001645    INSERT INTO d1 VALUES(1, 2, 8);
001646    INSERT INTO d1 VALUES(1, 2, 7);
001647    INSERT INTO d1 VALUES(2, 4, 93);
001648    INSERT INTO d1 VALUES(1, 2, -20);
001649    INSERT INTO d1 VALUES(1, 4, 93);
001650    INSERT INTO d1 VALUES(1, 5, -1);
001651  
001652    CREATE TABLE d2(a, b);
001653    INSERT INTO d2 VALUES('gently', 'failings');
001654    INSERT INTO d2 VALUES('commercials', 'bathrobe');
001655    INSERT INTO d2 VALUES('iterate', 'sexton');
001656    INSERT INTO d2 VALUES('babied', 'charitableness');
001657    INSERT INTO d2 VALUES('solemnness', 'annexed');
001658    INSERT INTO d2 VALUES('rejoicing', 'liabilities');
001659    INSERT INTO d2 VALUES('pragmatist', 'guarded');
001660    INSERT INTO d2 VALUES('barked', 'interrupted');
001661    INSERT INTO d2 VALUES('reemphasizes', 'reply');
001662    INSERT INTO d2 VALUES('lad', 'relenting');
001663  } {}
001664  
001665  # EVIDENCE-OF: R-44988-41064 Rows are first sorted based on the results
001666  # of evaluating the left-most expression in the ORDER BY list, then ties
001667  # are broken by evaluating the second left-most expression and so on.
001668  #
001669  do_select_tests e_select-8.1 {
001670    1  "SELECT * FROM d1 ORDER BY x, y, z" {
001671       1 2 -20    1 2 3    1 2 7    1 2 8    
001672       1 4  93    1 5 -1   2 4 93   2 5 -1
001673    }
001674  }
001675  
001676  # EVIDENCE-OF: R-06617-54588 Each ORDER BY expression may be optionally
001677  # followed by one of the keywords ASC (smaller values are returned
001678  # first) or DESC (larger values are returned first).
001679  #
001680  #   Test cases e_select-8.2.* test the above.
001681  #
001682  # EVIDENCE-OF: R-18705-33393 If neither ASC or DESC are specified, rows
001683  # are sorted in ascending (smaller values first) order by default.
001684  #
001685  #   Test cases e_select-8.3.* test the above. All 8.3 test cases are
001686  #   copies of 8.2 test cases with the explicit "ASC" removed.
001687  #
001688  do_select_tests e_select-8 {
001689    2.1  "SELECT * FROM d1 ORDER BY x ASC, y ASC, z ASC" {
001690       1 2 -20    1 2 3    1 2 7    1 2 8    
001691       1 4  93    1 5 -1   2 4 93   2 5 -1
001692    }
001693    2.2  "SELECT * FROM d1 ORDER BY x DESC, y DESC, z DESC" {
001694       2 5 -1     2 4 93   1 5 -1   1 4  93    
001695       1 2 8      1 2 7    1 2 3    1 2 -20    
001696    }
001697    2.3 "SELECT * FROM d1 ORDER BY x DESC, y ASC, z DESC" {
001698       2 4 93   2 5 -1     1 2 8      1 2 7    
001699       1 2 3    1 2 -20    1 4  93    1 5 -1   
001700    }
001701    2.4  "SELECT * FROM d1 ORDER BY x DESC, y ASC, z ASC" {
001702       2 4 93   2 5 -1     1 2 -20    1 2 3    
001703       1 2 7    1 2 8      1 4  93    1 5 -1   
001704    }
001705  
001706    3.1  "SELECT * FROM d1 ORDER BY x, y, z" {
001707       1 2 -20    1 2 3    1 2 7    1 2 8    
001708       1 4  93    1 5 -1   2 4 93   2 5 -1
001709    }
001710    3.3  "SELECT * FROM d1 ORDER BY x DESC, y, z DESC" {
001711       2 4 93   2 5 -1     1 2 8      1 2 7    
001712       1 2 3    1 2 -20    1 4  93    1 5 -1   
001713    }
001714    3.4 "SELECT * FROM d1 ORDER BY x DESC, y, z" {
001715       2 4 93   2 5 -1     1 2 -20    1 2 3    
001716       1 2 7    1 2 8      1 4  93    1 5 -1   
001717    }
001718  }
001719  
001720  # EVIDENCE-OF: R-29779-04281 If the ORDER BY expression is a constant
001721  # integer K then the expression is considered an alias for the K-th
001722  # column of the result set (columns are numbered from left to right
001723  # starting with 1).
001724  #
001725  do_select_tests e_select-8.4 {
001726    1  "SELECT * FROM d1 ORDER BY 1 ASC, 2 ASC, 3 ASC" {
001727       1 2 -20    1 2 3    1 2 7    1 2 8    
001728       1 4  93    1 5 -1   2 4 93   2 5 -1
001729    }
001730    2  "SELECT * FROM d1 ORDER BY 1 DESC, 2 DESC, 3 DESC" {
001731       2 5 -1     2 4 93   1 5 -1   1 4  93    
001732       1 2 8      1 2 7    1 2 3    1 2 -20    
001733    }
001734    3 "SELECT * FROM d1 ORDER BY 1 DESC, 2 ASC, 3 DESC" {
001735       2 4 93   2 5 -1     1 2 8      1 2 7    
001736       1 2 3    1 2 -20    1 4  93    1 5 -1   
001737    }
001738    4  "SELECT * FROM d1 ORDER BY 1 DESC, 2 ASC, 3 ASC" {
001739       2 4 93   2 5 -1     1 2 -20    1 2 3    
001740       1 2 7    1 2 8      1 4  93    1 5 -1   
001741    }
001742    5  "SELECT * FROM d1 ORDER BY 1, 2, 3" {
001743       1 2 -20    1 2 3    1 2 7    1 2 8    
001744       1 4  93    1 5 -1   2 4 93   2 5 -1
001745    }
001746    6  "SELECT * FROM d1 ORDER BY 1 DESC, 2, 3 DESC" {
001747       2 4 93   2 5 -1     1 2 8      1 2 7    
001748       1 2 3    1 2 -20    1 4  93    1 5 -1   
001749    }
001750    7  "SELECT * FROM d1 ORDER BY 1 DESC, 2, 3" {
001751       2 4 93   2 5 -1     1 2 -20    1 2 3    
001752       1 2 7    1 2 8      1 4  93    1 5 -1   
001753    }
001754    8  "SELECT z, x FROM d1 ORDER BY 2" {
001755       /# 1    # 1    # 1   # 1 
001756        # 1    # 1    # 2   # 2/
001757    }
001758    9  "SELECT z, x FROM d1 ORDER BY 1" {
001759       /-20 1  -1 #   -1 #   3 1
001760       7 1     8 1   93 #   93 #/   
001761    }
001762  }
001763  
001764  # EVIDENCE-OF: R-63286-51977 If the ORDER BY expression is an identifier
001765  # that corresponds to the alias of one of the output columns, then the
001766  # expression is considered an alias for that column.
001767  #
001768  do_select_tests e_select-8.5 {
001769    1   "SELECT z+1 AS abc FROM d1 ORDER BY abc" {
001770      -19 0 0 4 8 9 94 94
001771    }
001772    2   "SELECT z+1 AS abc FROM d1 ORDER BY abc DESC" {
001773      94 94 9 8 4 0 0 -19
001774    }
001775    3  "SELECT z AS x, x AS z FROM d1 ORDER BY z" {
001776      /# 1    # 1    # 1    # 1    # 1    # 1    # 2    # 2/
001777    }
001778    4  "SELECT z AS x, x AS z FROM d1 ORDER BY x" {
001779      /-20 1    -1 #    -1 #    3 1    7 1    8 1    93 #    93 #/
001780    }
001781  }
001782  
001783  # EVIDENCE-OF: R-65068-27207 Otherwise, if the ORDER BY expression is
001784  # any other expression, it is evaluated and the returned value used to
001785  # order the output rows.
001786  #
001787  # EVIDENCE-OF: R-03421-57988 If the SELECT statement is a simple SELECT,
001788  # then an ORDER BY may contain any arbitrary expressions.
001789  #
001790  do_select_tests e_select-8.6 {
001791    1   "SELECT * FROM d1 ORDER BY x+y+z" {
001792      1 2 -20    1 5 -1    1 2 3    2 5 -1 
001793      1 2 7      1 2 8     1 4 93   2 4 93
001794    }
001795    2   "SELECT * FROM d1 ORDER BY x*z" {
001796      1 2 -20    2 5 -1    1 5 -1    1 2 3 
001797      1 2 7      1 2 8     1 4 93    2 4 93
001798    }
001799    3   "SELECT * FROM d1 ORDER BY y*z" {
001800      1 2 -20    2 5 -1    1 5 -1    1 2 3 
001801      1 2 7      1 2 8     2 4 93    1 4 93
001802    }
001803  }
001804  
001805  # EVIDENCE-OF: R-28853-08147 However, if the SELECT is a compound
001806  # SELECT, then ORDER BY expressions that are not aliases to output
001807  # columns must be exactly the same as an expression used as an output
001808  # column.
001809  #
001810  do_select_tests e_select-8.7.1 -error {
001811    %s ORDER BY term does not match any column in the result set
001812  } {
001813    1   "SELECT x FROM d1 UNION ALL SELECT a FROM d2 ORDER BY x*z"        1st
001814    2   "SELECT x,z FROM d1 UNION ALL SELECT a,b FROM d2 ORDER BY x, x/z" 2nd
001815  } 
001816  
001817  do_select_tests e_select-8.7.2 {
001818    1   "SELECT x*z FROM d1 UNION ALL SELECT a FROM d2 ORDER BY x*z" {
001819      -20 -2 -1 3 7 8 93 186 babied barked commercials gently 
001820      iterate lad pragmatist reemphasizes rejoicing solemnness
001821    }
001822    2   "SELECT x, x/z FROM d1 UNION ALL SELECT a,b FROM d2 ORDER BY x, x/z" {
001823      1 -1 1 0 1 0 1 0 1 0 1 0 2 -2 2 0 
001824      babied charitableness barked interrupted commercials bathrobe gently
001825      failings iterate sexton lad relenting pragmatist guarded reemphasizes reply
001826      rejoicing liabilities solemnness annexed
001827    }
001828  } 
001829  
001830  do_execsql_test e_select-8.8.0 {
001831    CREATE TABLE d3(a);
001832    INSERT INTO d3 VALUES('text');
001833    INSERT INTO d3 VALUES(14.1);
001834    INSERT INTO d3 VALUES(13);
001835    INSERT INTO d3 VALUES(X'78787878');
001836    INSERT INTO d3 VALUES(15);
001837    INSERT INTO d3 VALUES(12.9);
001838    INSERT INTO d3 VALUES(null);
001839  
001840    CREATE TABLE d4(x COLLATE nocase);
001841    INSERT INTO d4 VALUES('abc');
001842    INSERT INTO d4 VALUES('ghi');
001843    INSERT INTO d4 VALUES('DEF');
001844    INSERT INTO d4 VALUES('JKL');
001845  } {}
001846  
001847  # EVIDENCE-OF: R-10883-17697 For the purposes of sorting rows, values
001848  # are compared in the same way as for comparison expressions.
001849  #
001850  #   The following tests verify that values of different types are sorted
001851  #   correctly, and that mixed real and integer values are compared properly.
001852  #
001853  do_execsql_test e_select-8.8.1 {
001854    SELECT a FROM d3 ORDER BY a
001855  } {{} 12.9 13 14.1 15 text xxxx}
001856  do_execsql_test e_select-8.8.2 {
001857    SELECT a FROM d3 ORDER BY a DESC
001858  } {xxxx text 15 14.1 13 12.9 {}}
001859  
001860  
001861  # EVIDENCE-OF: R-64199-22471 If the ORDER BY expression is assigned a
001862  # collation sequence using the postfix COLLATE operator, then the
001863  # specified collation sequence is used.
001864  #
001865  do_execsql_test e_select-8.9.1 {
001866    SELECT x FROM d4 ORDER BY 1 COLLATE binary
001867  } {DEF JKL abc ghi}
001868  do_execsql_test e_select-8.9.2 {
001869    SELECT x COLLATE binary FROM d4 ORDER BY 1 COLLATE nocase
001870  } {abc DEF ghi JKL}
001871  
001872  # EVIDENCE-OF: R-09398-26102 Otherwise, if the ORDER BY expression is 
001873  # an alias to an expression that has been assigned a collation sequence 
001874  # using the postfix COLLATE operator, then the collation sequence 
001875  # assigned to the aliased expression is used.
001876  #
001877  #   In the test 8.10.2, the only result-column expression has no alias. So the
001878  #   ORDER BY expression is not a reference to it and therefore does not inherit
001879  #   the collation sequence. In test 8.10.3, "x" is the alias (as well as the
001880  #   column name), so the ORDER BY expression is interpreted as an alias and the
001881  #   collation sequence attached to the result column is used for sorting.
001882  #
001883  do_execsql_test e_select-8.10.1 {
001884    SELECT x COLLATE binary FROM d4 ORDER BY 1
001885  } {DEF JKL abc ghi}
001886  do_execsql_test e_select-8.10.2 {
001887    SELECT x COLLATE binary FROM d4 ORDER BY x
001888  } {abc DEF ghi JKL}
001889  do_execsql_test e_select-8.10.3 {
001890    SELECT x COLLATE binary AS x FROM d4 ORDER BY x
001891  } {DEF JKL abc ghi}
001892  
001893  # EVIDENCE-OF: R-27301-09658 Otherwise, if the ORDER BY expression is a
001894  # column or an alias of an expression that is a column, then the default
001895  # collation sequence for the column is used.
001896  #
001897  do_execsql_test e_select-8.11.1 {
001898    SELECT x AS y FROM d4 ORDER BY y
001899  } {abc DEF ghi JKL}
001900  do_execsql_test e_select-8.11.2 {
001901    SELECT x||'' FROM d4 ORDER BY x
001902  } {abc DEF ghi JKL}
001903  
001904  # EVIDENCE-OF: R-49925-55905 Otherwise, the BINARY collation sequence is
001905  # used.
001906  #
001907  do_execsql_test e_select-8.12.1 {
001908    SELECT x FROM d4 ORDER BY x||''
001909  } {DEF JKL abc ghi}
001910  
001911  # EVIDENCE-OF: R-44130-32593 If an ORDER BY expression is not an integer
001912  # alias, then SQLite searches the left-most SELECT in the compound for a
001913  # result column that matches either the second or third rules above. If
001914  # a match is found, the search stops and the expression is handled as an
001915  # alias for the result column that it has been matched against.
001916  # Otherwise, the next SELECT to the right is tried, and so on.
001917  #
001918  do_execsql_test e_select-8.13.0 {
001919    CREATE TABLE d5(a, b);
001920    CREATE TABLE d6(c, d);
001921    CREATE TABLE d7(e, f);
001922   
001923    INSERT INTO d5 VALUES(1, 'f');
001924    INSERT INTO d6 VALUES(2, 'e');
001925    INSERT INTO d7 VALUES(3, 'd');
001926    INSERT INTO d5 VALUES(4, 'c');
001927    INSERT INTO d6 VALUES(5, 'b');
001928    INSERT INTO d7 VALUES(6, 'a');
001929  
001930    CREATE TABLE d8(x COLLATE nocase);
001931    CREATE TABLE d9(y COLLATE nocase);
001932  
001933    INSERT INTO d8 VALUES('a');
001934    INSERT INTO d9 VALUES('B');
001935    INSERT INTO d8 VALUES('c');
001936    INSERT INTO d9 VALUES('D');
001937  } {}
001938  do_select_tests e_select-8.13 {
001939    1   { SELECT a FROM d5 UNION ALL SELECT c FROM d6 UNION ALL SELECT e FROM d7
001940           ORDER BY a
001941        } {1 2 3 4 5 6}
001942    2   { SELECT a FROM d5 UNION ALL SELECT c FROM d6 UNION ALL SELECT e FROM d7
001943           ORDER BY c
001944        } {1 2 3 4 5 6}
001945    3   { SELECT a FROM d5 UNION ALL SELECT c FROM d6 UNION ALL SELECT e FROM d7
001946           ORDER BY e
001947        } {1 2 3 4 5 6}
001948    4   { SELECT a FROM d5 UNION ALL SELECT c FROM d6 UNION ALL SELECT e FROM d7
001949           ORDER BY 1
001950        } {1 2 3 4 5 6}
001951  
001952    5   { SELECT a, b FROM d5 UNION ALL SELECT b, a FROM d5 ORDER BY b } 
001953        {f 1   c 4   4 c   1 f}
001954    6   { SELECT a, b FROM d5 UNION ALL SELECT b, a FROM d5 ORDER BY 2 } 
001955        {f 1   c 4   4 c   1 f}
001956  
001957    7   { SELECT a, b FROM d5 UNION ALL SELECT b, a FROM d5 ORDER BY a } 
001958        {1 f   4 c   c 4   f 1}
001959    8   { SELECT a, b FROM d5 UNION ALL SELECT b, a FROM d5 ORDER BY 1 } 
001960        {1 f   4 c   c 4   f 1}
001961  
001962    9   { SELECT a, b FROM d5 UNION ALL SELECT b, a+1 FROM d5 ORDER BY a+1 } 
001963        {f 2   c 5   4 c   1 f}
001964    10  { SELECT a, b FROM d5 UNION ALL SELECT b, a+1 FROM d5 ORDER BY 2 } 
001965        {f 2   c 5   4 c   1 f}
001966  
001967    11  { SELECT a+1, b FROM d5 UNION ALL SELECT b, a+1 FROM d5 ORDER BY a+1 } 
001968        {2 f   5 c   c 5   f 2}
001969    12  { SELECT a+1, b FROM d5 UNION ALL SELECT b, a+1 FROM d5 ORDER BY 1 } 
001970        {2 f   5 c   c 5   f 2}
001971  } 
001972  
001973  # EVIDENCE-OF: R-39265-04070 If no matching expression can be found in
001974  # the result columns of any constituent SELECT, it is an error.
001975  #
001976  do_select_tests e_select-8.14 -error {
001977    %s ORDER BY term does not match any column in the result set
001978  } {
001979    1   { SELECT a FROM d5 UNION SELECT c FROM d6 ORDER BY a+1 }          1st
001980    2   { SELECT a FROM d5 UNION SELECT c FROM d6 ORDER BY a, a+1 }       2nd
001981    3   { SELECT * FROM d5 INTERSECT SELECT * FROM d6 ORDER BY 'hello' }  1st
001982    4   { SELECT * FROM d5 INTERSECT SELECT * FROM d6 ORDER BY blah    }  1st
001983    5   { SELECT * FROM d5 INTERSECT SELECT * FROM d6 ORDER BY c,d,c+d }  3rd
001984    6   { SELECT * FROM d5 EXCEPT SELECT * FROM d7 ORDER BY 1,2,b,a/b  }  4th
001985  } 
001986  
001987  # EVIDENCE-OF: R-03407-11483 Each term of the ORDER BY clause is
001988  # processed separately and may be matched against result columns from
001989  # different SELECT statements in the compound.
001990  # 
001991  do_select_tests e_select-8.15 {
001992    1  { SELECT a, b FROM d5 UNION ALL SELECT c-1, d FROM d6 ORDER BY a, d }
001993       {1 e   1 f   4 b   4 c}
001994    2  { SELECT a, b FROM d5 UNION ALL SELECT c-1, d FROM d6 ORDER BY c-1, b }
001995       {1 e   1 f   4 b   4 c}
001996    3  { SELECT a, b FROM d5 UNION ALL SELECT c-1, d FROM d6 ORDER BY 1, 2 }
001997       {1 e   1 f   4 b   4 c}
001998  } 
001999  
002000  
002001  #-------------------------------------------------------------------------
002002  # Tests related to statements made about the LIMIT/OFFSET clause.
002003  #
002004  do_execsql_test e_select-9.0 {
002005    CREATE TABLE f1(a, b);
002006    INSERT INTO f1 VALUES(26, 'z');
002007    INSERT INTO f1 VALUES(25, 'y');
002008    INSERT INTO f1 VALUES(24, 'x');
002009    INSERT INTO f1 VALUES(23, 'w');
002010    INSERT INTO f1 VALUES(22, 'v');
002011    INSERT INTO f1 VALUES(21, 'u');
002012    INSERT INTO f1 VALUES(20, 't');
002013    INSERT INTO f1 VALUES(19, 's');
002014    INSERT INTO f1 VALUES(18, 'r');
002015    INSERT INTO f1 VALUES(17, 'q');
002016    INSERT INTO f1 VALUES(16, 'p');
002017    INSERT INTO f1 VALUES(15, 'o');
002018    INSERT INTO f1 VALUES(14, 'n');
002019    INSERT INTO f1 VALUES(13, 'm');
002020    INSERT INTO f1 VALUES(12, 'l');
002021    INSERT INTO f1 VALUES(11, 'k');
002022    INSERT INTO f1 VALUES(10, 'j');
002023    INSERT INTO f1 VALUES(9, 'i');
002024    INSERT INTO f1 VALUES(8, 'h');
002025    INSERT INTO f1 VALUES(7, 'g');
002026    INSERT INTO f1 VALUES(6, 'f');
002027    INSERT INTO f1 VALUES(5, 'e');
002028    INSERT INTO f1 VALUES(4, 'd');
002029    INSERT INTO f1 VALUES(3, 'c');
002030    INSERT INTO f1 VALUES(2, 'b');
002031    INSERT INTO f1 VALUES(1, 'a');
002032  } {}
002033  
002034  # EVIDENCE-OF: R-30481-56627 Any scalar expression may be used in the
002035  # LIMIT clause, so long as it evaluates to an integer or a value that
002036  # can be losslessly converted to an integer.
002037  #
002038  do_select_tests e_select-9.1 {
002039    1  { SELECT b FROM f1 ORDER BY a LIMIT 5 } {a b c d e}
002040    2  { SELECT b FROM f1 ORDER BY a LIMIT 2+3 } {a b c d e}
002041    3  { SELECT b FROM f1 ORDER BY a LIMIT (SELECT a FROM f1 WHERE b = 'e') } 
002042       {a b c d e}
002043    4  { SELECT b FROM f1 ORDER BY a LIMIT 5.0 } {a b c d e}
002044    5  { SELECT b FROM f1 ORDER BY a LIMIT '5' } {a b c d e}
002045  }
002046  
002047  # EVIDENCE-OF: R-46155-47219 If the expression evaluates to a NULL value
002048  # or any other value that cannot be losslessly converted to an integer,
002049  # an error is returned.
002050  #
002051  
002052  do_select_tests e_select-9.2 -error "datatype mismatch" {
002053    1  { SELECT b FROM f1 ORDER BY a LIMIT 'hello' } {}
002054    2  { SELECT b FROM f1 ORDER BY a LIMIT NULL } {}
002055    3  { SELECT b FROM f1 ORDER BY a LIMIT X'ABCD' } {}
002056    4  { SELECT b FROM f1 ORDER BY a LIMIT 5.1 } {}
002057    5  { SELECT b FROM f1 ORDER BY a LIMIT (SELECT group_concat(b) FROM f1) } {}
002058  } 
002059  
002060  # EVIDENCE-OF: R-03014-26414 If the LIMIT expression evaluates to a
002061  # negative value, then there is no upper bound on the number of rows
002062  # returned.
002063  #
002064  do_select_tests e_select-9.4 {
002065    1  { SELECT b FROM f1 ORDER BY a LIMIT -1 } 
002066       {a b c d e f g h i j k l m n o p q r s t u v w x y z}
002067    2  { SELECT b FROM f1 ORDER BY a LIMIT length('abc')-100 } 
002068       {a b c d e f g h i j k l m n o p q r s t u v w x y z}
002069    3  { SELECT b FROM f1 ORDER BY a LIMIT (SELECT count(*) FROM f1)/2 - 14 }
002070       {a b c d e f g h i j k l m n o p q r s t u v w x y z}
002071  }
002072  
002073  # EVIDENCE-OF: R-33750-29536 Otherwise, the SELECT returns the first N
002074  # rows of its result set only, where N is the value that the LIMIT
002075  # expression evaluates to.
002076  #
002077  do_select_tests e_select-9.5 {
002078    1  { SELECT b FROM f1 ORDER BY a LIMIT 0 } {}
002079    2  { SELECT b FROM f1 ORDER BY a DESC LIMIT 4 } {z y x w}
002080    3  { SELECT b FROM f1 ORDER BY a DESC LIMIT 8 } {z y x w v u t s}
002081    4  { SELECT b FROM f1 ORDER BY a DESC LIMIT '12.0' } {z y x w v u t s r q p o}
002082  }
002083  
002084  # EVIDENCE-OF: R-54935-19057 Or, if the SELECT statement would return
002085  # less than N rows without a LIMIT clause, then the entire result set is
002086  # returned.
002087  #
002088  do_select_tests e_select-9.6 {
002089    1  { SELECT b FROM f1 WHERE a>21 ORDER BY a LIMIT 10 } {v w x y z}
002090    2  { SELECT count(*) FROM f1 GROUP BY a/5 ORDER BY 1 LIMIT 10 } {2 4 5 5 5 5}
002091  } 
002092  
002093  
002094  # EVIDENCE-OF: R-24188-24349 The expression attached to the optional
002095  # OFFSET clause that may follow a LIMIT clause must also evaluate to an
002096  # integer, or a value that can be losslessly converted to an integer.
002097  #
002098  foreach {tn select} {
002099    1  { SELECT b FROM f1 ORDER BY a LIMIT 2 OFFSET 'hello' } 
002100    2  { SELECT b FROM f1 ORDER BY a LIMIT 2 OFFSET NULL } 
002101    3  { SELECT b FROM f1 ORDER BY a LIMIT 2 OFFSET X'ABCD' } 
002102    4  { SELECT b FROM f1 ORDER BY a LIMIT 2 OFFSET 5.1 } 
002103    5  { SELECT b FROM f1 ORDER BY a 
002104         LIMIT 2 OFFSET (SELECT group_concat(b) FROM f1) 
002105    } 
002106  } {
002107    do_catchsql_test e_select-9.7.$tn $select {1 {datatype mismatch}}
002108  }
002109  
002110  # EVIDENCE-OF: R-20467-43422 If an expression has an OFFSET clause, then
002111  # the first M rows are omitted from the result set returned by the
002112  # SELECT statement and the next N rows are returned, where M and N are
002113  # the values that the OFFSET and LIMIT clauses evaluate to,
002114  # respectively.
002115  #
002116  do_select_tests e_select-9.8 {
002117    1  { SELECT b FROM f1 ORDER BY a LIMIT 10 OFFSET 5} {f g h i j k l m n o}
002118    2  { SELECT b FROM f1 ORDER BY a LIMIT 2+3 OFFSET 10} {k l m n o}
002119    3  { SELECT b FROM f1 ORDER BY a 
002120         LIMIT  (SELECT a FROM f1 WHERE b='j') 
002121         OFFSET (SELECT a FROM f1 WHERE b='b') 
002122       } {c d e f g h i j k l}
002123    4  { SELECT b FROM f1 ORDER BY a LIMIT '5' OFFSET 3.0 } {d e f g h}
002124    5  { SELECT b FROM f1 ORDER BY a LIMIT '5' OFFSET 0 } {a b c d e}
002125    6  { SELECT b FROM f1 ORDER BY a LIMIT 0 OFFSET 10 } {}
002126    7  { SELECT b FROM f1 ORDER BY a LIMIT 3 OFFSET '1'||'5' } {p q r}
002127  }
002128  
002129  # EVIDENCE-OF: R-34648-44875 Or, if the SELECT would return less than
002130  # M+N rows if it did not have a LIMIT clause, then the first M rows are
002131  # skipped and the remaining rows (if any) are returned.
002132  #
002133  do_select_tests e_select-9.9 {
002134    1  { SELECT b FROM f1 ORDER BY a LIMIT 10 OFFSET 20} {u v w x y z}
002135    2  { SELECT a FROM f1 ORDER BY a DESC LIMIT 100 OFFSET 18+4} {4 3 2 1}
002136  }
002137  
002138  
002139  # EVIDENCE-OF: R-23293-62447 If the OFFSET clause evaluates to a
002140  # negative value, the results are the same as if it had evaluated to
002141  # zero.
002142  #
002143  do_select_tests e_select-9.10 {
002144    1  { SELECT b FROM f1 ORDER BY a LIMIT 5 OFFSET -1 } {a b c d e}
002145    2  { SELECT b FROM f1 ORDER BY a LIMIT 5 OFFSET -500 } {a b c d e}
002146    3  { SELECT b FROM f1 ORDER BY a LIMIT 5 OFFSET 0  } {a b c d e}
002147  } 
002148  
002149  # EVIDENCE-OF: R-19509-40356 Instead of a separate OFFSET clause, the
002150  # LIMIT clause may specify two scalar expressions separated by a comma.
002151  #
002152  # EVIDENCE-OF: R-33788-46243 In this case, the first expression is used
002153  # as the OFFSET expression and the second as the LIMIT expression.
002154  #
002155  do_select_tests e_select-9.11 {
002156    1  { SELECT b FROM f1 ORDER BY a LIMIT 5, 10 } {f g h i j k l m n o}
002157    2  { SELECT b FROM f1 ORDER BY a LIMIT 10, 2+3 } {k l m n o}
002158    3  { SELECT b FROM f1 ORDER BY a 
002159         LIMIT (SELECT a FROM f1 WHERE b='b'), (SELECT a FROM f1 WHERE b='j') 
002160       } {c d e f g h i j k l}
002161    4  { SELECT b FROM f1 ORDER BY a LIMIT 3.0, '5' } {d e f g h}
002162    5  { SELECT b FROM f1 ORDER BY a LIMIT 0, '5' } {a b c d e}
002163    6  { SELECT b FROM f1 ORDER BY a LIMIT 10, 0 } {}
002164    7  { SELECT b FROM f1 ORDER BY a LIMIT '1'||'5', 3 } {p q r}
002165  
002166    8  { SELECT b FROM f1 ORDER BY a LIMIT 20, 10 } {u v w x y z}
002167    9  { SELECT a FROM f1 ORDER BY a DESC LIMIT 18+4, 100 } {4 3 2 1}
002168  
002169    10 { SELECT b FROM f1 ORDER BY a LIMIT -1, 5 } {a b c d e}
002170    11 { SELECT b FROM f1 ORDER BY a LIMIT -500, 5 } {a b c d e}
002171    12 { SELECT b FROM f1 ORDER BY a LIMIT 0, 5 } {a b c d e}
002172  }
002173  
002174  finish_test