Skip to content

Commit b4725d1

Browse files
committed
Add some new simplify tests
1 parent 925629b commit b4725d1

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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), (2), (NULL);
24+
25+
#######
26+
# Positive EXPLAIN cases for SUM(arg + literal) simplification
27+
#######
28+
29+
# Expect to see one COUNT and one SUM in each query below
30+
query TT
31+
EXPLAIN SELECT SUM(column1 + 1), SUM(column1 + 2) FROM sum_simplify_t;
32+
----
33+
logical_plan
34+
01)Projection: __common_expr_1 + __common_expr_2 AS sum(sum_simplify_t.column1 + Int64(1)), __common_expr_1 + Int64(2) * __common_expr_2 AS sum(sum_simplify_t.column1 + Int64(2))
35+
02)--Aggregate: groupBy=[[]], aggr=[[sum(sum_simplify_t.column1) AS __common_expr_1, count(sum_simplify_t.column1) AS __common_expr_2]]
36+
03)----TableScan: sum_simplify_t projection=[column1]
37+
physical_plan
38+
01)ProjectionExec: expr=[__common_expr_1@0 + __common_expr_2@1 as sum(sum_simplify_t.column1 + Int64(1)), __common_expr_1@0 + 2 * __common_expr_2@1 as sum(sum_simplify_t.column1 + Int64(2))]
39+
02)--AggregateExec: mode=Single, gby=[], aggr=[__common_expr_1, __common_expr_2]
40+
03)----DataSourceExec: partitions=1, partition_sizes=[1]
41+
42+
query TT
43+
EXPLAIN SELECT SUM(1 + column1), SUM(column1 + 2) FROM sum_simplify_t;
44+
----
45+
logical_plan
46+
01)Projection: __common_expr_1 + __common_expr_2 AS sum(Int64(1) + sum_simplify_t.column1), __common_expr_1 + Int64(2) * __common_expr_2 AS sum(sum_simplify_t.column1 + Int64(2))
47+
02)--Aggregate: groupBy=[[]], aggr=[[sum(sum_simplify_t.column1) AS __common_expr_1, count(sum_simplify_t.column1) AS __common_expr_2]]
48+
03)----TableScan: sum_simplify_t projection=[column1]
49+
physical_plan
50+
01)ProjectionExec: expr=[__common_expr_1@0 + __common_expr_2@1 as sum(Int64(1) + sum_simplify_t.column1), __common_expr_1@0 + 2 * __common_expr_2@1 as sum(sum_simplify_t.column1 + Int64(2))]
51+
02)--AggregateExec: mode=Single, gby=[], aggr=[__common_expr_1, __common_expr_2]
52+
03)----DataSourceExec: partitions=1, partition_sizes=[1]
53+
54+
#######
55+
# Cases where rewrite should not apply
56+
#######
57+
58+
query TT
59+
EXPLAIN SELECT SUM(DISTINCT column1 + 1), SUM(DISTINCT column1 + 2) FROM sum_simplify_t;
60+
----
61+
logical_plan
62+
01)Aggregate: groupBy=[[]], aggr=[[sum(DISTINCT sum_simplify_t.column1 + Int64(1)), sum(DISTINCT sum_simplify_t.column1 + Int64(2))]]
63+
02)--TableScan: sum_simplify_t projection=[column1]
64+
physical_plan
65+
01)AggregateExec: mode=Single, gby=[], aggr=[sum(DISTINCT sum_simplify_t.column1 + Int64(1)), sum(DISTINCT sum_simplify_t.column1 + Int64(2))]
66+
02)--DataSourceExec: partitions=1, partition_sizes=[1]
67+
68+
query TT
69+
EXPLAIN SELECT SUM(column1 + 1) FILTER (WHERE column1 > 1), SUM(column1 + 2) FILTER (WHERE column1 > 2 ) FROM sum_simplify_t;
70+
----
71+
logical_plan
72+
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))]]
73+
02)--TableScan: sum_simplify_t projection=[column1]
74+
physical_plan
75+
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))]
76+
02)--DataSourceExec: partitions=1, partition_sizes=[1]
77+
78+
# This test should work
79+
query error
80+
SELECT SUM(random() + 1), SUM(random() + 2) FROM sum_simplify_t;
81+
82+
83+
#######
84+
# Reproducers for known issues
85+
#######
86+
87+
# Blocking: single rewritten SUM fails with "Invalid aggregate expression"
88+
query error
89+
SELECT SUM(column1 + 1) FROM sum_simplify_t;
90+
91+
# Blocking: CSE can fail with "No field named ... Valid fields are __common_expr_1"
92+
query error
93+
SELECT SUM(column1), SUM(column1 + 1) FROM sum_simplify_t;
94+
95+
96+
statement ok
97+
DROP TABLE sum_simplify_t;

0 commit comments

Comments
 (0)