select1.test 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. # 2001 September 15
  2. #
  3. # The author disclaims copyright to this source code. In place of
  4. # a legal notice, here is a blessing:
  5. #
  6. # May you do good and not evil.
  7. # May you find forgiveness for yourself and forgive others.
  8. # May you share freely, never taking more than you give.
  9. #
  10. #***********************************************************************
  11. # This file implements regression tests for SQLite library. The
  12. # focus of this file is testing the SELECT statement.
  13. #
  14. # $Id: select1.test,v 1.30 2002/06/02 16:09:03 drh Exp $
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. # Try to select on a non-existant table.
  18. #
  19. do_test select1-1.1 {
  20. set v [catch {execsql {SELECT * FROM test1}} msg]
  21. lappend v $msg
  22. } {1 {no such table: test1}}
  23. execsql {CREATE TABLE test1(f1 int, f2 int)}
  24. do_test select1-1.2 {
  25. set v [catch {execsql {SELECT * FROM test1, test2}} msg]
  26. lappend v $msg
  27. } {1 {no such table: test2}}
  28. do_test select1-1.3 {
  29. set v [catch {execsql {SELECT * FROM test2, test1}} msg]
  30. lappend v $msg
  31. } {1 {no such table: test2}}
  32. execsql {INSERT INTO test1(f1,f2) VALUES(11,22)}
  33. # Make sure the columns are extracted correctly.
  34. #
  35. do_test select1-1.4 {
  36. execsql {SELECT f1 FROM test1}
  37. } {11}
  38. do_test select1-1.5 {
  39. execsql {SELECT f2 FROM test1}
  40. } {22}
  41. do_test select1-1.6 {
  42. execsql {SELECT f2, f1 FROM test1}
  43. } {22 11}
  44. do_test select1-1.7 {
  45. execsql {SELECT f1, f2 FROM test1}
  46. } {11 22}
  47. do_test select1-1.8 {
  48. execsql {SELECT * FROM test1}
  49. } {11 22}
  50. do_test select1-1.8.1 {
  51. execsql {SELECT *, * FROM test1}
  52. } {11 22 11 22}
  53. do_test select1-1.8.2 {
  54. execsql {SELECT *, min(f1,f2), max(f1,f2) FROM test1}
  55. } {11 22 11 22}
  56. do_test select1-1.8.3 {
  57. execsql {SELECT 'one', *, 'two', * FROM test1}
  58. } {one 11 22 two 11 22}
  59. execsql {CREATE TABLE test2(r1 real, r2 real)}
  60. execsql {INSERT INTO test2(r1,r2) VALUES(1.1,2.2)}
  61. do_test select1-1.9 {
  62. execsql {SELECT * FROM test1, test2}
  63. } {11 22 1.1 2.2}
  64. do_test select1-1.9.1 {
  65. execsql {SELECT *, 'hi' FROM test1, test2}
  66. } {11 22 1.1 2.2 hi}
  67. do_test select1-1.9.2 {
  68. execsql {SELECT 'one', *, 'two', * FROM test1, test2}
  69. } {one 11 22 1.1 2.2 two 11 22 1.1 2.2}
  70. do_test select1-1.10 {
  71. execsql {SELECT test1.f1, test2.r1 FROM test1, test2}
  72. } {11 1.1}
  73. do_test select1-1.11 {
  74. execsql {SELECT test1.f1, test2.r1 FROM test2, test1}
  75. } {11 1.1}
  76. do_test select1-1.11.1 {
  77. execsql {SELECT * FROM test2, test1}
  78. } {1.1 2.2 11 22}
  79. do_test select1-1.11.2 {
  80. execsql {SELECT * FROM test1 AS a, test1 AS b}
  81. } {11 22 11 22}
  82. do_test select1-1.12 {
  83. execsql {SELECT max(test1.f1,test2.r1), min(test1.f2,test2.r2)
  84. FROM test2, test1}
  85. } {11 2.2}
  86. do_test select1-1.13 {
  87. execsql {SELECT min(test1.f1,test2.r1), max(test1.f2,test2.r2)
  88. FROM test1, test2}
  89. } {1.1 22}
  90. set long {This is a string that is too big to fit inside a NBFS buffer}
  91. do_test select1-2.0 {
  92. execsql "
  93. DROP TABLE test2;
  94. DELETE FROM test1;
  95. INSERT INTO test1 VALUES(11,22);
  96. INSERT INTO test1 VALUES(33,44);
  97. CREATE TABLE t3(a,b);
  98. INSERT INTO t3 VALUES('abc',NULL);
  99. INSERT INTO t3 VALUES(NULL,'xyz');
  100. INSERT INTO t3 SELECT * FROM test1;
  101. CREATE TABLE t4(a,b);
  102. INSERT INTO t4 VALUES(NULL,'$long');
  103. SELECT * FROM t3;
  104. "
  105. } {abc {} {} xyz 11 22 33 44}
  106. # Error messges from sqliteExprCheck
  107. #
  108. do_test select1-2.1 {
  109. set v [catch {execsql {SELECT count(f1,f2) FROM test1}} msg]
  110. lappend v $msg
  111. } {1 {wrong number of arguments to function count()}}
  112. do_test select1-2.2 {
  113. set v [catch {execsql {SELECT count(f1) FROM test1}} msg]
  114. lappend v $msg
  115. } {0 2}
  116. do_test select1-2.3 {
  117. set v [catch {execsql {SELECT Count() FROM test1}} msg]
  118. lappend v $msg
  119. } {0 2}
  120. do_test select1-2.4 {
  121. set v [catch {execsql {SELECT COUNT(*) FROM test1}} msg]
  122. lappend v $msg
  123. } {0 2}
  124. do_test select1-2.5 {
  125. set v [catch {execsql {SELECT COUNT(*)+1 FROM test1}} msg]
  126. lappend v $msg
  127. } {0 3}
  128. do_test select1-2.5.1 {
  129. execsql {SELECT count(*),count(a),count(b) FROM t3}
  130. } {4 3 3}
  131. do_test select1-2.5.2 {
  132. execsql {SELECT count(*),count(a),count(b) FROM t4}
  133. } {1 0 1}
  134. do_test select1-2.5.3 {
  135. execsql {SELECT count(*),count(a),count(b) FROM t4 WHERE b=5}
  136. } {0 0 0}
  137. do_test select1-2.6 {
  138. set v [catch {execsql {SELECT min(*) FROM test1}} msg]
  139. lappend v $msg
  140. } {1 {wrong number of arguments to function min()}}
  141. do_test select1-2.7 {
  142. set v [catch {execsql {SELECT Min(f1) FROM test1}} msg]
  143. lappend v $msg
  144. } {0 11}
  145. do_test select1-2.8 {
  146. set v [catch {execsql {SELECT MIN(f1,f2) FROM test1}} msg]
  147. lappend v [lsort $msg]
  148. } {0 {11 33}}
  149. do_test select1-2.8.1 {
  150. execsql {SELECT coalesce(min(a),'xyzzy') FROM t3}
  151. } {11}
  152. do_test select1-2.8.2 {
  153. execsql {SELECT min(coalesce(a,'xyzzy')) FROM t3}
  154. } {11}
  155. do_test select1-2.8.3 {
  156. execsql {SELECT min(b), min(b) FROM t4}
  157. } [list $long $long]
  158. do_test select1-2.9 {
  159. set v [catch {execsql {SELECT MAX(*) FROM test1}} msg]
  160. lappend v $msg
  161. } {1 {wrong number of arguments to function MAX()}}
  162. do_test select1-2.10 {
  163. set v [catch {execsql {SELECT Max(f1) FROM test1}} msg]
  164. lappend v $msg
  165. } {0 33}
  166. do_test select1-2.11 {
  167. set v [catch {execsql {SELECT max(f1,f2) FROM test1}} msg]
  168. lappend v [lsort $msg]
  169. } {0 {22 44}}
  170. do_test select1-2.12 {
  171. set v [catch {execsql {SELECT MAX(f1,f2)+1 FROM test1}} msg]
  172. lappend v [lsort $msg]
  173. } {0 {23 45}}
  174. do_test select1-2.13 {
  175. set v [catch {execsql {SELECT MAX(f1)+1 FROM test1}} msg]
  176. lappend v $msg
  177. } {0 34}
  178. do_test select1-2.13.1 {
  179. execsql {SELECT coalesce(max(a),'xyzzy') FROM t3}
  180. } {abc}
  181. do_test select1-2.13.2 {
  182. execsql {SELECT max(coalesce(a,'xyzzy')) FROM t3}
  183. } {xyzzy}
  184. do_test select1-2.14 {
  185. set v [catch {execsql {SELECT SUM(*) FROM test1}} msg]
  186. lappend v $msg
  187. } {1 {wrong number of arguments to function SUM()}}
  188. do_test select1-2.15 {
  189. set v [catch {execsql {SELECT Sum(f1) FROM test1}} msg]
  190. lappend v $msg
  191. } {0 44}
  192. do_test select1-2.16 {
  193. set v [catch {execsql {SELECT sum(f1,f2) FROM test1}} msg]
  194. lappend v $msg
  195. } {1 {wrong number of arguments to function sum()}}
  196. do_test select1-2.17 {
  197. set v [catch {execsql {SELECT SUM(f1)+1 FROM test1}} msg]
  198. lappend v $msg
  199. } {0 45}
  200. do_test select1-2.17.1 {
  201. execsql {SELECT sum(a) FROM t3}
  202. } {44}
  203. do_test select1-2.18 {
  204. set v [catch {execsql {SELECT XYZZY(f1) FROM test1}} msg]
  205. lappend v $msg
  206. } {1 {no such function: XYZZY}}
  207. do_test select1-2.19 {
  208. set v [catch {execsql {SELECT SUM(min(f1,f2)) FROM test1}} msg]
  209. lappend v $msg
  210. } {0 44}
  211. do_test select1-2.20 {
  212. set v [catch {execsql {SELECT SUM(min(f1)) FROM test1}} msg]
  213. lappend v $msg
  214. } {1 {misuse of aggregate function min()}}
  215. # WHERE clause expressions
  216. #
  217. do_test select1-3.1 {
  218. set v [catch {execsql {SELECT f1 FROM test1 WHERE f1<11}} msg]
  219. lappend v $msg
  220. } {0 {}}
  221. do_test select1-3.2 {
  222. set v [catch {execsql {SELECT f1 FROM test1 WHERE f1<=11}} msg]
  223. lappend v $msg
  224. } {0 11}
  225. do_test select1-3.3 {
  226. set v [catch {execsql {SELECT f1 FROM test1 WHERE f1=11}} msg]
  227. lappend v $msg
  228. } {0 11}
  229. do_test select1-3.4 {
  230. set v [catch {execsql {SELECT f1 FROM test1 WHERE f1>=11}} msg]
  231. lappend v [lsort $msg]
  232. } {0 {11 33}}
  233. do_test select1-3.5 {
  234. set v [catch {execsql {SELECT f1 FROM test1 WHERE f1>11}} msg]
  235. lappend v [lsort $msg]
  236. } {0 33}
  237. do_test select1-3.6 {
  238. set v [catch {execsql {SELECT f1 FROM test1 WHERE f1!=11}} msg]
  239. lappend v [lsort $msg]
  240. } {0 33}
  241. do_test select1-3.7 {
  242. set v [catch {execsql {SELECT f1 FROM test1 WHERE min(f1,f2)!=11}} msg]
  243. lappend v [lsort $msg]
  244. } {0 33}
  245. do_test select1-3.8 {
  246. set v [catch {execsql {SELECT f1 FROM test1 WHERE max(f1,f2)!=11}} msg]
  247. lappend v [lsort $msg]
  248. } {0 {11 33}}
  249. do_test select1-3.9 {
  250. set v [catch {execsql {SELECT f1 FROM test1 WHERE count(f1,f2)!=11}} msg]
  251. lappend v $msg
  252. } {1 {wrong number of arguments to function count()}}
  253. # ORDER BY expressions
  254. #
  255. do_test select1-4.1 {
  256. set v [catch {execsql {SELECT f1 FROM test1 ORDER BY f1}} msg]
  257. lappend v $msg
  258. } {0 {11 33}}
  259. do_test select1-4.2 {
  260. set v [catch {execsql {SELECT f1 FROM test1 ORDER BY -f1}} msg]
  261. lappend v $msg
  262. } {0 {33 11}}
  263. do_test select1-4.3 {
  264. set v [catch {execsql {SELECT f1 FROM test1 ORDER BY min(f1,f2)}} msg]
  265. lappend v $msg
  266. } {0 {11 33}}
  267. do_test select1-4.4 {
  268. set v [catch {execsql {SELECT f1 FROM test1 ORDER BY min(f1)}} msg]
  269. lappend v $msg
  270. } {1 {misuse of aggregate function min()}}
  271. do_test select1-4.5 {
  272. catchsql {
  273. SELECT f1 FROM test1 ORDER BY 8.4;
  274. }
  275. } {1 {ORDER BY terms must not be non-integer constants}}
  276. do_test select1-4.6 {
  277. catchsql {
  278. SELECT f1 FROM test1 ORDER BY '8.4';
  279. }
  280. } {1 {ORDER BY terms must not be non-integer constants}}
  281. do_test select1-4.7 {
  282. catchsql {
  283. SELECT f1 FROM test1 ORDER BY 'xyz';
  284. }
  285. } {1 {ORDER BY terms must not be non-integer constants}}
  286. do_test select1-4.8 {
  287. execsql {
  288. CREATE TABLE t5(a,b);
  289. INSERT INTO t5 VALUES(1,10);
  290. INSERT INTO t5 VALUES(2,9);
  291. SELECT * FROM t5 ORDER BY 1;
  292. }
  293. } {1 10 2 9}
  294. do_test select1-4.9 {
  295. execsql {
  296. SELECT * FROM t5 ORDER BY 2;
  297. }
  298. } {2 9 1 10}
  299. do_test select1-4.10 {
  300. catchsql {
  301. SELECT * FROM t5 ORDER BY 3;
  302. }
  303. } {1 {ORDER BY column number 3 out of range - should be between 1 and 2}}
  304. do_test select1-4.11 {
  305. execsql {
  306. INSERT INTO t5 VALUES(3,10);
  307. SELECT * FROM t5 ORDER BY 2, 1 DESC;
  308. }
  309. } {2 9 3 10 1 10}
  310. do_test select1-4.12 {
  311. execsql {
  312. SELECT * FROM t5 ORDER BY 1 DESC, b;
  313. }
  314. } {3 10 2 9 1 10}
  315. do_test select1-4.13 {
  316. execsql {
  317. SELECT * FROM t5 ORDER BY b DESC, 1;
  318. }
  319. } {1 10 3 10 2 9}
  320. # ORDER BY ignored on an aggregate query
  321. #
  322. do_test select1-5.1 {
  323. set v [catch {execsql {SELECT max(f1) FROM test1 ORDER BY f2}} msg]
  324. lappend v $msg
  325. } {0 33}
  326. execsql {CREATE TABLE test2(t1 test, t2 text)}
  327. execsql {INSERT INTO test2 VALUES('abc','xyz')}
  328. # Check for column naming
  329. #
  330. do_test select1-6.1 {
  331. set v [catch {execsql2 {SELECT f1 FROM test1 ORDER BY f2}} msg]
  332. lappend v $msg
  333. } {0 {f1 11 f1 33}}
  334. do_test select1-6.1.1 {
  335. execsql {PRAGMA full_column_names=on}
  336. set v [catch {execsql2 {SELECT f1 FROM test1 ORDER BY f2}} msg]
  337. lappend v $msg
  338. } {0 {test1.f1 11 test1.f1 33}}
  339. do_test select1-6.1.2 {
  340. set v [catch {execsql2 {SELECT f1 as 'f1' FROM test1 ORDER BY f2}} msg]
  341. lappend v $msg
  342. } {0 {f1 11 f1 33}}
  343. do_test select1-6.1.3 {
  344. set v [catch {execsql2 {SELECT * FROM test1 WHERE f1==11}} msg]
  345. lappend v $msg
  346. } {0 {test1.f1 11 test1.f2 22}}
  347. do_test select1-6.1.4 {
  348. set v [catch {execsql2 {SELECT DISTINCT * FROM test1 WHERE f1==11}} msg]
  349. execsql {PRAGMA full_column_names=off}
  350. lappend v $msg
  351. } {0 {test1.f1 11 test1.f2 22}}
  352. do_test select1-6.1.5 {
  353. set v [catch {execsql2 {SELECT * FROM test1 WHERE f1==11}} msg]
  354. lappend v $msg
  355. } {0 {f1 11 f2 22}}
  356. do_test select1-6.1.6 {
  357. set v [catch {execsql2 {SELECT DISTINCT * FROM test1 WHERE f1==11}} msg]
  358. lappend v $msg
  359. } {0 {f1 11 f2 22}}
  360. do_test select1-6.2 {
  361. set v [catch {execsql2 {SELECT f1 as xyzzy FROM test1 ORDER BY f2}} msg]
  362. lappend v $msg
  363. } {0 {xyzzy 11 xyzzy 33}}
  364. do_test select1-6.3 {
  365. set v [catch {execsql2 {SELECT f1 as "xyzzy" FROM test1 ORDER BY f2}} msg]
  366. lappend v $msg
  367. } {0 {xyzzy 11 xyzzy 33}}
  368. do_test select1-6.3.1 {
  369. set v [catch {execsql2 {SELECT f1 as 'xyzzy ' FROM test1 ORDER BY f2}} msg]
  370. lappend v $msg
  371. } {0 {{xyzzy } 11 {xyzzy } 33}}
  372. do_test select1-6.4 {
  373. set v [catch {execsql2 {SELECT f1+F2 as xyzzy FROM test1 ORDER BY f2}} msg]
  374. lappend v $msg
  375. } {0 {xyzzy 33 xyzzy 77}}
  376. do_test select1-6.4a {
  377. set v [catch {execsql2 {SELECT f1+F2 FROM test1 ORDER BY f2}} msg]
  378. lappend v $msg
  379. } {0 {f1+F2 33 f1+F2 77}}
  380. do_test select1-6.5 {
  381. set v [catch {execsql2 {SELECT test1.f1+F2 FROM test1 ORDER BY f2}} msg]
  382. lappend v $msg
  383. } {0 {test1.f1+F2 33 test1.f1+F2 77}}
  384. do_test select1-6.5.1 {
  385. execsql2 {PRAGMA full_column_names=on}
  386. set v [catch {execsql2 {SELECT test1.f1+F2 FROM test1 ORDER BY f2}} msg]
  387. execsql2 {PRAGMA full_column_names=off}
  388. lappend v $msg
  389. } {0 {test1.f1+F2 33 test1.f1+F2 77}}
  390. do_test select1-6.6 {
  391. set v [catch {execsql2 {SELECT test1.f1+F2, t1 FROM test1, test2
  392. ORDER BY f2}} msg]
  393. lappend v $msg
  394. } {0 {test1.f1+F2 33 t1 abc test1.f1+F2 77 t1 abc}}
  395. do_test select1-6.7 {
  396. set v [catch {execsql2 {SELECT A.f1, t1 FROM test1 as A, test2
  397. ORDER BY f2}} msg]
  398. lappend v $msg
  399. } {0 {A.f1 11 t1 abc A.f1 33 t1 abc}}
  400. do_test select1-6.8 {
  401. set v [catch {execsql2 {SELECT A.f1, f1 FROM test1 as A, test1 as B
  402. ORDER BY f2}} msg]
  403. lappend v $msg
  404. } {1 {ambiguous column name: f1}}
  405. do_test select1-6.8b {
  406. set v [catch {execsql2 {SELECT A.f1, B.f1 FROM test1 as A, test1 as B
  407. ORDER BY f2}} msg]
  408. lappend v $msg
  409. } {1 {ambiguous column name: f2}}
  410. do_test select1-6.8c {
  411. set v [catch {execsql2 {SELECT A.f1, f1 FROM test1 as A, test1 as A
  412. ORDER BY f2}} msg]
  413. lappend v $msg
  414. } {1 {ambiguous column name: A.f1}}
  415. do_test select1-6.9 {
  416. set v [catch {execsql2 {SELECT A.f1, B.f1 FROM test1 as A, test1 as B
  417. ORDER BY A.f1, B.f1}} msg]
  418. lappend v $msg
  419. } {0 {A.f1 11 B.f1 11 A.f1 11 B.f1 33 A.f1 33 B.f1 11 A.f1 33 B.f1 33}}
  420. do_test select1-6.10 {
  421. set v [catch {execsql2 {
  422. SELECT f1 FROM test1 UNION SELECT f2 FROM test1
  423. ORDER BY f2;
  424. }} msg]
  425. lappend v $msg
  426. } {0 {f2 11 f2 22 f2 33 f2 44}}
  427. do_test select1-6.11 {
  428. set v [catch {execsql2 {
  429. SELECT f1 FROM test1 UNION SELECT f2+100 FROM test1
  430. ORDER BY f2+100;
  431. }} msg]
  432. lappend v $msg
  433. } {0 {f2+100 11 f2+100 33 f2+100 122 f2+100 144}}
  434. do_test select1-7.1 {
  435. set v [catch {execsql {
  436. SELECT f1 FROM test1 WHERE f2=;
  437. }} msg]
  438. lappend v $msg
  439. } {1 {near ";": syntax error}}
  440. do_test select1-7.2 {
  441. set v [catch {execsql {
  442. SELECT f1 FROM test1 UNION SELECT WHERE;
  443. }} msg]
  444. lappend v $msg
  445. } {1 {near "WHERE": syntax error}}
  446. do_test select1-7.3 {
  447. set v [catch {execsql {SELECT f1 FROM test1 as 'hi', test2 as}} msg]
  448. lappend v $msg
  449. } {1 {near "as": syntax error}}
  450. do_test select1-7.4 {
  451. set v [catch {execsql {
  452. SELECT f1 FROM test1 ORDER BY;
  453. }} msg]
  454. lappend v $msg
  455. } {1 {near ";": syntax error}}
  456. do_test select1-7.5 {
  457. set v [catch {execsql {
  458. SELECT f1 FROM test1 ORDER BY f1 desc, f2 where;
  459. }} msg]
  460. lappend v $msg
  461. } {1 {near "where": syntax error}}
  462. do_test select1-7.6 {
  463. set v [catch {execsql {
  464. SELECT count(f1,f2 FROM test1;
  465. }} msg]
  466. lappend v $msg
  467. } {1 {near "FROM": syntax error}}
  468. do_test select1-7.7 {
  469. set v [catch {execsql {
  470. SELECT count(f1,f2+) FROM test1;
  471. }} msg]
  472. lappend v $msg
  473. } {1 {near ")": syntax error}}
  474. do_test select1-7.8 {
  475. set v [catch {execsql {
  476. SELECT f1 FROM test1 ORDER BY f2, f1+;
  477. }} msg]
  478. lappend v $msg
  479. } {1 {near ";": syntax error}}
  480. do_test select1-8.1 {
  481. execsql {SELECT f1 FROM test1 WHERE 4.3+2.4 OR 1 ORDER BY f1}
  482. } {11 33}
  483. do_test select1-8.2 {
  484. execsql {
  485. SELECT f1 FROM test1 WHERE ('x' || f1) BETWEEN 'x10' AND 'x20'
  486. ORDER BY f1
  487. }
  488. } {11}
  489. do_test select1-8.3 {
  490. execsql {
  491. SELECT f1 FROM test1 WHERE 5-3==2
  492. ORDER BY f1
  493. }
  494. } {11 33}
  495. do_test select1-8.4 {
  496. execsql {
  497. SELECT coalesce(f1/(f1-11),'x'),
  498. coalesce(min(f1/(f1-11),5),'y'),
  499. coalesce(max(f1/(f1-33),6),'z')
  500. FROM test1 ORDER BY f1
  501. }
  502. } {x y 6 1.5 1.5 z}
  503. do_test select1-8.5 {
  504. execsql {
  505. SELECT min(1,2,3), -max(1,2,3)
  506. FROM test1 ORDER BY f1
  507. }
  508. } {1 -3 1 -3}
  509. # Check the behavior when the result set is empty
  510. #
  511. do_test select1-9.1 {
  512. catch {unset r}
  513. set r(*) {}
  514. db eval {SELECT * FROM test1 WHERE f1<0} r {}
  515. set r(*)
  516. } {}
  517. do_test select1-9.2 {
  518. execsql {PRAGMA empty_result_callbacks=on}
  519. set r(*) {}
  520. db eval {SELECT * FROM test1 WHERE f1<0} r {}
  521. set r(*)
  522. } {f1 f2}
  523. do_test select1-9.3 {
  524. set r(*) {}
  525. db eval {SELECT * FROM test1 WHERE f1<(select count(*) from test2)} r {}
  526. set r(*)
  527. } {f1 f2}
  528. do_test select1-9.4 {
  529. set r(*) {}
  530. db eval {SELECT * FROM test1 ORDER BY f1} r {}
  531. set r(*)
  532. } {f1 f2}
  533. do_test select1-9.5 {
  534. set r(*) {}
  535. db eval {SELECT * FROM test1 WHERE f1<0 ORDER BY f1} r {}
  536. set r(*)
  537. } {f1 f2}
  538. unset r
  539. # Check for ORDER BY clauses that refer to an AS name in the column list
  540. #
  541. do_test select1-10.1 {
  542. execsql {
  543. SELECT f1 AS x FROM test1 ORDER BY x
  544. }
  545. } {11 33}
  546. do_test select1-10.2 {
  547. execsql {
  548. SELECT f1 AS x FROM test1 ORDER BY -x
  549. }
  550. } {33 11}
  551. do_test select1-10.3 {
  552. execsql {
  553. SELECT f1-23 AS x FROM test1 ORDER BY abs(x)
  554. }
  555. } {10 -12}
  556. do_test select1-10.4 {
  557. execsql {
  558. SELECT f1-23 AS x FROM test1 ORDER BY -abs(x)
  559. }
  560. } {-12 10}
  561. do_test select1-10.5 {
  562. execsql {
  563. SELECT f1-22 AS x, f2-22 as y FROM test1
  564. }
  565. } {-11 0 11 22}
  566. do_test select1-10.6 {
  567. execsql {
  568. SELECT f1-22 AS x, f2-22 as y FROM test1 WHERE x>0 AND y<50
  569. }
  570. } {11 22}
  571. # Check the ability to specify "TABLE.*" in the result set of a SELECT
  572. #
  573. do_test select1-11.1 {
  574. execsql {
  575. DELETE FROM t3;
  576. DELETE FROM t4;
  577. INSERT INTO t3 VALUES(1,2);
  578. INSERT INTO t4 VALUES(3,4);
  579. SELECT * FROM t3, t4;
  580. }
  581. } {1 2 3 4}
  582. do_test select1-11.2 {
  583. execsql2 {
  584. SELECT * FROM t3, t4;
  585. }
  586. } {t3.a 1 t3.b 2 t4.a 3 t4.b 4}
  587. do_test select1-11.3 {
  588. execsql2 {
  589. SELECT * FROM t3 AS x, t4 AS y;
  590. }
  591. } {x.a 1 x.b 2 y.a 3 y.b 4}
  592. do_test select1-11.4 {
  593. execsql {
  594. SELECT t3.*, t4.b FROM t3, t4;
  595. }
  596. } {1 2 4}
  597. do_test select1-11.5 {
  598. execsql2 {
  599. SELECT t3.*, t4.b FROM t3, t4;
  600. }
  601. } {t3.a 1 t3.b 2 t4.b 4}
  602. do_test select1-11.6 {
  603. execsql2 {
  604. SELECT x.*, y.b FROM t3 AS x, t4 AS y;
  605. }
  606. } {x.a 1 x.b 2 y.b 4}
  607. do_test select1-11.7 {
  608. execsql {
  609. SELECT t3.b, t4.* FROM t3, t4;
  610. }
  611. } {2 3 4}
  612. do_test select1-11.8 {
  613. execsql2 {
  614. SELECT t3.b, t4.* FROM t3, t4;
  615. }
  616. } {t3.b 2 t4.a 3 t4.b 4}
  617. do_test select1-11.9 {
  618. execsql2 {
  619. SELECT x.b, y.* FROM t3 AS x, t4 AS y;
  620. }
  621. } {x.b 2 y.a 3 y.b 4}
  622. do_test select1-11.10 {
  623. catchsql {
  624. SELECT t5.* FROM t3, t4;
  625. }
  626. } {1 {no such table: t5}}
  627. do_test select1-11.11 {
  628. catchsql {
  629. SELECT t3.* FROM t3 AS x, t4;
  630. }
  631. } {1 {no such table: t3}}
  632. do_test select1-11.12 {
  633. execsql2 {
  634. SELECT t3.* FROM t3, (SELECT max(a), max(b) FROM t4)
  635. }
  636. } {t3.a 1 t3.b 2}
  637. do_test select1-11.13 {
  638. execsql2 {
  639. SELECT t3.* FROM (SELECT max(a), max(b) FROM t4), t3
  640. }
  641. } {t3.a 1 t3.b 2}
  642. do_test select1-11.14 {
  643. execsql2 {
  644. SELECT * FROM t3, (SELECT max(a), max(b) FROM t4) AS 'tx'
  645. }
  646. } {t3.a 1 t3.b 2 tx.max(a) 3 tx.max(b) 4}
  647. do_test select1-11.15 {
  648. execsql2 {
  649. SELECT y.*, t3.* FROM t3, (SELECT max(a), max(b) FROM t4) AS y
  650. }
  651. } {y.max(a) 3 y.max(b) 4 t3.a 1 t3.b 2}
  652. do_test select1-11.16 {
  653. execsql2 {
  654. SELECT y.* FROM t3 as y, t4 as z
  655. }
  656. } {y.a 1 y.b 2}
  657. # Tests of SELECT statements without a FROM clause.
  658. #
  659. do_test select1-12.1 {
  660. execsql2 {
  661. SELECT 1+2+3
  662. }
  663. } {1+2+3 6}
  664. do_test select1-12.2 {
  665. execsql2 {
  666. SELECT 1,'hello',2
  667. }
  668. } {1 1 'hello' hello 2 2}
  669. do_test select1-12.3 {
  670. execsql2 {
  671. SELECT 1 AS 'a','hello' AS 'b',2 AS 'c'
  672. }
  673. } {a 1 b hello c 2}
  674. do_test select1-12.4 {
  675. execsql {
  676. DELETE FROM t3;
  677. INSERT INTO t3 VALUES(1,2);
  678. SELECT * FROM t3 UNION SELECT 3 AS 'a', 4 ORDER BY a;
  679. }
  680. } {1 2 3 4}
  681. do_test select1-12.5 {
  682. execsql {
  683. SELECT 3, 4 UNION SELECT * FROM t3;
  684. }
  685. } {1 2 3 4}
  686. do_test select1-12.6 {
  687. execsql {
  688. SELECT * FROM t3 WHERE a=(SELECT 1);
  689. }
  690. } {1 2}
  691. do_test select1-12.7 {
  692. execsql {
  693. SELECT * FROM t3 WHERE a=(SELECT 2);
  694. }
  695. } {}
  696. do_test select1-12.8 {
  697. execsql2 {
  698. SELECT x FROM (
  699. SELECT a,b FROM t3 UNION SELECT a AS 'x', b AS 'y' FROM t4 ORDER BY a,b
  700. ) ORDER BY x;
  701. }
  702. } {x 1 x 3}
  703. do_test select1-12.9 {
  704. execsql2 {
  705. SELECT z.x FROM (
  706. SELECT a,b FROM t3 UNION SELECT a AS 'x', b AS 'y' FROM t4 ORDER BY a,b
  707. ) AS 'z' ORDER BY x;
  708. }
  709. } {z.x 1 z.x 3}
  710. finish_test