diff --git a/src/storage/table_codec.rs b/src/storage/table_codec.rs index 22d4ebb9..1cc182ad 100644 --- a/src/storage/table_codec.rs +++ b/src/storage/table_codec.rs @@ -227,7 +227,7 @@ impl TableCodec { ) -> Result<(Bytes, Bytes), DatabaseError> { let key = TableCodec::encode_index_key(name, index, Some(tuple_id))?; - Ok((Bytes::from(key), Bytes::from(tuple_id.to_raw()))) + Ok((Bytes::from(key), Bytes::from(tuple_id.to_raw(None)))) } fn _encode_index_key(name: &str, index: &Index) -> Result, DatabaseError> { @@ -267,7 +267,7 @@ impl TableCodec { if let Some(tuple_id) = tuple_id { if matches!(index.ty, IndexType::Normal | IndexType::Composite) { - key_prefix.append(&mut tuple_id.to_raw()); + key_prefix.append(&mut tuple_id.to_raw(None)); } } Ok(key_prefix) diff --git a/src/types/tuple.rs b/src/types/tuple.rs index a272ac6c..ff484a22 100644 --- a/src/types/tuple.rs +++ b/src/types/tuple.rs @@ -113,17 +113,13 @@ impl Tuple { if value.is_null() { bytes[i / BITS_MAX_INDEX] = flip_bit(bytes[i / BITS_MAX_INDEX], i % BITS_MAX_INDEX); } else { - let mut value_bytes = value.to_raw(); + let logical_type = types[i]; + let mut value_bytes = value.to_raw(Some(logical_type)); - if let Some(len) = types[i].raw_len() { - let difference = len.saturating_sub(value_bytes.len()); - - bytes.append(&mut value_bytes); - bytes.append(&mut vec![b' '; difference]); - } else { + if logical_type.raw_len().is_none() { bytes.append(&mut (value_bytes.len() as u32).encode_fixed_vec()); - bytes.append(&mut value_bytes); } + bytes.append(&mut value_bytes); } } diff --git a/src/types/value.rs b/src/types/value.rs index 5cdacf81..2a628fb8 100644 --- a/src/types/value.rs +++ b/src/types/value.rs @@ -378,7 +378,7 @@ impl DataValue { } } - pub fn to_raw(&self) -> Vec { + pub fn to_raw(&self, logical_type: Option) -> Vec { match self { DataValue::Null => None, DataValue::Boolean(v) => v.map(|v| vec![v as u8]), @@ -392,7 +392,16 @@ impl DataValue { DataValue::UInt16(v) => v.map(|v| v.encode_fixed_vec()), DataValue::UInt32(v) => v.map(|v| v.encode_fixed_vec()), DataValue::UInt64(v) => v.map(|v| v.encode_fixed_vec()), - DataValue::Utf8(v) => v.clone().map(|v| v.into_bytes()), + DataValue::Utf8(v) => v.clone().map(|mut v| { + if let Some(LogicalType::Char(len)) = logical_type { + let difference = (len as usize).saturating_sub(v.len()); + + for _ in 0..difference { + v.push(' ') + } + } + v.into_bytes() + }), DataValue::Date32(v) => v.map(|v| v.encode_fixed_vec()), DataValue::Date64(v) => v.map(|v| v.encode_fixed_vec()), DataValue::Decimal(v) => v.map(|v| v.serialize().to_vec()),