1# 2001 September 15 2# 3# The author disclaims copyright to this source code. In place of 4# a legal notice, here is a blessing: 5# 6# May you do good and not evil. 7# May you find forgiveness for yourself and forgive others. 8# May you share freely, never taking more than you give. 9# 10#*********************************************************************** 11# This file implements regression tests for SQLite library. The 12# focus of this file is testing UNION, INTERSECT and EXCEPT operators 13# in SELECT statements. 14# 15# $Id: select4.test,v 1.30 2009/04/16 00:24:24 drh Exp $ 16 17set testdir [file dirname $argv0] 18source $testdir/tester.tcl 19 20# Most tests in this file depend on compound-select. But there are a couple 21# right at the end that test DISTINCT, so we cannot omit the entire file. 22# 23ifcapable compound { 24 25# Build some test data 26# 27execsql { 28 CREATE TABLE t1(n int, log int); 29 BEGIN; 30} 31for {set i 1} {$i<32} {incr i} { 32 for {set j 0} {(1<<$j)<$i} {incr j} {} 33 execsql "INSERT INTO t1 VALUES($i,$j)" 34} 35execsql { 36 COMMIT; 37} 38 39do_test select4-1.0 { 40 execsql {SELECT DISTINCT log FROM t1 ORDER BY log} 41} {0 1 2 3 4 5} 42 43# Union All operator 44# 45do_test select4-1.1a { 46 lsort [execsql {SELECT DISTINCT log FROM t1}] 47} {0 1 2 3 4 5} 48do_test select4-1.1b { 49 lsort [execsql {SELECT n FROM t1 WHERE log=3}] 50} {5 6 7 8} 51do_test select4-1.1c { 52 execsql { 53 SELECT DISTINCT log FROM t1 54 UNION ALL 55 SELECT n FROM t1 WHERE log=3 56 ORDER BY log; 57 } 58} {0 1 2 3 4 5 5 6 7 8} 59do_test select4-1.1d { 60 execsql { 61 CREATE TABLE t2 AS 62 SELECT DISTINCT log FROM t1 63 UNION ALL 64 SELECT n FROM t1 WHERE log=3 65 ORDER BY log; 66 SELECT * FROM t2; 67 } 68} {0 1 2 3 4 5 5 6 7 8} 69execsql {DROP TABLE t2} 70do_test select4-1.1e { 71 execsql { 72 CREATE TABLE t2 AS 73 SELECT DISTINCT log FROM t1 74 UNION ALL 75 SELECT n FROM t1 WHERE log=3 76 ORDER BY log DESC; 77 SELECT * FROM t2; 78 } 79} {8 7 6 5 5 4 3 2 1 0} 80execsql {DROP TABLE t2} 81do_test select4-1.1f { 82 execsql { 83 SELECT DISTINCT log FROM t1 84 UNION ALL 85 SELECT n FROM t1 WHERE log=2 86 } 87} {0 1 2 3 4 5 3 4} 88do_test select4-1.1g { 89 execsql { 90 CREATE TABLE t2 AS 91 SELECT DISTINCT log FROM t1 92 UNION ALL 93 SELECT n FROM t1 WHERE log=2; 94 SELECT * FROM t2; 95 } 96} {0 1 2 3 4 5 3 4} 97execsql {DROP TABLE t2} 98ifcapable subquery { 99 do_test select4-1.2 { 100 execsql { 101 SELECT log FROM t1 WHERE n IN 102 (SELECT DISTINCT log FROM t1 UNION ALL 103 SELECT n FROM t1 WHERE log=3) 104 ORDER BY log; 105 } 106 } {0 1 2 2 3 3 3 3} 107} 108 109# EVIDENCE-OF: R-02644-22131 In a compound SELECT statement, only the 110# last or right-most simple SELECT may have an ORDER BY clause. 111# 112do_test select4-1.3 { 113 set v [catch {execsql { 114 SELECT DISTINCT log FROM t1 ORDER BY log 115 UNION ALL 116 SELECT n FROM t1 WHERE log=3 117 ORDER BY log; 118 }} msg] 119 lappend v $msg 120} {1 {ORDER BY clause should come after UNION ALL not before}} 121do_catchsql_test select4-1.4 { 122 SELECT (VALUES(0) INTERSECT SELECT(0) UNION SELECT(0) ORDER BY 1 UNION 123 SELECT 0 UNION SELECT 0 ORDER BY 1); 124} {1 {ORDER BY clause should come after UNION not before}} 125 126# Union operator 127# 128do_test select4-2.1 { 129 execsql { 130 SELECT DISTINCT log FROM t1 131 UNION 132 SELECT n FROM t1 WHERE log=3 133 ORDER BY log; 134 } 135} {0 1 2 3 4 5 6 7 8} 136ifcapable subquery { 137 do_test select4-2.2 { 138 execsql { 139 SELECT log FROM t1 WHERE n IN 140 (SELECT DISTINCT log FROM t1 UNION 141 SELECT n FROM t1 WHERE log=3) 142 ORDER BY log; 143 } 144 } {0 1 2 2 3 3 3 3} 145} 146do_test select4-2.3 { 147 set v [catch {execsql { 148 SELECT DISTINCT log FROM t1 ORDER BY log 149 UNION 150 SELECT n FROM t1 WHERE log=3 151 ORDER BY log; 152 }} msg] 153 lappend v $msg 154} {1 {ORDER BY clause should come after UNION not before}} 155do_test select4-2.4 { 156 set v [catch {execsql { 157 SELECT 0 ORDER BY (SELECT 0) UNION SELECT 0; 158 }} msg] 159 lappend v $msg 160} {1 {ORDER BY clause should come after UNION not before}} 161 162# Except operator 163# 164do_test select4-3.1.1 { 165 execsql { 166 SELECT DISTINCT log FROM t1 167 EXCEPT 168 SELECT n FROM t1 WHERE log=3 169 ORDER BY log; 170 } 171} {0 1 2 3 4} 172do_test select4-3.1.2 { 173 execsql { 174 CREATE TABLE t2 AS 175 SELECT DISTINCT log FROM t1 176 EXCEPT 177 SELECT n FROM t1 WHERE log=3 178 ORDER BY log; 179 SELECT * FROM t2; 180 } 181} {0 1 2 3 4} 182execsql {DROP TABLE t2} 183do_test select4-3.1.3 { 184 execsql { 185 CREATE TABLE t2 AS 186 SELECT DISTINCT log FROM t1 187 EXCEPT 188 SELECT n FROM t1 WHERE log=3 189 ORDER BY log DESC; 190 SELECT * FROM t2; 191 } 192} {4 3 2 1 0} 193execsql {DROP TABLE t2} 194ifcapable subquery { 195 do_test select4-3.2 { 196 execsql { 197 SELECT log FROM t1 WHERE n IN 198 (SELECT DISTINCT log FROM t1 EXCEPT 199 SELECT n FROM t1 WHERE log=3) 200 ORDER BY log; 201 } 202 } {0 1 2 2} 203} 204do_test select4-3.3 { 205 set v [catch {execsql { 206 SELECT DISTINCT log FROM t1 ORDER BY log 207 EXCEPT 208 SELECT n FROM t1 WHERE log=3 209 ORDER BY log; 210 }} msg] 211 lappend v $msg 212} {1 {ORDER BY clause should come after EXCEPT not before}} 213 214# Intersect operator 215# 216do_test select4-4.1.1 { 217 execsql { 218 SELECT DISTINCT log FROM t1 219 INTERSECT 220 SELECT n FROM t1 WHERE log=3 221 ORDER BY log; 222 } 223} {5} 224 225do_test select4-4.1.2 { 226 execsql { 227 SELECT DISTINCT log FROM t1 228 UNION ALL 229 SELECT 6 230 INTERSECT 231 SELECT n FROM t1 WHERE log=3 232 ORDER BY t1.log; 233 } 234} {5 6} 235 236do_test select4-4.1.3 { 237 execsql { 238 CREATE TABLE t2 AS 239 SELECT DISTINCT log FROM t1 UNION ALL SELECT 6 240 INTERSECT 241 SELECT n FROM t1 WHERE log=3 242 ORDER BY log; 243 SELECT * FROM t2; 244 } 245} {5 6} 246execsql {DROP TABLE t2} 247do_test select4-4.1.4 { 248 execsql { 249 CREATE TABLE t2 AS 250 SELECT DISTINCT log FROM t1 UNION ALL SELECT 6 251 INTERSECT 252 SELECT n FROM t1 WHERE log=3 253 ORDER BY log DESC; 254 SELECT * FROM t2; 255 } 256} {6 5} 257execsql {DROP TABLE t2} 258ifcapable subquery { 259 do_test select4-4.2 { 260 execsql { 261 SELECT log FROM t1 WHERE n IN 262 (SELECT DISTINCT log FROM t1 INTERSECT 263 SELECT n FROM t1 WHERE log=3) 264 ORDER BY log; 265 } 266 } {3} 267} 268do_test select4-4.3 { 269 set v [catch {execsql { 270 SELECT DISTINCT log FROM t1 ORDER BY log 271 INTERSECT 272 SELECT n FROM t1 WHERE log=3 273 ORDER BY log; 274 }} msg] 275 lappend v $msg 276} {1 {ORDER BY clause should come after INTERSECT not before}} 277do_catchsql_test select4-4.4 { 278 SELECT 3 IN ( 279 SELECT 0 ORDER BY 1 280 INTERSECT 281 SELECT 1 282 INTERSECT 283 SELECT 2 284 ORDER BY 1 285 ); 286} {1 {ORDER BY clause should come after INTERSECT not before}} 287 288# Various error messages while processing UNION or INTERSECT 289# 290do_test select4-5.1 { 291 set v [catch {execsql { 292 SELECT DISTINCT log FROM t2 293 UNION ALL 294 SELECT n FROM t1 WHERE log=3 295 ORDER BY log; 296 }} msg] 297 lappend v $msg 298} {1 {no such table: t2}} 299do_test select4-5.2 { 300 set v [catch {execsql { 301 SELECT DISTINCT log AS "xyzzy" FROM t1 302 UNION ALL 303 SELECT n FROM t1 WHERE log=3 304 ORDER BY xyzzy; 305 }} msg] 306 lappend v $msg 307} {0 {0 1 2 3 4 5 5 6 7 8}} 308do_test select4-5.2b { 309 set v [catch {execsql { 310 SELECT DISTINCT log AS xyzzy FROM t1 311 UNION ALL 312 SELECT n FROM t1 WHERE log=3 313 ORDER BY "xyzzy"; 314 }} msg] 315 lappend v $msg 316} {0 {0 1 2 3 4 5 5 6 7 8}} 317do_test select4-5.2c { 318 set v [catch {execsql { 319 SELECT DISTINCT log FROM t1 320 UNION ALL 321 SELECT n FROM t1 WHERE log=3 322 ORDER BY "xyzzy"; 323 }} msg] 324 lappend v $msg 325} {1 {1st ORDER BY term does not match any column in the result set}} 326do_test select4-5.2d { 327 set v [catch {execsql { 328 SELECT DISTINCT log FROM t1 329 INTERSECT 330 SELECT n FROM t1 WHERE log=3 331 ORDER BY "xyzzy"; 332 }} msg] 333 lappend v $msg 334} {1 {1st ORDER BY term does not match any column in the result set}} 335do_test select4-5.2e { 336 set v [catch {execsql { 337 SELECT DISTINCT log FROM t1 338 UNION ALL 339 SELECT n FROM t1 WHERE log=3 340 ORDER BY n; 341 }} msg] 342 lappend v $msg 343} {0 {0 1 2 3 4 5 5 6 7 8}} 344do_test select4-5.2f { 345 catchsql { 346 SELECT DISTINCT log FROM t1 347 UNION ALL 348 SELECT n FROM t1 WHERE log=3 349 ORDER BY log; 350 } 351} {0 {0 1 2 3 4 5 5 6 7 8}} 352do_test select4-5.2g { 353 catchsql { 354 SELECT DISTINCT log FROM t1 355 UNION ALL 356 SELECT n FROM t1 WHERE log=3 357 ORDER BY 1; 358 } 359} {0 {0 1 2 3 4 5 5 6 7 8}} 360do_test select4-5.2h { 361 catchsql { 362 SELECT DISTINCT log FROM t1 363 UNION ALL 364 SELECT n FROM t1 WHERE log=3 365 ORDER BY 2; 366 } 367} {1 {1st ORDER BY term out of range - should be between 1 and 1}} 368do_test select4-5.2i { 369 catchsql { 370 SELECT DISTINCT 1, log FROM t1 371 UNION ALL 372 SELECT 2, n FROM t1 WHERE log=3 373 ORDER BY 2, 1; 374 } 375} {0 {1 0 1 1 1 2 1 3 1 4 1 5 2 5 2 6 2 7 2 8}} 376do_test select4-5.2j { 377 catchsql { 378 SELECT DISTINCT 1, log FROM t1 379 UNION ALL 380 SELECT 2, n FROM t1 WHERE log=3 381 ORDER BY 1, 2 DESC; 382 } 383} {0 {1 5 1 4 1 3 1 2 1 1 1 0 2 8 2 7 2 6 2 5}} 384do_test select4-5.2k { 385 catchsql { 386 SELECT DISTINCT 1, log FROM t1 387 UNION ALL 388 SELECT 2, n FROM t1 WHERE log=3 389 ORDER BY n, 1; 390 } 391} {0 {1 0 1 1 1 2 1 3 1 4 1 5 2 5 2 6 2 7 2 8}} 392do_test select4-5.3 { 393 set v [catch {execsql { 394 SELECT DISTINCT log, n FROM t1 395 UNION ALL 396 SELECT n FROM t1 WHERE log=3 397 ORDER BY log; 398 }} msg] 399 lappend v $msg 400} {1 {SELECTs to the left and right of UNION ALL do not have the same number of result columns}} 401do_test select4-5.3-3807-1 { 402 catchsql { 403 SELECT 1 UNION SELECT 2, 3 UNION SELECT 4, 5 ORDER BY 1; 404 } 405} {1 {SELECTs to the left and right of UNION do not have the same number of result columns}} 406do_test select4-5.4 { 407 set v [catch {execsql { 408 SELECT log FROM t1 WHERE n=2 409 UNION ALL 410 SELECT log FROM t1 WHERE n=3 411 UNION ALL 412 SELECT log FROM t1 WHERE n=4 413 UNION ALL 414 SELECT log FROM t1 WHERE n=5 415 ORDER BY log; 416 }} msg] 417 lappend v $msg 418} {0 {1 2 2 3}} 419 420do_test select4-6.1 { 421 execsql { 422 SELECT log, count(*) as cnt FROM t1 GROUP BY log 423 UNION 424 SELECT log, n FROM t1 WHERE n=7 425 ORDER BY cnt, log; 426 } 427} {0 1 1 1 2 2 3 4 3 7 4 8 5 15} 428do_test select4-6.2 { 429 execsql { 430 SELECT log, count(*) FROM t1 GROUP BY log 431 UNION 432 SELECT log, n FROM t1 WHERE n=7 433 ORDER BY count(*), log; 434 } 435} {0 1 1 1 2 2 3 4 3 7 4 8 5 15} 436 437# NULLs are indistinct for the UNION operator. 438# Make sure the UNION operator recognizes this 439# 440do_test select4-6.3 { 441 execsql { 442 SELECT NULL UNION SELECT NULL UNION 443 SELECT 1 UNION SELECT 2 AS 'x' 444 ORDER BY x; 445 } 446} {{} 1 2} 447do_test select4-6.3.1 { 448 execsql { 449 SELECT NULL UNION ALL SELECT NULL UNION ALL 450 SELECT 1 UNION ALL SELECT 2 AS 'x' 451 ORDER BY x; 452 } 453} {{} {} 1 2} 454 455# Make sure the DISTINCT keyword treats NULLs as indistinct. 456# 457ifcapable subquery { 458 do_test select4-6.4 { 459 execsql { 460 SELECT * FROM ( 461 SELECT NULL, 1 UNION ALL SELECT NULL, 1 462 ); 463 } 464 } {{} 1 {} 1} 465 do_test select4-6.5 { 466 execsql { 467 SELECT DISTINCT * FROM ( 468 SELECT NULL, 1 UNION ALL SELECT NULL, 1 469 ); 470 } 471 } {{} 1} 472 do_test select4-6.6 { 473 execsql { 474 SELECT DISTINCT * FROM ( 475 SELECT 1,2 UNION ALL SELECT 1,2 476 ); 477 } 478 } {1 2} 479} 480 481# Test distinctness of NULL in other ways. 482# 483do_test select4-6.7 { 484 execsql { 485 SELECT NULL EXCEPT SELECT NULL 486 } 487} {} 488 489 490# Make sure column names are correct when a compound select appears as 491# an expression in the WHERE clause. 492# 493do_test select4-7.1 { 494 execsql { 495 CREATE TABLE t2 AS SELECT log AS 'x', count(*) AS 'y' FROM t1 GROUP BY log; 496 SELECT * FROM t2 ORDER BY x; 497 } 498} {0 1 1 1 2 2 3 4 4 8 5 15} 499ifcapable subquery { 500 do_test select4-7.2 { 501 execsql2 { 502 SELECT * FROM t1 WHERE n IN (SELECT n FROM t1 INTERSECT SELECT x FROM t2) 503 ORDER BY n 504 } 505 } {n 1 log 0 n 2 log 1 n 3 log 2 n 4 log 2 n 5 log 3} 506 do_test select4-7.3 { 507 execsql2 { 508 SELECT * FROM t1 WHERE n IN (SELECT n FROM t1 EXCEPT SELECT x FROM t2) 509 ORDER BY n LIMIT 2 510 } 511 } {n 6 log 3 n 7 log 3} 512 do_test select4-7.4 { 513 execsql2 { 514 SELECT * FROM t1 WHERE n IN (SELECT n FROM t1 UNION SELECT x FROM t2) 515 ORDER BY n LIMIT 2 516 } 517 } {n 1 log 0 n 2 log 1} 518} ;# ifcapable subquery 519 520} ;# ifcapable compound 521 522# Make sure DISTINCT works appropriately on TEXT and NUMERIC columns. 523do_test select4-8.1 { 524 execsql { 525 BEGIN; 526 CREATE TABLE t3(a text, b float, c text); 527 INSERT INTO t3 VALUES(1, 1.1, '1.1'); 528 INSERT INTO t3 VALUES(2, 1.10, '1.10'); 529 INSERT INTO t3 VALUES(3, 1.10, '1.1'); 530 INSERT INTO t3 VALUES(4, 1.1, '1.10'); 531 INSERT INTO t3 VALUES(5, 1.2, '1.2'); 532 INSERT INTO t3 VALUES(6, 1.3, '1.3'); 533 COMMIT; 534 } 535 execsql { 536 SELECT DISTINCT b FROM t3 ORDER BY c; 537 } 538} {1.1 1.2 1.3} 539do_test select4-8.2 { 540 execsql { 541 SELECT DISTINCT c FROM t3 ORDER BY c; 542 } 543} {1.1 1.10 1.2 1.3} 544 545# Make sure the names of columns are taken from the right-most subquery 546# right in a compound query. Ticket #1721 547# 548ifcapable compound { 549 550do_test select4-9.1 { 551 execsql2 { 552 SELECT x, y FROM t2 UNION SELECT a, b FROM t3 ORDER BY x LIMIT 1 553 } 554} {x 0 y 1} 555do_test select4-9.2 { 556 execsql2 { 557 SELECT x, y FROM t2 UNION ALL SELECT a, b FROM t3 ORDER BY x LIMIT 1 558 } 559} {x 0 y 1} 560do_test select4-9.3 { 561 execsql2 { 562 SELECT x, y FROM t2 EXCEPT SELECT a, b FROM t3 ORDER BY x LIMIT 1 563 } 564} {x 0 y 1} 565do_test select4-9.4 { 566 execsql2 { 567 SELECT x, y FROM t2 INTERSECT SELECT 0 AS a, 1 AS b; 568 } 569} {x 0 y 1} 570do_test select4-9.5 { 571 execsql2 { 572 SELECT 0 AS x, 1 AS y 573 UNION 574 SELECT 2 AS p, 3 AS q 575 UNION 576 SELECT 4 AS a, 5 AS b 577 ORDER BY x LIMIT 1 578 } 579} {x 0 y 1} 580 581ifcapable subquery { 582do_test select4-9.6 { 583 execsql2 { 584 SELECT * FROM ( 585 SELECT 0 AS x, 1 AS y 586 UNION 587 SELECT 2 AS p, 3 AS q 588 UNION 589 SELECT 4 AS a, 5 AS b 590 ) ORDER BY 1 LIMIT 1; 591 } 592} {x 0 y 1} 593do_test select4-9.7 { 594 execsql2 { 595 SELECT * FROM ( 596 SELECT 0 AS x, 1 AS y 597 UNION 598 SELECT 2 AS p, 3 AS q 599 UNION 600 SELECT 4 AS a, 5 AS b 601 ) ORDER BY x LIMIT 1; 602 } 603} {x 0 y 1} 604} ;# ifcapable subquery 605 606do_test select4-9.8 { 607 execsql { 608 SELECT 0 AS x, 1 AS y 609 UNION 610 SELECT 2 AS y, -3 AS x 611 ORDER BY x LIMIT 1; 612 } 613} {0 1} 614 615do_test select4-9.9.1 { 616 execsql2 { 617 SELECT 1 AS a, 2 AS b UNION ALL SELECT 3 AS b, 4 AS a 618 } 619} {a 1 b 2 a 3 b 4} 620 621ifcapable subquery { 622do_test select4-9.9.2 { 623 execsql2 { 624 SELECT * FROM (SELECT 1 AS a, 2 AS b UNION ALL SELECT 3 AS b, 4 AS a) 625 WHERE b=3 626 } 627} {} 628do_test select4-9.10 { 629 execsql2 { 630 SELECT * FROM (SELECT 1 AS a, 2 AS b UNION ALL SELECT 3 AS b, 4 AS a) 631 WHERE b=2 632 } 633} {a 1 b 2} 634do_test select4-9.11 { 635 execsql2 { 636 SELECT * FROM (SELECT 1 AS a, 2 AS b UNION ALL SELECT 3 AS e, 4 AS b) 637 WHERE b=2 638 } 639} {a 1 b 2} 640do_test select4-9.12 { 641 execsql2 { 642 SELECT * FROM (SELECT 1 AS a, 2 AS b UNION ALL SELECT 3 AS e, 4 AS b) 643 WHERE b>0 644 } 645} {a 1 b 2 a 3 b 4} 646} ;# ifcapable subquery 647 648# Try combining DISTINCT, LIMIT, and OFFSET. Make sure they all work 649# together. 650# 651do_test select4-10.1 { 652 execsql { 653 SELECT DISTINCT log FROM t1 ORDER BY log 654 } 655} {0 1 2 3 4 5} 656do_test select4-10.2 { 657 execsql { 658 SELECT DISTINCT log FROM t1 ORDER BY log LIMIT 4 659 } 660} {0 1 2 3} 661do_test select4-10.3 { 662 execsql { 663 SELECT DISTINCT log FROM t1 ORDER BY log LIMIT 0 664 } 665} {} 666do_test select4-10.4 { 667 execsql { 668 SELECT DISTINCT log FROM t1 ORDER BY log LIMIT -1 669 } 670} {0 1 2 3 4 5} 671do_test select4-10.5 { 672 execsql { 673 SELECT DISTINCT log FROM t1 ORDER BY log LIMIT -1 OFFSET 2 674 } 675} {2 3 4 5} 676do_test select4-10.6 { 677 execsql { 678 SELECT DISTINCT log FROM t1 ORDER BY log LIMIT 3 OFFSET 2 679 } 680} {2 3 4} 681do_test select4-10.7 { 682 execsql { 683 SELECT DISTINCT log FROM t1 ORDER BY +log LIMIT 3 OFFSET 20 684 } 685} {} 686do_test select4-10.8 { 687 execsql { 688 SELECT DISTINCT log FROM t1 ORDER BY log LIMIT 0 OFFSET 3 689 } 690} {} 691do_test select4-10.9 { 692 execsql { 693 SELECT DISTINCT max(n), log FROM t1 ORDER BY +log; -- LIMIT 2 OFFSET 1 694 } 695} {31 5} 696 697# Make sure compound SELECTs with wildly different numbers of columns 698# do not cause assertion faults due to register allocation issues. 699# 700do_test select4-11.1 { 701 catchsql { 702 SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2 703 UNION 704 SELECT x FROM t2 705 } 706} {1 {SELECTs to the left and right of UNION do not have the same number of result columns}} 707do_test select4-11.2 { 708 catchsql { 709 SELECT x FROM t2 710 UNION 711 SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2 712 } 713} {1 {SELECTs to the left and right of UNION do not have the same number of result columns}} 714do_test select4-11.3 { 715 catchsql { 716 SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2 717 UNION ALL 718 SELECT x FROM t2 719 } 720} {1 {SELECTs to the left and right of UNION ALL do not have the same number of result columns}} 721do_test select4-11.4 { 722 catchsql { 723 SELECT x FROM t2 724 UNION ALL 725 SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2 726 } 727} {1 {SELECTs to the left and right of UNION ALL do not have the same number of result columns}} 728do_test select4-11.5 { 729 catchsql { 730 SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2 731 EXCEPT 732 SELECT x FROM t2 733 } 734} {1 {SELECTs to the left and right of EXCEPT do not have the same number of result columns}} 735do_test select4-11.6 { 736 catchsql { 737 SELECT x FROM t2 738 EXCEPT 739 SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2 740 } 741} {1 {SELECTs to the left and right of EXCEPT do not have the same number of result columns}} 742do_test select4-11.7 { 743 catchsql { 744 SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2 745 INTERSECT 746 SELECT x FROM t2 747 } 748} {1 {SELECTs to the left and right of INTERSECT do not have the same number of result columns}} 749do_test select4-11.8 { 750 catchsql { 751 SELECT x FROM t2 752 INTERSECT 753 SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2 754 } 755} {1 {SELECTs to the left and right of INTERSECT do not have the same number of result columns}} 756 757do_test select4-11.11 { 758 catchsql { 759 SELECT x FROM t2 760 UNION 761 SELECT x FROM t2 762 UNION ALL 763 SELECT x FROM t2 764 EXCEPT 765 SELECT x FROM t2 766 INTERSECT 767 SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2 768 } 769} {1 {SELECTs to the left and right of INTERSECT do not have the same number of result columns}} 770do_test select4-11.12 { 771 catchsql { 772 SELECT x FROM t2 773 UNION 774 SELECT x FROM t2 775 UNION ALL 776 SELECT x FROM t2 777 EXCEPT 778 SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2 779 EXCEPT 780 SELECT x FROM t2 781 } 782} {1 {SELECTs to the left and right of EXCEPT do not have the same number of result columns}} 783do_test select4-11.13 { 784 catchsql { 785 SELECT x FROM t2 786 UNION 787 SELECT x FROM t2 788 UNION ALL 789 SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2 790 UNION ALL 791 SELECT x FROM t2 792 EXCEPT 793 SELECT x FROM t2 794 } 795} {1 {SELECTs to the left and right of UNION ALL do not have the same number of result columns}} 796do_test select4-11.14 { 797 catchsql { 798 SELECT x FROM t2 799 UNION 800 SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2 801 UNION 802 SELECT x FROM t2 803 UNION ALL 804 SELECT x FROM t2 805 EXCEPT 806 SELECT x FROM t2 807 } 808} {1 {SELECTs to the left and right of UNION do not have the same number of result columns}} 809do_test select4-11.15 { 810 catchsql { 811 SELECT x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x FROM t2 812 UNION 813 SELECT x FROM t2 814 INTERSECT 815 SELECT x FROM t2 816 UNION ALL 817 SELECT x FROM t2 818 EXCEPT 819 SELECT x FROM t2 820 } 821} {1 {SELECTs to the left and right of UNION do not have the same number of result columns}} 822do_test select4-11.16 { 823 catchsql { 824 INSERT INTO t2(rowid) VALUES(2) UNION SELECT 3,4 UNION SELECT 5,6 ORDER BY 1; 825 } 826} {1 {SELECTs to the left and right of UNION do not have the same number of result columns}} 827 828do_test select4-12.1 { 829 sqlite3 db2 :memory: 830 catchsql { 831 SELECT 1 UNION SELECT 2,3 UNION SELECT 4,5 ORDER BY 1; 832 } db2 833} {1 {SELECTs to the left and right of UNION do not have the same number of result columns}} 834 835} ;# ifcapable compound 836 837 838# Ticket [3557ad65a076c] - Incorrect DISTINCT processing with an 839# indexed query using IN. 840# 841do_test select4-13.1 { 842 sqlite3 db test.db 843 db eval { 844 CREATE TABLE t13(a,b); 845 INSERT INTO t13 VALUES(1,1); 846 INSERT INTO t13 VALUES(2,1); 847 INSERT INTO t13 VALUES(3,1); 848 INSERT INTO t13 VALUES(2,2); 849 INSERT INTO t13 VALUES(3,2); 850 INSERT INTO t13 VALUES(4,2); 851 CREATE INDEX t13ab ON t13(a,b); 852 SELECT DISTINCT b from t13 WHERE a IN (1,2,3); 853 } 854} {1 2} 855 856# 2014-02-18: Make sure compound SELECTs work with VALUES clauses 857# 858do_execsql_test select4-14.1 { 859 CREATE TABLE t14(a,b,c); 860 INSERT INTO t14 VALUES(1,2,3),(4,5,6); 861 SELECT * FROM t14 INTERSECT VALUES(3,2,1),(2,3,1),(1,2,3),(2,1,3); 862} {1 2 3} 863do_execsql_test select4-14.2 { 864 SELECT * FROM t14 INTERSECT VALUES(1,2,3); 865} {1 2 3} 866do_execsql_test select4-14.3 { 867 SELECT * FROM t14 868 UNION VALUES(3,2,1),(2,3,1),(1,2,3),(7,8,9),(4,5,6) 869 UNION SELECT * FROM t14 ORDER BY 1, 2, 3 870} {1 2 3 2 3 1 3 2 1 4 5 6 7 8 9} 871do_execsql_test select4-14.4 { 872 SELECT * FROM t14 873 UNION VALUES(3,2,1) 874 UNION SELECT * FROM t14 ORDER BY 1, 2, 3 875} {1 2 3 3 2 1 4 5 6} 876do_execsql_test select4-14.5 { 877 SELECT * FROM t14 EXCEPT VALUES(3,2,1),(2,3,1),(1,2,3),(2,1,3); 878} {4 5 6} 879do_execsql_test select4-14.6 { 880 SELECT * FROM t14 EXCEPT VALUES(1,2,3) 881} {4 5 6} 882do_execsql_test select4-14.7 { 883 SELECT * FROM t14 EXCEPT VALUES(1,2,3) EXCEPT VALUES(4,5,6) 884} {} 885do_execsql_test select4-14.8 { 886 SELECT * FROM t14 EXCEPT VALUES('a','b','c') EXCEPT VALUES(4,5,6) 887} {1 2 3} 888do_execsql_test select4-14.9 { 889 SELECT * FROM t14 UNION ALL VALUES(3,2,1),(2,3,1),(1,2,3),(2,1,3); 890} {1 2 3 4 5 6 3 2 1 2 3 1 1 2 3 2 1 3} 891do_execsql_test select4-14.10 { 892 SELECT (VALUES(1),(2),(3),(4)) 893} {1} 894do_execsql_test select4-14.11 { 895 SELECT (SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4) 896} {1} 897do_execsql_test select4-14.12 { 898 VALUES(1) UNION VALUES(2); 899} {1 2} 900do_execsql_test select4-14.13 { 901 VALUES(1),(2),(3) EXCEPT VALUES(2); 902} {1 3} 903do_execsql_test select4-14.14 { 904 VALUES(1),(2),(3) EXCEPT VALUES(1),(3); 905} {2} 906do_execsql_test select4-14.15 { 907 SELECT * FROM (SELECT 123), (SELECT 456) ON likely(0 OR 1) OR 0; 908} {123 456} 909 910finish_test 911