Skip to content

Commit

Permalink
More
Browse files Browse the repository at this point in the history
  • Loading branch information
viirya committed Jul 5, 2022
1 parent ab93dba commit bb2aa82
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 72 deletions.
72 changes: 22 additions & 50 deletions arrow/src/array/array_decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ use crate::datatypes::{
};
use crate::error::{ArrowError, Result};
use crate::util::decimal::{BasicDecimal, Decimal128, Decimal256};
use std::marker::PhantomData;

pub struct GenericDecimalArray<T: BasicDecimal, const VALUE_LENGTH: i32> {
data: ArrayData,
value_data: RawPtrBox<u8>,
precision: usize,
scale: usize,
phantom: PhantomData<T>,
}

/// `DecimalArray` stores fixed width decimal numbers,
Expand Down Expand Up @@ -90,13 +92,10 @@ impl<T: BasicDecimal, const VALUE_LENGTH: i32> GenericDecimalArray<T, VALUE_LENG
value_data,
precision,
scale,
phantom: PhantomData,
}
}

pub fn data(&self) -> &ArrayData {
&self.data
}

/// Return the precision (total digits) that can be stored by this array
pub fn precision(&self) -> usize {
self.precision
Expand All @@ -109,10 +108,8 @@ impl<T: BasicDecimal, const VALUE_LENGTH: i32> GenericDecimalArray<T, VALUE_LENG

/// Returns the element at index `i`.
pub fn value(&self, i: usize) -> T {
let data = self.data();
assert!(i < data.len(), "Out of bounds access");

let offset = i + data.offset();
assert!(i < self.data.len(), "DecimalArray out of bounds access");
let offset = i + self.data.offset();
let raw_val = unsafe {
let pos = self.value_offset_at(offset);
std::slice::from_raw_parts(
Expand All @@ -128,39 +125,37 @@ impl<T: BasicDecimal, const VALUE_LENGTH: i32> GenericDecimalArray<T, VALUE_LENG
/// Note this doesn't do any bound checking, for performance reason.
#[inline]
pub fn value_offset(&self, i: usize) -> i32 {
self.value_offset_at(self.data().offset() + i)
self.value_offset_at(self.data.offset() + i)
}

/// Returns the length for an element.
///
/// All elements have the same length as the array is a fixed size.
#[inline]
pub fn value_length(&self) -> i32 {
pub const fn value_length(&self) -> i32 {
VALUE_LENGTH
}

/// Returns a clone of the value data buffer
pub fn value_data(&self) -> Buffer {
self.data().buffers()[0].clone()
self.data.buffers()[0].clone()
}

#[inline]
pub fn value_offset_at(&self, i: usize) -> i32 {
fn value_offset_at(&self, i: usize) -> i32 {
VALUE_LENGTH * i as i32
}

#[inline]
pub fn value_as_string(&self, row: usize) -> String {
self.value(row).to_string()
}
}

pub trait FromFixedSizeList<U: From<ArrayData>> {
fn from_fixed_size_list_array(
pub fn from_fixed_size_list_array(
v: FixedSizeListArray,
precision: usize,
scale: usize,
) -> U {
) -> Self {
let child_data = &v.data_ref().child_data()[0];
assert_eq!(
child_data.child_data().len(),
Expand All @@ -183,13 +178,10 @@ pub trait FromFixedSizeList<U: From<ArrayData>> {
.offset(list_offset);

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

impl FromFixedSizeList<DecimalArray> for DecimalArray {}
impl FromFixedSizeList<Decimal256Array> for Decimal256Array {}

impl DecimalArray {
/// Creates a [DecimalArray] with default precision and scale,
/// based on an iterator of `i128` values without nulls
Expand Down Expand Up @@ -351,24 +343,16 @@ impl<Ptr: Borrow<Option<i128>>> FromIterator<Ptr> for DecimalArray {
}
}

impl fmt::Debug for DecimalArray {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "DecimalArray<{}, {}>\n[\n", self.precision, self.scale)?;
print_long_array(self, f, |array, index, f| {
let formatted_decimal = array.value_as_string(index);

write!(f, "{}", formatted_decimal)
})?;
write!(f, "]")
}
}

impl fmt::Debug for Decimal256Array {
impl<T: BasicDecimal + 'static, const VALUE_LENGTH: i32> fmt::Debug
for GenericDecimalArray<T, VALUE_LENGTH>
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"Decimal256Array<{}, {}>\n[\n",
self.precision, self.scale
"Decimal{}Array<{}, {}>\n[\n",
VALUE_LENGTH * 8,
self.precision,
self.scale
)?;
print_long_array(self, f, |array, index, f| {
let formatted_decimal = array.value_as_string(index);
Expand All @@ -379,21 +363,9 @@ impl fmt::Debug for Decimal256Array {
}
}

impl Array for DecimalArray {
fn as_any(&self) -> &dyn Any {
self
}

fn data(&self) -> &ArrayData {
&self.data
}

fn into_data(self) -> ArrayData {
self.into()
}
}

impl Array for Decimal256Array {
impl<T: BasicDecimal + 'static, const VALUE_LENGTH: i32> Array
for GenericDecimalArray<T, VALUE_LENGTH>
{
fn as_any(&self) -> &dyn Any {
self
}
Expand Down
24 changes: 4 additions & 20 deletions arrow/src/array/equal_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// under the License.

use super::*;
use crate::array::array_decimal::GenericDecimalArray;
use crate::datatypes::*;
use crate::util::decimal::BasicDecimal;
use array::Array;
Expand Down Expand Up @@ -360,26 +361,9 @@ impl PartialEq<FixedSizeBinaryArray> for Value {
}
}

impl JsonEqual for DecimalArray {
fn equals_json(&self, json: &[&Value]) -> bool {
if self.len() != json.len() {
return false;
}

(0..self.len()).all(|i| match json[i] {
JString(s) => {
self.is_valid(i)
&& (s
.parse::<i128>()
.map_or_else(|_| false, |v| v == self.value(i).as_i128()))
}
JNull => self.is_null(i),
_ => false,
})
}
}

impl JsonEqual for Decimal256Array {
impl<T: BasicDecimal + 'static, const VALUE_LENGTH: i32> JsonEqual
for GenericDecimalArray<T, VALUE_LENGTH>
{
fn equals_json(&self, json: &[&Value]) -> bool {
if self.len() != json.len() {
return false;
Expand Down
2 changes: 1 addition & 1 deletion arrow/src/util/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::error::{ArrowError, Result};
use num::bigint::BigInt;
use std::cmp::{min, Ordering};

pub trait BasicDecimal: PartialOrd + Ord + PartialEq + Eq {
pub trait BasicDecimal: PartialOrd + Ord + PartialEq + Eq + Sync + Send {
/// The bit-width of the internal representation.
const BIT_WIDTH: usize;

Expand Down
1 change: 0 additions & 1 deletion parquet/src/arrow/arrow_writer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use std::sync::Arc;

use arrow::array as arrow_array;
use arrow::array::ArrayRef;
use arrow::array::BasicDecimalArray;
use arrow::datatypes::{DataType as ArrowDataType, IntervalUnit, SchemaRef};
use arrow::record_batch::RecordBatch;
use arrow_array::Array;
Expand Down

0 comments on commit bb2aa82

Please sign in to comment.