taozhi8833998 / taozhi8833998/node-sql-parser

Snowflake: WITHIN GROUP (ORDER BY …) not parsed for LISTAGG / ARRAY_AGG (and similar ordered aggregates)

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

@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

  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.