Skip to content

fix: return NULL for negative array subscripts in PostgreSQL dialect - #22294

Closed
wyf027 wants to merge 1 commit into
apache:mainfrom
wyf027:fix/postgres-negative-array-index-22254
Closed

wyf027 wants to merge 1 commit into
apache:mainfrom
wyf027:fix/postgres-negative-array-index-22254

Conversation

@wyf027

@wyf027 wyf027 commented May 17, 2026 •

Copy link
Copy Markdown

Which issue does this PR close?

Closes #22254.

Rationale for this change

PostgreSQL returns NULL for expressions like (array[10,20,30])[-1], while DataFusion previously treated negative subscripts as end-relative indexing (returning 30).

What changes are included in this PR?

  • When datafusion.sql_parser.dialect is postgresql, array_element returns NULL for negative indexes instead of counting from the end
  • Default (non-PostgreSQL) behavior is unchanged
  • Unit test for PostgreSQL-style negative subscript handling

Are these changes tested?

  • cargo test -p datafusion-functions-nested test_array_element

Are there any user-facing changes?

Yes. With PostgreSQL dialect configured, negative array subscripts now match PostgreSQL semantics.

When `sql_parser.dialect` is PostgreSQL, `array_element` no longer treats
negative indexes as end-relative; out-of-range negative subscripts yield
NULL to match PostgreSQL. Fixes #22254.

Co-authored-by: Cursor <cursoragent@cursor.com>
@github-actions github-actions Bot added the functions Changes to functions implementation label May 17, 2026
@kosiew

kosiew commented May 23, 2026

Copy link
Copy Markdown
Contributor

@leno23
We normally let the assignee work on the issue.
In the future, if you could volunteer to take an issue to be assigned the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

functions Changes to functions implementation

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