nuno-faria opened a new pull request, #17528: URL: https://github.com/apache/datafusion/pull/17528
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - N/A. ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> Improves the performance of window functions in more than 3x when there are a large number of distinct partition keys. The performance with few groups remains the same. I believe the performance could be further improved, but it might require deeper changes. Here is a small benchmark: ```rust use datafusion::error::Result; use datafusion::prelude::SessionContext; use tokio::time::Instant; #[tokio::main] async fn main() -> Result<()> { let ctx = SessionContext::new(); ctx.sql("SET datafusion.execution.target_partitions = '1';") .await? .collect() .await?; ctx.sql("create table t (k1 int, k2 int, v int);") .await? .collect() .await?; ctx.sql("insert into t select i as k1, 1 as k2, i as v from generate_series(1, 10000000) t(i);") .await? .collect() .await?; // partition by k1: each key is unique let t = Instant::now(); ctx.sql(" explain analyze select *, rank() over(partition by k1 order by v) as rank from t " ) .await? .collect() .await?; println!("partition by k1: {}s", t.elapsed().as_secs_f64()); // partition by k2: each key is the same let t = Instant::now(); ctx.sql(" explain analyze select *, rank() over(partition by k2 order by v) as rank from t " ) .await? .collect() .await?; println!("partition by k2: {}s", t.elapsed().as_secs_f64()); Ok(()) } ``` ``` # before partition by k1: 62.201990669s partition by k2: 1.89616558s # after partition by k1: 18.177335377s partition by k2: 1.870575527s ``` ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> - Reuse `WindowAggState` to avoid potentially having to create many in `StandardWindowExpr::evaluate_stateful` (`standard.rs`). - Avoid `concat` in `WindowAggState::update` when the existing result in empty (`window_state.rs`). - Use `to_array` instead of `iter_to_array` when an `ArrayRef` only has a single row, in `StandardWindowExpr::evaluate_stateful` (`standard.rs`). - Concat all batches once instead of one at a time, in `get_aggregate_result_out_column` (`bounded_window_agg_exec.rs`). - Use `clone` instead of `slice` when the entire column is used, in `get_aggregate_result_out_column` (`bounded_window_agg_exec.rs`). - Avoid concatenating batches unnecessarily in `PartitionSearcher::update_partition_batch` (`bounded_window_agg_exec.rs`). - Iterate over `IndexMap` and probe `HashMap` instead of the other way around, in `SortedSearch::calculate_n_out_row` (`bounded_window_agg_exec.rs`). ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> Yes. ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. --> No. -- 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-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org