array_subquery.slt 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 xs (x int not null)
  12. statement ok
  13. CREATE TABLE ys (y int not null)
  14. statement ok
  15. INSERT INTO xs VALUES (1), (1), (2);
  16. statement ok
  17. INSERT INTO ys VALUES (2), (4), (5);
  18. statement ok
  19. SELECT (ARRAY[1])::text
  20. ### Array subqueries ###
  21. statement ok
  22. SELECT ARRAY(SELECT x FROM xs)::text
  23. query T
  24. SELECT ARRAY(SELECT x FROM xs)::text
  25. ----
  26. {1,1,2}
  27. query T
  28. SELECT ARRAY(SELECT x FROM xs WHERE x > 1)::text
  29. ----
  30. {2}
  31. query T
  32. SELECT ARRAY[ARRAY(SELECT x FROM xs)]::text
  33. ----
  34. {{1,1,2}}
  35. query T
  36. SELECT ARRAY(SELECT x FROM xs LIMIT 2)::text
  37. ----
  38. {1,1}
  39. query T
  40. SELECT ARRAY(SELECT x FROM xs ORDER BY x DESC)::text
  41. ----
  42. {2,1,1}
  43. query T
  44. SELECT ARRAY(SELECT x FROM xs ORDER BY x DESC LIMIT 2)::text
  45. ----
  46. {2,1}
  47. query T
  48. SELECT ARRAY(SELECT x FROM xs ORDER BY x DESC LIMIT 1)::text
  49. ----
  50. {2}
  51. query T
  52. SELECT ARRAY[ARRAY(SELECT x FROM xs), ARRAY(SELECT y FROM ys)]::text
  53. ----
  54. {{1,1,2},{2,4,5}}
  55. query T
  56. SELECT array_cat(ARRAY(SELECT x FROM xs), ARRAY(SELECT y FROM ys))::text
  57. ----
  58. {1,1,2,2,4,5}
  59. query T
  60. SELECT array_cat(ARRAY(SELECT x FROM xs), ARRAY(SELECT y FROM ys ORDER BY y DESC))::text
  61. ----
  62. {1,1,2,5,4,2}
  63. query T
  64. SELECT ARRAY(SELECT y FROM xs JOIN ys ON xs.x = ys.y)::text
  65. ----
  66. {2}
  67. query T
  68. SELECT ARRAY(SELECT y FROM xs JOIN ys ON 2*xs.x >= ys.y)::text
  69. ----
  70. {2,2,2,4}
  71. query T
  72. SELECT ARRAY(SELECT DISTINCT y FROM xs JOIN ys ON 2*xs.x >= ys.y)::text
  73. ----
  74. {2,4}
  75. query T
  76. SELECT ARRAY(SELECT 1 WHERE FALSE)::text
  77. ----
  78. {}
  79. statement ok
  80. CREATE TABLE zs (z int not null)
  81. query T
  82. SELECT ARRAY(SELECT z FROM zs)::text;
  83. ----
  84. {}
  85. query T
  86. SELECT ARRAY(SELECT AVG(0) FROM zs)::text;
  87. ----
  88. {NULL}
  89. statement ok
  90. CREATE TABLE users (id int not null, other_field int not null)
  91. statement ok
  92. CREATE TABLE customer (id int not null, first_name string not null, last_name string not null, zip string not null)
  93. statement ok
  94. INSERT INTO users VALUES (1, 10), (2, 5), (3, 8);
  95. statement ok
  96. INSERT INTO customer VALUES (1, 'alice', 'lasta', '10003'::text), (2, 'bob', 'lastb', '10013'::text), (3, 'charlie', 'lastc', '11217'::text);
  97. query T
  98. SELECT ARRAY(SELECT id FROM customer)::text
  99. ----
  100. {1,2,3}
  101. query T
  102. SELECT ARRAY(SELECT other_field FROM users ORDER BY id ASC)::text
  103. ----
  104. {10,5,8}
  105. query error Expected subselect to return 1 column, got 2 columns
  106. SELECT ARRAY(SELECT first_name, last_name FROM customer)::text
  107. # Verify nested arrays
  108. query error text\[\]\[\] not yet supported
  109. SELECT ARRAY(SELECT ARRAY[customer.first_name] FROM customer)
  110. # Check CTE syntax can be included in a query
  111. query T
  112. SELECT ARRAY(WITH usps AS (SELECT 42) SELECT customer.first_name FROM customer)
  113. ----
  114. {alice,bob,charlie}
  115. query T
  116. SELECT ARRAY(WITH usps AS (SELECT 42) SELECT * FROM usps)
  117. ----
  118. {42}
  119. statement ok
  120. CREATE TABLE qs (q int not null)
  121. query T
  122. SELECT ARRAY(SELECT TRUE FROM(SELECT AVG(0) FROM qs))::text;
  123. ----
  124. {t}
  125. query T
  126. SELECT ARRAY(SELECT TRUE FROM(SELECT AVG(0) FROM (SELECT FROM qs)))::text;
  127. ----
  128. {t}
  129. query error text list\[\] not yet supported
  130. SELECT ARRAY(SELECT LIST[customer.first_name, customer.last_name, customer.zip, customer.id::text] FROM customer);