Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support Utf8View for get_wider_type + binary_to_string_coercion functions #13370

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 63 additions & 2 deletions datafusion/expr-common/src/type_coercion/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -834,14 +834,16 @@ pub fn get_wider_type(lhs: &DataType, rhs: &DataType) -> Result<DataType> {
use arrow::datatypes::DataType::*;
Ok(match (lhs, rhs) {
(lhs, rhs) if lhs == rhs => lhs.clone(),
// Either Utf8View + Utf8 will return Utf8View
(Utf8View, Utf8) | (Utf8, Utf8View) => Utf8View,
// Right UInt is larger than left UInt.
(UInt8, UInt16 | UInt32 | UInt64) | (UInt16, UInt32 | UInt64) | (UInt32, UInt64) |
// Right Int is larger than left Int.
(Int8, Int16 | Int32 | Int64) | (Int16, Int32 | Int64) | (Int32, Int64) |
// Right Float is larger than left Float.
(Float16, Float32 | Float64) | (Float32, Float64) |
// Right String is larger than left String.
(Utf8, LargeUtf8) |
(Utf8 | Utf8View, LargeUtf8) |
// Any right type is wider than a left hand side Null.
(Null, _) => rhs.clone(),
// Left UInt is larger than right UInt.
Expand All @@ -851,7 +853,7 @@ pub fn get_wider_type(lhs: &DataType, rhs: &DataType) -> Result<DataType> {
// Left Float is larger than right Float.
(Float32 | Float64, Float16) | (Float64, Float32) |
// Left String is larger than right String.
(LargeUtf8, Utf8) |
(LargeUtf8, Utf8 | Utf8View) |
// Any left type is wider than a right hand side Null.
(_, Null) => lhs.clone(),
(List(lhs_field), List(rhs_field)) => {
Expand Down Expand Up @@ -1172,16 +1174,22 @@ fn binary_to_string_coercion(
match (lhs_type, rhs_type) {
(Binary, Utf8) => Some(Utf8),
(Binary, LargeUtf8) => Some(LargeUtf8),
(Binary, Utf8View) => Some(Utf8View),
(BinaryView, Utf8) => Some(Utf8View),
(BinaryView, LargeUtf8) => Some(LargeUtf8),
(BinaryView, Utf8View) => Some(Utf8View),
(LargeBinary, Utf8) => Some(LargeUtf8),
(LargeBinary, LargeUtf8) => Some(LargeUtf8),
(LargeBinary, Utf8View) => Some(Utf8View),
jonathanc-n marked this conversation as resolved.
Show resolved Hide resolved
(Utf8, Binary) => Some(Utf8),
(Utf8, LargeBinary) => Some(LargeUtf8),
(Utf8, BinaryView) => Some(Utf8View),
(LargeUtf8, Binary) => Some(LargeUtf8),
(LargeUtf8, LargeBinary) => Some(LargeUtf8),
(LargeUtf8, BinaryView) => Some(LargeUtf8),
(Utf8View, Binary) => Some(Utf8View),
(Utf8View, LargeBinary) => Some(LargeUtf8),
(Utf8View, BinaryView) => Some(Utf8View),
_ => None,
}
}
Expand Down Expand Up @@ -1544,6 +1552,59 @@ mod tests {
);
}

#[test]
fn test_get_wider_type_with_utf8view() {
assert_eq!(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not this kind of test, but a valid sql query that goes through the coercion function

Copy link
Contributor Author

@jonathanc-n jonathanc-n Nov 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jayzhan211 Correct me if I'm wrong, but this seems to be a problem with arrow casting from numerics to utf8view, the same can be said for this, #13366. However correct me if I'm wrong, I can't seem to find a sql query that triggers it, although i believe there should be one

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems only like_coercion utilize this coercion. I think current coercion is unnecessary complex for like operator

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think get_wider_type is actually only used by ArrayConcat

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean binary_to_string_coercion

get_wider_type(&DataType::Utf8View, &DataType::Utf8View).unwrap(),
DataType::Utf8View
);

assert_eq!(
get_wider_type(&DataType::Utf8View, &DataType::Utf8).unwrap(),
DataType::Utf8View
);

assert_eq!(
get_wider_type(&DataType::Utf8View, &DataType::LargeUtf8).unwrap(),
DataType::LargeUtf8
);

assert_eq!(
get_wider_type(&DataType::Utf8View, &DataType::Null).unwrap(),
DataType::Utf8View
);

assert!(get_wider_type(&DataType::Utf8View, &DataType::Int32).is_err());
}

#[test]
fn test_binary_to_string_coercion_with_utf8view() {
assert_eq!(
binary_to_string_coercion(&DataType::Binary, &DataType::Utf8View),
Some(DataType::Utf8View)
);
assert_eq!(
binary_to_string_coercion(&DataType::Utf8View, &DataType::Binary),
Some(DataType::Utf8View)
);
assert_eq!(
binary_to_string_coercion(&DataType::BinaryView, &DataType::Utf8View),
Some(DataType::Utf8View)
);
assert_eq!(
binary_to_string_coercion(&DataType::Utf8View, &DataType::LargeBinary),
Some(DataType::LargeUtf8)
);
assert_eq!(
binary_to_string_coercion(&DataType::LargeBinary, &DataType::Utf8View),
Some(DataType::Utf8View)
jonathanc-n marked this conversation as resolved.
Show resolved Hide resolved
);
assert_eq!(
binary_to_string_coercion(&DataType::Utf8View, &DataType::Int32),
None
);
}

/// Test coercion rules for binary operators
///
/// Applies coercion rules for `$LHS_TYPE $OP $RHS_TYPE` and asserts that the
Expand Down