Skip to content

Commit

Permalink
feat: implemented for Decimal type DataValue::to_index_key
Browse files Browse the repository at this point in the history
  • Loading branch information
KKould committed Sep 25, 2023
1 parent 8a711c2 commit f7a205d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 12 deletions.
4 changes: 1 addition & 3 deletions src/storage/table_codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,11 @@ mod tests {
"c1".into(),
false,
ColumnDesc::new(LogicalType::Integer, true, false)
)
ColumnDesc::new(LogicalType::Integer, true, false)
),
ColumnCatalog::new(
"c2".into(),
false,
ColumnDesc::new(LogicalType::Decimal(None,None), false)
ColumnDesc::new(LogicalType::Decimal(None,None), false, false)
),
];
let table_catalog = TableCatalog::new(Arc::new("t1".to_string()), columns, vec![]).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/types/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub enum TypeError {
#[source]
#[from]
Box<bincode::ErrorKind>
)
),
#[error("try from decimal")]
TryFromDecimal(
#[source]
Expand Down
3 changes: 1 addition & 2 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ pub mod index;

use serde::{Deserialize, Serialize};

use integer_encoding::FixedInt;
use sqlparser::ast::ExactNumberInfo;
use strum_macros::AsRefStr;

Expand Down Expand Up @@ -53,7 +52,7 @@ impl LogicalType {
LogicalType::UBigint => Some(8),
LogicalType::Float => Some(4),
LogicalType::Double => Some(8),
/// Note: The non-fixed length type's raw_len is None e.g. Varchar and Decimal
/// Note: The non-fixed length type's raw_len is None e.g. Varchar
LogicalType::Varchar(_) => None,
LogicalType::Decimal(_, _) => Some(16),
LogicalType::Date => Some(4),
Expand Down
50 changes: 44 additions & 6 deletions src/types/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,16 @@ impl DataValue {
}
(LogicalType::Decimal(full_len, scale_len), DataValue::Decimal(Some(val))) => {
if let Some(len) = full_len {
val.mantissa().ilog10() + 1 > *len as u32
} else if let Some(len) = scale_len {
val.scale() > *len as u32
} else {
false
if val.mantissa().ilog10() + 1 > *len as u32 {
return Err(TypeError::TooLong)
}
}
if let Some(len) = scale_len {
if val.scale() > *len as u32 {
return Err(TypeError::TooLong)
}
}
false
}
_ => false
};
Expand Down Expand Up @@ -414,6 +418,16 @@ impl DataValue {
DataValue::Boolean(option) => option.map(|b| if b { "1" } else { "0" }.to_string()),
DataValue::Float32(option) => option.map(|v| format!("{:0width$}", (unsafe { mem::transmute::<u32, i32>(v.to_bits()) }), width = 11)),
DataValue::Float64(option) => option.map(|v| format!("{:0width$}", (unsafe { mem::transmute::<u64, i64>(v.to_bits()) }), width = 20)),
DataValue::Decimal(option) => option.map(|v| {
let i = signed_to_primary_key!(i128, v.mantissa());
let scale = v.scale();
let mut string = format!("{:0width$}", i, width = 40);

if scale != 0 {
string.insert(40 - scale as usize, '.');
}
string
}),
_ => return Err(TypeError::InvalidType),
}.ok_or(TypeError::NotNull)
}
Expand Down Expand Up @@ -886,6 +900,7 @@ impl fmt::Debug for DataValue {

#[cfg(test)]
mod test {
use rust_decimal::Decimal;
use crate::types::errors::TypeError;
use crate::types::value::DataValue;

Expand Down Expand Up @@ -931,7 +946,7 @@ mod test {
}

#[test]
fn test_to_index_key() -> Result<(), TypeError> {
fn test_to_index_key_f() -> Result<(), TypeError> {
let key_f32_1 = DataValue::Float32(Some(f32::MIN)).to_index_key()?;
let key_f32_2 = DataValue::Float32(Some(-1_f32)).to_index_key()?;
let key_f32_3 = DataValue::Float32(Some(f32::MAX)).to_index_key()?;
Expand All @@ -952,4 +967,27 @@ mod test {

Ok(())
}

#[test]
fn test_to_index_key_d() -> Result<(), TypeError> {
let key_scale_0_1 = DataValue::Decimal(Some(Decimal::new(i64::MIN, 0))).to_index_key()?;
let key_scale_0_2 = DataValue::Decimal(Some(Decimal::new(-1_i64, 0))).to_index_key()?;
let key_scale_0_3 = DataValue::Decimal(Some(Decimal::new(i64::MAX, 0))).to_index_key()?;

println!("{} < {}", key_scale_0_1, key_scale_0_2);
println!("{} < {}", key_scale_0_2, key_scale_0_3);
assert!(key_scale_0_1 < key_scale_0_2);
assert!(key_scale_0_2 < key_scale_0_3);

let key_scale_10_1 = DataValue::Decimal(Some(Decimal::new(i64::MIN, 10))).to_index_key()?;
let key_scale_10_2 = DataValue::Decimal(Some(Decimal::new(-1_i64, 10))).to_index_key()?;
let key_scale_10_3 = DataValue::Decimal(Some(Decimal::new(i64::MAX, 10))).to_index_key()?;

println!("{} < {}", key_scale_10_1, key_scale_10_2);
println!("{} < {}", key_scale_10_2, key_scale_10_3);
assert!(key_scale_10_1 < key_scale_10_2);
assert!(key_scale_10_2 < key_scale_10_3);

Ok(())
}
}

0 comments on commit f7a205d

Please sign in to comment.