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: Enhance case expression type coercion #5820

Merged
merged 6 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions datafusion/core/tests/sqllogictests/test_files/select.slt
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,19 @@ select * from (select 1 a union all select 2) b order by a limit null;
query I
select * from (select 1 a union all select 2) b order by a limit 0;
----

# select case when type coercion with case expression
query I
select CASE 10.5 WHEN 0 THEN 1 ELSE 2 END;
----
2

# select case when type coercion without case expression
query I
select CASE
WHEN 10 = 5 THEN 1
WHEN 'true' THEN 2
ELSE 3
END;
----
2
2 changes: 1 addition & 1 deletion datafusion/expr/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ impl Display for BinaryExpr {
}

/// CASE expression
#[derive(Clone, PartialEq, Eq, Hash)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Case {
/// Optional base expression that can be compared to literal values in the "when" expressions
pub expr: Option<Box<Expr>>,
Expand Down
9 changes: 4 additions & 5 deletions datafusion/expr/src/expr_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::expr::{
};
use crate::field_util::get_indexed_field;
use crate::type_coercion::binary::binary_operator_data_type;
use crate::type_coercion::other::get_coerce_type_for_case_when;
use crate::type_coercion::other::get_coerce_type_for_case_expression;
use crate::{aggregate_function, function, window_function};
use arrow::compute::can_cast_types;
use arrow::datatypes::DataType;
Expand Down Expand Up @@ -81,13 +81,12 @@ impl ExprSchemable for Expr {
None => Ok(None),
Some(expr) => expr.get_type(schema).map(Some),
}?;
get_coerce_type_for_case_when(&then_types, else_type.as_ref()).ok_or_else(
|| {
get_coerce_type_for_case_expression(&then_types, else_type.as_ref())
.ok_or_else(|| {
DataFusionError::Internal(String::from(
"Cannot infer type for CASE statement",
))
},
)
})
}
Expr::Cast(Cast { data_type, .. })
| Expr::TryCast(TryCast { data_type, .. }) => Ok(data_type.clone()),
Expand Down
20 changes: 10 additions & 10 deletions datafusion/expr/src/type_coercion/other.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,20 @@ pub fn get_coerce_type_for_list(
})
}

/// Find a common coerceable type for all `then_types` as well
/// and the `else_type`, if specified.
/// Returns the common data type for `then_types` and `else_type`
pub fn get_coerce_type_for_case_when(
then_types: &[DataType],
else_type: Option<&DataType>,
/// Find a common coerceable type for all `when_or_then_types` as well
/// and the `case_or_else_type`, if specified.
/// Returns the common data type for `when_or_then_types` and `case_or_else_type`
pub fn get_coerce_type_for_case_expression(
when_or_then_types: &[DataType],
case_or_else_type: Option<&DataType>,
) -> Option<DataType> {
let else_type = match else_type {
None => then_types[0].clone(),
let case_or_else_type = match case_or_else_type {
None => when_or_then_types[0].clone(),
Some(data_type) => data_type.clone(),
};
then_types
when_or_then_types
.iter()
.fold(Some(else_type), |left, right_type| match left {
.fold(Some(case_or_else_type), |left, right_type| match left {
// failed to find a valid coercion in a previous iteration
None => None,
// TODO: now just use the `equal` coercion rule for case when. If find the issue, and
Expand Down
Loading