os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/where3.test
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/sqlite3api/TEST/TclScript/where3.test	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,216 @@
     1.4 +# 2006 January 31
     1.5 +#
     1.6 +# The author disclaims copyright to this source code.  In place of
     1.7 +# a legal notice, here is a blessing:
     1.8 +#
     1.9 +#    May you do good and not evil.
    1.10 +#    May you find forgiveness for yourself and forgive others.
    1.11 +#    May you share freely, never taking more than you give.
    1.12 +#
    1.13 +#***********************************************************************
    1.14 +# This file implements regression tests for SQLite library.  The
    1.15 +# focus of this file is testing the join reordering optimization
    1.16 +# in cases that include a LEFT JOIN.
    1.17 +#
    1.18 +# $Id: where3.test,v 1.4 2008/04/17 19:14:02 drh Exp $
    1.19 +
    1.20 +set testdir [file dirname $argv0]
    1.21 +source $testdir/tester.tcl
    1.22 +
    1.23 +# The following is from ticket #1652.
    1.24 +#
    1.25 +# A comma join then a left outer join:  A,B left join C.
    1.26 +# Arrange indices so that the B table is chosen to go first.
    1.27 +# Also put an index on C, but make sure that A is chosen before C.
    1.28 +#
    1.29 +do_test where3-1.1 {
    1.30 +  execsql {
    1.31 +    CREATE TABLE t1(a, b);
    1.32 +    CREATE TABLE t2(p, q);
    1.33 +    CREATE TABLE t3(x, y);
    1.34 +    
    1.35 +    INSERT INTO t1 VALUES(111,'one');
    1.36 +    INSERT INTO t1 VALUES(222,'two');
    1.37 +    INSERT INTO t1 VALUES(333,'three');
    1.38 +    
    1.39 +    INSERT INTO t2 VALUES(1,111);
    1.40 +    INSERT INTO t2 VALUES(2,222);
    1.41 +    INSERT INTO t2 VALUES(4,444);
    1.42 +    CREATE INDEX t2i1 ON t2(p);
    1.43 +    
    1.44 +    INSERT INTO t3 VALUES(999,'nine');
    1.45 +    CREATE INDEX t3i1 ON t3(x);
    1.46 +    
    1.47 +    SELECT * FROM t1, t2 LEFT JOIN t3 ON q=x WHERE p=2 AND a=q;
    1.48 +  }
    1.49 +} {222 two 2 222 {} {}}
    1.50 +
    1.51 +ifcapable explain {
    1.52 +  do_test where3-1.1.1 {
    1.53 +     explain_no_trace {SELECT * FROM t1, t2 LEFT JOIN t3 ON q=x
    1.54 +                        WHERE p=2 AND a=q}
    1.55 +  } [explain_no_trace {SELECT * FROM t1, t2 LEFT JOIN t3 ON x=q
    1.56 +                        WHERE p=2 AND a=q}]
    1.57 +}
    1.58 +
    1.59 +# Ticket #1830
    1.60 +#
    1.61 +# This is similar to the above but with the LEFT JOIN on the
    1.62 +# other side.
    1.63 +#
    1.64 +do_test where3-1.2 {
    1.65 +  execsql {
    1.66 +    CREATE TABLE parent1(parent1key, child1key, Child2key, child3key);
    1.67 +    CREATE TABLE child1 ( child1key NVARCHAR, value NVARCHAR );
    1.68 +    CREATE UNIQUE INDEX PKIDXChild1 ON child1 ( child1key );
    1.69 +    CREATE TABLE child2 ( child2key NVARCHAR, value NVARCHAR );
    1.70 +
    1.71 +    INSERT INTO parent1(parent1key,child1key,child2key)
    1.72 +       VALUES ( 1, 'C1.1', 'C2.1' );
    1.73 +    INSERT INTO child1 ( child1key, value ) VALUES ( 'C1.1', 'Value for C1.1' );
    1.74 +    INSERT INTO child2 ( child2key, value ) VALUES ( 'C2.1', 'Value for C2.1' );
    1.75 +
    1.76 +    INSERT INTO parent1 ( parent1key, child1key, child2key )
    1.77 +       VALUES ( 2, 'C1.2', 'C2.2' );
    1.78 +    INSERT INTO child2 ( child2key, value ) VALUES ( 'C2.2', 'Value for C2.2' );
    1.79 +
    1.80 +    INSERT INTO parent1 ( parent1key, child1key, child2key )
    1.81 +       VALUES ( 3, 'C1.3', 'C2.3' );
    1.82 +    INSERT INTO child1 ( child1key, value ) VALUES ( 'C1.3', 'Value for C1.3' );
    1.83 +    INSERT INTO child2 ( child2key, value ) VALUES ( 'C2.3', 'Value for C2.3' );
    1.84 +
    1.85 +    SELECT parent1.parent1key, child1.value, child2.value
    1.86 +    FROM parent1
    1.87 +    LEFT OUTER JOIN child1 ON child1.child1key = parent1.child1key
    1.88 +    INNER JOIN child2 ON child2.child2key = parent1.child2key;
    1.89 +  }
    1.90 +} {1 {Value for C1.1} {Value for C2.1} 2 {} {Value for C2.2} 3 {Value for C1.3} {Value for C2.3}}
    1.91 +
    1.92 +ifcapable explain {
    1.93 +  do_test where3-1.2.1 {
    1.94 +     explain_no_trace {
    1.95 +       SELECT parent1.parent1key, child1.value, child2.value
    1.96 +       FROM parent1
    1.97 +       LEFT OUTER JOIN child1 ON child1.child1key = parent1.child1key
    1.98 +       INNER JOIN child2 ON child2.child2key = parent1.child2key;
    1.99 +     }
   1.100 +  } [explain_no_trace {
   1.101 +       SELECT parent1.parent1key, child1.value, child2.value
   1.102 +       FROM parent1
   1.103 +       LEFT OUTER JOIN child1 ON parent1.child1key = child1.child1key 
   1.104 +       INNER JOIN child2 ON child2.child2key = parent1.child2key;
   1.105 +     }]
   1.106 +}
   1.107 +
   1.108 +# This procedure executes the SQL.  Then it appends 
   1.109 +# the ::sqlite_query_plan variable.
   1.110 +#
   1.111 +proc queryplan {sql} {
   1.112 +  set ::sqlite_sort_count 0
   1.113 +  set data [execsql $sql]
   1.114 +  return [concat $data $::sqlite_query_plan]
   1.115 +}
   1.116 +
   1.117 +
   1.118 +# If you have a from clause of the form:   A B C left join D
   1.119 +# then make sure the query optimizer is able to reorder the 
   1.120 +# A B C part anyway it wants. 
   1.121 +#
   1.122 +# Following the fix to ticket #1652, there was a time when
   1.123 +# the C table would not reorder.  So the following reorderings
   1.124 +# were possible:
   1.125 +#
   1.126 +#            A B C left join D
   1.127 +#            B A C left join D
   1.128 +#
   1.129 +# But these reorders were not allowed
   1.130 +#
   1.131 +#            C A B left join D
   1.132 +#            A C B left join D
   1.133 +#            C B A left join D
   1.134 +#            B C A left join D
   1.135 +#
   1.136 +# The following tests are here to verify that the latter four
   1.137 +# reorderings are allowed again.
   1.138 +#
   1.139 +do_test where3-2.1 {
   1.140 +  execsql {
   1.141 +    CREATE TABLE tA(apk integer primary key, ax);
   1.142 +    CREATE TABLE tB(bpk integer primary key, bx);
   1.143 +    CREATE TABLE tC(cpk integer primary key, cx);
   1.144 +    CREATE TABLE tD(dpk integer primary key, dx);
   1.145 +  }
   1.146 +  queryplan {
   1.147 +    SELECT * FROM tA, tB, tC LEFT JOIN tD ON dpk=cx
   1.148 +     WHERE cpk=bx AND bpk=ax
   1.149 +  }
   1.150 +} {tA {} tB * tC * tD *}
   1.151 +do_test where3-2.1.1 {
   1.152 +  queryplan {
   1.153 +    SELECT * FROM tA, tB, tC LEFT JOIN tD ON cx=dpk
   1.154 +     WHERE cpk=bx AND bpk=ax
   1.155 +  }
   1.156 +} {tA {} tB * tC * tD *}
   1.157 +do_test where3-2.1.2 {
   1.158 +  queryplan {
   1.159 +    SELECT * FROM tA, tB, tC LEFT JOIN tD ON cx=dpk
   1.160 +     WHERE bx=cpk AND bpk=ax
   1.161 +  }
   1.162 +} {tA {} tB * tC * tD *}
   1.163 +do_test where3-2.1.3 {
   1.164 +  queryplan {
   1.165 +    SELECT * FROM tA, tB, tC LEFT JOIN tD ON cx=dpk
   1.166 +     WHERE bx=cpk AND ax=bpk
   1.167 +  }
   1.168 +} {tA {} tB * tC * tD *}
   1.169 +do_test where3-2.1.4 {
   1.170 +  queryplan {
   1.171 +    SELECT * FROM tA, tB, tC LEFT JOIN tD ON dpk=cx
   1.172 +     WHERE bx=cpk AND ax=bpk
   1.173 +  }
   1.174 +} {tA {} tB * tC * tD *}
   1.175 +do_test where3-2.1.5 {
   1.176 +  queryplan {
   1.177 +    SELECT * FROM tA, tB, tC LEFT JOIN tD ON dpk=cx
   1.178 +     WHERE cpk=bx AND ax=bpk
   1.179 +  }
   1.180 +} {tA {} tB * tC * tD *}
   1.181 +do_test where3-2.2 {
   1.182 +  queryplan {
   1.183 +    SELECT * FROM tA, tB, tC LEFT JOIN tD ON dpk=cx
   1.184 +     WHERE cpk=bx AND apk=bx
   1.185 +  }
   1.186 +} {tB {} tA * tC * tD *}
   1.187 +do_test where3-2.3 {
   1.188 +  queryplan {
   1.189 +    SELECT * FROM tA, tB, tC LEFT JOIN tD ON dpk=cx
   1.190 +     WHERE cpk=bx AND apk=bx
   1.191 +  }
   1.192 +} {tB {} tA * tC * tD *}
   1.193 +do_test where3-2.4 {
   1.194 +  queryplan {
   1.195 +    SELECT * FROM tA, tB, tC LEFT JOIN tD ON dpk=cx
   1.196 +     WHERE apk=cx AND bpk=ax
   1.197 +  }
   1.198 +} {tC {} tA * tB * tD *}
   1.199 +do_test where3-2.5 {
   1.200 +  queryplan {
   1.201 +    SELECT * FROM tA, tB, tC LEFT JOIN tD ON dpk=cx
   1.202 +     WHERE cpk=ax AND bpk=cx
   1.203 +  }
   1.204 +} {tA {} tC * tB * tD *}
   1.205 +do_test where3-2.5 {
   1.206 +  queryplan {
   1.207 +    SELECT * FROM tA, tB, tC LEFT JOIN tD ON dpk=cx
   1.208 +     WHERE bpk=cx AND apk=bx
   1.209 +  }
   1.210 +} {tC {} tB * tA * tD *}
   1.211 +do_test where3-2.6 {
   1.212 +  queryplan {
   1.213 +    SELECT * FROM tA, tB, tC LEFT JOIN tD ON dpk=cx
   1.214 +     WHERE cpk=bx AND apk=cx
   1.215 +  }
   1.216 +} {tB {} tC * tA * tD *}
   1.217 +
   1.218 +
   1.219 +finish_test