Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion vortex-array/src/scalar/typed_view/decimal/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
let decimal_type = DecimalDType::try_from(dtype)?;
Scalar::validate(dtype, value)?;
let value = value.map(|v| *v.as_decimal());

Ok(Self {
Expand Down
22 changes: 22 additions & 0 deletions vortex-array/src/scalar/typed_view/decimal/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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<ScalarValue>,
) {
assert!(DecimalScalar::try_new(&dtype, value.as_ref()).is_err());
}

#[test]
fn test_fits_in_precision_negative_values() {
use crate::dtype::DecimalDType;
Expand Down