summaryrefslogtreecommitdiffstats
path: root/test/rowvalue9.test
blob: baa13f4f94b25664091ae781237c60dc5b48fe43 (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# 2016 September 3
#
# 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 SQL statements that use row value
# constructors.
#


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

# Tests:
#
#  1.*: Test that affinities are handled correctly by various row-value
#       operations without indexes.
#
#  2.*: Test an affinity bug that came up during testing.
#
#  3.*: Test a row-value version of the bug tested by 2.*.
#
#  4.*: Test that affinities are handled correctly by various row-value
#       operations with assorted indexes.
#

do_execsql_test 1.0.1 {
  CREATE TABLE a1(c, b INTEGER, a TEXT, PRIMARY KEY(a, b));
 
  INSERT INTO a1 (rowid, c, b, a) VALUES(3,  '0x03', 1, 1);
  INSERT INTO a1 (rowid, c, b, a) VALUES(14, '0x0E', 2, 2);
  INSERT INTO a1 (rowid, c, b, a) VALUES(15, '0x0F', 3, 3);
  INSERT INTO a1 (rowid, c, b, a) VALUES(92, '0x5C', 4, 4);

  CREATE TABLE a2(x BLOB, y BLOB);
  INSERT INTO a2(x, y) VALUES(1, 1);
  INSERT INTO a2(x, y) VALUES(2, '2');
  INSERT INTO a2(x, y) VALUES('3', 3);
  INSERT INTO a2(x, y) VALUES('4', '4');
}

do_execsql_test 1.0.2 { 
  SELECT x, typeof(x), y, typeof(y) FROM a2 ORDER BY rowid
} {
  1 integer 1 integer 
  2 integer 2 text 
  3 text    3 integer 
  4 text    4 text
}

do_execsql_test 1.1.1 {
  SELECT (SELECT rowid FROM a1 WHERE a=x AND b=y) FROM a2
} {{} {} 15 92}
do_execsql_test 1.1.2 {
  SELECT (SELECT rowid FROM a1 WHERE (a, b) = (x, y)) FROM a2
} {{} {} 15 92}

do_execsql_test 1.2.3 {
  SELECT a1.rowid FROM a1, a2 WHERE a=x AND b=y;
} {15 92}
do_execsql_test 1.2.4 {
  SELECT a1.rowid FROM a1, a2 WHERE (a, b) = (x, y)
} {15 92}


do_execsql_test 1.3.1 {
  SELECT a1.rowid FROM a1, a2 WHERE coalesce(NULL,x)=a AND coalesce(NULL,y)=b
} {3 14 15 92}
do_execsql_test 1.3.2 {
  SELECT a1.rowid FROM a1, a2 
  WHERE (coalesce(NULL,x), coalesce(NULL,y)) = (a, b)
} {3 14 15 92}

do_execsql_test 1.4.1 {
  SELECT a1.rowid FROM a1, a2 WHERE +x=a AND +y=b
} {3 14 15 92}
do_execsql_test 1.4.2 {
  SELECT a1.rowid FROM a1, a2 WHERE (+x, +y) = (a, b)
} {3 14 15 92}

do_execsql_test 1.5.1 {
  SELECT (SELECT rowid FROM a1 WHERE a=+x AND b=+y) FROM a2
} {3 14 15 92}
do_execsql_test 1.5.2 {
  SELECT (SELECT rowid FROM a1 WHERE (a, b) = (+x, +y)) FROM a2
} {3 14 15 92}
do_execsql_test 1.5.3 {
  SELECT (SELECT rowid FROM a1 WHERE (+x, +y) = (a, b)) FROM a2
} {3 14 15 92}

do_execsql_test 1.6.1 {
  SELECT a1.rowid FROM a1 WHERE (a, b) IN (SELECT x, y FROM a2)
} {15 92}
do_execsql_test 1.6.2 {
  SELECT a1.rowid FROM a1, a2 WHERE EXISTS (
    SELECT 1 FROM a1 WHERE a=x AND b=y
  )
} {3 14 15 92 3 14 15 92}

# Test that [199df416] is fixed.
#
do_execsql_test 2.1 {
  CREATE TABLE b1(a TEXT);
  CREATE TABLE b2(x BLOB);
  INSERT INTO b1 VALUES(1);
  INSERT INTO b2 VALUES(1);
}
do_execsql_test 2.2 { SELECT * FROM b1, b2 WHERE a=x; } {}
do_execsql_test 2.3 { SELECT * FROM b1 WHERE a IN (SELECT x FROM b2) } {}
do_execsql_test 2.4 { CREATE UNIQUE INDEX b1a ON b1(a); }
do_execsql_test 2.5 { SELECT * FROM b1 WHERE a IN (SELECT x FROM b2) } {}

# Test that a multi-column version of the query that revealed problem 
# [199df416] also works.
#
do_execsql_test 3.1 {
  CREATE TABLE c1(a INTEGER, b TEXT);
  INSERT INTO c1 VALUES(1, 1);
  CREATE TABLE c2(x BLOB, y BLOB);
  INSERT INTO c2 VALUES(1, 1);
}
do_execsql_test 3.2 {
  SELECT * FROM c1 WHERE (a, b) IN (SELECT x, y FROM c2)
} {}
do_execsql_test 3.3 {
  CREATE UNIQUE INDEX c1ab ON c1(a, b);
  SELECT * FROM c1 WHERE (a, b) IN (SELECT x, y FROM c2)
} {}
do_execsql_test 3.4 {
  SELECT * FROM c1 WHERE (a, +b) IN (SELECT x, y FROM c2)
} {}

do_execsql_test 3.5 {
  SELECT c1.rowid FROM c1 WHERE b = (SELECT y FROM c2);
} {}
do_execsql_test 3.6 {
  SELECT c1.rowid FROM c1 WHERE (a, b) = (SELECT x, y FROM c2);
} {}


#-------------------------------------------------------------------------
#
do_execsql_test 4.0 {
  CREATE TABLE d1(a TEXT, b INTEGER, c NUMERIC);
  CREATE TABLE d2(x BLOB, y BLOB);

  INSERT INTO d1 VALUES(1, 1, 1);
  INSERT INTO d1 VALUES(2, 2, 2);
  INSERT INTO d1 VALUES(3, 3, 3);
  INSERT INTO d1 VALUES(4, 4, 4);

  INSERT INTO d2 VALUES (1, 1);
  INSERT INTO d2 VALUES (2, '2');
  INSERT INTO d2 VALUES ('3', 3);
  INSERT INTO d2 VALUES ('4', '4');
}

foreach {tn idx} {
  1 {}
  2 { CREATE INDEX idx ON d1(a) }
  3 { CREATE INDEX idx ON d1(a, c) }
  4 { CREATE INDEX idx ON d1(c) }
  5 { CREATE INDEX idx ON d1(c, a) }

  6 { 
    CREATE INDEX idx ON d1(c, a) ;
    CREATE INDEX idx1 ON d2(x, y);
  }

  7 { 
    CREATE INDEX idx ON d1(c, a) ;
    CREATE UNIQUE INDEX idx2 ON d2(x, y) ;
  }

  8 { 
    CREATE INDEX idx ON d1(c) ;
    CREATE UNIQUE INDEX idx2 ON d2(x);
  }

} {
  execsql { DROP INDEX IF EXISTS idx } 
  execsql { DROP INDEX IF EXISTS idx2 } 
  execsql { DROP INDEX IF EXISTS idx3 } 
  execsql $idx

  do_execsql_test 4.$tn.1 {
    SELECT rowid FROM d1 WHERE (a, c) IN (SELECT x, y FROM d2);
  } {3 4}

  do_execsql_test 4.$tn.2 {
    SELECT rowid FROM d1 WHERE (c, a) IN (SELECT x, y FROM d2);
  } {2 4}

  do_execsql_test 4.$tn.3 {
    SELECT rowid FROM d1 WHERE (+c, a) IN (SELECT x, y FROM d2);
  } {2}

  do_execsql_test 4.$tn.4 {
    SELECT rowid FROM d1 WHERE (c, a) = (
      SELECT x, y FROM d2 WHERE d2.rowid=d1.rowid
    );
  } {2 4}

  do_execsql_test 4.$tn.5 {
    SELECT d1.rowid FROM d1, d2 WHERE a = y;
  } {2 4}

  do_execsql_test 4.$tn.6 {
    SELECT d1.rowid FROM d1 WHERE a = (
      SELECT y FROM d2 where d2.rowid=d1.rowid
    );
  } {2 4}
}

do_execsql_test 5.0 {
  CREATE TABLE e1(a TEXT, c NUMERIC);
  CREATE TABLE e2(x BLOB, y BLOB);

  INSERT INTO e1 VALUES(2, 2);

  INSERT INTO e2 VALUES ('2', 2);
  INSERT INTO e2 VALUES ('2', '2');
  INSERT INTO e2 VALUES ('2', '2.0');

  CREATE INDEX e1c ON e1(c);
}

do_execsql_test 5.1 {
  SELECT rowid FROM e1 WHERE (a, c) IN (SELECT x, y FROM e2);
} {1}
do_execsql_test 5.2 {
  SELECT rowid FROM e2 WHERE rowid IN (SELECT +c FROM e1);
} {2}
do_execsql_test 5.3 {
  SELECT rowid FROM e2 WHERE rowid IN (SELECT 0+c FROM e1);
} {2}

#-------------------------------------------------------------------------
#
do_execsql_test 6.0 {
  CREATE TABLE f1(a, b);
  CREATE TABLE f2(c, d);
  CREATE TABLE f3(e, f);
}

do_execsql_test 6.1 {
  SELECT * FROM f3 WHERE (e, f) IN (
    SELECT a, b FROM f1 UNION ALL SELECT c, d FROM f2
  );
}
do_execsql_test 6.2 {
  CREATE INDEX f3e ON f3(e);
  SELECT * FROM f3 WHERE (e, f) IN (
    SELECT a, b FROM f1 UNION ALL SELECT c, d FROM f2
  );
}


#-------------------------------------------------------------------------
#
do_execsql_test 7.0 {
  CREATE TABLE g1(a, b); 
  INSERT INTO g1 VALUES
      (1, 1), (1, 2), (1, 3), (1, 'i'), (1, 'j'),
      (1, 6), (1, 7), (1, 8), (1, 9), (1, 10),
      (1, 4), (1, 5);

  CREATE TABLE g2(x, y);
  CREATE INDEX g2x ON g2(x);

  INSERT INTO g2 VALUES(1, 4);
  INSERT INTO g2 VALUES(1, 5);
}

do_execsql_test 7.1 {
  SELECT * FROM g2 WHERE (x, y) IN (
    SELECT a, b FROM g1 ORDER BY +a, +b LIMIT 10
  );
} { 1 4 1 5 }

do_execsql_test 7.2 {
  SELECT * FROM g2 WHERE (x, y) IN (
    SELECT a, b FROM g1 ORDER BY a, b LIMIT 10
  );
} { 1 4 1 5 }

do_execsql_test 7.3 {
  SELECT * FROM g2 WHERE (x, y) IN (
    SELECT a, b FROM g1 ORDER BY 1, 2 LIMIT 10
  );
} { 1 4 1 5 }

#-------------------------------------------------------------------------
#
do_execsql_test 8.1 {
  CREATE TABLE t1(a ,b FLOAT);
  CREATE INDEX t1x1 ON t1(a,b,a,a,a,a,a,a,a,a,a,b);
}

do_catchsql_test 8.2 {
  SELECT a FROM t1 NATURAL JOIN t1 WHERE (a,b)> (SELECT 2 IN (SELECT 2,2), 2);
} {1 {sub-select returns 2 columns - expected 1}}

#-------------------------------------------------------------------------
#
reset_db
do_execsql_test 9.0 {
  CREATE TABLE t1(a, b);
  INSERT INTO t1 VALUES(1, 1), (1, 2), (2, 2), (2, 3), (3, 3), (3, 4), (4, 4);
}

do_execsql_test 9.1 {
  SELECT * FROM t1 WHERE (a, b) IN ( (3, 3), (2, 2) );
} {
  2 2 3 3
}
do_execsql_test 9.2 {
  CREATE INDEX i1 ON t1(a);
}

do_execsql_test 9.4 {
  SELECT * FROM t1 WHERE (a, b) IN ( (3, 3), (2, 2) );
} {
  2 2 3 3
}
do_eqp_test 9.4e {
  SELECT * FROM t1 WHERE (a, b) IN ( (3, 3), (2, 2) );
} {
  *SEARCH t1 USING INDEX i1*
}

do_execsql_test 9.5 {
  CREATE INDEX i2 ON t1(b, a);
  SELECT * FROM t1 WHERE (a, b) IN ( (3, 3), (2, 2) );
} {
  2 2 3 3
}
do_eqp_test 9.5e {
  SELECT * FROM t1 WHERE (a, b) IN ( (3, 3), (2, 2) );
} {
  *SEARCH t1 USING COVERING INDEX i2*
}


finish_test