Skip to content

Comments

case when supports NULL constant#2197

Merged
alamb merged 3 commits intoapache:masterfrom
WinkerDu:master-case-when-null
Apr 13, 2022
Merged

case when supports NULL constant#2197
alamb merged 3 commits intoapache:masterfrom
WinkerDu:master-case-when-null

Conversation

@WinkerDu
Copy link
Contributor

@WinkerDu WinkerDu commented Apr 10, 2022

Which issue does this PR close?

Closes #1189.

Rationale for this change

df case when expression cannot take NULL constant, like

> select CASE WHEN NULL THEN 'foo' ELSE 'bar' END;
thread 'main' panicked at 'WHEN expression did not return a BooleanArray', datafusion/src/physical_plan/expressions/case.rs:381:18
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Postgres:

# select case null when null then 'foo' else 'bar' end;
 case 
------
 bar
(1 row)

What changes are included in this PR?

For when expression without case expression, treat NULL as false before down cast to BooleanArray

Are there any user-facing changes?

No.

@WinkerDu WinkerDu changed the title case .. when .. supports NULL constants case when supports NULL constant Apr 10, 2022
@WinkerDu
Copy link
Contributor Author

WinkerDu commented Apr 11, 2022

cc @yjshen @Dandandan @alamb, please have a review, thank you.

Copy link
Member

@yjshen yjshen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems the type-coercion suggested in the issue is not used. Cc @alamb.

@WinkerDu
Copy link
Contributor Author

cc @yjshen @alamb another commit pushed, PTAL, thank you.

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @WinkerDu

.evaluate_selection(batch, &remainder)?;
// Treat 'NULL' as false value
let when_value = match when_value {
ColumnarValue::Scalar(value) if value.is_null() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a reasonable solution I think -- ideally we would have been able to coerce this value into a ColumnarValue::Scalar(ScalarValue::Boolean(None)) at some earlier point.

However, I think this special case is reasonable (and the test coverage is nice) so 👍 from me

@alamb alamb merged commit d81657d into apache:master Apr 13, 2022
@WinkerDu
Copy link
Contributor Author

@alamb @yjshen thank you both!

@alamb
Copy link
Contributor

alamb commented Apr 13, 2022

Thank you @WinkerDu

@WinkerDu WinkerDu deleted the master-case-when-null branch April 17, 2022 17:16
ovr pushed a commit to cube-js/arrow-datafusion that referenced this pull request Aug 15, 2022
* case when support  literal

* fmt fix

* code clean

Co-authored-by: duripeng <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CASE expr with NULL literals panics 'WHEN expression did not return a BooleanArray'

3 participants