]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Test cases and bug fixes applied to the ORDER BY optimization for joins.
authordrh <drh@noemail.net>
Thu, 27 Sep 2012 17:31:32 +0000 (17:31 +0000)
committerdrh <drh@noemail.net>
Thu, 27 Sep 2012 17:31:32 +0000 (17:31 +0000)
Some test cases fail, but except for the new orderby1.test failures, all
failures appear to be issues with the tests, not with the core code.

FossilOrigin-Name: 75cda864ededb6dc0f84bd52ed3311753a58e351

manifest
manifest.uuid
src/where.c
test/collate4.test
test/collate5.test
test/orderby1.test [new file with mode: 0644]

index 1bbd906b741ee998837b62bed2d99e6cd73c84e1..e8dbbe14e01e8609c003a38450e4070932150af9 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Add\smore\sbits\sto\sthe\sbit\svector\sthat\sis\sused\sto\sdisable\soptimizations\sfor\nbuilt-in\stest.\s\sAdd\sspecific\sbit\spatterns\sto\sdisable\sORDER\sBY\susing\san\nindex\sin\sgeneral\sand\sfor\sjoins.\s\sUse\smacros\sto\stest\sfor\sbits\sin\sthe\s\ndisabled-optimization\sbit\svector,\sin\sorder\sto\smake\sthe\scode\sclearer.
-D 2012-09-27T15:05:54.735
+C Test\scases\sand\sbug\sfixes\sapplied\sto\sthe\sORDER\sBY\soptimization\sfor\sjoins.\nSome\stest\scases\sfail,\sbut\sexcept\sfor\sthe\snew\sorderby1.test\sfailures,\sall\nfailures\sappear\sto\sbe\sissues\swith\sthe\stests,\snot\swith\sthe\score\scode.
+D 2012-09-27T17:31:32.268
 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
 F Makefile.in 5f4f26109f9d80829122e0e09f9cda008fa065fb
 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -249,7 +249,7 @@ F src/vtab.c d8020c0a0e8ccc490ca449d7e665311b6e9f3ba9
 F src/wal.c 5acb3e7bbd31f10ba39acad9ce6b399055337a9d
 F src/wal.h 29c197540b19044e6cd73487017e5e47a1d3dac6
 F src/walker.c 3d75ba73de15e0f8cd0737643badbeb0e002f07b
-F src/where.c 4b837884c4747b022cf36bd00d9eddbb6b451241
+F src/where.c a537824b2eec986c203a6a370090520b955f258b
 F test/8_3_names.test 631ea964a3edb091cf73c3b540f6bcfdb36ce823
 F test/aggerror.test a867e273ef9e3d7919f03ef4f0e8c0d2767944f2
 F test/aggnested.test 0be144b453e0622a085fae8665c32f5676708e00
@@ -324,8 +324,8 @@ F test/coalesce.test cee0dccb9fbd2d494b77234bccf9dc6c6786eb91
 F test/collate1.test e3eaa48c21e150814be1a7b852d2a8af24458d04
 F test/collate2.test 04cebe4a033be319d6ddbb3bbc69464e01700b49
 F test/collate3.test d28d2cfab2c3a3d4628ae4b2b7afc9965daa3b4c
-F test/collate4.test d37682293d3c32223dec2e6afdeaf9de18415248
-F test/collate5.test 67f1d3e848e230ff4802815a79acb0a8b5e69bd7
+F test/collate4.test 27adc324b58ec367bdd0bc6baab628af5129e9f1
+F test/collate5.test 65d928034d30d2d263a80f6359f7549ee1598ec6
 F test/collate6.test 8be65a182abaac8011a622131486dafb8076e907
 F test/collate7.test 8ec29d98f3ee4ccebce6e16ce3863fb6b8c7b868
 F test/collate8.test df26649cfcbddf109c04122b340301616d3a88f6
@@ -634,6 +634,7 @@ F test/notify3.test a86259abbfb923aa27d30f0fc038c88e5251488a
 F test/notnull.test cc7c78340328e6112a13c3e311a9ab3127114347
 F test/null.test a8b09b8ed87852742343b33441a9240022108993
 F test/openv2.test 0d3040974bf402e19b7df4b783e447289d7ab394
+F test/orderby1.test 868a4f88c4f4565684bcc18e8ed61a990ece431b
 F test/oserror.test 50417780d0e0d7cd23cf12a8277bb44024765df3
 F test/pager1.test 2163c6ef119f497a71a84137c957c63763e640ab
 F test/pager2.test 745b911dde3d1f24ae0870bd433dfa83d7c658c1
@@ -1016,7 +1017,7 @@ F tool/vdbe-compress.tcl f12c884766bd14277f4fcedcae07078011717381
 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
 F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381
 F tool/win/sqlite.vsix 67d8a99aceb56384a81b3f30d6c71743146d2cc9
-P c29538f9b1ee4d4869999570604c9618ca0d08ac
-R 112951e028390501419dede06d50cf2d
+P d2fcba1e143beca8c45724d2108870657c269e17
+R 388197fbdd69fccc34c9d73c7bc9f9e7
 U drh
-Z 58d07d9f2619813e6ef0c2cb6bcd8ae9
+Z f07796285dcd213d3100411a37b9d38f
index 17a2e618d2801c1e1383caf9b18661881205043f..38f15f501f3b3335c88e4a6eeb9374cde82c03f6 100644 (file)
@@ -1 +1 @@
-d2fcba1e143beca8c45724d2108870657c269e17
\ No newline at end of file
+75cda864ededb6dc0f84bd52ed3311753a58e351
\ No newline at end of file
index 0395496572c3b343434a54f551ab076b15c971ac..01155c14d3f4ae1da34993a1d90c0b09e5d88a13 100644 (file)
@@ -2883,16 +2883,27 @@ static int isOrderedColumn(WhereBestIdx *p, int iTab, int iCol, int *pbRev){
     if( pLevel->iTabCur!=iTab ) continue;
     if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){
       pIdx = pLevel->plan.u.pIdx;
-      for(j=0; j<pIdx->nColumn; j++){
-        if( iCol==pIdx->aiColumn[j] ) break;
+      if( iCol<0 ){
+        sortOrder = 0;
+        testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
+      }else{
+        for(j=0; j<pIdx->nColumn; j++){
+          if( iCol==pIdx->aiColumn[j] ) break;
+        }
+        if( j>=pIdx->nColumn ) return 0;
+        sortOrder = pIdx->aSortOrder[j];
+        testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
       }
-      if( j>=pIdx->nColumn ) return 0;
-      sortOrder = pIdx->aSortOrder[j];
     }else{
       if( iCol!=(-1) ) return 0;
       sortOrder = 0;
+      testcase( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 );
+    }
+    if( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 ){
+      assert( sortOrder==0 || sortOrder==1 );
+      testcase( sortOrder==1 );
+      sortOrder = 1 - sortOrder;
     }
-    if( (pLevel->plan.wsFlags & WHERE_REVERSE)!=0 ) sortOrder = 1 - sortOrder;
     if( *pbRev==2 ){
       *pbRev = sortOrder;
       return 1;
@@ -3200,8 +3211,8 @@ static void bestBtreeIndex(WhereBestIdx *p){
       if( nOrderBy==nOBSat ){
         bSort = 0;
         wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_ORDERBY;
-        wsFlags |= (bRev==1 ? WHERE_REVERSE : 0);
       }
+      if( bRev ) wsFlags |= WHERE_REVERSE;
     }
 
     /* If there is a DISTINCT qualifier and this index will scan rows in
index 6b3a1c7aee5995b6eb631a4904324b3e3776eb97..1cf8caf04c511595eb5f5c049139b8a133e78a65 100644 (file)
@@ -389,7 +389,7 @@ ifcapable subquery {
       SELECT a FROM collate4t1 WHERE a IN (SELECT * FROM collate4t2)
        ORDER BY rowid
     }
-  } {a A 6}
+  } {a A 5}
   do_test collate4-2.1.8 {
     count {
       SELECT a FROM collate4t1 WHERE a IN ('z', 'a');
index e5bea7a5a5ca54c8b0c54191c49e00e34552a1ec..5f8697ea51ee34c39ee7028cac52c0d4893efe5b 100644 (file)
@@ -221,7 +221,7 @@ do_test collate5-3.0 {
   execsql {
     SELECT a FROM collate5t1 UNION ALL SELECT a FROM collate5t2 ORDER BY 1;
   }
-} {a A a A b B b B n N}
+} {/[aA] [aA] [aA] [aA] [bB] [bB] [bB] [bB] [nN] [nN]/}
 do_test collate5-3.1 {
   execsql {
     SELECT a FROM collate5t2 UNION ALL SELECT a FROM collate5t1 ORDER BY 1;
@@ -282,7 +282,7 @@ do_test collate5-4.2 {
   execsql {
     SELECT a, b, count(*) FROM collate5t1 GROUP BY a, b ORDER BY a, b;
   }
-} {A 1.0 2 b 2 1 B 3 1}
+} {/[aA] 1(.0)? 2 [bB] 2 1 [bB] 3 1/}
 do_test collate5-4.3 {
   execsql {
     DROP TABLE collate5t1;
diff --git a/test/orderby1.test b/test/orderby1.test
new file mode 100644 (file)
index 0000000..2837dfc
--- /dev/null
@@ -0,0 +1,155 @@
+# 2012 Sept 27
+#
+# The author disclaims copyright to this source code.  In place of
+# a legal notice, here is a blessing:
+#
+#    May you do good and not evil.
+#    May you find forgiveness for yourself and forgive others.
+#    May you share freely, never taking more than you give.
+#
+#***********************************************************************
+# This file implements regression tests for SQLite library.  The
+# focus of this file is testing that the optimizations that disable
+# ORDER BY clauses when the natural order of a query is correct.
+#
+
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+set ::testprefix orderby1
+
+# Generate test data for a join.  Verify that the join gets the
+# correct answer.
+#
+do_test 1.0 {
+  db eval {
+    BEGIN;
+    CREATE TABLE album(
+      aid INTEGER PRIMARY KEY,
+      title TEXT UNIQUE NOT NULL
+    );
+    CREATE TABLE track(
+      tid INTEGER PRIMARY KEY,
+      aid INTEGER NOT NULL REFERENCES album,
+      tn INTEGER NOT NULL,
+      name TEXT,
+      UNIQUE(aid, tn)
+    );
+    INSERT INTO album VALUES(1, '1-one'), (2, '2-two'), (3, '3-three');
+    INSERT INTO track VALUES
+        (NULL, 1, 1, 'one-a'),
+        (NULL, 2, 2, 'two-b'),
+        (NULL, 3, 3, 'three-c'),
+        (NULL, 1, 3, 'one-c'),
+        (NULL, 2, 1, 'two-a'),
+        (NULL, 3, 1, 'three-a');
+    COMMIT;
+  }
+} {}
+do_test 1.1a {
+  db eval {
+    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn
+  }
+} {one-a one-c two-a two-b three-a three-c}
+
+# Verify that the ORDER BY clause is optimized out
+#
+do_test 1.1b {
+  db eval {
+    EXPLAIN QUERY PLAN
+    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn
+  }
+} {~/ORDER BY/}  ;# ORDER BY optimized out
+
+# The same query with ORDER BY clause optimization disabled via + operators
+# should give exactly the same answer.
+#
+do_test 1.2a {
+  db eval {
+    SELECT name FROM album JOIN track USING (aid) ORDER BY +title, +tn
+  }
+} {one-a one-c two-a two-b three-a three-c}
+
+# The output is sorted manually in this case.
+#
+do_test 1.2b {
+  db eval {
+    EXPLAIN QUERY PLAN
+    SELECT name FROM album JOIN track USING (aid) ORDER BY +title, +tn
+  }
+} {/ORDER BY/}   ;# separate sorting pass due to "+" on ORDER BY terms
+
+# The same query with ORDER BY optimizations turned off via built-in test.
+#
+do_test 1.3a {
+  optimization_control db order-by-idx-join 0
+  db cache flush
+  db eval {
+    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn
+  }
+} {one-a one-c two-a two-b three-a three-c}
+do_test 1.3b {
+  db eval {
+    EXPLAIN QUERY PLAN
+    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn
+  }
+} {/ORDER BY/}   ;# separate sorting pass due to disabled optimization
+optimization_control db all 1
+db cache flush
+
+# Reverse order sorts
+#
+do_test 1.4a {
+  db eval {
+    SELECT name FROM album JOIN track USING (aid) ORDER BY title DESC, tn
+  }
+} {three-a three-c two-a two-b one-a one-c}
+do_test 1.4b {
+  db eval {
+    SELECT name FROM album JOIN track USING (aid) ORDER BY +title DESC, +tn
+  }
+} {three-a three-c two-a two-b one-a one-c}  ;# verify same order after sorting
+do_test 1.4c {
+  db eval {
+    EXPLAIN QUERY PLAN
+    SELECT name FROM album JOIN track USING (aid) ORDER BY title DESC, tn
+  }
+} {~/ORDER BY/}  ;# ORDER BY optimized-out
+
+
+do_test 1.5a {
+  db eval {
+    SELECT name FROM album JOIN track USING (aid) ORDER BY title, tn DESC
+  }
+} {one-c one-a two-b two-a three-c three-a}
+do_test 1.5b {
+  db eval {
+    SELECT name FROM album JOIN track USING (aid) ORDER BY +title, +tn DESC
+  }
+} {one-c one-a two-b two-a three-c three-a}  ;# verify same order after sorting
+do_test 1.5c {
+  db eval {
+    EXPLAIN QUERY PLAN
+    SELECT name FROM album JOIN track USING (aid) ORDER BY title DESC, tn
+  }
+} {~/ORDER BY/}  ;# ORDER BY optimized-out
+
+do_test 1.6a {
+  db eval {
+    SELECT name FROM album JOIN track USING (aid) ORDER BY title DESC, tn DESC
+  }
+} {three-c three-a two-b two-a one-c one-a}
+do_test 1.6b {
+  db eval {
+    SELECT name FROM album JOIN track USING (aid) ORDER BY +title DESC, +tn DESC
+  }
+} {three-c three-a two-b two-a one-c one-a}  ;# verify same order after sorting
+do_test 1.6c {
+  db eval {
+    EXPLAIN QUERY PLAN
+    SELECT name FROM album JOIN track USING (aid) ORDER BY title DESC, tn DESC
+  }
+} {~/ORDER BY/}  ;# ORDER BY optimized-out
+
+
+finish_test