-
-
Notifications
You must be signed in to change notification settings - Fork 263
New function LISTAGG #8689
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ChudaykinAlex
wants to merge
7
commits into
FirebirdSQL:master
Choose a base branch
from
ChudaykinAlex:work/listagg
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
New function LISTAGG #8689
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
640b352
Adding an implementation of the new LISTAGG function
ChudaykinAlex 9f34905
Merge branch 'master' into work/listagg
ChudaykinAlex 72a977f
Add README
ChudaykinAlex dbf81d6
Merge branch 'master' into work/listagg
ChudaykinAlex dfcbe9c
Code formatting errors have been fixed.
ChudaykinAlex 0e3ec8f
dyemanov omments have been corrected.
ChudaykinAlex 26e9d42
Merge branch 'master' into work/listagg
ChudaykinAlex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| SQL Language Extension: LISTAGG | ||
|
|
||
| Function: | ||
| The current implementation has an aggregate function LIST which concatenates multiple row | ||
| fields into a blob. The SQL standard has a similar function called LISTAGG. The major | ||
| difference is that it also supports the ordered concatenation. | ||
|
|
||
| Authors: | ||
| Chudaykin Alex <chudaykinalex@gmail.com> | ||
|
|
||
| Format: | ||
| <listagg set function> ::= | ||
| LISTAGG <left paren> [ <set quantifier> ] <character value expression> <comma> <listagg separator> [ <listagg overflow clause> ] <right paren> [ <within group specification> ] | ||
|
|
||
| <listagg separator> ::= | ||
| <character string literal> | ||
|
|
||
| <listagg overflow clause> ::= | ||
| ON OVERFLOW <overflow behavior> | ||
|
|
||
| <overflow behavior> ::= | ||
| ERROR | TRUNCATE [ <listagg truncation filler> ] <listagg count indication> | ||
|
|
||
| <listagg truncation filler> ::= | ||
| <character string literal> | ||
|
|
||
| <listagg count indication> ::= | ||
| WITH COUNT | WITHOUT COUNT | ||
|
|
||
| <within group specification> ::= | ||
| WITHIN GROUP <left paren> ORDER BY <sort specification list> <right paren> | ||
|
|
||
| Syntax Rules: | ||
| The legacy LIST syntax is preserved for backward compatibility, LISTAGG is added to cover the | ||
| standard features. | ||
|
|
||
| There is a <listagg overflow clause> rule in the standard, which is intended to output an error | ||
| when the output value overflows. Since the LIST function always returns a BLOB, it was decided | ||
| that this rule would be meaningless. So the OVERFLOW clause is syntactically supported but | ||
| silently ignored if specified. | ||
|
|
||
| Examples: | ||
| CREATE TABLE TEST_T | ||
| (COL1 INT, COL2 VARCHAR(2), COL3 VARCHAR(2), COL4 VARCHAR(2), COL5 BOOLEAN, COL6 VARCHAR(2) | ||
| CHARACTER SET WIN1251); | ||
| COMMIT; | ||
| INSERT INTO TEST_T values(1, 'A', 'A', 'J', false, 'П'); | ||
| INSERT INTO TEST_T values(2, 'B', 'B', 'I', false, 'Д'); | ||
| INSERT INTO TEST_T values(3, 'C', 'A', 'L', true, 'Ж'); | ||
| INSERT INTO TEST_T values(4, 'D', 'B', 'K', true, 'Й'); | ||
| COMMIT; | ||
|
|
||
| SELECT LISTAGG (ALL COL4, ':') AS FROM TEST_T; | ||
| ======= | ||
| J:I:L:K | ||
|
|
||
| SELECT LISTAGG (DISTINCT COL4, ':') FROM TEST_T; | ||
| ======== | ||
| I:J:K:L | ||
|
|
||
| SELECT LISTAGG (DISTINCT COL3, ':') FROM TEST_T; | ||
| ==== | ||
| A:B | ||
|
|
||
| SELECT LISTAGG (DISTINCT COL3, ':') WITHIN GROUP (ORDER BY COL3 ASCENDING) FROM TEST_T; | ||
| ==== | ||
| A:B | ||
|
|
||
| SELECT LISTAGG (DISTINCT COL3, ':') WITHIN GROUP (ORDER BY COL3 DESCENDING) FROM TEST_T; | ||
| ==== | ||
| B:A | ||
|
|
||
| SELECT LISTAGG (DISTINCT COL3, ':') WITHIN GROUP (ORDER BY COL3 DESCENDING, COL4, COL5) FROM TEST_T; | ||
| ==== | ||
| B:A | ||
|
|
||
| SELECT LISTAGG (DISTINCT COL3, ':') WITHIN GROUP (ORDER BY COL4, COL3 DESCENDING, COL5) FROM TEST_T; | ||
| ==== | ||
| A:B | ||
|
|
||
| SELECT LISTAGG (DISTINCT COL3, ':') WITHIN GROUP (ORDER BY COL2) FROM TEST_T; | ||
| ==== | ||
| A:B | ||
|
|
||
| SELECT LISTAGG (DISTINCT COL3, ':') WITHIN GROUP (ORDER BY COL2 DESCENDING) FROM TEST_T; | ||
| ==== | ||
| A:B | ||
|
|
||
| SELECT LISTAGG (COL2, ':') WITHIN GROUP (ORDER BY COL2 DESCENDING) FROM TEST_T; | ||
| ======= | ||
| D:C:B:A | ||
|
|
||
| SELECT LISTAGG (COL4, ':') WITHIN GROUP (ORDER BY COL3 DESC) FROM TEST_T; | ||
| ======= | ||
| I:K:J:L | ||
|
|
||
| SELECT LISTAGG (COL3, ':') WITHIN GROUP (ORDER BY COL5 ASCENDING) FROM TEST_T; | ||
| ======= | ||
| A:B:A:B | ||
|
|
||
| SELECT LISTAGG (COL4, ':') WITHIN GROUP (ORDER BY COL3 ASC) FROM TEST_T; | ||
| ======= | ||
| J:L:I:K | ||
|
|
||
| SELECT LISTAGG (ALL COL2) WITHIN GROUP (ORDER BY COL4) FROM TEST_T; | ||
| ======= | ||
| B,A,D,C | ||
|
|
||
| SELECT LISTAGG (COL2, ':') WITHIN GROUP (ORDER BY COL3 DESC, COL4 ASC) FROM TEST_T; | ||
| ======= | ||
| B:D:A:C | ||
|
|
||
| SELECT LISTAGG (COL2, ':') WITHIN GROUP (ORDER BY COL3 DESC, COL4 DESC) FROM TEST_T; | ||
| ======= | ||
| D:B:C:A | ||
|
|
||
| SELECT LISTAGG (COL2, ':') WITHIN GROUP (ORDER BY COL3 ASC, COL4 DESC) FROM TEST_T; | ||
| ======= | ||
| C:A:D:B | ||
|
|
||
| SELECT LISTAGG (ALL COL6, ':') FROM TEST_T; | ||
| ======= | ||
| П:Д:Ж:Й | ||
|
|
||
| SELECT LISTAGG (ALL COL6, ':') WITHIN GROUP (ORDER BY COL2 DESC) FROM TEST_T; | ||
| ======= | ||
| Й:Ж:Д:П | ||
|
|
||
| SELECT LISTAGG (ALL COL2, ':') WITHIN GROUP (ORDER BY COL6) FROM TEST_T; | ||
| ======= | ||
| B:C:D:A | ||
|
|
||
| SELECT LISTAGG (COL4, ':' ON OVERFLOW TRUNCATE '...' WITHOUT COUNT) WITHIN GROUP (ORDER BY COL3 ASC) FROM TEST_T; | ||
| ======= | ||
| J:L:I:K | ||
|
|
||
| SELECT LISTAGG (COL4, ':' ON OVERFLOW TRUNCATE '...' WITH COUNT) WITHIN GROUP (ORDER BY COL3 DESC) FROM TEST_T; | ||
| ====== | ||
| I:K:J:L | ||
|
|
||
| SELECT LISTAGG (DISTINCT COL3, ':' ON OVERFLOW ERROR) WITHIN GROUP (ORDER BY COL3) FROM TEST_T; | ||
| === | ||
| A:B | ||
|
|
||
| INSERT INTO TEST_T values(5, 'E', NULL, NULL, NULL, NULL); | ||
| INSERT INTO TEST_T values(6, 'F', 'C', 'N', true, 'К'); | ||
|
|
||
| SELECT LISTAGG (ALL COL2, ':') WITHIN GROUP (ORDER BY COL3) FROM TEST_T; | ||
| =========== | ||
| E:A:C:B:D:F | ||
|
|
||
| SELECT LISTAGG (ALL COL2, ':') WITHIN GROUP (ORDER BY COL3 NULLS LAST) FROM TEST_T; | ||
| =========== | ||
| A:C:B:D:F:E | ||
|
|
||
| SELECT LISTAGG (ALL COL2, ':') WITHIN GROUP (ORDER BY COL6 NULLS FIRST) FROM TEST_T; | ||
| =========== | ||
| E:B:C:D:F:A | ||
|
|
||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the syntax format section,
<within group specification>is mandatory but does not exist in some examples.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The SQL specification declares
<within group specification>as mandatory. However, IMHO it's quite restrictive and neither Oracle nor DB2 follows that rule, they have it optional. Given thatLISTandLISTAGGshare the same syntax in this PR, we've also made<within group specification>optional. So the easiest solution is to fix the README ;-)Or we may go the standard way and separate the legacy
LIST(leave it with the current grammar, without ordering) fromLISTAGG(which is strictly standard-compliant) at the parser level. But IMHO it would be annoying for users to select either of them depending on whether you need ordering or not. So personally I'd keep everything "as is" and just fix the docs.Other opinions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree,
LISTandLISTAGGshould be complete synonyms.I would also remove the mention of
ON OVERFLOWfrom the documentation. It's standard, but we don't support it. It could be mentioned if it were simply ignored, but mentioning it leads to errors.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strange. I need to check it out
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we remove
ON OVERFLOWfrom the docs, then I believe we should remove it from the parser too.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ON OVERFLOWcan be kept if it will not cause errors.