Skip to content

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

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

fix: return NULL for negative array subscripts in PostgreSQL dialect#22294
leno23 wants to merge 1 commit into
apache:mainfrom
leno23:fix/postgres-negative-array-index-22254

Conversation

@leno23
Copy link
Copy Markdown

@leno23 leno23 commented May 17, 2026

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.

Made with Cursor

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 apache#22254.

Co-authored-by: Cursor <cursoragent@cursor.com>
@github-actions github-actions Bot added the functions Changes to functions implementation label May 17, 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant