use super::*;
use datafusion::arrow::datatypes::i256;
use datafusion::common::ScalarValue;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
#[test]
fn null_admission_returns_none() {
for scalar in [ScalarValue::Int64(None), ScalarValue::Float64(None)] {
assert_eq!(AggregateValue::from_scalar(&scalar), None);
}
}
#[test]
fn from_scalar_maps_supported_variants() {
use AggregateValue as A;
use ScalarValue as S;
for (scalar, aggregate) in [
(S::Int8(Some(42)), A::Int8(42)),
(S::Int16(Some(1000)), A::Int16(1000)),
(S::Int32(Some(100000)), A::Int32(100000)),
(S::Int64(Some(i64::MAX)), A::Int64(i64::MAX)),
(S::Int64(Some(1000)), A::Int64(1000)),
(S::UInt8(Some(u8::MAX)), A::UInt8(u8::MAX)),
(S::UInt16(Some(u16::MAX)), A::UInt16(u16::MAX)),
(S::UInt32(Some(u32::MAX)), A::UInt32(u32::MAX)),
(S::UInt64(Some(u64::MAX)), A::UInt64(u64::MAX)),
(S::Float32(Some(3.125)), A::Float32(3.125)),
(S::Float64(Some(2.5)), A::Float64(2.5)),
(S::Float64(Some(3.125)), A::Float64(3.125)),
(S::Boolean(Some(true)), A::Boolean(true)),
(
S::Decimal128(Some(12345), 10, 2),
A::Decimal128(12345, 10, 2),
),
(
S::Decimal256(Some(i256::from_i128(12345)), 50, 2),
A::Decimal256(i256::from_i128(12345), 50, 2),
),
] {
assert_eq!(AggregateValue::from_scalar(&scalar), Some(aggregate));
}
}
#[test]
fn test_hash_consistency() {
fn hash_value(v: &AggregateValue) -> u64 {
let mut hasher = DefaultHasher::new();
v.hash(&mut hasher);
hasher.finish()
}
for (left, right) in [
(AggregateValue::Int64(42), AggregateValue::Int64(42)),
(AggregateValue::Float32(0.0), AggregateValue::Float32(-0.0)),
(AggregateValue::Float64(0.0), AggregateValue::Float64(-0.0)),
] {
assert_eq!(left, right);
assert_eq!(hash_value(&left), hash_value(&right));
}
}