e_select.test 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167
  1. # 2010 July 16
  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. #
  12. # This file implements tests to verify that the "testable statements" in
  13. # the lang_select.html document are correct.
  14. #
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. ifcapable !compound {
  18. finish_test
  19. return
  20. }
  21. do_execsql_test e_select-1.0 {
  22. CREATE TABLE t1(a, b);
  23. INSERT INTO t1 VALUES('a', 'one');
  24. INSERT INTO t1 VALUES('b', 'two');
  25. INSERT INTO t1 VALUES('c', 'three');
  26. CREATE TABLE t2(a, b);
  27. INSERT INTO t2 VALUES('a', 'I');
  28. INSERT INTO t2 VALUES('b', 'II');
  29. INSERT INTO t2 VALUES('c', 'III');
  30. CREATE TABLE t3(a, c);
  31. INSERT INTO t3 VALUES('a', 1);
  32. INSERT INTO t3 VALUES('b', 2);
  33. CREATE TABLE t4(a, c);
  34. INSERT INTO t4 VALUES('a', NULL);
  35. INSERT INTO t4 VALUES('b', 2);
  36. } {}
  37. set t1_cross_t2 [list \
  38. a one a I a one b II \
  39. a one c III b two a I \
  40. b two b II b two c III \
  41. c three a I c three b II \
  42. c three c III \
  43. ]
  44. set t1_cross_t1 [list \
  45. a one a one a one b two \
  46. a one c three b two a one \
  47. b two b two b two c three \
  48. c three a one c three b two \
  49. c three c three \
  50. ]
  51. # This proc is a specialized version of [do_execsql_test].
  52. #
  53. # The second argument to this proc must be a SELECT statement that
  54. # features a cross join of some time. Instead of the usual ",",
  55. # "CROSS JOIN" or "INNER JOIN" join-op, the string %JOIN% must be
  56. # substituted.
  57. #
  58. # This test runs the SELECT three times - once with:
  59. #
  60. # * s/%JOIN%/,/
  61. # * s/%JOIN%/JOIN/
  62. # * s/%JOIN%/INNER JOIN/
  63. # * s/%JOIN%/CROSS JOIN/
  64. #
  65. # and checks that each time the results of the SELECT are $res.
  66. #
  67. proc do_join_test {tn select res} {
  68. foreach {tn2 joinop} [list 1 , 2 "CROSS JOIN" 3 "INNER JOIN"] {
  69. set S [string map [list %JOIN% $joinop] $select]
  70. uplevel do_execsql_test $tn.$tn2 [list $S] [list $res]
  71. }
  72. }
  73. #-------------------------------------------------------------------------
  74. # The following tests check that all paths on the syntax diagrams on
  75. # the lang_select.html page may be taken.
  76. #
  77. # -- syntax diagram join-constraint
  78. #
  79. do_join_test e_select-0.1.1 {
  80. SELECT count(*) FROM t1 %JOIN% t2 ON (t1.a=t2.a)
  81. } {3}
  82. do_join_test e_select-0.1.2 {
  83. SELECT count(*) FROM t1 %JOIN% t2 USING (a)
  84. } {3}
  85. do_join_test e_select-0.1.3 {
  86. SELECT count(*) FROM t1 %JOIN% t2
  87. } {9}
  88. do_catchsql_test e_select-0.1.4 {
  89. SELECT count(*) FROM t1, t2 ON (t1.a=t2.a) USING (a)
  90. } {1 {cannot have both ON and USING clauses in the same join}}
  91. do_catchsql_test e_select-0.1.5 {
  92. SELECT count(*) FROM t1, t2 USING (a) ON (t1.a=t2.a)
  93. } {1 {near "ON": syntax error}}
  94. # -- syntax diagram select-core
  95. #
  96. # 0: SELECT ...
  97. # 1: SELECT DISTINCT ...
  98. # 2: SELECT ALL ...
  99. #
  100. # 0: No FROM clause
  101. # 1: Has FROM clause
  102. #
  103. # 0: No WHERE clause
  104. # 1: Has WHERE clause
  105. #
  106. # 0: No GROUP BY clause
  107. # 1: Has GROUP BY clause
  108. # 2: Has GROUP BY and HAVING clauses
  109. #
  110. do_select_tests e_select-0.2 {
  111. 0000.1 "SELECT 1, 2, 3 " {1 2 3}
  112. 1000.1 "SELECT DISTINCT 1, 2, 3 " {1 2 3}
  113. 2000.1 "SELECT ALL 1, 2, 3 " {1 2 3}
  114. 0100.1 "SELECT a, b, a||b FROM t1 " {
  115. a one aone b two btwo c three cthree
  116. }
  117. 1100.1 "SELECT DISTINCT a, b, a||b FROM t1 " {
  118. a one aone b two btwo c three cthree
  119. }
  120. 1200.1 "SELECT ALL a, b, a||b FROM t1 " {
  121. a one aone b two btwo c three cthree
  122. }
  123. 0010.1 "SELECT 1, 2, 3 WHERE 1 " {1 2 3}
  124. 0010.2 "SELECT 1, 2, 3 WHERE 0 " {}
  125. 0010.3 "SELECT 1, 2, 3 WHERE NULL " {}
  126. 1010.1 "SELECT DISTINCT 1, 2, 3 WHERE 1 " {1 2 3}
  127. 2010.1 "SELECT ALL 1, 2, 3 WHERE 1 " {1 2 3}
  128. 0110.1 "SELECT a, b, a||b FROM t1 WHERE a!='x' " {
  129. a one aone b two btwo c three cthree
  130. }
  131. 0110.2 "SELECT a, b, a||b FROM t1 WHERE a=='x'" {}
  132. 1110.1 "SELECT DISTINCT a, b, a||b FROM t1 WHERE a!='x' " {
  133. a one aone b two btwo c three cthree
  134. }
  135. 2110.0 "SELECT ALL a, b, a||b FROM t1 WHERE a=='x'" {}
  136. 0001.1 "SELECT 1, 2, 3 GROUP BY 2" {1 2 3}
  137. 0002.1 "SELECT 1, 2, 3 GROUP BY 2 HAVING count(*)=1" {1 2 3}
  138. 0002.2 "SELECT 1, 2, 3 GROUP BY 2 HAVING count(*)>1" {}
  139. 1001.1 "SELECT DISTINCT 1, 2, 3 GROUP BY 2" {1 2 3}
  140. 1002.1 "SELECT DISTINCT 1, 2, 3 GROUP BY 2 HAVING count(*)=1" {1 2 3}
  141. 1002.2 "SELECT DISTINCT 1, 2, 3 GROUP BY 2 HAVING count(*)>1" {}
  142. 2001.1 "SELECT ALL 1, 2, 3 GROUP BY 2" {1 2 3}
  143. 2002.1 "SELECT ALL 1, 2, 3 GROUP BY 2 HAVING count(*)=1" {1 2 3}
  144. 2002.2 "SELECT ALL 1, 2, 3 GROUP BY 2 HAVING count(*)>1" {}
  145. 0101.1 "SELECT count(*), max(a) FROM t1 GROUP BY b" {1 a 1 c 1 b}
  146. 0102.1 "SELECT count(*), max(a) FROM t1 GROUP BY b HAVING count(*)=1" {
  147. 1 a 1 c 1 b
  148. }
  149. 0102.2 "SELECT count(*), max(a) FROM t1 GROUP BY b HAVING count(*)=2" { }
  150. 1101.1 "SELECT DISTINCT count(*), max(a) FROM t1 GROUP BY b" {1 a 1 c 1 b}
  151. 1102.1 "SELECT DISTINCT count(*), max(a) FROM t1
  152. GROUP BY b HAVING count(*)=1" {
  153. 1 a 1 c 1 b
  154. }
  155. 1102.2 "SELECT DISTINCT count(*), max(a) FROM t1
  156. GROUP BY b HAVING count(*)=2" {
  157. }
  158. 2101.1 "SELECT ALL count(*), max(a) FROM t1 GROUP BY b" {1 a 1 c 1 b}
  159. 2102.1 "SELECT ALL count(*), max(a) FROM t1
  160. GROUP BY b HAVING count(*)=1" {
  161. 1 a 1 c 1 b
  162. }
  163. 2102.2 "SELECT ALL count(*), max(a) FROM t1
  164. GROUP BY b HAVING count(*)=2" {
  165. }
  166. 0011.1 "SELECT 1, 2, 3 WHERE 1 GROUP BY 2" {1 2 3}
  167. 0012.1 "SELECT 1, 2, 3 WHERE 0 GROUP BY 2 HAVING count(*)=1" {}
  168. 0012.2 "SELECT 1, 2, 3 WHERE 0 GROUP BY 2 HAVING count(*)>1" {}
  169. 1011.1 "SELECT DISTINCT 1, 2, 3 WHERE 0 GROUP BY 2" {}
  170. 1012.1 "SELECT DISTINCT 1, 2, 3 WHERE 1 GROUP BY 2 HAVING count(*)=1"
  171. {1 2 3}
  172. 1012.2 "SELECT DISTINCT 1, 2, 3 WHERE NULL GROUP BY 2 HAVING count(*)>1" {}
  173. 2011.1 "SELECT ALL 1, 2, 3 WHERE 1 GROUP BY 2" {1 2 3}
  174. 2012.1 "SELECT ALL 1, 2, 3 WHERE 0 GROUP BY 2 HAVING count(*)=1" {}
  175. 2012.2 "SELECT ALL 1, 2, 3 WHERE 'abc' GROUP BY 2 HAVING count(*)>1" {}
  176. 0111.1 "SELECT count(*), max(a) FROM t1 WHERE a='a' GROUP BY b" {1 a}
  177. 0112.1 "SELECT count(*), max(a) FROM t1
  178. WHERE a='c' GROUP BY b HAVING count(*)=1" {1 c}
  179. 0112.2 "SELECT count(*), max(a) FROM t1
  180. WHERE 0 GROUP BY b HAVING count(*)=2" { }
  181. 1111.1 "SELECT DISTINCT count(*), max(a) FROM t1 WHERE a<'c' GROUP BY b"
  182. {1 a 1 b}
  183. 1112.1 "SELECT DISTINCT count(*), max(a) FROM t1 WHERE a>'a'
  184. GROUP BY b HAVING count(*)=1" {
  185. 1 c 1 b
  186. }
  187. 1112.2 "SELECT DISTINCT count(*), max(a) FROM t1 WHERE 0
  188. GROUP BY b HAVING count(*)=2" {
  189. }
  190. 2111.1 "SELECT ALL count(*), max(a) FROM t1 WHERE b>'one' GROUP BY b"
  191. {1 c 1 b}
  192. 2112.1 "SELECT ALL count(*), max(a) FROM t1 WHERE a!='b'
  193. GROUP BY b HAVING count(*)=1" {
  194. 1 a 1 c
  195. }
  196. 2112.2 "SELECT ALL count(*), max(a) FROM t1
  197. WHERE 0 GROUP BY b HAVING count(*)=2" { }
  198. }
  199. # -- syntax diagram result-column
  200. #
  201. do_select_tests e_select-0.3 {
  202. 1 "SELECT * FROM t1" {a one b two c three}
  203. 2 "SELECT t1.* FROM t1" {a one b two c three}
  204. 3 "SELECT 'x'||a||'x' FROM t1" {xax xbx xcx}
  205. 4 "SELECT 'x'||a||'x' alias FROM t1" {xax xbx xcx}
  206. 5 "SELECT 'x'||a||'x' AS alias FROM t1" {xax xbx xcx}
  207. }
  208. # -- syntax diagram join-source
  209. #
  210. # -- syntax diagram join-op
  211. #
  212. do_select_tests e_select-0.4 {
  213. 1 "SELECT t1.rowid FROM t1" {1 2 3}
  214. 2 "SELECT t1.rowid FROM t1,t2" {1 1 1 2 2 2 3 3 3}
  215. 3 "SELECT t1.rowid FROM t1,t2,t3" {1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3}
  216. 4 "SELECT t1.rowid FROM t1" {1 2 3}
  217. 5 "SELECT t1.rowid FROM t1 JOIN t2" {1 1 1 2 2 2 3 3 3}
  218. 6 "SELECT t1.rowid FROM t1 JOIN t2 JOIN t3"
  219. {1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3}
  220. 7 "SELECT t1.rowid FROM t1 NATURAL JOIN t3" {1 2}
  221. 8 "SELECT t1.rowid FROM t1 NATURAL LEFT OUTER JOIN t3" {1 2 3}
  222. 9 "SELECT t1.rowid FROM t1 NATURAL LEFT JOIN t3" {1 2 3}
  223. 10 "SELECT t1.rowid FROM t1 NATURAL INNER JOIN t3" {1 2}
  224. 11 "SELECT t1.rowid FROM t1 NATURAL CROSS JOIN t3" {1 2}
  225. 12 "SELECT t1.rowid FROM t1 JOIN t3" {1 1 2 2 3 3}
  226. 13 "SELECT t1.rowid FROM t1 LEFT OUTER JOIN t3" {1 1 2 2 3 3}
  227. 14 "SELECT t1.rowid FROM t1 LEFT JOIN t3" {1 1 2 2 3 3}
  228. 15 "SELECT t1.rowid FROM t1 INNER JOIN t3" {1 1 2 2 3 3}
  229. 16 "SELECT t1.rowid FROM t1 CROSS JOIN t3" {1 1 2 2 3 3}
  230. }
  231. # -- syntax diagram compound-operator
  232. #
  233. do_select_tests e_select-0.5 {
  234. 1 "SELECT rowid FROM t1 UNION ALL SELECT rowid+2 FROM t4" {1 2 3 3 4}
  235. 2 "SELECT rowid FROM t1 UNION SELECT rowid+2 FROM t4" {1 2 3 4}
  236. 3 "SELECT rowid FROM t1 INTERSECT SELECT rowid+2 FROM t4" {3}
  237. 4 "SELECT rowid FROM t1 EXCEPT SELECT rowid+2 FROM t4" {1 2}
  238. }
  239. # -- syntax diagram ordering-term
  240. #
  241. do_select_tests e_select-0.6 {
  242. 1 "SELECT b||a FROM t1 ORDER BY b||a" {onea threec twob}
  243. 2 "SELECT b||a FROM t1 ORDER BY (b||a) COLLATE nocase" {onea threec twob}
  244. 3 "SELECT b||a FROM t1 ORDER BY (b||a) ASC" {onea threec twob}
  245. 4 "SELECT b||a FROM t1 ORDER BY (b||a) DESC" {twob threec onea}
  246. }
  247. # -- syntax diagram select-stmt
  248. #
  249. do_select_tests e_select-0.7 {
  250. 1 "SELECT * FROM t1" {a one b two c three}
  251. 2 "SELECT * FROM t1 ORDER BY b" {a one c three b two}
  252. 3 "SELECT * FROM t1 ORDER BY b, a" {a one c three b two}
  253. 4 "SELECT * FROM t1 LIMIT 10" {a one b two c three}
  254. 5 "SELECT * FROM t1 LIMIT 10 OFFSET 5" {}
  255. 6 "SELECT * FROM t1 LIMIT 10, 5" {}
  256. 7 "SELECT * FROM t1 ORDER BY a LIMIT 10" {a one b two c three}
  257. 8 "SELECT * FROM t1 ORDER BY b LIMIT 10 OFFSET 5" {}
  258. 9 "SELECT * FROM t1 ORDER BY a,b LIMIT 10, 5" {}
  259. 10 "SELECT * FROM t1 UNION SELECT b, a FROM t1"
  260. {a one b two c three one a three c two b}
  261. 11 "SELECT * FROM t1 UNION SELECT b, a FROM t1 ORDER BY b"
  262. {one a two b three c a one c three b two}
  263. 12 "SELECT * FROM t1 UNION SELECT b, a FROM t1 ORDER BY b, a"
  264. {one a two b three c a one c three b two}
  265. 13 "SELECT * FROM t1 UNION SELECT b, a FROM t1 LIMIT 10"
  266. {a one b two c three one a three c two b}
  267. 14 "SELECT * FROM t1 UNION SELECT b, a FROM t1 LIMIT 10 OFFSET 5"
  268. {two b}
  269. 15 "SELECT * FROM t1 UNION SELECT b, a FROM t1 LIMIT 10, 5"
  270. {}
  271. 16 "SELECT * FROM t1 UNION SELECT b, a FROM t1 ORDER BY a LIMIT 10"
  272. {a one b two c three one a three c two b}
  273. 17 "SELECT * FROM t1 UNION SELECT b, a FROM t1 ORDER BY b LIMIT 10 OFFSET 5"
  274. {b two}
  275. 18 "SELECT * FROM t1 UNION SELECT b, a FROM t1 ORDER BY a,b LIMIT 10, 5"
  276. {}
  277. }
  278. #-------------------------------------------------------------------------
  279. # The following tests focus on FROM clause (join) processing.
  280. #
  281. # EVIDENCE-OF: R-16074-54196 If the FROM clause is omitted from a simple
  282. # SELECT statement, then the input data is implicitly a single row zero
  283. # columns wide
  284. #
  285. do_select_tests e_select-1.1 {
  286. 1 "SELECT 'abc'" {abc}
  287. 2 "SELECT 'abc' WHERE NULL" {}
  288. 3 "SELECT NULL" {{}}
  289. 4 "SELECT count(*)" {1}
  290. 5 "SELECT count(*) WHERE 0" {0}
  291. 6 "SELECT count(*) WHERE 1" {1}
  292. }
  293. # EVIDENCE-OF: R-48114-33255 If there is only a single table in the
  294. # join-source following the FROM clause, then the input data used by the
  295. # SELECT statement is the contents of the named table.
  296. #
  297. # The results of the SELECT queries suggest that they are operating on the
  298. # contents of the table 'xx'.
  299. #
  300. do_execsql_test e_select-1.2.0 {
  301. CREATE TABLE xx(x, y);
  302. INSERT INTO xx VALUES('IiJlsIPepMuAhU', X'10B00B897A15BAA02E3F98DCE8F2');
  303. INSERT INTO xx VALUES(NULL, -16.87);
  304. INSERT INTO xx VALUES(-17.89, 'linguistically');
  305. } {}
  306. do_select_tests e_select-1.2 {
  307. 1 "SELECT quote(x), quote(y) FROM xx" {
  308. 'IiJlsIPepMuAhU' X'10B00B897A15BAA02E3F98DCE8F2'
  309. NULL -16.87
  310. -17.89 'linguistically'
  311. }
  312. 2 "SELECT count(*), count(x), count(y) FROM xx" {3 2 3}
  313. 3 "SELECT sum(x), sum(y) FROM xx" {-17.89 -16.87}
  314. }
  315. # EVIDENCE-OF: R-23593-12456 If there is more than one table specified
  316. # as part of the join-source following the FROM keyword, then the
  317. # contents of each named table are joined into a single dataset for the
  318. # simple SELECT statement to operate on.
  319. #
  320. # There are more detailed tests for subsequent requirements that add
  321. # more detail to this idea. We just add a single test that shows that
  322. # data is coming from each of the three tables following the FROM clause
  323. # here to show that the statement, vague as it is, is not incorrect.
  324. #
  325. do_select_tests e_select-1.3 {
  326. 1 "SELECT * FROM t1, t2, t3" {
  327. a one a I a 1 a one a I b 2 a one b II a 1
  328. a one b II b 2 a one c III a 1 a one c III b 2
  329. b two a I a 1 b two a I b 2 b two b II a 1
  330. b two b II b 2 b two c III a 1 b two c III b 2
  331. c three a I a 1 c three a I b 2 c three b II a 1
  332. c three b II b 2 c three c III a 1 c three c III b 2
  333. }
  334. }
  335. #
  336. # The following block of tests - e_select-1.4.* - test that the description
  337. # of cartesian joins in the SELECT documentation is consistent with SQLite.
  338. # In doing so, we test the following three requirements as a side-effect:
  339. #
  340. # EVIDENCE-OF: R-46122-14930 If the join-op is "CROSS JOIN", "INNER
  341. # JOIN", "JOIN" or a comma (",") and there is no ON or USING clause,
  342. # then the result of the join is simply the cartesian product of the
  343. # left and right-hand datasets.
  344. #
  345. # The tests are built on this assertion. Really, they test that the output
  346. # of a CROSS JOIN, JOIN, INNER JOIN or "," join matches the expected result
  347. # of calculating the cartesian product of the left and right-hand datasets.
  348. #
  349. # EVIDENCE-OF: R-46256-57243 There is no difference between the "INNER
  350. # JOIN", "JOIN" and "," join operators.
  351. #
  352. # EVIDENCE-OF: R-25071-21202 The "CROSS JOIN" join operator produces the
  353. # same result as the "INNER JOIN", "JOIN" and "," operators
  354. #
  355. # All tests are run 4 times, with the only difference in each run being
  356. # which of the 4 equivalent cartesian product join operators are used.
  357. # Since the output data is the same in all cases, we consider that this
  358. # qualifies as testing the two statements above.
  359. #
  360. do_execsql_test e_select-1.4.0 {
  361. CREATE TABLE x1(a, b);
  362. CREATE TABLE x2(c, d, e);
  363. CREATE TABLE x3(f, g, h, i);
  364. -- x1: 3 rows, 2 columns
  365. INSERT INTO x1 VALUES(24, 'converging');
  366. INSERT INTO x1 VALUES(NULL, X'CB71');
  367. INSERT INTO x1 VALUES('blonds', 'proprietary');
  368. -- x2: 2 rows, 3 columns
  369. INSERT INTO x2 VALUES(-60.06, NULL, NULL);
  370. INSERT INTO x2 VALUES(-58, NULL, 1.21);
  371. -- x3: 5 rows, 4 columns
  372. INSERT INTO x3 VALUES(-39.24, NULL, 'encompass', -1);
  373. INSERT INTO x3 VALUES('presenting', 51, 'reformation', 'dignified');
  374. INSERT INTO x3 VALUES('conducting', -87.24, 37.56, NULL);
  375. INSERT INTO x3 VALUES('coldest', -96, 'dramatists', 82.3);
  376. INSERT INTO x3 VALUES('alerting', NULL, -93.79, NULL);
  377. } {}
  378. # EVIDENCE-OF: R-59089-25828 The columns of the cartesian product
  379. # dataset are, in order, all the columns of the left-hand dataset
  380. # followed by all the columns of the right-hand dataset.
  381. #
  382. do_join_test e_select-1.4.1.1 {
  383. SELECT * FROM x1 %JOIN% x2 LIMIT 1
  384. } [concat {24 converging} {-60.06 {} {}}]
  385. do_join_test e_select-1.4.1.2 {
  386. SELECT * FROM x2 %JOIN% x1 LIMIT 1
  387. } [concat {-60.06 {} {}} {24 converging}]
  388. do_join_test e_select-1.4.1.3 {
  389. SELECT * FROM x3 %JOIN% x2 LIMIT 1
  390. } [concat {-39.24 {} encompass -1} {-60.06 {} {}}]
  391. do_join_test e_select-1.4.1.4 {
  392. SELECT * FROM x2 %JOIN% x3 LIMIT 1
  393. } [concat {-60.06 {} {}} {-39.24 {} encompass -1}]
  394. # EVIDENCE-OF: R-44414-54710 There is a row in the cartesian product
  395. # dataset formed by combining each unique combination of a row from the
  396. # left-hand and right-hand datasets.
  397. #
  398. do_join_test e_select-1.4.2.1 {
  399. SELECT * FROM x2 %JOIN% x3 ORDER BY +c, +f
  400. } [list -60.06 {} {} -39.24 {} encompass -1 \
  401. -60.06 {} {} alerting {} -93.79 {} \
  402. -60.06 {} {} coldest -96 dramatists 82.3 \
  403. -60.06 {} {} conducting -87.24 37.56 {} \
  404. -60.06 {} {} presenting 51 reformation dignified \
  405. -58 {} 1.21 -39.24 {} encompass -1 \
  406. -58 {} 1.21 alerting {} -93.79 {} \
  407. -58 {} 1.21 coldest -96 dramatists 82.3 \
  408. -58 {} 1.21 conducting -87.24 37.56 {} \
  409. -58 {} 1.21 presenting 51 reformation dignified \
  410. ]
  411. # TODO: Come back and add a few more like the above.
  412. # EVIDENCE-OF: R-20659-43267 In other words, if the left-hand dataset
  413. # consists of Nlhs rows of Mlhs columns, and the right-hand dataset of
  414. # Nrhs rows of Mrhs columns, then the cartesian product is a dataset of
  415. # Nlhs.Nrhs rows, each containing Mlhs+Mrhs columns.
  416. #
  417. # x1, x2 (Nlhs=3, Nrhs=2) (Mlhs=2, Mrhs=3)
  418. do_join_test e_select-1.4.3.1 {
  419. SELECT count(*) FROM x1 %JOIN% x2
  420. } [expr 3*2]
  421. do_test e_select-1.4.3.2 {
  422. expr {[llength [execsql {SELECT * FROM x1, x2}]] / 6}
  423. } [expr 2+3]
  424. # x2, x3 (Nlhs=2, Nrhs=5) (Mlhs=3, Mrhs=4)
  425. do_join_test e_select-1.4.3.3 {
  426. SELECT count(*) FROM x2 %JOIN% x3
  427. } [expr 2*5]
  428. do_test e_select-1.4.3.4 {
  429. expr {[llength [execsql {SELECT * FROM x2 JOIN x3}]] / 10}
  430. } [expr 3+4]
  431. # x3, x1 (Nlhs=5, Nrhs=3) (Mlhs=4, Mrhs=2)
  432. do_join_test e_select-1.4.3.5 {
  433. SELECT count(*) FROM x3 %JOIN% x1
  434. } [expr 5*3]
  435. do_test e_select-1.4.3.6 {
  436. expr {[llength [execsql {SELECT * FROM x3 CROSS JOIN x1}]] / 15}
  437. } [expr 4+2]
  438. # x3, x3 (Nlhs=5, Nrhs=5) (Mlhs=4, Mrhs=4)
  439. do_join_test e_select-1.4.3.7 {
  440. SELECT count(*) FROM x3 %JOIN% x3
  441. } [expr 5*5]
  442. do_test e_select-1.4.3.8 {
  443. expr {[llength [execsql {SELECT * FROM x3 INNER JOIN x3 AS x4}]] / 25}
  444. } [expr 4+4]
  445. # Some extra cartesian product tests using tables t1 and t2.
  446. #
  447. do_execsql_test e_select-1.4.4.1 { SELECT * FROM t1, t2 } $t1_cross_t2
  448. do_execsql_test e_select-1.4.4.2 { SELECT * FROM t1 AS x, t1 AS y} $t1_cross_t1
  449. do_select_tests e_select-1.4.5 [list \
  450. 1 { SELECT * FROM t1 CROSS JOIN t2 } $t1_cross_t2 \
  451. 2 { SELECT * FROM t1 AS y CROSS JOIN t1 AS x } $t1_cross_t1 \
  452. 3 { SELECT * FROM t1 INNER JOIN t2 } $t1_cross_t2 \
  453. 4 { SELECT * FROM t1 AS y INNER JOIN t1 AS x } $t1_cross_t1 \
  454. ]
  455. # EVIDENCE-OF: R-22775-56496 If there is an ON clause specified, then
  456. # the ON expression is evaluated for each row of the cartesian product
  457. # as a boolean expression. All rows for which the expression evaluates
  458. # to false are excluded from the dataset.
  459. #
  460. foreach {tn select res} [list \
  461. 1 { SELECT * FROM t1 %JOIN% t2 ON (1) } $t1_cross_t2 \
  462. 2 { SELECT * FROM t1 %JOIN% t2 ON (0) } [list] \
  463. 3 { SELECT * FROM t1 %JOIN% t2 ON (NULL) } [list] \
  464. 4 { SELECT * FROM t1 %JOIN% t2 ON ('abc') } [list] \
  465. 5 { SELECT * FROM t1 %JOIN% t2 ON ('1ab') } $t1_cross_t2 \
  466. 6 { SELECT * FROM t1 %JOIN% t2 ON (0.9) } $t1_cross_t2 \
  467. 7 { SELECT * FROM t1 %JOIN% t2 ON ('0.9') } $t1_cross_t2 \
  468. 8 { SELECT * FROM t1 %JOIN% t2 ON (0.0) } [list] \
  469. \
  470. 9 { SELECT t1.b, t2.b FROM t1 %JOIN% t2 ON (t1.a = t2.a) } \
  471. {one I two II three III} \
  472. 10 { SELECT t1.b, t2.b FROM t1 %JOIN% t2 ON (t1.a = 'a') } \
  473. {one I one II one III} \
  474. 11 { SELECT t1.b, t2.b
  475. FROM t1 %JOIN% t2 ON (CASE WHEN t1.a = 'a' THEN NULL ELSE 1 END) } \
  476. {two I two II two III three I three II three III} \
  477. ] {
  478. do_join_test e_select-1.3.$tn $select $res
  479. }
  480. # EVIDENCE-OF: R-63358-54862 If there is a USING clause specified as
  481. # part of the join-constraint, then each of the column names specified
  482. # must exist in the datasets to both the left and right of the join-op.
  483. #
  484. do_select_tests e_select-1.4 -error {
  485. cannot join using column %s - column not present in both tables
  486. } {
  487. 1 { SELECT * FROM t1, t3 USING (b) } "b"
  488. 2 { SELECT * FROM t3, t1 USING (c) } "c"
  489. 3 { SELECT * FROM t3, (SELECT a AS b, b AS c FROM t1) USING (a) } "a"
  490. }
  491. # EVIDENCE-OF: R-55987-04584 For each pair of namesake columns, the
  492. # expression "lhs.X = rhs.X" is evaluated for each row of the cartesian
  493. # product as a boolean expression. All rows for which one or more of the
  494. # expressions evaluates to false are excluded from the result set.
  495. #
  496. do_select_tests e_select-1.5 {
  497. 1 { SELECT * FROM t1, t3 USING (a) } {a one 1 b two 2}
  498. 2 { SELECT * FROM t3, t4 USING (a,c) } {b 2}
  499. }
  500. # EVIDENCE-OF: R-54046-48600 When comparing values as a result of a
  501. # USING clause, the normal rules for handling affinities, collation
  502. # sequences and NULL values in comparisons apply.
  503. #
  504. # EVIDENCE-OF: R-35466-18578 The column from the dataset on the
  505. # left-hand side of the join operator is considered to be on the
  506. # left-hand side of the comparison operator (=) for the purposes of
  507. # collation sequence and affinity precedence.
  508. #
  509. do_execsql_test e_select-1.6.0 {
  510. CREATE TABLE t5(a COLLATE nocase, b COLLATE binary);
  511. INSERT INTO t5 VALUES('AA', 'cc');
  512. INSERT INTO t5 VALUES('BB', 'dd');
  513. INSERT INTO t5 VALUES(NULL, NULL);
  514. CREATE TABLE t6(a COLLATE binary, b COLLATE nocase);
  515. INSERT INTO t6 VALUES('aa', 'cc');
  516. INSERT INTO t6 VALUES('bb', 'DD');
  517. INSERT INTO t6 VALUES(NULL, NULL);
  518. } {}
  519. foreach {tn select res} {
  520. 1 { SELECT * FROM t5 %JOIN% t6 USING (a) } {AA cc cc BB dd DD}
  521. 2 { SELECT * FROM t6 %JOIN% t5 USING (a) } {}
  522. 3 { SELECT * FROM (SELECT a COLLATE nocase, b FROM t6) %JOIN% t5 USING (a) }
  523. {aa cc cc bb DD dd}
  524. 4 { SELECT * FROM t5 %JOIN% t6 USING (a,b) } {AA cc}
  525. 5 { SELECT * FROM t6 %JOIN% t5 USING (a,b) } {}
  526. } {
  527. do_join_test e_select-1.6.$tn $select $res
  528. }
  529. # EVIDENCE-OF: R-57047-10461 For each pair of columns identified by a
  530. # USING clause, the column from the right-hand dataset is omitted from
  531. # the joined dataset.
  532. #
  533. # EVIDENCE-OF: R-56132-15700 This is the only difference between a USING
  534. # clause and its equivalent ON constraint.
  535. #
  536. foreach {tn select res} {
  537. 1a { SELECT * FROM t1 %JOIN% t2 USING (a) }
  538. {a one I b two II c three III}
  539. 1b { SELECT * FROM t1 %JOIN% t2 ON (t1.a=t2.a) }
  540. {a one a I b two b II c three c III}
  541. 2a { SELECT * FROM t3 %JOIN% t4 USING (a) }
  542. {a 1 {} b 2 2}
  543. 2b { SELECT * FROM t3 %JOIN% t4 ON (t3.a=t4.a) }
  544. {a 1 a {} b 2 b 2}
  545. 3a { SELECT * FROM t3 %JOIN% t4 USING (a,c) } {b 2}
  546. 3b { SELECT * FROM t3 %JOIN% t4 ON (t3.a=t4.a AND t3.c=t4.c) } {b 2 b 2}
  547. 4a { SELECT * FROM (SELECT a COLLATE nocase, b FROM t6) AS x
  548. %JOIN% t5 USING (a) }
  549. {aa cc cc bb DD dd}
  550. 4b { SELECT * FROM (SELECT a COLLATE nocase, b FROM t6) AS x
  551. %JOIN% t5 ON (x.a=t5.a) }
  552. {aa cc AA cc bb DD BB dd}
  553. } {
  554. do_join_test e_select-1.7.$tn $select $res
  555. }
  556. # EVIDENCE-OF: R-41434-12448 If the join-op is a "LEFT JOIN" or "LEFT
  557. # OUTER JOIN", then after the ON or USING filtering clauses have been
  558. # applied, an extra row is added to the output for each row in the
  559. # original left-hand input dataset that corresponds to no rows at all in
  560. # the composite dataset (if any).
  561. #
  562. do_execsql_test e_select-1.8.0 {
  563. CREATE TABLE t7(a, b, c);
  564. CREATE TABLE t8(a, d, e);
  565. INSERT INTO t7 VALUES('x', 'ex', 24);
  566. INSERT INTO t7 VALUES('y', 'why', 25);
  567. INSERT INTO t8 VALUES('x', 'abc', 24);
  568. INSERT INTO t8 VALUES('z', 'ghi', 26);
  569. } {}
  570. do_select_tests e_select-1.8 {
  571. 1a "SELECT count(*) FROM t7 JOIN t8 ON (t7.a=t8.a)" {1}
  572. 1b "SELECT count(*) FROM t7 LEFT JOIN t8 ON (t7.a=t8.a)" {2}
  573. 2a "SELECT count(*) FROM t7 JOIN t8 USING (a)" {1}
  574. 2b "SELECT count(*) FROM t7 LEFT JOIN t8 USING (a)" {2}
  575. }
  576. # EVIDENCE-OF: R-15607-52988 The added rows contain NULL values in the
  577. # columns that would normally contain values copied from the right-hand
  578. # input dataset.
  579. #
  580. do_select_tests e_select-1.9 {
  581. 1a "SELECT * FROM t7 JOIN t8 ON (t7.a=t8.a)" {x ex 24 x abc 24}
  582. 1b "SELECT * FROM t7 LEFT JOIN t8 ON (t7.a=t8.a)"
  583. {x ex 24 x abc 24 y why 25 {} {} {}}
  584. 2a "SELECT * FROM t7 JOIN t8 USING (a)" {x ex 24 abc 24}
  585. 2b "SELECT * FROM t7 LEFT JOIN t8 USING (a)" {x ex 24 abc 24 y why 25 {} {}}
  586. }
  587. # EVIDENCE-OF: R-01809-52134 If the NATURAL keyword is added to any of
  588. # the join-ops, then an implicit USING clause is added to the
  589. # join-constraints. The implicit USING clause contains each of the
  590. # column names that appear in both the left and right-hand input
  591. # datasets.
  592. #
  593. do_select_tests e_select-1-10 {
  594. 1a "SELECT * FROM t7 JOIN t8 USING (a)" {x ex 24 abc 24}
  595. 1b "SELECT * FROM t7 NATURAL JOIN t8" {x ex 24 abc 24}
  596. 2a "SELECT * FROM t8 JOIN t7 USING (a)" {x abc 24 ex 24}
  597. 2b "SELECT * FROM t8 NATURAL JOIN t7" {x abc 24 ex 24}
  598. 3a "SELECT * FROM t7 LEFT JOIN t8 USING (a)" {x ex 24 abc 24 y why 25 {} {}}
  599. 3b "SELECT * FROM t7 NATURAL LEFT JOIN t8" {x ex 24 abc 24 y why 25 {} {}}
  600. 4a "SELECT * FROM t8 LEFT JOIN t7 USING (a)" {x abc 24 ex 24 z ghi 26 {} {}}
  601. 4b "SELECT * FROM t8 NATURAL LEFT JOIN t7" {x abc 24 ex 24 z ghi 26 {} {}}
  602. 5a "SELECT * FROM t3 JOIN t4 USING (a,c)" {b 2}
  603. 5b "SELECT * FROM t3 NATURAL JOIN t4" {b 2}
  604. 6a "SELECT * FROM t3 LEFT JOIN t4 USING (a,c)" {a 1 b 2}
  605. 6b "SELECT * FROM t3 NATURAL LEFT JOIN t4" {a 1 b 2}
  606. }
  607. # EVIDENCE-OF: R-49566-01570 If the left and right-hand input datasets
  608. # feature no common column names, then the NATURAL keyword has no effect
  609. # on the results of the join.
  610. #
  611. do_execsql_test e_select-1.11.0 {
  612. CREATE TABLE t10(x, y);
  613. INSERT INTO t10 VALUES(1, 'true');
  614. INSERT INTO t10 VALUES(0, 'false');
  615. } {}
  616. do_select_tests e_select-1-11 {
  617. 1a "SELECT a, x FROM t1 CROSS JOIN t10" {a 1 a 0 b 1 b 0 c 1 c 0}
  618. 1b "SELECT a, x FROM t1 NATURAL CROSS JOIN t10" {a 1 a 0 b 1 b 0 c 1 c 0}
  619. }
  620. # EVIDENCE-OF: R-39625-59133 A USING or ON clause may not be added to a
  621. # join that specifies the NATURAL keyword.
  622. #
  623. foreach {tn sql} {
  624. 1 {SELECT * FROM t1 NATURAL LEFT JOIN t2 USING (a)}
  625. 2 {SELECT * FROM t1 NATURAL LEFT JOIN t2 ON (t1.a=t2.a)}
  626. 3 {SELECT * FROM t1 NATURAL LEFT JOIN t2 ON (45)}
  627. } {
  628. do_catchsql_test e_select-1.12.$tn "
  629. $sql
  630. " {1 {a NATURAL join may not have an ON or USING clause}}
  631. }
  632. #-------------------------------------------------------------------------
  633. # The next block of tests - e_select-3.* - concentrate on verifying
  634. # statements made regarding WHERE clause processing.
  635. #
  636. drop_all_tables
  637. do_execsql_test e_select-3.0 {
  638. CREATE TABLE x1(k, x, y, z);
  639. INSERT INTO x1 VALUES(1, 'relinquished', 'aphasia', 78.43);
  640. INSERT INTO x1 VALUES(2, X'A8E8D66F', X'07CF', -81);
  641. INSERT INTO x1 VALUES(3, -22, -27.57, NULL);
  642. INSERT INTO x1 VALUES(4, NULL, 'bygone', 'picky');
  643. INSERT INTO x1 VALUES(5, NULL, 96.28, NULL);
  644. INSERT INTO x1 VALUES(6, 0, 1, 2);
  645. CREATE TABLE x2(k, x, y2);
  646. INSERT INTO x2 VALUES(1, 50, X'B82838');
  647. INSERT INTO x2 VALUES(5, 84.79, 65.88);
  648. INSERT INTO x2 VALUES(3, -22, X'0E1BE452A393');
  649. INSERT INTO x2 VALUES(7, 'mistrusted', 'standardized');
  650. } {}
  651. # EVIDENCE-OF: R-06999-14330 If a WHERE clause is specified, the WHERE
  652. # expression is evaluated for each row in the input data as a boolean
  653. # expression. All rows for which the WHERE clause expression evaluates
  654. # to false are excluded from the dataset before continuing.
  655. #
  656. do_execsql_test e_select-3.1.1 { SELECT k FROM x1 WHERE x } {3}
  657. do_execsql_test e_select-3.1.2 { SELECT k FROM x1 WHERE y } {3 5 6}
  658. do_execsql_test e_select-3.1.3 { SELECT k FROM x1 WHERE z } {1 2 6}
  659. do_execsql_test e_select-3.1.4 { SELECT k FROM x1 WHERE '1'||z } {1 2 4 6}
  660. do_execsql_test e_select-3.1.5 { SELECT k FROM x1 WHERE x IS NULL } {4 5}
  661. do_execsql_test e_select-3.1.6 { SELECT k FROM x1 WHERE z - 78.43 } {2 4 6}
  662. do_execsql_test e_select-3.2.1a {
  663. SELECT k FROM x1 LEFT JOIN x2 USING(k)
  664. } {1 2 3 4 5 6}
  665. do_execsql_test e_select-3.2.1b {
  666. SELECT k FROM x1 LEFT JOIN x2 USING(k) WHERE x2.k
  667. } {1 3 5}
  668. do_execsql_test e_select-3.2.2 {
  669. SELECT k FROM x1 LEFT JOIN x2 USING(k) WHERE x2.k IS NULL
  670. } {2 4 6}
  671. do_execsql_test e_select-3.2.3 {
  672. SELECT k FROM x1 NATURAL JOIN x2 WHERE x2.k
  673. } {3}
  674. do_execsql_test e_select-3.2.4 {
  675. SELECT k FROM x1 NATURAL JOIN x2 WHERE x2.k-3
  676. } {}
  677. #-------------------------------------------------------------------------
  678. # Tests below this point are focused on verifying the testable statements
  679. # related to caculating the result rows of a simple SELECT statement.
  680. #
  681. drop_all_tables
  682. do_execsql_test e_select-4.0 {
  683. CREATE TABLE z1(a, b, c);
  684. CREATE TABLE z2(d, e);
  685. CREATE TABLE z3(a, b);
  686. INSERT INTO z1 VALUES(51.65, -59.58, 'belfries');
  687. INSERT INTO z1 VALUES(-5, NULL, 75);
  688. INSERT INTO z1 VALUES(-2.2, -23.18, 'suiters');
  689. INSERT INTO z1 VALUES(NULL, 67, 'quartets');
  690. INSERT INTO z1 VALUES(-1.04, -32.3, 'aspen');
  691. INSERT INTO z1 VALUES(63, 'born', -26);
  692. INSERT INTO z2 VALUES(NULL, 21);
  693. INSERT INTO z2 VALUES(36, 6);
  694. INSERT INTO z3 VALUES('subsistence', 'gauze');
  695. INSERT INTO z3 VALUES(49.17, -67);
  696. } {}
  697. # EVIDENCE-OF: R-36327-17224 If a result expression is the special
  698. # expression "*" then all columns in the input data are substituted for
  699. # that one expression.
  700. #
  701. # EVIDENCE-OF: R-43693-30522 If the expression is the alias of a table
  702. # or subquery in the FROM clause followed by ".*" then all columns from
  703. # the named table or subquery are substituted for the single expression.
  704. #
  705. do_select_tests e_select-4.1 {
  706. 1 "SELECT * FROM z1 LIMIT 1" {51.65 -59.58 belfries}
  707. 2 "SELECT * FROM z1,z2 LIMIT 1" {51.65 -59.58 belfries {} 21}
  708. 3 "SELECT z1.* FROM z1,z2 LIMIT 1" {51.65 -59.58 belfries}
  709. 4 "SELECT z2.* FROM z1,z2 LIMIT 1" {{} 21}
  710. 5 "SELECT z2.*, z1.* FROM z1,z2 LIMIT 1" {{} 21 51.65 -59.58 belfries}
  711. 6 "SELECT count(*), * FROM z1" {6 63 born -26}
  712. 7 "SELECT max(a), * FROM z1" {63 63 born -26}
  713. 8 "SELECT *, min(a) FROM z1" {-5 {} 75 -5}
  714. 9 "SELECT *,* FROM z1,z2 LIMIT 1" {
  715. 51.65 -59.58 belfries {} 21 51.65 -59.58 belfries {} 21
  716. }
  717. 10 "SELECT z1.*,z1.* FROM z2,z1 LIMIT 1" {
  718. 51.65 -59.58 belfries 51.65 -59.58 belfries
  719. }
  720. }
  721. # EVIDENCE-OF: R-61869-22578 It is an error to use a "*" or "alias.*"
  722. # expression in any context other than than a result expression list.
  723. #
  724. # EVIDENCE-OF: R-44324-41166 It is also an error to use a "*" or
  725. # "alias.*" expression in a simple SELECT query that does not have a
  726. # FROM clause.
  727. #
  728. foreach {tn select err} {
  729. 1.1 "SELECT a, b, c FROM z1 WHERE *" {near "*": syntax error}
  730. 1.2 "SELECT a, b, c FROM z1 GROUP BY *" {near "*": syntax error}
  731. 1.3 "SELECT 1 + * FROM z1" {near "*": syntax error}
  732. 1.4 "SELECT * + 1 FROM z1" {near "+": syntax error}
  733. 2.1 "SELECT *" {no tables specified}
  734. 2.2 "SELECT * WHERE 1" {no tables specified}
  735. 2.3 "SELECT * WHERE 0" {no tables specified}
  736. 2.4 "SELECT count(*), *" {no tables specified}
  737. } {
  738. do_catchsql_test e_select-4.2.$tn $select [list 1 $err]
  739. }
  740. # EVIDENCE-OF: R-08669-22397 The number of columns in the rows returned
  741. # by a simple SELECT statement is equal to the number of expressions in
  742. # the result expression list after substitution of * and alias.*
  743. # expressions.
  744. #
  745. foreach {tn select nCol} {
  746. 1 "SELECT * FROM z1" 3
  747. 2 "SELECT * FROM z1 NATURAL JOIN z3" 3
  748. 3 "SELECT z1.* FROM z1 NATURAL JOIN z3" 3
  749. 4 "SELECT z3.* FROM z1 NATURAL JOIN z3" 2
  750. 5 "SELECT z1.*, z3.* FROM z1 NATURAL JOIN z3" 5
  751. 6 "SELECT 1, 2, z1.* FROM z1" 5
  752. 7 "SELECT a, *, b, c FROM z1" 6
  753. } {
  754. set ::stmt [sqlite3_prepare_v2 db $select -1 DUMMY]
  755. do_test e_select-4.3.$tn { sqlite3_column_count $::stmt } $nCol
  756. sqlite3_finalize $::stmt
  757. }
  758. # In lang_select.html, a non-aggregate query is defined as any simple SELECT
  759. # that has no GROUP BY clause and no aggregate expressions in the result
  760. # expression list. Other queries are aggregate queries. Test cases
  761. # e_select-4.4.* through e_select-4.12.*, inclusive, which test the part of
  762. # simple SELECT that is different for aggregate and non-aggregate queries
  763. # verify (in a way) that these definitions are consistent:
  764. #
  765. # EVIDENCE-OF: R-20637-43463 A simple SELECT statement is an aggregate
  766. # query if it contains either a GROUP BY clause or one or more aggregate
  767. # functions in the result-set.
  768. #
  769. # EVIDENCE-OF: R-23155-55597 Otherwise, if a simple SELECT contains no
  770. # aggregate functions or a GROUP BY clause, it is a non-aggregate query.
  771. #
  772. # EVIDENCE-OF: R-44050-47362 If the SELECT statement is a non-aggregate
  773. # query, then each expression in the result expression list is evaluated
  774. # for each row in the dataset filtered by the WHERE clause.
  775. #
  776. do_select_tests e_select-4.4 {
  777. 1 "SELECT a, b FROM z1"
  778. {51.65 -59.58 -5 {} -2.2 -23.18 {} 67 -1.04 -32.3 63 born}
  779. 2 "SELECT a IS NULL, b+1, * FROM z1" {
  780. 0 -58.58 51.65 -59.58 belfries
  781. 0 {} -5 {} 75
  782. 0 -22.18 -2.2 -23.18 suiters
  783. 1 68 {} 67 quartets
  784. 0 -31.3 -1.04 -32.3 aspen
  785. 0 1 63 born -26
  786. }
  787. 3 "SELECT 32*32, d||e FROM z2" {1024 {} 1024 366}
  788. }
  789. # Test cases e_select-4.5.* and e_select-4.6.* together show that:
  790. #
  791. # EVIDENCE-OF: R-51988-01124 The single row of result-set data created
  792. # by evaluating the aggregate and non-aggregate expressions in the
  793. # result-set forms the result of an aggregate query without a GROUP BY
  794. # clause.
  795. #
  796. # EVIDENCE-OF: R-57629-25253 If the SELECT statement is an aggregate
  797. # query without a GROUP BY clause, then each aggregate expression in the
  798. # result-set is evaluated once across the entire dataset.
  799. #
  800. do_select_tests e_select-4.5 {
  801. 1 "SELECT count(a), max(a), count(b), max(b) FROM z1" {5 63 5 born}
  802. 2 "SELECT count(*), max(1)" {1 1}
  803. 3 "SELECT sum(b+1) FROM z1 NATURAL LEFT JOIN z3" {-43.06}
  804. 4 "SELECT sum(b+2) FROM z1 NATURAL LEFT JOIN z3" {-38.06}
  805. 5 "SELECT sum(b IS NOT NULL) FROM z1 NATURAL LEFT JOIN z3" {5}
  806. }
  807. # EVIDENCE-OF: R-26684-40576 Each non-aggregate expression in the
  808. # result-set is evaluated once for an arbitrarily selected row of the
  809. # dataset.
  810. #
  811. # EVIDENCE-OF: R-27994-60376 The same arbitrarily selected row is used
  812. # for each non-aggregate expression.
  813. #
  814. # Note: The results of many of the queries in this block of tests are
  815. # technically undefined, as the documentation does not specify which row
  816. # SQLite will arbitrarily select to use for the evaluation of the
  817. # non-aggregate expressions.
  818. #
  819. drop_all_tables
  820. do_execsql_test e_select-4.6.0 {
  821. CREATE TABLE a1(one PRIMARY KEY, two);
  822. INSERT INTO a1 VALUES(1, 1);
  823. INSERT INTO a1 VALUES(2, 3);
  824. INSERT INTO a1 VALUES(3, 6);
  825. INSERT INTO a1 VALUES(4, 10);
  826. CREATE TABLE a2(one PRIMARY KEY, three);
  827. INSERT INTO a2 VALUES(1, 1);
  828. INSERT INTO a2 VALUES(3, 2);
  829. INSERT INTO a2 VALUES(6, 3);
  830. INSERT INTO a2 VALUES(10, 4);
  831. } {}
  832. do_select_tests e_select-4.6 {
  833. 1 "SELECT one, two, count(*) FROM a1" {4 10 4}
  834. 2 "SELECT one, two, count(*) FROM a1 WHERE one<3" {2 3 2}
  835. 3 "SELECT one, two, count(*) FROM a1 WHERE one>3" {4 10 1}
  836. 4 "SELECT *, count(*) FROM a1 JOIN a2" {4 10 10 4 16}
  837. 5 "SELECT *, sum(three) FROM a1 NATURAL JOIN a2" {3 6 2 3}
  838. 6 "SELECT *, sum(three) FROM a1 NATURAL JOIN a2" {3 6 2 3}
  839. 7 "SELECT group_concat(three, ''), a1.* FROM a1 NATURAL JOIN a2" {12 3 6}
  840. }
  841. # EVIDENCE-OF: R-04486-07266 Or, if the dataset contains zero rows, then
  842. # each non-aggregate expression is evaluated against a row consisting
  843. # entirely of NULL values.
  844. #
  845. do_select_tests e_select-4.7 {
  846. 1 "SELECT one, two, count(*) FROM a1 WHERE 0" {{} {} 0}
  847. 2 "SELECT sum(two), * FROM a1, a2 WHERE three>5" {{} {} {} {} {}}
  848. 3 "SELECT max(one) IS NULL, one IS NULL, two IS NULL FROM a1 WHERE two=7" {
  849. 1 1 1
  850. }
  851. }
  852. # EVIDENCE-OF: R-64138-28774 An aggregate query without a GROUP BY
  853. # clause always returns exactly one row of data, even if there are zero
  854. # rows of input data.
  855. #
  856. foreach {tn select} {
  857. 8.1 "SELECT count(*) FROM a1"
  858. 8.2 "SELECT count(*) FROM a1 WHERE 0"
  859. 8.3 "SELECT count(*) FROM a1 WHERE 1"
  860. 8.4 "SELECT max(a1.one)+min(two), a1.one, two, * FROM a1, a2 WHERE 1"
  861. 8.5 "SELECT max(a1.one)+min(two), a1.one, two, * FROM a1, a2 WHERE 0"
  862. } {
  863. # Set $nRow to the number of rows returned by $select:
  864. set ::stmt [sqlite3_prepare_v2 db $select -1 DUMMY]
  865. set nRow 0
  866. while {"SQLITE_ROW" == [sqlite3_step $::stmt]} { incr nRow }
  867. set rc [sqlite3_finalize $::stmt]
  868. # Test that $nRow==1 and that statement execution was successful
  869. # (rc==SQLITE_OK).
  870. do_test e_select-4.$tn [list list $rc $nRow] {SQLITE_OK 1}
  871. }
  872. drop_all_tables
  873. do_execsql_test e_select-4.9.0 {
  874. CREATE TABLE b1(one PRIMARY KEY, two);
  875. INSERT INTO b1 VALUES(1, 'o');
  876. INSERT INTO b1 VALUES(4, 'f');
  877. INSERT INTO b1 VALUES(3, 't');
  878. INSERT INTO b1 VALUES(2, 't');
  879. INSERT INTO b1 VALUES(5, 'f');
  880. INSERT INTO b1 VALUES(7, 's');
  881. INSERT INTO b1 VALUES(6, 's');
  882. CREATE TABLE b2(x, y);
  883. INSERT INTO b2 VALUES(NULL, 0);
  884. INSERT INTO b2 VALUES(NULL, 1);
  885. INSERT INTO b2 VALUES('xyz', 2);
  886. INSERT INTO b2 VALUES('abc', 3);
  887. INSERT INTO b2 VALUES('xyz', 4);
  888. CREATE TABLE b3(a COLLATE nocase, b COLLATE binary);
  889. INSERT INTO b3 VALUES('abc', 'abc');
  890. INSERT INTO b3 VALUES('aBC', 'aBC');
  891. INSERT INTO b3 VALUES('Def', 'Def');
  892. INSERT INTO b3 VALUES('dEF', 'dEF');
  893. } {}
  894. # EVIDENCE-OF: R-57754-57109 If the SELECT statement is an aggregate
  895. # query with a GROUP BY clause, then each of the expressions specified
  896. # as part of the GROUP BY clause is evaluated for each row of the
  897. # dataset. Each row is then assigned to a "group" based on the results;
  898. # rows for which the results of evaluating the GROUP BY expressions are
  899. # the same are assigned to the same group.
  900. #
  901. # These tests also show that the following is not untrue:
  902. #
  903. # EVIDENCE-OF: R-25883-55063 The expressions in the GROUP BY clause do
  904. # not have to be expressions that appear in the result.
  905. #
  906. do_select_tests e_select-4.9 {
  907. 1 "SELECT group_concat(one), two FROM b1 GROUP BY two" {
  908. /#,# f 1 o #,# s #,# t/
  909. }
  910. 2 "SELECT group_concat(one), sum(one) FROM b1 GROUP BY (one>4)" {
  911. 1,2,3,4 10 5,6,7 18
  912. }
  913. 3 "SELECT group_concat(one) FROM b1 GROUP BY (two>'o'), one%2" {
  914. 4 1,5 2,6 3,7
  915. }
  916. 4 "SELECT group_concat(one) FROM b1 GROUP BY (one==2 OR two=='o')" {
  917. 4,3,5,7,6 1,2
  918. }
  919. }
  920. # EVIDENCE-OF: R-14926-50129 For the purposes of grouping rows, NULL
  921. # values are considered equal.
  922. #
  923. do_select_tests e_select-4.10 {
  924. 1 "SELECT group_concat(y) FROM b2 GROUP BY x" {/#,# 3 #,#/}
  925. 2 "SELECT count(*) FROM b2 GROUP BY CASE WHEN y<4 THEN NULL ELSE 0 END" {4 1}
  926. }
  927. # EVIDENCE-OF: R-10470-30318 The usual rules for selecting a collation
  928. # sequence with which to compare text values apply when evaluating
  929. # expressions in a GROUP BY clause.
  930. #
  931. do_select_tests e_select-4.11 {
  932. 1 "SELECT count(*) FROM b3 GROUP BY b" {1 1 1 1}
  933. 2 "SELECT count(*) FROM b3 GROUP BY a" {2 2}
  934. 3 "SELECT count(*) FROM b3 GROUP BY +b" {1 1 1 1}
  935. 4 "SELECT count(*) FROM b3 GROUP BY +a" {2 2}
  936. 5 "SELECT count(*) FROM b3 GROUP BY b||''" {1 1 1 1}
  937. 6 "SELECT count(*) FROM b3 GROUP BY a||''" {1 1 1 1}
  938. }
  939. # EVIDENCE-OF: R-63573-50730 The expressions in a GROUP BY clause may
  940. # not be aggregate expressions.
  941. #
  942. foreach {tn select} {
  943. 12.1 "SELECT * FROM b3 GROUP BY count(*)"
  944. 12.2 "SELECT max(a) FROM b3 GROUP BY max(b)"
  945. 12.3 "SELECT group_concat(a) FROM b3 GROUP BY a, max(b)"
  946. } {
  947. set res {1 {aggregate functions are not allowed in the GROUP BY clause}}
  948. do_catchsql_test e_select-4.$tn $select $res
  949. }
  950. # EVIDENCE-OF: R-31537-00101 If a HAVING clause is specified, it is
  951. # evaluated once for each group of rows as a boolean expression. If the
  952. # result of evaluating the HAVING clause is false, the group is
  953. # discarded.
  954. #
  955. # This requirement is tested by all e_select-4.13.* tests.
  956. #
  957. # EVIDENCE-OF: R-04132-09474 If the HAVING clause is an aggregate
  958. # expression, it is evaluated across all rows in the group.
  959. #
  960. # Tested by e_select-4.13.1.*
  961. #
  962. # EVIDENCE-OF: R-28262-47447 If a HAVING clause is a non-aggregate
  963. # expression, it is evaluated with respect to an arbitrarily selected
  964. # row from the group.
  965. #
  966. # Tested by e_select-4.13.2.*
  967. #
  968. # Tests in this block also show that this is not untrue:
  969. #
  970. # EVIDENCE-OF: R-55403-13450 The HAVING expression may refer to values,
  971. # even aggregate functions, that are not in the result.
  972. #
  973. do_execsql_test e_select-4.13.0 {
  974. CREATE TABLE c1(up, down);
  975. INSERT INTO c1 VALUES('x', 1);
  976. INSERT INTO c1 VALUES('x', 2);
  977. INSERT INTO c1 VALUES('x', 4);
  978. INSERT INTO c1 VALUES('x', 8);
  979. INSERT INTO c1 VALUES('y', 16);
  980. INSERT INTO c1 VALUES('y', 32);
  981. CREATE TABLE c2(i, j);
  982. INSERT INTO c2 VALUES(1, 0);
  983. INSERT INTO c2 VALUES(2, 1);
  984. INSERT INTO c2 VALUES(3, 3);
  985. INSERT INTO c2 VALUES(4, 6);
  986. INSERT INTO c2 VALUES(5, 10);
  987. INSERT INTO c2 VALUES(6, 15);
  988. INSERT INTO c2 VALUES(7, 21);
  989. INSERT INTO c2 VALUES(8, 28);
  990. INSERT INTO c2 VALUES(9, 36);
  991. CREATE TABLE c3(i PRIMARY KEY, k TEXT);
  992. INSERT INTO c3 VALUES(1, 'hydrogen');
  993. INSERT INTO c3 VALUES(2, 'helium');
  994. INSERT INTO c3 VALUES(3, 'lithium');
  995. INSERT INTO c3 VALUES(4, 'beryllium');
  996. INSERT INTO c3 VALUES(5, 'boron');
  997. INSERT INTO c3 VALUES(94, 'plutonium');
  998. } {}
  999. do_select_tests e_select-4.13 {
  1000. 1.1 "SELECT up FROM c1 GROUP BY up HAVING count(*)>3" {x}
  1001. 1.2 "SELECT up FROM c1 GROUP BY up HAVING sum(down)>16" {y}
  1002. 1.3 "SELECT up FROM c1 GROUP BY up HAVING sum(down)<16" {x}
  1003. 1.4 "SELECT up||down FROM c1 GROUP BY (down<5) HAVING max(down)<10" {x4}
  1004. 2.1 "SELECT up FROM c1 GROUP BY up HAVING down>10" {y}
  1005. 2.2 "SELECT up FROM c1 GROUP BY up HAVING up='y'" {y}
  1006. 2.3 "SELECT i, j FROM c2 GROUP BY i>4 HAVING i>6" {9 36}
  1007. }
  1008. # EVIDENCE-OF: R-23927-54081 Each expression in the result-set is then
  1009. # evaluated once for each group of rows.
  1010. #
  1011. # EVIDENCE-OF: R-53735-47017 If the expression is an aggregate
  1012. # expression, it is evaluated across all rows in the group.
  1013. #
  1014. do_select_tests e_select-4.15 {
  1015. 1 "SELECT sum(down) FROM c1 GROUP BY up" {15 48}
  1016. 2 "SELECT sum(j), max(j) FROM c2 GROUP BY (i%3)" {54 36 27 21 39 28}
  1017. 3 "SELECT sum(j), max(j) FROM c2 GROUP BY (j%2)" {80 36 40 21}
  1018. 4 "SELECT 1+sum(j), max(j)+1 FROM c2 GROUP BY (j%2)" {81 37 41 22}
  1019. 5 "SELECT count(*), round(avg(i),2) FROM c1, c2 ON (i=down) GROUP BY j%2"
  1020. {3 4.33 1 2.0}
  1021. }
  1022. # EVIDENCE-OF: R-62913-19830 Otherwise, it is evaluated against a single
  1023. # arbitrarily chosen row from within the group.
  1024. #
  1025. # EVIDENCE-OF: R-53924-08809 If there is more than one non-aggregate
  1026. # expression in the result-set, then all such expressions are evaluated
  1027. # for the same row.
  1028. #
  1029. do_select_tests e_select-4.15 {
  1030. 1 "SELECT i, j FROM c2 GROUP BY i%2" {8 28 9 36}
  1031. 2 "SELECT i, j FROM c2 GROUP BY i%2 HAVING j<30" {8 28}
  1032. 3 "SELECT i, j FROM c2 GROUP BY i%2 HAVING j>30" {9 36}
  1033. 4 "SELECT i, j FROM c2 GROUP BY i%2 HAVING j>30" {9 36}
  1034. 5 "SELECT count(*), i, k FROM c2 NATURAL JOIN c3 GROUP BY substr(k, 1, 1)"
  1035. {2 5 boron 2 2 helium 1 3 lithium}
  1036. }
  1037. # EVIDENCE-OF: R-19334-12811 Each group of input dataset rows
  1038. # contributes a single row to the set of result rows.
  1039. #
  1040. # EVIDENCE-OF: R-02223-49279 Subject to filtering associated with the
  1041. # DISTINCT keyword, the number of rows returned by an aggregate query
  1042. # with a GROUP BY clause is the same as the number of groups of rows
  1043. # produced by applying the GROUP BY and HAVING clauses to the filtered
  1044. # input dataset.
  1045. #
  1046. do_select_tests e_select.4.16 -count {
  1047. 1 "SELECT i, j FROM c2 GROUP BY i%2" 2
  1048. 2 "SELECT i, j FROM c2 GROUP BY i" 9
  1049. 3 "SELECT i, j FROM c2 GROUP BY i HAVING i<5" 4
  1050. }
  1051. #-------------------------------------------------------------------------
  1052. # The following tests attempt to verify statements made regarding the ALL
  1053. # and DISTINCT keywords.
  1054. #
  1055. drop_all_tables
  1056. do_execsql_test e_select-5.1.0 {
  1057. CREATE TABLE h1(a, b);
  1058. INSERT INTO h1 VALUES(1, 'one');
  1059. INSERT INTO h1 VALUES(1, 'I');
  1060. INSERT INTO h1 VALUES(1, 'i');
  1061. INSERT INTO h1 VALUES(4, 'four');
  1062. INSERT INTO h1 VALUES(4, 'IV');
  1063. INSERT INTO h1 VALUES(4, 'iv');
  1064. CREATE TABLE h2(x COLLATE nocase);
  1065. INSERT INTO h2 VALUES('One');
  1066. INSERT INTO h2 VALUES('Two');
  1067. INSERT INTO h2 VALUES('Three');
  1068. INSERT INTO h2 VALUES('Four');
  1069. INSERT INTO h2 VALUES('one');
  1070. INSERT INTO h2 VALUES('two');
  1071. INSERT INTO h2 VALUES('three');
  1072. INSERT INTO h2 VALUES('four');
  1073. CREATE TABLE h3(c, d);
  1074. INSERT INTO h3 VALUES(1, NULL);
  1075. INSERT INTO h3 VALUES(2, NULL);
  1076. INSERT INTO h3 VALUES(3, NULL);
  1077. INSERT INTO h3 VALUES(4, '2');
  1078. INSERT INTO h3 VALUES(5, NULL);
  1079. INSERT INTO h3 VALUES(6, '2,3');
  1080. INSERT INTO h3 VALUES(7, NULL);
  1081. INSERT INTO h3 VALUES(8, '2,4');
  1082. INSERT INTO h3 VALUES(9, '3');
  1083. } {}
  1084. # EVIDENCE-OF: R-60770-10612 One of the ALL or DISTINCT keywords may
  1085. # follow the SELECT keyword in a simple SELECT statement.
  1086. #
  1087. do_select_tests e_select-5.1 {
  1088. 1 "SELECT ALL a FROM h1" {1 1 1 4 4 4}
  1089. 2 "SELECT DISTINCT a FROM h1" {1 4}
  1090. }
  1091. # EVIDENCE-OF: R-08861-34280 If the simple SELECT is a SELECT ALL, then
  1092. # the entire set of result rows are returned by the SELECT.
  1093. #
  1094. # EVIDENCE-OF: R-01256-01950 If neither ALL or DISTINCT are present,
  1095. # then the behavior is as if ALL were specified.
  1096. #
  1097. # EVIDENCE-OF: R-14442-41305 If the simple SELECT is a SELECT DISTINCT,
  1098. # then duplicate rows are removed from the set of result rows before it
  1099. # is returned.
  1100. #
  1101. # The three testable statements above are tested by e_select-5.2.*,
  1102. # 5.3.* and 5.4.* respectively.
  1103. #
  1104. do_select_tests e_select-5 {
  1105. 3.1 "SELECT ALL x FROM h2" {One Two Three Four one two three four}
  1106. 3.2 "SELECT ALL x FROM h1, h2 ON (x=b)" {One one Four four}
  1107. 3.1 "SELECT x FROM h2" {One Two Three Four one two three four}
  1108. 3.2 "SELECT x FROM h1, h2 ON (x=b)" {One one Four four}
  1109. 4.1 "SELECT DISTINCT x FROM h2" {One Two Three Four}
  1110. 4.2 "SELECT DISTINCT x FROM h1, h2 ON (x=b)" {One Four}
  1111. }
  1112. # EVIDENCE-OF: R-02054-15343 For the purposes of detecting duplicate
  1113. # rows, two NULL values are considered to be equal.
  1114. #
  1115. do_select_tests e_select-5.5 {
  1116. 1 "SELECT DISTINCT d FROM h3" {{} 2 2,3 2,4 3}
  1117. }
  1118. # EVIDENCE-OF: R-58359-52112 The normal rules for selecting a collation
  1119. # sequence to compare text values with apply.
  1120. #
  1121. do_select_tests e_select-5.6 {
  1122. 1 "SELECT DISTINCT b FROM h1" {one I i four IV iv}
  1123. 2 "SELECT DISTINCT b COLLATE nocase FROM h1" {one I four IV}
  1124. 3 "SELECT DISTINCT x FROM h2" {One Two Three Four}
  1125. 4 "SELECT DISTINCT x COLLATE binary FROM h2" {
  1126. One Two Three Four one two three four
  1127. }
  1128. }
  1129. #-------------------------------------------------------------------------
  1130. # The following tests - e_select-7.* - test that statements made to do
  1131. # with compound SELECT statements are correct.
  1132. #
  1133. # EVIDENCE-OF: R-39368-64333 In a compound SELECT, all the constituent
  1134. # SELECTs must return the same number of result columns.
  1135. #
  1136. # All the other tests in this section use compound SELECTs created
  1137. # using component SELECTs that do return the same number of columns.
  1138. # So the tests here just show that it is an error to attempt otherwise.
  1139. #
  1140. drop_all_tables
  1141. do_execsql_test e_select-7.1.0 {
  1142. CREATE TABLE j1(a, b, c);
  1143. CREATE TABLE j2(e, f);
  1144. CREATE TABLE j3(g);
  1145. } {}
  1146. do_select_tests e_select-7.1 -error {
  1147. SELECTs to the left and right of %s do not have the same number of result columns
  1148. } {
  1149. 1 "SELECT a, b FROM j1 UNION ALL SELECT g FROM j3" {{UNION ALL}}
  1150. 2 "SELECT * FROM j1 UNION ALL SELECT * FROM j3" {{UNION ALL}}
  1151. 3 "SELECT a, b FROM j1 UNION ALL SELECT g FROM j3" {{UNION ALL}}
  1152. 4 "SELECT a, b FROM j1 UNION ALL SELECT * FROM j3,j2" {{UNION ALL}}
  1153. 5 "SELECT * FROM j3,j2 UNION ALL SELECT a, b FROM j1" {{UNION ALL}}
  1154. 6 "SELECT a, b FROM j1 UNION SELECT g FROM j3" {UNION}
  1155. 7 "SELECT * FROM j1 UNION SELECT * FROM j3" {UNION}
  1156. 8 "SELECT a, b FROM j1 UNION SELECT g FROM j3" {UNION}
  1157. 9 "SELECT a, b FROM j1 UNION SELECT * FROM j3,j2" {UNION}
  1158. 10 "SELECT * FROM j3,j2 UNION SELECT a, b FROM j1" {UNION}
  1159. 11 "SELECT a, b FROM j1 INTERSECT SELECT g FROM j3" {INTERSECT}
  1160. 12 "SELECT * FROM j1 INTERSECT SELECT * FROM j3" {INTERSECT}
  1161. 13 "SELECT a, b FROM j1 INTERSECT SELECT g FROM j3" {INTERSECT}
  1162. 14 "SELECT a, b FROM j1 INTERSECT SELECT * FROM j3,j2" {INTERSECT}
  1163. 15 "SELECT * FROM j3,j2 INTERSECT SELECT a, b FROM j1" {INTERSECT}
  1164. 16 "SELECT a, b FROM j1 EXCEPT SELECT g FROM j3" {EXCEPT}
  1165. 17 "SELECT * FROM j1 EXCEPT SELECT * FROM j3" {EXCEPT}
  1166. 18 "SELECT a, b FROM j1 EXCEPT SELECT g FROM j3" {EXCEPT}
  1167. 19 "SELECT a, b FROM j1 EXCEPT SELECT * FROM j3,j2" {EXCEPT}
  1168. 20 "SELECT * FROM j3,j2 EXCEPT SELECT a, b FROM j1" {EXCEPT}
  1169. }
  1170. # EVIDENCE-OF: R-01450-11152 As the components of a compound SELECT must
  1171. # be simple SELECT statements, they may not contain ORDER BY or LIMIT
  1172. # clauses.
  1173. #
  1174. foreach {tn select op1 op2} {
  1175. 1 "SELECT * FROM j1 ORDER BY a UNION ALL SELECT * FROM j2,j3"
  1176. {ORDER BY} {UNION ALL}
  1177. 2 "SELECT count(*) FROM j1 ORDER BY 1 UNION ALL SELECT max(e) FROM j2"
  1178. {ORDER BY} {UNION ALL}
  1179. 3 "SELECT count(*), * FROM j1 ORDER BY 1,2,3 UNION ALL SELECT *,* FROM j2"
  1180. {ORDER BY} {UNION ALL}
  1181. 4 "SELECT * FROM j1 LIMIT 10 UNION ALL SELECT * FROM j2,j3"
  1182. LIMIT {UNION ALL}
  1183. 5 "SELECT * FROM j1 LIMIT 10 OFFSET 5 UNION ALL SELECT * FROM j2,j3"
  1184. LIMIT {UNION ALL}
  1185. 6 "SELECT a FROM j1 LIMIT (SELECT e FROM j2) UNION ALL SELECT g FROM j2,j3"
  1186. LIMIT {UNION ALL}
  1187. 7 "SELECT * FROM j1 ORDER BY a UNION SELECT * FROM j2,j3"
  1188. {ORDER BY} {UNION}
  1189. 8 "SELECT count(*) FROM j1 ORDER BY 1 UNION SELECT max(e) FROM j2"
  1190. {ORDER BY} {UNION}
  1191. 9 "SELECT count(*), * FROM j1 ORDER BY 1,2,3 UNION SELECT *,* FROM j2"
  1192. {ORDER BY} {UNION}
  1193. 10 "SELECT * FROM j1 LIMIT 10 UNION SELECT * FROM j2,j3"
  1194. LIMIT {UNION}
  1195. 11 "SELECT * FROM j1 LIMIT 10 OFFSET 5 UNION SELECT * FROM j2,j3"
  1196. LIMIT {UNION}
  1197. 12 "SELECT a FROM j1 LIMIT (SELECT e FROM j2) UNION SELECT g FROM j2,j3"
  1198. LIMIT {UNION}
  1199. 13 "SELECT * FROM j1 ORDER BY a EXCEPT SELECT * FROM j2,j3"
  1200. {ORDER BY} {EXCEPT}
  1201. 14 "SELECT count(*) FROM j1 ORDER BY 1 EXCEPT SELECT max(e) FROM j2"
  1202. {ORDER BY} {EXCEPT}
  1203. 15 "SELECT count(*), * FROM j1 ORDER BY 1,2,3 EXCEPT SELECT *,* FROM j2"
  1204. {ORDER BY} {EXCEPT}
  1205. 16 "SELECT * FROM j1 LIMIT 10 EXCEPT SELECT * FROM j2,j3"
  1206. LIMIT {EXCEPT}
  1207. 17 "SELECT * FROM j1 LIMIT 10 OFFSET 5 EXCEPT SELECT * FROM j2,j3"
  1208. LIMIT {EXCEPT}
  1209. 18 "SELECT a FROM j1 LIMIT (SELECT e FROM j2) EXCEPT SELECT g FROM j2,j3"
  1210. LIMIT {EXCEPT}
  1211. 19 "SELECT * FROM j1 ORDER BY a INTERSECT SELECT * FROM j2,j3"
  1212. {ORDER BY} {INTERSECT}
  1213. 20 "SELECT count(*) FROM j1 ORDER BY 1 INTERSECT SELECT max(e) FROM j2"
  1214. {ORDER BY} {INTERSECT}
  1215. 21 "SELECT count(*), * FROM j1 ORDER BY 1,2,3 INTERSECT SELECT *,* FROM j2"
  1216. {ORDER BY} {INTERSECT}
  1217. 22 "SELECT * FROM j1 LIMIT 10 INTERSECT SELECT * FROM j2,j3"
  1218. LIMIT {INTERSECT}
  1219. 23 "SELECT * FROM j1 LIMIT 10 OFFSET 5 INTERSECT SELECT * FROM j2,j3"
  1220. LIMIT {INTERSECT}
  1221. 24 "SELECT a FROM j1 LIMIT (SELECT e FROM j2) INTERSECT SELECT g FROM j2,j3"
  1222. LIMIT {INTERSECT}
  1223. } {
  1224. set err "$op1 clause should come after $op2 not before"
  1225. do_catchsql_test e_select-7.2.$tn $select [list 1 $err]
  1226. }
  1227. # EVIDENCE-OF: R-22874-32655 ORDER BY and LIMIT clauses may only occur
  1228. # at the end of the entire compound SELECT.
  1229. #
  1230. foreach {tn select} {
  1231. 1 "SELECT * FROM j1 UNION ALL SELECT * FROM j2,j3 ORDER BY a"
  1232. 2 "SELECT count(*) FROM j1 UNION ALL SELECT max(e) FROM j2 ORDER BY 1"
  1233. 3 "SELECT count(*), * FROM j1 UNION ALL SELECT *,* FROM j2 ORDER BY 1,2,3"
  1234. 4 "SELECT * FROM j1 UNION ALL SELECT * FROM j2,j3 LIMIT 10"
  1235. 5 "SELECT * FROM j1 UNION ALL SELECT * FROM j2,j3 LIMIT 10 OFFSET 5"
  1236. 6 "SELECT a FROM j1 UNION ALL SELECT g FROM j2,j3 LIMIT (SELECT 10)"
  1237. 7 "SELECT * FROM j1 UNION SELECT * FROM j2,j3 ORDER BY a"
  1238. 8 "SELECT count(*) FROM j1 UNION SELECT max(e) FROM j2 ORDER BY 1"
  1239. 9 "SELECT count(*), * FROM j1 UNION SELECT *,* FROM j2 ORDER BY 1,2,3"
  1240. 10 "SELECT * FROM j1 UNION SELECT * FROM j2,j3 LIMIT 10"
  1241. 11 "SELECT * FROM j1 UNION SELECT * FROM j2,j3 LIMIT 10 OFFSET 5"
  1242. 12 "SELECT a FROM j1 UNION SELECT g FROM j2,j3 LIMIT (SELECT 10)"
  1243. 13 "SELECT * FROM j1 EXCEPT SELECT * FROM j2,j3 ORDER BY a"
  1244. 14 "SELECT count(*) FROM j1 EXCEPT SELECT max(e) FROM j2 ORDER BY 1"
  1245. 15 "SELECT count(*), * FROM j1 EXCEPT SELECT *,* FROM j2 ORDER BY 1,2,3"
  1246. 16 "SELECT * FROM j1 EXCEPT SELECT * FROM j2,j3 LIMIT 10"
  1247. 17 "SELECT * FROM j1 EXCEPT SELECT * FROM j2,j3 LIMIT 10 OFFSET 5"
  1248. 18 "SELECT a FROM j1 EXCEPT SELECT g FROM j2,j3 LIMIT (SELECT 10)"
  1249. 19 "SELECT * FROM j1 INTERSECT SELECT * FROM j2,j3 ORDER BY a"
  1250. 20 "SELECT count(*) FROM j1 INTERSECT SELECT max(e) FROM j2 ORDER BY 1"
  1251. 21 "SELECT count(*), * FROM j1 INTERSECT SELECT *,* FROM j2 ORDER BY 1,2,3"
  1252. 22 "SELECT * FROM j1 INTERSECT SELECT * FROM j2,j3 LIMIT 10"
  1253. 23 "SELECT * FROM j1 INTERSECT SELECT * FROM j2,j3 LIMIT 10 OFFSET 5"
  1254. 24 "SELECT a FROM j1 INTERSECT SELECT g FROM j2,j3 LIMIT (SELECT 10)"
  1255. } {
  1256. do_test e_select-7.3.$tn { catch {execsql $select} msg } 0
  1257. }
  1258. # EVIDENCE-OF: R-08531-36543 A compound SELECT created using UNION ALL
  1259. # operator returns all the rows from the SELECT to the left of the UNION
  1260. # ALL operator, and all the rows from the SELECT to the right of it.
  1261. #
  1262. drop_all_tables
  1263. do_execsql_test e_select-7.4.0 {
  1264. CREATE TABLE q1(a TEXT, b INTEGER, c);
  1265. CREATE TABLE q2(d NUMBER, e BLOB);
  1266. CREATE TABLE q3(f REAL, g);
  1267. INSERT INTO q1 VALUES(16, -87.66, NULL);
  1268. INSERT INTO q1 VALUES('legible', 94, -42.47);
  1269. INSERT INTO q1 VALUES('beauty', 36, NULL);
  1270. INSERT INTO q2 VALUES('legible', 1);
  1271. INSERT INTO q2 VALUES('beauty', 2);
  1272. INSERT INTO q2 VALUES(-65.91, 4);
  1273. INSERT INTO q2 VALUES('emanating', -16.56);
  1274. INSERT INTO q3 VALUES('beauty', 2);
  1275. INSERT INTO q3 VALUES('beauty', 2);
  1276. } {}
  1277. do_select_tests e_select-7.4 {
  1278. 1 {SELECT a FROM q1 UNION ALL SELECT d FROM q2}
  1279. {16 legible beauty legible beauty -65.91 emanating}
  1280. 2 {SELECT * FROM q1 WHERE a=16 UNION ALL SELECT 'x', * FROM q2 WHERE oid=1}
  1281. {16 -87.66 {} x legible 1}
  1282. 3 {SELECT count(*) FROM q1 UNION ALL SELECT min(e) FROM q2}
  1283. {3 -16.56}
  1284. 4 {SELECT * FROM q2 UNION ALL SELECT * FROM q3}
  1285. {legible 1 beauty 2 -65.91 4 emanating -16.56 beauty 2 beauty 2}
  1286. }
  1287. # EVIDENCE-OF: R-20560-39162 The UNION operator works the same way as
  1288. # UNION ALL, except that duplicate rows are removed from the final
  1289. # result set.
  1290. #
  1291. do_select_tests e_select-7.5 {
  1292. 1 {SELECT a FROM q1 UNION SELECT d FROM q2}
  1293. {-65.91 16 beauty emanating legible}
  1294. 2 {SELECT * FROM q1 WHERE a=16 UNION SELECT 'x', * FROM q2 WHERE oid=1}
  1295. {16 -87.66 {} x legible 1}
  1296. 3 {SELECT count(*) FROM q1 UNION SELECT min(e) FROM q2}
  1297. {-16.56 3}
  1298. 4 {SELECT * FROM q2 UNION SELECT * FROM q3}
  1299. {-65.91 4 beauty 2 emanating -16.56 legible 1}
  1300. }
  1301. # EVIDENCE-OF: R-45764-31737 The INTERSECT operator returns the
  1302. # intersection of the results of the left and right SELECTs.
  1303. #
  1304. do_select_tests e_select-7.6 {
  1305. 1 {SELECT a FROM q1 INTERSECT SELECT d FROM q2} {beauty legible}
  1306. 2 {SELECT * FROM q2 INTERSECT SELECT * FROM q3} {beauty 2}
  1307. }
  1308. # EVIDENCE-OF: R-25787-28949 The EXCEPT operator returns the subset of
  1309. # rows returned by the left SELECT that are not also returned by the
  1310. # right-hand SELECT.
  1311. #
  1312. do_select_tests e_select-7.7 {
  1313. 1 {SELECT a FROM q1 EXCEPT SELECT d FROM q2} {16}
  1314. 2 {SELECT * FROM q2 EXCEPT SELECT * FROM q3}
  1315. {-65.91 4 emanating -16.56 legible 1}
  1316. }
  1317. # EVIDENCE-OF: R-40729-56447 Duplicate rows are removed from the results
  1318. # of INTERSECT and EXCEPT operators before the result set is returned.
  1319. #
  1320. do_select_tests e_select-7.8 {
  1321. 0 {SELECT * FROM q3} {beauty 2 beauty 2}
  1322. 1 {SELECT * FROM q3 INTERSECT SELECT * FROM q3} {beauty 2}
  1323. 2 {SELECT * FROM q3 EXCEPT SELECT a,b FROM q1} {beauty 2}
  1324. }
  1325. # EVIDENCE-OF: R-46765-43362 For the purposes of determining duplicate
  1326. # rows for the results of compound SELECT operators, NULL values are
  1327. # considered equal to other NULL values and distinct from all non-NULL
  1328. # values.
  1329. #
  1330. db nullvalue null
  1331. do_select_tests e_select-7.9 {
  1332. 1 {SELECT NULL UNION ALL SELECT NULL} {null null}
  1333. 2 {SELECT NULL UNION SELECT NULL} {null}
  1334. 3 {SELECT NULL INTERSECT SELECT NULL} {null}
  1335. 4 {SELECT NULL EXCEPT SELECT NULL} {}
  1336. 5 {SELECT NULL UNION ALL SELECT 'ab'} {null ab}
  1337. 6 {SELECT NULL UNION SELECT 'ab'} {null ab}
  1338. 7 {SELECT NULL INTERSECT SELECT 'ab'} {}
  1339. 8 {SELECT NULL EXCEPT SELECT 'ab'} {null}
  1340. 9 {SELECT NULL UNION ALL SELECT 0} {null 0}
  1341. 10 {SELECT NULL UNION SELECT 0} {null 0}
  1342. 11 {SELECT NULL INTERSECT SELECT 0} {}
  1343. 12 {SELECT NULL EXCEPT SELECT 0} {null}
  1344. 13 {SELECT c FROM q1 UNION ALL SELECT g FROM q3} {null -42.47 null 2 2}
  1345. 14 {SELECT c FROM q1 UNION SELECT g FROM q3} {null -42.47 2}
  1346. 15 {SELECT c FROM q1 INTERSECT SELECT g FROM q3} {}
  1347. 16 {SELECT c FROM q1 EXCEPT SELECT g FROM q3} {null -42.47}
  1348. }
  1349. db nullvalue {}
  1350. # EVIDENCE-OF: R-51232-50224 The collation sequence used to compare two
  1351. # text values is determined as if the columns of the left and right-hand
  1352. # SELECT statements were the left and right-hand operands of the equals
  1353. # (=) operator, except that greater precedence is not assigned to a
  1354. # collation sequence specified with the postfix COLLATE operator.
  1355. #
  1356. drop_all_tables
  1357. do_execsql_test e_select-7.10.0 {
  1358. CREATE TABLE y1(a COLLATE nocase, b COLLATE binary, c);
  1359. INSERT INTO y1 VALUES('Abc', 'abc', 'aBC');
  1360. } {}
  1361. do_select_tests e_select-7.10 {
  1362. 1 {SELECT 'abc' UNION SELECT 'ABC'} {ABC abc}
  1363. 2 {SELECT 'abc' COLLATE nocase UNION SELECT 'ABC'} {ABC}
  1364. 3 {SELECT 'abc' UNION SELECT 'ABC' COLLATE nocase} {ABC}
  1365. 4 {SELECT 'abc' COLLATE binary UNION SELECT 'ABC' COLLATE nocase} {ABC abc}
  1366. 5 {SELECT 'abc' COLLATE nocase UNION SELECT 'ABC' COLLATE binary} {ABC}
  1367. 6 {SELECT a FROM y1 UNION SELECT b FROM y1} {abc}
  1368. 7 {SELECT b FROM y1 UNION SELECT a FROM y1} {Abc abc}
  1369. 8 {SELECT a FROM y1 UNION SELECT c FROM y1} {aBC}
  1370. 9 {SELECT a FROM y1 UNION SELECT c COLLATE binary FROM y1} {aBC}
  1371. }
  1372. # EVIDENCE-OF: R-32706-07403 No affinity transformations are applied to
  1373. # any values when comparing rows as part of a compound SELECT.
  1374. #
  1375. drop_all_tables
  1376. do_execsql_test e_select-7.10.0 {
  1377. CREATE TABLE w1(a TEXT, b NUMBER);
  1378. CREATE TABLE w2(a, b TEXT);
  1379. INSERT INTO w1 VALUES('1', 4.1);
  1380. INSERT INTO w2 VALUES(1, 4.1);
  1381. } {}
  1382. do_select_tests e_select-7.11 {
  1383. 1 { SELECT a FROM w1 UNION SELECT a FROM w2 } {1 1}
  1384. 2 { SELECT a FROM w2 UNION SELECT a FROM w1 } {1 1}
  1385. 3 { SELECT b FROM w1 UNION SELECT b FROM w2 } {4.1 4.1}
  1386. 4 { SELECT b FROM w2 UNION SELECT b FROM w1 } {4.1 4.1}
  1387. 5 { SELECT a FROM w1 INTERSECT SELECT a FROM w2 } {}
  1388. 6 { SELECT a FROM w2 INTERSECT SELECT a FROM w1 } {}
  1389. 7 { SELECT b FROM w1 INTERSECT SELECT b FROM w2 } {}
  1390. 8 { SELECT b FROM w2 INTERSECT SELECT b FROM w1 } {}
  1391. 9 { SELECT a FROM w1 EXCEPT SELECT a FROM w2 } {1}
  1392. 10 { SELECT a FROM w2 EXCEPT SELECT a FROM w1 } {1}
  1393. 11 { SELECT b FROM w1 EXCEPT SELECT b FROM w2 } {4.1}
  1394. 12 { SELECT b FROM w2 EXCEPT SELECT b FROM w1 } {4.1}
  1395. }
  1396. # EVIDENCE-OF: R-32562-20566 When three or more simple SELECTs are
  1397. # connected into a compound SELECT, they group from left to right. In
  1398. # other words, if "A", "B" and "C" are all simple SELECT statements, (A
  1399. # op B op C) is processed as ((A op B) op C).
  1400. #
  1401. # e_select-7.12.1: Precedence of UNION vs. INTERSECT
  1402. # e_select-7.12.2: Precedence of UNION vs. UNION ALL
  1403. # e_select-7.12.3: Precedence of UNION vs. EXCEPT
  1404. # e_select-7.12.4: Precedence of INTERSECT vs. UNION ALL
  1405. # e_select-7.12.5: Precedence of INTERSECT vs. EXCEPT
  1406. # e_select-7.12.6: Precedence of UNION ALL vs. EXCEPT
  1407. # e_select-7.12.7: Check that "a EXCEPT b EXCEPT c" is processed as
  1408. # "(a EXCEPT b) EXCEPT c".
  1409. #
  1410. # The INTERSECT and EXCEPT operations are mutually commutative. So
  1411. # the e_select-7.12.5 test cases do not prove very much.
  1412. #
  1413. drop_all_tables
  1414. do_execsql_test e_select-7.12.0 {
  1415. CREATE TABLE t1(x);
  1416. INSERT INTO t1 VALUES(1);
  1417. INSERT INTO t1 VALUES(2);
  1418. INSERT INTO t1 VALUES(3);
  1419. } {}
  1420. foreach {tn select res} {
  1421. 1a "(1,2) INTERSECT (1) UNION (3)" {1 3}
  1422. 1b "(3) UNION (1,2) INTERSECT (1)" {1}
  1423. 2a "(1,2) UNION (3) UNION ALL (1)" {1 2 3 1}
  1424. 2b "(1) UNION ALL (3) UNION (1,2)" {1 2 3}
  1425. 3a "(1,2) UNION (3) EXCEPT (1)" {2 3}
  1426. 3b "(1,2) EXCEPT (3) UNION (1)" {1 2}
  1427. 4a "(1,2) INTERSECT (1) UNION ALL (3)" {1 3}
  1428. 4b "(3) UNION (1,2) INTERSECT (1)" {1}
  1429. 5a "(1,2) INTERSECT (2) EXCEPT (2)" {}
  1430. 5b "(2,3) EXCEPT (2) INTERSECT (2)" {}
  1431. 6a "(2) UNION ALL (2) EXCEPT (2)" {}
  1432. 6b "(2) EXCEPT (2) UNION ALL (2)" {2}
  1433. 7 "(2,3) EXCEPT (2) EXCEPT (3)" {}
  1434. } {
  1435. set select [string map {( {SELECT x FROM t1 WHERE x IN (}} $select]
  1436. do_execsql_test e_select-7.12.$tn $select [list {*}$res]
  1437. }
  1438. #-------------------------------------------------------------------------
  1439. # ORDER BY clauses
  1440. #
  1441. drop_all_tables
  1442. do_execsql_test e_select-8.1.0 {
  1443. CREATE TABLE d1(x, y, z);
  1444. INSERT INTO d1 VALUES(1, 2, 3);
  1445. INSERT INTO d1 VALUES(2, 5, -1);
  1446. INSERT INTO d1 VALUES(1, 2, 8);
  1447. INSERT INTO d1 VALUES(1, 2, 7);
  1448. INSERT INTO d1 VALUES(2, 4, 93);
  1449. INSERT INTO d1 VALUES(1, 2, -20);
  1450. INSERT INTO d1 VALUES(1, 4, 93);
  1451. INSERT INTO d1 VALUES(1, 5, -1);
  1452. CREATE TABLE d2(a, b);
  1453. INSERT INTO d2 VALUES('gently', 'failings');
  1454. INSERT INTO d2 VALUES('commercials', 'bathrobe');
  1455. INSERT INTO d2 VALUES('iterate', 'sexton');
  1456. INSERT INTO d2 VALUES('babied', 'charitableness');
  1457. INSERT INTO d2 VALUES('solemnness', 'annexed');
  1458. INSERT INTO d2 VALUES('rejoicing', 'liabilities');
  1459. INSERT INTO d2 VALUES('pragmatist', 'guarded');
  1460. INSERT INTO d2 VALUES('barked', 'interrupted');
  1461. INSERT INTO d2 VALUES('reemphasizes', 'reply');
  1462. INSERT INTO d2 VALUES('lad', 'relenting');
  1463. } {}
  1464. # EVIDENCE-OF: R-44988-41064 Rows are first sorted based on the results
  1465. # of evaluating the left-most expression in the ORDER BY list, then ties
  1466. # are broken by evaluating the second left-most expression and so on.
  1467. #
  1468. do_select_tests e_select-8.1 {
  1469. 1 "SELECT * FROM d1 ORDER BY x, y, z" {
  1470. 1 2 -20 1 2 3 1 2 7 1 2 8
  1471. 1 4 93 1 5 -1 2 4 93 2 5 -1
  1472. }
  1473. }
  1474. # EVIDENCE-OF: R-06617-54588 Each ORDER BY expression may be optionally
  1475. # followed by one of the keywords ASC (smaller values are returned
  1476. # first) or DESC (larger values are returned first).
  1477. #
  1478. # Test cases e_select-8.2.* test the above.
  1479. #
  1480. # EVIDENCE-OF: R-18705-33393 If neither ASC or DESC are specified, rows
  1481. # are sorted in ascending (smaller values first) order by default.
  1482. #
  1483. # Test cases e_select-8.3.* test the above. All 8.3 test cases are
  1484. # copies of 8.2 test cases with the explicit "ASC" removed.
  1485. #
  1486. do_select_tests e_select-8 {
  1487. 2.1 "SELECT * FROM d1 ORDER BY x ASC, y ASC, z ASC" {
  1488. 1 2 -20 1 2 3 1 2 7 1 2 8
  1489. 1 4 93 1 5 -1 2 4 93 2 5 -1
  1490. }
  1491. 2.2 "SELECT * FROM d1 ORDER BY x DESC, y DESC, z DESC" {
  1492. 2 5 -1 2 4 93 1 5 -1 1 4 93
  1493. 1 2 8 1 2 7 1 2 3 1 2 -20
  1494. }
  1495. 2.3 "SELECT * FROM d1 ORDER BY x DESC, y ASC, z DESC" {
  1496. 2 4 93 2 5 -1 1 2 8 1 2 7
  1497. 1 2 3 1 2 -20 1 4 93 1 5 -1
  1498. }
  1499. 2.4 "SELECT * FROM d1 ORDER BY x DESC, y ASC, z ASC" {
  1500. 2 4 93 2 5 -1 1 2 -20 1 2 3
  1501. 1 2 7 1 2 8 1 4 93 1 5 -1
  1502. }
  1503. 3.1 "SELECT * FROM d1 ORDER BY x, y, z" {
  1504. 1 2 -20 1 2 3 1 2 7 1 2 8
  1505. 1 4 93 1 5 -1 2 4 93 2 5 -1
  1506. }
  1507. 3.3 "SELECT * FROM d1 ORDER BY x DESC, y, z DESC" {
  1508. 2 4 93 2 5 -1 1 2 8 1 2 7
  1509. 1 2 3 1 2 -20 1 4 93 1 5 -1
  1510. }
  1511. 3.4 "SELECT * FROM d1 ORDER BY x DESC, y, z" {
  1512. 2 4 93 2 5 -1 1 2 -20 1 2 3
  1513. 1 2 7 1 2 8 1 4 93 1 5 -1
  1514. }
  1515. }
  1516. # EVIDENCE-OF: R-29779-04281 If the ORDER BY expression is a constant
  1517. # integer K then the expression is considered an alias for the K-th
  1518. # column of the result set (columns are numbered from left to right
  1519. # starting with 1).
  1520. #
  1521. do_select_tests e_select-8.4 {
  1522. 1 "SELECT * FROM d1 ORDER BY 1 ASC, 2 ASC, 3 ASC" {
  1523. 1 2 -20 1 2 3 1 2 7 1 2 8
  1524. 1 4 93 1 5 -1 2 4 93 2 5 -1
  1525. }
  1526. 2 "SELECT * FROM d1 ORDER BY 1 DESC, 2 DESC, 3 DESC" {
  1527. 2 5 -1 2 4 93 1 5 -1 1 4 93
  1528. 1 2 8 1 2 7 1 2 3 1 2 -20
  1529. }
  1530. 3 "SELECT * FROM d1 ORDER BY 1 DESC, 2 ASC, 3 DESC" {
  1531. 2 4 93 2 5 -1 1 2 8 1 2 7
  1532. 1 2 3 1 2 -20 1 4 93 1 5 -1
  1533. }
  1534. 4 "SELECT * FROM d1 ORDER BY 1 DESC, 2 ASC, 3 ASC" {
  1535. 2 4 93 2 5 -1 1 2 -20 1 2 3
  1536. 1 2 7 1 2 8 1 4 93 1 5 -1
  1537. }
  1538. 5 "SELECT * FROM d1 ORDER BY 1, 2, 3" {
  1539. 1 2 -20 1 2 3 1 2 7 1 2 8
  1540. 1 4 93 1 5 -1 2 4 93 2 5 -1
  1541. }
  1542. 6 "SELECT * FROM d1 ORDER BY 1 DESC, 2, 3 DESC" {
  1543. 2 4 93 2 5 -1 1 2 8 1 2 7
  1544. 1 2 3 1 2 -20 1 4 93 1 5 -1
  1545. }
  1546. 7 "SELECT * FROM d1 ORDER BY 1 DESC, 2, 3" {
  1547. 2 4 93 2 5 -1 1 2 -20 1 2 3
  1548. 1 2 7 1 2 8 1 4 93 1 5 -1
  1549. }
  1550. 8 "SELECT z, x FROM d1 ORDER BY 2" {
  1551. /# 1 # 1 # 1 # 1
  1552. # 1 # 1 # 2 # 2/
  1553. }
  1554. 9 "SELECT z, x FROM d1 ORDER BY 1" {
  1555. /-20 1 -1 # -1 # 3 1
  1556. 7 1 8 1 93 # 93 #/
  1557. }
  1558. }
  1559. # EVIDENCE-OF: R-63286-51977 If the ORDER BY expression is an identifier
  1560. # that corresponds to the alias of one of the output columns, then the
  1561. # expression is considered an alias for that column.
  1562. #
  1563. do_select_tests e_select-8.5 {
  1564. 1 "SELECT z+1 AS abc FROM d1 ORDER BY abc" {
  1565. -19 0 0 4 8 9 94 94
  1566. }
  1567. 2 "SELECT z+1 AS abc FROM d1 ORDER BY abc DESC" {
  1568. 94 94 9 8 4 0 0 -19
  1569. }
  1570. 3 "SELECT z AS x, x AS z FROM d1 ORDER BY z" {
  1571. /# 1 # 1 # 1 # 1 # 1 # 1 # 2 # 2/
  1572. }
  1573. 4 "SELECT z AS x, x AS z FROM d1 ORDER BY x" {
  1574. /-20 1 -1 # -1 # 3 1 7 1 8 1 93 # 93 #/
  1575. }
  1576. }
  1577. # EVIDENCE-OF: R-65068-27207 Otherwise, if the ORDER BY expression is
  1578. # any other expression, it is evaluated and the returned value used to
  1579. # order the output rows.
  1580. #
  1581. # EVIDENCE-OF: R-03421-57988 If the SELECT statement is a simple SELECT,
  1582. # then an ORDER BY may contain any arbitrary expressions.
  1583. #
  1584. do_select_tests e_select-8.6 {
  1585. 1 "SELECT * FROM d1 ORDER BY x+y+z" {
  1586. 1 2 -20 1 5 -1 1 2 3 2 5 -1
  1587. 1 2 7 1 2 8 1 4 93 2 4 93
  1588. }
  1589. 2 "SELECT * FROM d1 ORDER BY x*z" {
  1590. 1 2 -20 2 5 -1 1 5 -1 1 2 3
  1591. 1 2 7 1 2 8 1 4 93 2 4 93
  1592. }
  1593. 3 "SELECT * FROM d1 ORDER BY y*z" {
  1594. 1 2 -20 2 5 -1 1 5 -1 1 2 3
  1595. 1 2 7 1 2 8 2 4 93 1 4 93
  1596. }
  1597. }
  1598. # EVIDENCE-OF: R-28853-08147 However, if the SELECT is a compound
  1599. # SELECT, then ORDER BY expressions that are not aliases to output
  1600. # columns must be exactly the same as an expression used as an output
  1601. # column.
  1602. #
  1603. do_select_tests e_select-8.7.1 -error {
  1604. %s ORDER BY term does not match any column in the result set
  1605. } {
  1606. 1 "SELECT x FROM d1 UNION ALL SELECT a FROM d2 ORDER BY x*z" 1st
  1607. 2 "SELECT x,z FROM d1 UNION ALL SELECT a,b FROM d2 ORDER BY x, x/z" 2nd
  1608. }
  1609. do_select_tests e_select-8.7.2 {
  1610. 1 "SELECT x*z FROM d1 UNION ALL SELECT a FROM d2 ORDER BY x*z" {
  1611. -20 -2 -1 3 7 8 93 186 babied barked commercials gently
  1612. iterate lad pragmatist reemphasizes rejoicing solemnness
  1613. }
  1614. 2 "SELECT x, x/z FROM d1 UNION ALL SELECT a,b FROM d2 ORDER BY x, x/z" {
  1615. 1 -1 1 0 1 0 1 0 1 0 1 0 2 -2 2 0
  1616. babied charitableness barked interrupted commercials bathrobe gently
  1617. failings iterate sexton lad relenting pragmatist guarded reemphasizes reply
  1618. rejoicing liabilities solemnness annexed
  1619. }
  1620. }
  1621. do_execsql_test e_select-8.8.0 {
  1622. CREATE TABLE d3(a);
  1623. INSERT INTO d3 VALUES('text');
  1624. INSERT INTO d3 VALUES(14.1);
  1625. INSERT INTO d3 VALUES(13);
  1626. INSERT INTO d3 VALUES(X'78787878');
  1627. INSERT INTO d3 VALUES(15);
  1628. INSERT INTO d3 VALUES(12.9);
  1629. INSERT INTO d3 VALUES(null);
  1630. CREATE TABLE d4(x COLLATE nocase);
  1631. INSERT INTO d4 VALUES('abc');
  1632. INSERT INTO d4 VALUES('ghi');
  1633. INSERT INTO d4 VALUES('DEF');
  1634. INSERT INTO d4 VALUES('JKL');
  1635. } {}
  1636. # EVIDENCE-OF: R-10883-17697 For the purposes of sorting rows, values
  1637. # are compared in the same way as for comparison expressions.
  1638. #
  1639. # The following tests verify that values of different types are sorted
  1640. # correctly, and that mixed real and integer values are compared properly.
  1641. #
  1642. do_execsql_test e_select-8.8.1 {
  1643. SELECT a FROM d3 ORDER BY a
  1644. } {{} 12.9 13 14.1 15 text xxxx}
  1645. do_execsql_test e_select-8.8.2 {
  1646. SELECT a FROM d3 ORDER BY a DESC
  1647. } {xxxx text 15 14.1 13 12.9 {}}
  1648. # EVIDENCE-OF: R-64199-22471 If the ORDER BY expression is assigned a
  1649. # collation sequence using the postfix COLLATE operator, then the
  1650. # specified collation sequence is used.
  1651. #
  1652. do_execsql_test e_select-8.9.1 {
  1653. SELECT x FROM d4 ORDER BY 1 COLLATE binary
  1654. } {DEF JKL abc ghi}
  1655. do_execsql_test e_select-8.9.2 {
  1656. SELECT x COLLATE binary FROM d4 ORDER BY 1 COLLATE nocase
  1657. } {abc DEF ghi JKL}
  1658. # EVIDENCE-OF: R-09398-26102 Otherwise, if the ORDER BY expression is
  1659. # an alias to an expression that has been assigned a collation sequence
  1660. # using the postfix COLLATE operator, then the collation sequence
  1661. # assigned to the aliased expression is used.
  1662. #
  1663. # In the test 8.10.2, the only result-column expression has no alias. So the
  1664. # ORDER BY expression is not a reference to it and therefore does not inherit
  1665. # the collation sequence. In test 8.10.3, "x" is the alias (as well as the
  1666. # column name), so the ORDER BY expression is interpreted as an alias and the
  1667. # collation sequence attached to the result column is used for sorting.
  1668. #
  1669. do_execsql_test e_select-8.10.1 {
  1670. SELECT x COLLATE binary FROM d4 ORDER BY 1
  1671. } {DEF JKL abc ghi}
  1672. do_execsql_test e_select-8.10.2 {
  1673. SELECT x COLLATE binary FROM d4 ORDER BY x
  1674. } {abc DEF ghi JKL}
  1675. do_execsql_test e_select-8.10.3 {
  1676. SELECT x COLLATE binary AS x FROM d4 ORDER BY x
  1677. } {DEF JKL abc ghi}
  1678. # EVIDENCE-OF: R-27301-09658 Otherwise, if the ORDER BY expression is a
  1679. # column or an alias of an expression that is a column, then the default
  1680. # collation sequence for the column is used.
  1681. #
  1682. do_execsql_test e_select-8.11.1 {
  1683. SELECT x AS y FROM d4 ORDER BY y
  1684. } {abc DEF ghi JKL}
  1685. do_execsql_test e_select-8.11.2 {
  1686. SELECT x||'' FROM d4 ORDER BY x
  1687. } {abc DEF ghi JKL}
  1688. # EVIDENCE-OF: R-49925-55905 Otherwise, the BINARY collation sequence is
  1689. # used.
  1690. #
  1691. do_execsql_test e_select-8.12.1 {
  1692. SELECT x FROM d4 ORDER BY x||''
  1693. } {DEF JKL abc ghi}
  1694. # EVIDENCE-OF: R-44130-32593 If an ORDER BY expression is not an integer
  1695. # alias, then SQLite searches the left-most SELECT in the compound for a
  1696. # result column that matches either the second or third rules above. If
  1697. # a match is found, the search stops and the expression is handled as an
  1698. # alias for the result column that it has been matched against.
  1699. # Otherwise, the next SELECT to the right is tried, and so on.
  1700. #
  1701. do_execsql_test e_select-8.13.0 {
  1702. CREATE TABLE d5(a, b);
  1703. CREATE TABLE d6(c, d);
  1704. CREATE TABLE d7(e, f);
  1705. INSERT INTO d5 VALUES(1, 'f');
  1706. INSERT INTO d6 VALUES(2, 'e');
  1707. INSERT INTO d7 VALUES(3, 'd');
  1708. INSERT INTO d5 VALUES(4, 'c');
  1709. INSERT INTO d6 VALUES(5, 'b');
  1710. INSERT INTO d7 VALUES(6, 'a');
  1711. CREATE TABLE d8(x COLLATE nocase);
  1712. CREATE TABLE d9(y COLLATE nocase);
  1713. INSERT INTO d8 VALUES('a');
  1714. INSERT INTO d9 VALUES('B');
  1715. INSERT INTO d8 VALUES('c');
  1716. INSERT INTO d9 VALUES('D');
  1717. } {}
  1718. do_select_tests e_select-8.13 {
  1719. 1 { SELECT a FROM d5 UNION ALL SELECT c FROM d6 UNION ALL SELECT e FROM d7
  1720. ORDER BY a
  1721. } {1 2 3 4 5 6}
  1722. 2 { SELECT a FROM d5 UNION ALL SELECT c FROM d6 UNION ALL SELECT e FROM d7
  1723. ORDER BY c
  1724. } {1 2 3 4 5 6}
  1725. 3 { SELECT a FROM d5 UNION ALL SELECT c FROM d6 UNION ALL SELECT e FROM d7
  1726. ORDER BY e
  1727. } {1 2 3 4 5 6}
  1728. 4 { SELECT a FROM d5 UNION ALL SELECT c FROM d6 UNION ALL SELECT e FROM d7
  1729. ORDER BY 1
  1730. } {1 2 3 4 5 6}
  1731. 5 { SELECT a, b FROM d5 UNION ALL SELECT b, a FROM d5 ORDER BY b }
  1732. {f 1 c 4 4 c 1 f}
  1733. 6 { SELECT a, b FROM d5 UNION ALL SELECT b, a FROM d5 ORDER BY 2 }
  1734. {f 1 c 4 4 c 1 f}
  1735. 7 { SELECT a, b FROM d5 UNION ALL SELECT b, a FROM d5 ORDER BY a }
  1736. {1 f 4 c c 4 f 1}
  1737. 8 { SELECT a, b FROM d5 UNION ALL SELECT b, a FROM d5 ORDER BY 1 }
  1738. {1 f 4 c c 4 f 1}
  1739. 9 { SELECT a, b FROM d5 UNION ALL SELECT b, a+1 FROM d5 ORDER BY a+1 }
  1740. {f 2 c 5 4 c 1 f}
  1741. 10 { SELECT a, b FROM d5 UNION ALL SELECT b, a+1 FROM d5 ORDER BY 2 }
  1742. {f 2 c 5 4 c 1 f}
  1743. 11 { SELECT a+1, b FROM d5 UNION ALL SELECT b, a+1 FROM d5 ORDER BY a+1 }
  1744. {2 f 5 c c 5 f 2}
  1745. 12 { SELECT a+1, b FROM d5 UNION ALL SELECT b, a+1 FROM d5 ORDER BY 1 }
  1746. {2 f 5 c c 5 f 2}
  1747. }
  1748. # EVIDENCE-OF: R-39265-04070 If no matching expression can be found in
  1749. # the result columns of any constituent SELECT, it is an error.
  1750. #
  1751. do_select_tests e_select-8.14 -error {
  1752. %s ORDER BY term does not match any column in the result set
  1753. } {
  1754. 1 { SELECT a FROM d5 UNION SELECT c FROM d6 ORDER BY a+1 } 1st
  1755. 2 { SELECT a FROM d5 UNION SELECT c FROM d6 ORDER BY a, a+1 } 2nd
  1756. 3 { SELECT * FROM d5 INTERSECT SELECT * FROM d6 ORDER BY 'hello' } 1st
  1757. 4 { SELECT * FROM d5 INTERSECT SELECT * FROM d6 ORDER BY blah } 1st
  1758. 5 { SELECT * FROM d5 INTERSECT SELECT * FROM d6 ORDER BY c,d,c+d } 3rd
  1759. 6 { SELECT * FROM d5 EXCEPT SELECT * FROM d7 ORDER BY 1,2,b,a/b } 4th
  1760. }
  1761. # EVIDENCE-OF: R-03407-11483 Each term of the ORDER BY clause is
  1762. # processed separately and may be matched against result columns from
  1763. # different SELECT statements in the compound.
  1764. #
  1765. do_select_tests e_select-8.15 {
  1766. 1 { SELECT a, b FROM d5 UNION ALL SELECT c-1, d FROM d6 ORDER BY a, d }
  1767. {1 e 1 f 4 b 4 c}
  1768. 2 { SELECT a, b FROM d5 UNION ALL SELECT c-1, d FROM d6 ORDER BY c-1, b }
  1769. {1 e 1 f 4 b 4 c}
  1770. 3 { SELECT a, b FROM d5 UNION ALL SELECT c-1, d FROM d6 ORDER BY 1, 2 }
  1771. {1 e 1 f 4 b 4 c}
  1772. }
  1773. #-------------------------------------------------------------------------
  1774. # Tests related to statements made about the LIMIT/OFFSET clause.
  1775. #
  1776. do_execsql_test e_select-9.0 {
  1777. CREATE TABLE f1(a, b);
  1778. INSERT INTO f1 VALUES(26, 'z');
  1779. INSERT INTO f1 VALUES(25, 'y');
  1780. INSERT INTO f1 VALUES(24, 'x');
  1781. INSERT INTO f1 VALUES(23, 'w');
  1782. INSERT INTO f1 VALUES(22, 'v');
  1783. INSERT INTO f1 VALUES(21, 'u');
  1784. INSERT INTO f1 VALUES(20, 't');
  1785. INSERT INTO f1 VALUES(19, 's');
  1786. INSERT INTO f1 VALUES(18, 'r');
  1787. INSERT INTO f1 VALUES(17, 'q');
  1788. INSERT INTO f1 VALUES(16, 'p');
  1789. INSERT INTO f1 VALUES(15, 'o');
  1790. INSERT INTO f1 VALUES(14, 'n');
  1791. INSERT INTO f1 VALUES(13, 'm');
  1792. INSERT INTO f1 VALUES(12, 'l');
  1793. INSERT INTO f1 VALUES(11, 'k');
  1794. INSERT INTO f1 VALUES(10, 'j');
  1795. INSERT INTO f1 VALUES(9, 'i');
  1796. INSERT INTO f1 VALUES(8, 'h');
  1797. INSERT INTO f1 VALUES(7, 'g');
  1798. INSERT INTO f1 VALUES(6, 'f');
  1799. INSERT INTO f1 VALUES(5, 'e');
  1800. INSERT INTO f1 VALUES(4, 'd');
  1801. INSERT INTO f1 VALUES(3, 'c');
  1802. INSERT INTO f1 VALUES(2, 'b');
  1803. INSERT INTO f1 VALUES(1, 'a');
  1804. } {}
  1805. # EVIDENCE-OF: R-30481-56627 Any scalar expression may be used in the
  1806. # LIMIT clause, so long as it evaluates to an integer or a value that
  1807. # can be losslessly converted to an integer.
  1808. #
  1809. do_select_tests e_select-9.1 {
  1810. 1 { SELECT b FROM f1 ORDER BY a LIMIT 5 } {a b c d e}
  1811. 2 { SELECT b FROM f1 ORDER BY a LIMIT 2+3 } {a b c d e}
  1812. 3 { SELECT b FROM f1 ORDER BY a LIMIT (SELECT a FROM f1 WHERE b = 'e') }
  1813. {a b c d e}
  1814. 4 { SELECT b FROM f1 ORDER BY a LIMIT 5.0 } {a b c d e}
  1815. 5 { SELECT b FROM f1 ORDER BY a LIMIT '5' } {a b c d e}
  1816. }
  1817. # EVIDENCE-OF: R-46155-47219 If the expression evaluates to a NULL value
  1818. # or any other value that cannot be losslessly converted to an integer,
  1819. # an error is returned.
  1820. #
  1821. do_select_tests e_select-9.2 -error "datatype mismatch" {
  1822. 1 { SELECT b FROM f1 ORDER BY a LIMIT 'hello' } {}
  1823. 2 { SELECT b FROM f1 ORDER BY a LIMIT NULL } {}
  1824. 3 { SELECT b FROM f1 ORDER BY a LIMIT X'ABCD' } {}
  1825. 4 { SELECT b FROM f1 ORDER BY a LIMIT 5.1 } {}
  1826. 5 { SELECT b FROM f1 ORDER BY a LIMIT (SELECT group_concat(b) FROM f1) } {}
  1827. }
  1828. # EVIDENCE-OF: R-03014-26414 If the LIMIT expression evaluates to a
  1829. # negative value, then there is no upper bound on the number of rows
  1830. # returned.
  1831. #
  1832. do_select_tests e_select-9.4 {
  1833. 1 { SELECT b FROM f1 ORDER BY a LIMIT -1 }
  1834. {a b c d e f g h i j k l m n o p q r s t u v w x y z}
  1835. 2 { SELECT b FROM f1 ORDER BY a LIMIT length('abc')-100 }
  1836. {a b c d e f g h i j k l m n o p q r s t u v w x y z}
  1837. 3 { SELECT b FROM f1 ORDER BY a LIMIT (SELECT count(*) FROM f1)/2 - 14 }
  1838. {a b c d e f g h i j k l m n o p q r s t u v w x y z}
  1839. }
  1840. # EVIDENCE-OF: R-33750-29536 Otherwise, the SELECT returns the first N
  1841. # rows of its result set only, where N is the value that the LIMIT
  1842. # expression evaluates to.
  1843. #
  1844. do_select_tests e_select-9.5 {
  1845. 1 { SELECT b FROM f1 ORDER BY a LIMIT 0 } {}
  1846. 2 { SELECT b FROM f1 ORDER BY a DESC LIMIT 4 } {z y x w}
  1847. 3 { SELECT b FROM f1 ORDER BY a DESC LIMIT 8 } {z y x w v u t s}
  1848. 4 { SELECT b FROM f1 ORDER BY a DESC LIMIT '12.0' } {z y x w v u t s r q p o}
  1849. }
  1850. # EVIDENCE-OF: R-54935-19057 Or, if the SELECT statement would return
  1851. # less than N rows without a LIMIT clause, then the entire result set is
  1852. # returned.
  1853. #
  1854. do_select_tests e_select-9.6 {
  1855. 1 { SELECT b FROM f1 WHERE a>21 ORDER BY a LIMIT 10 } {v w x y z}
  1856. 2 { SELECT count(*) FROM f1 GROUP BY a/5 ORDER BY 1 LIMIT 10 } {2 4 5 5 5 5}
  1857. }
  1858. # EVIDENCE-OF: R-24188-24349 The expression attached to the optional
  1859. # OFFSET clause that may follow a LIMIT clause must also evaluate to an
  1860. # integer, or a value that can be losslessly converted to an integer.
  1861. #
  1862. foreach {tn select} {
  1863. 1 { SELECT b FROM f1 ORDER BY a LIMIT 2 OFFSET 'hello' }
  1864. 2 { SELECT b FROM f1 ORDER BY a LIMIT 2 OFFSET NULL }
  1865. 3 { SELECT b FROM f1 ORDER BY a LIMIT 2 OFFSET X'ABCD' }
  1866. 4 { SELECT b FROM f1 ORDER BY a LIMIT 2 OFFSET 5.1 }
  1867. 5 { SELECT b FROM f1 ORDER BY a
  1868. LIMIT 2 OFFSET (SELECT group_concat(b) FROM f1)
  1869. }
  1870. } {
  1871. do_catchsql_test e_select-9.7.$tn $select {1 {datatype mismatch}}
  1872. }
  1873. # EVIDENCE-OF: R-20467-43422 If an expression has an OFFSET clause, then
  1874. # the first M rows are omitted from the result set returned by the
  1875. # SELECT statement and the next N rows are returned, where M and N are
  1876. # the values that the OFFSET and LIMIT clauses evaluate to,
  1877. # respectively.
  1878. #
  1879. do_select_tests e_select-9.8 {
  1880. 1 { SELECT b FROM f1 ORDER BY a LIMIT 10 OFFSET 5} {f g h i j k l m n o}
  1881. 2 { SELECT b FROM f1 ORDER BY a LIMIT 2+3 OFFSET 10} {k l m n o}
  1882. 3 { SELECT b FROM f1 ORDER BY a
  1883. LIMIT (SELECT a FROM f1 WHERE b='j')
  1884. OFFSET (SELECT a FROM f1 WHERE b='b')
  1885. } {c d e f g h i j k l}
  1886. 4 { SELECT b FROM f1 ORDER BY a LIMIT '5' OFFSET 3.0 } {d e f g h}
  1887. 5 { SELECT b FROM f1 ORDER BY a LIMIT '5' OFFSET 0 } {a b c d e}
  1888. 6 { SELECT b FROM f1 ORDER BY a LIMIT 0 OFFSET 10 } {}
  1889. 7 { SELECT b FROM f1 ORDER BY a LIMIT 3 OFFSET '1'||'5' } {p q r}
  1890. }
  1891. # EVIDENCE-OF: R-34648-44875 Or, if the SELECT would return less than
  1892. # M+N rows if it did not have a LIMIT clause, then the first M rows are
  1893. # skipped and the remaining rows (if any) are returned.
  1894. #
  1895. do_select_tests e_select-9.9 {
  1896. 1 { SELECT b FROM f1 ORDER BY a LIMIT 10 OFFSET 20} {u v w x y z}
  1897. 2 { SELECT a FROM f1 ORDER BY a DESC LIMIT 100 OFFSET 18+4} {4 3 2 1}
  1898. }
  1899. # EVIDENCE-OF: R-23293-62447 If the OFFSET clause evaluates to a
  1900. # negative value, the results are the same as if it had evaluated to
  1901. # zero.
  1902. #
  1903. do_select_tests e_select-9.10 {
  1904. 1 { SELECT b FROM f1 ORDER BY a LIMIT 5 OFFSET -1 } {a b c d e}
  1905. 2 { SELECT b FROM f1 ORDER BY a LIMIT 5 OFFSET -500 } {a b c d e}
  1906. 3 { SELECT b FROM f1 ORDER BY a LIMIT 5 OFFSET 0 } {a b c d e}
  1907. }
  1908. # EVIDENCE-OF: R-19509-40356 Instead of a separate OFFSET clause, the
  1909. # LIMIT clause may specify two scalar expressions separated by a comma.
  1910. #
  1911. # EVIDENCE-OF: R-33788-46243 In this case, the first expression is used
  1912. # as the OFFSET expression and the second as the LIMIT expression.
  1913. #
  1914. do_select_tests e_select-9.11 {
  1915. 1 { SELECT b FROM f1 ORDER BY a LIMIT 5, 10 } {f g h i j k l m n o}
  1916. 2 { SELECT b FROM f1 ORDER BY a LIMIT 10, 2+3 } {k l m n o}
  1917. 3 { SELECT b FROM f1 ORDER BY a
  1918. LIMIT (SELECT a FROM f1 WHERE b='b'), (SELECT a FROM f1 WHERE b='j')
  1919. } {c d e f g h i j k l}
  1920. 4 { SELECT b FROM f1 ORDER BY a LIMIT 3.0, '5' } {d e f g h}
  1921. 5 { SELECT b FROM f1 ORDER BY a LIMIT 0, '5' } {a b c d e}
  1922. 6 { SELECT b FROM f1 ORDER BY a LIMIT 10, 0 } {}
  1923. 7 { SELECT b FROM f1 ORDER BY a LIMIT '1'||'5', 3 } {p q r}
  1924. 8 { SELECT b FROM f1 ORDER BY a LIMIT 20, 10 } {u v w x y z}
  1925. 9 { SELECT a FROM f1 ORDER BY a DESC LIMIT 18+4, 100 } {4 3 2 1}
  1926. 10 { SELECT b FROM f1 ORDER BY a LIMIT -1, 5 } {a b c d e}
  1927. 11 { SELECT b FROM f1 ORDER BY a LIMIT -500, 5 } {a b c d e}
  1928. 12 { SELECT b FROM f1 ORDER BY a LIMIT 0, 5 } {a b c d e}
  1929. }
  1930. finish_test