Skip to content

Commit

Permalink
Rewrite Decimal and DecimalArray using const_generic (#2383)
Browse files Browse the repository at this point in the history
* const generic decimal

Signed-off-by: remzi <[email protected]>

* fix docs and lint

Signed-off-by: remzi <[email protected]>

* add bound

Signed-off-by: remzi <[email protected]>
  • Loading branch information
HaoYang670 authored Aug 9, 2022
1 parent 56f7904 commit 77c814c
Show file tree
Hide file tree
Showing 18 changed files with 324 additions and 384 deletions.
422 changes: 192 additions & 230 deletions arrow/src/array/array_decimal.rs

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions arrow/src/array/builder/decimal_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ use num::BigInt;
use std::any::Any;
use std::sync::Arc;

use crate::array::array_decimal::{BasicDecimalArray, Decimal256Array};
use crate::array::array_decimal::Decimal256Array;
use crate::array::ArrayRef;
use crate::array::Decimal128Array;
use crate::array::{ArrayBuilder, FixedSizeBinaryBuilder};

use crate::error::{ArrowError, Result};

use crate::datatypes::{validate_decimal256_precision, validate_decimal_precision};
use crate::util::decimal::{BasicDecimal, Decimal256};
use crate::util::decimal::Decimal256;

/// Array Builder for [`Decimal128Array`]
///
Expand Down Expand Up @@ -258,7 +258,7 @@ mod tests {
use super::*;
use num::Num;

use crate::array::array_decimal::{BasicDecimalArray, Decimal128Array};
use crate::array::array_decimal::Decimal128Array;
use crate::array::{array_decimal, Array};
use crate::datatypes::DataType;
use crate::util::decimal::{Decimal128, Decimal256};
Expand Down Expand Up @@ -305,21 +305,21 @@ mod tests {
fn test_decimal256_builder() {
let mut builder = Decimal256Builder::new(30, 40, 6);

let mut bytes = vec![0; 32];
let mut bytes = [0_u8; 32];
bytes[0..16].clone_from_slice(&8_887_000_000_i128.to_le_bytes());
let value = Decimal256::try_new_from_bytes(40, 6, bytes.as_slice()).unwrap();
let value = Decimal256::try_new_from_bytes(40, 6, &bytes).unwrap();
builder.append_value(&value).unwrap();

builder.append_null();

bytes = vec![255; 32];
let value = Decimal256::try_new_from_bytes(40, 6, bytes.as_slice()).unwrap();
bytes = [255; 32];
let value = Decimal256::try_new_from_bytes(40, 6, &bytes).unwrap();
builder.append_value(&value).unwrap();

bytes = vec![0; 32];
bytes = [0; 32];
bytes[0..16].clone_from_slice(&0_i128.to_le_bytes());
bytes[15] = 128;
let value = Decimal256::try_new_from_bytes(40, 6, bytes.as_slice()).unwrap();
let value = Decimal256::try_new_from_bytes(40, 6, &bytes).unwrap();
builder.append_value(&value).unwrap();

builder.append_option(None::<&Decimal256>).unwrap();
Expand Down Expand Up @@ -349,9 +349,9 @@ mod tests {
fn test_decimal256_builder_unmatched_precision_scale() {
let mut builder = Decimal256Builder::new(30, 10, 6);

let mut bytes = vec![0; 32];
let mut bytes = [0_u8; 32];
bytes[0..16].clone_from_slice(&8_887_000_000_i128.to_le_bytes());
let value = Decimal256::try_new_from_bytes(40, 6, bytes.as_slice()).unwrap();
let value = Decimal256::try_new_from_bytes(40, 6, &bytes).unwrap();
builder.append_value(&value).unwrap();
}

Expand Down
1 change: 0 additions & 1 deletion arrow/src/array/equal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,6 @@ mod tests {
use std::convert::TryFrom;
use std::sync::Arc;

use crate::array::BasicDecimalArray;
use crate::array::{
array::Array, ArrayData, ArrayDataBuilder, ArrayRef, BooleanArray,
FixedSizeBinaryBuilder, FixedSizeListBuilder, GenericBinaryArray, Int32Builder,
Expand Down
11 changes: 6 additions & 5 deletions arrow/src/array/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// under the License.

use crate::array::array::ArrayAccessor;
use crate::array::{BasicDecimalArray, Decimal256Array};
use crate::array::BasicDecimalArray;

use super::{
Array, BooleanArray, Decimal128Array, GenericBinaryArray, GenericListArray,
Expand Down Expand Up @@ -104,14 +104,15 @@ pub type GenericStringIter<'a, T> = ArrayIter<&'a GenericStringArray<T>>;
pub type GenericBinaryIter<'a, T> = ArrayIter<&'a GenericBinaryArray<T>>;
pub type GenericListArrayIter<'a, O> = ArrayIter<&'a GenericListArray<O>>;

pub type BasicDecimalIter<'a, const BYTE_WIDTH: usize> =
ArrayIter<&'a BasicDecimalArray<BYTE_WIDTH>>;
/// an iterator that returns `Some(Decimal128)` or `None`, that can be used on a
/// [`Decimal128Array`]
pub type Decimal128Iter<'a> = ArrayIter<&'a Decimal128Array>;
pub type Decimal128Iter<'a> = BasicDecimalIter<'a, 16>;

/// an iterator that returns `Some(Decimal256)` or `None`, that can be used on a
/// [`Decimal256Array`]
pub type Decimal256Iter<'a> = ArrayIter<&'a Decimal256Array>;

/// [`super::Decimal256Array`]
pub type Decimal256Iter<'a> = BasicDecimalIter<'a, 32>;
/// an iterator that returns `Some(i128)` or `None`, that can be used on a
/// [`Decimal128Array`]
#[derive(Debug)]
Expand Down
1 change: 0 additions & 1 deletion arrow/src/array/ord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use std::cmp::Ordering;

use crate::array::BasicDecimalArray;
use crate::array::*;
use crate::datatypes::TimeUnit;
use crate::datatypes::*;
Expand Down
2 changes: 0 additions & 2 deletions arrow/src/array/transform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,8 +670,6 @@ mod tests {
use std::{convert::TryFrom, sync::Arc};

use super::*;

use crate::array::BasicDecimalArray;
use crate::array::Decimal128Array;
use crate::{
array::{
Expand Down
1 change: 0 additions & 1 deletion arrow/src/compute/kernels/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2420,7 +2420,6 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::array::BasicDecimalArray;
use crate::datatypes::TimeUnit;
use crate::util::decimal::Decimal128;
use crate::{buffer::Buffer, util::display::array_value_to_string};
Expand Down
1 change: 0 additions & 1 deletion arrow/src/compute/kernels/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

//! Defines sort kernel for `ArrayRef`
use crate::array::BasicDecimalArray;
use crate::array::*;
use crate::buffer::MutableBuffer;
use crate::compute::take;
Expand Down
2 changes: 0 additions & 2 deletions arrow/src/compute/kernels/take.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
use std::{ops::AddAssign, sync::Arc};

use crate::array::BasicDecimalArray;

use crate::buffer::{Buffer, MutableBuffer};
use crate::compute::util::{
take_value_indices_from_fixed_size_list, take_value_indices_from_list,
Expand Down
1 change: 0 additions & 1 deletion arrow/src/csv/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1116,7 +1116,6 @@ mod tests {
use std::io::{Cursor, Write};
use tempfile::NamedTempFile;

use crate::array::BasicDecimalArray;
use crate::array::*;
use crate::compute::cast;
use crate::datatypes::Field;
Expand Down
1 change: 0 additions & 1 deletion arrow/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,6 @@ impl<'a> ArrowArrayChild<'a> {
#[cfg(test)]
mod tests {
use super::*;
use crate::array::BasicDecimalArray;
use crate::array::{
export_array_into_raw, make_array, Array, ArrayData, BooleanArray,
Decimal128Array, DictionaryArray, DurationSecondArray, FixedSizeBinaryArray,
Expand Down
Loading

0 comments on commit 77c814c

Please sign in to comment.