Repository navigation
[sqllib] Fix binary substring boundary calculation for non-positive start index - #7831
Merged
Merged
Conversation
Contributor
|
This looks fine, but can you also please update the documentation for substring in binary.md? |
Contributor
|
Also check string.md if it needs updates. |
…tart index Fixes feldera#7830 Signed-off-by: Mihai Budiu <mbudiu@feldera.com>
mihaibudiu
force-pushed
the
fix-binary-substring-bounds
branch
from
November 6, 2026 03:25
39071ba to
f5e27d7
Compare
Contributor
|
I took the liberty to amend this commit adding documentation. Now I plan to merge it. |
mihaibudiu
approved these changes
Nov 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix the SQL standard boundary calculation in
binary_substring3___for non-positive starting offsets, fix 1-based indexing inbinary_overlay4____, and add unit tests.Fixes #7830.
Root Cause & Problem
In
crates/sqllib/src/binary.rs:binary_substring3___(source, left, count)handlesleft < 1by clampingstart = 0but leavingcountunchanged. Under SQL 1-based substring semantics (such as instring.rs), the extraction window starts at indexleftwith lengthcount. Any part of the window preceding position 1 should be omitted, yielding an effective count of(count + left - 1).max(0). Becausecountwas unadjusted, non-positive start values extracted more bytes than expected (or extracted prefix bytes when the entire window was non-positive).binary_overlay4____invokedbinary_substring3___(source.clone(), 0, position - 1)with0instead of1, which previously succeeded only becauseleft = 0was clamped to 0 without adjusting count.binary_substring2__andbinary_substring3___were consumingsource.dataand allocating via.into_iter().skip(...).take(...).collect(), which is inefficient compared to direct slice construction viaByteArray::new(&slice[start..end]).Technical Solution
binary_substring3___, calculate the extracted count forleft < 1using(count.saturating_add(left).saturating_sub(1)).max(0)to prevent integer overflow while correctly clamping non-positive starting windows.source.as_slice().binary_overlay4____, pass1as the 1-based start position for the prefix substring.crates/sqllib/src/binary.rstesting standard 1-based offsets, non-positive starts (left = 0,left < 0), zero/negative count, edge-case integer extremes (i32::MIN,i32::MAX), andbinary_overlay.Test Plan
cargo test -p feldera-sqllib --lib binary::(passed, 34 tests ok)rustfmt --check crates/sqllib/src/binary.rs(passed with code 0)