taozhi8833998 / taozhi8833998/node-sql-parser

PostgreSQL parser fails on column alias list after LATERAL unnest

Open
#2,672 0 comments 0 reactions 1 assignee View on GitHub

@taozhi8833998 is already working on this.

Since Jul 28, 2026.

Dominant language
PEG.js
Stars
1k
Forks
244
PR merge metrics
No merged PRs in 30d

Description

Describe the bug

The PostgreSQL parser fails to parse a LATERAL unnest(...) table function when its alias includes a column alias list, such as:

AS "pid"("id")

This is valid PostgreSQL syntax. The parser accepts AS "pid" but reports a syntax error on the ( in AS "pid"("id").

Database Engine

PostgreSQL

To Reproduce

SQL being parsed:

SELECT "pid"."id"
FROM (
  SELECT ARRAY[1, 2]::integer[] AS "Parent ids"
) AS "e"
LEFT JOIN LATERAL unnest(
  COALESCE("e"."Parent ids", ARRAY[]::integer[])
) AS "pid"("id") ON TRUE;

JavaScript:

const { Parser } = require('node-sql-parser');

const parser = new Parser();

const sql = `
SELECT "pid"."id"
FROM (
  SELECT ARRAY[1, 2]::integer[] AS "Parent ids"
) AS "e"
LEFT JOIN LATERAL unnest(
  COALESCE("e"."Parent ids", ARRAY[]::integer[])
) AS "pid"("id") ON TRUE;
`;

const ast = parser.astify(sql, {
  database: 'postgresql',
});

Error:

SyntaxError: Expected ")", ",", "ON", "JOIN", "WHERE", ...
but "(" found.

The unexpected ( is the opening parenthesis in:

AS "pid"("id")
        ^

Versions:

node-sql-parser: 5.4.0
Node.js: v22.21.1

Expected behavior

The query should parse successfully.

PostgreSQL supports assigning a table alias and an output-column alias to a table function:

unnest(...) AS "pid"("id")

The unnested value can then be referenced as:

"pid"."id"

Screenshots

Not applicable. The parser error and reproduction are included above.

Additional context

Removing the column alias list allows the query to parse:

LEFT JOIN LATERAL unnest(
  COALESCE("e"."Parent ids", ARRAY[]::integer[])
) AS "pid" ON TRUE

However, the output column must then be referenced using its default name:

"pid"."unnest"

Therefore, this appears to be a limitation in parsing PostgreSQL table-function column aliases rather than invalid PostgreSQL syntax.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.