-
Notifications
You must be signed in to change notification settings - Fork 25.6k
SQL: Add PIVOT support #46489
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
Merged
Merged
SQL: Add PIVOT support #46489
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
84cee40
SQL: Add PIVOT support
costin a0caa34
Merge branch 'master' into sql/piv
costin 48c6a83
Address feedback
costin 3f356ba
Address feedback
costin 7af8f37
Restrict usage of PIVOT
costin d28984a
Merge remote-tracking branch 'remotes/upstream/master' into sql/piv
costin d668751
Prevent usage of InnerAgg for now
costin 854d09c
Remove unneeded file
costin 2606f36
Fix typos
costin 7bbe026
Merge remote-tracking branch 'remotes/upstream/master' into sql/piv
costin 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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| averageWithOneValue | ||
| schema::languages:bt|'F':d | ||
| SELECT * FROM (SELECT languages, gender, salary FROM test_emp) PIVOT (AVG(salary) FOR gender IN ('F')); | ||
|
|
||
| languages | 'F' | ||
| ---------------+------------------ | ||
| null |62140.666666666664 | ||
| 1 |47073.25 | ||
| 2 |50684.4 | ||
| 3 |53660.0 | ||
| 4 |49291.5 | ||
| 5 |46705.555555555555 | ||
| ; | ||
|
|
||
| averageWithAliasAndOneValue | ||
| schema::languages:bt|'F':d | ||
| SELECT * FROM (SELECT languages, gender, salary FROM test_emp) PIVOT (AVG(salary) AS "AVG" FOR gender IN ('F')); | ||
|
|
||
| languages | 'F' | ||
| ---------------+------------------ | ||
| null |62140.666666666664 | ||
| 1 |47073.25 | ||
| 2 |50684.4 | ||
| 3 |53660.0 | ||
| 4 |49291.5 | ||
| 5 |46705.555555555555 | ||
| ; | ||
|
|
||
| averageWithAliasedValue | ||
| schema::languages:bt|XX:d | ||
| SELECT * FROM (SELECT languages, gender, salary FROM test_emp) PIVOT (AVG(salary) FOR gender IN ('F' AS "XX")); | ||
|
|
||
| languages | XX | ||
| ---------------+------------------ | ||
| null |62140.666666666664 | ||
| 1 |47073.25 | ||
| 2 |50684.4 | ||
| 3 |53660.0 | ||
| 4 |49291.5 | ||
| 5 |46705.555555555555 | ||
| ; | ||
|
|
||
| averageWithTwoValues | ||
| schema::languages:bt|'M':d|'F':d | ||
| SELECT * FROM (SELECT languages, gender, salary FROM test_emp) PIVOT (AVG(salary) FOR gender IN ('M', 'F')); | ||
|
|
||
| languages | 'M' | 'F' | ||
| ---------------+-----------------+------------------ | ||
| null |48396.28571428572|62140.666666666664 | ||
| 1 |49767.22222222222|47073.25 | ||
| 2 |44103.90909090909|50684.4 | ||
| 3 |51741.90909090909|53660.0 | ||
| 4 |47058.90909090909|49291.5 | ||
| 5 |39052.875 |46705.555555555555 | ||
| ; | ||
|
|
||
| averageWithTwoValuesAndAlias | ||
| schema::languages:bt|XY:d|XX:d | ||
| SELECT * FROM (SELECT languages, gender, salary FROM test_emp) PIVOT (AVG(salary) FOR gender IN ('M' AS "XY", 'F' "XX")); | ||
|
|
||
| languages | XY | XX | ||
| ---------------+-----------------+------------------ | ||
| null |48396.28571428572|62140.666666666664 | ||
| 1 |49767.22222222222|47073.25 | ||
| 2 |44103.90909090909|50684.4 | ||
| 3 |51741.90909090909|53660.0 | ||
| 4 |47058.90909090909|49291.5 | ||
| 5 |39052.875 |46705.555555555555 | ||
| ; | ||
|
|
||
| averageWithThreeValuesIncludingNull | ||
| schema::languages:bt|'M':d|'F':d | ||
| SELECT * FROM (SELECT languages, gender, salary FROM test_emp) PIVOT (AVG(salary) FOR gender IN ('M', 'F')); | ||
|
|
||
| languages | 'M' | 'F' | ||
| ---------------+-----------------+------------------ | ||
| null |48396.28571428572|62140.666666666664 | ||
| 1 |49767.22222222222|47073.25 | ||
| 2 |44103.90909090909|50684.4 | ||
| 3 |51741.90909090909|53660.0 | ||
| 4 |47058.90909090909|49291.5 | ||
| 5 |39052.875 |46705.555555555555 | ||
| ; | ||
|
|
||
|
|
||
| averageWithOneValueAndLimit | ||
| schema::languages:bt|'F':d | ||
| SELECT * FROM (SELECT languages, gender, salary FROM test_emp) PIVOT (AVG(salary) FOR gender IN ('F')) LIMIT 3; | ||
|
|
||
| languages | 'F' | ||
| ---------------+------------------ | ||
| null |62140.666666666664 | ||
| 1 |47073.25 | ||
| 2 |50684.4 | ||
| ; | ||
|
|
||
| averageWithTwoValuesAndLimit | ||
| schema::languages:bt|'M':d|'F':d | ||
| SELECT * FROM (SELECT languages, gender, salary FROM test_emp) PIVOT (AVG(salary) FOR gender IN ('M', 'F')) LIMIT 3; | ||
|
|
||
| languages | 'M' | 'F' | ||
| ---------------+-----------------+------------------ | ||
| null |48396.28571428572|62140.666666666664 | ||
| 1 |49767.22222222222|47073.25 | ||
| 2 |44103.90909090909|50684.4 | ||
| ; | ||
|
|
||
|
|
||
| averageWithTwoValuesAndTinyLimit | ||
| schema::languages:bt|'M':d|'F':d | ||
| SELECT * FROM (SELECT languages, gender, salary FROM test_emp) PIVOT (AVG(salary) FOR gender IN ('M', 'F')) LIMIT 1; | ||
|
|
||
| languages | 'M' | 'F' | ||
| ---------------+-----------------+------------------ | ||
| null |48396.28571428572|62140.666666666664 | ||
| ; | ||
|
|
||
|
|
||
| averageWithTwoValuesAndSmallLimit | ||
| schema::languages:bt|'M':d|'F':d | ||
| SELECT * FROM (SELECT languages, gender, salary FROM test_emp) PIVOT (AVG(salary) FOR gender IN ('M', 'F')) LIMIT 2; | ||
|
|
||
| languages | 'M' | 'F' | ||
| ---------------+-----------------+------------------ | ||
| null |48396.28571428572|62140.666666666664 | ||
| 1 |49767.22222222222|47073.25 | ||
| ; | ||
|
|
||
| averageWithOneValueAndOrder | ||
| schema::languages:bt|'F':d | ||
| SELECT * FROM (SELECT languages, gender, salary FROM test_emp) PIVOT (AVG(salary) FOR gender IN ('F')) ORDER BY languages DESC LIMIT 4; | ||
|
|
||
| languages | 'F' | ||
| ---------------+------------------ | ||
| 5 |46705.555555555555 | ||
| 4 |49291.5 | ||
| 3 |53660.0 | ||
| 2 |50684.4 | ||
| ; | ||
|
|
||
| averageWithTwoValuesAndOrderDesc | ||
| schema::languages:bt|'M':d|'F':d | ||
| SELECT * FROM (SELECT languages, gender, salary FROM test_emp) PIVOT (AVG(salary) FOR gender IN ('M', 'F')) ORDER BY languages DESC; | ||
|
|
||
| languages | 'M' | 'F' | ||
| ---------------+-----------------+------------------ | ||
| 5 |39052.875 |46705.555555555555 | ||
| 4 |47058.90909090909|49291.5 | ||
| 3 |51741.90909090909|53660.0 | ||
| 2 |44103.90909090909|50684.4 | ||
| 1 |49767.22222222222|47073.25 | ||
| null |48396.28571428572|62140.666666666664 | ||
| ; | ||
|
|
||
| averageWithTwoValuesAndOrderDesc | ||
|
||
| schema::languages:bt|'M':d|'F':d | ||
| SELECT * FROM (SELECT languages, gender, salary FROM test_emp) PIVOT (AVG(salary) FOR gender IN ('M', 'F')) ORDER BY languages DESC LIMIT 2; | ||
|
|
||
| languages | 'M' | 'F' | ||
| ---------------+-----------------+------------------ | ||
| 5 |39052.875 |46705.555555555555 | ||
| 4 |47058.90909090909|49291.5 | ||
| ; | ||
|
|
||
| averageWithTwoValuesAndOrderAsc | ||
| schema::languages:bt|'M':d|'F':d | ||
| SELECT * FROM (SELECT languages, gender, salary FROM test_emp) PIVOT (AVG(salary) FOR gender IN ('M', 'F')) ORDER BY languages ASC; | ||
|
|
||
| languages | 'M' | 'F' | ||
| ---------------+-----------------+------------------ | ||
| null |48396.28571428572|62140.666666666664 | ||
| 1 |49767.22222222222|47073.25 | ||
| 2 |44103.90909090909|50684.4 | ||
| 3 |51741.90909090909|53660.0 | ||
| 4 |47058.90909090909|49291.5 | ||
| 5 |39052.875 |46705.555555555555 | ||
| ; | ||
|
|
||
|
|
||
| sumWithoutSubquery | ||
| schema::birth_date:ts|emp_no:i|first_name:s|gender:s|hire_date:ts|last_name:s|1:i|2:i | ||
| SELECT * FROM test_emp PIVOT (SUM(salary) FOR languages IN (1, 2)) LIMIT 5; | ||
|
|
||
| birth_date | emp_no | first_name | gender | hire_date | last_name | 1 | 2 | ||
| ---------------------+---------------+---------------+---------------+---------------------+---------------+---------------+--------------- | ||
| null |10041 |Uri |F |1989-11-12 00:00:00.0|Lenart |56415 |null | ||
| null |10043 |Yishay |M |1990-10-20 00:00:00.0|Tzvieli |34341 |null | ||
| null |10044 |Mingsen |F |1994-05-21 00:00:00.0|Casley |39728 |null | ||
| 1952-04-19 00:00:00.0|10009 |Sumant |F |1985-02-18 00:00:00.0|Peac |66174 |null | ||
| 1953-01-07 00:00:00.0|10067 |Claudi |M |1987-03-04 00:00:00.0|Stavenow |null |52044 | ||
| 1953-01-23 00:00:00.0|10019 |Lillian |null |1999-04-30 00:00:00.0|Haddadi |73717 |null | ||
| ; | ||
|
|
||
| averageWithOneValueAndMath | ||
| schema::languages:bt|'F':d | ||
| SELECT * FROM (SELECT languages, gender, salary FROM test_emp) PIVOT (ROUND(AVG(salary) / 2) FOR gender IN ('F')); | ||
|
|
||
| languages | 'F' | ||
| ---------------+--------------- | ||
| null |31070.0 | ||
| 1 |23537.0 | ||
| 2 |25342.0 | ||
| 3 |26830.0 | ||
| 4 |24646.0 | ||
| 5 |23353.0 | ||
| ; | ||
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.
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.
This file shouldn't be here - I'll remove it on the next commit once the feedback lands to avoid an extra build.