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

Support casting from integer to binary #5015

Merged
merged 4 commits into from
Nov 3, 2023
Merged
Changes from 2 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
80 changes: 79 additions & 1 deletion arrow-cast/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use crate::parse::{
string_to_datetime, Parser,
};
use arrow_array::{builder::*, cast::*, temporal_conversions::*, timezone::Tz, types::*, *};
use arrow_buffer::{i256, ArrowNativeType, Buffer, OffsetBuffer};
use arrow_buffer::{i256, ArrowNativeType, Buffer, OffsetBuffer, ToByteSlice};
use arrow_data::ArrayData;
use arrow_schema::*;
use arrow_select::take::take;
Expand Down Expand Up @@ -203,6 +203,8 @@ pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
(Utf8 | LargeUtf8, _) => to_type.is_numeric() && to_type != &Float16,
(_, Utf8 | LargeUtf8) => from_type.is_primitive(),

(_, Binary | LargeBinary) => from_type.is_integer(),

// start numeric casts
(
UInt8 | UInt16 | UInt32 | UInt64 | Int8 | Int16 | Int32 | Int64 | Float32 | Float64,
Expand Down Expand Up @@ -1368,6 +1370,28 @@ pub fn cast_with_options(
(from_type, Utf8) if from_type.is_primitive() => {
value_to_string::<i32>(array, cast_options)
}
(from_type, Binary) if from_type.is_integer() => match from_type {
UInt8 => cast_numeric_to_binary::<UInt8Type, i32>(array),
UInt16 => cast_numeric_to_binary::<UInt16Type, i32>(array),
UInt32 => cast_numeric_to_binary::<UInt32Type, i32>(array),
UInt64 => cast_numeric_to_binary::<UInt64Type, i32>(array),
Int8 => cast_numeric_to_binary::<Int8Type, i32>(array),
Int16 => cast_numeric_to_binary::<Int16Type, i32>(array),
Int32 => cast_numeric_to_binary::<Int32Type, i32>(array),
Int64 => cast_numeric_to_binary::<Int64Type, i32>(array),
_ => unreachable!(),
},
(from_type, LargeBinary) if from_type.is_integer() => match from_type {
UInt8 => cast_numeric_to_binary::<UInt8Type, i64>(array),
UInt16 => cast_numeric_to_binary::<UInt16Type, i64>(array),
UInt32 => cast_numeric_to_binary::<UInt32Type, i64>(array),
UInt64 => cast_numeric_to_binary::<UInt64Type, i64>(array),
Int8 => cast_numeric_to_binary::<Int8Type, i64>(array),
Int16 => cast_numeric_to_binary::<Int16Type, i64>(array),
Int32 => cast_numeric_to_binary::<Int32Type, i64>(array),
Int64 => cast_numeric_to_binary::<Int64Type, i64>(array),
_ => unreachable!(),
},
// start numeric casts
(UInt8, UInt16) => cast_numeric_arrays::<UInt8Type, UInt16Type>(array, cast_options),
(UInt8, UInt32) => cast_numeric_arrays::<UInt8Type, UInt32Type>(array, cast_options),
Expand Down Expand Up @@ -2317,6 +2341,22 @@ fn value_to_string<O: OffsetSizeTrait>(
Ok(Arc::new(builder.finish()))
}

fn cast_numeric_to_binary<FROM: ArrowPrimitiveType, O: OffsetSizeTrait>(
Copy link
Member Author

Choose a reason for hiding this comment

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

BTW, actually I also want to support non native endian encoding configured from cast options. But I will put it into follow ups.

Copy link
Contributor

Choose a reason for hiding this comment

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

That might be better as a custom kernel then? Perhaps kernels mirroring https://doc.rust-lang.org/std/primitive.i32.html#method.to_be and https://doc.rust-lang.org/std/primitive.i32.html#method.to_le ?

I think it would be good to avoid adding data type specific options to CastOptions

Copy link
Member Author

Choose a reason for hiding this comment

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

You mean to have a kernel doing to_be/to_le? It sounds good.

array: &dyn Array,
) -> Result<ArrayRef, ArrowError> {
let array = array.as_primitive::<FROM>();

let mut builder = GenericBinaryBuilder::<O>::new();
Copy link
Contributor

Choose a reason for hiding this comment

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

You could possibly initialize the correct capacity here

for i in 0..array.len() {
if array.is_null(i) {
builder.append_null();
} else {
builder.append_value(array.value(i).to_byte_slice());
}
}
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
for i in 0..array.len() {
if array.is_null(i) {
builder.append_null();
} else {
builder.append_value(array.value(i).to_byte_slice());
}
}
for v in array {
builder.append_option(v);
}

Perhaps

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, v is not AsRef<[u8]> though.

2352 |         builder.append_option(v);
     |                 ------------- ^ the trait `AsRef<[u8]>` is not implemented for `<FROM as arrow_array::ArrowPrimitiveType>::Native`
     |                 |
     |                 required by a bound introduced by this call

But I also cannot do builder.append_option(v.map(|v| v.to_byte_slice())); too as it complains returns a value referencing data owned by the current function.

I keep it unchanged so.

Ok(Arc::new(builder.finish()))
}

/// Parse UTF-8
fn parse_string<P: Parser, O: OffsetSizeTrait>(
array: &dyn Array,
Expand Down Expand Up @@ -5176,6 +5216,44 @@ mod tests {
assert!(down_cast.is_null(2));
}

#[test]
fn test_numeric_to_binary() {
let a = Int16Array::from(vec![Some(1), Some(511), None]);

let array_ref = cast(&a, &DataType::Binary).unwrap();
let down_cast = array_ref.as_binary::<i32>();
assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
assert!(down_cast.is_null(2));

let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);

let array_ref = cast(&a, &DataType::Binary).unwrap();
let down_cast = array_ref.as_binary::<i32>();
assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
assert!(down_cast.is_null(2));
}

#[test]
fn test_numeric_to_large_binary() {
let a = Int16Array::from(vec![Some(1), Some(511), None]);

let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
let down_cast = array_ref.as_binary::<i64>();
assert_eq!(&1_i16.to_le_bytes(), down_cast.value(0));
assert_eq!(&511_i16.to_le_bytes(), down_cast.value(1));
assert!(down_cast.is_null(2));

let a = Int64Array::from(vec![Some(-1), Some(123456789), None]);

let array_ref = cast(&a, &DataType::LargeBinary).unwrap();
let down_cast = array_ref.as_binary::<i64>();
assert_eq!(&(-1_i64).to_le_bytes(), down_cast.value(0));
assert_eq!(&123456789_i64.to_le_bytes(), down_cast.value(1));
assert!(down_cast.is_null(2));
}

#[test]
fn test_cast_date32_to_int32() {
let array = Date32Array::from(vec![10000, 17890]);
Expand Down
Loading