Skip to content

Commit 0d36376

Browse files
committed
fix and add ci tests for more types
Committed-by: bingqing.lbq from Dev container
1 parent 55bdca4 commit 0d36376

File tree

4 files changed

+162
-21
lines changed

4 files changed

+162
-21
lines changed

interactive_engine/executor/store/groot/src/db/api/property.rs

Lines changed: 85 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,11 @@ impl ValueType {
104104
DataTypePb::UINT => Ok(ValueType::UInt),
105105
DataTypePb::ULONG => Ok(ValueType::ULong),
106106
DataTypePb::DATE32 => Ok(ValueType::Date32),
107+
DataTypePb::TIME32_MS => Ok(ValueType::Time32),
107108
DataTypePb::TIMESTAMP_MS => Ok(ValueType::Timestamp),
108109
DataTypePb::FIXED_CHAR => Ok(ValueType::FixedChar(1)), // todo: fixed_length
109110
DataTypePb::VAR_CHAR => Ok(ValueType::VarChar(255)), // todo: max_length
110111
_ => {
111-
let msg = format!("unsupported data type {:?}", pb);
112-
let err = gen_graph_err!(ErrorCode::INVALID_DATA, msg, from_proto, pb);
113112
let msg = format!("unsupported data type {:?}", pb);
114113
let err = gen_graph_err!(ErrorCode::INVALID_DATA, msg, from_proto, pb);
115114
Err(err)
@@ -138,8 +137,8 @@ impl ValueType {
138137
ValueType::Date32 => Ok(DataTypePb::DATE32),
139138
ValueType::Time32 => Ok(DataTypePb::TIME32_MS),
140139
ValueType::Timestamp => Ok(DataTypePb::TIMESTAMP_MS),
141-
ValueType::FixedChar(fixed_length) => Ok(DataTypePb::FIXED_CHAR),
142-
ValueType::VarChar(max_length) => Ok(DataTypePb::VAR_CHAR),
140+
ValueType::FixedChar(_fixed_length) => Ok(DataTypePb::FIXED_CHAR),
141+
ValueType::VarChar(_max_length) => Ok(DataTypePb::VAR_CHAR),
143142
}
144143
}
145144

@@ -250,12 +249,44 @@ impl<'a> ValueRef<'a> {
250249
.map_err(|e| gen_graph_err!(ErrorCode::INVALID_DATA, e.to_string(), get_str))
251250
}
252251

252+
pub fn get_fixed_char(&self) -> GraphResult<&str> {
253+
let res = self.check_type_match(ValueType::FixedChar(1));
254+
res_unwrap!(res, get_fixed_char)?;
255+
::std::str::from_utf8(self.data)
256+
.map_err(|e| gen_graph_err!(ErrorCode::INVALID_DATA, e.to_string(), get_fixed_char))
257+
}
258+
259+
pub fn get_var_char(&self) -> GraphResult<&str> {
260+
let res = self.check_type_match(ValueType::VarChar(255));
261+
res_unwrap!(res, get_var_char)?;
262+
::std::str::from_utf8(self.data)
263+
.map_err(|e| gen_graph_err!(ErrorCode::INVALID_DATA, e.to_string(), get_var_char))
264+
}
265+
253266
pub fn get_bytes(&self) -> GraphResult<&[u8]> {
254267
let res = self.check_type_match(ValueType::Bytes);
255268
res_unwrap!(res, get_str)?;
256269
Ok(self.data)
257270
}
258271

272+
pub fn get_date32(&self) -> GraphResult<i32> {
273+
let res = self.check_type_match(ValueType::Date32);
274+
res_unwrap!(res, get_date32)?;
275+
Ok(get_int(self.data))
276+
}
277+
278+
pub fn get_time32(&self) -> GraphResult<i32> {
279+
let res = self.check_type_match(ValueType::Time32);
280+
res_unwrap!(res, get_time32)?;
281+
Ok(get_int(self.data))
282+
}
283+
284+
pub fn get_timestamp(&self) -> GraphResult<i64> {
285+
let res = self.check_type_match(ValueType::Timestamp);
286+
res_unwrap!(res, get_timestamp)?;
287+
Ok(get_long(self.data))
288+
}
289+
259290
pub fn get_int_list(&self) -> GraphResult<NumericArray<i32>> {
260291
let res = self
261292
.check_type_match(ValueType::IntList)
@@ -332,17 +363,9 @@ impl<'a> ValueRef<'a> {
332363

333364
pub fn check_type_match(&self, value_type: ValueType) -> GraphResult<()> {
334365
if self.r#type != value_type {
335-
if (self.r#type.eq(&ValueType::Date32) || self.r#type.eq(&ValueType::Time32))
336-
&& value_type.eq(&ValueType::Int)
337-
{
338-
return Ok(());
339-
} else if self.r#type.eq(&ValueType::Timestamp) && value_type.eq(&ValueType::Long) {
340-
return Ok(());
341-
} else {
342-
let msg = format!("cannot transform {:?} to {:?}", self.r#type, value_type);
343-
let err = gen_graph_err!(ErrorCode::VALUE_TYPE_MISMATCH, msg, check_type_match, value_type);
344-
return Err(err);
345-
}
366+
let msg = format!("cannot transform {:?} to {:?}", self.r#type, value_type);
367+
let err = gen_graph_err!(ErrorCode::VALUE_TYPE_MISMATCH, msg, check_type_match, value_type);
368+
return Err(err);
346369
}
347370
Ok(())
348371
}
@@ -698,11 +721,21 @@ impl Value {
698721
Value::new(ValueType::Int, data)
699722
}
700723

724+
pub fn uint(v: u32) -> Self {
725+
let data = transform::u32_to_vec(v.to_be());
726+
Value::new(ValueType::UInt, data)
727+
}
728+
701729
pub fn long(v: i64) -> Self {
702730
let data = transform::i64_to_vec(v.to_be());
703731
Value::new(ValueType::Long, data)
704732
}
705733

734+
pub fn ulong(v: u64) -> Self {
735+
let data = transform::u64_to_vec(v.to_be());
736+
Value::new(ValueType::ULong, data)
737+
}
738+
706739
pub fn float(v: f32) -> Self {
707740
let data = transform::u32_to_vec(v.to_bits().to_be());
708741
Value::new(ValueType::Float, data)
@@ -718,11 +751,48 @@ impl Value {
718751
Value::new(ValueType::String, data)
719752
}
720753

754+
pub fn fixed_char(v: &str, len: usize) -> Self {
755+
let str = if v.len() > len {
756+
// truncate the string
757+
&v[..len]
758+
} else {
759+
v
760+
};
761+
let data = str.as_bytes().to_vec();
762+
Value::new(ValueType::FixedChar(len), data)
763+
}
764+
765+
pub fn var_char(v: &str, max_len: usize) -> Self {
766+
let str = if v.len() > max_len {
767+
// truncate the string
768+
&v[..max_len]
769+
} else {
770+
v
771+
};
772+
let data = str.as_bytes().to_vec();
773+
Value::new(ValueType::VarChar(max_len), data)
774+
}
775+
721776
pub fn bytes(v: &[u8]) -> Self {
722777
let data = v.to_vec();
723778
Value::new(ValueType::Bytes, data)
724779
}
725780

781+
pub fn date32(v: i32) -> Self {
782+
let data = transform::i32_to_vec(v.to_be());
783+
Value::new(ValueType::Date32, data)
784+
}
785+
786+
pub fn time32(v: i32) -> Self {
787+
let data = transform::i32_to_vec(v.to_be());
788+
Value::new(ValueType::Time32, data)
789+
}
790+
791+
pub fn timestamp(v: i64) -> Self {
792+
let data = transform::i64_to_vec(v.to_be());
793+
Value::new(ValueType::Timestamp, data)
794+
}
795+
726796
pub fn int_list(v: &[i32]) -> Self {
727797
let data = gen_array!(v, i32, write_i32);
728798
Value::new(ValueType::IntList, data)

interactive_engine/executor/store/groot/src/db/api/types.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,25 @@ impl From<ValueRef<'_>> for PropertyValue {
124124
.map(String::from)
125125
.collect(),
126126
),
127-
ValueType::Date32 => PropertyValue::Date32(value_ref.get_int().unwrap()),
128-
ValueType::Time32 => PropertyValue::Time32(value_ref.get_int().unwrap()),
129-
ValueType::Timestamp => PropertyValue::Timestamp(value_ref.get_long().unwrap()),
130-
ValueType::FixedChar(fixed_length) => todo!(),
131-
ValueType::VarChar(max_length) => todo!(),
127+
ValueType::Date32 => PropertyValue::Date32(value_ref.get_date32().unwrap()),
128+
ValueType::Time32 => PropertyValue::Time32(value_ref.get_time32().unwrap()),
129+
ValueType::Timestamp => PropertyValue::Timestamp(value_ref.get_timestamp().unwrap()),
130+
ValueType::FixedChar(fixed) => PropertyValue::FixedChar(FixedCharValue {
131+
value: value_ref
132+
.get_fixed_char()
133+
.unwrap()
134+
.chars()
135+
.collect(),
136+
fixed_length: *fixed,
137+
}),
138+
ValueType::VarChar(max) => PropertyValue::VarChar(VarCharValue {
139+
value: value_ref
140+
.get_var_char()
141+
.unwrap()
142+
.chars()
143+
.collect(),
144+
max_length: *max,
145+
}),
132146
}
133147
}
134148
}

interactive_engine/executor/store/groot/src/db/graph/codec.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ impl Decoder {
178178
ValueType::Short => reader.read_bytes(offset, 2),
179179
ValueType::Int | ValueType::Float => reader.read_bytes(offset, 4),
180180
ValueType::Double | ValueType::Long => reader.read_bytes(offset, 8),
181+
ValueType::FixedChar(len) => reader.read_bytes(offset, len),
181182
_ => unreachable!(),
182183
};
183184
let ret = ValueRef::new(info.r#type, bytes);
@@ -720,7 +721,7 @@ mod tests {
720721
assert_ne!(decode_item, None);
721722
if let Some((prop_id, v)) = decode_item {
722723
assert_eq!(prop_id, 18);
723-
assert_eq!(*v.get_type() as i32, ValueType::Long as i32);
724+
assert_eq!(*v.get_type(), ValueType::Long);
724725
assert_eq!(v.get_long().unwrap(), 20120904101614543);
725726
}
726727
assert_eq!(decode_iter.next(), None);

interactive_engine/executor/store/groot/src/db/graph/tests/data.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,34 @@ fn vertex_prop(si: SnapshotId, label: LabelId, id: VertexId, r#type: ValueType)
138138
let v = vec![format!("{}", x), format!("{}", y), format!("{}_{}", x, y), format!("{}", s)];
139139
Value::string_list(&v)
140140
}
141+
ValueType::UInt => {
142+
let v = (x * x + y * 23 + 123 + s) % 1000000007;
143+
Value::uint(v as u32)
144+
}
145+
ValueType::ULong => {
146+
let v = x * x * x + y * y - 1234 * x + 3333 * y + 1 + s;
147+
Value::ulong(v as u64)
148+
}
149+
ValueType::Date32 => {
150+
let v = (x + y + s) % 10000;
151+
Value::date32(v as i32)
152+
}
153+
ValueType::Time32 => {
154+
let v = (x + y + s) % 10000;
155+
Value::time32(v as i32)
156+
}
157+
ValueType::Timestamp => {
158+
let v = (x + y + s) % 10000;
159+
Value::timestamp(v as i64)
160+
}
161+
ValueType::FixedChar(fixed_length) => {
162+
let v = format!("t");
163+
Value::fixed_char(&v, fixed_length)
164+
}
165+
ValueType::VarChar(max_length) => {
166+
let v = format!("test");
167+
Value::var_char(&v, max_length)
168+
}
141169
}
142170
}
143171

@@ -213,5 +241,33 @@ fn edge_prop(si: SnapshotId, edge_type: &EdgeKind, id: &EdgeId, r#type: ValueTyp
213241
let v = vec![format!("{}", x), format!("{}", y), format!("{}_{}", x, y), format!("{}", s)];
214242
Value::string_list(&v)
215243
}
244+
ValueType::UInt => {
245+
let v = (x * x + y * 23 + 123 + s) % 1000000007;
246+
Value::uint(v as u32)
247+
}
248+
ValueType::ULong => {
249+
let v = x * x * 2 + y * 100 - 1234 * x + 3333 * y + 1 + s;
250+
Value::ulong(v as u64)
251+
}
252+
ValueType::Date32 => {
253+
let v = (x + y + s) % 10000;
254+
Value::date32(v as i32)
255+
}
256+
ValueType::Time32 => {
257+
let v = (x + y + s) % 10000;
258+
Value::time32(v as i32)
259+
}
260+
ValueType::Timestamp => {
261+
let v = (x + y + s) % 10000;
262+
Value::timestamp(v as i64)
263+
}
264+
ValueType::FixedChar(fixed_length) => {
265+
let v = format!("t");
266+
Value::fixed_char(&v, fixed_length)
267+
}
268+
ValueType::VarChar(max_length) => {
269+
let v = format!("test");
270+
Value::var_char(&v, max_length)
271+
}
216272
}
217273
}

0 commit comments

Comments
 (0)