forked from tomjaguarpaw/haskell-opaleye
-
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.
Rewrite only references in aggregation, not all values
To recap, PostgreSQL does not allow lateral references in aggregation functions, e.g., the following SQL is invalid: ```sql SELECT result FROM ( VALUES (1), (2), (3) ) _(x), LATERAL ( SELECT sum(x) AS result ) __ ``` It fails with the error message `ERROR: aggregate functions are not allowed in FROM clause of their own query level`. Opaleye works around this limitation by rewriting the above query as follows: ```sql SELECT result FROM ( VALUES (1), (2), (3) ) _(x), LATERAL ( SELECT sum(inner1) AS result FROM ( SELECT x AS inner1 ) _ ) __ ``` The current implementation of this rewriting rewrites all arguments of aggregation functions regardless of whether they contain (potentially lateral) references or not. However, the same effect could be achieved if we only rewrote references and not all expressions. That's what this PR does. This change is beneficial when using set aggregation functions such as `percentile_cont`. With the current rewriting scheme, Opaleye will generate: ```sql SELECT result FROM ( VALUES (1), (2), (3) ) _(x), LATERAL ( SELECT percentile_cont(inner1) WITHIN GROUP (ORDER BY inner2) AS result FROM ( SELECT 0.5 AS inner1, x AS inner2 ) _ ) __ ``` Which fails with the error message: ``` ERROR: column "_.inner1" must appear in the GROUP BY clause or be used in an aggregate function LINE 12: percentile_cont(inner1) WITHIN GROUP (ORDER BY inner2) ^ DETAIL: Direct arguments of an ordered-set aggregate must use only grouped columns. ``` After the change in this PR, this instead becomes: ```sql SELECT result FROM ( VALUES (1), (2), (3) ) _(x), LATERAL ( SELECT percentile_cont(0.5) WITHIN GROUP (ORDER BY inner1) AS result FROM ( SELECT x AS inner1 ) _ ) __ ``` Which works.
- Loading branch information
1 parent
e0274be
commit 9183036
Showing
6 changed files
with
107 additions
and
51 deletions.
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
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