Skip to content

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

Open

Description

@Dandandan

Split out from #22247.

Mismatch

Negative array subscripts should not index from the end.

SELECT (array[10,20,30])[-1];

PostgreSQL:

NULL

DataFusion:

30

Expected behavior

For PostgreSQL-compatible SQL semantics, DataFusion should either match PostgreSQL's result or raise the same class of domain/semantic error.

Activity

  1. xiedeyantu commented on May 16, 2026

    @xiedeyantu
    Member

    take

  2. kumarUjjawal commented on May 18, 2026

    @kumarUjjawal
    Contributor

    DuckDB:

    memory D SELECT (array[10,20,30])[-1];
    ┌─────────────────────────┐
    │ (ARRAY[10, 20, 30])[-1] │
    │          int32          │
    ├─────────────────────────┤
    │                      30 │
    └─────────────────────────┘
    
  3. xiedeyantu commented on May 18, 2026

    @xiedeyantu
    Member

    DuckDB does indeed work that way, so should we keep it as is? Returning 30 might be a better approach.

  4. kumarUjjawal commented on May 18, 2026

    @kumarUjjawal
    Contributor

    DuckDB intentionally implements Python-style negative indexing: arr[-1] = last element, arr[-k] = k-th from end, out-of-bounds → NULL. PG returns NULL for negative subscripts (no from-end semantics). DuckDB documents this through examples (['a','b','c'][-1] → 'c').

    We can get consensus about which engine to follow for this.

  5. xiedeyantu commented on May 19, 2026

    @xiedeyantu
    Member

    I'm leaning towards the DuckDB approach and would like to get your thoughts.

  6. kumarUjjawal commented on May 19, 2026

    @kumarUjjawal
    Contributor

    I'm leaning towards the DuckDB approach and would like to get your thoughts.

    I agree we should stick to the current approach unless we have some special use case for the change.

  7. xiedeyantu commented on May 19, 2026

    @xiedeyantu
    Member

    We can wait for Dan's suggestion, and if he agrees, I can close the PR at any time.

  8. xiedeyantu commented on May 22, 2026

    @xiedeyantu
    Member

    HI @Dandandan , WDYT?

  9. Jefffrey commented on May 26, 2026

    @Jefffrey
    Contributor

    Spark also doesn't tolerate negative indices it seems (though it errors outright). Clickhouse seems to align with DuckDB.

    I agree that negative indexing makes sense to me, since I'm also used to how Python treats it.

  10. Jefffrey commented on May 26, 2026

    @Jefffrey
    Contributor

    Another option is this PR:

    Which ties it to the dialect setting

  11. ngli-me commented on Jun 8, 2026

    @ngli-me
    Contributor

    Another option is this PR:

    * [fix: return NULL for negative array subscripts in PostgreSQL dialect #22294](https://github.com/apache/datafusion/pull/22294)
    

    Which ties it to the dialect setting

    Based on #22252, it seems like it should be tied to the dialect in cases where the behavior is conflicting, and where the postgres behavior subjective (unlike in cases like #22270).

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

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinggood first issueGood for newcomers

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions