select1.test 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  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.70 2009/05/28 01:00:56 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.0}
  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. # Ticket #2526
  216. #
  217. do_test select1-2.21 {
  218. catchsql {
  219. SELECT min(f1) AS m FROM test1 GROUP BY f1 HAVING max(m+5)<10
  220. }
  221. } {1 {misuse of aliased aggregate m}}
  222. do_test select1-2.22 {
  223. catchsql {
  224. SELECT coalesce(min(f1)+5,11) AS m FROM test1
  225. GROUP BY f1
  226. HAVING max(m+5)<10
  227. }
  228. } {1 {misuse of aliased aggregate m}}
  229. do_test select1-2.23 {
  230. execsql {
  231. CREATE TABLE tkt2526(a,b,c PRIMARY KEY);
  232. INSERT INTO tkt2526 VALUES('x','y',NULL);
  233. INSERT INTO tkt2526 VALUES('x','z',NULL);
  234. }
  235. catchsql {
  236. SELECT count(a) AS cn FROM tkt2526 GROUP BY a HAVING cn<max(cn)
  237. }
  238. } {1 {misuse of aliased aggregate cn}}
  239. # WHERE clause expressions
  240. #
  241. do_test select1-3.1 {
  242. set v [catch {execsql {SELECT f1 FROM test1 WHERE f1<11}} msg]
  243. lappend v $msg
  244. } {0 {}}
  245. do_test select1-3.2 {
  246. set v [catch {execsql {SELECT f1 FROM test1 WHERE f1<=11}} msg]
  247. lappend v $msg
  248. } {0 11}
  249. do_test select1-3.3 {
  250. set v [catch {execsql {SELECT f1 FROM test1 WHERE f1=11}} msg]
  251. lappend v $msg
  252. } {0 11}
  253. do_test select1-3.4 {
  254. set v [catch {execsql {SELECT f1 FROM test1 WHERE f1>=11}} msg]
  255. lappend v [lsort $msg]
  256. } {0 {11 33}}
  257. do_test select1-3.5 {
  258. set v [catch {execsql {SELECT f1 FROM test1 WHERE f1>11}} msg]
  259. lappend v [lsort $msg]
  260. } {0 33}
  261. do_test select1-3.6 {
  262. set v [catch {execsql {SELECT f1 FROM test1 WHERE f1!=11}} msg]
  263. lappend v [lsort $msg]
  264. } {0 33}
  265. do_test select1-3.7 {
  266. set v [catch {execsql {SELECT f1 FROM test1 WHERE min(f1,f2)!=11}} msg]
  267. lappend v [lsort $msg]
  268. } {0 33}
  269. do_test select1-3.8 {
  270. set v [catch {execsql {SELECT f1 FROM test1 WHERE max(f1,f2)!=11}} msg]
  271. lappend v [lsort $msg]
  272. } {0 {11 33}}
  273. do_test select1-3.9 {
  274. set v [catch {execsql {SELECT f1 FROM test1 WHERE count(f1,f2)!=11}} msg]
  275. lappend v $msg
  276. } {1 {wrong number of arguments to function count()}}
  277. # ORDER BY expressions
  278. #
  279. do_test select1-4.1 {
  280. set v [catch {execsql {SELECT f1 FROM test1 ORDER BY f1}} msg]
  281. lappend v $msg
  282. } {0 {11 33}}
  283. do_test select1-4.2 {
  284. set v [catch {execsql {SELECT f1 FROM test1 ORDER BY -f1}} msg]
  285. lappend v $msg
  286. } {0 {33 11}}
  287. do_test select1-4.3 {
  288. set v [catch {execsql {SELECT f1 FROM test1 ORDER BY min(f1,f2)}} msg]
  289. lappend v $msg
  290. } {0 {11 33}}
  291. do_test select1-4.4 {
  292. set v [catch {execsql {SELECT f1 FROM test1 ORDER BY min(f1)}} msg]
  293. lappend v $msg
  294. } {1 {misuse of aggregate: min()}}
  295. # The restriction not allowing constants in the ORDER BY clause
  296. # has been removed. See ticket #1768
  297. #do_test select1-4.5 {
  298. # catchsql {
  299. # SELECT f1 FROM test1 ORDER BY 8.4;
  300. # }
  301. #} {1 {ORDER BY terms must not be non-integer constants}}
  302. #do_test select1-4.6 {
  303. # catchsql {
  304. # SELECT f1 FROM test1 ORDER BY '8.4';
  305. # }
  306. #} {1 {ORDER BY terms must not be non-integer constants}}
  307. #do_test select1-4.7.1 {
  308. # catchsql {
  309. # SELECT f1 FROM test1 ORDER BY 'xyz';
  310. # }
  311. #} {1 {ORDER BY terms must not be non-integer constants}}
  312. #do_test select1-4.7.2 {
  313. # catchsql {
  314. # SELECT f1 FROM test1 ORDER BY -8.4;
  315. # }
  316. #} {1 {ORDER BY terms must not be non-integer constants}}
  317. #do_test select1-4.7.3 {
  318. # catchsql {
  319. # SELECT f1 FROM test1 ORDER BY +8.4;
  320. # }
  321. #} {1 {ORDER BY terms must not be non-integer constants}}
  322. #do_test select1-4.7.4 {
  323. # catchsql {
  324. # SELECT f1 FROM test1 ORDER BY 4294967296; -- constant larger than 32 bits
  325. # }
  326. #} {1 {ORDER BY terms must not be non-integer constants}}
  327. do_test select1-4.5 {
  328. execsql {
  329. SELECT f1 FROM test1 ORDER BY 8.4
  330. }
  331. } {11 33}
  332. do_test select1-4.6 {
  333. execsql {
  334. SELECT f1 FROM test1 ORDER BY '8.4'
  335. }
  336. } {11 33}
  337. do_test select1-4.8 {
  338. execsql {
  339. CREATE TABLE t5(a,b);
  340. INSERT INTO t5 VALUES(1,10);
  341. INSERT INTO t5 VALUES(2,9);
  342. SELECT * FROM t5 ORDER BY 1;
  343. }
  344. } {1 10 2 9}
  345. do_test select1-4.9.1 {
  346. execsql {
  347. SELECT * FROM t5 ORDER BY 2;
  348. }
  349. } {2 9 1 10}
  350. do_test select1-4.9.2 {
  351. execsql {
  352. SELECT * FROM t5 ORDER BY +2;
  353. }
  354. } {2 9 1 10}
  355. do_test select1-4.10.1 {
  356. catchsql {
  357. SELECT * FROM t5 ORDER BY 3;
  358. }
  359. } {1 {1st ORDER BY term out of range - should be between 1 and 2}}
  360. do_test select1-4.10.2 {
  361. catchsql {
  362. SELECT * FROM t5 ORDER BY -1;
  363. }
  364. } {1 {1st ORDER BY term out of range - should be between 1 and 2}}
  365. do_test select1-4.11 {
  366. execsql {
  367. INSERT INTO t5 VALUES(3,10);
  368. SELECT * FROM t5 ORDER BY 2, 1 DESC;
  369. }
  370. } {2 9 3 10 1 10}
  371. do_test select1-4.12 {
  372. execsql {
  373. SELECT * FROM t5 ORDER BY 1 DESC, b;
  374. }
  375. } {3 10 2 9 1 10}
  376. do_test select1-4.13 {
  377. execsql {
  378. SELECT * FROM t5 ORDER BY b DESC, 1;
  379. }
  380. } {1 10 3 10 2 9}
  381. # ORDER BY ignored on an aggregate query
  382. #
  383. do_test select1-5.1 {
  384. set v [catch {execsql {SELECT max(f1) FROM test1 ORDER BY f2}} msg]
  385. lappend v $msg
  386. } {0 33}
  387. execsql {CREATE TABLE test2(t1 text, t2 text)}
  388. execsql {INSERT INTO test2 VALUES('abc','xyz')}
  389. # Check for column naming
  390. #
  391. do_test select1-6.1 {
  392. set v [catch {execsql2 {SELECT f1 FROM test1 ORDER BY f2}} msg]
  393. lappend v $msg
  394. } {0 {f1 11 f1 33}}
  395. do_test select1-6.1.1 {
  396. db eval {PRAGMA full_column_names=on}
  397. set v [catch {execsql2 {SELECT f1 FROM test1 ORDER BY f2}} msg]
  398. lappend v $msg
  399. } {0 {test1.f1 11 test1.f1 33}}
  400. do_test select1-6.1.2 {
  401. set v [catch {execsql2 {SELECT f1 as 'f1' FROM test1 ORDER BY f2}} msg]
  402. lappend v $msg
  403. } {0 {f1 11 f1 33}}
  404. do_test select1-6.1.3 {
  405. set v [catch {execsql2 {SELECT * FROM test1 WHERE f1==11}} msg]
  406. lappend v $msg
  407. } {0 {f1 11 f2 22}}
  408. do_test select1-6.1.4 {
  409. set v [catch {execsql2 {SELECT DISTINCT * FROM test1 WHERE f1==11}} msg]
  410. db eval {PRAGMA full_column_names=off}
  411. lappend v $msg
  412. } {0 {f1 11 f2 22}}
  413. do_test select1-6.1.5 {
  414. set v [catch {execsql2 {SELECT * FROM test1 WHERE f1==11}} msg]
  415. lappend v $msg
  416. } {0 {f1 11 f2 22}}
  417. do_test select1-6.1.6 {
  418. set v [catch {execsql2 {SELECT DISTINCT * FROM test1 WHERE f1==11}} msg]
  419. lappend v $msg
  420. } {0 {f1 11 f2 22}}
  421. do_test select1-6.2 {
  422. set v [catch {execsql2 {SELECT f1 as xyzzy FROM test1 ORDER BY f2}} msg]
  423. lappend v $msg
  424. } {0 {xyzzy 11 xyzzy 33}}
  425. do_test select1-6.3 {
  426. set v [catch {execsql2 {SELECT f1 as "xyzzy" FROM test1 ORDER BY f2}} msg]
  427. lappend v $msg
  428. } {0 {xyzzy 11 xyzzy 33}}
  429. do_test select1-6.3.1 {
  430. set v [catch {execsql2 {SELECT f1 as 'xyzzy ' FROM test1 ORDER BY f2}} msg]
  431. lappend v $msg
  432. } {0 {{xyzzy } 11 {xyzzy } 33}}
  433. do_test select1-6.4 {
  434. set v [catch {execsql2 {SELECT f1+F2 as xyzzy FROM test1 ORDER BY f2}} msg]
  435. lappend v $msg
  436. } {0 {xyzzy 33 xyzzy 77}}
  437. do_test select1-6.4a {
  438. set v [catch {execsql2 {SELECT f1+F2 FROM test1 ORDER BY f2}} msg]
  439. lappend v $msg
  440. } {0 {f1+F2 33 f1+F2 77}}
  441. do_test select1-6.5 {
  442. set v [catch {execsql2 {SELECT test1.f1+F2 FROM test1 ORDER BY f2}} msg]
  443. lappend v $msg
  444. } {0 {test1.f1+F2 33 test1.f1+F2 77}}
  445. do_test select1-6.5.1 {
  446. execsql2 {PRAGMA full_column_names=on}
  447. set v [catch {execsql2 {SELECT test1.f1+F2 FROM test1 ORDER BY f2}} msg]
  448. execsql2 {PRAGMA full_column_names=off}
  449. lappend v $msg
  450. } {0 {test1.f1+F2 33 test1.f1+F2 77}}
  451. do_test select1-6.6 {
  452. set v [catch {execsql2 {SELECT test1.f1+F2, t1 FROM test1, test2
  453. ORDER BY f2}} msg]
  454. lappend v $msg
  455. } {0 {test1.f1+F2 33 t1 abc test1.f1+F2 77 t1 abc}}
  456. do_test select1-6.7 {
  457. set v [catch {execsql2 {SELECT A.f1, t1 FROM test1 as A, test2
  458. ORDER BY f2}} msg]
  459. lappend v $msg
  460. } {0 {f1 11 t1 abc f1 33 t1 abc}}
  461. do_test select1-6.8 {
  462. set v [catch {execsql2 {SELECT A.f1, f1 FROM test1 as A, test1 as B
  463. ORDER BY f2}} msg]
  464. lappend v $msg
  465. } {1 {ambiguous column name: f1}}
  466. do_test select1-6.8b {
  467. set v [catch {execsql2 {SELECT A.f1, B.f1 FROM test1 as A, test1 as B
  468. ORDER BY f2}} msg]
  469. lappend v $msg
  470. } {1 {ambiguous column name: f2}}
  471. do_test select1-6.8c {
  472. set v [catch {execsql2 {SELECT A.f1, f1 FROM test1 as A, test1 as A
  473. ORDER BY f2}} msg]
  474. lappend v $msg
  475. } {1 {ambiguous column name: A.f1}}
  476. do_test select1-6.9.1 {
  477. set v [catch {execsql {SELECT A.f1, B.f1 FROM test1 as A, test1 as B
  478. ORDER BY A.f1, B.f1}} msg]
  479. lappend v $msg
  480. } {0 {11 11 11 33 33 11 33 33}}
  481. do_test select1-6.9.2 {
  482. set v [catch {execsql2 {SELECT A.f1, B.f1 FROM test1 as A, test1 as B
  483. ORDER BY A.f1, B.f1}} msg]
  484. lappend v $msg
  485. } {0 {f1 11 f1 11 f1 33 f1 33 f1 11 f1 11 f1 33 f1 33}}
  486. do_test select1-6.9.3 {
  487. db eval {
  488. PRAGMA short_column_names=OFF;
  489. PRAGMA full_column_names=OFF;
  490. }
  491. execsql2 {
  492. SELECT test1 . f1, test1 . f2 FROM test1 LIMIT 1
  493. }
  494. } {{test1 . f1} 11 {test1 . f2} 22}
  495. do_test select1-6.9.4 {
  496. db eval {
  497. PRAGMA short_column_names=OFF;
  498. PRAGMA full_column_names=ON;
  499. }
  500. execsql2 {
  501. SELECT test1 . f1, test1 . f2 FROM test1 LIMIT 1
  502. }
  503. } {test1.f1 11 test1.f2 22}
  504. do_test select1-6.9.5 {
  505. db eval {
  506. PRAGMA short_column_names=OFF;
  507. PRAGMA full_column_names=ON;
  508. }
  509. execsql2 {
  510. SELECT 123.45;
  511. }
  512. } {123.45 123.45}
  513. do_test select1-6.9.6 {
  514. execsql2 {
  515. SELECT * FROM test1 a, test1 b LIMIT 1
  516. }
  517. } {a.f1 11 a.f2 22 b.f1 11 b.f2 22}
  518. do_test select1-6.9.7 {
  519. set x [execsql2 {
  520. SELECT * FROM test1 a, (select 5, 6) LIMIT 1
  521. }]
  522. regsub -all {sq_[0-9a-fA-F_]+} $x {subquery} x
  523. set x
  524. } {a.f1 11 a.f2 22 sqlite_subquery.5 5 sqlite_subquery.6 6}
  525. do_test select1-6.9.8 {
  526. set x [execsql2 {
  527. SELECT * FROM test1 a, (select 5 AS x, 6 AS y) AS b LIMIT 1
  528. }]
  529. regsub -all {subquery_[0-9a-fA-F]+_} $x {subquery} x
  530. set x
  531. } {a.f1 11 a.f2 22 b.x 5 b.y 6}
  532. do_test select1-6.9.9 {
  533. execsql2 {
  534. SELECT a.f1, b.f2 FROM test1 a, test1 b LIMIT 1
  535. }
  536. } {test1.f1 11 test1.f2 22}
  537. do_test select1-6.9.10 {
  538. execsql2 {
  539. SELECT f1, t1 FROM test1, test2 LIMIT 1
  540. }
  541. } {test1.f1 11 test2.t1 abc}
  542. do_test select1-6.9.11 {
  543. db eval {
  544. PRAGMA short_column_names=ON;
  545. PRAGMA full_column_names=ON;
  546. }
  547. execsql2 {
  548. SELECT a.f1, b.f2 FROM test1 a, test1 b LIMIT 1
  549. }
  550. } {test1.f1 11 test1.f2 22}
  551. do_test select1-6.9.12 {
  552. execsql2 {
  553. SELECT f1, t1 FROM test1, test2 LIMIT 1
  554. }
  555. } {test1.f1 11 test2.t1 abc}
  556. do_test select1-6.9.13 {
  557. db eval {
  558. PRAGMA short_column_names=ON;
  559. PRAGMA full_column_names=OFF;
  560. }
  561. execsql2 {
  562. SELECT a.f1, b.f1 FROM test1 a, test1 b LIMIT 1
  563. }
  564. } {f1 11 f1 11}
  565. do_test select1-6.9.14 {
  566. execsql2 {
  567. SELECT f1, t1 FROM test1, test2 LIMIT 1
  568. }
  569. } {f1 11 t1 abc}
  570. do_test select1-6.9.15 {
  571. db eval {
  572. PRAGMA short_column_names=OFF;
  573. PRAGMA full_column_names=ON;
  574. }
  575. execsql2 {
  576. SELECT a.f1, b.f1 FROM test1 a, test1 b LIMIT 1
  577. }
  578. } {test1.f1 11 test1.f1 11}
  579. do_test select1-6.9.16 {
  580. execsql2 {
  581. SELECT f1, t1 FROM test1, test2 LIMIT 1
  582. }
  583. } {test1.f1 11 test2.t1 abc}
  584. db eval {
  585. PRAGMA short_column_names=ON;
  586. PRAGMA full_column_names=OFF;
  587. }
  588. ifcapable compound {
  589. do_test select1-6.10 {
  590. set v [catch {execsql2 {
  591. SELECT f1 FROM test1 UNION SELECT f2 FROM test1
  592. ORDER BY f2;
  593. }} msg]
  594. lappend v $msg
  595. } {0 {f1 11 f1 22 f1 33 f1 44}}
  596. do_test select1-6.11 {
  597. set v [catch {execsql2 {
  598. SELECT f1 FROM test1 UNION SELECT f2+100 FROM test1
  599. ORDER BY f2+101;
  600. }} msg]
  601. lappend v $msg
  602. } {1 {1st ORDER BY term does not match any column in the result set}}
  603. # Ticket #2296
  604. ifcapable subquery&&compound {
  605. do_test select1-6.20 {
  606. execsql {
  607. CREATE TABLE t6(a TEXT, b TEXT);
  608. INSERT INTO t6 VALUES('a','0');
  609. INSERT INTO t6 VALUES('b','1');
  610. INSERT INTO t6 VALUES('c','2');
  611. INSERT INTO t6 VALUES('d','3');
  612. SELECT a FROM t6 WHERE b IN
  613. (SELECT b FROM t6 WHERE a<='b' UNION SELECT '3' AS x
  614. ORDER BY 1 LIMIT 1)
  615. }
  616. } {a}
  617. do_test select1-6.21 {
  618. execsql {
  619. SELECT a FROM t6 WHERE b IN
  620. (SELECT b FROM t6 WHERE a<='b' UNION SELECT '3' AS x
  621. ORDER BY 1 DESC LIMIT 1)
  622. }
  623. } {d}
  624. do_test select1-6.22 {
  625. execsql {
  626. SELECT a FROM t6 WHERE b IN
  627. (SELECT b FROM t6 WHERE a<='b' UNION SELECT '3' AS x
  628. ORDER BY b LIMIT 2)
  629. ORDER BY a;
  630. }
  631. } {a b}
  632. do_test select1-6.23 {
  633. execsql {
  634. SELECT a FROM t6 WHERE b IN
  635. (SELECT b FROM t6 WHERE a<='b' UNION SELECT '3' AS x
  636. ORDER BY x DESC LIMIT 2)
  637. ORDER BY a;
  638. }
  639. } {b d}
  640. }
  641. } ;#ifcapable compound
  642. do_test select1-7.1 {
  643. set v [catch {execsql {
  644. SELECT f1 FROM test1 WHERE f2=;
  645. }} msg]
  646. lappend v $msg
  647. } {1 {near ";": syntax error}}
  648. ifcapable compound {
  649. do_test select1-7.2 {
  650. set v [catch {execsql {
  651. SELECT f1 FROM test1 UNION SELECT WHERE;
  652. }} msg]
  653. lappend v $msg
  654. } {1 {near "WHERE": syntax error}}
  655. } ;# ifcapable compound
  656. do_test select1-7.3 {
  657. set v [catch {execsql {SELECT f1 FROM test1 as 'hi', test2 as}} msg]
  658. lappend v $msg
  659. } {1 {near "as": syntax error}}
  660. do_test select1-7.4 {
  661. set v [catch {execsql {
  662. SELECT f1 FROM test1 ORDER BY;
  663. }} msg]
  664. lappend v $msg
  665. } {1 {near ";": syntax error}}
  666. do_test select1-7.5 {
  667. set v [catch {execsql {
  668. SELECT f1 FROM test1 ORDER BY f1 desc, f2 where;
  669. }} msg]
  670. lappend v $msg
  671. } {1 {near "where": syntax error}}
  672. do_test select1-7.6 {
  673. set v [catch {execsql {
  674. SELECT count(f1,f2 FROM test1;
  675. }} msg]
  676. lappend v $msg
  677. } {1 {near "FROM": syntax error}}
  678. do_test select1-7.7 {
  679. set v [catch {execsql {
  680. SELECT count(f1,f2+) FROM test1;
  681. }} msg]
  682. lappend v $msg
  683. } {1 {near ")": syntax error}}
  684. do_test select1-7.8 {
  685. set v [catch {execsql {
  686. SELECT f1 FROM test1 ORDER BY f2, f1+;
  687. }} msg]
  688. lappend v $msg
  689. } {1 {near ";": syntax error}}
  690. do_test select1-7.9 {
  691. catchsql {
  692. SELECT f1 FROM test1 LIMIT 5+3 OFFSET 11 ORDER BY f2;
  693. }
  694. } {1 {near "ORDER": syntax error}}
  695. do_test select1-8.1 {
  696. execsql {SELECT f1 FROM test1 WHERE 4.3+2.4 OR 1 ORDER BY f1}
  697. } {11 33}
  698. do_test select1-8.2 {
  699. execsql {
  700. SELECT f1 FROM test1 WHERE ('x' || f1) BETWEEN 'x10' AND 'x20'
  701. ORDER BY f1
  702. }
  703. } {11}
  704. do_test select1-8.3 {
  705. execsql {
  706. SELECT f1 FROM test1 WHERE 5-3==2
  707. ORDER BY f1
  708. }
  709. } {11 33}
  710. # TODO: This test is failing because f1 is now being loaded off the
  711. # disk as a vdbe integer, not a string. Hence the value of f1/(f1-11)
  712. # changes because of rounding. Disable the test for now.
  713. if 0 {
  714. do_test select1-8.4 {
  715. execsql {
  716. SELECT coalesce(f1/(f1-11),'x'),
  717. coalesce(min(f1/(f1-11),5),'y'),
  718. coalesce(max(f1/(f1-33),6),'z')
  719. FROM test1 ORDER BY f1
  720. }
  721. } {x y 6 1.5 1.5 z}
  722. }
  723. do_test select1-8.5 {
  724. execsql {
  725. SELECT min(1,2,3), -max(1,2,3)
  726. FROM test1 ORDER BY f1
  727. }
  728. } {1 -3 1 -3}
  729. # Check the behavior when the result set is empty
  730. #
  731. # SQLite v3 always sets r(*).
  732. #
  733. # do_test select1-9.1 {
  734. # catch {unset r}
  735. # set r(*) {}
  736. # db eval {SELECT * FROM test1 WHERE f1<0} r {}
  737. # set r(*)
  738. # } {}
  739. do_test select1-9.2 {
  740. execsql {PRAGMA empty_result_callbacks=on}
  741. catch {unset r}
  742. set r(*) {}
  743. db eval {SELECT * FROM test1 WHERE f1<0} r {}
  744. set r(*)
  745. } {f1 f2}
  746. ifcapable subquery {
  747. do_test select1-9.3 {
  748. set r(*) {}
  749. db eval {SELECT * FROM test1 WHERE f1<(select count(*) from test2)} r {}
  750. set r(*)
  751. } {f1 f2}
  752. }
  753. do_test select1-9.4 {
  754. set r(*) {}
  755. db eval {SELECT * FROM test1 ORDER BY f1} r {}
  756. set r(*)
  757. } {f1 f2}
  758. do_test select1-9.5 {
  759. set r(*) {}
  760. db eval {SELECT * FROM test1 WHERE f1<0 ORDER BY f1} r {}
  761. set r(*)
  762. } {f1 f2}
  763. unset r
  764. # Check for ORDER BY clauses that refer to an AS name in the column list
  765. #
  766. do_test select1-10.1 {
  767. execsql {
  768. SELECT f1 AS x FROM test1 ORDER BY x
  769. }
  770. } {11 33}
  771. do_test select1-10.2 {
  772. execsql {
  773. SELECT f1 AS x FROM test1 ORDER BY -x
  774. }
  775. } {33 11}
  776. do_test select1-10.3 {
  777. execsql {
  778. SELECT f1-23 AS x FROM test1 ORDER BY abs(x)
  779. }
  780. } {10 -12}
  781. do_test select1-10.4 {
  782. execsql {
  783. SELECT f1-23 AS x FROM test1 ORDER BY -abs(x)
  784. }
  785. } {-12 10}
  786. do_test select1-10.5 {
  787. execsql {
  788. SELECT f1-22 AS x, f2-22 as y FROM test1
  789. }
  790. } {-11 0 11 22}
  791. do_test select1-10.6 {
  792. execsql {
  793. SELECT f1-22 AS x, f2-22 as y FROM test1 WHERE x>0 AND y<50
  794. }
  795. } {11 22}
  796. do_test select1-10.7 {
  797. execsql {
  798. SELECT f1 COLLATE nocase AS x FROM test1 ORDER BY x
  799. }
  800. } {11 33}
  801. # Check the ability to specify "TABLE.*" in the result set of a SELECT
  802. #
  803. do_test select1-11.1 {
  804. execsql {
  805. DELETE FROM t3;
  806. DELETE FROM t4;
  807. INSERT INTO t3 VALUES(1,2);
  808. INSERT INTO t4 VALUES(3,4);
  809. SELECT * FROM t3, t4;
  810. }
  811. } {1 2 3 4}
  812. do_test select1-11.2.1 {
  813. execsql {
  814. SELECT * FROM t3, t4;
  815. }
  816. } {1 2 3 4}
  817. do_test select1-11.2.2 {
  818. execsql2 {
  819. SELECT * FROM t3, t4;
  820. }
  821. } {a 3 b 4 a 3 b 4}
  822. do_test select1-11.4.1 {
  823. execsql {
  824. SELECT t3.*, t4.b FROM t3, t4;
  825. }
  826. } {1 2 4}
  827. do_test select1-11.4.2 {
  828. execsql {
  829. SELECT "t3".*, t4.b FROM t3, t4;
  830. }
  831. } {1 2 4}
  832. do_test select1-11.5.1 {
  833. execsql2 {
  834. SELECT t3.*, t4.b FROM t3, t4;
  835. }
  836. } {a 1 b 4 b 4}
  837. do_test select1-11.6 {
  838. execsql2 {
  839. SELECT x.*, y.b FROM t3 AS x, t4 AS y;
  840. }
  841. } {a 1 b 4 b 4}
  842. do_test select1-11.7 {
  843. execsql {
  844. SELECT t3.b, t4.* FROM t3, t4;
  845. }
  846. } {2 3 4}
  847. do_test select1-11.8 {
  848. execsql2 {
  849. SELECT t3.b, t4.* FROM t3, t4;
  850. }
  851. } {b 4 a 3 b 4}
  852. do_test select1-11.9 {
  853. execsql2 {
  854. SELECT x.b, y.* FROM t3 AS x, t4 AS y;
  855. }
  856. } {b 4 a 3 b 4}
  857. do_test select1-11.10 {
  858. catchsql {
  859. SELECT t5.* FROM t3, t4;
  860. }
  861. } {1 {no such table: t5}}
  862. do_test select1-11.11 {
  863. catchsql {
  864. SELECT t3.* FROM t3 AS x, t4;
  865. }
  866. } {1 {no such table: t3}}
  867. ifcapable subquery {
  868. do_test select1-11.12 {
  869. execsql2 {
  870. SELECT t3.* FROM t3, (SELECT max(a), max(b) FROM t4)
  871. }
  872. } {a 1 b 2}
  873. do_test select1-11.13 {
  874. execsql2 {
  875. SELECT t3.* FROM (SELECT max(a), max(b) FROM t4), t3
  876. }
  877. } {a 1 b 2}
  878. do_test select1-11.14 {
  879. execsql2 {
  880. SELECT * FROM t3, (SELECT max(a), max(b) FROM t4) AS 'tx'
  881. }
  882. } {a 1 b 2 max(a) 3 max(b) 4}
  883. do_test select1-11.15 {
  884. execsql2 {
  885. SELECT y.*, t3.* FROM t3, (SELECT max(a), max(b) FROM t4) AS y
  886. }
  887. } {max(a) 3 max(b) 4 a 1 b 2}
  888. }
  889. do_test select1-11.16 {
  890. execsql2 {
  891. SELECT y.* FROM t3 as y, t4 as z
  892. }
  893. } {a 1 b 2}
  894. # Tests of SELECT statements without a FROM clause.
  895. #
  896. do_test select1-12.1 {
  897. execsql2 {
  898. SELECT 1+2+3
  899. }
  900. } {1+2+3 6}
  901. do_test select1-12.2 {
  902. execsql2 {
  903. SELECT 1,'hello',2
  904. }
  905. } {1 1 'hello' hello 2 2}
  906. do_test select1-12.3 {
  907. execsql2 {
  908. SELECT 1 AS 'a','hello' AS 'b',2 AS 'c'
  909. }
  910. } {a 1 b hello c 2}
  911. do_test select1-12.4 {
  912. execsql {
  913. DELETE FROM t3;
  914. INSERT INTO t3 VALUES(1,2);
  915. }
  916. } {}
  917. ifcapable compound {
  918. do_test select1-12.5 {
  919. execsql {
  920. SELECT * FROM t3 UNION SELECT 3 AS 'a', 4 ORDER BY a;
  921. }
  922. } {1 2 3 4}
  923. do_test select1-12.6 {
  924. execsql {
  925. SELECT 3, 4 UNION SELECT * FROM t3;
  926. }
  927. } {1 2 3 4}
  928. } ;# ifcapable compound
  929. ifcapable subquery {
  930. do_test select1-12.7 {
  931. execsql {
  932. SELECT * FROM t3 WHERE a=(SELECT 1);
  933. }
  934. } {1 2}
  935. do_test select1-12.8 {
  936. execsql {
  937. SELECT * FROM t3 WHERE a=(SELECT 2);
  938. }
  939. } {}
  940. }
  941. ifcapable {compound && subquery} {
  942. do_test select1-12.9 {
  943. execsql2 {
  944. SELECT x FROM (
  945. SELECT a AS x, b AS y FROM t3 UNION SELECT a,b FROM t4 ORDER BY a,b
  946. ) ORDER BY x;
  947. }
  948. } {x 1 x 3}
  949. do_test select1-12.10 {
  950. execsql2 {
  951. SELECT z.x FROM (
  952. SELECT a AS x,b AS y FROM t3 UNION SELECT a, b FROM t4 ORDER BY a,b
  953. ) AS 'z' ORDER BY x;
  954. }
  955. } {x 1 x 3}
  956. } ;# ifcapable compound
  957. # Check for a VDBE stack growth problem that existed at one point.
  958. #
  959. ifcapable subquery {
  960. do_test select1-13.1 {
  961. execsql {
  962. BEGIN;
  963. create TABLE abc(a, b, c, PRIMARY KEY(a, b));
  964. INSERT INTO abc VALUES(1, 1, 1);
  965. }
  966. for {set i 0} {$i<10} {incr i} {
  967. execsql {
  968. INSERT INTO abc SELECT a+(select max(a) FROM abc),
  969. b+(select max(a) FROM abc), c+(select max(a) FROM abc) FROM abc;
  970. }
  971. }
  972. execsql {COMMIT}
  973. # This used to seg-fault when the problem existed.
  974. execsql {
  975. SELECT count(
  976. (SELECT a FROM abc WHERE a = NULL AND b >= upper.c)
  977. ) FROM abc AS upper;
  978. }
  979. } {0}
  980. }
  981. foreach tab [db eval {SELECT name FROM sqlite_master WHERE type = 'table'}] {
  982. db eval "DROP TABLE $tab"
  983. }
  984. db close
  985. sqlite3 db test.db
  986. do_test select1-14.1 {
  987. execsql {
  988. SELECT * FROM sqlite_master WHERE rowid>10;
  989. SELECT * FROM sqlite_master WHERE rowid=10;
  990. SELECT * FROM sqlite_master WHERE rowid<10;
  991. SELECT * FROM sqlite_master WHERE rowid<=10;
  992. SELECT * FROM sqlite_master WHERE rowid>=10;
  993. SELECT * FROM sqlite_master;
  994. }
  995. } {}
  996. do_test select1-14.2 {
  997. execsql {
  998. SELECT 10 IN (SELECT rowid FROM sqlite_master);
  999. }
  1000. } {0}
  1001. if {[db one {PRAGMA locking_mode}]=="normal"} {
  1002. # Check that ticket #3771 has been fixed. This test does not
  1003. # work with locking_mode=EXCLUSIVE so disable in that case.
  1004. #
  1005. do_test select1-15.1 {
  1006. execsql {
  1007. CREATE TABLE t1(a);
  1008. CREATE INDEX i1 ON t1(a);
  1009. INSERT INTO t1 VALUES(1);
  1010. INSERT INTO t1 VALUES(2);
  1011. INSERT INTO t1 VALUES(3);
  1012. }
  1013. } {}
  1014. do_test select1-15.2 {
  1015. sqlite3 db2 test.db
  1016. execsql { DROP INDEX i1 } db2
  1017. db2 close
  1018. } {}
  1019. do_test select1-15.3 {
  1020. execsql { SELECT 2 IN (SELECT a FROM t1) }
  1021. } {1}
  1022. }
  1023. # Crash bug reported on the mailing list on 2012-02-23
  1024. #
  1025. do_test select1-16.1 {
  1026. catchsql {SELECT 1 FROM (SELECT *)}
  1027. } {1 {no tables specified}}
  1028. finish_test