use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
use crate::decimal::Decimal;
use crate::value::Value;
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RowKey(Vec<Part>);
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
enum Part {
Boolean(bool),
Number(Number),
Float(Float),
Text(String),
Bytes(Vec<u8>),
Date(NaiveDate),
Time(NaiveTime),
Timestamp(NaiveDateTime),
Undecoded { domain: u16, bytes: Vec<u8> },
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
enum Number {
Least,
Other(Decimal),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct Float(u64);
impl RowKey {
#[must_use]
pub fn new<'value>(values: impl IntoIterator<Item = &'value Value>) -> Option<Self> {
let parts = values.into_iter().map(part);
parts.collect::<Option<_>>().map(Self)
}
}
fn part(value: &Value) -> Option<Part> {
Some(match value {
Value::Null => return None,
Value::Boolean(boolean) => Part::Boolean(*boolean),
Value::Integer(integer) => Part::Number(match Decimal::new(*integer, 0) {
Some(decimal) => Number::Other(decimal),
None => Number::Least,
}),
Value::Decimal(decimal) => Part::Number(Number::Other(*decimal)),
Value::Float(float) => Part::Float(Float::new((*float).into())),
Value::Double(double) => Part::Float(Float::new(*double)),
Value::Text(text) => Part::Text(text.clone()),
Value::Bytes(bytes) => Part::Bytes(bytes.clone()),
Value::Date(date) => Part::Date(*date),
Value::Time(time) => Part::Time(*time),
Value::Timestamp(timestamp) => Part::Timestamp(*timestamp),
Value::Undecoded(undecoded) => Part::Undecoded {
domain: undecoded.domain(),
bytes: undecoded.bytes().to_vec(),
},
})
}
impl Float {
fn new(float: f64) -> Self {
const SIGN: u64 = 1 << 63;
let one_of_each = if float == 0.0 {
0.0
} else if float.is_nan() {
f64::NAN
} else {
float
};
let bits = one_of_each.to_bits();
Self(if bits & SIGN == 0 { bits | SIGN } else { !bits })
}
}
#[cfg(test)]
mod tests {
use std::collections::HashSet;
use super::*;
use crate::value::Undecoded;
fn key(value: impl Into<Value>) -> Option<RowKey> {
RowKey::new(&[value.into()])
}
fn decimal(text: &str) -> Value {
Value::Decimal(text.parse().unwrap())
}
#[test]
fn equal_numbers_make_equal_keys() {
assert_eq!(key(decimal("1.50")), key(decimal("1.5")));
assert_eq!(key(7), key(decimal("7.00")));
assert_ne!(key(7), key(decimal("7.01")));
assert_eq!(key(0.5_f32), key(0.5));
assert_eq!(key(-0.0), key(0.0));
assert_eq!(key(f64::NAN), key(-f64::NAN));
assert_ne!(key(7), key(7.0));
assert_ne!(key(7), key("7"));
assert_ne!(key(i128::MIN), key(i128::MIN + 1));
let same: HashSet<_> = [key(7), key(decimal("7.0")), key(7_u8)].into();
assert_eq!(same.len(), 1);
}
#[test]
fn a_key_with_null_is_none() {
assert_eq!(key(Value::Null), None);
assert_eq!(RowKey::new(&[Value::from(1), Value::Null]), None);
let none: [&Value; 0] = [];
assert_eq!(RowKey::new(none), Some(RowKey(Vec::new())));
}
#[test]
fn keys_sort_as_their_values_do() {
let sorted = [f64::NEG_INFINITY, -2.5, -0.5, 0.0, 0.5, 2.5, f64::INFINITY];
let keys = sorted.map(key);
assert!(keys.is_sorted());
let numbers = [key(i128::MIN), key(-3), key(decimal("-2.5")), key(4)];
assert!(numbers.is_sorted());
assert!(key("a") < key("b") && key(vec![1_u8]) < key(vec![1_u8, 0]));
}
#[test]
fn every_other_value_has_a_key_of_its_own() {
let day = NaiveDate::from_ymd_opt(2024, 2, 29).unwrap();
let values = [
Value::from(true),
day.into(),
NaiveTime::MIN.into(),
day.and_time(NaiveTime::MIN).into(),
Value::Undecoded(Undecoded::new(36, vec![1])),
Value::Undecoded(Undecoded::new(37, vec![1])),
];
let keys: HashSet<_> = values.iter().map(|value| key(value.clone())).collect();
assert_eq!(keys.len(), values.len());
}
}