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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
|
#!/usr/bin/env tarantool
test = require("sqltester")
test:plan(28)
--!./tcltestrunner.lua
-- 2007 November 29
--
-- 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 tests the optimisations made in November 2007 of expressions
-- of the following form:
--
-- <value> IN (SELECT <column> FROM <table>)
--
-- $Id: in3.test,v 1.5 2008/08/04 03:51:24 danielk1977 Exp $
-- ["set","testdir",[["file","dirname",["argv0"]]]]
-- ["source",[["testdir"],"\/tester.tcl"]]
-- Return the number of OpenEphemeral instructions used in the
-- implementation of the sql statement passed as a an argument.
--
local function nEphemeral(sql)
nEph = 0
for _, op in ipairs(test:execsql("EXPLAIN "..sql.."")) do
if (op == "OpenTEphemeral")
then
nEph = nEph + 1
end
end
return nEph
end
-- This proc works the same way as execsql, except that the number
-- of OpenEphemeral instructions used in the implementation of the
-- statement is inserted into the start of the returned list.
--
local function exec_neph(sql)
local res = test:execsql('EXPLAIN '..sql)
local cnt = 0
for _, v in ipairs(res) do
if string.find(v, 'OpenTEphemeral') then
cnt = cnt + 1
end
end
res = test:execsql(sql)
table.insert(res, 1, cnt)
return res
-- return X(46, "X!cmd", [=[["concat",[["nEphemeral",["sql"]]],[["execsql",["sql"]]]]]=])
end
test:do_execsql_test(
"in3-1.1",
[[
CREATE TABLE t1(a INT PRIMARY KEY, b INT );
INSERT INTO t1 VALUES(1, 2);
INSERT INTO t1 VALUES(3, 4);
INSERT INTO t1 VALUES(5, 6);
]], {
-- <in3-1.1>
-- </in3-1.1>
})
-- All of these queries should avoid using a temp-table:
--
-- do_test in3-1.2 {
-- exec_neph { SELECT rowid FROM t1 WHERE rowid IN (SELECT rowid FROM t1); }
-- } {0 1 2 3}
test:do_test(
"in3-1.3",
function()
return exec_neph(" SELECT a FROM t1 WHERE a IN (SELECT a FROM t1); ")
end, {
-- <in3-1.3>
0, 1, 3, 5
-- </in3-1.3>
})
-- do_test in3-1.4 {
-- exec_neph { SELECT rowid FROM t1 WHERE rowid+0 IN (SELECT rowid FROM t1); }
-- } {0 1 2 3}
test:do_test(
"in3-1.5",
function()
return exec_neph(" SELECT a FROM t1 WHERE a+0 IN (SELECT a FROM t1); ")
end, {
-- <in3-1.5>
0, 1, 3, 5
-- </in3-1.5>
})
-- Because none of the sub-select queries in the following statements
-- match the pattern ("SELECT <column> FROM <table>"), the following do
-- require a temp table.
--
-- do_test in3-1.6 {
-- exec_neph { SELECT rowid FROM t1 WHERE rowid IN (SELECT rowid+0 FROM t1); }
-- } {1 1 2 3}
test:do_test(
"in3-1.7",
function()
return exec_neph(" SELECT a FROM t1 WHERE a IN (SELECT a+0 FROM t1); ")
end, {
-- <in3-1.7>
1, 1, 3, 5
-- </in3-1.7>
})
test:do_test(
"in3-1.8",
function()
return exec_neph(" SELECT a FROM t1 WHERE a IN (SELECT a FROM t1 WHERE true); ")
end, {
-- <in3-1.8>
1, 1, 3, 5
-- </in3-1.8>
})
test:do_test(
"in3-1.9",
function()
return exec_neph(" SELECT a FROM t1 WHERE a IN (SELECT a FROM t1 GROUP BY a); ")
end, {
-- <in3-1.9>
1, 1, 3, 5
-- </in3-1.9>
})
-- This should not use a temp-table. Even though the sub-select does
-- not exactly match the pattern "SELECT <column> FROM <table>", in
-- this case the ORDER BY is a no-op and can be ignored.
test:do_test(
"in3-1.10",
function()
return exec_neph(" SELECT a FROM t1 WHERE a IN (SELECT a FROM t1 ORDER BY a); ")
end, {
-- <in3-1.10>
0, 1, 3, 5
-- </in3-1.10>
})
-- These do use the temp-table. Adding the LIMIT clause means the
-- ORDER BY cannot be ignored.
test:do_test(
"in3-1.11",
function()
return exec_neph("SELECT a FROM t1 WHERE a IN (SELECT a FROM t1 ORDER BY a LIMIT 1)")
end, {
-- <in3-1.11>
1, 1
-- </in3-1.11>
})
test:do_test(
"in3-1.12",
function()
return exec_neph([[
SELECT a FROM t1 WHERE a IN (SELECT a FROM t1 ORDER BY a LIMIT 1 OFFSET 1)
]])
end, {
-- <in3-1.12>
1, 3
-- </in3-1.12>
})
-- Has to use a temp-table because of the compound sub-select.
--
test:do_test(
"in3-1.13",
function()
return exec_neph([[
SELECT a FROM t1 WHERE a IN (
SELECT a FROM t1 UNION ALL SELECT a FROM t1
)
]])
end, {
-- <in3-1.13>
1, 1, 3, 5
-- </in3-1.13>
})
test:do_execsql_test(
"in3-1.14",
[[
CREATE TABLE t2(a TEXT PRIMARY KEY);
INSERT INTO t2 VALUES('A');
INSERT INTO t2 VALUES('B');
INSERT INTO t2 VALUES('C');
]], {
-- <in3-1.14>
-- </in3-1.14>
})
-- The first of these queries has to use the temp-table, because the
-- collation sequence used for the index on "t1.a" does not match the
-- collation sequence used by the "IN" comparison. The second does not
-- require a temp-table, because the collation sequences match.
--
test:do_test(
"in3-1.15",
function()
return exec_neph(" SELECT a FROM t2 WHERE a COLLATE \"unicode_ci\" IN (SELECT a FROM t2) ")
end, {
-- <in3-1.15>
1, "A", "B", "C"
-- </in3-1.15>
})
test:do_test(
"in3-1.16",
function()
return exec_neph(" SELECT a FROM t2 WHERE a COLLATE \"binary\" IN (SELECT a FROM t2) ")
end, {
-- <in3-1.16>
1, "A", "B", "C"
-- </in3-1.16>
})
test:do_execsql_test(
"in3-1.17",
[[
DROP TABLE t2
]], {
-- <in3-1.17>
-- </in3-1.17>
})
-- Neither of these queries require a temp-table. The collation sequence
-- makes no difference when using a rowid.
--
-- do_test in3-1.16 {
-- exec_neph {SELECT a FROM t1 WHERE a COLLATE nocase IN (SELECT rowid FROM t1)}
-- } {0 1 3}
-- do_test in3-1.17 {
-- exec_neph {SELECT a FROM t1 WHERE a COLLATE binary IN (SELECT rowid FROM t1)}
-- } {0 1 3}
-- # The following tests - in3.2.* - test a bug that was difficult to track
-- # down during development. They are not particularly well focused.
-- #
-- do_test in3-2.1 {
-- execsql {
-- DROP TABLE IF EXISTS t1;
-- CREATE TABLE t1(w int, x int, y int);
-- CREATE TABLE t2(p int, q int, r int, s int);
-- }
-- for {set i 1} {$i<=100} {incr i} {
-- set w $i
-- set x [expr {int(log($i)/log(2))}]
-- set y [expr {$i*$i + 2*$i + 1}]
-- execsql "INSERT INTO t1 VALUES($w,$x,$y)"
-- }
-- set maxy [execsql {select max(y) from t1}]
-- db eval { INSERT INTO t2 SELECT 101-w, x, $maxy+1-y, y FROM t1 }
-- } {}
-- do_test in3-2.2 {
-- execsql {
-- SELECT rowid
-- FROM t1
-- WHERE rowid IN (SELECT rowid FROM t1 WHERE rowid IN (1, 2));
-- }
-- } {1 2}
-- do_test in3-2.3 {
-- execsql {
-- select rowid from t1 where rowid IN (-1,2,4)
-- }
-- } {2 4}
-- do_test in3-2.4 {
-- execsql {
-- SELECT rowid FROM t1 WHERE rowid IN
-- (select rowid from t1 where rowid IN (-1,2,4))
-- }
-- } {2 4}
---------------------------------------------------------------------------
-- This next block of tests - in3-3.* - verify that column affinity is
-- correctly handled in cases where an index might be used to optimise
-- an IN (SELECT) expression.
--
test:do_test(
"in3-3.1",
function()
-- X(184, "X!cmd", [=[["catch","execsql {\n DROP TABLE t1;\n DROP TABLE t2;\n }"]]=])
return test:execsql [[
DROP TABLE IF EXISTS t1;
DROP TABLE IF EXISTS t1;
CREATE TABLE t1(id INT primary key, a SCALAR, b NUMBER ,c TEXT);
CREATE UNIQUE INDEX t1_i1 ON t1(a); /* no affinity */
CREATE UNIQUE INDEX t1_i2 ON t1(b); /* numeric affinity */
CREATE UNIQUE INDEX t1_i3 ON t1(c); /* text affinity */
CREATE TABLE t2(id INT primary key, x SCALAR, y NUMBER, z TEXT);
CREATE UNIQUE INDEX t2_i1 ON t2(x); /* no affinity */
CREATE UNIQUE INDEX t2_i2 ON t2(y); /* numeric affinity */
CREATE UNIQUE INDEX t2_i3 ON t2(z); /* text affinity */
INSERT INTO t1 VALUES(1, '1', 1, '1');
INSERT INTO t2 VALUES(1, '1', 1, '1');
]]
end, {
-- <in3-3.1>
-- </in3-3.1>
})
test:do_test(
"in3-3.2",
function()
-- Both columns have type 'BLOB' so they are comparable.
-- Moreover, we can use index and avoid materializing
-- retults into ephemeral table.
return exec_neph(" SELECT x IN (SELECT a FROM t1) FROM t2 ")
end, {
-- <in3-3.2>
0, true
-- </in3-3.2>
})
test:do_test(
"in3-3.4",
function()
-- SCALAR is compatible with TEXT, however index can't
-- be used since SCALAR can accept not only string values.
return exec_neph(" SELECT x IN (SELECT c FROM t1) FROM t2 ")
end, {
-- <in3-3.4>
1, true
-- </in3-3.4>
})
test:do_test(
"in3-3.5",
function()
-- Numeric affinity should be applied to each side before the comparison
-- takes place. Therefore we cannot use index t1_i1, which has no affinity.
return exec_neph(" SELECT y IN (SELECT a FROM t1) FROM t2 ")
end, {
-- <in3-3.5>
1, true
-- </in3-3.5>
})
test:do_test(
"in3-3.6",
function()
-- Numeric affinity is applied to both sides before
-- the comparison. Therefore it is possible to use index t1_i2.
return exec_neph(" SELECT y IN (SELECT b FROM t1) FROM t2 ")
end, {
-- <in3-3.6>
0, true
-- </in3-3.6>
})
test:do_test(
"in3-3.7",
function()
-- Numeric affinity is applied before the comparison takes place.
-- Making it impossible to use index t1_i3.
return exec_neph(" SELECT y IN (SELECT c FROM t1) FROM t2 ")
end, {
-- <in3-3.7>
1, true
-- </in3-3.7>
})
-----------------------------------------------------------------------
--
-- Test using a multi-column index.
--
test:do_test(
"in3-4.1",
function()
test:execsql [[
CREATE TABLE t3(a INT PRIMARY KEY, b TEXT , c INT );
CREATE UNIQUE INDEX t3_i ON t3(b, a);
]]
return test:execsql [[
INSERT INTO t3 VALUES(1, 'numeric', 2);
INSERT INTO t3 VALUES(2, 'text', 2);
INSERT INTO t3 VALUES(3, 'real', 2);
INSERT INTO t3 VALUES(4, 'none', 2);
]]
end, {
-- <in3-4.1>
-- </in3-4.1>
})
-- No need in ephemeral table since index t3_i can be used:
-- types are matching, column 'b' is leftmost in index.
--
test:do_test(
"in3-4.2",
function()
return exec_neph(" SELECT 'text' IN (SELECT b FROM t3)")
end, {
-- <in3-4.2>
0, true
-- </in3-4.2>
})
-- Ephemeral table is used since collations of indexed
-- column (rhs) 'b' and searched value (lhs) are different.
--
test:do_test(
"in3-4.3",
function()
return exec_neph(" SELECT 'TEXT' COLLATE \"unicode_ci\" IN (SELECT b FROM t3) ")
end, {
-- <in3-4.3>
1, true
-- </in3-4.3>
})
test:do_test(
"in3-4.4",
function()
-- A temp table must be used because t3_i.b is not guaranteed to be unique.
return exec_neph(" SELECT b FROM t3 WHERE b IN (SELECT b FROM t3) ")
end, {
-- <in3-4.4>
1, "none", "numeric", "real", "text"
-- </in3-4.4>
})
test:do_test(
"in3-4.5",
function()
test:execsql " CREATE UNIQUE INDEX t3_i2 ON t3(b) "
return exec_neph(" SELECT b FROM t3 WHERE b IN (SELECT b FROM t3) ")
end, {
-- <in3-4.5>
0, "none", "numeric", "real", "text"
-- </in3-4.5>
})
test:do_execsql_test(
"in3-4.6",
[[
DROP INDEX t3_i2 ON t3
]], {
-- <in3-4.6>
-- </in3-4.6>
})
-- The following two test cases verify that ticket #2991 has been fixed.
--
test:do_execsql_test(
"in3-5.1",
[[
CREATE TABLE Folders(
folderid INTEGER PRIMARY KEY,
parentid INTEGER,
rootid INTEGER,
path VARCHAR(255)
);
]], {
-- <in3-5.1>
-- </in3-5.1>
})
test:do_catchsql_test(
"in3-5.2",
[[
DELETE FROM Folders WHERE folderid IN
(SELECT folderid FROM Folder WHERE path LIKE 'C:\MP3\Albums\' || '%');
]], {
-- <in3-5.2>
1, "Space 'FOLDER' does not exist"
-- </in3-5.2>
})
test:finish_test()
|