select4.test 19 KB

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