Skip to content

Commit 73d79d7

Browse files
committed
Decimal Vector
Signed-off-by: Nicholas Gates <[email protected]>
1 parent 1aa6825 commit 73d79d7

File tree

22 files changed

+520
-56
lines changed

22 files changed

+520
-56
lines changed

vortex-array/src/builders/decimal.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22
// SPDX-FileCopyrightText: Copyright the Vortex contributors
33

44
use std::any::Any;
5+
56
use vortex_buffer::BufferMut;
67
use vortex_dtype::{BigCast, DType, DecimalDType, NativeDecimalType, Nullability};
7-
use vortex_error::{vortex_ensure, vortex_panic, VortexExpect, VortexResult};
8+
use vortex_error::{VortexExpect, VortexResult, vortex_ensure, vortex_panic};
89
use vortex_mask::Mask;
910
use vortex_scalar::{
10-
i256, match_each_decimal_value, match_each_decimal_value_type, DecimalValue, Scalar,
11+
DecimalValue, Scalar, i256, match_each_decimal_value, match_each_decimal_value_type,
1112
};
1213

1314
use crate::arrays::DecimalArray;
14-
use crate::builders::{ArrayBuilder, LazyBitBufferBuilder, DEFAULT_BUILDER_CAPACITY};
15+
use crate::builders::{ArrayBuilder, DEFAULT_BUILDER_CAPACITY, LazyBitBufferBuilder};
1516
use crate::canonical::Canonical;
1617
use crate::{Array, ArrayRef, IntoArray, ToCanonical};
1718

vortex-array/src/stats/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ use std::hash::Hash;
88

99
use arrow_buffer::bit_iterator::BitIterator;
1010
use arrow_buffer::{BooleanBufferBuilder, MutableBuffer};
11-
use enum_iterator::{all, last, Sequence};
11+
use enum_iterator::{Sequence, all, last};
1212
use log::debug;
1313
use num_enum::{IntoPrimitive, TryFromPrimitive};
1414
pub use stats_set::*;
1515
use vortex_dtype::Nullability::{NonNullable, Nullable};
16-
use vortex_dtype::{i256, DType, DecimalDType, NativeDecimalType, PType};
16+
use vortex_dtype::{DType, DecimalDType, NativeDecimalType, PType, i256};
1717

1818
mod array;
1919
mod bound;

vortex-compute/src/mask/mod.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
66
use std::ops::BitAnd;
77

8-
use vortex_dtype::NativePType;
8+
use vortex_dtype::{NativeDecimalType, NativePType};
99
use vortex_mask::Mask;
1010
use vortex_vector::{
11-
BoolVector, NullVector, PVector, PrimitiveVector, StructVector, VarBinType, VarBinVector,
12-
Vector, match_each_pvector, match_each_vector,
11+
BoolVector, DVector, DecimalVector, NullVector, PVector, PrimitiveVector, StructVector,
12+
VarBinType, VarBinVector, Vector, match_each_dvector, match_each_pvector, match_each_vector,
1313
};
1414

1515
/// Trait for masking the validity of an array or vector.
@@ -42,6 +42,20 @@ impl MaskValidity for BoolVector {
4242
}
4343
}
4444

45+
impl MaskValidity for DecimalVector {
46+
fn mask_validity(self, mask: &Mask) -> Self {
47+
match_each_dvector!(self, |v| { MaskValidity::mask_validity(v, mask).into() })
48+
}
49+
}
50+
51+
impl<D: NativeDecimalType> MaskValidity for DVector<D> {
52+
fn mask_validity(self, mask: &Mask) -> Self {
53+
let (ps, elements, validity) = self.into_parts();
54+
// SAFETY: we are preserving the original elements buffer and only modifying the validity.
55+
unsafe { Self::new_unchecked(ps, elements, validity.bitand(mask)) }
56+
}
57+
}
58+
4559
impl MaskValidity for PrimitiveVector {
4660
fn mask_validity(self, mask: &Mask) -> Self {
4761
match_each_pvector!(self, |v| { MaskValidity::mask_validity(v, mask).into() })

vortex-datafusion/src/convert/scalars.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ use std::sync::Arc;
66
use datafusion_common::ScalarValue;
77
use vortex::buffer::ByteBuffer;
88
use vortex::dtype::datetime::arrow::make_temporal_ext_dtype;
9-
use vortex::dtype::datetime::{is_temporal_ext_type, TemporalMetadata, TimeUnit};
9+
use vortex::dtype::datetime::{TemporalMetadata, TimeUnit, is_temporal_ext_type};
1010
use vortex::dtype::half::f16;
1111
use vortex::dtype::{DType, DecimalDType, NativeDecimalType, Nullability, PType};
12-
use vortex::error::{vortex_bail, VortexResult};
13-
use vortex::scalar::{i256, DecimalValue, Scalar};
12+
use vortex::error::{VortexResult, vortex_bail};
13+
use vortex::scalar::{DecimalValue, Scalar, i256};
1414

1515
use crate::convert::{FromDataFusion, TryToDataFusion};
1616

@@ -244,12 +244,12 @@ impl FromDataFusion<ScalarValue> for Scalar {
244244

245245
#[cfg(test)]
246246
mod tests {
247-
use datafusion_common::arrow::datatypes::i256 as arrow_i256;
248247
use datafusion_common::ScalarValue;
248+
use datafusion_common::arrow::datatypes::i256 as arrow_i256;
249249
use rstest::rstest;
250250
use vortex::buffer::ByteBuffer;
251251
use vortex::dtype::{DType, DecimalDType, Nullability, PType};
252-
use vortex::scalar::{i256, DecimalValue, Scalar};
252+
use vortex::scalar::{DecimalValue, Scalar, i256};
253253

254254
use super::*;
255255

vortex-dtype/src/arbitrary/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use arbitrary::{Arbitrary, Result, Unstructured};
77
use vortex_error::VortexExpect;
88

99
use crate::{
10-
i256, DType, DecimalDType, FieldName, FieldNames, NativeDecimalType, Nullability,
11-
PType, StructFields,
10+
DType, DecimalDType, FieldName, FieldNames, NativeDecimalType, Nullability, PType,
11+
StructFields, i256,
1212
};
1313

1414
mod decimal;

vortex-dtype/src/decimal/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: Copyright the Vortex contributors
33

4+
mod max_precision;
45
mod precision;
56
mod types;
6-
mod max_precision;
77

8-
use crate::{i256, DType};
9-
use num_traits::ToPrimitive;
108
use std::fmt::{Display, Formatter};
11-
use vortex_error::{vortex_bail, vortex_panic, VortexError, VortexExpect, VortexResult};
129

10+
use num_traits::ToPrimitive;
1311
pub use precision::*;
1412
pub use types::*;
13+
use vortex_error::{VortexError, VortexExpect, VortexResult, vortex_bail, vortex_panic};
14+
15+
use crate::{DType, i256};
1516

1617
const MAX_PRECISION: u8 = <i256 as NativeDecimalType>::MAX_PRECISION;
1718
const MAX_SCALE: i8 = <i256 as NativeDecimalType>::MAX_SCALE;

vortex-dtype/src/decimal/precision.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: Copyright the Vortex contributors
33

4-
use crate::{DecimalDType, NativeDecimalType};
4+
use std::any::type_name;
5+
use std::fmt::Display;
56
use std::marker::PhantomData;
6-
use vortex_error::{vortex_bail, VortexExpect, VortexResult};
7+
8+
use vortex_error::{VortexExpect, VortexResult, vortex_bail};
9+
10+
use crate::{DecimalDType, NativeDecimalType};
711

812
/// A struct representing the precision and scale of a decimal type, to be represented
913
/// by the native type `D`.
@@ -14,6 +18,18 @@ pub struct PrecisionScale<D> {
1418
phantom: PhantomData<D>,
1519
}
1620

21+
impl<D: NativeDecimalType> Display for PrecisionScale<D> {
22+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23+
write!(
24+
f,
25+
"decimal({}, p={}, s={})",
26+
type_name::<D>(),
27+
self.precision,
28+
self.scale
29+
)
30+
}
31+
}
32+
1733
impl<D: NativeDecimalType> PrecisionScale<D> {
1834
/// Create a new [`PrecisionScale`] with the given precision and scale.
1935
///

vortex-dtype/src/decimal/types.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: Copyright the Vortex contributors
33

4+
use std::fmt::{Debug, Display};
5+
use std::panic::RefUnwindSafe;
6+
7+
use num_traits::{ConstOne, ConstZero};
8+
use paste::paste;
9+
410
use crate::decimal::max_precision::{
511
MAX_DECIMAL256_FOR_EACH_PRECISION, MIN_DECIMAL256_FOR_EACH_PRECISION,
612
};
7-
use crate::{i256, BigCast};
8-
use num_traits::ConstOne;
9-
use num_traits::ConstZero;
10-
use paste::paste;
11-
use std::fmt::{Debug, Display};
12-
use std::panic::RefUnwindSafe;
13+
use crate::{BigCast, i256};
1314

1415
/// Type of the decimal values.
1516
///

vortex-scalar/src/arbitrary.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::sync::Arc;
1212
use arbitrary::{Result, Unstructured};
1313
use vortex_buffer::{BufferString, ByteBuffer};
1414
use vortex_dtype::half::f16;
15-
use vortex_dtype::{i256, DType, DecimalDType, NativeDecimalType, PType};
15+
use vortex_dtype::{DType, DecimalDType, NativeDecimalType, PType, i256};
1616

1717
use crate::{DecimalValue, InnerScalarValue, PValue, Scalar, ScalarValue};
1818

vortex-scalar/src/scalar.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use std::hash::Hash;
66
use std::sync::Arc;
77

88
use vortex_buffer::Buffer;
9-
use vortex_dtype::{i256, DType, NativeDType, NativeDecimalType, Nullability};
10-
use vortex_error::{vortex_bail, vortex_err, VortexError, VortexExpect, VortexResult};
9+
use vortex_dtype::{DType, NativeDType, NativeDecimalType, Nullability, i256};
10+
use vortex_error::{VortexError, VortexExpect, VortexResult, vortex_bail, vortex_err};
1111

1212
use super::*;
1313

0 commit comments

Comments
 (0)