PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/test/tkt2822.test

https://bitbucket.org/aware/sqlite
Unknown | 336 lines | 314 code | 22 blank | 0 comment | 0 complexity | 3e8e904978ba475b7d50211446f5b0e0 MD5 | raw file
  1. # 2007 Dec 4
  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 is to test that the issues surrounding expressions in
  13. # ORDER BY clauses on compound SELECT statements raised by ticket
  14. # #2822 have been dealt with.
  15. #
  16. # $Id:$
  17. #
  18. set testdir [file dirname $argv0]
  19. source $testdir/tester.tcl
  20. ifcapable !compound {
  21. finish_test
  22. return
  23. }
  24. # The ORDER BY matching algorithm is three steps:
  25. #
  26. # (1) If the ORDER BY term is an integer constant i, then
  27. # sort by the i-th column of the result set.
  28. #
  29. # (2) If the ORDER BY term is an identifier (not x.y or x.y.z
  30. # but simply x) then look for a column alias with the same
  31. # name. If found, then sort by that column.
  32. #
  33. # (3) Evaluate the term as an expression and sort by the
  34. # value of the expression.
  35. #
  36. # For a compound SELECT the rules are modified slightly.
  37. # In the third rule, the expression must exactly match one
  38. # of the result columns. The sequences of three rules is
  39. # attempted first on the left-most SELECT. If that doesn't
  40. # work, we move to the right, one by one.
  41. #
  42. # Rule (3) is not in standard SQL - it is an SQLite extension,
  43. # though one copied from PostgreSQL. The rule for compound
  44. # queries where a search is made of SELECTs to the right
  45. # if the left-most SELECT does not match is not a part of
  46. # standard SQL either. This extension is unique to SQLite
  47. # as far as we know.
  48. #
  49. # Rule (2) was added by the changes ticket #2822. Prior to
  50. # that changes, SQLite did not support rule (2), making it
  51. # technically in violation of standard SQL semantics.
  52. # No body noticed because rule (3) has the same effect as
  53. # rule (2) except in some obscure cases.
  54. #
  55. # Test plan:
  56. #
  57. # tkt2822-1.* - Simple identifier as ORDER BY expression.
  58. # tkt2822-2.* - More complex ORDER BY expressions.
  59. do_test tkt2822-0.1 {
  60. execsql {
  61. CREATE TABLE t1(a, b, c);
  62. CREATE TABLE t2(a, b, c);
  63. INSERT INTO t1 VALUES(1, 3, 9);
  64. INSERT INTO t1 VALUES(3, 9, 27);
  65. INSERT INTO t1 VALUES(5, 15, 45);
  66. INSERT INTO t2 VALUES(2, 6, 18);
  67. INSERT INTO t2 VALUES(4, 12, 36);
  68. INSERT INTO t2 VALUES(6, 18, 54);
  69. }
  70. } {}
  71. # Test the "ORDER BY <integer>" syntax.
  72. #
  73. do_test tkt2822-1.1 {
  74. execsql {
  75. SELECT a, b, c FROM t1 UNION ALL SELECT a, b, c FROM t2 ORDER BY 1;
  76. }
  77. } {1 3 9 2 6 18 3 9 27 4 12 36 5 15 45 6 18 54}
  78. do_test tkt2822-1.2 {
  79. execsql {
  80. SELECT a, CAST (b AS TEXT), c FROM t1
  81. UNION ALL
  82. SELECT a, b, c FROM t2
  83. ORDER BY 2;
  84. }
  85. } {2 6 18 4 12 36 6 18 54 5 15 45 1 3 9 3 9 27}
  86. # Test the "ORDER BY <identifier>" syntax.
  87. #
  88. do_test tkt2822-2.1 {
  89. execsql {
  90. SELECT a, b, c FROM t1 UNION ALL SELECT a, b, c FROM t2 ORDER BY a;
  91. }
  92. } {1 3 9 2 6 18 3 9 27 4 12 36 5 15 45 6 18 54}
  93. do_test tkt2822-2.2 {
  94. execsql {
  95. SELECT a, CAST (b AS TEXT) AS x, c FROM t1
  96. UNION ALL
  97. SELECT a, b, c FROM t2
  98. ORDER BY x;
  99. }
  100. } {2 6 18 4 12 36 6 18 54 5 15 45 1 3 9 3 9 27}
  101. do_test tkt2822-2.3 {
  102. execsql {
  103. SELECT t1.a, b, c FROM t1 UNION ALL SELECT t2.a, b, c FROM t2 ORDER BY a;
  104. }
  105. } {1 3 9 2 6 18 3 9 27 4 12 36 5 15 45 6 18 54}
  106. # Test the "ORDER BY <expression>" syntax.
  107. #
  108. do_test tkt2822-3.1 {
  109. execsql {
  110. SELECT a, CAST (b AS TEXT) AS x, c FROM t1
  111. UNION ALL
  112. SELECT a, b, c FROM t2
  113. ORDER BY CAST (b AS TEXT);
  114. }
  115. } {2 6 18 4 12 36 6 18 54 5 15 45 1 3 9 3 9 27}
  116. do_test tkt2822-3.2 {
  117. execsql {
  118. SELECT t1.a, b, c FROM t1 UNION ALL SELECT t2.a, b, c FROM t2 ORDER BY t1.a;
  119. }
  120. } {1 3 9 2 6 18 3 9 27 4 12 36 5 15 45 6 18 54}
  121. # Test that if a match cannot be found in the leftmost SELECT, an
  122. # attempt is made to find a match in subsequent SELECT statements.
  123. #
  124. do_test tkt2822-3.3 {
  125. execsql {
  126. SELECT a, b, c FROM t1 UNION ALL SELECT a AS x, b, c FROM t2 ORDER BY x;
  127. }
  128. } {1 3 9 2 6 18 3 9 27 4 12 36 5 15 45 6 18 54}
  129. do_test tkt2822-3.4 {
  130. # But the leftmost SELECT takes precedence.
  131. execsql {
  132. SELECT a AS b, CAST (b AS TEXT) AS a, c FROM t1
  133. UNION ALL
  134. SELECT a, b, c FROM t2
  135. ORDER BY a;
  136. }
  137. } {2 6 18 4 12 36 6 18 54 5 15 45 1 3 9 3 9 27}
  138. do_test tkt2822-3.5 {
  139. execsql {
  140. SELECT a, b, c FROM t2
  141. UNION ALL
  142. SELECT a AS b, CAST (b AS TEXT) AS a, c FROM t1
  143. ORDER BY a;
  144. }
  145. } {1 3 9 2 6 18 3 9 27 4 12 36 5 15 45 6 18 54}
  146. # Test some error conditions (ORDER BY clauses that match no column).
  147. #
  148. do_test tkt2822-4.1 {
  149. catchsql {
  150. SELECT a, b, c FROM t1 UNION ALL SELECT a, b, c FROM t2 ORDER BY x
  151. }
  152. } {1 {1st ORDER BY term does not match any column in the result set}}
  153. do_test tkt2822-4.2 {
  154. catchsql {
  155. SELECT a, CAST (b AS TEXT) AS x, c FROM t1
  156. UNION ALL
  157. SELECT a, b, c FROM t2
  158. ORDER BY CAST (b AS INTEGER);
  159. }
  160. } {1 {1st ORDER BY term does not match any column in the result set}}
  161. # Tests for rule (2).
  162. #
  163. # The "ORDER BY b" should match the column alias (rule 2), not the
  164. # the t3.b value (rule 3).
  165. #
  166. do_test tkt2822-5.1 {
  167. execsql {
  168. CREATE TABLE t3(a,b);
  169. INSERT INTO t3 VALUES(1,8);
  170. INSERT INTO t3 VALUES(9,2);
  171. SELECT a AS b FROM t3 ORDER BY b;
  172. }
  173. } {1 9}
  174. do_test tkt2822-5.2 {
  175. # Case does not matter. b should match B
  176. execsql {
  177. SELECT a AS b FROM t3 ORDER BY B;
  178. }
  179. } {1 9}
  180. do_test tkt2822-5.3 {
  181. # Quoting should not matter
  182. execsql {
  183. SELECT a AS 'b' FROM t3 ORDER BY "B";
  184. }
  185. } {1 9}
  186. do_test tkt2822-5.4 {
  187. # Quoting should not matter
  188. execsql {
  189. SELECT a AS "b" FROM t3 ORDER BY [B];
  190. }
  191. } {1 9}
  192. # In "ORDER BY +b" the term is now an expression rather than
  193. # a label. It therefore matches by rule (3) instead of rule (2).
  194. #
  195. do_test tkt2822-5.5 {
  196. execsql {
  197. SELECT a AS b FROM t3 ORDER BY +b;
  198. }
  199. } {9 1}
  200. # Tests for rule 2 in compound queries
  201. #
  202. do_test tkt2822-6.1 {
  203. execsql {
  204. CREATE TABLE t6a(p,q);
  205. INSERT INTO t6a VALUES(1,8);
  206. INSERT INTO t6a VALUES(9,2);
  207. CREATE TABLE t6b(x,y);
  208. INSERT INTO t6b VALUES(1,7);
  209. INSERT INTO t6b VALUES(7,2);
  210. SELECT p, q FROM t6a UNION ALL SELECT x, y FROM t6b ORDER BY 1, 2
  211. }
  212. } {1 7 1 8 7 2 9 2}
  213. do_test tkt2822-6.2 {
  214. execsql {
  215. SELECT p PX, q QX FROM t6a UNION ALL SELECT x XX, y YX FROM t6b
  216. ORDER BY PX, YX
  217. }
  218. } {1 7 1 8 7 2 9 2}
  219. do_test tkt2822-6.3 {
  220. execsql {
  221. SELECT p PX, q QX FROM t6a UNION ALL SELECT x XX, y YX FROM t6b
  222. ORDER BY XX, QX
  223. }
  224. } {1 7 1 8 7 2 9 2}
  225. do_test tkt2822-6.4 {
  226. execsql {
  227. SELECT p PX, q QX FROM t6a UNION ALL SELECT x XX, y YX FROM t6b
  228. ORDER BY QX, XX
  229. }
  230. } {7 2 9 2 1 7 1 8}
  231. do_test tkt2822-6.5 {
  232. execsql {
  233. SELECT p PX, q QX FROM t6a UNION ALL SELECT x XX, y YX FROM t6b
  234. ORDER BY t6b.x, QX
  235. }
  236. } {1 7 1 8 7 2 9 2}
  237. do_test tkt2822-6.6 {
  238. execsql {
  239. SELECT p PX, q QX FROM t6a UNION ALL SELECT x XX, y YX FROM t6b
  240. ORDER BY t6a.q, XX
  241. }
  242. } {7 2 9 2 1 7 1 8}
  243. # More error message tests. This is really more of a test of the
  244. # %r ordinal value formatting capablity added to sqlite3_snprintf()
  245. # by ticket #2822.
  246. #
  247. do_test tkt2822-7.1 {
  248. execsql {
  249. CREATE TABLE t7(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,
  250. a15,a16,a17,a18,a19,a20,a21,a22,a23,a24,a25);
  251. }
  252. catchsql {
  253. SELECT * FROM t7 ORDER BY 0;
  254. }
  255. } {1 {1st ORDER BY term out of range - should be between 1 and 25}}
  256. do_test tkt2822-7.2 {
  257. catchsql {
  258. SELECT * FROM t7 ORDER BY 1, 0;
  259. }
  260. } {1 {2nd ORDER BY term out of range - should be between 1 and 25}}
  261. do_test tkt2822-7.3 {
  262. catchsql {
  263. SELECT * FROM t7 ORDER BY 1, 2, 0;
  264. }
  265. } {1 {3rd ORDER BY term out of range - should be between 1 and 25}}
  266. do_test tkt2822-7.4 {
  267. catchsql {
  268. SELECT * FROM t7 ORDER BY 1, 2, 3, 0;
  269. }
  270. } {1 {4th ORDER BY term out of range - should be between 1 and 25}}
  271. do_test tkt2822-7.9 {
  272. catchsql {
  273. SELECT * FROM t7 ORDER BY 1, 2, 3, 4, 5, 6, 7, 8, 0;
  274. }
  275. } {1 {9th ORDER BY term out of range - should be between 1 and 25}}
  276. do_test tkt2822-7.10 {
  277. catchsql {
  278. SELECT * FROM t7 ORDER BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 0;
  279. }
  280. } {1 {10th ORDER BY term out of range - should be between 1 and 25}}
  281. do_test tkt2822-7.11 {
  282. catchsql {
  283. SELECT * FROM t7 ORDER BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0;
  284. }
  285. } {1 {11th ORDER BY term out of range - should be between 1 and 25}}
  286. do_test tkt2822-7.12 {
  287. catchsql {
  288. SELECT * FROM t7 ORDER BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 0;
  289. }
  290. } {1 {12th ORDER BY term out of range - should be between 1 and 25}}
  291. do_test tkt2822-7.13 {
  292. catchsql {
  293. SELECT * FROM t7 ORDER BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 0;
  294. }
  295. } {1 {13th ORDER BY term out of range - should be between 1 and 25}}
  296. do_test tkt2822-7.20 {
  297. catchsql {
  298. SELECT * FROM t7 ORDER BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
  299. 11,12,13,14,15,16,17,18,19, 0
  300. }
  301. } {1 {20th ORDER BY term out of range - should be between 1 and 25}}
  302. do_test tkt2822-7.21 {
  303. catchsql {
  304. SELECT * FROM t7 ORDER BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
  305. 11,12,13,14,15,16,17,18,19, 20, 0
  306. }
  307. } {1 {21st ORDER BY term out of range - should be between 1 and 25}}
  308. do_test tkt2822-7.22 {
  309. catchsql {
  310. SELECT * FROM t7 ORDER BY 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
  311. 11,12,13,14,15,16,17,18,19, 20, 21, 0
  312. }
  313. } {1 {22nd ORDER BY term out of range - should be between 1 and 25}}
  314. finish_test