Skip to content

Commit 54a7868

Browse files
authored
Fix bug where binary types were incorrectly being casted for coercible signatures (#18750)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Closes #18746 ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> When presented with a coercible signature that expects binary types, there was a bug where a binary type could be incorrectly casted to another binary type even though it should've stayed as its original self. We were missing an arm to allowed binary origin types to pass through as is where the native type was also binary. ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> Add arm in native default cast to ensure binary types don't get needlessly casted if the native type is binary. ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> Added test. ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> No. <!-- If there are any breaking changes to public APIs, please add the `api change` label. -->
1 parent 0366f0e commit 54a7868

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

datafusion/common/src/types/native.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,7 @@ impl LogicalType for NativeType {
241241
(Self::Decimal(p, s), _) => Decimal256(*p, *s),
242242
(Self::Timestamp(tu, tz), _) => Timestamp(*tu, tz.clone()),
243243
// If given type is Date, return the same type
244-
(Self::Date, origin) if matches!(origin, Date32 | Date64) => {
245-
origin.to_owned()
246-
}
244+
(Self::Date, Date32 | Date64) => origin.to_owned(),
247245
(Self::Date, _) => Date32,
248246
(Self::Time(tu), _) => match tu {
249247
TimeUnit::Second | TimeUnit::Millisecond => Time32(*tu),
@@ -253,6 +251,8 @@ impl LogicalType for NativeType {
253251
(Self::Interval(iu), _) => Interval(*iu),
254252
(Self::Binary, LargeUtf8) => LargeBinary,
255253
(Self::Binary, Utf8View) => BinaryView,
254+
// We don't cast to another kind of binary type if the origin one is already a binary type
255+
(Self::Binary, Binary | LargeBinary | BinaryView) => origin.to_owned(),
256256
(Self::Binary, data_type) if can_cast_types(data_type, &BinaryView) => {
257257
BinaryView
258258
}

datafusion/expr/src/type_coercion/functions.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,8 @@ mod tests {
943943

944944
use super::*;
945945
use arrow::datatypes::Field;
946-
use datafusion_common::assert_contains;
946+
use datafusion_common::{assert_contains, types::logical_binary};
947+
use datafusion_expr_common::signature::{Coercion, TypeSignatureClass};
947948

948949
#[test]
949950
fn test_string_conversion() {
@@ -1336,6 +1337,30 @@ mod tests {
13361337
Ok(())
13371338
}
13381339

1340+
#[test]
1341+
fn test_get_valid_types_coercible_binary() -> Result<()> {
1342+
let signature = Signature::coercible(
1343+
vec![Coercion::new_exact(TypeSignatureClass::Native(
1344+
logical_binary(),
1345+
))],
1346+
Volatility::Immutable,
1347+
);
1348+
1349+
// Binary types should stay their original selves
1350+
for t in [
1351+
DataType::Binary,
1352+
DataType::BinaryView,
1353+
DataType::LargeBinary,
1354+
] {
1355+
assert_eq!(
1356+
get_valid_types("", &signature.type_signature, std::slice::from_ref(&t))?,
1357+
vec![vec![t]]
1358+
);
1359+
}
1360+
1361+
Ok(())
1362+
}
1363+
13391364
#[test]
13401365
fn test_get_valid_types_fixed_size_arrays() -> Result<()> {
13411366
let function = "fixed_size_arrays";

0 commit comments

Comments
 (0)