select_table_alias.slt 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # Copyright 2015 - 2019 The Cockroach Authors. All rights reserved.
  2. # Copyright Materialize, Inc. and contributors. All rights reserved.
  3. #
  4. # Use of this software is governed by the Business Source License
  5. # included in the LICENSE file at the root of this repository.
  6. #
  7. # As of the Change Date specified in that file, in accordance with
  8. # the Business Source License, use of this software will be governed
  9. # by the Apache License, Version 2.0.
  10. #
  11. # This file is derived from the logic test suite in CockroachDB. The
  12. # original file was retrieved on June 10, 2019 from:
  13. #
  14. # https://github.com/cockroachdb/cockroach/blob/d2f7fbf5dd1fc1a099bbad790a2e1f7c60a66cc3/pkg/sql/logictest/testdata/logic_test/select_table_alias
  15. #
  16. # The original source code is subject to the terms of the Apache
  17. # 2.0 license, a copy of which can be found in the LICENSE file at the
  18. # root of this repository.
  19. mode cockroach
  20. simple conn=mz_system,user=mz_system
  21. ALTER SYSTEM SET unsafe_enable_table_keys = true
  22. ----
  23. COMPLETE 0
  24. # Tests for SELECT with table aliasing.
  25. statement ok
  26. CREATE TABLE abc (a INT PRIMARY KEY, b INT, c INT)
  27. statement ok
  28. INSERT INTO abc VALUES (1, 2, 3), (4, 5, 6)
  29. # Verify output column naming with *.
  30. query III colnames,rowsort
  31. SELECT * FROM abc
  32. ----
  33. a b c
  34. 1 2 3
  35. 4 5 6
  36. query III colnames,rowsort
  37. SELECT * FROM abc AS foo
  38. ----
  39. a b c
  40. 1 2 3
  41. 4 5 6
  42. query III colnames,rowsort
  43. SELECT * FROM abc AS foo (foo1)
  44. ----
  45. foo1 b c
  46. 1 2 3
  47. 4 5 6
  48. query III colnames,rowsort
  49. SELECT * FROM abc AS foo (foo1, foo2)
  50. ----
  51. foo1 foo2 c
  52. 1 2 3
  53. 4 5 6
  54. query III colnames,rowsort
  55. SELECT * FROM abc AS foo (foo1, foo2, foo3)
  56. ----
  57. foo1 foo2 foo3
  58. 1 2 3
  59. 4 5 6
  60. # Verify qualified name resolution.
  61. query IIII colnames,rowsort
  62. SELECT foo1, foo.foo1, b, foo.c FROM abc AS foo (foo1)
  63. ----
  64. foo1 foo1 b c
  65. 1 1 2 3
  66. 4 4 5 6
  67. query III colnames,rowsort
  68. SELECT * FROM abc AS foo (foo1, foo2) WHERE foo.foo1 = 1
  69. ----
  70. foo1 foo2 c
  71. 1 2 3
  72. query III colnames,rowsort
  73. SELECT * FROM abc AS foo (foo1, foo2) WHERE foo.foo2 = 2
  74. ----
  75. foo1 foo2 c
  76. 1 2 3
  77. query III colnames,rowsort
  78. SELECT * FROM abc AS foo (foo1, foo2) WHERE foo.c = 6
  79. ----
  80. foo1 foo2 c
  81. 4 5 6
  82. # Verify we can't resolve columns using overridden table or colum names.
  83. query error column "abc.foo1" does not exist
  84. SELECT abc.foo1 FROM abc AS foo (foo1)
  85. query error column "abc.b" does not exist
  86. SELECT abc.b FROM abc AS foo (foo1)
  87. query error column "foo.a" does not exist
  88. SELECT foo.a FROM abc AS foo (foo1)
  89. # Verify error for too many column aliases.
  90. query error pgcode 42P10 foo has 3 columns available but 4 columns specified
  91. SELECT * FROM abc AS foo (foo1, foo2, foo3, foo4)
  92. # Verify that implicit columns don't interfere with aliasing.
  93. statement ok
  94. CREATE TABLE ab (a INT, b INT)
  95. statement ok
  96. INSERT INTO ab VALUES (1, 2), (1, 3), (2, 5)
  97. query II colnames,rowsort
  98. SELECT * FROM ab AS foo (foo1, foo2)
  99. ----
  100. foo1 foo2
  101. 1 2
  102. 1 3
  103. 2 5
  104. # NOTE(benesch): rowid is a CockroachDB-ism that we are unlikely to support.
  105. #
  106. # statement ok
  107. # SELECT rowid, foo.rowid FROM ab AS foo (foo1, foo2)
  108. #
  109. # query error no data source matches prefix: ab
  110. # SELECT ab.rowid FROM ab AS foo (foo1)
  111. query error foo has 2 columns available but 3 columns specified
  112. SELECT * FROM ab AS foo (foo1, foo2, foo3)
  113. # TODO(benesch): support scalar functions in table position.
  114. #
  115. # query T colnames
  116. # SELECT * FROM length('abc') AS x
  117. # ----
  118. # x
  119. # 3
  120. #
  121. # query T colnames
  122. # TABLE ROWS FROM length('abc') AS x
  123. # ----
  124. # x
  125. # 3