Skip to content

enable_optimize_predicate_expression don't work with finalizeAggregation  #14847

@filimonov

Description

@filimonov
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

if (expression_info.is_stateful_function)
return {}; /// give up the optimization when the predicate contains stateful function

bool isStateful() const override
{
return true;
}

Introduced in #3890

@zhang2014 is there some good reason why finalizeAggregation is marked as statefull?

Metadata

Metadata

Assignees

No one assigned

    Labels

    comp-query-optimizerQuery plan optimization: physical plan steps, plan-level rewrites and optimizations (QueryPlan pa...unexpected behaviourResult is unexpected, but not entirely wrong at the same time.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions