]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Add extra tests for the optimization on this branch.
authordan <dan@noemail.net>
Mon, 1 May 2017 14:09:32 +0000 (14:09 +0000)
committerdan <dan@noemail.net>
Mon, 1 May 2017 14:09:32 +0000 (14:09 +0000)
FossilOrigin-Name: 4921cd9520080f9baff70e548f64a56e2204b398b8397a2d318a98c32ec4b00c

manifest
manifest.uuid
test/having.test

index a30d603c081704cc9a4f3989558fabdb55e966fd..d89bebfd83d496c98255b88f6f12b3e62837a0aa 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Automatically\stransfer\sterms\sfrom\sthe\sHAVING\sclause\sto\sthe\sWHERE\sclause\sof\san\naggregate\squery\sin\scases\swhere\sthe\sresult\sof\sevaluating\sthe\sterm\sdepends\sonly\none\sone\sor\smore\sof\sthe\sGROUP\sBY\sexpressions\s(and\son\sno\sother\sinputs).
-D 2017-04-29T20:53:09.360
+C Add\sextra\stests\sfor\sthe\soptimization\son\sthis\sbranch.
+D 2017-05-01T14:09:32.423
 F Makefile.in 1cc758ce3374a32425e4d130c2fe7b026b20de5b8843243de75f087c0a2661fb
 F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434
 F Makefile.msc 6a8c838220f7c00820e1fc0ac1bccaaa8e5676067e1dbfa1bafa7a4ffecf8ae6
@@ -850,7 +850,7 @@ F test/fuzzer2.test a85ef814ce071293bce1ad8dffa217cbbaad4c14
 F test/fuzzerfault.test 8792cd77fd5bce765b05d0c8e01b9edcf8af8536
 F test/gcfault.test dd28c228a38976d6336a3fc42d7e5f1ad060cb8c
 F test/genesis.tcl 1e2e2e8e5cc4058549a154ff1892fe5c9de19f98
-F test/having.test a03676a754815628a08d3b96d506dd9eda8ffbdd356cd8ea9c2e5368286fbe6a
+F test/having.test 30a02b8a9a47cba7bdb5281999c5cbff407c2ac296511ee64dd0b418fe38eb0f
 F test/hexlit.test 4a6a5f46e3c65c4bf1fa06f5dd5a9507a5627751
 F test/hidden.test 23c1393a79e846d68fd902d72c85d5e5dcf98711
 F test/hook.test dbc0b87756e1e20e7497b56889c9e9cd2f8cc2b5
@@ -1578,10 +1578,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P d7bb79ed3a40419d143fbe35c310e51fe7b384a22f082a61ad788671d2d33ee0
-R bd50b7d1e9edcc2d5d20fca02c02c5c7
-T *branch * having-where-optimization
-T *sym-having-where-optimization *
-T -sym-trunk *
+P 5375a3ce56f1d993b13b469fe33ec7679948f53940f62a15ddbaeb8aaa26a22c
+R 123e296789286f2ee6ee6640a2471eff
 U dan
-Z 94cbfc69ef14a78710cd01b746487e79
+Z 8431f2088d761010cb187c22f8f8d978
index 57fc2eb5ffd315fb6d472a3fd141e9cbfe8b2901..2428b6a0d4a70db04882cc80fd126588849f9d1b 100644 (file)
@@ -1 +1 @@
-5375a3ce56f1d993b13b469fe33ec7679948f53940f62a15ddbaeb8aaa26a22c
\ No newline at end of file
+4921cd9520080f9baff70e548f64a56e2204b398b8397a2d318a98c32ec4b00c
\ No newline at end of file
index 7763b068505fc4ee4d5556c8c043175b000ec054..6d2f9fdcc5aac20b1325c1c756c35729066a3c01 100644 (file)
@@ -17,6 +17,8 @@ source $testdir/tester.tcl
 set testprefix having
 
 do_execsql_test 1.0 {
+  CREATE TABLE t2(c, d);
+
   CREATE TABLE t1(a, b);
   INSERT INTO t1 VALUES(1, 1);
   INSERT INTO t1 VALUES(2, 2);
@@ -34,11 +36,14 @@ foreach {tn sql res} {
   do_execsql_test 1.$tn $sql $res
 }
 
+# Run an EXPLAIN command for both SQL statements. Return true if 
+# the outputs are identical, or false otherwise.
+#
 proc compare_vdbe {sql1 sql2} {
   set r1 [list]
   set r2 [list]
-  db eval "explain $sql1" { lappend r1 $opcode $p1 $p2 $p3}
-  db eval "explain $sql2" { lappend r2 $opcode $p1 $p2 $p3}
+  db eval "explain $sql1" { lappend r1 $opcode $p1 $p2 $p3 $p4 $p5}
+  db eval "explain $sql2" { lappend r2 $opcode $p1 $p2 $p3 $p4 $p5}
   return [expr {$r1==$r2}]
 }
 
@@ -46,17 +51,10 @@ proc do_compare_vdbe_test {tn sql1 sql2 res} {
   uplevel [list do_test $tn [list compare_vdbe $sql1 $sql2] $res]
 }
 
-do_compare_vdbe_test 2.1 {
-  SELECT a, sum(b) FROM t1 GROUP BY a HAVING a=2
-} {
-  SELECT a, sum(b) FROM t1 WHERE a=2 GROUP BY a
-} 1
-do_compare_vdbe_test 2.2 {
-  SELECT a, sum(b) FROM t1 GROUP BY a+1 HAVING a=2
-} {
-  SELECT a, sum(b) FROM t1 GROUP BY a+1 HAVING a=2
-} 1
-
+#-------------------------------------------------------------------------
+# Test that various statements that are eligible for the optimization
+# produce the same VDBE code as optimizing by hand does.
+#
 foreach {tn sql1 sql2} {
   1 "SELECT a, sum(b) FROM t1 GROUP BY a HAVING a=2"
     "SELECT a, sum(b) FROM t1 WHERE a=2 GROUP BY a"
@@ -79,16 +77,78 @@ foreach {tn sql1 sql2} {
         GROUP BY a
       )
     }
+
+  5 "SELECT a, sum(b) FROM t1 GROUP BY a COLLATE binary HAVING 0"
+    "SELECT a, sum(b) FROM t1 WHERE 0 GROUP BY a COLLATE binary"
+
+  6 "SELECT count(*) FROM t1,t2 WHERE a=c GROUP BY b, d HAVING b=d"
+    "SELECT count(*) FROM t1,t2 WHERE a=c AND b=d GROUP BY b, d"
+
+  7 {
+      SELECT count(*) FROM t1,t2 WHERE a=c GROUP BY b, d 
+      HAVING b=d COLLATE nocase
+    } {
+      SELECT count(*) FROM t1,t2 WHERE a=c AND b=d COLLATE nocase 
+      GROUP BY b, d
+    }
+
+  8 "SELECT a, sum(b) FROM t1 GROUP BY a||b HAVING substr(a||b, 1, 1)='a'"
+    "SELECT a, sum(b) FROM t1 WHERE substr(a||b, 1, 1)='a' GROUP BY a||b"
 } {
-  do_compare_vdbe_test 3.$tn $sql1 $sql2 1
+  do_compare_vdbe_test 2.$tn $sql1 $sql2 1
 }
 
+#-------------------------------------------------------------------------
+# 1: Test that the optimization is only applied if the GROUP BY term
+#    uses BINARY collation.
+#
+# 2: Not applied if there is a non-deterministic function in the HAVING
+#    term.
+#
 foreach {tn sql1 sql2} {
   1 "SELECT a, sum(b) FROM t1 GROUP BY a COLLATE nocase HAVING a=2"
     "SELECT a, sum(b) FROM t1 WHERE a=2 GROUP BY a COLLATE nocase"
+
+  2 "SELECT a, sum(b) FROM t1 GROUP BY a HAVING randomblob(a)<X'88'"
+    "SELECT a, sum(b) FROM t1 WHERE randomblob(a)<X'88' GROUP BY a"
 } {
-  do_compare_vdbe_test 4.$tn $sql1 $sql2 0
+  do_compare_vdbe_test 3.$tn $sql1 $sql2 0
+}
+
+
+#-------------------------------------------------------------------------
+# Test that non-deterministic functions disqualify a term from being
+# moved from the HAVING to WHERE clause.
+#
+do_execsql_test 4.1 {
+  CREATE TABLE t3(a, b);
+  INSERT INTO t3 VALUES(1, 1);
+  INSERT INTO t3 VALUES(1, 2);
+  INSERT INTO t3 VALUES(1, 3);
+  INSERT INTO t3 VALUES(2, 1);
+  INSERT INTO t3 VALUES(2, 2);
+  INSERT INTO t3 VALUES(2, 3);
 }
 
+proc nondeter {args} {
+  incr ::nondeter_ret
+  expr {$::nondeter_ret % 2}
+}
+db func nondeter nondeter
+
+set ::nondeter_ret 0
+do_execsql_test 4.2 {
+  SELECT a, sum(b) FROM t3 GROUP BY a HAVING nondeter(a)
+} {1 6}
+
+# If the term where moved, the query above would return the same
+# result as the following. But it does not.
+#
+set ::nondeter_ret 0
+do_execsql_test 4.3 {
+  SELECT a, sum(b) FROM t3 WHERE nondeter(a) GROUP BY a
+} {1 4 2 2}
+
+
 finish_test