Skip to content

Commit cba6bf9

Browse files
authored
fix: Decimal append_scalar_value (#3412)
1 parent d8a95d0 commit cba6bf9

File tree

5 files changed

+51
-39
lines changed

5 files changed

+51
-39
lines changed

encodings/decimal-byte-parts/src/decimal_byte_parts/compute/compare.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ where
5656
P: NativePType + NumCast,
5757
ScalarValue: From<P>,
5858
{
59-
match_each_decimal_value!(decimal_value, |$decimal_v| {
60-
Some(ScalarValue::from(<P as NumCast>::from($decimal_v)?))
59+
match_each_decimal_value!(decimal_value, |decimal_v| {
60+
Some(ScalarValue::from(<P as NumCast>::from(decimal_v)?))
6161
})
6262
}
6363

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,8 @@ impl CanonicalVTable<ConstantVTable> for ConstantVTable {
6666
return Ok(Canonical::Decimal(all_null));
6767
};
6868

69-
let decimal_array = match_each_decimal_value!(value, |$V| {
70-
DecimalArray::new(
71-
Buffer::full(*$V, array.len()),
72-
*decimal_type,
73-
validity,
74-
)
69+
let decimal_array = match_each_decimal_value!(value, |value| {
70+
DecimalArray::new(Buffer::full(*value, array.len()), *decimal_type, validity)
7571
});
7672
Canonical::Decimal(decimal_array)
7773
}

vortex-array/src/builders/mod.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ pub use primitive::*;
4646
pub use struct_::*;
4747
pub use varbinview::*;
4848
use vortex_dtype::{DType, match_each_native_ptype};
49-
use vortex_error::{VortexExpect, VortexResult, vortex_bail, vortex_err};
49+
use vortex_error::{VortexResult, vortex_bail, vortex_err};
5050
use vortex_mask::Mask;
5151
use vortex_scalar::{
52-
BinaryScalar, BoolScalar, ExtScalar, ListScalar, PrimitiveScalar, Scalar, ScalarValue,
53-
StructScalar, Utf8Scalar, match_each_decimal_value_type,
52+
BinaryScalar, BoolScalar, DecimalValue, ExtScalar, ListScalar, PrimitiveScalar, Scalar,
53+
ScalarValue, StructScalar, Utf8Scalar, match_each_decimal_value, match_each_decimal_value_type,
5454
};
5555

5656
use crate::arrays::smallest_storage_type;
@@ -210,18 +210,19 @@ pub trait ArrayBuilderExt: ArrayBuilder {
210210
.append_option(PrimitiveScalar::try_from(scalar)?.typed_value::<P>())
211211
})
212212
}
213-
DType::Decimal(decimal_type, _) => {
214-
match_each_decimal_value_type!(smallest_storage_type(decimal_type), |D| {
215-
self.as_any_mut()
216-
.downcast_mut::<DecimalBuilder>()
217-
.ok_or_else(|| {
218-
vortex_err!("Cannot append decimal scalar to non-decimal builder")
219-
})?
220-
.append_option(
221-
Option::<D>::try_from(scalar.as_decimal())
222-
.vortex_expect("decimal conversion failure"),
223-
)
224-
})
213+
DType::Decimal(..) => {
214+
let builder = self
215+
.as_any_mut()
216+
.downcast_mut::<DecimalBuilder>()
217+
.ok_or_else(|| {
218+
vortex_err!("Cannot append decimal scalar to non-decimal builder")
219+
})?;
220+
match scalar.as_decimal().decimal_value() {
221+
None => builder.append_null(),
222+
Some(v) => match_each_decimal_value!(v, |dec_val| {
223+
builder.append_value(*dec_val);
224+
}),
225+
}
225226
}
226227
DType::Utf8(_) => self
227228
.as_any_mut()

vortex-python/src/scalar/into_py.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,10 @@ fn decimal_value_to_py(
169169
let m = py.import("decimal")?;
170170
let decimal_class = m.getattr("Decimal")?;
171171

172-
match_each_decimal_value!(decimal_value, |$V| {
173-
{
174-
let (whole, decimal) = $V.decimal_parts(scale);
175-
let repr = format!("{}.{:0>width$}", whole, decimal, width = scale as usize)
176-
.into_pyobject(py)?;
177-
decimal_class.call1((repr,))
178-
}
172+
match_each_decimal_value!(decimal_value, |value| {
173+
let (whole, decimal) = value.decimal_parts(scale);
174+
let repr =
175+
format!("{}.{:0>width$}", whole, decimal, width = scale as usize).into_pyobject(py)?;
176+
decimal_class.call1((repr,))
179177
})
180178
}

vortex-scalar/src/decimal.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -316,17 +316,34 @@ decimal_scalar_pack!(i256, i256, I256);
316316

317317
#[macro_export]
318318
macro_rules! match_each_decimal_value {
319-
($self:expr, | $_:tt $value:ident | $($body:tt)*) => ({
320-
macro_rules! __with__ {( $_ $value:ident ) => ( $($body)* )}
319+
($self:expr, | $value:ident | $body:block) => {{
321320
match $self {
322-
DecimalValue::I8(v) => __with__! { v },
323-
DecimalValue::I16(v) => __with__! { v },
324-
DecimalValue::I32(v) => __with__! { v },
325-
DecimalValue::I64(v) => __with__! { v },
326-
DecimalValue::I128(v) => __with__! { v },
327-
DecimalValue::I256(v) => __with__! { v },
321+
DecimalValue::I8(v) => {
322+
let $value = v;
323+
$body
324+
}
325+
DecimalValue::I16(v) => {
326+
let $value = v;
327+
$body
328+
}
329+
DecimalValue::I32(v) => {
330+
let $value = v;
331+
$body
332+
}
333+
DecimalValue::I64(v) => {
334+
let $value = v;
335+
$body
336+
}
337+
DecimalValue::I128(v) => {
338+
let $value = v;
339+
$body
340+
}
341+
DecimalValue::I256(v) => {
342+
let $value = v;
343+
$body
344+
}
328345
}
329-
});
346+
}};
330347
}
331348

332349
/// Macro to match over each decimal value type, binding the corresponding native type (from `DecimalValueType`)

0 commit comments

Comments
 (0)