summaryrefslogtreecommitdiffstats
path: root/test/indexA.test
blob: 518d7e18adb6d5fdd330b3f14fd4ca28c053ecf5 (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
# 2023 September 23
#
# 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.
#
#***********************************************************************
#
#

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

do_execsql_test 1.0 {
  CREATE TABLE t1(a TEXT, b, c);
  CREATE INDEX i1 ON t1(b, c) WHERE a='abc';
  INSERT INTO t1 VALUES('abc', 1, 2);
}

do_execsql_test 1.1 {
  SELECT * FROM t1 WHERE a='abc'
} {abc 1 2}

do_eqp_test 1.2 {
  SELECT * FROM t1 WHERE a='abc'
} {USING COVERING INDEX i1}

do_execsql_test 1.3 {
  CREATE INDEX i2 ON t1(b, c) WHERE a=5;
  INSERT INTO t1 VALUES(5, 4, 3);

  SELECT a, typeof(a), b, c FROM t1 WHERE a=5;
} {5 text 4 3}

do_execsql_test 1.4 {
  CREATE TABLE t2(x);
  INSERT INTO t2 VALUES('v');
}

do_execsql_test 1.5 {
  SELECT x, a, b, c FROM t2 LEFT JOIN t1 ON (a=5 AND b=x)
} {v {} {} {}}

do_execsql_test 1.6 {
  SELECT x, a, b, c FROM t2 RIGHT JOIN t1 ON (t1.a=5 AND t1.b=t2.x)
} {{} abc 1 2   {} 5 4 3}

do_eqp_test 1.7 {
  SELECT x, a, b, c FROM t2 RIGHT JOIN t1 ON (t1.a=5 AND t1.b=t2.x)
} {USING INDEX i2}

#-------------------------------------------------------------------------
reset_db

do_execsql_test 2.0 {
  CREATE TABLE x1(a TEXT, b, c);
  INSERT INTO x1 VALUES('2', 'two', 'ii');
  INSERT INTO x1 VALUES('2.0', 'twopointoh', 'ii.0');

  CREATE TABLE x2(a NUMERIC, b, c);
  INSERT INTO x2 VALUES('2', 'two', 'ii');
  INSERT INTO x2 VALUES('2.0', 'twopointoh', 'ii.0');

  CREATE TABLE x3(a REAL, b, c);
  INSERT INTO x3 VALUES('2', 'two', 'ii');
  INSERT INTO x3 VALUES('2.0', 'twopointoh', 'ii.0');
}

foreach {tn idx} {
  0 {
  }
  1 {
    CREATE INDEX i1 ON x1(b, c) WHERE a=2;
    CREATE INDEX i2 ON x2(b, c) WHERE a=2;
    CREATE INDEX i3 ON x3(b, c) WHERE a=2;
  }
  2 {
    CREATE INDEX i1 ON x1(b, c) WHERE a=2.0;
    CREATE INDEX i2 ON x2(b, c) WHERE a=2.0;
    CREATE INDEX i3 ON x3(b, c) WHERE a=2.0;
  }
  3 {
    CREATE INDEX i1 ON x1(b, c) WHERE a='2.0';
    CREATE INDEX i2 ON x2(b, c) WHERE a='2.0';
    CREATE INDEX i3 ON x3(b, c) WHERE a='2.0';
  }
  4 {
    CREATE INDEX i1 ON x1(b, c) WHERE a='2';
    CREATE INDEX i2 ON x2(b, c) WHERE a='2';
    CREATE INDEX i3 ON x3(b, c) WHERE a='2';
  }
} {
  execsql { DROP INDEX IF EXISTS i1 }
  execsql { DROP INDEX IF EXISTS i2 }
  execsql { DROP INDEX IF EXISTS i3 }

  execsql $idx
  do_execsql_test 2.1.$tn.1 {
    SELECT *, typeof(a) FROM x1 WHERE a=2
  } {2 two ii text}
  do_execsql_test 2.1.$tn.2 {
    SELECT *, typeof(a) FROM x1 WHERE a=2.0
  } {2.0 twopointoh ii.0 text}
  do_execsql_test 2.1.$tn.3 {
    SELECT *, typeof(a) FROM x1 WHERE a='2'
  } {2 two ii text}
  do_execsql_test 2.1.$tn.4 {
    SELECT *, typeof(a) FROM x1 WHERE a='2.0'
  } {2.0 twopointoh ii.0 text}

  do_execsql_test 2.1.$tn.5 {
    SELECT *, typeof(a) FROM x2 WHERE a=2
  } {2 two ii integer 2 twopointoh ii.0 integer}
  do_execsql_test 2.1.$tn.6 {
    SELECT *, typeof(a) FROM x2 WHERE a=2.0
  } {2 two ii integer 2 twopointoh ii.0 integer}
  do_execsql_test 2.1.$tn.7 {
    SELECT *, typeof(a) FROM x2 WHERE a='2'
  } {2 two ii integer 2 twopointoh ii.0 integer}
  do_execsql_test 2.1.$tn.8 {
    SELECT *, typeof(a) FROM x2 WHERE a='2.0'
  } {2 two ii integer 2 twopointoh ii.0 integer}

  do_execsql_test 2.1.$tn.9 {
    SELECT *, typeof(a) FROM x3 WHERE a=2
  } {2.0 two ii real 2.0 twopointoh ii.0 real}
  do_execsql_test 2.1.$tn.10 {
    SELECT *, typeof(a) FROM x3 WHERE a=2.0
  } {2.0 two ii real 2.0 twopointoh ii.0 real}
  do_execsql_test 2.1.$tn.11 {
    SELECT *, typeof(a) FROM x3 WHERE a='2'
  } {2.0 two ii real 2.0 twopointoh ii.0 real}
  do_execsql_test 2.1.$tn.12 {
    SELECT *, typeof(a) FROM x3 WHERE a='2.0'
  } {2.0 two ii real 2.0 twopointoh ii.0 real}

}

reset_db
do_execsql_test 3.0 {
  CREATE TABLE x1(a TEXT, d PRIMARY KEY, b, c) WITHOUT ROWID;
  INSERT INTO x1 VALUES('2', 1, 'two', 'ii');
  INSERT INTO x1 VALUES('2.0', 2, 'twopointoh', 'ii.0');

  CREATE TABLE x2(a NUMERIC, b, c, d PRIMARY KEY) WITHOUT ROWID;
  INSERT INTO x2 VALUES('2', 'two', 'ii', 1);
  INSERT INTO x2 VALUES('2.0', 'twopointoh', 'ii.0', 2);

  CREATE TABLE x3(d PRIMARY KEY, a REAL, b, c) WITHOUT ROWID;
  INSERT INTO x3 VALUES(34, '2', 'two', 'ii');
  INSERT INTO x3 VALUES(35, '2.0', 'twopointoh', 'ii.0');
}

foreach {tn idx} {
  0 {
  }
  1 {
    CREATE INDEX i1 ON x1(b, c) WHERE a=2;
    CREATE INDEX i2 ON x2(b, c) WHERE a=2;
    CREATE INDEX i3 ON x3(b, c) WHERE a=2;
  }
  2 {
    CREATE INDEX i1 ON x1(b, c) WHERE a=2.0;
    CREATE INDEX i2 ON x2(b, c) WHERE a=2.0;
    CREATE INDEX i3 ON x3(b, c) WHERE a=2.0;
  }
  3 {
    CREATE INDEX i1 ON x1(b, c) WHERE a='2.0';
    CREATE INDEX i2 ON x2(b, c) WHERE a='2.0';
    CREATE INDEX i3 ON x3(b, c) WHERE a='2.0';
  }
  4 {
    CREATE INDEX i1 ON x1(b, c) WHERE a='2';
    CREATE INDEX i2 ON x2(b, c) WHERE a='2';
    CREATE INDEX i3 ON x3(b, c) WHERE a='2';
  }
} {
  execsql { DROP INDEX IF EXISTS i1 }
  execsql { DROP INDEX IF EXISTS i2 }
  execsql { DROP INDEX IF EXISTS i3 }

  execsql $idx
  do_execsql_test 3.1.$tn.1 {
    SELECT a, b, c, typeof(a) FROM x1 WHERE a=2
  } {2 two ii text}
  do_execsql_test 3.1.$tn.2 {
    SELECT a, b, c, typeof(a) FROM x1 WHERE a=2.0
  } {2.0 twopointoh ii.0 text}
  do_execsql_test 3.1.$tn.3 {
    SELECT a, b, c, typeof(a) FROM x1 WHERE a='2'
  } {2 two ii text}
  do_execsql_test 3.1.$tn.4 {
    SELECT a, b, c, typeof(a) FROM x1 WHERE a='2.0'
  } {2.0 twopointoh ii.0 text}

  do_execsql_test 3.1.$tn.5 {
    SELECT a, b, c, typeof(a) FROM x2 WHERE a=2
  } {2 two ii integer 2 twopointoh ii.0 integer}
  do_execsql_test 3.1.$tn.6 {
    SELECT a, b, c, typeof(a) FROM x2 WHERE a=2.0
  } {2 two ii integer 2 twopointoh ii.0 integer}
  do_execsql_test 3.1.$tn.7 {
    SELECT a, b, c, typeof(a) FROM x2 WHERE a='2'
  } {2 two ii integer 2 twopointoh ii.0 integer}
  do_execsql_test 3.1.$tn.8 {
    SELECT a, b, c, typeof(a) FROM x2 WHERE a='2.0'
  } {2 two ii integer 2 twopointoh ii.0 integer}

  do_execsql_test 3.1.$tn.9 {
    SELECT a, b, c, typeof(a) FROM x3 WHERE a=2
  } {2.0 two ii real 2.0 twopointoh ii.0 real}
  do_execsql_test 3.1.$tn.10 {
    SELECT a, b, c, typeof(a) FROM x3 WHERE a=2.0
  } {2.0 two ii real 2.0 twopointoh ii.0 real}
  do_execsql_test 3.1.$tn.11 {
    SELECT a, b, c, typeof(a) FROM x3 WHERE a='2'
  } {2.0 two ii real 2.0 twopointoh ii.0 real}
  do_execsql_test 3.1.$tn.12 {
    SELECT a, b, c, typeof(a) FROM x3 WHERE a='2.0'
  } {2.0 two ii real 2.0 twopointoh ii.0 real}
}

#-------------------------------------------------------------------------
reset_db
do_execsql_test 4.0 {
  CREATE TABLE t2(a INTEGER, b TEXT);
  INSERT INTO t2 VALUES(1, 'two');
  INSERT INTO t2 VALUES(2, 'two');
  INSERT INTO t2 VALUES(3, 'two');
  INSERT INTO t2 VALUES(1, 'three');
  INSERT INTO t2 VALUES(2, 'three');
  INSERT INTO t2 VALUES(3, 'three');

  CREATE INDEX t2a_two ON t2(a) WHERE b='two';
}

# explain_i { SELECT sum(a), b FROM t2 WHERE b='two' }
do_execsql_test 4.1.1 {
  SELECT sum(a), b FROM t2 WHERE b='two'
} {6 two}
do_eqp_test 4.1.2 {
  SELECT sum(a), b FROM t2 WHERE b='two'
} {USING COVERING INDEX t2a_two}

#-------------------------------------------------------------------------
reset_db
do_execsql_test 5.0 {
  CREATE TABLE t1(a INTEGER PRIMQRY KEY, b, c);
}
do_catchsql_test 5.1 {
  CREATE INDEX ex1 ON t1(c) WHERE b IS 'abc' COLLATE g;
} {1 {no such collation sequence: g}}

proc xyz {lhs rhs} {
  return [string compare $lhs $rhs]
}
db collate xyz xyz
do_execsql_test 5.2 {
  CREATE INDEX ex1 ON t1(c) WHERE b IS 'abc' COLLATE xyz;
}
db close
sqlite3 db test.db
do_execsql_test 5.3 {
  SELECT * FROM t1
}

#-------------------------------------------------------------------------
reset_db
do_execsql_test 6.0 {
  CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
  CREATE TABLE t2(x INTEGER PRIMARY KEY, y INTEGER, z INTEGER);
  INSERT INTO t1 VALUES(1, 1, 1);
  INSERT INTO t1 VALUES(2, 1, 2);
  INSERT INTO t2 VALUES(1, 5, 1);
  INSERT INTO t2 VALUES(2, 5, 2);

  CREATE INDEX t2z ON t2(z) WHERE y=5;
}

do_execsql_test 6.1 {
  ANALYZE;
  UPDATE sqlite_stat1 SET stat = '50 1' WHERE idx='t2z';
  UPDATE sqlite_stat1 SET stat = '50' WHERE tbl='t2' AND idx IS NULL;
  UPDATE sqlite_stat1 SET stat = '5000' WHERE tbl='t1' AND idx IS NULL;
  ANALYZE sqlite_schema;
}

do_execsql_test 6.2 {
  SELECT * FROM t1, t2 WHERE b=1 AND z=c AND y=5;
} {
  1 1 1  1 5 1
  2 1 2  2 5 2
}

do_eqp_test 6.3 {
  SELECT * FROM t1, t2 WHERE b=1 AND z=c AND y=5;
} {BLOOM FILTER ON t2}

do_execsql_test 6.4 {
  SELECT * FROM t1 LEFT JOIN t2 ON (y=5) WHERE b=1 AND z IS c;
} {
  1 1 1  1 5 1
  2 1 2  2 5 2
}

do_eqp_test 6.5 {
  SELECT * FROM t1 LEFT JOIN t2 ON (y=5) WHERE b=1 AND z IS c;
} {BLOOM FILTER ON t2}

do_execsql_test 6.6 {
  CREATE INDEX t2yz ON t2(y, z) WHERE y=5;
}

do_execsql_test 6.7 {
  SELECT * FROM t1 LEFT JOIN t2 ON (y=5) WHERE b=1 AND z IS c;
} {
  1 1 1  1 5 1
  2 1 2  2 5 2
}

#-------------------------------------------------------------------------
reset_db
do_execsql_test 7.0 {
  CREATE TABLE t1(i INTEGER PRIMARY KEY, b TEXT, c TEXT);
  CREATE INDEX i1 ON t1(c) WHERE b='abc' AND i=5;
  INSERT INTO t1 VALUES(5, 'abc', 'xyz');
  SELECT * FROM t1 INDEXED BY i1 WHERE b='abc' AND i=5 ORDER BY c;
} {5 abc xyz}

#-------------------------------------------------------------------------
reset_db
do_execsql_test 8.0 {
  CREATE TABLE t1(a, b, c);
  CREATE INDEX ex2 ON t1(a, 4);
  CREATE INDEX ex1 ON t1(a) WHERE 4=b;
  INSERT INTO t1 VALUES(1, 4, 1);
  INSERT INTO t1 VALUES(1, 5, 1);
  INSERT INTO t1 VALUES(2, 4, 2);
}
do_execsql_test 8.1 {
  SELECT * FROM t1 WHERE b=4;
} {
  1 4 1  2 4 2
}

finish_test