Skip to content

fix negative array subscripts should not index from the end#22302

Closed
xiedeyantu wants to merge 1 commit into
apache:mainfrom
xiedeyantu:negative
Closed

fix negative array subscripts should not index from the end#22302
xiedeyantu wants to merge 1 commit into
apache:mainfrom
xiedeyantu:negative

Conversation

@xiedeyantu
Copy link
Copy Markdown
Member

Which issue does this PR close?

Rationale for this change

PostgreSQL treats negative array subscripts as out of bounds and returns NULL. DataFusion was incorrectly interpreting negative subscripts as indexing from the end of the array, which caused behavioral mismatch.

What changes are included in this PR?

  • Updated array_element / array subscript evaluation to treat non-positive indexes as out of bounds and return NULL.
  • Updated sqllogictest coverage for array subscript and array_element behavior, including the [-1] case.

Are these changes tested?

Yes.

Validated with:

  • cargo test -p datafusion-functions-nested test_array_element_non_positive_indexes_return_null
  • cargo test -p datafusion-sqllogictest --test sqllogictests array_index
  • cargo test -p datafusion-sqllogictest --test sqllogictests expr

Are there any user-facing changes?

Yes. Array subscripts with negative indexes now return NULL instead of indexing from the end, aligning DataFusion behavior with PostgreSQL.

@github-actions github-actions Bot added sqllogictest SQL Logic Tests (.slt) functions Changes to functions implementation labels May 17, 2026
@Jefffrey Jefffrey marked this pull request as draft May 26, 2026 07:49
@Jefffrey
Copy link
Copy Markdown
Contributor

Moving to draft as pending some discussion in the original issue

@xiedeyantu
Copy link
Copy Markdown
Member Author

Moving to draft as pending some discussion in the original issue

@Jefffrey Perhaps we can close this PR, most people love the original behavior.

@xiedeyantu xiedeyantu closed this May 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

functions Changes to functions implementation sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PostgreSQL compatibility: negative array subscripts should not index from the end

2 participants