klion26 commented on code in PR #9563:
URL: https://github.com/apache/arrow-rs/pull/9563#discussion_r2999430580
##########
parquet-variant/src/variant.rs:
##########
@@ -760,10 +779,34 @@ impl<'m, 'v> Variant<'m, 'v> {
}
}
+ /// Converts a boolean or numeric variant to the specified numeric type
`T`.
+ ///
+ /// Uses Arrow's casting logic to perform the conversion. Returns
`Some(T)` if
+ /// the conversion succeeds, `None` if the variant can't be casted to type
`T`.
+ fn as_num<T>(&self) -> Option<T>
+ where
+ T: NumCast + Default,
+ {
+ match *self {
+ Variant::BooleanFalse => single_bool_to_numeric::<T>(false),
+ Variant::BooleanTrue => single_bool_to_numeric::<T>(true),
+ Variant::Int8(i) => num_cast::<_, T>(i),
+ Variant::Int16(i) => num_cast::<_, T>(i),
+ Variant::Int32(i) => num_cast::<_, T>(i),
+ Variant::Int64(i) => num_cast::<_, T>(i),
+ Variant::Float(f) => num_cast::<_, T>(f),
+ Variant::Double(d) => num_cast::<_, T>(d),
+ Variant::Decimal4(d) if d.scale() == 0 => num_cast::<_,
T>(d.integer()),
+ Variant::Decimal8(d) if d.scale() == 0 => num_cast::<_,
T>(d.integer()),
+ Variant::Decimal16(d) if d.scale() == 0 => num_cast::<_,
T>(d.integer()),
+ _ => None,
+ }
+ }
+
/// Converts this variant to an `i8` if possible.
///
- /// Returns `Some(i8)` for integer variants that fit in `i8` range,
- /// `None` for non-integer variants or values that would overflow.
+ /// Returns `Some(i8)` for integer variants that fit in `i8` range and
boolean variant,
Review Comment:
fixed
##########
arrow-cast/src/cast/mod.rs:
##########
@@ -2589,6 +2605,20 @@ where
unsafe { PrimitiveArray::<T>::from_trusted_len_iter(iter) }
}
+/// Cat single bool value to numeric value.
Review Comment:
done
##########
parquet-variant-compute/src/variant_get.rs:
##########
@@ -2518,7 +2647,7 @@ mod test {
#[test]
fn test_error_message_numeric_type_display() {
let mut builder = VariantArrayBuilder::new(1);
- builder.append_variant(Variant::BooleanTrue);
+ builder.append_variant(Variant::Null);
Review Comment:
Changed to other variants
##########
parquet-variant/src/variant.rs:
##########
@@ -931,27 +938,31 @@ impl<'m, 'v> Variant<'m, 'v> {
/// let v2 = Variant::from(d);
/// assert_eq!(v2.as_u8(), Some(26u8));
///
+ /// // or from boolean variant
+ /// let v3 = Variant::BooleanFalse;
+ /// assert_eq!(v3.as_int8(), Some(0));
Review Comment:
ah, thanks for point this out, fixed
##########
parquet-variant-compute/src/variant_get.rs:
##########
Review Comment:
done
##########
parquet-variant-compute/src/variant_get.rs:
##########
@@ -2518,7 +2647,7 @@ mod test {
#[test]
fn test_error_message_numeric_type_display() {
let mut builder = VariantArrayBuilder::new(1);
- builder.append_variant(Variant::BooleanTrue);
+ builder.append_variant(Variant::Null);
let variant_array: ArrayRef = ArrayRef::from(builder.build());
// Request Boolean with strict casting to force an error
Review Comment:
done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]