Skip to content
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

fix: CASE with NULL #11542

Merged
merged 11 commits into from
Jul 22, 2024
Merged

fix: CASE with NULL #11542

merged 11 commits into from
Jul 22, 2024

Conversation

Weijun-H
Copy link
Member

Which issue does this PR close?

Closes #11258

Rationale for this change

What changes are included in this PR?

Are these changes tested?

Are there any user-facing changes?

@github-actions github-actions bot added logical-expr Logical plan and expressions sqllogictest SQL Logic Tests (.slt) labels Jul 19, 2024
.map_or(Ok(DataType::Null), |e| e.get_type(schema))?;

match (then_type.clone(), else_type.clone()) {
(DataType::Null, DataType::Null) => Ok(DataType::Int64),
Copy link
Member

Choose a reason for hiding this comment

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

There might be a problem with the following expression.

CASE data WHEN 1 THEN NULL WHEN 2 THEN 3.3 ELSE NULL END;

Its type being float would be more reasonable.

Another more general fix might be to ensure that type coercion has already been performed before calling get_type, but I am not sure if that can be achieved. Therefore, the current solution is okay for me.

Copy link
Member Author

Choose a reason for hiding this comment

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

added a test for it

datafusion/expr/src/expr_schema.rs Outdated Show resolved Hide resolved
@Weijun-H Weijun-H requested a review from jonahgao July 21, 2024 02:20
Comment on lines +117 to +122
for (_, then_expr) in &case.when_then_expr {
let then_type = then_expr.get_type(schema)?;
if !then_type.is_null() {
return Ok(then_type);
}
}
Copy link
Member Author

@Weijun-H Weijun-H Jul 22, 2024

Choose a reason for hiding this comment

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

return first non-null then type here @jonahgao

Copy link
Member

@jonahgao jonahgao left a comment

Choose a reason for hiding this comment

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

Looks good to me 👍, thanks @Weijun-H

@alamb alamb merged commit 5c65efc into apache:main Jul 22, 2024
24 checks passed
@alamb
Copy link
Contributor

alamb commented Jul 22, 2024

Thanks @Weijun-H and @jonahgao

Lordworms pushed a commit to Lordworms/arrow-datafusion that referenced this pull request Jul 23, 2024
* fix: CASE with NULL

* chore: Add tests

* chore

* chore: Fix CI

* chore: Support all types are NULL

* chore: Fix CI

* chore: add more tests

* fix: Return first non-null type in then exprs

* chore: Fix CI

* Update datafusion/expr/src/expr_schema.rs

Co-authored-by: Jonah Gao <[email protected]>

* Update datafusion/expr/src/expr_schema.rs

Co-authored-by: Jonah Gao <[email protected]>

---------

Co-authored-by: Jonah Gao <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
logical-expr Logical plan and expressions sqllogictest SQL Logic Tests (.slt)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CASE with NULL branch does not coerce when passed to aggregate function
3 participants