Skip to content

[BUG] JSQLParser 5.1 : SQL-99 : unnest(...) with ordinality #2218

Closed

Description

@davyboyhayes

Failing SQL Feature:

  • The unnest function which can have an optional with ordinality qualifier, is an unsupported statement when the qualifier is supplied.

SQL Example:

select * from unnest(array[4,5,6]) with ordinality;

Software Information:

  • JSqlParser 5.1
  • Database (e. g. SQL-99 )

It is part of the SQL-99 spec, although this is parsed into an UnsupportedStatement, and once in an unsupported statement, the statement can't be parsed and tree walked using JSQLParser.

https://www.iso.org/obp/ui/en/#iso:std:iso-iec:19075:-8:ed-1:v1:en (5.6.2)

http://jsqlformatter.manticore-projects.com/jsqlformatter/demo.html?args=-c%20M4UwNiDGAuAEBUsBmAnA9gW1gVwHa5GGgAoBDFFUgTwG0AWAGgFYGA2AXQEpYB3AS2gALWGhQATPrlJgBVANxA$$

Subclause 7.6, "<table reference>":
  
 <collection derived table> ::=
 UNNEST <left paren> <collection value expression>
 [ { <comma> <collection value expression> }... ] <right paren>
 [ WITH ORDINALITY ]

This is present in a number of Databases:
H2 - https://www.h2database.com/html/functions.html#unnest
HSQLDB - https://hsqldb.org/doc/guide/sqlgeneral-chapt.html (grep for unnest)
Postgres - https://www.postgresql.org/docs/current/queries-table-expressions.html#QUERIES-TABLEFUNCTIONS although this declares the with ordinality to be a property of a Table Function, which probably makes more sense in a general sense
DB2 - https://www.ibm.com/docs/en/db2/11.1.0?topic=functions-unnest
GoogleSQL - https://cloud.google.com/bigquery/docs/arrays#flattening_arrays (although they have with offset)

SQL Server doesn't support this - https://learn.microsoft.com/en-us/openspecs/sql_standards/ms-tsqliso02/698d73c4-1410-4a75-affe-cef7323bb0d8

Activity

  1. davyboyhayes commented on Apr 7, 2025

    @davyboyhayes
    ContributorAuthor

    I would be happy to submit a PR, the changes to TableFunction.java would be simple enough, adding an optional String clause to it. Where I am less certain is how to change the JSqlParser.jjt file to modify the TableFunction part to take in potentially a multi token clause. Or should it be a case of declaring a withClause and only allowing a singular word to be present after WITH in a TableFunction (which again, I am less certain on how to achieve in the jjt file)

  2. added a commit that references this issue on Apr 7, 2025
    348e82f
  3. added a commit that references this issue on Apr 8, 2025
    e5fa004
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions