summaryrefslogtreecommitdiffstats
path: root/test/rowvalue3.test
blob: 80ebeb7617ebe4f789f08c0d3fe48da1967125d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# 2016 June 17
#
# 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 "(...) IN (SELECT ...)" expressions
# where the SELECT statement returns more than one column.
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl
set ::testprefix rowvalue3

do_execsql_test 1.0 {
  CREATE TABLE t1(a, b, c);
  CREATE INDEX i1 ON t1(a, b);
  INSERT INTO t1 VALUES(1, 2, 3);
  INSERT INTO t1 VALUES(4, 5, 6);
  INSERT INTO t1 VALUES(7, 8, 9);
}

foreach {tn sql res} {
  1  "SELECT 1 WHERE (4, 5) IN (SELECT a, b FROM t1)"  1
  2  "SELECT 1 WHERE (5, 5) IN (SELECT a, b FROM t1)"  {}
  3  "SELECT 1 WHERE (5, 4) IN (SELECT a, b FROM t1)"  {}
  4  "SELECT 1 WHERE (5, 4) IN (SELECT b, a FROM t1)"  1
  5  "SELECT 1 WHERE (SELECT a, b FROM t1 WHERE c=6) IN (SELECT a, b FROM t1)" 1
  6  "SELECT (5, 4) IN (SELECT a, b FROM t1)" 0
  7  "SELECT 1 WHERE (5, 4) IN (SELECT +b, +a FROM t1)"  1
  8  "SELECT (5, 4) IN (SELECT +b, +a FROM t1)"  1
  9  "SELECT (1, 2) IN (SELECT rowid, b FROM t1)"  1
  10 "SELECT 1 WHERE (1, 2) IN (SELECT rowid, b FROM t1)"  1
  11 "SELECT 1 WHERE (1, NULL) IN (SELECT rowid, b FROM t1)"  {}
  12 "SELECT 1 FROM t1 WHERE (a, b) = (SELECT +a, +b FROM t1)" {1}
} {
  do_execsql_test 1.$tn $sql $res
}

#-------------------------------------------------------------------------

do_execsql_test 2.0 {
  CREATE TABLE z1(x, y, z);
  CREATE TABLE kk(a, b);

  INSERT INTO z1 VALUES('a', 'b', 'c');
  INSERT INTO z1 VALUES('d', 'e', 'f');
  INSERT INTO z1 VALUES('g', 'h', 'i');

  -- INSERT INTO kk VALUES('y', 'y');
  INSERT INTO kk VALUES('d', 'e');
  -- INSERT INTO kk VALUES('x', 'x');

}

foreach {tn idx} {
  1 { }
  2 { CREATE INDEX z1idx ON z1(x, y) }
  3 { CREATE UNIQUE INDEX z1idx ON z1(x, y) }
  4 { CREATE INDEX z1idx ON kk(a, b) }
} {
  execsql "DROP INDEX IF EXISTS z1idx"
  execsql $idx

  do_execsql_test 2.$tn.1 {
    SELECT * FROM z1 WHERE x IN (SELECT a FROM kk)
  } {d e f}

  do_execsql_test 2.$tn.2 {
    SELECT * FROM z1 WHERE (x,y) IN (SELECT a, b FROM kk)
  } {d e f}

  do_execsql_test 2.$tn.3 {
    SELECT * FROM z1 WHERE (x, +y) IN (SELECT a, b FROM kk)
  } {d e f}
  
  do_execsql_test 2.$tn.4 {
    SELECT * FROM z1 WHERE (x, +y) IN (SELECT a, b||'x' FROM kk)
  } {}

  do_execsql_test 2.$tn.5 {
    SELECT * FROM z1 WHERE (+x, y) IN (SELECT a, b FROM kk)
  } {d e f}
}

#-------------------------------------------------------------------------
#

do_execsql_test 3.0 {
  CREATE TABLE c1(a, b, c, d);
  INSERT INTO c1(rowid, a, b) VALUES(1,   NULL, 1);
  INSERT INTO c1(rowid, a, b) VALUES(2,   2, NULL);
  INSERT INTO c1(rowid, a, b) VALUES(3,   2, 2);
  INSERT INTO c1(rowid, a, b) VALUES(4,   3, 3);

  INSERT INTO c1(rowid, a, b, c, d) VALUES(101, 'a', 'b', 1, 1);
  INSERT INTO c1(rowid, a, b, c, d) VALUES(102, 'a', 'b', 1, 2);
  INSERT INTO c1(rowid, a, b, c, d) VALUES(103, 'a', 'b', 1, 3);
  INSERT INTO c1(rowid, a, b, c, d) VALUES(104, 'a', 'b', 2, 1);
  INSERT INTO c1(rowid, a, b, c, d) VALUES(105, 'a', 'b', 2, 2);
  INSERT INTO c1(rowid, a, b, c, d) VALUES(106, 'a', 'b', 2, 3);
  INSERT INTO c1(rowid, a, b, c, d) VALUES(107, 'a', 'b', 3, 1);
  INSERT INTO c1(rowid, a, b, c, d) VALUES(108, 'a', 'b', 3, 2);
  INSERT INTO c1(rowid, a, b, c, d) VALUES(109, 'a', 'b', 3, 3);
}


foreach {tn idx} {
  1 { }
  2 { CREATE INDEX c1ab ON c1(a, b); }
  3 { CREATE INDEX c1ba ON c1(b, a); }

  4 { CREATE INDEX c1cd ON c1(c, d); }
  5 { CREATE INDEX c1dc ON c1(d, c); }
} {
  drop_all_indexes

  foreach {tn2 sql res} {
    1 "SELECT (1, 2) IN (SELECT a, b FROM c1)" {0}
    2 "SELECT (1, 1) IN (SELECT a, b FROM c1)" {{}}
    3 "SELECT (2, 1) IN (SELECT a, b FROM c1)" {{}}
    4 "SELECT (2, 2) IN (SELECT a, b FROM c1)" {1}
    5 "SELECT c, d FROM c1 WHERE (c, d) IN (SELECT d, c FROM c1)"
      { 1 1 1 2 1 3   2 1 2 2 2 3   3 1 3 2 3 3 }

    6 "SELECT c, d FROM c1 WHERE (c,d) IN (SELECT d, c FROM c1) ORDER BY c DESC"
      { 3 1 3 2 3 3   2 1 2 2 2 3   1 1 1 2 1 3 }

    7 {
        SELECT c, d FROM c1 WHERE (c,d) IN (SELECT d, c FROM c1) 
        ORDER BY c DESC, d ASC
      } { 3 1 3 2 3 3   2 1 2 2 2 3   1 1 1 2 1 3 }

    8 {
        SELECT c, d FROM c1 WHERE (c,d) IN (SELECT d, c FROM c1) 
        ORDER BY c ASC, d DESC
      } { 1 3 1 2 1 1   2 3 2 2 2 1   3 3 3 2 3 1 }

    9 {
        SELECT c, d FROM c1 WHERE (c,d) IN (SELECT d, c FROM c1) 
        ORDER BY c ASC, d ASC
      } { 1 1 1 2 1 3   2 1 2 2 2 3   3 1 3 2 3 3 }
    10 {
        SELECT c, d FROM c1 WHERE (c,d) IN (SELECT d, c FROM c1) 
        ORDER BY c DESC, d DESC
      } { 3 3 3 2 3 1   2 3 2 2 2 1   1 3 1 2 1 1 }

  } {
    do_execsql_test 3.$tn.$tn2 $sql $res
  }
}

#-------------------------------------------------------------------------

do_execsql_test 4.0 {
  CREATE TABLE hh(a, b, c);

  INSERT INTO hh VALUES('a', 'a', 1);
  INSERT INTO hh VALUES('a', 'b', 2);
  INSERT INTO hh VALUES('b', 'a', 3);
  INSERT INTO hh VALUES('b', 'b', 4);

  CREATE TABLE k1(x, y);
  INSERT INTO k1 VALUES('a', 'a');
  INSERT INTO k1 VALUES('b', 'b');
  INSERT INTO k1 VALUES('a', 'b');
  INSERT INTO k1 VALUES('b', 'a');
}

foreach {tn idx} {
  1 { }
  2 { CREATE INDEX h1 ON hh(a, b); }
  3 { CREATE UNIQUE INDEX k1idx ON k1(x, y) }
  4 { CREATE UNIQUE INDEX k1idx ON k1(x, y DESC) }
  5 { 
    CREATE INDEX h1 ON hh(a, b);
    CREATE UNIQUE INDEX k1idx ON k1(x, y); 
  }
  6 { 
    CREATE INDEX h1 ON hh(a, b);
    CREATE UNIQUE INDEX k1idx ON k1(x, y DESC); 
  }
} {
  drop_all_indexes
  execsql $idx
  foreach {tn2 orderby res} {
    1 "a ASC, b ASC"  {1 2 3 4}
    2 "a ASC, b DESC" {2 1 4 3}
    3 "a DESC, b ASC" {3 4 1 2}
    4 "a DESC, b DESC" {4 3 2 1}
  } {
    do_execsql_test 4.$tn.$tn2 "
      SELECT c FROM hh WHERE (a, b) in (SELECT x, y FROM k1) ORDER BY $orderby
    " $res
  }
}

#-------------------------------------------------------------------------

# 2016-11-17.  Query flattening in a vector SELECT on the RHS of an IN
# operator.  Ticket https://www.sqlite.org/src/info/da7841375186386c
#
do_execsql_test 5.0 {
  DROP TABLE IF EXISTS t1;
  DROP TABLE IF EXISTS t2;
  CREATE TABLE T1(a TEXT);
  INSERT INTO T1(a) VALUES ('aaa');
  CREATE TABLE T2(a TEXT PRIMARY KEY,n INT);
  INSERT INTO T2(a, n) VALUES('aaa',0);
  SELECT * FROM T2
   WHERE (a,n) IN (SELECT T1.a, V.n
                     FROM T1, (SELECT * FROM (SELECT 0 n) T3) V);
} {aaa 0}


finish_test