subquery-scalar-errors.td 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #
  10. # Test the detection of the case where a scalar subquery returns more than 1 row
  11. # and make sure there are no false negatives and only the expected false positives.
  12. # Queries prefixed with "!" are expected to return an error while those with ">" are expected to succeed
  13. #
  14. # Some of the queries that fail in Materialize will succeed in Postgresql
  15. #
  16. # See https://github.com/MaterializeInc/database-issues/issues/1745 for a discussion on the remaining
  17. # differences between the two databases
  18. #
  19. $ set-sql-timeout duration=125ms
  20. > CREATE TABLE empty (f1 INTEGER, f2 INTEGER);
  21. > CREATE TABLE one_row (f1 INTEGER, f2 INTEGER);
  22. > INSERT INTO one_row VALUES (1, 1);
  23. > CREATE TABLE two_rows (f1 INTEGER, f2 INTEGER);
  24. > INSERT INTO two_rows VALUES (1, 1), (2, 1);
  25. ! SELECT (SELECT f1 FROM two_rows);
  26. contains:Evaluation error: more than one record produced in subquery
  27. ! SELECT (SELECT f1 FROM two_rows) FROM two_rows LIMIT 0;
  28. contains:Evaluation error: more than one record produced in subquery
  29. ! SELECT (SELECT TRUE FROM two_rows);
  30. contains:Evaluation error: more than one record produced in subquery
  31. ! SELECT (SELECT f1 FROM two_rows) FROM two_rows;
  32. contains:Evaluation error: more than one record produced in subquery
  33. ! SELECT (SELECT f1 FROM two_rows) FROM empty;
  34. contains:Evaluation error: more than one record produced in subquery
  35. ! SELECT (SELECT f1 FROM one_row UNION ALL SELECT f1 FROM one_row) FROM two_rows;
  36. contains:Evaluation error: more than one record produced in subquery
  37. ! SELECT (SELECT f1 FROM empty UNION ALL SELECT f1 FROM two_rows) FROM two_rows;
  38. contains:Evaluation error: more than one record produced in subquery
  39. ! SELECT (SELECT DISTINCT f1 FROM two_rows) FROM two_rows;
  40. contains:Evaluation error: more than one record produced in subquery
  41. > SELECT (SELECT DISTINCT f2 FROM two_rows) FROM two_rows;
  42. 1
  43. 1
  44. > SELECT (SELECT MIN(f1) FROM two_rows) FROM two_rows;
  45. 1
  46. 1
  47. > SELECT (SELECT MIN(f2) FROM two_rows GROUP BY f1 ORDER BY MIN(f2) LIMIT 1);
  48. 1
  49. ! SELECT (SELECT f1 FROM two_rows GROUP BY f1);
  50. contains:Evaluation error: more than one record produced in subquery
  51. ! SELECT (SELECT MIN(f1) FROM two_rows UNION ALL SELECT MIN(f2) FROM two_rows) FROM two_rows;
  52. contains:Evaluation error: more than one record produced in subquery
  53. > SELECT (SELECT MIN(f1) FROM two_rows UNION DISTINCT SELECT MIN(f2) FROM two_rows) FROM two_rows;
  54. 1
  55. 1
  56. ! SELECT MIN((SELECT f1 FROM two_rows)) FROM two_rows;
  57. contains:Evaluation error: more than one record produced in subquery
  58. ! SELECT * FROM two_rows WHERE f1 > (SELECT f1 FROM two_rows);
  59. contains:Evaluation error: more than one record produced in subquery
  60. ! SELECT * FROM two_rows WHERE f1 > (SELECT f1 FROM one_row) AND f1 > (SELECT f1 FROM two_rows);
  61. contains:Evaluation error: more than one record produced in subquery
  62. ! SELECT * FROM two_rows WHERE f1 > (SELECT f1 FROM one_row) OR f1 > (SELECT f1 FROM two_rows);
  63. contains:Evaluation error: more than one record produced in subquery
  64. ! SELECT * FROM two_rows WHERE (SELECT f1 FROM two_rows) > (SELECT f1 FROM one_row);
  65. contains:Evaluation error: more than one record produced in subquery
  66. ! SELECT * FROM two_rows AS o WHERE (SELECT f1 FROM two_rows AS i WHERE o.f2 = i.f2) = 1 AND f1 = 2;
  67. contains:Evaluation error: more than one record produced in subquery
  68. ! SELECT * FROM two_rows JOIN two_rows AS r ON (SELECT TRUE FROM two_rows);
  69. contains:Evaluation error: more than one record produced in subquery
  70. ! SELECT * FROM two_rows LEFT JOIN two_rows AS r ON (SELECT f1 = 1 FROM two_rows);
  71. contains:Evaluation error: more than one record produced in subquery
  72. ! SELECT f1, COUNT(*) as C FROM two_rows GROUP BY f1 HAVING COUNT(*) > (SELECT f1 FROM two_rows);
  73. contains:Evaluation error: more than one record produced in subquery
  74. ! SELECT (SELECT f1 FROM two_rows), COUNT(*) FROM two_rows GROUP BY (SELECT f1 FROM two_rows);
  75. contains:Evaluation error: more than one record produced in subquery
  76. ! SELECT COUNT(*) FROM two_rows GROUP BY (SELECT f1 FROM two_rows);
  77. contains:Evaluation error: more than one record produced in subquery
  78. ! SELECT * FROM two_rows ORDER BY (SELECT f1 FROM two_rows);
  79. contains:Evaluation error: more than one record produced in subquery
  80. ! INSERT INTO two_rows VALUES ((SELECT 1 UNION ALL SELECT 2));
  81. contains:more than one record produced in subquery
  82. ! SELECT (SELECT f1 from two_rows) + 1 FROM two_rows;
  83. contains:Evaluation error: more than one record produced in subquery
  84. > SELECT (SELECT f1 FROM two_rows LIMIT 0) IS NULL;
  85. true
  86. > SELECT (SELECT f1 FROM two_rows LIMIT 0) IS NULL FROM two_rows;
  87. true
  88. true
  89. > SELECT (SELECT f1 FROM two_rows ORDER BY 1 LIMIT 1);
  90. 1
  91. > SELECT (SELECT f1 FROM two_rows ORDER BY 1 LIMIT 1) FROM two_rows;
  92. 1
  93. 1
  94. > SELECT * FROM two_rows AS a1 WHERE 1 = ( SELECT 1 FROM two_rows ORDER BY 1 LIMIT 1 )
  95. 1 1
  96. 2 1
  97. > SELECT (SELECT f1 FROM two_rows EXCEPT SELECT f1 FROM one_row) FROM two_rows;
  98. 2
  99. 2
  100. > SELECT f1 FROM two_rows WHERE f1 = (SELECT f1 FROM two_rows EXCEPT SELECT f1 FROM one_row);
  101. 2
  102. # Two columns returned by subquery in scalar context
  103. ! SELECT (SELECT * FROM one_row) FROM one_row;
  104. contains:Expected subselect to return 1 column, got 2 columns
  105. ! SELECT * FROM one_row WHERE f1 = ( SELECT * FROM one_row );
  106. contains:WHERE clause error: Expected subselect to return 1 column, got 2 columns