From aea181ce9896f6e14dd7a64dd52933dc0c914601 Mon Sep 17 00:00:00 2001 From: Serag Ahmed Date: Mon, 30 Nov 2026 04:32:06 +0300 Subject: [PATCH] Hash DecimalScalars consistently with their equality DecimalScalar derived Hash over its full dtype, including nullability, while PartialEq ignores nullability. Two decimal scalars that differ only in nullability compared equal but could hash differently, breaking the Eq/Hash contract. Hash the dtype with hash_ignore_nullability, as ListScalar and ExtScalar already do. Closes #11958 Signed-off-by: Serag Ahmed --- .../src/scalar/typed_view/decimal/scalar.rs | 11 +++++++++- .../src/scalar/typed_view/decimal/tests.rs | 20 +++++++++++++++++++ 2 files changed, 30 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..d12b6ebc647 100644 --- a/vortex-array/src/scalar/typed_view/decimal/scalar.rs +++ b/vortex-array/src/scalar/typed_view/decimal/scalar.rs @@ -5,6 +5,8 @@ use std::cmp::Ordering; use std::fmt; +use std::hash::Hash; +use std::hash::Hasher; use vortex_error::VortexResult; use vortex_error::vortex_bail; @@ -24,7 +26,7 @@ use crate::scalar::Scalar; use crate::scalar::ScalarValue; /// A scalar value representing a decimal number with fixed precision and scale. -#[derive(Debug, Clone, Copy, Hash)] +#[derive(Debug, Clone, Copy)] pub struct DecimalScalar<'a> { /// The data type of this scalar. dtype: &'a DType, @@ -193,6 +195,13 @@ impl PartialEq for DecimalScalar<'_> { impl Eq for DecimalScalar<'_> {} +impl Hash for DecimalScalar<'_> { + fn hash(&self, state: &mut H) { + self.dtype.hash_ignore_nullability(state); + self.decimal_value.hash(state); + } +} + /// Ord is not implemented since it's undefined for different PTypes impl PartialOrd for DecimalScalar<'_> { fn partial_cmp(&self, other: &Self) -> Option { diff --git a/vortex-array/src/scalar/typed_view/decimal/tests.rs b/vortex-array/src/scalar/typed_view/decimal/tests.rs index 2600256688e..6ce0c9f144d 100644 --- a/vortex-array/src/scalar/typed_view/decimal/tests.rs +++ b/vortex-array/src/scalar/typed_view/decimal/tests.rs @@ -50,6 +50,26 @@ fn test_hash() { assert_eq!(set.len(), 1); } +#[test] +fn decimal_scalars_differing_only_in_nullability_hash_equal() { + let decimal_dtype = DecimalDType::new(5, 2); + let non_nullable = Scalar::decimal( + DecimalValue::I32(12345), + decimal_dtype, + Nullability::NonNullable, + ); + let nullable = Scalar::decimal( + DecimalValue::I32(12345), + decimal_dtype, + Nullability::Nullable, + ); + let (non_nullable, nullable) = (non_nullable.as_decimal(), nullable.as_decimal()); + assert_eq!(non_nullable, nullable); + + let set: HashSet<_> = [non_nullable, nullable].into_iter().collect(); + assert_eq!(set.len(), 1); +} + #[test] fn test_decimal_cast_to_primitive() { // Create a decimal with value 123.45 (scale=2, so stored as 12345)