You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "alamb (via GitHub)" <gi...@apache.org> on 2026/06/25 21:15:06 UTC

[I] [EPIC] Improve the per-window performance for large windows [datafusion]

alamb opened a new issue, #23197:
URL: https://github.com/apache/datafusion/issues/23197

   ### Is your feature request related to a problem or challenge?
   
   This ticket tracks various ideas to make evaluating window functions faster for single, often large windows. This has come up several times recently wiht @avantgardnerio @2010YOUY01 and @wirybeaver among others 
   
   The core use case is to make evaluating a query like this fast:
   ```
   SELECT
       -- moving average over the current row and the 5 previous rows
       AVG(cpu_usage) OVER (ORDER BY time ROWS BETWEEN 5 PRECEDING AND CURRENT ROW)  as avg_cpu
   FROM metrics;
   ```
   
   The key property is that the number of rows in individual windows is large as there is no `PARTITION BY` in the window clause (`OVER`)
   
   (note a similar thing can happen when there is a `PARTITION BY` clause but imbalanced window sizes due to skew or small numbers of partitions)
   
   Today, each window is executed in a single DataFusion partition which means:
   1. They are limited to a single core
   2. They are limited to a single machine in distributed environments
   
   
   # Background
   DataFusion has support for many window functions (see [doc link](https://datafusion.apache.org/user-guide/sql/window_functions.html)). Window functions are used like this 
   ```sql
   SELECT
       customer, time, cpu_usage,
       -- moving average over the current row and the 5 previous rows
       AVG(cpu_usage) OVER (PARTITION BY customer ORDER BY time ROWS BETWEEN 5 PRECEDING AND CURRENT ROW)  as avg_cpu
     FROM
       metrics;
   ```
   
   Which results in something like
   ```sql
   +----------+---------------------+-----------+---------+
   | customer | time                | cpu_usage | avg_cpu |
   +----------+---------------------+-----------+---------+
   | acme     | 2026-01-01T00:00:00 | 10.0      | 10.0    |
   | acme     | 2026-01-01T00:01:00 | 20.0      | 15.0    |
   ...
   | globex   | 2026-01-01T00:03:00 | 45.0      | 30.0    |
   | globex   | 2026-01-01T00:04:00 | 55.0      | 35.0    |
   +----------+---------------------+-----------+---------+
   ```
   
   The plan for such a query looks like the following, and typically keeps all cores fully occupied
   ```sql
   BoundedWindowExec(...)
     Repartition (on customer) <--- this is responsible for dividing work among partitions and thus cores
       Sort(customer, time)
          Scan
   ```
   Here is an example from the tests: https://github.com/apache/datafusion/blob/a0e9887550065324320c6fd52001aa23bae67485/datafusion/sqllogictest/test_files/window_topk_pushdown.slt#L112-L118
   
   However, for the case in question, when we remove the `PARTITION BY customer` from the `OVER` clause
   
   ```sql
   SELECT
       customer, time, cpu_usage,
       -- moving average over the current row and the 5 previous rows
       AVG(cpu_usage) OVER (ORDER BY time ROWS BETWEEN 5 PRECEDING AND CURRENT ROW)  as avg_cpu
     FROM
       metrics;
   ```
   
   The plan looks like this (no `Repartition` and it executes in a single core)
   ```sql
   BoundedWindowExec(...) <-- single partition, single core
       Sort(customer, time) 
          Scan
   ```
   
   Here is an example https://github.com/apache/datafusion/blob/a0e9887550065324320c6fd52001aa23bae67485/datafusion/sqllogictest/test_files/window.slt#L4100-L4103
   
   <details><summary>Table definition</summary>
   <p>
   
   ```sql
     CREATE TABLE metrics (
       time TIMESTAMP,
       customer VARCHAR,
       cpu_usage DOUBLE
     );
   
     INSERT INTO metrics VALUES
       (TIMESTAMP '2026-01-01 00:00:00', 'acme', 10.0),
       (TIMESTAMP '2026-01-01 00:01:00', 'acme', 20.0),
       (TIMESTAMP '2026-01-01 00:02:00', 'acme', 30.0),
       (TIMESTAMP '2026-01-01 00:03:00', 'acme', 40.0),
       (TIMESTAMP '2026-01-01 00:04:00', 'acme', 50.0),
       (TIMESTAMP '2026-01-01 00:00:00', 'globex', 15.0),
       (TIMESTAMP '2026-01-01 00:01:00', 'globex', 25.0),
       (TIMESTAMP '2026-01-01 00:02:00', 'globex', 35.0),
       (TIMESTAMP '2026-01-01 00:03:00', 'globex', 45.0),
       (TIMESTAMP '2026-01-01 00:04:00', 'globex', 55.0);
   ```
   
   </p>
   </details> 
   
   
   The performance currently relied 
   
   ### Describe the solution you'd like
   
   * DataFusion can use multiple cores to evaluate window functions for large windows
   * Distributed systems such as Ballista can use multiple machines to compute the window function results in parallel
   * DataFusion can complete window function queries even when the windows are larger than available memory
   
   ### Describe alternatives you've considered
   
   Here are some ideas for improving this usecase:
   1. https://github.com/apache/datafusion/pull/23124 from @Dandandan 
   
   
   ### Additional context
   
     ## Prefix Sums / Prefix Scan
   
     Used for cumulative windows such as `ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW`.
   
     - Paper: [Prefix Sums and Their Applications](https://www.cs.cmu.edu/~guyb/papers/Ble93.pdf)
     - PoC from @avantgardnerio: https://github.com/coralogix/arrow-datafusion/pull/426
   
     ## "Halo" Rows / Parallel Bounded Windows
   
     Used for bounded window frames where each output partition needs some rows from neighboring ranges. Note I don't think "halo rows" is a standard database term -- it means "overlapping partitions with replicated boundary rows"
   
     - Main PoC from @avantgardnerio: https://github.com/apache/datafusion/pull/23026
     - Runtime partition extrema design: https://github.com/apache/datafusion/issues/23089
     - Runtime partition extrema PR: https://github.com/apache/datafusion/pull/23090
     - Dynamic range partitioning design: https://github.com/apache/datafusion/issues/23093
     - Dynamic range partitioning PR: https://github.com/apache/datafusion/pull/23094
   
     ## WindowAggExec Memory / Spilling
   
     Avoid `WindowAggExec` OOMs by processing partitions incrementally and spilling when needed.
   
     - Support spilling for `WindowAggExec`: https://github.com/apache/datafusion/issues/22946 
     - POC from @wirybeaver:  https://github.com/apache/datafusion/pull/22947
   
     ## Window Frame Evaluation / Vectorization
   
     Improve the CPU and memory efficiency for window frame boundary calculations:
   
     - https://github.com/apache/datafusion/issues/7518
   
   ## Intra operator parallelism
    Alternative/complementary approach: keep the logical window as one partition, but parallelize execution inside the operator.
   
      - Paper: [Efficient Processing of Window Functions in Analytical SQL Queries](https://www.vldb.org/pvldb/vol8/p1058-leis.pdf), PVLDB 2015
      - General issue from @2010YOUY01: https://github.com/apache/datafusion/issues/23174
     - Window-specific issue from @2010YOUY01: https://github.com/apache/datafusion/issues/22355
     - PoC from @2010YOUY01: https://github.com/apache/datafusion/pull/22356
     - Related paper on window algorithms: https://www.vldb.org/pvldb/vol8/p1058-leis.pdf
   
     ## Adaptive Query Execution / Runtime-Informed Planning
   
     Relevant if DataFusion wants a general framework for runtime stats, dynamic split points, repartition choices, skew handling, and similar optimizations.
   
     - AQE issue from @avantgardnerio: https://github.com/apache/datafusion/issues/23194
     - AQE-lite PoC from @avantgardnerio: https://github.com/apache/datafusion/pull/23167
   
     ## Related Sort / Merge Parallelism
   
     Relevant because single-partition windows often sit downstream of sort / merge bottlenecks.
   
     - Parallel sort-preserving merge PR from @Dandandan: https://github.com/apache/datafusion/pull/23124


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] [EPIC] Improve window function performance (especially for large windows) [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on issue #23197:
URL: https://github.com/apache/datafusion/issues/23197#issuecomment-5151123526

   I spent some more time cleaning up this ticket / honing the description to try and capture various approaches 
   
   In my opinion if we want to improve window function performance, the most obvious things would be to focus on raw evaluation speed in the core:
   - https://github.com/apache/datafusion/issues/15607
   - https://github.com/apache/datafusion/issues/4904
   
   This doesn't really address parallelizing single window functions within a window (though SegmentTrees can do so)
   
   I also think this would likely be used widely and should be considered for inclusion in the core:
   - https://github.com/apache/datafusion/issues/23093


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] [EPIC] Improve window function performance for large windows [datafusion]

Posted by "rluvaton (via GitHub)" <gi...@apache.org>.
rluvaton commented on issue #23197:
URL: https://github.com/apache/datafusion/issues/23197#issuecomment-4896271118

   Here is my take from my experience after implementing couple of Window Execs that are way more optimized but not for the general case due to the Window API limitation
   
   The requirement for window expressions to get the entire partition in order to output a value is a major thing that prevent from optimizing window expression as it requires both
   1. having all the partition in hand even when the expression might not need that
   2. it requires having it in memory - which takes a lot of memory and you can't do any spilling
   
   It should be similar to `GroupsAccumulator` that hold state and handle that.
   
    
   
    


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] [EPIC] Improve window function performance for large windows [datafusion]

Posted by "akurmustafa (via GitHub)" <gi...@apache.org>.
akurmustafa commented on issue #23197:
URL: https://github.com/apache/datafusion/issues/23197#issuecomment-4836070068

   Yes, as @alamb and @2010YOUY01 said the original idea was to have a support for a window operator where given the input is ordered by either or both by PARTITION BY and ORDER BY clauses; Window function didn't buffer all of the batches at its input to save memory and support streaming.
   
   For a window function with following clause `PARTITION BY <expr1> ORDER BY <expr2>` operator supports input with following orderings:
   - case1: `<expr1>`
   - case2: `<exp2>`
   - case3: `<exp1> + <exp2>`
   
   I think, most of the complexity in the implementation comes from having the support for different use cases. However, I think case 1 and case 2 are mostly for streaming cases. I don't see a benefit for keeping them for non-streaming cases. If we assume input ordering will always be satisfied as in the case 3 (both PARTITION BY AND ORDER BY expression clauses), I think implementation can be simplified and at the end DataFusion can have single Window operator which doesn't expect all input to be buffered. 
   
   As far as I remember, `WindowAggExec` works assumes always case 3 is valid and buffers all of the data at its input. 
   
   I agree to @alamb and @2010YOUY01  that focusing on `WindowAggExec` for improvement is better course and in the future maybe in the planning phase, we can only generate plans that contain `WindowAggExec` then discontinue the `BoundedWindowAggExec`.
   
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] [EPIC] Improve window function performance for large windows [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on issue #23197:
URL: https://github.com/apache/datafusion/issues/23197#issuecomment-4835395681

   > So if we continue investing in vectorization and better parallelization, we could potentially evolve WindowAggExec directly. Its current implementation is relatively minimal, making it easier to extend, and eventually we could replace BoundedWindowAggExec.
   
   In general I think focusing efforts on a single execution operator is probably better than trying to optimize both


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] [EPIC] Improve window function performance for large windows [datafusion]

Posted by "2010YOUY01 (via GitHub)" <gi...@apache.org>.
2010YOUY01 commented on issue #23197:
URL: https://github.com/apache/datafusion/issues/23197#issuecomment-4806401319

   Here is some additional context:
   
   I think we don't have to rely on `BoundedWindowAggExec` in the future.
   
   The reason is that it does not assume the input is fully sorted by the window partition/order keys, so it performs internal sorting. This adds both implementation complexity and execution overhead.
   
   Here is my guess on why such design decision was made before: because the original window aggregation implementation was fairly naive: it buffered all input before continuing execution. As a result, a downstream implementation was upstreamed to support streaming and out-of-order input, where assuming fully sorted input was not possible for their use cases. Given that goal, the additional complexity made sense because it improved memory efficiency.
   
   However, DataFusion's primary use case is still batched analytics. Under that assumption, we can require the window operator to consume input that has already been repartitioned and sorted by the window partition/order keys. This makes the implementation both simpler and faster, and we can achieve the same memory efficiency given bounded window frames.
   
   So if we continue investing in vectorization and better parallelization, we could potentially evolve `WindowAggExec` directly. Its current implementation is relatively minimal, making it easier to extend, and eventually we could replace `BoundedWindowAggExec`.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] [EPIC] Improve window function performance for large windows [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on issue #23197:
URL: https://github.com/apache/datafusion/issues/23197#issuecomment-4835392373

   > Here is my guess on why such design decision was made before: because the original window aggregation implementation was fairly naive: it buffered all input before continuing execution. As a result, a downstream implementation was upstreamed to support streaming and out-of-order input, where assuming fully sorted input was not possible for their use cases. Given that goal, the additional complexity made sense because it improved memory efficiency.
   
   Yes I think that is mostly accurate (I think @akurmustafa / @mustafasrepo  did a lot of the initial work)
   
   I think `BoundedWindowExec` was designed to support streaming sql engines (e.g. where the ExecutionPlan was started and the incrementally produced results as data was incrementally pushed through it)
   
    > However, DataFusion's primary use case is still batched analytics. Under that assumption, we can require the window operator to consume input that has already been repartitioned and sorted by the window partition/order keys. This makes the implementation both simpler and faster, and we can achieve the same memory efficiency given bounded window frames.
   
   I agree it makes the implementation simpler -- I suspect a fully specialized operator could be made faster than spilling / resorting, but I also think we could work on that after we have exhausted all options for making BoundedWindowAggExec faster


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] [EPIC] Improve window function performance for large windows [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on issue #23197:
URL: https://github.com/apache/datafusion/issues/23197#issuecomment-4909766289

   > It should be similar to GroupsAccumulator that hold state and handle that.
   
   As in are you are suggesting we need a new API for function implementations (to augment or replace [`PartitionEvaluator`](https://docs.rs/datafusion/latest/datafusion/logical_expr/trait.PartitionEvaluator.html) ?
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] [EPIC] Improve window function performance (especially for large windows) [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on issue #23197:
URL: https://github.com/apache/datafusion/issues/23197#issuecomment-5193780831

   @avantgardnerio  noted he has a proposed API to help support this work
    - https://github.com/apache/datafusion/pull/24035


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org