should be it

This commit is contained in:
2025-10-24 19:21:19 -05:00
parent a4b23fc57c
commit f09560c7b1
14047 changed files with 3161551 additions and 1 deletions

View File

@@ -0,0 +1,39 @@
# name: test/sql/aggregate/having/having_alias.test
# description: Test aliases in the HAVING clause
# group: [having]
statement ok
PRAGMA enable_verification
query II
SELECT b, sum(a) AS a
FROM (VALUES (1, 0), (1, 1)) t(a, b)
GROUP BY b
HAVING a > 0
ORDER BY ALL
----
0 1
1 1
# if a reference is both a group and an alias, we prefer to bind to the group
statement ok
create table t1(a int);
statement ok
insert into t1 values (42), (84);
query I
select a+1 as a from t1 group by a having a=42;
----
43
statement ok
create table t2(a int);
statement ok
insert into t2 values (42), (84), (42);
query II
select a as b, sum(a) as a from t2 group by b having a=42;
----
42 84

View File

@@ -0,0 +1,80 @@
# name: test/sql/aggregate/having/test_having.test
# description: Test HAVING clause
# group: [having]
statement ok
CREATE TABLE test (a INTEGER, b INTEGER);
statement ok
INSERT INTO test VALUES (11, 22), (13, 22), (12, 21)
# HAVING with condition on group
query IR
SELECT b, SUM(a) AS sum FROM test GROUP BY b HAVING b=21 ORDER BY b;
----
21 12.000000
# HAVING with condition on sum
query IR
SELECT b, SUM(a) FROM test GROUP BY b HAVING SUM(a) < 20 ORDER BY b;
----
21 12.000000
# HAVING with condition on ALIAS
# CONTROVERSIAL: this DOES work in SQLite, but not in PostgreSQL
query II
SELECT b, SUM(a) AS sum FROM test GROUP BY b HAVING sum < 20 ORDER BY b;
----
21 12
# HAVING without alias
query IR
SELECT b, SUM(a) AS sum FROM test GROUP BY b HAVING SUM(a) < 20 ORDER BY b;
----
21 12.000000
# HAVING on column not in aggregate
query IR
SELECT b, SUM(a) AS sum FROM test GROUP BY b HAVING COUNT(*) = 1 ORDER BY b;
----
21 12.000000
# expression in having
query IR
SELECT b, SUM(a) FROM test GROUP BY b HAVING SUM(a)+10>28;
----
22 24.000000
# uncorrelated subquery in having
query IR
SELECT b, SUM(a) FROM test GROUP BY b HAVING SUM(a)>(SELECT SUM(t.a)*0.5 FROM test t);
----
22 24.000000
# correlated subquery in having
query IR
SELECT test.b, SUM(a) FROM test GROUP BY test.b HAVING SUM(a)=(SELECT SUM(a) FROM test t WHERE test.b=t.b) ORDER BY test.b;
----
21 12.000000
22 24.000000
# use outer aggregation in inner subquery
query IR
SELECT test.b, SUM(a) FROM test GROUP BY test.b HAVING SUM(a)*2=(SELECT SUM(a)+SUM(t.a) FROM test t WHERE test.b=t.b) ORDER BY test.b
----
21 12.000000
22 24.000000
# use outer aggregation that hasn't been used yet in subquery
query IR
SELECT test.b, SUM(a) FROM test GROUP BY test.b HAVING SUM(a)*2+2=(SELECT SUM(a)+SUM(t.a)+COUNT(t.a) FROM test t WHERE test.b=t.b) ORDER BY test.b
----
22 24.000000
# ORDER BY subquery
query IR
SELECT test.b, SUM(a) FROM test GROUP BY test.b ORDER BY (SELECT SUM(a) FROM test t WHERE test.b=t.b) DESC;
----
22 24.000000
21 12.000000

View File

@@ -0,0 +1,83 @@
# name: test/sql/aggregate/having/test_scalar_having.test
# description: Test HAVING clause without GROUP BY
# group: [having]
# CONTROVERSIAL: HAVING without GROUP BY works in PostgreSQL, but not in SQLite
# scalar HAVING queries
# constants only
query I
SELECT 42 HAVING 42 > 20
----
42
query I
SELECT 42 HAVING 42 > 80
----
# aggregates
query R
SELECT SUM(42) HAVING AVG(42) > MIN(20)
----
42.000000
query R
SELECT SUM(42) HAVING SUM(42) > SUM(80)
----
query RI
SELECT SUM(42)+COUNT(*)+COUNT(1), 3 HAVING SUM(42)+MAX(20)+AVG(30) > SUM(120)-MIN(100)
----
44.000000 3
# subqueries
query R
SELECT SUM(42) HAVING (SELECT SUM(42)) > SUM(80)
----
statement ok
CREATE TABLE test (a INTEGER, b INTEGER);
statement ok
INSERT INTO test VALUES (11, 22), (13, 22), (12, 21)
# HAVING with column references does not work
# HAVING clause can only contain aggregates
statement error
SELECT a FROM test WHERE a=13 HAVING a > 11
----
# HAVING clause also turns the rest of the query into an aggregate
# thus column references in SELECT clause also produce errors
statement error
SELECT a FROM test WHERE a=13 HAVING SUM(a) > 11
----
# once we produce a sum this works though
query R
SELECT SUM(a) FROM test WHERE a=13 HAVING SUM(a) > 11
----
13.000000
query R
SELECT SUM(a) FROM test WHERE a=13 HAVING SUM(a) > 20
----
# HAVING with single-node aggregation does work, even without GROUP BY
query R
SELECT SUM(a) FROM test HAVING SUM(a)>10;
----
36.000000
query R
SELECT SUM(a) FROM test HAVING SUM(a)<10;
----
query R
SELECT SUM(a) FROM test HAVING COUNT(*)>1;
----
36.000000
query R
SELECT SUM(a) FROM test HAVING COUNT(*)>10;
----