Skip to content

Commit 8288bcd

Browse files
committed
move
Signed-off-by: Joe Isaacs <[email protected]>
1 parent 15a76c0 commit 8288bcd

File tree

10 files changed

+67
-63
lines changed

10 files changed

+67
-63
lines changed

encodings/sparse/src/canonical.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ use vortex_array::vtable::{CanonicalVTable, ValidityHelper};
1919
use vortex_array::{Array, Canonical, ToCanonical};
2020
use vortex_buffer::{BitBuffer, Buffer, BufferString, ByteBuffer, buffer, buffer_mut};
2121
use vortex_dtype::{
22-
DType, DecimalDType, IntegerPType, NativeDecimalType, NativePType, Nullability, StructFields,
23-
match_each_integer_ptype, match_each_native_ptype, smallest_decimal_value_type,
22+
DType, DecimalDType, DecimalType, IntegerPType, NativeDecimalType, NativePType, Nullability,
23+
StructFields, match_each_integer_ptype, match_each_native_ptype,
2424
};
2525
use vortex_error::{VortexError, VortexExpect, vortex_panic};
2626
use vortex_scalar::{
@@ -58,7 +58,8 @@ impl CanonicalVTable<SparseVTable> for SparseVTable {
5858
array.len(),
5959
),
6060
DType::Decimal(decimal_dtype, nullability) => {
61-
let canonical_decimal_value_type = smallest_decimal_value_type(decimal_dtype);
61+
let canonical_decimal_value_type =
62+
DecimalType::smallest_decimal_value_type(decimal_dtype);
6263
let fill_value = array.fill_scalar().as_decimal();
6364
match_each_decimal_value_type!(canonical_decimal_value_type, |D| {
6465
canonicalize_sparse_decimal::<D>(

vortex-array/src/arrays/arbitrary.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ use std::sync::Arc;
66

77
use arbitrary::{Arbitrary, Result, Unstructured};
88
use vortex_buffer::{BitBuffer, Buffer};
9-
use vortex_dtype::{
10-
DType, IntegerPType, NativePType, Nullability, PType, smallest_decimal_value_type,
11-
};
9+
use vortex_dtype::{DType, IntegerPType, NativePType, Nullability, PType};
1210
use vortex_error::{VortexExpect, VortexUnwrap};
1311
use vortex_scalar::arbitrary::random_scalar;
1412
use vortex_scalar::{Scalar, match_each_decimal_value_type};
@@ -97,17 +95,20 @@ fn random_array_chunk(
9795
},
9896
DType::Decimal(decimal, n) => {
9997
let elem_len = chunk_len.unwrap_or(u.int_in_range(0..=20)?);
100-
match_each_decimal_value_type!(smallest_decimal_value_type(decimal), |DVT| {
101-
let mut builder =
102-
DecimalBuilder::new::<DVT>(decimal.precision(), decimal.scale(), *n);
103-
for _i in 0..elem_len {
104-
let random_decimal = random_scalar(u, &DType::Decimal(*decimal, *n))?;
105-
builder.append_scalar(&random_decimal).vortex_expect(
106-
"was somehow unable to append a decimal to a decimal builder",
107-
);
98+
match_each_decimal_value_type!(
99+
DecimalType::smallest_decimal_value_type(decimal),
100+
|DVT| {
101+
let mut builder =
102+
DecimalBuilder::new::<DVT>(decimal.precision(), decimal.scale(), *n);
103+
for _i in 0..elem_len {
104+
let random_decimal = random_scalar(u, &DType::Decimal(*decimal, *n))?;
105+
builder.append_scalar(&random_decimal).vortex_expect(
106+
"was somehow unable to append a decimal to a decimal builder",
107+
);
108+
}
109+
Ok(builder.finish())
108110
}
109-
Ok(builder.finish())
110-
})
111+
)
111112
}
112113
DType::Utf8(n) => random_string(u, *n, chunk_len),
113114
DType::Binary(n) => random_bytes(u, *n, chunk_len),

vortex-array/src/arrays/constant/vtable/canonical.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use std::sync::Arc;
55

66
use vortex_buffer::{BitBuffer, Buffer, buffer};
7-
use vortex_dtype::{DType, Nullability, match_each_native_ptype, smallest_decimal_value_type};
7+
use vortex_dtype::{DType, DecimalType, Nullability, match_each_native_ptype};
88
use vortex_error::VortexExpect;
99
use vortex_scalar::{
1010
BinaryScalar, BoolScalar, DecimalValue, ExtScalar, ListScalar, Scalar, StructScalar,
@@ -66,7 +66,7 @@ impl CanonicalVTable<ConstantVTable> for ConstantVTable {
6666
})
6767
}
6868
DType::Decimal(decimal_type, ..) => {
69-
let size = smallest_decimal_value_type(decimal_type);
69+
let size = DecimalType::smallest_decimal_value_type(decimal_type);
7070
let decimal = scalar.as_decimal();
7171
let Some(value) = decimal.decimal_value() else {
7272
let all_null = match_each_decimal_value_type!(size, |D| {

vortex-array/src/arrays/decimal/array.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use itertools::Itertools;
55
use vortex_buffer::{BitBufferMut, Buffer, BufferMut, ByteBuffer};
66
use vortex_dtype::{
77
BigCast, DType, DecimalDType, DecimalType, IntegerPType, NativeDecimalType,
8-
is_compatible_decimal_value_type, match_each_integer_ptype,
8+
match_each_integer_ptype,
99
};
1010
use vortex_error::{VortexExpect, VortexResult, vortex_ensure, vortex_panic};
1111
use vortex_scalar::match_each_decimal_value_type;
@@ -278,7 +278,7 @@ where
278278
PatchDVT: NativeDecimalType,
279279
ValuesDVT: NativeDecimalType,
280280
{
281-
if !is_compatible_decimal_value_type(ValuesDVT::DECIMAL_TYPE, decimal_dtype) {
281+
if !ValuesDVT::DECIMAL_TYPE.is_compatible_decimal_value_type(decimal_dtype) {
282282
vortex_panic!(
283283
"patch_typed: {:?} cannot represent every value in {}.",
284284
ValuesDVT::DECIMAL_TYPE,

vortex-array/src/arrays/decimal/compute/sum.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use arrow_schema::DECIMAL256_MAX_PRECISION;
55
use num_traits::AsPrimitive;
66
use vortex_dtype::Nullability::Nullable;
7-
use vortex_dtype::{DecimalDType, smallest_decimal_value_type};
7+
use vortex_dtype::{DecimalDType, DecimalType};
88
use vortex_error::{VortexResult, vortex_bail};
99
use vortex_mask::Mask;
1010
use vortex_scalar::{DecimalValue, Scalar, match_each_decimal_value_type};
@@ -54,7 +54,7 @@ impl SumKernel for DecimalVTable {
5454
vortex_bail!("invalid state, all-null array should be checked by top-level sum fn")
5555
}
5656
Mask::AllTrue(_) => {
57-
let values_type = smallest_decimal_value_type(&return_dtype);
57+
let values_type = DecimalType::smallest_decimal_value_type(&return_dtype);
5858
match_each_decimal_value_type!(array.values_type(), |I| {
5959
match_each_decimal_value_type!(values_type, |O| {
6060
Ok(Scalar::decimal(
@@ -66,7 +66,7 @@ impl SumKernel for DecimalVTable {
6666
})
6767
}
6868
Mask::Values(mask_values) => {
69-
let values_type = smallest_decimal_value_type(&return_dtype);
69+
let values_type = DecimalType::smallest_decimal_value_type(&return_dtype);
7070
match_each_decimal_value_type!(array.values_type(), |I| {
7171
match_each_decimal_value_type!(values_type, |O| {
7272
Ok(Scalar::decimal(

vortex-array/src/builders/mod.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
3131
use std::any::Any;
3232

33-
use vortex_dtype::{DType, match_each_native_ptype, smallest_decimal_value_type};
33+
use vortex_dtype::{DType, match_each_native_ptype};
3434
use vortex_error::{VortexResult, vortex_panic};
3535
use vortex_mask::Mask;
3636
use vortex_scalar::{Scalar, match_each_decimal_value_type};
@@ -245,13 +245,16 @@ pub fn builder_with_capacity(dtype: &DType, capacity: usize) -> Box<dyn ArrayBui
245245
})
246246
}
247247
DType::Decimal(decimal_type, n) => {
248-
match_each_decimal_value_type!(smallest_decimal_value_type(decimal_type), |D| {
249-
Box::new(DecimalBuilder::with_capacity::<D>(
250-
capacity,
251-
*decimal_type,
252-
*n,
253-
))
254-
})
248+
match_each_decimal_value_type!(
249+
DecimalType::smallest_decimal_value_type(decimal_type),
250+
|D| {
251+
Box::new(DecimalBuilder::with_capacity::<D>(
252+
capacity,
253+
*decimal_type,
254+
*n,
255+
))
256+
}
257+
)
255258
}
256259
DType::Utf8(n) => Box::new(VarBinViewBuilder::with_capacity(DType::Utf8(*n), capacity)),
257260
DType::Binary(n) => Box::new(VarBinViewBuilder::with_capacity(

vortex-dtype/src/decimal/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33

44
mod max_precision;
55
mod precision;
6-
mod smallest_type;
76
mod types;
87

98
use std::fmt::{Display, Formatter};
109

1110
use num_traits::ToPrimitive;
1211
pub use precision::*;
13-
pub use smallest_type::*;
1412
pub use types::*;
1513
use vortex_error::{VortexError, VortexExpect, VortexResult, vortex_bail, vortex_panic};
1614

vortex-dtype/src/decimal/smallest_type.rs

Lines changed: 0 additions & 23 deletions
This file was deleted.

vortex-dtype/src/decimal/types.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use paste::paste;
1010
use crate::decimal::max_precision::{
1111
MAX_DECIMAL256_FOR_EACH_PRECISION, MIN_DECIMAL256_FOR_EACH_PRECISION,
1212
};
13-
use crate::{BigCast, i256};
13+
use crate::{BigCast, DecimalDType, i256};
1414

1515
/// Type of the decimal values.
1616
///
@@ -35,6 +35,27 @@ pub enum DecimalType {
3535
I256 = 5,
3636
}
3737

38+
impl DecimalType {
39+
/// Maps a `DecimalDType` (precision) into the smallest `DecimalType` that can represent it.
40+
pub fn smallest_decimal_value_type(decimal_dtype: &DecimalDType) -> DecimalType {
41+
match decimal_dtype.precision() {
42+
1..=2 => DecimalType::I8,
43+
3..=4 => DecimalType::I16,
44+
5..=9 => DecimalType::I32,
45+
10..=18 => DecimalType::I64,
46+
19..=38 => DecimalType::I128,
47+
39..=76 => DecimalType::I256,
48+
0 => unreachable!("precision must be greater than 0"),
49+
p => unreachable!("precision larger than 76 is invalid found precision {p}"),
50+
}
51+
}
52+
53+
/// True if `Self` can represent every value of the type `DecimalDType`.
54+
pub fn is_compatible_decimal_value_type(self, dtype: DecimalDType) -> bool {
55+
self >= Self::smallest_decimal_value_type(&dtype)
56+
}
57+
}
58+
3859
/// Type of decimal scalar values.
3960
///
4061
/// This trait is implemented by native integer types that can be used to store decimal values.

vortex-scalar/src/arbitrary.rs

Lines changed: 9 additions & 6 deletions
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::{DType, DecimalDType, NativeDecimalType, PType, smallest_decimal_value_type};
15+
use vortex_dtype::{DType, DecimalDType, NativeDecimalType, PType};
1616

1717
use crate::{
1818
DecimalValue, InnerScalarValue, PValue, Scalar, ScalarValue, match_each_decimal_value_type,
@@ -84,11 +84,14 @@ fn random_pvalue(u: &mut Unstructured, ptype: &PType) -> Result<PValue> {
8484
/// Generate an arbitrary decimal scalar confined to the given bounds of precision and scale.
8585
pub fn random_decimal(u: &mut Unstructured, decimal_type: &DecimalDType) -> Result<ScalarValue> {
8686
let precision = decimal_type.precision();
87-
let value = match_each_decimal_value_type!(smallest_decimal_value_type(decimal_type), |D| {
88-
DecimalValue::from(u.int_in_range(
89-
D::MIN_BY_PRECISION[precision as usize]..=D::MAX_BY_PRECISION[precision as usize],
90-
)?)
91-
});
87+
let value = match_each_decimal_value_type!(
88+
DecimalType::smallest_decimal_value_type(decimal_type),
89+
|D| {
90+
DecimalValue::from(u.int_in_range(
91+
D::MIN_BY_PRECISION[precision as usize]..=D::MAX_BY_PRECISION[precision as usize],
92+
)?)
93+
}
94+
);
9295

9396
Ok(ScalarValue(InnerScalarValue::Decimal(value)))
9497
}

0 commit comments

Comments
 (0)