-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat: Optimize CASE expression for usage where then and else values are literals #11553
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,7 +17,7 @@ | |
|
|
||
| # create test data | ||
| statement ok | ||
| create table foo (a int, b int) as values (1, 2), (3, 4), (5, 6); | ||
| create table foo (a int, b int) as values (1, 2), (3, 4), (5, 6), (null, null), (6, null), (null, 7); | ||
|
|
||
| # CASE WHEN with condition | ||
| query T | ||
|
|
@@ -26,6 +26,9 @@ SELECT CASE a WHEN 1 THEN 'one' WHEN 3 THEN 'three' ELSE '?' END FROM foo | |
| one | ||
| three | ||
| ? | ||
| ? | ||
| ? | ||
| ? | ||
|
|
||
| # CASE WHEN with no condition | ||
| query I | ||
|
|
@@ -34,6 +37,9 @@ SELECT CASE WHEN a > 2 THEN a ELSE b END FROM foo | |
| 2 | ||
| 3 | ||
| 5 | ||
| NULL | ||
| 6 | ||
| 7 | ||
|
|
||
| # column or explicit null | ||
| query I | ||
|
|
@@ -42,6 +48,9 @@ SELECT CASE WHEN a > 2 THEN b ELSE null END FROM foo | |
| NULL | ||
| 4 | ||
| 6 | ||
| NULL | ||
| NULL | ||
| 7 | ||
|
|
||
| # column or implicit null | ||
| query I | ||
|
|
@@ -50,3 +59,52 @@ SELECT CASE WHEN a > 2 THEN b END FROM foo | |
| NULL | ||
| 4 | ||
| 6 | ||
| NULL | ||
| NULL | ||
| 7 | ||
|
|
||
| # scalar or scalar (string) | ||
|
Contributor
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. Could you also add a test where both arguments are scalars (like
Member
Author
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. Added |
||
| query T | ||
| SELECT CASE WHEN a > 2 THEN 'even' ELSE 'odd' END FROM foo | ||
|
Contributor
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. Looks like
Member
Author
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. Good point. I have added this. |
||
| ---- | ||
| odd | ||
| even | ||
| even | ||
| odd | ||
| even | ||
| odd | ||
|
|
||
| # scalar or scalar (int) | ||
| query I | ||
| SELECT CASE WHEN a > 2 THEN 1 ELSE 0 END FROM foo | ||
| ---- | ||
| 0 | ||
| 1 | ||
| 1 | ||
| 0 | ||
| 1 | ||
| 0 | ||
|
|
||
| # predicate binary expression with scalars (does not make much sense because the expression in | ||
| # this case is always false, so this expression could be rewritten as a literal 0 during planning | ||
| query I | ||
| SELECT CASE WHEN 1 > 2 THEN 1 ELSE 0 END FROM foo | ||
| ---- | ||
| 0 | ||
| 0 | ||
| 0 | ||
| 0 | ||
| 0 | ||
| 0 | ||
|
|
||
| # predicate using boolean literal (does not make much sense because the expression in | ||
| # this case is always false, so this expression could be rewritten as a literal 0 during planning | ||
| query I | ||
| SELECT CASE WHEN false THEN 1 ELSE 0 END FROM foo | ||
| ---- | ||
| 0 | ||
| 0 | ||
| 0 | ||
| 0 | ||
| 0 | ||
| 0 | ||
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 the input is
ColumnarValue::Scalarshouldn't the output also be aColumnarValue::Scalar(rather than aColumnarValue::Array?)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.
No, the output will be an array containing values based on two scalar arguments.
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 wonder if it would make sense (in a separate PR) to produce a dictionary array in this case since it will only even contain two distinct values? 🤔