forked from tekumara/fakesnow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ARRAY_AGG() WITHIN GROUP() support (tekumara#66)
In Snowflake, to order elements in `ARRAY_AGG` aggregation one needs to provide `WITHIN GROUP (..)`; * https://docs.snowflake.com/en/sql-reference/functions/array_agg#arguments ``` ARRAY_AGG(<expr>) WITHIN GROUP (<order-by-clause>) ``` In DuckDB, `LIST()/ARRAY_AGG()` requires expression to be passed ordered and does not support `WITHIN GROUP (...)`; * https://duckdb.org/docs/sql/aggregates.html#order-by-clause-in-aggregate-functions ``` ARRAY_AGG( <expr> <order-by-clause> ) ``` This transformer simply combines *aggreage* expression in `ARRAY_AGG` and *ordering* one in `WITHIN GROUP` in an *order* expression. ```sql ARRAY_AGG(DISTINCT id) WITHIN GROUP (ORDER BY id) ``` ```python WithinGroup( this=ArrayAgg( this=Distinct( expressions=[ Column( this=Identifier(this=id, quoted=False))])), expression=Order( expressions=[ Ordered( this=Column( this=Identifier(this=id, quoted=False)), nulls_first=False)])) ``` ```python ArrayAgg( this=Order( this=Distinct( expressions=[ Column( this=Identifier(this=ID, quoted=False))]), expressions=[ Ordered( this=Column( this=Identifier(this=ID, quoted=False)), nulls_first=False)])) ``` ```diff -WithinGroup( - this=ArrayAgg( +ArrayAgg( + this=Order( this=Distinct( expressions=[ Column( - this=Identifier(this=id, quoted=False))])), - expression=Order( + this=Identifier(this=ID, quoted=False))]), expressions=[ Ordered( this=Column( - this=Identifier(this=id, quoted=False)), + this=Identifier(this=ID, quoted=False)), nulls_first=False)])) ``` ----- Snowflake has following limitation though; > https://docs.snowflake.com/en/sql-reference/functions/array_agg#usage-notes > If you specify DISTINCT and WITHIN GROUP, both must refer to the same column. For example: I've left it out of the scope of this PR to keep it simple. --------- Co-authored-by: Oliver Mannion <[email protected]>
- Loading branch information
Showing
4 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters