Skip to content

fix(isthmus)!: translate array indexes correctly - #1261

Draft
bvolpato wants to merge 2 commits into
substrait-io:mainfrom
bvolpato:bvolpato/fix-array-item-index
Draft

fix(isthmus)!: translate array indexes correctly#1261
bvolpato wants to merge 2 commits into
substrait-io:mainfrom
bvolpato:bvolpato/fix-array-item-index

Conversation

@bvolpato

@bvolpato bvolpato commented Sep 3, 2026

Copy link
Copy Markdown
Member

Calcite's safe array operators have a zero- or one-based index, while Substrait list references use zero-based offsets and interpret negative offsets from the end. Copying the literal index changes the selected value: for a = [10, 20, 30], a[1] becomes the second element and a[-1] becomes the last instead of null.

SELECT a[1] FROM (VALUES (ARRAY[10, 20, 30])) AS t(a);

Normalize the index using the operator's declared base. Null and below-base indexes produce typed null when the array operand can safely be omitted, and wide integer indexes are checked before narrowing to the protobuf offset. The resulting offsets follow spec v0.102.0.

Downsides

Substrait list references cannot preserve the throwing behavior of unsafe OFFSET/ORDINAL operators, or represent offsets outside the signed 32-bit range. These conversions now fall through to unsupported-function handling. Computed array operands are also rejected for null or below-base indexes when folding them to null would discard evaluation, including errors from casts.

BREAKING CHANGE: Unsafe array access operators, unrepresentable indexes, and null or below-base accesses whose array operand cannot safely be omitted now fail conversion. Callers must handle the unsupported conversion when the operator or operand evaluation cannot be preserved.

Normalize safe array indexes by their operator base. Preserve null and
out-of-range low indexes without introducing negative list offsets, and
check wide indexes before narrowing.

BREAKING CHANGE: Unsafe array operators and unrepresentable offsets now
reach unsupported-function handling instead of changing semantics.
Folding an invalid or null index to NULL can remove a throwing cast or another computed array operand. Restrict this fold to literals and references into an existing record; reject other operands when their evaluation cannot be preserved.

Cover throwing casts, nested array selections, computed operands, and safe literal and column references for ITEM, SAFE_OFFSET, and SAFE_ORDINAL.
@bvolpato
bvolpato force-pushed the bvolpato/fix-array-item-index branch from 8659726 to 125c546 Compare September 4, 2026 16:21
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.

1 participant