Skip to content

Commit 8f91b9e

Browse files
committed
address comment
1 parent 084872b commit 8f91b9e

File tree

2 files changed

+35
-41
lines changed

2 files changed

+35
-41
lines changed

parquet-variant-compute/src/type_conversion.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -256,16 +256,12 @@ where
256256
),
257257
Variant::Float(f) => single_float_to_decimal::<O>(f64::from(*f), mul),
258258
Variant::Double(f) => single_float_to_decimal::<O>(*f, mul),
259-
Variant::String(v) if scale > 0 => parse_string_to_decimal_native::<O>(
260-
v,
261-
<i8 as TryInto<usize>>::try_into(scale).expect("scale is positive, would never fail"),
262-
)
263-
.ok(),
264-
Variant::ShortString(v) if scale > 0 => parse_string_to_decimal_native::<O>(
265-
v,
266-
<i8 as TryInto<usize>>::try_into(scale).expect("scale is positive, would never fail"),
267-
)
268-
.ok(),
259+
// arrow-cast only support cast string to decimal with scale >=0 for now
260+
// Please see `cast_string_to_decimal` in arrow-cast/src/cast/decimal.rs for more detail
261+
Variant::String(v) if scale >= 0 => parse_string_to_decimal_native::<O>(v, scale as _).ok(),
262+
Variant::ShortString(v) if scale >= 0 => {
263+
parse_string_to_decimal_native::<O>(v, scale as _).ok()
264+
}
269265
Variant::Decimal4(d) => rescale_decimal::<Decimal32Type, O>(
270266
d.integer(),
271267
VariantDecimal4::MAX_PRECISION,

parquet-variant/src/variant.rs

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -309,16 +309,14 @@ enum NumericKind {
309309

310310
trait DecimalCastTarget: NumCast + Default {
311311
const KIND: NumericKind;
312-
fn arrow_type() -> DataType;
312+
const ARROW_TYPE: DataType;
313313
}
314314

315315
macro_rules! impl_decimal_cast_target {
316316
($raw_type: ident, $target_kind:expr, $arrow_type: expr) => {
317317
impl DecimalCastTarget for $raw_type {
318318
const KIND: NumericKind = $target_kind;
319-
fn arrow_type() -> DataType {
320-
$arrow_type
321-
}
319+
const ARROW_TYPE: DataType = $arrow_type;
322320
}
323321
};
324322
}
@@ -847,22 +845,21 @@ impl<'m, 'v> Variant<'m, 'v> {
847845
{
848846
let base: D::Native = NumCast::from(10)?;
849847

850-
base.pow_checked(<u32 as From<u8>>::from(scale))
851-
.ok()
852-
.and_then(|div| match T::KIND {
853-
NumericKind::Integer => cast_single_decimal_to_integer::<D, T>(
854-
raw,
855-
div,
856-
<i16 as From<u8>>::from(scale),
857-
T::arrow_type(),
858-
)
859-
.ok(),
860-
NumericKind::Float => T::from(single_decimal_to_float_lossy::<D, _>(
861-
&as_float,
862-
raw,
863-
<i32 as From<u8>>::from(scale),
864-
)),
865-
})
848+
let div = base.pow_checked(<u32 as From<u8>>::from(scale)).ok()?;
849+
match T::KIND {
850+
NumericKind::Integer => cast_single_decimal_to_integer::<D, T>(
851+
raw,
852+
div,
853+
<i16 as From<u8>>::from(scale),
854+
T::ARROW_TYPE,
855+
)
856+
.ok(),
857+
NumericKind::Float => T::from(single_decimal_to_float_lossy::<D, _>(
858+
&as_float,
859+
raw,
860+
<i32 as From<u8>>::from(scale),
861+
)),
862+
}
866863
}
867864

868865
/// Converts a boolean or numeric variant(integers, floating-point, and decimals)
@@ -1193,9 +1190,7 @@ impl<'m, 'v> Variant<'m, 'v> {
11931190
D::Native: NumCast + DecimalCast,
11941191
{
11951192
// find the last '.'
1196-
let scale_usize = input
1197-
.rsplit_once('.')
1198-
.map_or_else(|| 0, |(_, frac)| frac.len());
1193+
let scale_usize = input.rsplit_once('.').map_or(0, |(_, frac)| frac.len());
11991194

12001195
let scale = u8::try_from(scale_usize).ok()?;
12011196

@@ -1330,14 +1325,17 @@ impl<'m, 'v> Variant<'m, 'v> {
13301325
/// ```
13311326
pub fn as_decimal16(&self) -> Option<VariantDecimal16> {
13321327
match *self {
1333-
Variant::Int8(_) | Variant::Int16(_) | Variant::Int32(_) | Variant::Int64(_) => self
1334-
.as_num::<i64>()
1335-
.map(|x| <i128 as From<i64>>::from(x).try_into().ok())
1336-
.unwrap(),
1337-
Variant::Float(f) => single_float_to_decimal::<Decimal128Type>(f as _, 1f64)
1338-
.and_then(|x: i128| x.try_into().ok()),
1339-
Variant::Double(f) => single_float_to_decimal::<Decimal128Type>(f, 1f64)
1340-
.and_then(|x: i128| x.try_into().ok()),
1328+
Variant::Int8(_) | Variant::Int16(_) | Variant::Int32(_) | Variant::Int64(_) => {
1329+
let x = self.as_num::<i64>()?;
1330+
<i128 as From<i64>>::from(x).try_into().ok()
1331+
}
1332+
Variant::Float(f) => {
1333+
single_float_to_decimal::<Decimal128Type>(<f64 as From<f32>>::from(f), 1f64)
1334+
.and_then(|x| x.try_into().ok())
1335+
}
1336+
Variant::Double(f) => {
1337+
single_float_to_decimal::<Decimal128Type>(f, 1f64).and_then(|x| x.try_into().ok())
1338+
}
13411339
Variant::String(v) => Self::convert_string_to_decimal::<Decimal128Type, _>(v),
13421340
Variant::ShortString(v) => {
13431341
Self::convert_string_to_decimal::<Decimal128Type, _>(v.as_str())

0 commit comments

Comments
 (0)