Skip to content

Commit 50c2d1c

Browse files
committed
Support concatenation of mixed FixedSizeBinary types without conversion to Binary
1 parent b89473e commit 50c2d1c

2 files changed

Lines changed: 50 additions & 37 deletions

File tree

‎datafusion/expr-common/src/type_coercion/binary.rs‎

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ impl<'a> BinaryTypeCoercer<'a> {
237237
})
238238
}
239239
StringConcat => {
240-
string_concat_coercion(lhs, rhs).map(Signature::uniform).ok_or_else(|| {
240+
string_concat_coercion(lhs, rhs).ok_or_else(|| {
241241
plan_datafusion_err!(
242242
"Cannot infer common string type for string concat operation {} {} {}", self.lhs, self.op, self.rhs
243243
)
@@ -1635,42 +1635,55 @@ fn ree_coercion(
16351635
/// 1. At least one side of lhs and rhs should be string type (Utf8 / LargeUtf8)
16361636
/// 2. Data type of the other side should be able to cast to string type
16371637
/// 3. Binary and string types cannot be mixed
1638-
fn string_concat_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<DataType> {
1638+
fn string_concat_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<Signature> {
16391639
use arrow::datatypes::DataType::*;
1640-
string_coercion(lhs_type, rhs_type).or_else(|| match (lhs_type, rhs_type) {
1641-
// Allow pure binary + binary
1642-
(
1643-
Binary | LargeBinary | BinaryView | FixedSizeBinary(_),
1644-
Binary | LargeBinary | BinaryView | FixedSizeBinary(_),
1645-
) => {
1646-
// Coerce fixed-sized binary to variable-sized `Binary` to make uniform signature
1647-
// with the `Binary` result
1648-
let lhs_type = match lhs_type {
1649-
FixedSizeBinary(_) => &Binary,
1650-
val => val,
1651-
};
1652-
let rhs_type = match rhs_type {
1653-
FixedSizeBinary(_) => &Binary,
1654-
val => val,
1655-
};
1656-
binary_coercion(lhs_type, rhs_type)
1657-
}
1658-
// Predicate-based coercion rules are following,
1659-
// including mixed binary + string combinations
1660-
(Utf8View, from_type) | (from_type, Utf8View) => {
1661-
string_concat_internal_coercion(from_type, &Utf8View)
1662-
}
1663-
(Utf8, from_type) | (from_type, Utf8) => {
1664-
string_concat_internal_coercion(from_type, &Utf8)
1665-
}
1666-
(LargeUtf8, from_type) | (from_type, LargeUtf8) => {
1667-
string_concat_internal_coercion(from_type, &LargeUtf8)
1668-
}
1669-
(Dictionary(_, lhs_value_type), Dictionary(_, rhs_value_type)) => {
1670-
string_coercion(lhs_value_type, rhs_value_type).or(None)
1671-
}
1672-
_ => None,
1673-
})
1640+
1641+
string_coercion(lhs_type, rhs_type)
1642+
.map(Signature::uniform)
1643+
.or_else(|| match (lhs_type, rhs_type) {
1644+
// Allow concatenation of mixed fixed size binary
1645+
(FixedSizeBinary(l), FixedSizeBinary(r)) => Some(Signature {
1646+
lhs: lhs_type.clone(),
1647+
rhs: rhs_type.clone(),
1648+
ret: FixedSizeBinary(l + r),
1649+
}),
1650+
// Allow pure binary + binary
1651+
(
1652+
Binary | LargeBinary | BinaryView | FixedSizeBinary(_),
1653+
Binary | LargeBinary | BinaryView | FixedSizeBinary(_),
1654+
) => {
1655+
// Coerce fixed-sized binary to variable-sized `Binary` to make uniform signature
1656+
// with the `Binary` result
1657+
let lhs_type = match lhs_type {
1658+
FixedSizeBinary(_) => &Binary,
1659+
val => val,
1660+
};
1661+
let rhs_type = match rhs_type {
1662+
FixedSizeBinary(_) => &Binary,
1663+
val => val,
1664+
};
1665+
binary_coercion(lhs_type, rhs_type).map(Signature::uniform)
1666+
}
1667+
// Predicate-based coercion rules are following,
1668+
// including mixed binary + string combinations
1669+
(Utf8View, from_type) | (from_type, Utf8View) => {
1670+
string_concat_internal_coercion(from_type, &Utf8View)
1671+
.map(Signature::uniform)
1672+
}
1673+
(Utf8, from_type) | (from_type, Utf8) => {
1674+
string_concat_internal_coercion(from_type, &Utf8).map(Signature::uniform)
1675+
}
1676+
(LargeUtf8, from_type) | (from_type, LargeUtf8) => {
1677+
string_concat_internal_coercion(from_type, &LargeUtf8)
1678+
.map(Signature::uniform)
1679+
}
1680+
(Dictionary(_, lhs_value_type), Dictionary(_, rhs_value_type)) => {
1681+
string_coercion(lhs_value_type, rhs_value_type)
1682+
.or(None)
1683+
.map(Signature::uniform)
1684+
}
1685+
_ => None,
1686+
})
16741687
}
16751688

16761689
fn array_coercion(lhs_type: &DataType, rhs_type: &DataType) -> Option<DataType> {

‎datafusion/sqllogictest/test_files/binary.slt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ SELECT x'636166c3a9' || arrow_cast(x'68656c6c6f', 'FixedSizeBinary(5)'), arrow_t
347347
query ?T
348348
SELECT arrow_cast(x'6361', 'FixedSizeBinary(2)') || arrow_cast(x'68656c6c6f', 'FixedSizeBinary(5)'), arrow_typeof(arrow_cast(x'6361', 'FixedSizeBinary(2)') || arrow_cast(x'68656c6c6f', 'FixedSizeBinary(5)'));
349349
----
350-
636168656c6c6f Binary
350+
636168656c6c6f FixedSizeBinary(7)
351351

352352
# Byte pipe operator is allowed for mixed binary and text
353353
query T

0 commit comments

Comments
 (0)