@@ -5655,6 +5655,97 @@ select count(null), min(null), max(null), bit_and(NULL), bit_or(NULL), bit_xor(N
56555655----
565656560 NULL NULL NULL NULL NULL NULL NULL
56575657
5658+ statement ok
5659+ create table having_test(v1 int, v2 int)
5660+
5661+ statement ok
5662+ create table join_table(v1 int, v2 int)
5663+
5664+ statement ok
5665+ insert into having_test values (1, 2), (2, 3), (3, 4)
5666+
5667+ statement ok
5668+ insert into join_table values (1, 2), (2, 3), (3, 4)
5669+
5670+
5671+ query II
5672+ select * from having_test group by v1, v2 having max(v1) = 3
5673+ ----
5674+ 3 4
5675+
5676+ query TT
5677+ EXPLAIN select * from having_test group by v1, v2 having max(v1) = 3
5678+ ----
5679+ logical_plan
5680+ 01)Projection: having_test.v1, having_test.v2
5681+ 02)--Filter: max(having_test.v1) = Int32(3)
5682+ 03)----Aggregate: groupBy=[[having_test.v1, having_test.v2]], aggr=[[max(having_test.v1)]]
5683+ 04)------TableScan: having_test projection=[v1, v2]
5684+ physical_plan
5685+ 01)ProjectionExec: expr=[v1@0 as v1, v2@1 as v2]
5686+ 02)--CoalesceBatchesExec: target_batch_size=8192
5687+ 03)----FilterExec: max(having_test.v1)@2 = 3
5688+ 04)------AggregateExec: mode=FinalPartitioned, gby=[v1@0 as v1, v2@1 as v2], aggr=[max(having_test.v1)]
5689+ 05)--------CoalesceBatchesExec: target_batch_size=8192
5690+ 06)----------RepartitionExec: partitioning=Hash([v1@0, v2@1], 4), input_partitions=4
5691+ 07)------------RepartitionExec: partitioning=RoundRobinBatch(4), input_partitions=1
5692+ 08)--------------AggregateExec: mode=Partial, gby=[v1@0 as v1, v2@1 as v2], aggr=[max(having_test.v1)]
5693+ 09)----------------MemoryExec: partitions=1, partition_sizes=[1]
5694+
5695+
5696+ query error
5697+ select * from having_test having max(v1) = 3
5698+
5699+ query I
5700+ select max(v1) from having_test having max(v1) = 3
5701+ ----
5702+ 3
5703+
5704+ query I
5705+ select max(v1), * exclude (v1, v2) from having_test having max(v1) = 3
5706+ ----
5707+ 3
5708+
5709+ # because v1, v2 is not in the group by clause, the sql is invalid
5710+ query III
5711+ select max(v1), * replace ('v1' as v3) from having_test group by v1, v2 having max(v1) = 3
5712+ ----
5713+ 3 3 4
5714+
5715+ query III
5716+ select max(v1), t.* from having_test t group by v1, v2 having max(v1) = 3
5717+ ----
5718+ 3 3 4
5719+
5720+ # j.* should also be included in the group-by clause
5721+ query error
5722+ select max(t.v1), j.* from having_test t join join_table j on t.v1 = j.v1 group by t.v1, t.v2 having max(t.v1) = 3
5723+
5724+ query III
5725+ select max(t.v1), j.* from having_test t join join_table j on t.v1 = j.v1 group by j.v1, j.v2 having max(t.v1) = 3
5726+ ----
5727+ 3 3 4
5728+
5729+ # If the select items only contain scalar expressions, the having clause is valid.
5730+ query P
5731+ select now() from having_test having max(v1) = 4
5732+ ----
5733+
5734+ # If the select items only contain scalar expressions, the having clause is valid.
5735+ query I
5736+ select 0 from having_test having max(v1) = 4
5737+ ----
5738+
5739+ # v2 should also be included in group-by clause
5740+ query error
5741+ select * from having_test group by v1 having max(v1) = 3
5742+
5743+ statement ok
5744+ drop table having_test
5745+
5746+ statement ok
5747+ drop table join_table
5748+
56585749# test min/max Float16 without group expression
56595750query RRTT
56605751WITH data AS (
0 commit comments