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 Decimal128 and Decimal256 to downcast_primitive #3056

Merged
merged 5 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
8 changes: 8 additions & 0 deletions arrow-array/src/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ macro_rules! downcast_temporal_array {
/// assert_eq!(primitive_size(&DataType::Int32), 4);
/// assert_eq!(primitive_size(&DataType::Int64), 8);
/// assert_eq!(primitive_size(&DataType::Float16), 2);
/// assert_eq!(primitive_size(&DataType::Decimal128(38, 10)), 16);
/// assert_eq!(primitive_size(&DataType::Decimal256(76, 20)), 32);
/// ```
///
/// [`DataType`]: arrow_schema::DataType
Expand All @@ -242,6 +244,12 @@ macro_rules! downcast_primitive {
$crate::repeat_pat!(arrow_schema::DataType::Float64, $($data_type),+) => {
$m!($crate::types::Float64Type $(, $args)*)
}
$crate::repeat_pat!(arrow_schema::DataType::Decimal128(_, _), $($data_type),+) => {
$m!($crate::types::Decimal128Type $(, $args)*)
}
$crate::repeat_pat!(arrow_schema::DataType::Decimal256(_, _), $($data_type),+) => {
$m!($crate::types::Decimal256Type $(, $args)*)
}
$crate::repeat_pat!(arrow_schema::DataType::Interval(arrow_schema::IntervalUnit::YearMonth), $($data_type),+) => {
$m!($crate::types::IntervalYearMonthType $(, $args)*)
}
Expand Down
13 changes: 12 additions & 1 deletion arrow/src/row/fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,20 @@ fn decode_fixed<T: FixedLengthEncoding + ToByteSlice>(
pub fn decode_primitive<T: ArrowPrimitiveType>(
rows: &mut [&[u8]],
options: SortOptions,
data_type: &DataType,
Copy link
Contributor

Choose a reason for hiding this comment

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

This is technically unsafe as it could allow for transmute from decimal128 to decimal256, etc... I have a PR in the works to fix this

) -> PrimitiveArray<T>
where
T::Native: FixedLengthEncoding + ToByteSlice,
{
decode_fixed::<T::Native>(rows, T::DATA_TYPE, options).into()
let array_data: ArrayData = decode_fixed::<T::Native>(rows, T::DATA_TYPE, options);
match data_type {
DataType::Decimal128(_, _)
| DataType::Decimal256(_, _)
| DataType::Timestamp(_, _) => {
let data = array_data.into_builder().data_type(data_type.clone());

unsafe { data.build_unchecked().into() }
}
_ => array_data.into(),
}
}
36 changes: 4 additions & 32 deletions arrow/src/row/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ use std::sync::Arc;

use arrow_array::cast::*;
use arrow_array::*;
use arrow_buffer::i256;

use crate::compute::SortOptions;
use crate::datatypes::*;
Expand Down Expand Up @@ -504,8 +503,6 @@ fn new_empty_rows(
array => lengths.iter_mut().for_each(|x| *x += fixed::encoded_len(array)),
DataType::Null => {},
DataType::Boolean => lengths.iter_mut().for_each(|x| *x += bool::ENCODED_LEN),
DataType::Decimal128(_, _) => lengths.iter_mut().for_each(|x| *x += i128::ENCODED_LEN),
DataType::Decimal256(_, _) => lengths.iter_mut().for_each(|x| *x += i256::ENCODED_LEN),
DataType::Binary => as_generic_binary_array::<i32>(array)
.iter()
.zip(lengths.iter_mut())
Expand Down Expand Up @@ -586,22 +583,6 @@ fn encode_column(
column => fixed::encode(out, column, opts),
DataType::Null => {}
DataType::Boolean => fixed::encode(out, as_boolean_array(column), opts),
DataType::Decimal128(_, _) => {
let column = column
.as_any()
.downcast_ref::<Decimal128Array>()
.unwrap();

fixed::encode(out, column, opts)
},
DataType::Decimal256(_, _) => {
let column = column
.as_any()
.downcast_ref::<Decimal256Array>()
.unwrap();

fixed::encode(out, column, opts)
},
DataType::Binary => {
variable::encode(out, as_generic_binary_array::<i32>(column).iter(), opts)
}
Expand Down Expand Up @@ -629,8 +610,8 @@ fn encode_column(
}

macro_rules! decode_primitive_helper {
($t:ty, $rows: ident, $options:ident) => {
Arc::new(decode_primitive::<$t>($rows, $options))
($t:ty, $rows: ident, $options:ident, $data_type: ident) => {
Arc::new(decode_primitive::<$t>($rows, $options, $data_type))
};
}

Expand All @@ -645,24 +626,15 @@ unsafe fn decode_column(
interner: Option<&OrderPreservingInterner>,
) -> Result<ArrayRef> {
let options = field.options;
let data_type = &field.data_type;
let array: ArrayRef = downcast_primitive! {
&field.data_type => (decode_primitive_helper, rows, options),
&field.data_type => (decode_primitive_helper, rows, options, data_type),
DataType::Null => Arc::new(NullArray::new(rows.len())),
DataType::Boolean => Arc::new(decode_bool(rows, options)),
DataType::Binary => Arc::new(decode_binary::<i32>(rows, options)),
DataType::LargeBinary => Arc::new(decode_binary::<i64>(rows, options)),
DataType::Utf8 => Arc::new(decode_string::<i32>(rows, options)),
DataType::LargeUtf8 => Arc::new(decode_string::<i64>(rows, options)),
DataType::Decimal128(p, s) => Arc::new(
decode_primitive::<Decimal128Type>(rows, options)
.with_precision_and_scale(*p, *s)
.unwrap(),
),
DataType::Decimal256(p, s) => Arc::new(
decode_primitive::<Decimal256Type>(rows, options)
.with_precision_and_scale(*p, *s)
.unwrap(),
),
DataType::Dictionary(k, v) => match k.as_ref() {
DataType::Int8 => Arc::new(decode_dictionary::<Int8Type>(
interner.unwrap(),
Expand Down