Skip to content
Permalink
Browse files

copr: Add vectorize day_of_month (tikv#6103)

Signed-off-by: FateTHarlaown <623239185@qq.com>
  • Loading branch information
FateTHarlaown authored and sre-bot committed Nov 30, 2019
1 parent 5e279c5 commit e862ba3d7f170646535f10c8c5228897fd79751c
Showing with 41 additions and 0 deletions.
  1. +40 −0 components/tidb_query/src/rpn_expr/impl_time.rs
  2. +1 −0 components/tidb_query/src/rpn_expr/mod.rs
@@ -128,6 +128,25 @@ pub fn year(ctx: &mut EvalContext, t: &Option<DateTime>) -> Result<Option<Int>>
Ok(Some(Int::from(t.year())))
}

#[rpn_fn(capture = [ctx])]
#[inline]
pub fn day_of_month(ctx: &mut EvalContext, t: &Option<DateTime>) -> Result<Option<Int>> {
let t = match t {
Some(v) => v,
_ => return Ok(None),
};

if t.is_zero() {
if ctx.cfg.sql_mode.contains(SqlMode::NO_ZERO_DATE) {
return ctx
.handle_invalid_time_error(Error::incorrect_datetime_value(&format!("{}", t)))
.map(|_| Ok(None))?;
}
return Ok(Some(0));
}
Ok(Some(Int::from(t.day())))
}

#[cfg(test)]
mod tests {
use super::*;
@@ -455,4 +474,25 @@ mod tests {
assert_eq!(output, expect);
}
}

#[test]
fn test_day_of_month() {
let cases = vec![
(Some("0000-00-00 00:00:00.000000"), Some(0)),
(Some("2018-02-01 00:00:00.000000"), Some(1)),
(Some("2018-02-15 00:00:00.000000"), Some(15)),
(Some("2018-02-28 00:00:00.000000"), Some(28)),
(Some("2016-02-29 00:00:00.000000"), Some(29)),
(None, None),
];
let mut ctx = EvalContext::default();
for (time, expect) in cases {
let time = time.map(|t| DateTime::parse_datetime(&mut ctx, t, 6, true).unwrap());
let output = RpnFnScalarEvaluator::new()
.push_param(time)
.evaluate(ScalarFuncSig::DayOfMonth)
.unwrap();
assert_eq!(output, expect);
}
}
}
@@ -406,6 +406,7 @@ fn map_expr_node_to_rpn_func(expr: &Expr) -> Result<RpnFnMeta> {
ScalarFuncSig::Space => space_fn_meta(),
ScalarFuncSig::Strcmp => strcmp_fn_meta(),
ScalarFuncSig::Year => year_fn_meta(),
ScalarFuncSig::DayOfMonth => day_of_month_fn_meta(),
_ => return Err(other_err!(
"ScalarFunction {:?} is not supported in batch mode",
value

0 comments on commit e862ba3

Please sign in to comment.
You can’t perform that action at this time.