-
Notifications
You must be signed in to change notification settings - Fork 8.3k
enable_optimize_predicate_expression don't work with finalizeAggregation #14847
Copy link
Copy link
Closed
Closed
Copy link
Labels
comp-query-optimizerQuery plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa...Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa...unexpected behaviourResult is unexpected, but not entirely wrong at the same time.Result is unexpected, but not entirely wrong at the same time.
Description
DROP TABLE IF EXISTS test_push_down;
CREATE TABLE test_push_down
ENGINE = AggregatingMergeTree
ORDER BY n AS
SELECT
intDiv(number, 25) AS n,
avgState(number) AS s
FROM numbers(2500)
GROUP BY n;
SET enable_debug_queries = 1;
ANALYZE SELECT *
FROM
(
SELECT
n,
finalizeAggregation(s)
FROM test_push_down
)
WHERE (n >= 2) AND (n <= 5)
Result is
SELECT
n,
`finalizeAggregation(s)`
FROM
(
SELECT
n,
finalizeAggregation(s)
FROM test_push_down
)
WHERE (n >= 2) AND (n <= 5)
W/o finalizeAggregation - it was pushed down:
ANALYZE SELECT *
FROM
(
SELECT
n,
s
FROM test_push_down
)
WHERE (n >= 2) AND (n <= 5)
SELECT
n,
s
FROM
(
SELECT
n,
s
FROM test_push_down
WHERE (n <= 5) AND (n >= 2)
)
WHERE (n >= 2) AND (n <= 5)
It looks like the reason is finalizeAggregation is marked as 'stateful' and for stateful functions
ClickHouse/src/Interpreters/PredicateExpressionsOptimizer.cpp
Lines 93 to 94 in 89e9673
| if (expression_info.is_stateful_function) | |
| return {}; /// give up the optimization when the predicate contains stateful function |
ClickHouse/src/Functions/finalizeAggregation.cpp
Lines 36 to 39 in e4689ce
| bool isStateful() const override | |
| { | |
| return true; | |
| } |
Introduced in #3890
@zhang2014 is there some good reason why finalizeAggregation is marked as statefull?
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
comp-query-optimizerQuery plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa...Query plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa...unexpected behaviourResult is unexpected, but not entirely wrong at the same time.Result is unexpected, but not entirely wrong at the same time.