sl@0: # 2007 Dec 4 sl@0: # sl@0: # The author disclaims copyright to this source code. In place of sl@0: # a legal notice, here is a blessing: sl@0: # sl@0: # May you do good and not evil. sl@0: # May you find forgiveness for yourself and forgive others. sl@0: # May you share freely, never taking more than you give. sl@0: # sl@0: #*********************************************************************** sl@0: # sl@0: # This file is to test that the issues surrounding expressions in sl@0: # ORDER BY clauses on compound SELECT statements raised by ticket sl@0: # #2822 have been dealt with. sl@0: # sl@0: # $Id: tkt2822.test,v 1.6 2008/08/20 16:35:10 drh Exp $ sl@0: # sl@0: sl@0: set testdir [file dirname $argv0] sl@0: source $testdir/tester.tcl sl@0: sl@0: ifcapable !compound { sl@0: finish_test sl@0: return sl@0: } sl@0: sl@0: # The ORDER BY matching algorithm is three steps: sl@0: # sl@0: # (1) If the ORDER BY term is an integer constant i, then sl@0: # sort by the i-th column of the result set. sl@0: # sl@0: # (2) If the ORDER BY term is an identifier (not x.y or x.y.z sl@0: # but simply x) then look for a column alias with the same sl@0: # name. If found, then sort by that column. sl@0: # sl@0: # (3) Evaluate the term as an expression and sort by the sl@0: # value of the expression. sl@0: # sl@0: # For a compound SELECT the rules are modified slightly. sl@0: # In the third rule, the expression must exactly match one sl@0: # of the result columns. The sequences of three rules is sl@0: # attempted first on the left-most SELECT. If that doesn't sl@0: # work, we move to the right, one by one. sl@0: # sl@0: # Rule (3) is not in standard SQL - it is an SQLite extension, sl@0: # though one copied from PostgreSQL. The rule for compound sl@0: # queries where a search is made of SELECTs to the right sl@0: # if the left-most SELECT does not match is not a part of sl@0: # standard SQL either. This extension is unique to SQLite sl@0: # as far as we know. sl@0: # sl@0: # Rule (2) was added by the changes ticket #2822. Prior to sl@0: # that changes, SQLite did not support rule (2), making it sl@0: # technically in violation of standard SQL semantics. sl@0: # No body noticed because rule (3) has the same effect as sl@0: # rule (2) except in some obscure cases. sl@0: # sl@0: sl@0: sl@0: # Test plan: sl@0: # sl@0: # tkt2822-1.* - Simple identifier as ORDER BY expression. sl@0: # tkt2822-2.* - More complex ORDER BY expressions. sl@0: sl@0: do_test tkt2822-0.1 { sl@0: execsql { sl@0: CREATE TABLE t1(a, b, c); sl@0: CREATE TABLE t2(a, b, c); sl@0: sl@0: INSERT INTO t1 VALUES(1, 3, 9); sl@0: INSERT INTO t1 VALUES(3, 9, 27); sl@0: INSERT INTO t1 VALUES(5, 15, 45); sl@0: sl@0: INSERT INTO t2 VALUES(2, 6, 18); sl@0: INSERT INTO t2 VALUES(4, 12, 36); sl@0: INSERT INTO t2 VALUES(6, 18, 54); sl@0: } sl@0: } {} sl@0: sl@0: # Test the "ORDER BY " syntax. sl@0: # sl@0: do_test tkt2822-1.1 { sl@0: execsql { sl@0: SELECT a, b, c FROM t1 UNION ALL SELECT a, b, c FROM t2 ORDER BY 1; sl@0: } sl@0: } {1 3 9 2 6 18 3 9 27 4 12 36 5 15 45 6 18 54} sl@0: do_test tkt2822-1.2 { sl@0: execsql { sl@0: SELECT a, CAST (b AS TEXT), c FROM t1 sl@0: UNION ALL sl@0: SELECT a, b, c FROM t2 sl@0: ORDER BY 2; sl@0: } sl@0: } {2 6 18 4 12 36 6 18 54 5 15 45 1 3 9 3 9 27} sl@0: sl@0: # Test the "ORDER BY " syntax. sl@0: # sl@0: do_test tkt2822-2.1 { sl@0: execsql { sl@0: SELECT a, b, c FROM t1 UNION ALL SELECT a, b, c FROM t2 ORDER BY a; sl@0: } sl@0: } {1 3 9 2 6 18 3 9 27 4 12 36 5 15 45 6 18 54} sl@0: sl@0: do_test tkt2822-2.2 { sl@0: execsql { sl@0: SELECT a, CAST (b AS TEXT) AS x, c FROM t1 sl@0: UNION ALL sl@0: SELECT a, b, c FROM t2 sl@0: ORDER BY x; sl@0: } sl@0: } {2 6 18 4 12 36 6 18 54 5 15 45 1 3 9 3 9 27} sl@0: do_test tkt2822-2.3 { sl@0: execsql { sl@0: SELECT t1.a, b, c FROM t1 UNION ALL SELECT t2.a, b, c FROM t2 ORDER BY a; sl@0: } sl@0: } {1 3 9 2 6 18 3 9 27 4 12 36 5 15 45 6 18 54} sl@0: sl@0: # Test the "ORDER BY " syntax. sl@0: # sl@0: do_test tkt2822-3.1 { sl@0: execsql { sl@0: SELECT a, CAST (b AS TEXT) AS x, c FROM t1 sl@0: UNION ALL sl@0: SELECT a, b, c FROM t2 sl@0: ORDER BY CAST (b AS TEXT); sl@0: } sl@0: } {2 6 18 4 12 36 6 18 54 5 15 45 1 3 9 3 9 27} sl@0: do_test tkt2822-3.2 { sl@0: execsql { sl@0: SELECT t1.a, b, c FROM t1 UNION ALL SELECT t2.a, b, c FROM t2 ORDER BY t1.a; sl@0: } sl@0: } {1 3 9 2 6 18 3 9 27 4 12 36 5 15 45 6 18 54} sl@0: sl@0: # Test that if a match cannot be found in the leftmost SELECT, an sl@0: # attempt is made to find a match in subsequent SELECT statements. sl@0: # sl@0: do_test tkt2822-3.3 { sl@0: execsql { sl@0: SELECT a, b, c FROM t1 UNION ALL SELECT a AS x, b, c FROM t2 ORDER BY x; sl@0: } sl@0: } {1 3 9 2 6 18 3 9 27 4 12 36 5 15 45 6 18 54} sl@0: do_test tkt2822-3.4 { sl@0: # But the leftmost SELECT takes precedence. sl@0: execsql { sl@0: SELECT a AS b, CAST (b AS TEXT) AS a, c FROM t1 sl@0: UNION ALL sl@0: SELECT a, b, c FROM t2 sl@0: ORDER BY a; sl@0: } sl@0: } {2 6 18 4 12 36 6 18 54 5 15 45 1 3 9 3 9 27} sl@0: do_test tkt2822-3.5 { sl@0: execsql { sl@0: SELECT a, b, c FROM t2 sl@0: UNION ALL sl@0: SELECT a AS b, CAST (b AS TEXT) AS a, c FROM t1 sl@0: ORDER BY a; sl@0: } sl@0: } {1 3 9 2 6 18 3 9 27 4 12 36 5 15 45 6 18 54} sl@0: sl@0: # Test some error conditions (ORDER BY clauses that match no column). sl@0: # sl@0: do_test tkt2822-4.1 { sl@0: catchsql { sl@0: SELECT a, b, c FROM t1 UNION ALL SELECT a, b, c FROM t2 ORDER BY x sl@0: } sl@0: } {1 {1st ORDER BY term does not match any column in the result set}} sl@0: do_test tkt2822-4.2 { sl@0: catchsql { sl@0: SELECT a, CAST (b AS TEXT) AS x, c FROM t1 sl@0: UNION ALL sl@0: SELECT a, b, c FROM t2 sl@0: ORDER BY CAST (b AS INTEGER); sl@0: } sl@0: } {1 {1st ORDER BY term does not match any column in the result set}} sl@0: sl@0: # Tests for rule (2). sl@0: # sl@0: # The "ORDER BY b" should match the column alias (rule 2), not the sl@0: # the t3.b value (rule 3). sl@0: # sl@0: do_test tkt2822-5.1 { sl@0: execsql { sl@0: CREATE TABLE t3(a,b); sl@0: INSERT INTO t3 VALUES(1,8); sl@0: INSERT INTO t3 VALUES(9,2); sl@0: sl@0: SELECT a AS b FROM t3 ORDER BY b; sl@0: } sl@0: } {1 9} sl@0: do_test tkt2822-5.2 { sl@0: # Case does not matter. b should match B sl@0: execsql { sl@0: SELECT a AS b FROM t3 ORDER BY B; sl@0: } sl@0: } {1 9} sl@0: do_test tkt2822-5.3 { sl@0: # Quoting should not matter sl@0: execsql { sl@0: SELECT a AS 'b' FROM t3 ORDER BY "B"; sl@0: } sl@0: } {1 9} sl@0: do_test tkt2822-5.4 { sl@0: # Quoting should not matter sl@0: execsql { sl@0: SELECT a AS "b" FROM t3 ORDER BY [B]; sl@0: } sl@0: } {1 9} sl@0: sl@0: # In "ORDER BY +b" the term is now an expression rather than sl@0: # a label. It therefore matches by rule (3) instead of rule (2). sl@0: # sl@0: do_test tkt2822-5.5 { sl@0: execsql { sl@0: SELECT a AS b FROM t3 ORDER BY +b; sl@0: } sl@0: } {9 1} sl@0: sl@0: # Tests for rule 2 in compound queries sl@0: # sl@0: do_test tkt2822-6.1 { sl@0: execsql { sl@0: CREATE TABLE t6a(p,q); sl@0: INSERT INTO t6a VALUES(1,8); sl@0: INSERT INTO t6a VALUES(9,2); sl@0: CREATE TABLE t6b(x,y); sl@0: INSERT INTO t6b VALUES(1,7); sl@0: INSERT INTO t6b VALUES(7,2); sl@0: sl@0: SELECT p, q FROM t6a UNION ALL SELECT x, y FROM t6b ORDER BY 1, 2 sl@0: } sl@0: } {1 7 1 8 7 2 9 2} sl@0: do_test tkt2822-6.2 { sl@0: execsql { sl@0: SELECT p PX, q QX FROM t6a UNION ALL SELECT x XX, y YX FROM t6b sl@0: ORDER BY PX, YX sl@0: } sl@0: } {1 7 1 8 7 2 9 2} sl@0: do_test tkt2822-6.3 { sl@0: execsql { sl@0: SELECT p PX, q QX FROM t6a UNION ALL SELECT x XX, y YX FROM t6b sl@0: ORDER BY XX, QX sl@0: } sl@0: } {1 7 1 8 7 2 9 2} sl@0: do_test tkt2822-6.4 { sl@0: execsql { sl@0: SELECT p PX, q QX FROM t6a UNION ALL SELECT x XX, y YX FROM t6b sl@0: ORDER BY QX, XX sl@0: } sl@0: } {7 2 9 2 1 7 1 8} sl@0: do_test tkt2822-6.5 { sl@0: execsql { sl@0: SELECT p PX, q QX FROM t6a UNION ALL SELECT x XX, y YX FROM t6b sl@0: ORDER BY t6b.x, QX sl@0: } sl@0: } {1 7 1 8 7 2 9 2} sl@0: do_test tkt2822-6.6 { sl@0: execsql { sl@0: SELECT p PX, q QX FROM t6a UNION ALL SELECT x XX, y YX FROM t6b sl@0: ORDER BY t6a.q, XX sl@0: } sl@0: } {7 2 9 2 1 7 1 8} sl@0: sl@0: # More error message tests. This is really more of a test of the sl@0: # %r ordinal value formatting capablity added to sqlite3_snprintf() sl@0: # by ticket #2822. sl@0: # sl@0: do_test tkt2822-7.1 { sl@0: execsql { sl@0: CREATE TABLE t7(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14, sl@0: a15,a16,a17,a18,a19,a20,a21,a22,a23,a24,a25); sl@0: } sl@0: catchsql { sl@0: SELECT * FROM t7 ORDER BY 0; sl@0: } sl@0: } {1 {1st ORDER BY term out of range - should be between 1 and 25}} sl@0: do_test tkt2822-7.2 { sl@0: catchsql { sl@0: SELECT * FROM t7 ORDER BY 1, 0; sl@0: } sl@0: } {1 {2nd ORDER BY term out of range - should be between 1 and 25}} sl@0: do_test tkt2822-7.3 { sl@0: catchsql { sl@0: SELECT * FROM t7 ORDER BY 1, 2, 0; sl@0: } sl@0: } {1 {3rd ORDER BY term out of range - should be between 1 and 25}} sl@0: do_test tkt2822-7.4 { sl@0: catchsql { sl@0: SELECT * FROM t7 ORDER BY 1, 2, 3, 0; sl@0: } sl@0: } {1 {4th ORDER BY term out of range - should be between 1 and 25}} sl@0: do_test tkt2822-7.9 { sl@0: catchsql { sl@0: SELECT * FROM t7 ORDER BY 1, 2, 3, 4, 5, 6, 7, 8, 0; sl@0: } sl@0: } {1 {9th ORDER BY term out of range - should be between 1 and 25}} sl@0: do_test tkt2822-7.10 { sl@0: catchsql { sl@0: SELECT * FROM t7 ORDER BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 0; sl@0: } sl@0: } {1 {10th ORDER BY term out of range - should be between 1 and 25}} sl@0: do_test tkt2822-7.11 { sl@0: catchsql { sl@0: SELECT * FROM t7 ORDER BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0; sl@0: } sl@0: } {1 {11th ORDER BY term out of range - should be between 1 and 25}} sl@0: do_test tkt2822-7.12 { sl@0: catchsql { sl@0: SELECT * FROM t7 ORDER BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 0; sl@0: } sl@0: } {1 {12th ORDER BY term out of range - should be between 1 and 25}} sl@0: do_test tkt2822-7.13 { sl@0: catchsql { sl@0: SELECT * FROM t7 ORDER BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 0; sl@0: } sl@0: } {1 {13th ORDER BY term out of range - should be between 1 and 25}} sl@0: do_test tkt2822-7.20 { sl@0: catchsql { sl@0: SELECT * FROM t7 ORDER BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, sl@0: 11,12,13,14,15,16,17,18,19, 0 sl@0: } sl@0: } {1 {20th ORDER BY term out of range - should be between 1 and 25}} sl@0: do_test tkt2822-7.21 { sl@0: catchsql { sl@0: SELECT * FROM t7 ORDER BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, sl@0: 11,12,13,14,15,16,17,18,19, 20, 0 sl@0: } sl@0: } {1 {21st ORDER BY term out of range - should be between 1 and 25}} sl@0: do_test tkt2822-7.22 { sl@0: catchsql { sl@0: SELECT * FROM t7 ORDER BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, sl@0: 11,12,13,14,15,16,17,18,19, 20, 21, 0 sl@0: } sl@0: } {1 {22nd ORDER BY term out of range - should be between 1 and 25}} sl@0: sl@0: sl@0: finish_test