quill_sql/storage/codec/
scalar.rs1use crate::catalog::DataType;
2use crate::error::QuillSQLResult;
3use crate::storage::codec::{CommonCodec, DecodedData};
4use crate::utils::scalar::ScalarValue;
5
6pub struct ScalarValueCodec;
7
8impl ScalarValueCodec {
9 pub fn encode(value: &ScalarValue) -> Vec<u8> {
10 match value {
11 ScalarValue::Boolean(Some(v)) => CommonCodec::encode_bool(*v),
12 ScalarValue::Int8(Some(v)) => CommonCodec::encode_i8(*v),
13 ScalarValue::Int16(Some(v)) => CommonCodec::encode_i16(*v),
14 ScalarValue::Int32(Some(v)) => CommonCodec::encode_i32(*v),
15 ScalarValue::Int64(Some(v)) => CommonCodec::encode_i64(*v),
16 ScalarValue::UInt8(Some(v)) => CommonCodec::encode_u8(*v),
17 ScalarValue::UInt16(Some(v)) => CommonCodec::encode_u16(*v),
18 ScalarValue::UInt32(Some(v)) => CommonCodec::encode_u32(*v),
19 ScalarValue::UInt64(Some(v)) => CommonCodec::encode_u64(*v),
20 ScalarValue::Float32(Some(v)) => CommonCodec::encode_f32(*v),
21 ScalarValue::Float64(Some(v)) => CommonCodec::encode_f64(*v),
22 ScalarValue::Varchar(Some(v)) => {
23 if v.len() > u16::MAX as usize {
24 panic!("Varchar length is greater than u16::Max")
25 }
26 let mut bytes = vec![];
27 bytes.extend(CommonCodec::encode_u16(v.len() as u16));
28 bytes.extend(CommonCodec::encode_string(v));
29 bytes
30 }
31 ScalarValue::Boolean(None)
33 | ScalarValue::Int8(None)
34 | ScalarValue::Int16(None)
35 | ScalarValue::Int32(None)
36 | ScalarValue::Int64(None)
37 | ScalarValue::UInt8(None)
38 | ScalarValue::UInt16(None)
39 | ScalarValue::UInt32(None)
40 | ScalarValue::UInt64(None)
41 | ScalarValue::Float32(None)
42 | ScalarValue::Float64(None)
43 | ScalarValue::Varchar(None) => vec![],
44 }
45 }
46
47 pub fn decode(bytes: &[u8], data_type: DataType) -> QuillSQLResult<DecodedData<ScalarValue>> {
48 match data_type {
49 DataType::Boolean => {
50 let (value, offset) = CommonCodec::decode_bool(bytes)?;
51 Ok((ScalarValue::Boolean(Some(value)), offset))
52 }
53 DataType::Int8 => {
54 let (value, offset) = CommonCodec::decode_i8(bytes)?;
55 Ok((ScalarValue::Int8(Some(value)), offset))
56 }
57 DataType::Int16 => {
58 let (value, offset) = CommonCodec::decode_i16(bytes)?;
59 Ok((ScalarValue::Int16(Some(value)), offset))
60 }
61 DataType::Int32 => {
62 let (value, offset) = CommonCodec::decode_i32(bytes)?;
63 Ok((ScalarValue::Int32(Some(value)), offset))
64 }
65 DataType::Int64 => {
66 let (value, offset) = CommonCodec::decode_i64(bytes)?;
67 Ok((ScalarValue::Int64(Some(value)), offset))
68 }
69 DataType::UInt8 => {
70 let (value, offset) = CommonCodec::decode_u8(bytes)?;
71 Ok((ScalarValue::UInt8(Some(value)), offset))
72 }
73 DataType::UInt16 => {
74 let (value, offset) = CommonCodec::decode_u16(bytes)?;
75 Ok((ScalarValue::UInt16(Some(value)), offset))
76 }
77 DataType::UInt32 => {
78 let (value, offset) = CommonCodec::decode_u32(bytes)?;
79 Ok((ScalarValue::UInt32(Some(value)), offset))
80 }
81 DataType::UInt64 => {
82 let (value, offset) = CommonCodec::decode_u64(bytes)?;
83 Ok((ScalarValue::UInt64(Some(value)), offset))
84 }
85 DataType::Float32 => {
86 let (value, offset) = CommonCodec::decode_f32(bytes)?;
87 Ok((ScalarValue::Float32(Some(value)), offset))
88 }
89 DataType::Float64 => {
90 let (value, offset) = CommonCodec::decode_f64(bytes)?;
91 Ok((ScalarValue::Float64(Some(value)), offset))
92 }
93 DataType::Varchar(_) => {
94 let mut left_bytes = bytes;
95
96 let (length, offset) = CommonCodec::decode_u16(left_bytes)?;
97 left_bytes = &left_bytes[offset..];
98
99 let (value, offset) = CommonCodec::decode_string(&left_bytes[0..length as usize])?;
100 left_bytes = &left_bytes[offset..];
101
102 Ok((
103 ScalarValue::Varchar(Some(value)),
104 bytes.len() - left_bytes.len(),
105 ))
106 }
107 }
108 }
109}