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

Add support of converting FixedSizeBinaryArray to DecimalArray #2041

Merged
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
64 changes: 63 additions & 1 deletion arrow/src/array/array_decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ use std::convert::From;
use std::fmt;
use std::{any::Any, iter::FromIterator};

use super::BooleanBufferBuilder;
use super::{
array::print_long_array, raw_pointer::RawPtrBox, Array, ArrayData, FixedSizeListArray,
};
use super::{BooleanBufferBuilder, FixedSizeBinaryArray};
pub use crate::array::DecimalIter;
use crate::buffer::Buffer;
use crate::datatypes::DataType;
Expand Down Expand Up @@ -148,6 +148,32 @@ pub trait BasicDecimalArray<T: BasicDecimal, U: From<ArrayData>>:
self.value(row).to_string()
}

/// Build a decimal array from [`FixedSizeBinaryArray`].
///
/// NB: This function does not validate that each value is in the permissible
/// range for a decimal
fn from_fixed_size_binary_array(
v: FixedSizeBinaryArray,
precision: usize,
scale: usize,
) -> U {
assert!(
v.value_length() == Self::VALUE_LENGTH,
"Value length of the array ({}) must equal to the byte width of the decimal ({})",
Copy link
Contributor

Choose a reason for hiding this comment

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

But from this message, this function is just used to convert to the decimal array.
@HaoYang670

v.value_length(),
Self::VALUE_LENGTH,
);

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 use the new APIs for this

let builder = v
    .into_data()
    .into_builder()
    .data_type(DataType::Decimal(precision, scale));
Self::from(unsafe { builder.build_unchecked() });

Saves some clones and is slightly less verbose

let builder = ArrayData::builder(DataType::Decimal(precision, scale))
.len(v.len())
.add_buffer(v.value_data())
.null_bit_buffer(v.data_ref().null_buffer().cloned())
.offset(v.offset());

let array_data = unsafe { builder.build_unchecked() };
U::from(array_data)
}

fn from_fixed_size_list_array(
v: FixedSizeListArray,
precision: usize,
Expand Down Expand Up @@ -646,6 +672,42 @@ mod tests {
);
}

#[test]
fn test_decimal_array_from_fixed_size_binary() {
let value_data = ArrayData::builder(DataType::FixedSizeBinary(16))
.offset(1)
.len(3)
.add_buffer(Buffer::from_slice_ref(&[99999_i128, 2, 34, 560]))
.null_bit_buffer(Some(Buffer::from_slice_ref(&[0b1010])))
.build()
.unwrap();

let binary_array = FixedSizeBinaryArray::from(value_data);
let decimal = DecimalArray::from_fixed_size_binary_array(binary_array, 38, 1);

assert_eq!(decimal.len(), 3);
assert_eq!(decimal.value_as_string(0), "0.2".to_string());
assert!(decimal.is_null(1));
assert_eq!(decimal.value_as_string(2), "56.0".to_string());
}

#[test]
#[should_panic(
expected = "Value length of the array (8) must equal to the byte width of the decimal (16)"
)]
fn test_decimal_array_from_fixed_size_binary_wrong_length() {
let value_data = ArrayData::builder(DataType::FixedSizeBinary(8))
.offset(1)
.len(3)
.add_buffer(Buffer::from_slice_ref(&[99999_i64, 2, 34, 560]))
.null_bit_buffer(Some(Buffer::from_slice_ref(&[0b1010])))
.build()
.unwrap();

let binary_array = FixedSizeBinaryArray::from(value_data);
let _ = DecimalArray::from_fixed_size_binary_array(binary_array, 38, 1);
}

#[test]
fn test_decimal_array_from_fixed_size_list() {
let value_data = ArrayData::builder(DataType::UInt8)
Expand Down