From 2b98bca9caaa683a101cde305bc960645504c09c Mon Sep 17 00:00:00 2001 From: Edd Robinson Date: Fri, 14 May 2021 18:39:25 +0100 Subject: [PATCH] test: allow from slice to be testable --- .../src/column/encoding/scalar/fixed.rs | 2 +- .../src/column/encoding/scalar/fixed_null.rs | 2 +- read_buffer/src/column/integer.rs | 78 ++++++++++++++++++- 3 files changed, 76 insertions(+), 6 deletions(-) diff --git a/read_buffer/src/column/encoding/scalar/fixed.rs b/read_buffer/src/column/encoding/scalar/fixed.rs index 7305769ea4..64b65169e9 100644 --- a/read_buffer/src/column/encoding/scalar/fixed.rs +++ b/read_buffer/src/column/encoding/scalar/fixed.rs @@ -21,7 +21,7 @@ use arrow::array::Array; use crate::column::{cmp, RowIDs}; -#[derive(Debug, Default)] +#[derive(Debug, Default, PartialEq, PartialOrd)] /// A Fixed encoding is one in which every value has a fixed width, and is /// stored contiguous in a backing vector. Fixed encodings do not support NULL /// values, so are suitable for columns known to not have NULL values that we diff --git a/read_buffer/src/column/encoding/scalar/fixed_null.rs b/read_buffer/src/column/encoding/scalar/fixed_null.rs index c0c64c610b..0b1b92d23f 100644 --- a/read_buffer/src/column/encoding/scalar/fixed_null.rs +++ b/read_buffer/src/column/encoding/scalar/fixed_null.rs @@ -23,7 +23,7 @@ use arrow::{ use crate::column::{cmp, RowIDs}; -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub struct FixedNull where T: ArrowNumericType, diff --git a/read_buffer/src/column/integer.rs b/read_buffer/src/column/integer.rs index aaf03dbc85..23940aeed5 100644 --- a/read_buffer/src/column/integer.rs +++ b/read_buffer/src/column/integer.rs @@ -25,6 +25,35 @@ pub enum IntegerEncoding { U64U64N(FixedNull), } +impl PartialEq for IntegerEncoding { + fn eq(&self, other: &Self) -> bool { + match (self, other) { + (Self::I64I64(a), Self::I64I64(b)) => a == b, + (Self::I64I32(a), Self::I64I32(b)) => a == b, + (Self::I64U32(a), Self::I64U32(b)) => a == b, + (Self::I64I16(a), Self::I64I16(b)) => a == b, + (Self::I64U16(a), Self::I64U16(b)) => a == b, + (Self::I64I8(a), Self::I64I8(b)) => a == b, + (Self::I64U8(a), Self::I64U8(b)) => a == b, + (Self::U64U64(a), Self::U64U64(b)) => a == b, + (Self::U64U32(a), Self::U64U32(b)) => a == b, + (Self::U64U16(a), Self::U64U16(b)) => a == b, + (Self::U64U8(a), Self::U64U8(b)) => a == b, + (Self::I64I64N(a), Self::I64I64N(b)) => { + let a = a.all_values(vec![]); + let b = b.all_values(vec![]); + a == b + } + (Self::U64U64N(a), Self::U64U64N(b)) => { + let a = a.all_values(vec![]); + let b = b.all_values(vec![]); + a == b + } + (_, _) => false, + } + } +} + impl IntegerEncoding { /// The total size in bytes of the store columnar data. pub fn size(&self) -> usize { @@ -504,6 +533,27 @@ impl std::fmt::Display for IntegerEncoding { } } +impl std::fmt::Debug for IntegerEncoding { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let name = self.name(); + match self { + Self::I64I64(enc) => enc.fmt(f), + Self::I64I32(enc) => write!(f, "[{}]: {:?}", name, enc), + Self::I64U32(enc) => write!(f, "[{}]: {:?}", name, enc), + Self::I64I16(enc) => write!(f, "[{}]: {:?}", name, enc), + Self::I64U16(enc) => write!(f, "[{}]: {:?}", name, enc), + Self::I64I8(enc) => write!(f, "[{}]: {:?}", name, enc), + Self::I64U8(enc) => write!(f, "[{}]: {:?}", name, enc), + Self::U64U64(enc) => write!(f, "[{}]: {:?}", name, enc), + Self::U64U32(enc) => write!(f, "[{}]: {:?}", name, enc), + Self::U64U16(enc) => write!(f, "[{}]: {:?}", name, enc), + Self::U64U8(enc) => write!(f, "[{}]: {:?}", name, enc), + Self::I64I64N(enc) => write!(f, "[{}]: {:?}", name, enc), + Self::U64U64N(enc) => write!(f, "[{}]: {:?}", name, enc), + } + } +} + /// Converts a slice of i64 values into an IntegerEncoding. /// /// The most compact physical type needed to store the columnar values is @@ -628,7 +678,7 @@ mod test { vec![399_i64, 2, 2452, 3], vec![-399_i64, 2, 2452, 3], vec![u32::MAX as i64, 2, 245, 3], - vec![i32::MAX as i64, 2, 245, 3], + vec![i32::MIN as i64, 2, 245, 3], vec![0_i64, 2, 245, u32::MAX as i64 + 1], ]; @@ -642,9 +692,29 @@ mod test { IntegerEncoding::I64I64(Fixed::::from(cases[6].as_slice())), ]; - for (_case, _exp) in cases.iter().zip(exp.iter()) { - // TODO - add debug - //assert_eq!(IntegerEncoding::from(&case), exp); + for (case, exp) in cases.into_iter().zip(exp.into_iter()) { + assert_eq!(IntegerEncoding::from(case.as_slice()), exp); + } + } + + #[test] + fn from_slice_u64() { + let cases = vec![ + vec![0_u64, 2, 245, 3], + vec![399_u64, 2, 2452, 3], + vec![u32::MAX as u64, 2, 245, 3], + vec![0_u64, 2, 245, u32::MAX as u64 + 1], + ]; + + let exp = vec![ + IntegerEncoding::U64U8(Fixed::::from(cases[0].as_slice())), + IntegerEncoding::U64U16(Fixed::::from(cases[1].as_slice())), + IntegerEncoding::U64U32(Fixed::::from(cases[2].as_slice())), + IntegerEncoding::U64U64(Fixed::::from(cases[3].as_slice())), + ]; + + for (case, exp) in cases.into_iter().zip(exp.into_iter()) { + assert_eq!(IntegerEncoding::from(case.as_slice()), exp); } }