scoping.slt 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. mode cockroach
  10. statement ok
  11. CREATE TABLE t1 (a int)
  12. statement ok
  13. CREATE TABLE t2 (a int)
  14. statement ok
  15. CREATE TABLE t3 (a int)
  16. # These queries work in MySQL but not PostgreSQL. We used to support them,
  17. # back before we'd fully committed to PostgreSQL compatibility.
  18. query error column "t1.a" must appear in the GROUP BY clause or be used in an aggregate function
  19. SELECT t1.a FROM t1 JOIN t2 ON t1.a = t2.a GROUP BY t2.a
  20. query error column "t1.a" must appear in the GROUP BY clause or be used in an aggregate function
  21. SELECT t1.a FROM t1 JOIN t2 ON t1.a = t2.a LEFT JOIN t3 ON t2.a = t3.a GROUP BY t2.a
  22. query error column "t1.a" must appear in the GROUP BY clause or be used in an aggregate function
  23. SELECT t1.a FROM t1 JOIN (t2 JOIN t3 ON t2.a = t3.a) ON t1.a = t2.a GROUP BY t3.a
  24. # Ensure that reflexive equality expressions do not cause the specified column
  25. # to become unnameable. See database-issues#1778.
  26. query I
  27. SELECT t1.a FROM t1 JOIN t2 ON t1.a = t1.a GROUP BY t1.a
  28. ----
  29. # This works in PostgreSQL.
  30. query I
  31. SELECT t1.a FROM t1 NATURAL JOIN t2
  32. ----
  33. # This works in PostgreSQL too.
  34. query I
  35. SELECT t2.a FROM t1 NATURAL JOIN t2
  36. ----
  37. # Regression tests for database-issues#4887.
  38. query I
  39. SELECT t.a FROM (t1 NATURAL JOIN t2) t
  40. ----
  41. query I
  42. SELECT t.a FROM (t1 JOIN t2 USING (a)) t
  43. ----
  44. # Test sources with unnamed columns.
  45. statement ok
  46. CREATE VIEW v1 AS SELECT 1
  47. query I
  48. SELECT * FROM v1
  49. ----
  50. 1
  51. query I
  52. SELECT v1."?column?" FROM v1
  53. ----
  54. 1
  55. query I
  56. SELECT v1.* FROM v1
  57. ----
  58. 1
  59. # Test wildcards in SELECT list.
  60. query I
  61. SELECT v1.* FROM v1
  62. ----
  63. 1
  64. query I
  65. SELECT v.* FROM v1 v
  66. ----
  67. 1
  68. query error no table named 'v1' in scope
  69. SELECT v1.* FROM v1 v
  70. query error no table named 'totalgarbage' in scope
  71. SELECT totalgarbage.* FROM v1
  72. query error no table named 'totalgarbage' in scope
  73. SELECT totalgarbage.*
  74. query error SELECT \* with no tables specified is not valid
  75. SELECT *
  76. query error column reference "k" is ambiguous
  77. SELECT k FROM (SELECT 1 AS k, 2 AS k)
  78. # Wildcards on a zero-arity table are ok, though.
  79. statement ok
  80. CREATE TABLE nullary ()
  81. query
  82. SELECT * FROM nullary
  83. ----
  84. # Check that column names propagate through several layers of subqueries.
  85. query T colnames
  86. SELECT (SELECT * FROM (SELECT 1 AS a) _)
  87. ----
  88. a
  89. 1
  90. # Check that the EXISTS operator names its output column as such.
  91. query T colnames
  92. SELECT EXISTS (SELECT 1)
  93. ----
  94. exists
  95. true
  96. # Check that duplicated columns with different names retain their different
  97. # names.
  98. query TT colnames
  99. SELECT column1, column1 as column2 FROM (VALUES (1))
  100. ----
  101. column1 column2
  102. 1 1
  103. # Ensure that table references only expand to columns in the innermost scope.
  104. # Past versions of Materialize had a bug that would cause the following query
  105. # to produce (1,2,3,4).
  106. query T
  107. SELECT (SELECT v FROM (VALUES (1, 2)) v) FROM (VALUES (3, 4)) v
  108. ----
  109. (1,2)
  110. query error column reference "f1" is ambiguous
  111. SELECT * FROM (SELECT 1 f1) s1 CROSS JOIN ((SELECT 2 f1) s2 CROSS JOIN LATERAL (SELECT f1) s3)
  112. query error column reference "a" is ambiguous
  113. SELECT * FROM (SELECT 1 a) t1 CROSS JOIN ((SELECT 1 a) t1 CROSS JOIN LATERAL (SELECT a) t2) t3;
  114. query error column reference "a" is ambiguous
  115. WITH cte1 AS (SELECT 1 AS a, 2 AS a) SELECT * FROM cte1 WHERE cte1.a = 1
  116. query error table reference "t1" is ambiguous
  117. SELECT * FROM (SELECT 1 a) t1 CROSS JOIN ((SELECT 1 a) t1 CROSS JOIN LATERAL (SELECT t1.a) t2) t3;
  118. query error table reference "t1" is ambiguous
  119. SELECT * FROM (SELECT 1 a) t1 CROSS JOIN ((SELECT 1 a) t1 CROSS JOIN LATERAL (SELECT t1) t2) t3;
  120. # Test column name inference corner cases.
  121. query T colnames
  122. SELECT 1::int
  123. ----
  124. int4
  125. 1
  126. query T colnames
  127. SELECT 1::text::int
  128. ----
  129. int4
  130. 1
  131. query T colnames
  132. SELECT CASE WHEN TRUE THEN 1 END
  133. ----
  134. case
  135. 1
  136. query T colnames
  137. SELECT CASE WHEN TRUE THEN 1 ELSE column1 END FROM (VALUES (1))
  138. ----
  139. column1
  140. 1
  141. query T colnames
  142. SELECT CASE WHEN TRUE THEN 1 ELSE 1::int END
  143. ----
  144. case
  145. 1
  146. statement ok
  147. CREATE TYPE scoping_composite AS (i int);
  148. query T colnames
  149. SELECT ROW(1)::scoping_composite
  150. ----
  151. row
  152. (1)