Apache DataFusion SQL Query Engine
Add Utf8->Binary type coercion for comparison (#7080)
Jonah Gao52cf58b46133
diff --git a/datafusion/core/tests/sqllogictests/test_files/binary.slt b/datafusion/core/tests/sqllogictests/test_files/binary.sltindex ca55ff56cb..54499e2978 100644--- a/datafusion/core/tests/sqllogictests/test_files/binary.slt+++ b/datafusion/core/tests/sqllogictests/test_files/binary.slt@@ -45,6 +45,23 @@ FF01 ff01 Utf8 BinaryABC 0abc Utf8 Binary000 0000 Utf8 Binary+# comparisons+query ?BBBB+SELECT+ column2,+ -- binary compare with string+ column2 = 'ABC',+ column2 <> 'ABC',+ -- binary compared with binary+ column2 = X'ABC',+ column2 <> X'ABC'+FROM t;+----+ff01 false true false true+0abc false true true false+0000 false true false true++# predicatesquery T?SELECT column1, column2@@ -127,3 +144,9 @@ SELECT column1, column1 = arrow_cast(X'0102', 'FixedSizeBinary(2)') FROM t# Comparison to different sized Binaryquery error DataFusion error: Error during planning: Cannot infer common argument type for comparison operation FixedSizeBinary\(3\) = BinarySELECT column1, column1 = X'0102' FROM t++statement ok+drop table t_source++statement ok+drop table tdiff --git a/datafusion/expr/src/type_coercion/binary.rs b/datafusion/expr/src/type_coercion/binary.rsindex 56a5664638..9ebea19a16 100644--- a/datafusion/expr/src/type_coercion/binary.rs+++ b/datafusion/expr/src/type_coercion/binary.rs@@ -318,6 +318,7 @@ pub fn comparison_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<D.or_else(|| null_coercion(lhs_type, rhs_type)).or_else(|| string_numeric_coercion(lhs_type, rhs_type)).or_else(|| string_temporal_coercion(lhs_type, rhs_type))+ .or_else(|| binary_coercion(lhs_type, rhs_type))}/// Coerce `lhs_type` and `rhs_type` to a common type for the purposes of a comparison operation@@ -769,6 +770,18 @@ fn string_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<DataType>}}+/// Coercion rules for Binaries: the type that both lhs and rhs can be+/// casted to for the purpose of a computation+fn binary_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<DataType> {+ use arrow::datatypes::DataType::*;+ match (lhs_type, rhs_type) {+ (Binary | Utf8, Binary) | (Binary, Utf8) => Some(Binary),+ (LargeBinary | Binary | Utf8 | LargeUtf8, LargeBinary)+ | (LargeBinary, Binary | Utf8 | LargeUtf8) => Some(LargeBinary),+ _ => None,+ }+}+/// coercion rules for like operations./// This is a union of string coercion rules and dictionary coercion rulespub fn like_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<DataType> {@@ -1039,10 +1052,13 @@ mod tests {let rhs_type = Dictionary(Box::new(Int8), Box::new(Int16));assert_eq!(dictionary_coercion(&lhs_type, &rhs_type, true), Some(Utf8));- // Can not coerce values of Binary to int, cannot support this+ // Since we can coerce values of Utf8 to Binary can support thislet lhs_type = Dictionary(Box::new(Int8), Box::new(Utf8));let rhs_type = Dictionary(Box::new(Int8), Box::new(Binary));- assert_eq!(dictionary_coercion(&lhs_type, &rhs_type, true), None);+ assert_eq!(+ dictionary_coercion(&lhs_type, &rhs_type, true),+ Some(Binary)+ );let lhs_type = Dictionary(Box::new(Int8), Box::new(Utf8));let rhs_type = Utf8;@@ -1443,6 +1459,70 @@ mod tests {DataType::Decimal128(15, 3));+ // Binary+ test_coercion_binary_rule!(+ DataType::Binary,+ DataType::Binary,+ Operator::Eq,+ DataType::Binary+ );+ test_coercion_binary_rule!(+ DataType::Utf8,+ DataType::Binary,+ Operator::Eq,+ DataType::Binary+ );+ test_coercion_binary_rule!(+ DataType::Binary,+ DataType::Utf8,+ Operator::Eq,+ DataType::Binary+ );++ // LargeBinary+ test_coercion_binary_rule!(+ DataType::LargeBinary,+ DataType::LargeBinary,+ Operator::Eq,+ DataType::LargeBinary+ );+ test_coercion_binary_rule!(+ DataType::Binary,+ DataType::LargeBinary,+ Operator::Eq,+ DataType::LargeBinary+ );+ test_coercion_binary_rule!(+ DataType::LargeBinary,+ DataType::Binary,+ Operator::Eq,+ DataType::LargeBinary+ );+ test_coercion_binary_rule!(+ DataType::Utf8,+ DataType::LargeBinary,+ Operator::Eq,+ DataType::LargeBinary+ );+ test_coercion_binary_rule!(+ DataType::LargeBinary,+ DataType::Utf8,+ Operator::Eq,+ DataType::LargeBinary+ );+ test_coercion_binary_rule!(+ DataType::LargeUtf8,+ DataType::LargeBinary,+ Operator::Eq,+ DataType::LargeBinary+ );+ test_coercion_binary_rule!(+ DataType::LargeBinary,+ DataType::LargeUtf8,+ Operator::Eq,+ DataType::LargeBinary+ );+// TODO add other data typeOk(())}