Skip to content

perf: optimize array_empty udf - #23923

Merged
rluvaton merged 3 commits into
apache:mainfrom
rluvaton:optimize-empty-fn
Jul 28, 2026
Merged

perf: optimize array_empty udf#23923
rluvaton merged 3 commits into
apache:mainfrom
rluvaton:optimize-empty-fn

Conversation

@rluvaton

@rluvaton rluvaton commented Jul 27, 2026

Copy link
Copy Markdown
Member

Which issue does this PR close?

N/A

Rationale for this change

I saw that empty implementation is inefficient.
I wrote review comments for more why inefficient.

there are some optimizations that do not require benchmark since they are obvious once you understand, this is one of them

What changes are included in this PR?

rewrote the function to be fast

Are these changes tested?

existing tests

Are there any user-facing changes?

no

@github-actions github-actions Bot added the functions Changes to functions implementation label Jul 27, 2026

fn general_array_empty<O: OffsetSizeTrait>(array: &ArrayRef) -> Result<ArrayRef> {
let result = as_generic_list_array::<O>(array)?
.iter()

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

calling iter on ListArray is slow since it slice each list values which every slice recompute the null count and more

fn general_array_empty<O: OffsetSizeTrait>(array: &ArrayRef) -> Result<ArrayRef> {
let result = as_generic_list_array::<O>(array)?
.iter()
.map(|arr| arr.map(|arr| arr.is_empty()))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

iter also return None for nulls which means that it need to read the null info while it is not needed

let result = as_generic_list_array::<O>(array)?
.iter()
.map(|arr| arr.map(|arr| arr.is_empty()))
.collect::<BooleanArray>();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

collect not using the from_trusted_len_iter helper which is faster

.collect::<BooleanArray>();
let result = as_generic_list_array::<O>(array)?;
// No nulls, just look at the offsets
let is_empty_iter = result.offsets().lengths().map(|n| n == 0);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

this is faster since we only need to look at one buffer and can make it very fast
the values of the list are irrelevent

let (values, _) = output_buffer.into_parts();

// Add the nulls
let result = BooleanArray::new(values, result.nulls().filter(|n| n.null_count() > 0).cloned());

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We don't need to evaluate the null of specific items since they are not relevant, we just need to move them

@codecov-commenter

codecov-commenter commented Jul 27, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.71429% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 80.69%. Comparing base (1c3232c) to head (f21e287).
⚠️ Report is 9 commits behind head on main.

Files with missing lines Patch % Lines
datafusion/functions-nested/src/empty.rs 85.71% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #23923      +/-   ##
==========================================
+ Coverage   80.67%   80.69%   +0.02%     
==========================================
  Files        1095     1095              
  Lines      372376   372626     +250     
  Branches   372376   372626     +250     
==========================================
+ Hits       300397   300687     +290     
+ Misses      54052    53986      -66     
- Partials    17927    17953      +26     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@rluvaton
rluvaton added this pull request to the merge queue Jul 28, 2026
Merged via the queue into apache:main with commit 8fbbfdc Jul 28, 2026
37 checks passed
@rluvaton
rluvaton deleted the optimize-empty-fn branch July 28, 2026 11:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

functions Changes to functions implementation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants