Skip to content

Commit

Permalink
fix: memcomparable for DataValue::Tuple (#189)
Browse files Browse the repository at this point in the history
* fix: memcomparable for `DataValue::Tuple`

* docs: fix `NonUniqueIndex` comment

* style: code fmt
  • Loading branch information
KKould authored Mar 30, 2024
1 parent 481e63a commit 173c395
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/storage/table_codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ impl TableCodec {
}

/// NonUnique Index:
/// Key: {TableName}{INDEX_TAG}{BOUND_MIN_TAG}{IndexID}{BOUND_MIN_TAG}{DataValue1}{DataValue2} .. {TupleId}
/// Key: {TableName}{INDEX_TAG}{BOUND_MIN_TAG}{IndexID}{BOUND_MIN_TAG}{DataValue1}{BOUND_MIN_TAG}{DataValue2} .. {TupleId}
/// Value: TupleID
///
/// Unique Index:
Expand Down
4 changes: 3 additions & 1 deletion src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub mod tuple;
pub mod tuple_builder;
pub mod value;

use chrono::{NaiveDate, NaiveDateTime};
use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use std::any::TypeId;
Expand Down Expand Up @@ -72,6 +72,8 @@ impl LogicalType {
Some(LogicalType::Date)
} else if type_id == TypeId::of::<NaiveDateTime>() {
Some(LogicalType::DateTime)
} else if type_id == TypeId::of::<NaiveTime>() {
Some(LogicalType::Time)
} else if type_id == TypeId::of::<Decimal>() {
Some(LogicalType::Decimal(None, None))
} else if type_id == TypeId::of::<String>() {
Expand Down
35 changes: 35 additions & 0 deletions src/types/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,7 @@ impl DataValue {
DataValue::Tuple(Some(values)) => {
for v in values.iter() {
v.memcomparable_encode(b)?;
b.push(0u8);
}
}
value => {
Expand Down Expand Up @@ -1573,6 +1574,7 @@ impl fmt::Debug for DataValue {
mod test {
use crate::errors::DatabaseError;
use crate::types::value::DataValue;
use std::sync::Arc;

#[test]
fn test_mem_comparable_int() -> Result<(), DatabaseError> {
Expand Down Expand Up @@ -1661,4 +1663,37 @@ mod test {

Ok(())
}

#[test]
fn test_mem_comparable_tuple() -> Result<(), DatabaseError> {
let mut key_tuple_1 = Vec::new();
let mut key_tuple_2 = Vec::new();
let mut key_tuple_3 = Vec::new();

DataValue::Tuple(Some(vec![
Arc::new(DataValue::Int8(None)),
Arc::new(DataValue::Int8(Some(0))),
Arc::new(DataValue::Int8(Some(1))),
]))
.memcomparable_encode(&mut key_tuple_1)?;
DataValue::Tuple(Some(vec![
Arc::new(DataValue::Int8(Some(0))),
Arc::new(DataValue::Int8(Some(0))),
Arc::new(DataValue::Int8(Some(1))),
]))
.memcomparable_encode(&mut key_tuple_2)?;
DataValue::Tuple(Some(vec![
Arc::new(DataValue::Int8(Some(0))),
Arc::new(DataValue::Int8(Some(0))),
Arc::new(DataValue::Int8(Some(2))),
]))
.memcomparable_encode(&mut key_tuple_3)?;

println!("{:?} < {:?}", key_tuple_1, key_tuple_2);
println!("{:?} < {:?}", key_tuple_2, key_tuple_3);
assert!(key_tuple_1 < key_tuple_2);
assert!(key_tuple_2 < key_tuple_3);

Ok(())
}
}

0 comments on commit 173c395

Please sign in to comment.