Skip to content

Commit

Permalink
some code refine
Browse files Browse the repository at this point in the history
Committed-by: bingqing.lbq from Dev container
  • Loading branch information
BingqingLyu committed Oct 16, 2023
1 parent b37c026 commit 5875cef
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 24 deletions.
16 changes: 6 additions & 10 deletions interactive_engine/executor/common/dyn_type/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -891,12 +891,10 @@ impl Object {
}

#[inline]
pub fn as_date_format(&self) -> Result<DateTimeFormats, CastError> {
pub fn as_date_format(&self) -> Result<&DateTimeFormats, CastError> {
match self {
Object::Primitive(p) => Err(CastError::new::<DateTimeFormats>(p.raw_type())),
Object::String(str) => DateTimeFormats::from_str(str),
Object::DateFormat(d) => Ok((*d).clone()),
Object::DynOwned(x) => try_downcast_ref!(x, DateTimeFormats).map(|dt| dt.clone()),
Object::DateFormat(d) => Ok(d),
Object::DynOwned(x) => try_downcast_ref!(x, DateTimeFormats),
_ => Err(CastError::new::<DateTimeFormats>(self.raw_type())),
}
}
Expand Down Expand Up @@ -1099,12 +1097,10 @@ impl<'a> BorrowObject<'a> {
}

#[inline]
pub fn as_date_format(&self) -> Result<DateTimeFormats, CastError> {
pub fn as_date_format(&self) -> Result<&DateTimeFormats, CastError> {
match self {
BorrowObject::Primitive(p) => Err(CastError::new::<DateTimeFormats>(p.raw_type())),
BorrowObject::String(str) => DateTimeFormats::from_str(str),
BorrowObject::DateFormat(d) => Ok((*d).clone()),
BorrowObject::DynRef(x) => try_downcast_ref!(x, DateTimeFormats).map(|dt| dt.clone()),
BorrowObject::DateFormat(d) => Ok(d),
BorrowObject::DynRef(x) => try_downcast_ref!(x, DateTimeFormats),
_ => Err(CastError::new::<DateTimeFormats>(self.raw_type())),
}
}
Expand Down
21 changes: 17 additions & 4 deletions interactive_engine/executor/ir/graph_proxy/src/utils/expr/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,7 @@ impl InnerOpr {
#[cfg(test)]
mod tests {
use ahash::HashMap;
use dyn_type::DateTimeFormats;
use ir_common::expr_parse::str_to_expr_pb;

use super::*;
Expand Down Expand Up @@ -1086,22 +1087,34 @@ mod tests {

fn prepare_context_with_date() -> Vertices {
let map1: HashMap<NameOrId, Object> = vec![
(NameOrId::from("date1".to_string()), "2020-08-08".into()),
(
NameOrId::from("date1".to_string()),
(DateTimeFormats::from_str("2020-08-08").unwrap()).into(),
),
(
NameOrId::from("date2".to_string()),
chrono::NaiveDate::from_ymd_opt(2020, 8, 8)
.unwrap()
.into(),
),
(NameOrId::from("time1".to_string()), "10:11:12.100".into()),
(
NameOrId::from("time1".to_string()),
(DateTimeFormats::from_str("10:11:12.100").unwrap()).into(),
),
(
NameOrId::from("time2".to_string()),
chrono::NaiveTime::from_hms_milli_opt(10, 11, 12, 100)
.unwrap()
.into(),
),
(NameOrId::from("datetime1".to_string()), "2020-08-08T23:11:12.100-11:00".into()),
(NameOrId::from("datetime2".to_string()), "2020-08-09 10:11:12.100".into()),
(
NameOrId::from("datetime1".to_string()),
(DateTimeFormats::from_str("2020-08-08T23:11:12.100-11:00").unwrap()).into(),
),
(
NameOrId::from("datetime2".to_string()),
(DateTimeFormats::from_str("2020-08-09 10:11:12.100").unwrap()).into(),
),
(
NameOrId::from("datetime3".to_string()),
chrono::NaiveDateTime::from_timestamp_millis(1602324610100)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ mod tests {
use crate::process::operator::map::FilterMapFuncGen;
use crate::process::operator::tests::{
init_source, init_source_with_multi_tags, init_source_with_tag, init_vertex1, init_vertex2,
to_expr_var_pb, to_expr_vars_pb, to_ir_data_type, to_var_pb, PERSON_LABEL, TAG_A, TAG_B, TAG_C,
TAG_D, TAG_E, TAG_F, TAG_G,
to_expr_var_pb, to_expr_vars_pb, to_var_pb, PERSON_LABEL, TAG_A, TAG_B, TAG_C, TAG_D, TAG_E, TAG_F,
TAG_G,
};
use crate::process::record::Record;

Expand Down Expand Up @@ -831,28 +831,28 @@ mod tests {
r1.append(datetime_obj, Some(TAG_C.into()));

let extract_date_year_opr = common_pb::ExprOpr {
node_type: Some(to_ir_data_type(common_pb::DataType::Date32)),
node_type: None,
item: Some(common_pb::expr_opr::Item::Extract(common_pb::Extract {
interval: common_pb::extract::Interval::Year as i32,
})),
};

let extract_time_hour_opr = common_pb::ExprOpr {
node_type: Some(to_ir_data_type(common_pb::DataType::Time32)),
node_type: None,
item: Some(common_pb::expr_opr::Item::Extract(common_pb::Extract {
interval: common_pb::extract::Interval::Hour as i32,
})),
};

let extract_datetime_month_opr = common_pb::ExprOpr {
node_type: Some(to_ir_data_type(common_pb::DataType::Timestamp)),
node_type: None,
item: Some(common_pb::expr_opr::Item::Extract(common_pb::Extract {
interval: common_pb::extract::Interval::Month as i32,
})),
};

let extract_datetime_minute_opr = common_pb::ExprOpr {
node_type: Some(to_ir_data_type(common_pb::DataType::Timestamp)),
node_type: None,
item: Some(common_pb::expr_opr::Item::Extract(common_pb::Extract {
interval: common_pb::extract::Interval::Minute as i32,
})),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,6 @@ pub(crate) mod tests {
vec![r1]
}

pub fn to_ir_data_type(data_type: common_pb::DataType) -> common_pb::IrDataType {
common_pb::IrDataType { r#type: Some(common_pb::ir_data_type::Type::DataType(data_type as i32)) }
}

pub fn to_var_pb(tag: Option<NameOrId>, key: Option<NameOrId>) -> common_pb::Variable {
common_pb::Variable {
tag: tag.map(|t| t.into()),
Expand Down

0 comments on commit 5875cef

Please sign in to comment.