Skip to content

Implement window functions with partition_by clause - #558

Merged
alamb merged 1 commit into
apache:masterfrom
jimexist:impl-window-partition-by
Jun 21, 2021
Merged

Implement window functions with partition_by clause#558
alamb merged 1 commit into
apache:masterfrom
jimexist:impl-window-partition-by

Conversation

@jimexist

@jimexistjimexist commented Jun 14, 2021

Copy link
Copy Markdown
Member

Which issue does this PR close?

Closes#299

Rationale for this change

with order by implemented, we can add partition by support.

What changes are included in this PR?

Are there any user-facing changes?

@jimexist
jimexistforce-pushed the impl-window-partition-by branch from e663573 to 7cb72ecCompareJune 14, 2021 09:54
@jimexistjimexist changed the title Impl window partition byImplement window functions with partition_by clauseJun 14, 2021
@codecov-commenter

codecov-commenter commented Jun 14, 2021

Copy link
Copy Markdown

Codecov Report

Merging #558 (4a7a499) into master (e3e7e29) will decrease coverage by 0.03%.
The diff coverage is 81.01%.

Impacted file tree graph

@@ Coverage Diff @@## master #558 +/- ##
==========================================
- Coverage 76.12% 76.08% -0.04% 
==========================================
Files 156 156 Lines 27074 27121 +47 ==========================================
+ Hits 20609 20635 +26 - Misses 6465 6486 +21 
Impacted FilesCoverage Δ
datafusion/src/physical_plan/window_functions.rs86.42% <ø> (+0.71%)⬆️
datafusion/src/sql/planner.rs84.75% <ø> (ø)
datafusion/src/physical_plan/planner.rs79.84% <33.33%> (+2.30%)⬆️
datafusion/src/physical_plan/mod.rs80.00% <70.96%> (+0.90%)⬆️
datafusion/src/physical_plan/windows.rs82.59% <75.00%> (-3.88%)⬇️
...afusion/src/physical_plan/expressions/nth_value.rs79.41% <75.67%> (-11.07%)⬇️
datafusion/src/execution/context.rs92.13% <100.00%> (+0.13%)⬆️
...fusion/src/physical_plan/expressions/row_number.rs94.28% <100.00%> (+13.03%)⬆️
datafusion/src/physical_plan/hash_aggregate.rs86.54% <100.00%> (ø)
datafusion/src/scalar.rs56.19% <100.00%> (ø)
... and 8 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e3e7e29...4a7a499. Read the comment docs.

@jimexist
jimexistforce-pushed the impl-window-partition-by branch 2 times, most recently from 0c6f31f to c3c0ef5CompareJune 15, 2021 00:16
@jimexist
jimexist marked this pull request as ready for review June 15, 2021 00:40
@jimexist
jimexistforce-pushed the impl-window-partition-by branch from 4a7a499 to 1ae529fCompareJune 16, 2021 11:31
@jimexist

Copy link
Copy Markdown
MemberAuthor

@Dandandan and @alamb this is ready now

@jimexist

Copy link
Copy Markdown
MemberAuthor

after this pull request i'll rebase and merge #564 so that we can have a benchmark for future iterations

new_null_array(value.data_type(), num_rows)
} else {
let value = ScalarValue::try_from_array(value, index)?;
value.to_array_of_size(num_rows)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same here applies for normal aggregations as probably happens here: if we have a partition by that creates a lot of groups, we will create many individual arrow arrays (which is slow / memory consuming).

Probably what would be better in the long run is store the offsets to the values in a contiguous array, and the values as well and extend / update them instead.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed for this PR btw, but just noting there are similar needs/performance issues in both aggregation and window functions.

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, not in this pull request but I believe this can warrant a dedicated compute kernel in arrow for batched array slice transformation and then concatenation

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Dandandan down the road I've started to work on this issue:

https://github.com/apache/arrow-datafusion/pull/579/files#diff-8b6b5ea3976c91229244e4e7a31a7026422b1374d1683e44b41af67a6bd43187R246-R254

- let results = partition_points- .iter()- .map(|partition_range| {- let sort_partition_points =- find_ranges_in_range(partition_range, &sort_partition_points);- let mut window_accumulators = self.create_accumulator()?;- sort_partition_points- .iter()- .map(|range| window_accumulators.scan_peers(&values, range))- .collect::<Result<Vec<_>>>()- })- .collect::<Result<Vec<Vec<ArrayRef>>>>()?- .into_iter()- .flatten()- .collect::<Vec<ArrayRef>>();- let results = results.iter().map(|i| i.as_ref()).collect::<Vec<_>>();- concat(&results).map_err(DataFusionError::ArrowError)+ let mut result = Vec::with_capacity(num_rows);+ for partition_range in partition_points {+ let sort_partition_points =+ find_ranges_in_range(&partition_range, &sort_partition_points);+ let mut window_accumulators = self.create_accumulator()?;+ for range in sort_partition_points {+ result.extend(window_accumulators.scan_peers(&values, range)?);+ }+ }+ ScalarValue::iter_to_array(result.into_iter())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - that should probably already be quite an improvement 👍

Comment threaddatafusion/src/execution/context.rs Outdated
@jimexist
jimexistforce-pushed the impl-window-partition-by branch 2 times, most recently from 0ab7340 to 4f98195CompareJune 19, 2021 06:15
@jimexist

Copy link
Copy Markdown
MemberAuthor

@Dandandan this is fixed now

Comment threaddatafusion/src/execution/context.rs Outdated
Comment threaddatafusion/src/execution/context.rs Outdated

@DandandanDandandan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great again! - 2 comments about tests for being a bit more future proof

@jimexist
jimexistforce-pushed the impl-window-partition-by branch from 4f98195 to 2488674CompareJune 19, 2021 11:23
@jimexist

Copy link
Copy Markdown
MemberAuthor

Looks great again! - 2 comments about tests for being a bit more future proof

fixed, about repartition i'll handle that in #569 but so far i'm seeing regressions in performance

@alamb

Copy link
Copy Markdown
Contributor

Thanks @jimexist

@alamb
alamb merged commit 05d5f01 into apache:masterJun 21, 2021
@houqphouqp added datafusion enhancement New feature or request labels Jul 30, 2021
unkloud pushed a commit to unkloud/datafusion that referenced this pull request Mar 23, 2025
…pping` (apache#558)
* Re-implement int decode methods using safe code
* fix
* fix tests
* more tests
* fix
* fix
* re-implement using unsafe read/write unaligned and add benchmark
* lint
* macros
* more macros
* combine macros
* replace another impl with the macro
* fix a regression
* remove zero_value arg from generate_cast_to_signed and rename impl_plain_decoding_int to the original name of make_int_variant_impl
HairstonE pushed a commit to HairstonE/datafusion that referenced this pull request Oct 7, 2025
Bumps [tokio](https://github.com/tokio-rs/tokio) from 1.35.0 to 1.35.1.
- [Release notes](https://github.com/tokio-rs/tokio/releases)
- [Commits](tokio-rs/tokio@tokio-1.35.0...tokio-1.35.1)
---
updated-dependencies:
- dependency-name: tokio
dependency-type: direct:production
update-type: version-update:semver-patch
...
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancementNew feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support window functions with PARTITION BY clause

5 participants

@jimexist@codecov-commenter@alamb@Dandandan@houqp