1
0

resolver01.test 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. # 2013-04-13
  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 tests features of the name resolver (the component that
  13. # figures out what identifiers in the SQL statement refer to) that
  14. # were fixed by ticket [2500cdb9be]
  15. #
  16. # See also tickets [1c69be2daf] and [f617ea3125] from 2013-08-14.
  17. #
  18. set testdir [file dirname $argv0]
  19. source $testdir/tester.tcl
  20. # "ORDER BY y" binds to the output result-set column named "y"
  21. # if available. If no output column is named "y", then try to
  22. # bind against an input column named "y".
  23. #
  24. # This is classical SQL92 behavior.
  25. #
  26. do_test resolver01-1.1 {
  27. catchsql {
  28. CREATE TABLE t1(x, y); INSERT INTO t1 VALUES(11,22);
  29. CREATE TABLE t2(y, z); INSERT INTO t2 VALUES(33,44);
  30. SELECT 1 AS y FROM t1, t2 ORDER BY y;
  31. }
  32. } {0 1}
  33. do_test resolver01-1.2 {
  34. catchsql {
  35. SELECT 1 AS yy FROM t1, t2 ORDER BY y;
  36. }
  37. } {1 {ambiguous column name: y}}
  38. do_test resolver01-1.3 {
  39. catchsql {
  40. CREATE TABLE t3(x,y); INSERT INTO t3 VALUES(11,44),(33,22);
  41. SELECT x AS y FROM t3 ORDER BY y;
  42. }
  43. } {0 {11 33}}
  44. do_test resolver01-1.4 {
  45. catchsql {
  46. SELECT x AS yy FROM t3 ORDER BY y;
  47. }
  48. } {0 {33 11}}
  49. # SQLite allows the WHERE clause to reference output columns if there is
  50. # no other way to resolve the name.
  51. #
  52. do_test resolver01-1.5 {
  53. catchsql {
  54. SELECT x AS yy FROM t3 ORDER BY yy;
  55. }
  56. } {0 {11 33}}
  57. do_test resolver01-1.6 {
  58. catchsql {
  59. SELECT x AS yy FROM t3 ORDER BY 1;
  60. }
  61. } {0 {11 33}}
  62. # The "ORDER BY y COLLATE nocase" form works the same as "ORDER BY y".
  63. # The "y" binds more tightly to output columns than to input columns.
  64. #
  65. # This is for compatibility with SQL92 and with historical SQLite behavior.
  66. # Note that PostgreSQL considers "y COLLATE nocase" to be an expression
  67. # and thus PostgreSQL treats this case as if it where the 3.x case below.
  68. #
  69. do_test resolver01-2.1 {
  70. catchsql {
  71. SELECT 2 AS y FROM t1, t2 ORDER BY y COLLATE nocase;
  72. }
  73. } {0 2}
  74. do_test resolver01-2.2 {
  75. catchsql {
  76. SELECT 2 AS yy FROM t1, t2 ORDER BY y COLLATE nocase;
  77. }
  78. } {1 {ambiguous column name: y}}
  79. do_test resolver01-2.3 {
  80. catchsql {
  81. SELECT x AS y FROM t3 ORDER BY y COLLATE nocase;
  82. }
  83. } {0 {11 33}}
  84. do_test resolver01-2.4 {
  85. catchsql {
  86. SELECT x AS yy FROM t3 ORDER BY y COLLATE nocase;
  87. }
  88. } {0 {33 11}}
  89. do_test resolver01-2.5 {
  90. catchsql {
  91. SELECT x AS yy FROM t3 ORDER BY yy COLLATE nocase;
  92. }
  93. } {0 {11 33}}
  94. do_test resolver01-2.6 {
  95. catchsql {
  96. SELECT x AS yy FROM t3 ORDER BY 1 COLLATE nocase;
  97. }
  98. } {0 {11 33}}
  99. # But if the form is "ORDER BY expr" then bind more tightly to the
  100. # the input column names and only use the output column names if no
  101. # input column name matches.
  102. #
  103. # This is SQL99 behavior, as implemented by PostgreSQL and MS-SQL.
  104. # Note that Oracle works differently.
  105. #
  106. do_test resolver01-3.1 {
  107. catchsql {
  108. SELECT 3 AS y FROM t1, t2 ORDER BY +y;
  109. }
  110. } {1 {ambiguous column name: y}}
  111. do_test resolver01-3.2 {
  112. catchsql {
  113. SELECT 2 AS yy FROM t1, t2 ORDER BY +y;
  114. }
  115. } {1 {ambiguous column name: y}}
  116. do_test resolver01-3.3 {
  117. catchsql {
  118. SELECT x AS y FROM t3 ORDER BY +y;
  119. }
  120. } {0 {33 11}}
  121. do_test resolver01-3.4 {
  122. catchsql {
  123. SELECT x AS yy FROM t3 ORDER BY +y;
  124. }
  125. } {0 {33 11}}
  126. do_test resolver01-3.5 {
  127. catchsql {
  128. SELECT x AS yy FROM t3 ORDER BY +yy
  129. }
  130. } {0 {11 33}}
  131. # This is the test case given in ticket [f617ea3125e9] (with table name
  132. # changed from "t1" to "t4". The behavior of (1) and (3) match with
  133. # PostgreSQL, but we intentionally break with PostgreSQL to provide
  134. # SQL92 behavior for case (2).
  135. #
  136. do_execsql_test resolver01-4.1 {
  137. CREATE TABLE t4(m CHAR(2));
  138. INSERT INTO t4 VALUES('az');
  139. INSERT INTO t4 VALUES('by');
  140. INSERT INTO t4 VALUES('cx');
  141. SELECT '1', substr(m,2) AS m FROM t4 ORDER BY m;
  142. SELECT '2', substr(m,2) AS m FROM t4 ORDER BY m COLLATE binary;
  143. SELECT '3', substr(m,2) AS m FROM t4 ORDER BY lower(m);
  144. } {1 x 1 y 1 z 2 x 2 y 2 z 3 z 3 y 3 x}
  145. ##########################################################################
  146. # Test cases for ticket [1c69be2dafc28]: Make sure the GROUP BY binds
  147. # more tightly to the input tables in all cases.
  148. #
  149. # This first case case has been wrong in SQLite for time out of mind.
  150. # For SQLite version 3.7.17 the answer was two rows, which is wrong.
  151. #
  152. do_execsql_test resolver01-5.1 {
  153. CREATE TABLE t5(m CHAR(2));
  154. INSERT INTO t5 VALUES('ax');
  155. INSERT INTO t5 VALUES('bx');
  156. INSERT INTO t5 VALUES('cy');
  157. SELECT count(*), substr(m,2,1) AS m FROM t5 GROUP BY m ORDER BY 1, 2;
  158. } {1 x 1 x 1 y}
  159. # This case is unambiguous and has always been correct.
  160. #
  161. do_execsql_test resolver01-5.2 {
  162. SELECT count(*), substr(m,2,1) AS mx FROM t5 GROUP BY m ORDER BY 1, 2;
  163. } {1 x 1 x 1 y}
  164. # This case is not allowed in standard SQL, but SQLite allows and does
  165. # the sensible thing.
  166. #
  167. do_execsql_test resolver01-5.3 {
  168. SELECT count(*), substr(m,2,1) AS mx FROM t5 GROUP BY mx ORDER BY 1, 2;
  169. } {1 y 2 x}
  170. do_execsql_test resolver01-5.4 {
  171. SELECT count(*), substr(m,2,1) AS mx FROM t5
  172. GROUP BY substr(m,2,1) ORDER BY 1, 2;
  173. } {1 y 2 x}
  174. # These test case weere provided in the 2013-08-14 email from Rob Golsteijn
  175. # that originally reported the problem of ticket [1c69be2dafc28].
  176. #
  177. do_execsql_test resolver01-6.1 {
  178. CREATE TABLE t61(name);
  179. SELECT min(name) FROM t61 GROUP BY lower(name);
  180. } {}
  181. do_execsql_test resolver01-6.2 {
  182. SELECT min(name) AS name FROM t61 GROUP BY lower(name);
  183. } {}
  184. do_execsql_test resolver01-6.3 {
  185. CREATE TABLE t63(name);
  186. INSERT INTO t63 VALUES (NULL);
  187. INSERT INTO t63 VALUES ('abc');
  188. SELECT count(),
  189. NULLIF(name,'abc') AS name
  190. FROM t63
  191. GROUP BY lower(name);
  192. } {1 {} 1 {}}
  193. finish_test