apache / datafusion-sqlparser-rs

Extensible SQL Lexer and Parser for Rust
Apache License 2.0
2.8k stars 543 forks source link

support column definition list in table alias for postgres #1524

Closed lovasoa closed 4 days ago

lovasoa commented 1 week ago

Postgres supports specifying column names AND types in a table alias (after AS) for functions that return values of type "record".

The following syntax is valid in postgres

SELECT * FROM jsonb_to_record('{"a": "x", "b": 2}'::jsonb) AS x(a TEXT, b INT);

But in sqlparser, it returns

Expected: ), found: TEXT

This was initially reported in SQLPage, as: https://github.com/sqlpage/SQLPage/discussions/690

lovasoa commented 6 days ago

From https://www.postgresql.org/docs/17/queries-table-expressions.html#QUERIES-TABLEFUNCTIONS :

In some cases it is useful to define table functions that can return different column sets depending on how they are invoked. To support this, the table function can be declared as returning the pseudo-type record with no OUT parameters. When such a function is used in a query, the expected row structure must be specified in the query itself, so that the system can know how to parse and plan the query. This syntax looks like:

function_call [AS] alias (column_definition [, ... ])
function_call AS [alias] (column_definition [, ... ])
ROWS FROM( ... function_call AS (column_definition [, ... ]) [, ... ] )