select_having.err.1 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <sql-statement>
  2. --
  3. -- SELECT_HAVING
  4. --
  5. -- load test data
  6. CREATE TABLE test_having (a int, b int, c char(8), d char);
  7. </sql-statement>
  8. <sql-statement>
  9. INSERT INTO test_having VALUES (0, 1, 'XXXX', 'A');
  10. </sql-statement>
  11. <sql-statement>
  12. INSERT INTO test_having VALUES (1, 2, 'AAAA', 'b');
  13. </sql-statement>
  14. <sql-statement>
  15. INSERT INTO test_having VALUES (2, 2, 'AAAA', 'c');
  16. </sql-statement>
  17. <sql-statement>
  18. INSERT INTO test_having VALUES (3, 3, 'BBBB', 'D');
  19. </sql-statement>
  20. <sql-statement>
  21. INSERT INTO test_having VALUES (4, 3, 'BBBB', 'e');
  22. </sql-statement>
  23. <sql-statement>
  24. INSERT INTO test_having VALUES (5, 3, 'bbbb', 'F');
  25. </sql-statement>
  26. <sql-statement>
  27. INSERT INTO test_having VALUES (6, 4, 'cccc', 'g');
  28. </sql-statement>
  29. <sql-statement>
  30. INSERT INTO test_having VALUES (7, 4, 'cccc', 'h');
  31. </sql-statement>
  32. <sql-statement>
  33. INSERT INTO test_having VALUES (8, 4, 'CCCC', 'I');
  34. </sql-statement>
  35. <sql-statement>
  36. INSERT INTO test_having VALUES (9, 4, 'CCCC', 'j');
  37. </sql-statement>
  38. <sql-statement>
  39. SELECT b, c FROM test_having
  40. GROUP BY b, c HAVING count(*) = 1 ORDER BY b, c;
  41. </sql-statement>
  42. <sql-statement>
  43. -- HAVING is effectively equivalent to WHERE in this case
  44. SELECT b, c FROM test_having
  45. GROUP BY b, c HAVING b = 3 ORDER BY b, c;
  46. </sql-statement>
  47. <sql-statement>
  48. SELECT lower(c), count(c) FROM test_having
  49. GROUP BY lower(c) HAVING count(*) > 2 OR min(a) = max(a)
  50. ORDER BY lower(c);
  51. </sql-statement>
  52. <sql-statement>
  53. SELECT c, max(a) FROM test_having
  54. GROUP BY c HAVING count(*) > 2 OR min(a) = max(a)
  55. ORDER BY c;
  56. </sql-statement>
  57. <sql-statement>
  58. -- test degenerate cases involving HAVING without GROUP BY
  59. -- Per SQL spec, these should generate 0 or 1 row, even without aggregates
  60. SELECT min(a), max(a) FROM test_having HAVING min(a) = max(a);
  61. </sql-statement>
  62. <sql-statement>
  63. SELECT min(a), max(a) FROM test_having HAVING min(a) < max(a);
  64. </sql-statement>
  65. <sql-statement>
  66. -- errors: ungrouped column references
  67. SELECT a FROM test_having HAVING min(a) < max(a);
  68. </sql-statement>
  69. -stdin-:<main>: Error: Type annotation
  70. -stdin-:<main>:1:1: Error: At function: AssumeColumnOrder, At function: PgReplaceUnknown, At function: OrderedMap, At function: AsStruct
  71. -- errors: ungrouped column references
  72. ^
  73. -stdin-:<main>:2:8: Error: At function: Member
  74. SELECT a FROM test_having HAVING min(a) < max(a);
  75. ^
  76. -stdin-:<main>:2:8: Error: Member not found: _alias_test_having.a
  77. SELECT a FROM test_having HAVING min(a) < max(a);
  78. ^
  79. <sql-statement>
  80. SELECT 1 AS one FROM test_having HAVING a > 1;
  81. </sql-statement>
  82. -stdin-:<main>: Error: Type annotation
  83. -stdin-:<main>:1:1: Error: At function: AssumeColumnOrder, At function: PgReplaceUnknown, At function: OrderedMap, At function: OrderedFilter, At function: Coalesce, At function: FromPg
  84. SELECT 1 AS one FROM test_having HAVING a > 1;
  85. ^
  86. -stdin-:<main>:1:43: Error: At function: PgResolvedOp
  87. SELECT 1 AS one FROM test_having HAVING a > 1;
  88. ^
  89. -stdin-:<main>:1:41: Error: At function: Member
  90. SELECT 1 AS one FROM test_having HAVING a > 1;
  91. ^
  92. -stdin-:<main>:1:41: Error: Member not found: _alias_test_having.a
  93. SELECT 1 AS one FROM test_having HAVING a > 1;
  94. ^
  95. <sql-statement>
  96. -- the really degenerate case: need not scan table at all
  97. SELECT 1 AS one FROM test_having HAVING 1 > 2;
  98. </sql-statement>
  99. <sql-statement>
  100. SELECT 1 AS one FROM test_having HAVING 1 < 2;
  101. </sql-statement>
  102. <sql-statement>
  103. -- and just to prove that we aren't scanning the table:
  104. SELECT 1 AS one FROM test_having WHERE 1/a = 1 HAVING 1 < 2;
  105. </sql-statement>
  106. <sql-statement>
  107. DROP TABLE test_having;
  108. </sql-statement>