list_subquery.slt 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 (LIST[1])::text
  20. ### List subqueries ###
  21. statement ok
  22. SELECT LIST(SELECT x FROM xs)::text
  23. query T
  24. SELECT LIST(SELECT x FROM xs)::text
  25. ----
  26. {1,1,2}
  27. query T
  28. SELECT LIST(SELECT x FROM xs WHERE x > 1)::text
  29. ----
  30. {2}
  31. query T
  32. SELECT LIST[LIST(SELECT x FROM xs)]::text
  33. ----
  34. {{1,1,2}}
  35. query T
  36. SELECT LIST(SELECT x FROM xs LIMIT 2)::text
  37. ----
  38. {1,1}
  39. query T
  40. SELECT LIST(SELECT x FROM xs ORDER BY x DESC)::text
  41. ----
  42. {2,1,1}
  43. query T
  44. SELECT LIST(SELECT x FROM xs ORDER BY x DESC LIMIT 2)::text
  45. ----
  46. {2,1}
  47. query T
  48. SELECT LIST(SELECT x FROM xs ORDER BY x DESC LIMIT 1)::text
  49. ----
  50. {2}
  51. query T
  52. SELECT LIST[LIST(SELECT x FROM xs), LIST(SELECT y FROM ys)]::text
  53. ----
  54. {{1,1,2},{2,4,5}}
  55. query T
  56. SELECT list_cat(LIST(SELECT x FROM xs), LIST(SELECT y FROM ys))::text
  57. ----
  58. {1,1,2,2,4,5}
  59. query T
  60. SELECT list_cat(LIST(SELECT x FROM xs), LIST(SELECT y FROM ys ORDER BY y DESC))::text
  61. ----
  62. {1,1,2,5,4,2}
  63. query T
  64. SELECT LIST(SELECT y FROM xs JOIN ys ON xs.x = ys.y)::text
  65. ----
  66. {2}
  67. query T
  68. SELECT LIST(SELECT y FROM xs JOIN ys ON 2*xs.x >= ys.y)::text
  69. ----
  70. {2,2,2,4}
  71. query T
  72. SELECT LIST(SELECT DISTINCT y FROM xs JOIN ys ON 2*xs.x >= ys.y)::text
  73. ----
  74. {2,4}
  75. query T
  76. SELECT LIST(SELECT 1 WHERE FALSE)::text
  77. ----
  78. {}
  79. statement ok
  80. CREATE TABLE zs (z int not null)
  81. query T
  82. SELECT LIST(SELECT z FROM zs)::text;
  83. ----
  84. {}
  85. query T
  86. SELECT LIST(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 LIST(SELECT id FROM customer)::text
  99. ----
  100. {1,2,3}
  101. query T
  102. SELECT LIST(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 LIST(SELECT first_name, last_name FROM customer)::text
  107. # Verify nested lists
  108. query T
  109. SELECT LIST(SELECT LIST[customer.first_name] FROM customer)::text
  110. ----
  111. {{alice},{bob},{charlie}}
  112. # Check CTE syntax can be included in a query
  113. query T
  114. SELECT LIST(WITH usps AS (SELECT 42) SELECT LIST[LIST[customer.first_name, customer.last_name], LIST[customer.zip], LIST[customer.id::text]] FROM customer JOIN users ON customer.id = users.id ORDER BY users.other_field DESC LIMIT 2)::text
  115. ----
  116. {{{alice,lasta},{10003},{1}},{{charlie,lastc},{11217},{3}}}
  117. query T
  118. SELECT LIST(WITH usps AS (SELECT 42) SELECT * FROM usps)::text
  119. ----
  120. {42}
  121. statement ok
  122. CREATE TABLE qs (q int not null)
  123. query T
  124. SELECT LIST(SELECT TRUE FROM(SELECT AVG(0) FROM qs))::text;
  125. ----
  126. {t}
  127. query T
  128. SELECT LIST(SELECT TRUE FROM(SELECT AVG(0) FROM (SELECT FROM qs)))::text;
  129. ----
  130. {t}