← 输入框|github.com|刷新
Skip to content

[sqllib] Fix binary substring boundary calculation for non-positive start index - #7831

Merged
mihaibudiu merged 1 commit into
feldera:mainfrom
Tyagiquamar:fix-binary-substring-bounds
Nov 6, 2026
Merged

mihaibudiu merged 1 commit into
feldera:mainfrom
Tyagiquamar:fix-binary-substring-bounds

Conversation

@Tyagiquamar

Copy link
Copy Markdown
Contributor

Summary

Fix the SQL standard boundary calculation in binary_substring3___ for non-positive starting offsets, fix 1-based indexing in binary_overlay4____, and add unit tests.

Fixes #7830.

Root Cause & Problem

In crates/sqllib/src/binary.rs:

  1. binary_substring3___(source, left, count) handles left < 1 by clamping start = 0 but leaving count unchanged. Under SQL 1-based substring semantics (such as in string.rs), the extraction window starts at index left with length count. Any part of the window preceding position 1 should be omitted, yielding an effective count of (count + left - 1).max(0). Because count was unadjusted, non-positive start values extracted more bytes than expected (or extracted prefix bytes when the entire window was non-positive).
  2. binary_overlay4____ invoked binary_substring3___(source.clone(), 0, position - 1) with 0 instead of 1, which previously succeeded only because left = 0 was clamped to 0 without adjusting count.
  3. Both binary_substring2__ and binary_substring3___ were consuming source.data and allocating via .into_iter().skip(...).take(...).collect(), which is inefficient compared to direct slice construction via ByteArray::new(&slice[start..end]).

Technical Solution

  1. In binary_substring3___, calculate the extracted count for left < 1 using (count.saturating_add(left).saturating_sub(1)).max(0) to prevent integer overflow while correctly clamping non-positive starting windows.
  2. Replace iterator-based collection with direct byte slicing on source.as_slice().
  3. In binary_overlay4____, pass 1 as the 1-based start position for the prefix substring.
  4. Add comprehensive unit test coverage in crates/sqllib/src/binary.rs testing standard 1-based offsets, non-positive starts (left = 0, left < 0), zero/negative count, edge-case integer extremes (i32::MIN, i32::MAX), and binary_overlay.

Test Plan

  • Run targeted unit tests in Rust 1.99 container: cargo test -p feldera-sqllib --lib binary:: (passed, 34 tests ok)
  • Run code formatting check: rustfmt --check crates/sqllib/src/binary.rs (passed with code 0)

@mihaibudiu

Copy link
Copy Markdown
Contributor

This looks fine, but can you also please update the documentation for substring in binary.md?

@mihaibudiu

Copy link
Copy Markdown
Contributor

Also check string.md if it needs updates.

…tart index

Fixes feldera#7830

Signed-off-by: Mihai Budiu <mbudiu@feldera.com>
@mihaibudiu
mihaibudiu force-pushed the fix-binary-substring-bounds branch from 39071ba to f5e27d7 Compare November 6, 2026 03:25
@mihaibudiu

Copy link
Copy Markdown
Contributor

I took the liberty to amend this commit adding documentation. Now I plan to merge it.

@mihaibudiu
mihaibudiu added this pull request to the merge queue Nov 6, 2026
Merged via the queue into feldera:main with commit 206cf9f Nov 6, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(sqllib): fix binary substring boundary calculation for non-positive start index

2 participants