Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

copr: implement several mysql time related types json #13418

Merged
merged 2 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions components/tidb_query_datatype/src/codec/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,14 +510,14 @@ impl<'a> ToInt for JsonRef<'a> {
// TiDB: 5
// MySQL: 4
let val = match self.get_type() {
JsonType::Object | JsonType::Array | JsonType::Opaque => Ok(ctx
.handle_truncate_err(Error::truncated_wrong_val("Integer", self.to_string()))
.map(|_| 0)?),
JsonType::Literal => Ok(self.get_literal().map_or(0, |x| x as i64)),
JsonType::I64 => Ok(self.get_i64()),
JsonType::U64 => Ok(self.get_u64() as i64),
JsonType::Double => self.get_double().to_int(ctx, tp),
JsonType::String => self.get_str_bytes()?.to_int(ctx, tp),
_ => Ok(ctx
.handle_truncate_err(Error::truncated_wrong_val("Integer", self.to_string()))
.map(|_| 0)?),
}?;
val.to_int(ctx, tp)
}
Expand All @@ -526,14 +526,14 @@ impl<'a> ToInt for JsonRef<'a> {
#[inline]
fn to_uint(&self, ctx: &mut EvalContext, tp: FieldTypeTp) -> Result<u64> {
let val = match self.get_type() {
JsonType::Object | JsonType::Array | JsonType::Opaque => Ok(ctx
.handle_truncate_err(Error::truncated_wrong_val("Integer", self.to_string()))
.map(|_| 0)?),
JsonType::Literal => Ok(self.get_literal().map_or(0, |x| x as u64)),
JsonType::I64 => Ok(self.get_i64() as u64),
JsonType::U64 => Ok(self.get_u64()),
JsonType::Double => self.get_double().to_uint(ctx, tp),
JsonType::String => self.get_str_bytes()?.to_uint(ctx, tp),
_ => Ok(ctx
.handle_truncate_err(Error::truncated_wrong_val("Integer", self.to_string()))
.map(|_| 0)?),
}?;
val.to_uint(ctx, tp)
}
Expand Down
249 changes: 248 additions & 1 deletion components/tidb_query_datatype/src/codec/mysql/json/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ impl<'a> JsonRef<'a> {
&self.value()[val_offset..val_offset + opaque_bytes_len as usize + len_len + 1],
)
}
JsonType::Date | JsonType::Datetime | JsonType::Timestamp => {
YangKeao marked this conversation as resolved.
Show resolved Hide resolved
JsonRef::new(val_type, &self.value()[val_offset..val_offset + TIME_LEN])
}
JsonType::Time => JsonRef::new(
val_type,
&self.value()[val_offset..val_offset + DURATION_LEN],
),
_ => {
let data_size =
NumberCodec::decode_u32_le(&self.value()[val_offset + ELEMENT_COUNT_LEN..])
Expand Down Expand Up @@ -122,7 +129,16 @@ impl<'a> JsonRef<'a> {

#[cfg(test)]
mod tests {
use super::{super::Json, *};
use std::collections::BTreeMap;

use super::*;
use crate::{
codec::{
data_type::Duration,
mysql::{Json, Time, TimeType},
},
expr::EvalContext,
};

#[test]
fn test_type() {
Expand All @@ -143,4 +159,235 @@ mod tests {
assert_eq!(json.as_ref().get_type(), tp, "{:?}", json_str);
}
}

#[test]
fn test_array_get_elem() {
let mut ctx = EvalContext::default();

let time = Time::parse(
&mut ctx,
"1998-06-13 12:13:14",
TimeType::DateTime,
0,
false,
)
.unwrap();
let duration = Duration::parse(&mut ctx, "12:13:14", 0).unwrap();
let array = vec![
Json::from_u64(1).unwrap(),
Json::from_str_val("abcdefg").unwrap(),
];
let object = BTreeMap::from([
("key1".to_string(), Json::from_u64(1).unwrap()),
("key2".to_string(), Json::from_str_val("abcdefg").unwrap()),
]);

let json_array = Json::from_array(vec![
Json::from_u64(1).unwrap(),
Json::from_time(time).unwrap(),
Json::from_duration(duration).unwrap(),
Json::from_array(array).unwrap(),
Json::from_str_val("abcdefg").unwrap(),
Json::from_bool(false).unwrap(),
Json::from_object(object).unwrap(),
])
.unwrap();
let json_array_ref = json_array.as_ref();

assert_eq!(json_array_ref.array_get_elem(0).unwrap().get_u64(), 1);
assert_eq!(
json_array_ref
.array_get_elem(1)
.unwrap()
.get_time()
.unwrap(),
time
);
assert_eq!(
json_array_ref
.array_get_elem(2)
.unwrap()
.get_duration()
.unwrap(),
duration
);
assert_eq!(
json_array_ref
.array_get_elem(3)
.unwrap()
.array_get_elem(0)
.unwrap()
.get_u64(),
1
);
assert_eq!(
json_array_ref
.array_get_elem(3)
.unwrap()
.array_get_elem(1)
.unwrap()
.get_str()
.unwrap(),
"abcdefg"
);
assert_eq!(
json_array_ref.array_get_elem(4).unwrap().get_str().unwrap(),
"abcdefg"
);
assert_eq!(
json_array_ref
.array_get_elem(5)
.unwrap()
.get_literal()
.unwrap(),
false
);
assert_eq!(
json_array_ref.array_get_elem(6).unwrap().object_get_key(0),
b"key1"
);
assert_eq!(
json_array_ref.array_get_elem(6).unwrap().object_get_key(1),
b"key2"
);
assert_eq!(
json_array_ref
.array_get_elem(6)
.unwrap()
.object_get_val(0)
.unwrap()
.get_u64(),
1
);
assert_eq!(
json_array_ref
.array_get_elem(6)
.unwrap()
.object_get_val(1)
.unwrap()
.get_str()
.unwrap(),
"abcdefg"
);
}

#[test]
fn test_object_get_val() {
let mut ctx = EvalContext::default();

let time = Time::parse(
&mut ctx,
"1998-06-13 12:13:14",
TimeType::DateTime,
0,
false,
)
.unwrap();
let duration = Duration::parse(&mut ctx, "12:13:14", 0).unwrap();
let array = vec![
Json::from_u64(1).unwrap(),
Json::from_str_val("abcdefg").unwrap(),
];
let object = BTreeMap::from([
("key1".to_string(), Json::from_u64(1).unwrap()),
("key2".to_string(), Json::from_str_val("abcdefg").unwrap()),
]);

let json_object = Json::from_object(BTreeMap::from([
("0".to_string(), Json::from_u64(1).unwrap()),
("1".to_string(), Json::from_time(time).unwrap()),
("2".to_string(), Json::from_duration(duration).unwrap()),
("3".to_string(), Json::from_array(array).unwrap()),
("4".to_string(), Json::from_str_val("abcdefg").unwrap()),
("5".to_string(), Json::from_bool(false).unwrap()),
("6".to_string(), Json::from_object(object).unwrap()),
]))
.unwrap();
let json_object_ref = json_object.as_ref();

assert_eq!(json_object_ref.object_get_key(0), b"0");
assert_eq!(json_object_ref.object_get_key(1), b"1");
assert_eq!(json_object_ref.object_get_key(2), b"2");
assert_eq!(json_object_ref.object_get_key(3), b"3");

assert_eq!(json_object_ref.object_get_val(0).unwrap().get_u64(), 1);
assert_eq!(
json_object_ref
.object_get_val(1)
.unwrap()
.get_time()
.unwrap(),
time
);
assert_eq!(
json_object_ref
.object_get_val(2)
.unwrap()
.get_duration()
.unwrap(),
duration
);
assert_eq!(
json_object_ref
.object_get_val(3)
.unwrap()
.array_get_elem(0)
.unwrap()
.get_u64(),
1
);
assert_eq!(
json_object_ref
.object_get_val(3)
.unwrap()
.array_get_elem(1)
.unwrap()
.get_str()
.unwrap(),
"abcdefg"
);
assert_eq!(
json_object_ref
.object_get_val(4)
.unwrap()
.get_str()
.unwrap(),
"abcdefg"
);
assert_eq!(
json_object_ref
.object_get_val(5)
.unwrap()
.get_literal()
.unwrap(),
false
);
assert_eq!(
json_object_ref.object_get_val(6).unwrap().object_get_key(0),
b"key1"
);
assert_eq!(
json_object_ref.object_get_val(6).unwrap().object_get_key(1),
b"key2"
);
assert_eq!(
json_object_ref
.object_get_val(6)
.unwrap()
.object_get_val(0)
.unwrap()
.get_u64(),
1
);
assert_eq!(
json_object_ref
.object_get_val(6)
.unwrap()
.object_get_val(1)
.unwrap()
.get_str()
.unwrap(),
"abcdefg"
);
}
}