Skip to content

Push down common aggregation filters #18774

@pepijnve

Description

@pepijnve

Is your feature request related to a problem or challenge?

When a SQL query contains multiple aggregations with filters, those filters are currently not pushed down. For instance in a query like

select
  sum(a) filter (where a > 1 and a % 2 = 0),
  count(a) filter (where a > 1)
from t;

a > 1 is common for both aggregates so the query could be rewritten as

select
  sum(a) filter (where a % 2 = 0),
  count(a)
from t
where a > 1;

Describe the solution you'd like

When all aggregations of an aggregate node share partial or complete filter expressions, extract the common parts to a filter node. Rather than executing the above query as

AggregateExec: mode=Single, gby=[], aggr=[sum(t.a) FILTER (WHERE t.a > Int64(1) AND t.a % Int64(2) = Int64(0)), count(t.a) FILTER (WHERE t.a > Int64(1))]
  ProjectionExec: expr=[column1@0 > 1 as __common_expr_1, column1@0 as a]
    DataSourceExec: partitions=1, partition_sizes=[1]

it could be executed as

AggregateExec: mode=Single, gby=[], aggr=[sum(t.a) FILTER (WHERE t.a % Int64(2) = Int64(0)), count(t.a)]
  ProjectionExec: expr=[column1@0 as a]
    CoalesceBatchesExec: target_batch_size=8192
      FilterExec: column1@0 > 1
        DataSourceExec: partitions=1, partition_sizes=[1]

Describe alternatives you've considered

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions