diff --git a/datafusion/expr/src/type_coercion/functions.rs b/datafusion/expr/src/type_coercion/functions.rs index 1bc17cffa60b..19acde87b080 100644 --- a/datafusion/expr/src/type_coercion/functions.rs +++ b/datafusion/expr/src/type_coercion/functions.rs @@ -277,8 +277,8 @@ fn maybe_data_types( new_type.push(current_type.clone()) } else { // attempt to coerce. - if let Some(valid_type) = coerced_from(valid_type, current_type) { - new_type.push(valid_type) + if let Some(coerced_type) = coerced_from(valid_type, current_type) { + new_type.push(coerced_type) } else { // not possible return None; @@ -416,7 +416,15 @@ fn coerced_from<'a>( // Note that not all rules in `comparison_coercion` can be reused here. // For example, all numeric types can be coerced into Utf8 for comparison, // but not for function arguments. - _ => comparison_binary_numeric_coercion(type_into, type_from), + _ => comparison_binary_numeric_coercion(type_into, type_from).and_then( + |coerced_type| { + if *type_into == coerced_type { + Some(coerced_type) + } else { + None + } + }, + ), } } diff --git a/datafusion/physical-expr/src/math_expressions.rs b/datafusion/physical-expr/src/math_expressions.rs index 98a05dff5386..a8c115ba3a82 100644 --- a/datafusion/physical-expr/src/math_expressions.rs +++ b/datafusion/physical-expr/src/math_expressions.rs @@ -25,6 +25,7 @@ use std::sync::Arc; use arrow::array::ArrayRef; use arrow::array::{BooleanArray, Float32Array, Float64Array, Int64Array}; use arrow::datatypes::DataType; +use arrow_array::Array; use rand::{thread_rng, Rng}; use datafusion_common::ScalarValue::{Float32, Int64}; @@ -92,8 +93,9 @@ macro_rules! downcast_arg { ($ARG:expr, $NAME:expr, $ARRAY_TYPE:ident) => {{ $ARG.as_any().downcast_ref::<$ARRAY_TYPE>().ok_or_else(|| { DataFusionError::Internal(format!( - "could not cast {} to {}", + "could not cast {} from {} to {}", $NAME, + $ARG.data_type(), type_name::<$ARRAY_TYPE>() )) })?