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: cast between Binary/LargeBinary and FixedSizeBinary #3961

Merged
merged 4 commits into from
Mar 29, 2023

Conversation

Weijun-H
Copy link
Member

@Weijun-H Weijun-H commented Mar 27, 2023

Which issue does this PR close?

Closes #3826

Rationale for this change

What changes are included in this PR?

Support cast between Binary/LargeBinary and FixedSizeBinary

Are there any user-facing changes?

@github-actions github-actions bot added the arrow Changes to the arrow crate label Mar 27, 2023
Comment on lines 3446 to 3450
if is_binary && offsets > i32::MAX as i64 {
return Err(ArrowError::ComputeError(
"Cast from FixedSizeBinary to Binary would overflow".to_string(),
));
}
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this also used to LargeBinaryArray?

/// If the target one is too large for the source array it will return an Error.
fn cast_fixed_size_binary_to_binary<O: OffsetSizeTrait>(
array: &dyn Array,
to_type: &DataType,
Copy link
Member

Choose a reason for hiding this comment

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

to_type looks redundant as it is known GenericBinaryType<O>.

Copy link
Contributor

@tustvold tustvold left a comment

Choose a reason for hiding this comment

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

Looking good, left some comments

Comment on lines 3413 to 3416
let array = array
.as_any()
.downcast_ref::<GenericBinaryArray<O>>()
.unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
let array = array
.as_any()
.downcast_ref::<GenericBinaryArray<O>>()
.unwrap();
let array = array.as_binary::<O>();


let offsets: i128 = byte_width as i128 * array.len() as i128;

let is_binary = matches!(to_type, DataType::Binary);
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we change this to match on GenericBinaryType<O>::DATA_TYPE instead

let a1 = Arc::new(FixedSizeBinaryArray::from(binary_data.clone())) as ArrayRef;

let array_ref = cast(&a1, &DataType::Binary).unwrap();
let down_cast = array_ref.as_any().downcast_ref::<BinaryArray>().unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
let down_cast = array_ref.as_any().downcast_ref::<BinaryArray>().unwrap();
let down_cast = array_ref.as_binary::<i32>();

Comment on lines 5431 to 5434
let down_cast = array_ref
.as_any()
.downcast_ref::<LargeBinaryArray>()
.unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
let down_cast = array_ref
.as_any()
.downcast_ref::<LargeBinaryArray>()
.unwrap();
let down_cast = array_ref.as_binary::<i64>();

if array.is_null(i) {
builder.append_null();
} else {
builder.append_value(array.value(i))?;
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 this should handle CastOptions::safe and insert nulls instead of erroring if safe = true

Comment on lines 3448 to 3453
"Cast from FixedSizeBinary to Binary would overflow".to_string(),
));
} else if !is_binary && offsets > i64::MAX as i128 {
return Err(ArrowError::ComputeError(
"Cast from FixedSizeBinary to LargeBinary would overflow".to_string(),
));
Copy link
Member

Choose a reason for hiding this comment

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

The error message can be more specific. E.g., Cannot cast the FixedSizeBinary to Binary because exceeding maximum offset of Binary.

Copy link
Contributor

@kazuyukitanimura kazuyukitanimura left a comment

Choose a reason for hiding this comment

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

LGTM (non-binding)

@tustvold tustvold merged commit c876bbd into apache:master Mar 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrow Changes to the arrow crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support Casting Between Binary / LargeBinary and FixedSizeBinary
4 participants