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 expressions type coercion #5796

Closed
wants to merge 1 commit into from
Closed
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
50 changes: 47 additions & 3 deletions datafusion/expr/src/expr_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +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::{aggregate_function, function, window_function};
use arrow::compute::can_cast_types;
use arrow::datatypes::DataType;
Expand Down Expand Up @@ -68,7 +69,25 @@ impl ExprSchemable for Expr {
Expr::OuterReferenceColumn(ty, _) => Ok(ty.clone()),
Expr::ScalarVariable(ty, _) => Ok(ty.clone()),
Expr::Literal(l) => Ok(l.get_datatype()),
Expr::Case(case) => case.when_then_expr[0].1.get_type(schema),
Expr::Case(case) => {
let then_types = case
.when_then_expr
.iter()
.map(|when_then| when_then.1.get_type(schema))
.collect::<Result<Vec<_>>>()?;
let else_type = case
.else_expr
.as_ref()
.map_or(Ok(None), |e| e.get_type(schema).map(Some))?;

get_coerce_type_for_case_when(&then_types, else_type.as_ref()).ok_or_else(
|| {
DataFusionError::Plan(
"Unable to coerce return type of CASE expression".to_owned(),
)
},
)
}
Expr::Cast(Cast { data_type, .. })
| Expr::TryCast(TryCast { data_type, .. }) => Ok(data_type.clone()),
Expr::ScalarUDF { fun, args } => {
Expand Down Expand Up @@ -289,9 +308,9 @@ impl ExprSchemable for Expr {
#[cfg(test)]
mod tests {
use super::*;
use crate::{col, lit};
use crate::{col, lit, when};
use arrow::datatypes::DataType;
use datafusion_common::Column;
use datafusion_common::{Column, ScalarValue};

#[test]
fn expr_schema_nullability() {
Expand All @@ -312,6 +331,31 @@ mod tests {
);
}

#[test]
fn case_expr_data_type() {
let expr_else_null = when(col("foo").eq(lit(1)), lit(1_i32))
.otherwise(lit(ScalarValue::Null))
.unwrap();

assert_eq!(
DataType::Int32,
expr_else_null
.get_type(&MockExprSchema::new().with_data_type(DataType::Int64))
.unwrap()
);

let expr_then_null = when(col("foo").eq(lit(1)), lit(ScalarValue::Null))
.otherwise(lit(1_i32))
.unwrap();

assert_eq!(
DataType::Int32,
expr_then_null
.get_type(&MockExprSchema::new().with_data_type(DataType::Int64))
.unwrap()
);
}

struct MockExprSchema {
nullable: bool,
data_type: DataType,
Expand Down