Skip to content

Apache DataFusion SQL Query Engine

Fix bug where binary types were incorrectly being casted for coercible signatures (#18750)

Jeffrey Vo54a78689c37b
diff --git a/datafusion/common/src/types/native.rs b/datafusion/common/src/types/native.rs
index a1495b779a..5e6fca040c 100644
--- a/datafusion/common/src/types/native.rs
+++ b/datafusion/common/src/types/native.rs
@@ -241,9 +241,7 @@ impl LogicalType for NativeType {
(Self::Decimal(p, s), _) => Decimal256(*p, *s),
(Self::Timestamp(tu, tz), _) => Timestamp(*tu, tz.clone()),
// If given type is Date, return the same type
- (Self::Date, origin) if matches!(origin, Date32 | Date64) => {
- origin.to_owned()
- }
+ (Self::Date, Date32 | Date64) => origin.to_owned(),
(Self::Date, _) => Date32,
(Self::Time(tu), _) => match tu {
TimeUnit::Second | TimeUnit::Millisecond => Time32(*tu),
@@ -253,6 +251,8 @@ impl LogicalType for NativeType {
(Self::Interval(iu), _) => Interval(*iu),
(Self::Binary, LargeUtf8) => LargeBinary,
(Self::Binary, Utf8View) => BinaryView,
+ // We don't cast to another kind of binary type if the origin one is already a binary type
+ (Self::Binary, Binary | LargeBinary | BinaryView) => origin.to_owned(),
(Self::Binary, data_type) if can_cast_types(data_type, &BinaryView) => {
BinaryView
}
diff --git a/datafusion/expr/src/type_coercion/functions.rs b/datafusion/expr/src/type_coercion/functions.rs
index bcaff11bcd..0239ed38b6 100644
--- a/datafusion/expr/src/type_coercion/functions.rs
+++ b/datafusion/expr/src/type_coercion/functions.rs
@@ -943,7 +943,8 @@ mod tests {
use super::*;
use arrow::datatypes::Field;
- use datafusion_common::assert_contains;
+ use datafusion_common::{assert_contains, types::logical_binary};
+ use datafusion_expr_common::signature::{Coercion, TypeSignatureClass};
#[test]
fn test_string_conversion() {
@@ -1336,6 +1337,30 @@ mod tests {
Ok(())
}
+ #[test]
+ fn test_get_valid_types_coercible_binary() -> Result<()> {
+ let signature = Signature::coercible(
+ vec![Coercion::new_exact(TypeSignatureClass::Native(
+ logical_binary(),
+ ))],
+ Volatility::Immutable,
+ );
+
+ // Binary types should stay their original selves
+ for t in [
+ DataType::Binary,
+ DataType::BinaryView,
+ DataType::LargeBinary,
+ ] {
+ assert_eq!(
+ get_valid_types("", &signature.type_signature, std::slice::from_ref(&t))?,
+ vec![vec![t]]
+ );
+ }
+
+ Ok(())
+ }
+
#[test]
fn test_get_valid_types_fixed_size_arrays() -> Result<()> {
let function = "fixed_size_arrays";