-
Notifications
You must be signed in to change notification settings - Fork 24.8k
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
SQL: Implement CASE... WHEN... THEN... ELSE... END #41349
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8fd1da2
SQL: Implement CASE... WHEN... THEN... ELSE... END
matriv 1f17aca
remove empty lines
matriv 0a53fb6
Added integration tests
matriv 1bd870f
fix test
matriv 83dc691
Fixed an issue with types verification and enhanced integ tests
matriv 365f135
Merge remote-tracking branch 'upstream/master' into impl-36200
matriv 1853714
address comments pt1
matriv a791a84
address comments pt2
matriv 5793884
fixed docs
matriv fda4180
fix docs
matriv 94d5a60
Merge remote-tracking branch 'upstream/master' into impl-36200
matriv 7527f49
add missing ';'
matriv 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 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
181 changes: 181 additions & 0 deletions
181
x-pack/plugin/sql/qa/src/main/resources/conditionals.csv-spec
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
caseField | ||
SELECT emp_no, CASE WHEN emp_no - 10000 < 10 THEN 'First 10' ELSE 'Second 10' END as "case" FROM test_emp WHERE emp_no >= 10005 | ||
ORDER BY emp_no LIMIT 10; | ||
|
||
emp_no | case | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Following this PR - #41355 - can you change one of these tests to not have an alias? |
||
--------+----------- | ||
10005 | First 10 | ||
10006 | First 10 | ||
10007 | First 10 | ||
10008 | First 10 | ||
10009 | First 10 | ||
10010 | Second 10 | ||
10011 | Second 10 | ||
10012 | Second 10 | ||
10013 | Second 10 | ||
10014 | Second 10 | ||
; | ||
|
||
caseFieldWithoutAlias | ||
SELECT emp_no, CASE WHEN emp_no - 10000 < 10 THEN emp_no ELSE emp_no % 10 END FROM test_emp WHERE emp_no >= 10005 | ||
ORDER BY emp_no LIMIT 10; | ||
|
||
emp_no | CASE WHEN emp_no - 10000 < 10 THEN emp_no ELSE emp_no % 10 END | ||
--------+---------------------------------------------------------------- | ||
10005 | 10005 | ||
10006 | 10006 | ||
10007 | 10007 | ||
10008 | 10008 | ||
10009 | 10009 | ||
10010 | 0 | ||
10011 | 1 | ||
10012 | 2 | ||
10013 | 3 | ||
10014 | 4 | ||
; | ||
|
||
caseFieldNoElse | ||
SELECT emp_no, CASE WHEN emp_no - 10000 < 10 THEN 'First 10' END as "case" FROM test_emp WHERE emp_no >= 10005 | ||
ORDER BY emp_no LIMIT 10; | ||
|
||
emp_no | case | ||
--------+---------- | ||
10005 | First 10 | ||
10006 | First 10 | ||
10007 | First 10 | ||
10008 | First 10 | ||
10009 | First 10 | ||
10010 | null | ||
10011 | null | ||
10012 | null | ||
10013 | null | ||
10014 | null | ||
; | ||
|
||
caseWhere | ||
SELECT last_name FROM test_emp WHERE CASE WHEN LENGTH(last_name) < 7 THEN 'ShortName' ELSE 'LongName' END = 'LongName' | ||
ORDER BY emp_no LIMIT 10; | ||
|
||
last_name | ||
----------- | ||
Facello | ||
Bamford | ||
Koblick | ||
Maliniak | ||
Preusig | ||
Zielinski | ||
Kalloufi | ||
Piveteau | ||
Bridgland | ||
Nooteboom | ||
; | ||
|
||
caseWhereNoElse | ||
SELECT last_name FROM test_emp WHERE CASE WHEN LENGTH(last_name) < 7 THEN 'ShortName' END IS NOT NULL | ||
ORDER BY emp_no LIMIT 10; | ||
|
||
last_name | ||
----------- | ||
Simmel | ||
Peac | ||
Sluis | ||
Terkki | ||
Genin | ||
Peha | ||
Erde | ||
Famili | ||
Pettey | ||
Heyers | ||
; | ||
|
||
caseOrderBy | ||
schema::last_name:s|languages:byte|emp_no:i | ||
SELECT last_name, languages, emp_no FROM test_emp WHERE emp_no BETWEEN 10005 AND 10015 | ||
ORDER BY CASE WHEN languages >= 3 THEN 'first' ELSE 'second' END, emp_no LIMIT 10; | ||
|
||
last_name | languages | emp_no | ||
-----------+-----------+-------- | ||
Preusig | 3 | 10006 | ||
Zielinski | 4 | 10007 | ||
Piveteau | 4 | 10010 | ||
Sluis | 5 | 10011 | ||
Bridgland | 5 | 10012 | ||
Genin | 5 | 10014 | ||
Nooteboom | 5 | 10015 | ||
Maliniak | 1 | 10005 | ||
Kalloufi | 2 | 10008 | ||
Peac | 1 | 10009 | ||
; | ||
|
||
caseOrderByNoElse | ||
schema::last_name:s|languages:byte|emp_no:i | ||
SELECT last_name, languages, emp_no FROM test_emp WHERE emp_no BETWEEN 10005 AND 10015 | ||
ORDER BY CASE WHEN languages >= 3 THEN 'first' END NULLS FIRST, emp_no LIMIT 10; | ||
|
||
last_name | languages | emp_no | ||
-----------+-----------+-------- | ||
Maliniak | 1 | 10005 | ||
Kalloufi | 2 | 10008 | ||
Peac | 1 | 10009 | ||
Terkki | 1 | 10013 | ||
Preusig | 3 | 10006 | ||
Zielinski | 4 | 10007 | ||
Piveteau | 4 | 10010 | ||
Sluis | 5 | 10011 | ||
Bridgland | 5 | 10012 | ||
Genin | 5 | 10014 | ||
; | ||
|
||
caseGroupBy | ||
schema::count:l|lang_skills:s | ||
SELECT count(*) AS count, CASE WHEN NVL(languages, 0) <= 1 THEN 'zero-to-one' ELSE 'multilingual' END as lang_skills | ||
FROM test_emp GROUP BY lang_skills ORDER BY lang_skills; | ||
|
||
count | lang_skills | ||
-------+-------------- | ||
75 | multilingual | ||
25 | zero-to-one | ||
; | ||
|
||
caseGroupByNoElse | ||
schema::count:l|lang_skills:s | ||
SELECT count(*) AS count, CASE WHEN NVL(languages, 0) <= 1 THEN 'zero-to-one' END as lang_skills | ||
FROM test_emp GROUP BY lang_skills ORDER BY lang_skills; | ||
|
||
count | lang_skills | ||
-------+------------- | ||
75 | null | ||
25 | zero-to-one | ||
; | ||
|
||
caseGroupByComplexNested | ||
schema::count:l|lang_skills:s | ||
SELECT count(*) AS count, | ||
CASE WHEN NVL(languages, 0) = 0 THEN 'zero' | ||
WHEN languages = 1 THEN 'one' | ||
WHEN languages = 2 THEN 'bilingual' | ||
WHEN languages = 3 THEN 'trilingual' | ||
ELSE 'multilingual' | ||
END as lang_skills FROM test_emp GROUP BY lang_skills ORDER BY 2; | ||
|
||
count | lang_skills | ||
-------+-------------- | ||
19 | bilingual | ||
39 | multilingual | ||
15 | one | ||
17 | trilingual | ||
10 | zero | ||
; | ||
|
||
caseGroupByAndHaving | ||
schema::count:l|gender:s|languages:byte | ||
SELECT count(*) AS count, gender, languages FROM test_emp | ||
GROUP BY 2, 3 HAVING CASE WHEN count(*) > 10 THEN 'many' ELSE 'a few' END = 'many' | ||
ORDER BY 2, 3; | ||
|
||
count | gender | languages | ||
----------+-------------+--------------- | ||
11 | M | 2 | ||
11 | M | 3 | ||
11 | M | 4 | ||
; |
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
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.
Do you agree with listing CASE as a conditional function?
Case is quite special and doesn't follow at all the traditional functional format.