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

CaseWhen: coerce the all then and else data type to a common data type #2819

Merged
merged 5 commits into from
Jul 4, 2022

Conversation

liukun4515
Copy link
Contributor

Which issue does this PR close?

Closes #2818

Rationale for this change

What changes are included in this PR?

Are there any user-facing changes?

@liukun4515 liukun4515 marked this pull request as draft June 30, 2022 14:02
@github-actions github-actions bot added the physical-expr Changes to the physical-expr crates label Jun 30, 2022
@codecov-commenter
Copy link

codecov-commenter commented Jun 30, 2022

Codecov Report

Merging #2819 (de66710) into master (b47ab7c) will increase coverage by 0.01%.
The diff coverage is 98.78%.

@@            Coverage Diff             @@
##           master    #2819      +/-   ##
==========================================
+ Coverage   85.18%   85.19%   +0.01%     
==========================================
  Files         275      275              
  Lines       48564    48634      +70     
==========================================
+ Hits        41367    41434      +67     
- Misses       7197     7200       +3     
Impacted Files Coverage Δ
datafusion/physical-expr/src/expressions/case.rs 92.75% <98.75%> (+0.75%) ⬆️
datafusion/physical-expr/src/planner.rs 92.36% <100.00%> (ø)
datafusion/expr/src/logical_plan/plan.rs 74.11% <0.00%> (-0.40%) ⬇️
datafusion/core/src/physical_plan/metrics/value.rs 87.43% <0.00%> (+0.50%) ⬆️
datafusion/expr/src/window_frame.rs 93.27% <0.00%> (+0.84%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b47ab7c...de66710. Read the comment docs.

@liukun4515 liukun4515 force-pushed the fix_case_when_datatype_#2818 branch from 50fd76b to 00edadf Compare July 1, 2022 07:31
@liukun4515 liukun4515 marked this pull request as ready for review July 1, 2022 07:31
@liukun4515 liukun4515 requested review from alamb, yjshen and andygrove and removed request for alamb and yjshen July 1, 2022 07:31
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.

Looks good to me -- I had some style suggestions but nothing major. Thank you @liukun4515

@@ -76,7 +77,7 @@ impl CaseExpr {
/// Create a new CASE WHEN expression
pub fn try_new(
expr: Option<Arc<dyn PhysicalExpr>>,
when_then_expr: &[WhenThen],
when_then_expr: Vec<WhenThen>,
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

Ok(Arc::new(CaseExpr::try_new(expr, when_thens, else_expr)?))
}

fn get_case_common_type(
when_thens: &[WhenThen],
else_expr: Option<Arc<dyn PhysicalExpr>>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
else_expr: Option<Arc<dyn PhysicalExpr>>,
else_expr: Option<&PhysicalExpr>,

I wonder if there is a reason it needs to get a copy, or would a reference work too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The reference is not work for this.

Comment on lines 309 to 315
let left = when_thens
.into_iter()
.map(|(when, then)| {
let then = try_cast(then, input_schema, data_type.clone()).unwrap();
(when, then)
})
.collect::<Vec<WhenThen>>();
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we sure this will always return without error (aka that this will not panic)?

Since this function can return an Result anyways, would it be better to return it. Something like (untested):

Suggested change
let left = when_thens
.into_iter()
.map(|(when, then)| {
let then = try_cast(then, input_schema, data_type.clone()).unwrap();
(when, then)
})
.collect::<Vec<WhenThen>>();
let left = when_thens
.into_iter()
.map(|(when, then)| {
let then = try_cast(then, input_schema, data_type.clone())?
Ok((when, then))
})
.collect::<Result<Vec<WhenThen>>>()?;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good catch

let when1 = binary(
col("a", &schema)?,
Operator::Eq,
lit(ScalarValue::Utf8(Some("foo".to_string()))),
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be nice if the the lit in the physical expr could use lit("foo") as well. But I tried it and it doesn't work

Suggested change
lit(ScalarValue::Utf8(Some("foo".to_string()))),
lit("foo"),

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will rebase this pr after #2828 merged

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed the code to use lit("foo") with merged #2828

@github-actions github-actions bot added the core Core DataFusion crate label Jul 2, 2022
@alamb alamb force-pushed the fix_case_when_datatype_#2818 branch from 00b0c39 to d50c671 Compare July 2, 2022 10:35
@alamb
Copy link
Contributor

alamb commented Jul 2, 2022

I accidentally pushed commits to this branch -- I think I have removed them now. My apologies

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Core DataFusion crate physical-expr Changes to the physical-expr crates
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CASE When: result type should be coercible to a common type
3 participants