coprocessor/xeval: add ceil int function impl
@@ -17,6 +17,7 @@ use super::{Evaluator, EvalContext, Result, Error};
const ERROR_UNIMPLEMENTED: &'static str = "unimplemented";
pub const TYPE_INT: &'static str = "int";
+pub const TYPE_FLOAT: &'static str = "float";
pub fn invalid_type_error(datum: &Datum, expected_type: &str) -> Result<Datum> {
Err(Error::Eval(format!("invalid expr type: {:?}, expect: {}", datum, expected_type)))
@@ -44,9 +45,17 @@ impl Evaluator {
Err(Error::Eval(ERROR_UNIMPLEMENTED.to_owned()))
}
- pub fn ceil_int(&mut self, _ctx: &EvalContext, _expr: &Expr) -> Result<Datum> {
- // TODO add impl
- Err(Error::Eval(ERROR_UNIMPLEMENTED.to_owned()))
+ pub fn ceil_int(&mut self, ctx: &EvalContext, expr: &Expr) -> Result<Datum> {
+ let child = try!(self.get_one_child(expr));
+ let d = try!(self.eval(ctx, child));
+ match d {
+ Datum::F64(i) => {
+ let result = i.ceil() as i64;
+ Ok(Datum::I64(result))
+ }
+ Datum::Null => Ok(Datum::Null),
+ _ => invalid_type_error(&d, TYPE_FLOAT),
+ }
}
pub fn ceil_real(&mut self, _ctx: &EvalContext, _expr: &Expr) -> Result<Datum> {
@@ -110,4 +119,22 @@ mod test {
ExprType::ScalarFunc,
ScalarFuncSig::AbsInt),
Datum::U64(1))]);
+
+ test_eval!(test_ceil_int,
+ vec![(build_expr_with_sig(vec![Datum::F64(-1.23)],
+ ExprType::ScalarFunc,
+ ScalarFuncSig::CeilInt),
+ Datum::I64(-1)),
+ (build_expr_with_sig(vec![Datum::F64(1.23)],
+ ExprType::ScalarFunc,
+ ScalarFuncSig::CeilInt),
+ Datum::I64(2)),
+ (build_expr_with_sig(vec![Datum::F64(2.0)],
+ ExprType::ScalarFunc,
+ ScalarFuncSig::CeilInt),
+ Datum::I64(2)),
+ (build_expr_with_sig(vec![Datum::Null],
+ ExprType::ScalarFunc,
+ ScalarFuncSig::CeilInt),
+ Datum::Null)]);
}
