Skip to content

Commit ef3510b

Browse files
committed
Add tests for simplifying multiple aggregate expressions
1 parent 848cd63 commit ef3510b

1 file changed

Lines changed: 176 additions & 0 deletions

File tree

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
#######
19+
# Tests for aggregate optimizations / simplifications
20+
#######
21+
22+
statement ok
23+
CREATE TABLE sum_simplify_t AS VALUES (1, 100), (1, 200), (2, 100), (NULL, NULL);
24+
25+
query I
26+
SELECT SUM(column1 + 1) FROM sum_simplify_t;
27+
----
28+
7
29+
30+
query TT
31+
EXPLAIN SELECT SUM(column1 + 1) FROM sum_simplify_t;
32+
----
33+
logical_plan
34+
01)Aggregate: groupBy=[[]], aggr=[[sum(sum_simplify_t.column1 + Int64(1))]]
35+
02)--TableScan: sum_simplify_t projection=[column1]
36+
physical_plan
37+
01)AggregateExec: mode=Single, gby=[], aggr=[sum(sum_simplify_t.column1 + Int64(1))]
38+
02)--DataSourceExec: partitions=1, partition_sizes=[1]
39+
40+
41+
query II
42+
SELECT SUM(column1), SUM(column1 + 1) FROM sum_simplify_t;
43+
----
44+
4 7
45+
46+
query TT
47+
EXPLAIN SELECT SUM(column1), SUM(column1 + 1) FROM sum_simplify_t;
48+
----
49+
logical_plan
50+
01)Aggregate: groupBy=[[]], aggr=[[sum(sum_simplify_t.column1), sum(sum_simplify_t.column1 + Int64(1))]]
51+
02)--TableScan: sum_simplify_t projection=[column1]
52+
physical_plan
53+
01)AggregateExec: mode=Single, gby=[], aggr=[sum(sum_simplify_t.column1), sum(sum_simplify_t.column1 + Int64(1))]
54+
02)--DataSourceExec: partitions=1, partition_sizes=[1]
55+
56+
57+
query II
58+
SELECT SUM(2+1), SUM(3) FROM sum_simplify_t;
59+
----
60+
12 12
61+
62+
query TT
63+
EXPLAIN SELECT SUM(2+1), SUM(3) FROM sum_simplify_t;
64+
----
65+
logical_plan
66+
01)Projection: __common_expr_1 AS sum(Int64(2) + Int64(1)), __common_expr_1 AS sum(Int64(3))
67+
02)--Aggregate: groupBy=[[]], aggr=[[sum(Int64(3)) AS __common_expr_1]]
68+
03)----TableScan: sum_simplify_t projection=[]
69+
physical_plan
70+
01)ProjectionExec: expr=[__common_expr_1@0 as sum(Int64(2) + Int64(1)), __common_expr_1@0 as sum(Int64(3))]
71+
02)--AggregateExec: mode=Single, gby=[], aggr=[__common_expr_1]
72+
03)----DataSourceExec: partitions=1, partition_sizes=[1]
73+
74+
75+
query II
76+
SELECT SUM(column1 + 1), SUM(column1 + 2) FROM sum_simplify_t;
77+
----
78+
7 10
79+
80+
81+
query TT
82+
EXPLAIN SELECT SUM(column1 + 1), SUM(column1 + 2) FROM sum_simplify_t;
83+
----
84+
logical_plan
85+
01)Aggregate: groupBy=[[]], aggr=[[sum(sum_simplify_t.column1 + Int64(1)), sum(sum_simplify_t.column1 + Int64(2))]]
86+
02)--TableScan: sum_simplify_t projection=[column1]
87+
physical_plan
88+
01)AggregateExec: mode=Single, gby=[], aggr=[sum(sum_simplify_t.column1 + Int64(1)), sum(sum_simplify_t.column1 + Int64(2))]
89+
02)--DataSourceExec: partitions=1, partition_sizes=[1]
90+
91+
query II
92+
SELECT SUM(1 + column1), SUM(column1 + 2) FROM sum_simplify_t;
93+
----
94+
7 10
95+
96+
query TT
97+
EXPLAIN SELECT SUM(1 + column1), SUM(column1 + 2) FROM sum_simplify_t;
98+
----
99+
logical_plan
100+
01)Aggregate: groupBy=[[]], aggr=[[sum(Int64(1) + sum_simplify_t.column1), sum(sum_simplify_t.column1 + Int64(2))]]
101+
02)--TableScan: sum_simplify_t projection=[column1]
102+
physical_plan
103+
01)AggregateExec: mode=Single, gby=[], aggr=[sum(Int64(1) + sum_simplify_t.column1), sum(sum_simplify_t.column1 + Int64(2))]
104+
02)--DataSourceExec: partitions=1, partition_sizes=[1]
105+
106+
# Distinct aggregates
107+
query II
108+
SELECT SUM(DISTINCT column1 + 1), SUM(DISTINCT column1 + 2) FROM sum_simplify_t;
109+
----
110+
5 7
111+
112+
query TT
113+
EXPLAIN SELECT SUM(DISTINCT column1 + 1), SUM(DISTINCT column1 + 2) FROM sum_simplify_t;
114+
----
115+
logical_plan
116+
01)Aggregate: groupBy=[[]], aggr=[[sum(DISTINCT sum_simplify_t.column1 + Int64(1)), sum(DISTINCT sum_simplify_t.column1 + Int64(2))]]
117+
02)--TableScan: sum_simplify_t projection=[column1]
118+
physical_plan
119+
01)AggregateExec: mode=Single, gby=[], aggr=[sum(DISTINCT sum_simplify_t.column1 + Int64(1)), sum(DISTINCT sum_simplify_t.column1 + Int64(2))]
120+
02)--DataSourceExec: partitions=1, partition_sizes=[1]
121+
122+
# filter clauses
123+
124+
query II
125+
SELECT SUM(column1 + 1) FILTER (WHERE column1 > 1), SUM(column1 + 2) FILTER (WHERE column1 > 2) FROM sum_simplify_t;
126+
----
127+
3 NULL
128+
129+
query TT
130+
EXPLAIN SELECT SUM(column1 + 1) FILTER (WHERE column1 > 1), SUM(column1 + 2) FILTER (WHERE column1 > 2 ) FROM sum_simplify_t;
131+
----
132+
logical_plan
133+
01)Aggregate: groupBy=[[]], aggr=[[sum(sum_simplify_t.column1 + Int64(1)) FILTER (WHERE sum_simplify_t.column1 > Int64(1)), sum(sum_simplify_t.column1 + Int64(2)) FILTER (WHERE sum_simplify_t.column1 > Int64(2))]]
134+
02)--TableScan: sum_simplify_t projection=[column1]
135+
physical_plan
136+
01)AggregateExec: mode=Single, gby=[], aggr=[sum(sum_simplify_t.column1 + Int64(1)) FILTER (WHERE sum_simplify_t.column1 > Int64(1)), sum(sum_simplify_t.column1 + Int64(2)) FILTER (WHERE sum_simplify_t.column1 > Int64(2))]
137+
02)--DataSourceExec: partitions=1, partition_sizes=[1]
138+
139+
# Volatile expressions
140+
query RR
141+
SELECT SUM(random() + 1), SUM(random() + 2) FROM sum_simplify_t;
142+
----
143+
6.304091772178 9.234613543033
144+
145+
query TT
146+
EXPLAIN SELECT SUM(random() + 1), SUM(random() + 2) FROM sum_simplify_t;
147+
----
148+
logical_plan
149+
01)Aggregate: groupBy=[[]], aggr=[[sum(random() + Float64(1)) AS sum(random() + Int64(1)), sum(random() + Float64(2)) AS sum(random() + Int64(2))]]
150+
02)--TableScan: sum_simplify_t projection=[]
151+
physical_plan
152+
01)AggregateExec: mode=Single, gby=[], aggr=[sum(random() + Int64(1)), sum(random() + Int64(2))]
153+
02)--DataSourceExec: partitions=1, partition_sizes=[1]
154+
155+
query III
156+
SELECT column2, SUM(column1 + 1), SUM(column1 + 2) FROM sum_simplify_t GROUP BY column2;
157+
----
158+
200 2 3
159+
100 5 7
160+
NULL NULL NULL
161+
162+
query TT
163+
EXPLAIN SELECT column2, SUM(column1 + 1), SUM(column1 + 2) FROM sum_simplify_t GROUP BY column2;
164+
----
165+
logical_plan
166+
01)Aggregate: groupBy=[[sum_simplify_t.column2]], aggr=[[sum(sum_simplify_t.column1 + Int64(1)), sum(sum_simplify_t.column1 + Int64(2))]]
167+
02)--TableScan: sum_simplify_t projection=[column1, column2]
168+
physical_plan
169+
01)AggregateExec: mode=FinalPartitioned, gby=[column2@0 as column2], aggr=[sum(sum_simplify_t.column1 + Int64(1)), sum(sum_simplify_t.column1 + Int64(2))]
170+
02)--RepartitionExec: partitioning=Hash([column2@0], 4), input_partitions=1
171+
03)----AggregateExec: mode=Partial, gby=[column2@1 as column2], aggr=[sum(sum_simplify_t.column1 + Int64(1)), sum(sum_simplify_t.column1 + Int64(2))]
172+
04)------DataSourceExec: partitions=1, partition_sizes=[1]
173+
174+
175+
statement ok
176+
DROP TABLE sum_simplify_t;

0 commit comments

Comments
 (0)