taozhi8833998 / taozhi8833998/node-sql-parser
Snowflake: WITHIN GROUP (ORDER BY …) not parsed for LISTAGG / ARRAY_AGG (and similar ordered aggregates)
@taozhi8833998 is already working on this.
Since May 8, 2026.
- Dominant language
- PEG.js
- Stars
- 1k
- Forks
- 244
- PR merge metrics
- No merged PRs in 30d
Description
Describe the bug
With database: 'Snowflake', parse / astify on expressions that use WITHIN GROUP (ORDER BY …) (e.g. LISTAGG, ARRAY_AGG) throws a parse error at the ( after GROUP, so valid Snowflake SQL is rejected.
Database Engine
Snowflake
To Reproduce
SQL that should parse:
SELECT LISTAGG(x, ', ') WITHIN GROUP (ORDER BY x) FROM t GROUP BY g
SELECT ARRAY_AGG(x) WITHIN GROUP (ORDER BY x) FROM t GROUP BY g
const { Parser } = require('node-sql-parser');
const parser = new Parser();
parser.parse(
"SELECT LISTAGG(x, ', ') WITHIN GROUP (ORDER BY x) FROM t GROUP BY g",
{ database: 'Snowflake' }
);
node-sql-parser: 5.4.x (e.g. 5.4.0)
Node.js: 20.x (any recent LTS should show the same)
Expected behavior
Parsing succeeds and the AST represents the aggregate with its WITHIN GROUP (ORDER BY …) clause, consistent with Snowflake’s grammar.
Additional context
The same pattern works in Snowflake itself.
LISTAGG(x, ', ') FROM t GROUP BY g without WITHIN GROUP parses, so the failure is specific to the ordered-aggregate form.
PERCENTILE_CONT(…) WITHIN GROUP (ORDER BY …) is in the same family and may need the same grammar fix.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.