Skip to content

Optimizer should push limits past window functions #17346

@avantgardnerio

Description

@avantgardnerio

Is your feature request related to a problem or challenge?

Window functions are cardinality preserving, they just might need to see more data than the limits applied after them, so we should be able to push limits down through them, assuming we know how to grow the limits appropriately.

Describe the solution you'd like

This is fairly trivial if WindowFrameUnits::Rows, so let's start there. Assume we have a plan like:

GlobalLimitExec fetch=5
--BoundedWindowAggExec:[sum(c4) ORDER BY [c4 ASC] ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING 
----SortExec: expr=[c4@0 ASC NULLS LAST], preserve_partitioning=[false]

We should be able to transform it into:

GlobalLimitExec fetch=5
--BoundedWindowAggExec:[sum(c4) ORDER BY [c4 ASC] ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING 
----SortExec: TopK(fetch=7), expr=[c4@0 ASC NULLS LAST], preserve_partitioning=[false]

Describe alternatives you've considered

Ignore opportunities like this.

Additional context

No response

Metadata

Metadata

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions