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

coprocessor, dag: support decode Time from tipb.Expr #2199

Merged
merged 43 commits into from Aug 28, 2017
Commits
Jump to file or symbol
Failed to load files and symbols.
+41 −25
Diff settings

Always

Just for now

@@ -224,13 +224,13 @@ mod test {
let rus = mysql::has_unsigned_flag(rhs.get_field_type().get_flag());
let unsigned = lus | rus;
let mut op = Expression::build(fncall_expr(tt.0, &[lhs, rhs]), 0).unwrap();
let mut op = Expression::build(fncall_expr(tt.0, &[lhs, rhs]), 0, &ctx).unwrap();
if unsigned {
// According to TiDB, the result is unsigned if any of arguments is unsigned.
op.mut_tp().set_flag(types::UNSIGNED_FLAG as u32);
}
let expected = Expression::build(datum_expr(tt.3), 0).unwrap();
let expected = Expression::build(datum_expr(tt.3), 0, &ctx).unwrap();
let got = op.eval_int(&ctx, &[]).unwrap();
let exp = expected.eval_int(&ctx, &[]).unwrap();
@@ -265,8 +265,8 @@ mod test {
let lhs = datum_expr(tt.1);
let rhs = datum_expr(tt.2);
let op = Expression::build(fncall_expr(tt.0, &[lhs, rhs]), 0).unwrap();
let expected = Expression::build(datum_expr(tt.3), 0).unwrap();
let op = Expression::build(fncall_expr(tt.0, &[lhs, rhs]), 0, &ctx).unwrap();
let expected = Expression::build(datum_expr(tt.3), 0, &ctx).unwrap();
let got = op.eval_real(&ctx, &[]).unwrap();
let exp = expected.eval_real(&ctx, &[]).unwrap();
@@ -301,8 +301,8 @@ mod test {
let lhs = datum_expr(tt.1);
let rhs = datum_expr(tt.2);
let op = Expression::build(fncall_expr(tt.0, &[lhs, rhs]), 0).unwrap();
let expected = Expression::build(datum_expr(tt.3), 0).unwrap();
let op = Expression::build(fncall_expr(tt.0, &[lhs, rhs]), 0, &ctx).unwrap();
let expected = Expression::build(datum_expr(tt.3), 0, &ctx).unwrap();
let got = op.eval_decimal(&ctx, &[]).unwrap();
let exp = expected.eval_decimal(&ctx, &[]).unwrap();
@@ -362,7 +362,7 @@ mod test {
let rus = mysql::has_unsigned_flag(rhs.get_field_type().get_flag());
let unsigned = lus | rus;
let mut op = Expression::build(fncall_expr(tt.0, &[lhs, rhs]), 0).unwrap();
let mut op = Expression::build(fncall_expr(tt.0, &[lhs, rhs]), 0, &ctx).unwrap();
if unsigned {
// According to TiDB, the result is unsigned if any of arguments is unsigned.
op.mut_tp().set_flag(types::UNSIGNED_FLAG as u32);
@@ -397,7 +397,7 @@ mod test {
let lhs = datum_expr(tt.1);
let rhs = datum_expr(tt.2);
let op = Expression::build(fncall_expr(tt.0, &[lhs, rhs]), 0).unwrap();
let op = Expression::build(fncall_expr(tt.0, &[lhs, rhs]), 0, &ctx).unwrap();
let got = op.eval_real(&ctx, &[]).unwrap_err();
assert!(check_overflow(got).is_ok());
}
@@ -236,8 +236,8 @@ mod test {
for (operator, branch1, branch2, exp) in tests {
let arg1 = datum_expr(branch1);
let arg2 = datum_expr(branch2);
let expected = Expression::build(datum_expr(exp), 0).unwrap();
let op = Expression::build(fncall_expr(operator, &[arg1, arg2]), 0).unwrap();
let expected = Expression::build(datum_expr(exp), 0, &ctx).unwrap();
let op = Expression::build(fncall_expr(operator, &[arg1, arg2]), 0, &ctx).unwrap();
match operator {
ScalarFuncSig::IfNullInt => {
let lhs = op.eval_int(&ctx, &[]).unwrap();
@@ -389,8 +389,9 @@ mod test {
let arg1 = datum_expr(cond);
let arg2 = datum_expr(branch1);
let arg3 = datum_expr(branch2);
let expected = Expression::build(datum_expr(exp), 0).unwrap();
let op = Expression::build(fncall_expr(operator, &[arg1, arg2, arg3]), 0).unwrap();
let expected = Expression::build(datum_expr(exp), 0, &ctx).unwrap();
let op =
Expression::build(fncall_expr(operator, &[arg1, arg2, arg3]), 0, &ctx).unwrap();
match operator {
ScalarFuncSig::IfInt => {
let lhs = op.eval_int(&ctx, &[]).unwrap();
@@ -222,13 +222,13 @@ mod test {
let arg1 = datum_expr(lhs);
let arg2 = datum_expr(rhs);
{
let op =
Expression::build(fncall_expr(op, &[arg1.clone(), arg2.clone()]), 0).unwrap();
let op = Expression::build(fncall_expr(op, &[arg1.clone(), arg2.clone()]), 0, &ctx)
.unwrap();
let res = op.eval_int(&ctx, &[]).unwrap();
assert_eq!(res, exp);
}
{
let op = Expression::build(fncall_expr(op, &[arg2, arg1]), 0).unwrap();
let op = Expression::build(fncall_expr(op, &[arg2, arg1]), 0, &ctx).unwrap();
let res = op.eval_int(&ctx, &[]).unwrap();
assert_eq!(res, exp);
}
@@ -276,7 +276,7 @@ mod test {
let ctx = StatementContext::default();
for (op, arg, exp) in tests {
let arg1 = datum_expr(arg);
let op = Expression::build(fncall_expr(op, &[arg1]), 0).unwrap();
let op = Expression::build(fncall_expr(op, &[arg1]), 0, &ctx).unwrap();
let res = op.eval_int(&ctx, &[]).unwrap();
assert_eq!(res, exp);
}
@@ -110,7 +110,7 @@ mod test {
let ctx = StatementContext::default();
for (ii, exp) in expecteds.iter().enumerate().take(row.len()) {
let c = col_expr(ii as i64);
let e = Expression::build(c, row.len()).unwrap();
let e = Expression::build(c, row.len(), &ctx).unwrap();
let i = e.eval_int(&ctx, &row).unwrap_or(None);
let r = e.eval_real(&ctx, &row).unwrap_or(None);
@@ -167,7 +167,7 @@ mod test {
let ctx = StatementContext::default();
for (case, expected) in tests.into_iter().zip(expecteds.into_iter()) {
let e = Expression::build(case, 0).unwrap();
let e = Expression::build(case, 0, &ctx).unwrap();
let i = e.eval_int(&ctx, &[]).unwrap_or(None);
let r = e.eval_real(&ctx, &[]).unwrap_or(None);
@@ -357,7 +357,7 @@ impl Expression {
}
impl Expression {
fn build(mut expr: Expr, row_len: usize) -> Result<Self> {
fn build(mut expr: Expr, row_len: usize, ctx: &StatementContext) -> Result<Self> {
let tp = expr.take_field_type();
match expr.get_tp() {
ExprType::Null => Ok(Expression::new_const(Datum::Null, tp)),
@@ -379,6 +379,15 @@ impl Expression {
.map(Datum::F64)
.map(|e| Expression::new_const(e, tp))
.map_err(Error::from),
ExprType::MysqlTime => expr.get_val()
.decode_u64()
.and_then(|i| {
let fsp = expr.get_field_type().get_decimal() as i8;
let tp = expr.get_field_type().get_tp() as u8;
Time::from_packed_u64(i, tp, fsp, &ctx.tz)
})
.map(|t| Expression::new_const(Datum::Time(t), tp))
.map_err(Error::from),
ExprType::MysqlDuration => expr.get_val()
.decode_i64()
.and_then(|n| Duration::from_nanos(n, MAX_FSP))
@@ -397,7 +406,7 @@ impl Expression {
));
expr.take_children()
.into_iter()
.map(|child| Expression::build(child, row_len))
.map(|child| Expression::build(child, row_len, ctx))
.collect::<Result<Vec<_>>>()
.map(|children| {
Expression::ScalarFn(FnCall {
@@ -424,9 +433,10 @@ impl Expression {
#[cfg(test)]
mod test {
use coprocessor::codec::Datum;
use coprocessor::codec::mysql::{Time, MAX_FSP};
use coprocessor::select::xeval::evaluator::test::{col_expr, datum_expr};
use tipb::expression::{Expr, ExprType, FieldType, ScalarFuncSig};
use super::Expression;
use super::{Expression, StatementContext};
#[inline]
pub fn str2dec(s: &str) -> Datum {
@@ -447,14 +457,18 @@ mod test {
#[test]
fn test_expression_build() {
let colref = col_expr(1);
let constant = datum_expr(Datum::Null);
let const_null = datum_expr(Datum::Null);
let const_time = datum_expr(Datum::Time(
Time::parse_utc_datetime("1970-01-01 12:00:00", MAX_FSP).unwrap(),
));
let tests = vec![
(colref.clone(), 1, false),
(colref.clone(), 2, true),
(constant.clone(), 0, true),
(const_null.clone(), 0, true),
(const_time.clone(), 0, true),
(
fncall_expr(ScalarFuncSig::LTInt, &[colref.clone(), constant.clone()]),
fncall_expr(ScalarFuncSig::LTInt, &[colref.clone(), const_null.clone()]),
2,
true,
),
@@ -465,8 +479,9 @@ mod test {
),
];
let ctx = StatementContext::default();
for tt in tests {
let expr = Expression::build(tt.0, tt.1);
let expr = Expression::build(tt.0, tt.1, &ctx);
assert_eq!(expr.is_ok(), tt.2);
}
}
ProTip! Use n and p to navigate between commits in a pull request.