Skip to content

Support Utf8View and BinaryView in substring - #10672

Merged
Jefffrey merged 2 commits into
apache:mainfrom
1fanwang:feat/substring-view-types
Aug 14, 2026
Merged

Jefffrey merged 2 commits into
apache:mainfrom
1fanwang:feat/substring-view-types

Conversation

@1fanwang

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

substring() takes Utf8, LargeUtf8, Binary, LargeBinary, FixedSizeBinary and Dictionary, but rejects the view types, so a caller holding a StringViewArray has to cast to Utf8 first and pay for the copy. The other string kernels in this crate already accept them: length, bit_length, like and concat_elements all have Utf8View arms.

What changes are included in this PR?

Utf8View and BinaryView arms. Views carry their own offsets rather than sharing one value buffer, so the byte range is computed per element and the result is built through the view builder. The existing Dictionary arm recurses, so a dictionary of views now works too.

The utf-8 boundary check applies to Utf8View the same way it does to Utf8. The offset quoted in that error is per element, because there is no shared buffer to index into.

Are these changes tested?

Yes. The view result is compared against the Utf8 and Binary result for the same inputs, so the two paths cannot drift: empty strings, nulls, values longer than the twelve-byte inline limit, zero length, lengths past the end, and starts past either end in both directions. Plus the boundary error and a dictionary of views.

The cross-check earned its keep — it caught the error offsets differing between the two paths, which is what prompted the note above.

Tests
$ cargo test -p arrow-string
186 passed; 0 failed

Removing the two new match arms fails exactly the four new tests, so they cannot pass vacuously:

test substring::tests::string_view_matches_utf8 ... FAILED
test substring::tests::binary_view_matches_binary ... FAILED
test substring::tests::string_view_rejects_an_invalid_char_boundary ... FAILED
test substring::tests::dictionary_of_string_view ... FAILED

substring() handles Utf8, LargeUtf8, Binary, LargeBinary, FixedSizeBinary and
Dictionary, but rejects the view types with "substring does not support type
Utf8View". The sibling kernels in this crate already take them: length,
bit_length, like and concat_elements all have Utf8View arms.

Views carry their own offsets rather than sharing one value buffer, so the byte
range is computed per element and the result is built through the view builder.
The utf-8 boundary check applies to Utf8View as it does to Utf8; the offset in
that error is per element, since there is no shared buffer to index.

Tested by comparing the view result against the Utf8 and Binary results over the
same inputs, including empty strings, nulls, values longer than the twelve-byte
inline limit, zero length, and starts past either end.

Signed-off-by: 1fanwang <[email protected]>
@github-actions github-actions Bot added arrow Changes to the arrow crate arrow-string labels Aug 13, 2026
@Jefffrey Jefffrey added the enhancement New feature/addition label Aug 13, 2026
Comment thread arrow-string/src/substring.rs Resolved
Comment thread arrow-string/src/substring.rs Resolved
Comment thread arrow-string/src/substring.rs Outdated

#[test]
fn binary_view_matches_binary() {
let values: Vec<Option<&[u8]>> = vec![Some(b"hello world"), Some(b""), None, Some(b"abc")];

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.

should add a long one here too

@1fanwang 1fanwang Aug 13, 2026 •

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.

Done in 336f3d5

}

#[test]
fn string_view_rejects_an_invalid_char_boundary() {

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.

do we have a positive case, for slicing on a valid boundary that isnt only ascii?

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.

Added in 336f3d5

@Jefffrey
Jefffrey merged commit e6dbdbd into apache:main Aug 14, 2026
26 checks passed
@Jefffrey

Copy link
Copy Markdown
Contributor

thanks @1fanwang

MassivePizza pushed a commit to massive-com/arrow-rs that referenced this pull request Aug 19, 2026
# Which issue does this PR close?

- Closes apache#10671.

# Rationale for this change

`substring()` takes `Utf8`, `LargeUtf8`, `Binary`, `LargeBinary`,
`FixedSizeBinary` and `Dictionary`, but rejects the view types, so a
caller holding a `StringViewArray` has to cast to `Utf8` first and pay
for the copy. The other string kernels in this crate already accept
them: `length`, `bit_length`, `like` and `concat_elements` all have
`Utf8View` arms.

# What changes are included in this PR?

`Utf8View` and `BinaryView` arms. Views carry their own offsets rather
than sharing one value buffer, so the byte range is computed per element
and the result is built through the view builder. The existing
`Dictionary` arm recurses, so a dictionary of views now works too.

The utf-8 boundary check applies to `Utf8View` the same way it does to
`Utf8`. The offset quoted in that error is per element, because there is
no shared buffer to index into.

# Are these changes tested?

Yes. The view result is compared against the `Utf8` and `Binary` result
for the same inputs, so the two paths cannot drift: empty strings,
nulls, values longer than the twelve-byte inline limit, zero length,
lengths past the end, and starts past either end in both directions.
Plus the boundary error and a dictionary of views.

The cross-check earned its keep — it caught the error offsets differing
between the two paths, which is what prompted the note above.

<details><summary>Tests</summary>

```
$ cargo test -p arrow-string
186 passed; 0 failed
```

Removing the two new match arms fails exactly the four new tests, so
they cannot pass vacuously:

```
test substring::tests::string_view_matches_utf8 ... FAILED
test substring::tests::binary_view_matches_binary ... FAILED
test substring::tests::string_view_rejects_an_invalid_char_boundary ... FAILED
test substring::tests::dictionary_of_string_view ... FAILED
```

</details>

---------

Signed-off-by: 1fanwang <[email protected]>
Phoenix500526 pushed a commit to Phoenix500526/arrow-rs that referenced this pull request Nov 4, 2026
…ng (apache#12006)

# Which issue does this PR close?

- Follow-up to apache#10995, which made the other `substring` arms saturate
large lengths. The `Utf8View` / `BinaryView` arms added in apache#10672 were
missed.

# Rationale for this change

`view_substring_range` computed the end as
`new_start.saturating_add(length as i64)`. For `length > i64::MAX` the
cast wraps negative, so the end lands before the start:

- `substring(&BinaryViewArray["hello"], 1, Some(u64::MAX))` panics with
`slice index starts at 1 but ends at 0`;
- the same call on `StringViewArray` returns `ComputeError("The offset
9223372036854775808 is at an invalid utf-8 boundary.")`.

`LargeUtf8` and `LargeBinary` return `"ello"` for the same input.

# What changes are included in this PR?

Use the existing `u64_to_i64_saturating(length)` in
`view_substring_range`, like the other arms.

# Are these changes tested?

Yes. A new test checks that `Utf8View` matches `LargeUtf8` and
`BinaryView` matches `LargeBinary` for several starts (including
negative and beyond the end) with lengths `2^63` and `u64::MAX`.

# Are there any significant user-facing changes?

No.

---

AI disclosure: the bug was found and the patch and test drafted with AI
assistance (Claude); I reviewed every line, verified the failing test
before/after the fix, and ran fmt/clippy/tests for the crate.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arrow Changes to the arrow crate arrow-string enhancement New feature/addition

Projects

None yet

Development

Successfully merging this pull request may close these issues.

substring does not support Utf8View or BinaryView

2 participants