Skip to content

fix(arrow-string): saturate the offset casts in substring - #10995

Open
Cintu07 wants to merge 1 commit into
apache:mainfrom
Cintu07:fix/substring-saturate-offset-cast
Open

fix(arrow-string): saturate the offset casts in substring#10995
Cintu07 wants to merge 1 commit into
apache:mainfrom
Cintu07:fix/substring-saturate-offset-cast

Conversation

@Cintu07

@Cintu07 Cintu07 commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

substring takes start as i64 and length as Option and cast both straight into
the offset type. on the Utf8 and Binary arms a start at or above 2^31 wrapped
negative, and byte_substring reads a negative start as counting from the end of
the value, so the call did not fail, it quietly did something else. LargeUtf8 and
LargeBinary narrow to i64 and were not affected, so the same call returned
different data depending only on the offset width of the input.

saturating rather than rejecting, per the discussion on the issue. a start past the
end of every value is what the caller asked for, and it is already what the 64 bit
arms do.

What changes are included in this PR?

start and length saturate into the offset type at the dispatch instead of being
cast. inside byte_substring the two additions that can carry a saturated value
past the offset type go through checked_add and clamp to the end of the value.

one thing i did not expect. the length cast was wrong on all four arms, not only
the 32 bit ones. u64::MAX as i64 is -1, and a negative length puts the end of a
substring before its start, which drives the output offsets negative and then
allocates on the result of as_usize. i only found it because the test compares the
narrow and wide arms against each other and the wide one panicked. so LargeUtf8 and
LargeBinary are fixed here too.

the third addition, pair[1] + start on the negative branch, is left alone. pair[1]
is non-negative and start is at worst i32::MIN, so it cannot overflow.

Are these changes tested?

yes. out_of_range_start_and_length_match_the_64_bit_arms runs four out of range
starts against three lengths, on Utf8 against LargeUtf8 and on Binary against
LargeBinary, and asserts the pairs agree. it also pins the answer itself, since
agreeing on the wrong result would still pass. skipping 2^31 characters of a five
character string gives empty strings, and a start that already fits is untouched.

on current main that test fails twice. Utf8 returns ["hello", "world"] where
LargeUtf8 returns ["", ""], and Some(u64::MAX) panics inside MutableBuffer.

arrow-string is 189 passed, and fmt and clippy with -D warnings are clean.

Are there any user-facing changes?

yes, for input that was previously wrong. a start or length outside the offset type
now saturates, so Utf8 and Binary return what LargeUtf8 and LargeBinary already
returned. values that fit are unaffected.

substring takes start as i64 and length as Option<u64> and cast both
straight into the offset type. On the Utf8 and Binary arms a start at or
above 2^31 wrapped negative, and byte_substring reads a negative start as
counting from the end of the value, so the call did not fail, it quietly
did something else. LargeUtf8 and LargeBinary narrow to i64 and were not
affected, which meant the same call returned different data depending on
the offset width of the input.

Saturate instead. A start past the end of every value is what the caller
asked for and what the 64 bit arms already do.

The length cast was wrong on all four arms, not just the 32 bit ones.
u64::MAX as i64 is -1, and a negative length puts the end of a substring
before its start, which drives the output offsets negative and then
allocates on the result of as_usize.

Inside byte_substring the two additions that can carry a saturated value
past the offset type now go through checked_add and clamp to the end of
the value. The third, pair[1] + start on the negative branch, cannot
overflow, since pair[1] is non-negative and start is at worst i32::MIN.
Copilot AI lite review requested due to automatic review settings September 5, 2026 04:06

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions github-actions Bot added arrow Changes to the arrow crate arrow-string labels Sep 5, 2026
@Jefffrey Jefffrey added the bug label Sep 6, 2026
@Jefffrey

Jefffrey commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

run benchmark substring_kernels

@adriangbot

This comment was marked as duplicate.

@adriangbot

Copy link
Copy Markdown

🤖 Arrow criterion benchmark completed (GKE) | trigger

Instance: c4a-highmem-16 (12 vCPU / 65 GiB)

Comparing fix/substring-saturate-offset-cast (4136775) to b1aa94b (merge-base) diff

Run configuration
run benchmark substring_kernels
CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  16
On-line CPU(s) list:                     0-15
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     16
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               1 MiB (16 instances)
L1i cache:                               1 MiB (16 instances)
L2 cache:                                32 MiB (16 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-15
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected
Details

group                                               fix_substring-saturate-offset-cast      main
-----                                               ----------------------------------      ----
substring by char (ascii, prefix)                   1.03      4.2±0.30ms        ? ?/sec     1.00      4.0±0.22ms        ? ?/sec
substring by char (ascii, tail)                     1.00      4.1±0.13ms        ? ?/sec     1.08      4.5±0.35ms        ? ?/sec
substring by char (non-ascii, prefix)               1.01  1498.5±14.91µs        ? ?/sec     1.00  1488.9±35.72µs        ? ?/sec
substring by char (non-ascii, tail)                 1.10  1632.5±117.14µs        ? ?/sec    1.00  1486.7±17.73µs        ? ?/sec
substring fixed size binary array                   1.01     20.0±1.11ms        ? ?/sec     1.00     19.7±0.86ms        ? ?/sec
substring utf8 (start = 0, length = None)           1.22     26.4±0.90ms        ? ?/sec     1.00     21.6±1.15ms        ? ?/sec
substring utf8 (start = 1, length = str_len - 1)    1.08     24.7±1.45ms        ? ?/sec     1.00     23.0±1.39ms        ? ?/sec
substring utf8 by char                              1.11     29.6±1.34ms        ? ?/sec     1.00     26.7±1.09ms        ? ?/sec

Resource Usage

base (merge-base)

Metric Value
Wall time 90.0s
Peak memory 275.6 MiB
Avg memory 222.4 MiB
CPU user 51.7s
CPU sys 35.2s
Peak spill 0 B

branch

Metric Value
Wall time 85.0s
Peak memory 275.9 MiB
Avg memory 220.8 MiB
CPU user 50.7s
CPU sys 32.4s
Peak spill 0 B

File an issue against this benchmark runner

@Cintu07

Cintu07 commented Sep 6, 2026

Copy link
Copy Markdown
Contributor Author

i ran it on my machine too, aarch64.

different rows moved for me. your +22% row came out +1.8%. instead by char
(non-ascii, prefix) was +11.7%.

so i ran the same branch again against the same baseline, no code change at all
between the two runs:

by char (non-ascii, prefix) +53%
by char (non-ascii, tail) +31%
by char (ascii, prefix) -3.7% improved
by char (ascii, tail) -2.3% improved

same code both times. so this bench can swing 50% on my box by itself.

the row that runs the new branches is start = 1, length = str_len - 1. run one was
+0.06% at p 0.96, run two -1.35% at p 0.22. it did not move either time.

my laptop is noisier than your runner, sure. but every by char row goes through
substring_by_char_impl and the diff never touches that function, so those cannot be
my change.

if you would rather not have the extra branch in the loop at all, i can move the
checks out. start and length do not change per row, they only need checking once.

ryux1

This comment was marked as spam.

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 bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

substring silently wraps start and length to i32 on Utf8 and Binary, so the same call returns different data than on LargeUtf8

5 participants