Skip to content

chore: Add helpers for computing OffsetBuffer span, length - #25481

Merged
neilconway merged 5 commits into
apache:mainfrom
neilconway:neilc/chore-offset-span-helpers
Sep 19, 2026
Merged

neilconway merged 5 commits into
apache:mainfrom
neilconway:neilc/chore-offset-span-helpers

Conversation

@neilconway

@neilconway neilconway commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

  • N/A

Rationale for this change

Code that works with Arrow arrays often neglects to account for slicing: that is, all of the array's backing values might not be visible, so code should generally not iterate over values outside the visible range, or assume that the raw length of the backing array is equivalent to the logical length of the array.

To simplify common coding patterns in this area and to encourage correct usage, this PR introduces two helper functions:

  • offset_span returns (first_offset, value_len), the "visible" span described by an OffsetBuffer
  • offset_span_len returns value_len, the number of values covered by an OffsetBuffer

This replaces typical boilerplate required to handle sliced arrays.

What changes are included in this PR?

  • Introduce two new helper functions
  • Adopt those helper functions through DataFusion, replacing hand-written offset arithmetic
  • Simplify or eliminate several helpers that did similar things already (e.g., estimate_byte_data_size in encoding/inner.rs)
  • Add unit tests

What is the testing strategy for this PR?

Existing tests pass; new unit tests added.

Are there any user-facing changes?

No. No behavior changes.

@github-actions github-actions Bot added common Related to common crate functions Changes to functions implementation spark labels Sep 18, 2026
Comment on lines -576 to +573
for i in 0..num_rows {
let start = offsets[i].as_usize() - first_offset;
let end = offsets[i + 1].as_usize() - first_offset;
for (i, window) in offsets.windows(2).enumerate() {
let start = window[0].as_usize() - first_offset;
let end = window[1].as_usize() - first_offset;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I found I had to switch to windows(2) to avoid regressing codegen (by introducing bounds checks that were otherwise eliminated). Using windows(2) is arguably more idiomatic, and I checked other locations -- Codex couldn't find another place where using the helpers regressed codegen.

@neilconway neilconway changed the title chore: Add helpers for computing buffer offset span, length chore: Add helpers for computing OffsetBuffer span, length Sep 18, 2026
@neilconway

Copy link
Copy Markdown
Contributor Author

If we decide these are a net improvement, I think this would be a candidate to add to arrow-rs -- but I'd argue we should land this in DataFusion first, and then we can switch over to the Arrow versions if/when similar helpers are added there.

@codecov-commenter

codecov-commenter commented Sep 18, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.05882% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.38%. Comparing base (ccfe704) to head (d1dfe1f).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
datafusion/functions/src/unicode/substrindex.rs 50.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #25481      +/-   ##
==========================================
- Coverage   82.38%   82.38%   -0.01%     
==========================================
  Files        1138     1138              
  Lines      433733   433696      -37     
  Branches   433733   433696      -37     
==========================================
- Hits       357331   357283      -48     
- Misses      54848    54855       +7     
- Partials    21554    21558       +4     

☔ 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.

@alamb alamb 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.

Thank you @neilconway

Comment thread datafusion/common/src/utils/mod.rs Outdated
if let (Some(first), Some(last)) = (offsets.first(), offsets.last()) {
let first = first.as_usize();
let last = last.as_usize();
let (start, len) = offset_span(list.offsets());

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.

I think this is much easier to read -- thank you @neilconway

@alamb

alamb commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

FYI @rluvaton as I think you are often interested in nested arrays

@neilconway
neilconway added this pull request to the merge queue Sep 19, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Sep 19, 2026
@neilconway
neilconway added this pull request to the merge queue Sep 19, 2026
Merged via the queue into apache:main with commit 24a7f13 Sep 19, 2026
41 checks passed
@neilconway
neilconway deleted the neilc/chore-offset-span-helpers branch September 19, 2026 15:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

common Related to common crate functions Changes to functions implementation spark

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants