From 097f1d929074a04fb80929048dea6688e646b5d6 Mon Sep 17 00:00:00 2001 From: Serag Ahmed Date: Mon, 30 Nov 2026 04:40:08 +0300 Subject: [PATCH] Reject decimal values that violate their dtype in DecimalScalar::try_new DecimalScalar::try_new accepted values that did not fit the precision, nulls for a non-nullable dtype, and panicked on non-decimal values. The infallible From for Scalar then built the Scalar with new_unchecked, assuming the view was valid. try_new now runs Scalar::validate, so invalid inputs are rejected at construction. Closes #11957 Signed-off-by: Serag Ahmed --- .../src/scalar/typed_view/decimal/scalar.rs | 5 ++++- .../src/scalar/typed_view/decimal/tests.rs | 22 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/vortex-array/src/scalar/typed_view/decimal/scalar.rs b/vortex-array/src/scalar/typed_view/decimal/scalar.rs index 2c1c88319e0..619e638e6cb 100644 --- a/vortex-array/src/scalar/typed_view/decimal/scalar.rs +++ b/vortex-array/src/scalar/typed_view/decimal/scalar.rs @@ -39,9 +39,12 @@ impl<'a> DecimalScalar<'a> { /// /// # Errors /// - /// Returns an error if the data type is not a decimal type. + /// Returns an error if the data type is not a decimal type, or if the value is not valid for + /// it: a null value for a non-nullable type, a non-decimal value, or a value that does not + /// fit the precision. pub fn try_new(dtype: &'a DType, value: Option<&ScalarValue>) -> VortexResult { let decimal_type = DecimalDType::try_from(dtype)?; + Scalar::validate(dtype, value)?; let value = value.map(|v| *v.as_decimal()); Ok(Self { diff --git a/vortex-array/src/scalar/typed_view/decimal/tests.rs b/vortex-array/src/scalar/typed_view/decimal/tests.rs index 2600256688e..4feaa8c978e 100644 --- a/vortex-array/src/scalar/typed_view/decimal/tests.rs +++ b/vortex-array/src/scalar/typed_view/decimal/tests.rs @@ -17,9 +17,11 @@ use crate::dtype::NativeDecimalType; use crate::dtype::Nullability; use crate::dtype::PType; use crate::dtype::i256; +use crate::scalar::DecimalScalar; use crate::scalar::DecimalValue; use crate::scalar::NumericOperator; use crate::scalar::Scalar; +use crate::scalar::ScalarValue; use crate::scalar::decimal_numeric_result_dtype; #[rstest] @@ -1380,6 +1382,26 @@ fn test_fits_in_precision_different_scales() { assert!(!value.fits_in_precision(DecimalDType::new(4, 2))); } +#[rstest] +#[case::exceeds_precision( + DType::Decimal(DecimalDType::new(2, 0), Nullability::NonNullable), + Some(ScalarValue::Decimal(DecimalValue::I8(100))) +)] +#[case::null_for_non_nullable( + DType::Decimal(DecimalDType::new(2, 0), Nullability::NonNullable), + None +)] +#[case::non_decimal_value( + DType::Decimal(DecimalDType::new(2, 0), Nullability::Nullable), + Some(ScalarValue::Bool(true)) +)] +fn decimal_scalar_try_new_rejects_values_invalid_for_dtype( + #[case] dtype: DType, + #[case] value: Option, +) { + assert!(DecimalScalar::try_new(&dtype, value.as_ref()).is_err()); +} + #[test] fn test_fits_in_precision_negative_values() { use crate::dtype::DecimalDType;