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
11 changes: 10 additions & 1 deletion vortex-array/src/scalar/typed_view/decimal/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -193,6 +195,13 @@ impl PartialEq for DecimalScalar<'_> {

impl Eq for DecimalScalar<'_> {}

impl Hash for DecimalScalar<'_> {
fn hash<H: Hasher>(&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<Ordering> {
Expand Down
20 changes: 20 additions & 0 deletions vortex-array/src/scalar/typed_view/decimal/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down