avoid unnecessary evaluation

main
Ziyang Hu 2 years ago
parent dff378c7ac
commit 5fb4a14234

@ -116,44 +116,65 @@ impl<'a> Expr<'a> {
// expr => expr.partial_eval(ctx)?,
// };
// match expr {expr
match arg.partial_eval(ctx)? {
Expr::Const(Value::Null) => Expr::Const(Value::Null),
Expr::Const(Value::Dict(mut d)) => {
Expr::Const(d.remove(&f as &str).unwrap_or(Value::Null))
match *arg {
Expr::Dict(mut d) => {
// This skips evaluation of other keys
d.remove(&f as &str)
.unwrap_or(Expr::Const(Value::Null))
.partial_eval(ctx)?
}
arg => {
match arg.partial_eval(ctx)? {
Expr::Const(Value::Null) => Expr::Const(Value::Null),
Expr::Const(Value::Dict(mut d)) => {
Expr::Const(d.remove(&f as &str).unwrap_or(Value::Null))
}
v @ (Expr::IdxAcc(_, _)
| Expr::FieldAcc(_, _)
| Expr::TableCol(_, _)
| Expr::Apply(_, _)
| Expr::ApplyAgg(_, _, _)) => Expr::FieldAcc(f, v.into()),
Expr::Dict(mut d) => d.remove(&f as &str).unwrap_or(Expr::Const(Value::Null)),
v => return Err(EvalError::FieldAccess(f, Value::from(v).to_static())),
}
}
v @ (Expr::IdxAcc(_, _)
| Expr::FieldAcc(_, _)
| Expr::TableCol(_, _)
| Expr::Apply(_, _)
| Expr::ApplyAgg(_, _, _)) => Expr::FieldAcc(f, v.into()),
Expr::Dict(mut d) => d.remove(&f as &str).unwrap_or(Expr::Const(Value::Null)),
v => return Err(EvalError::FieldAccess(f, Value::from(v).to_static())),
}
}
Expr::IdxAcc(i, arg) => {
let arg = arg.partial_eval(ctx)?;
match arg {
Expr::Const(Value::Null) => Expr::Const(Value::Null),
Expr::Const(Value::List(mut l)) => {
match *arg {
// This skips evaluation of other keys
Expr::List(mut l) => {
if i >= l.len() {
Expr::Const(Value::Null)
} else {
Expr::Const(l.swap_remove(i))
l.swap_remove(i).partial_eval(ctx)?
}
}
Expr::List(mut l) => {
if i >= l.len() {
Expr::Const(Value::Null)
} else {
l.swap_remove(i)
arg => {
match arg.partial_eval(ctx)? {
Expr::Const(Value::Null) => Expr::Const(Value::Null),
Expr::Const(Value::List(mut l)) => {
if i >= l.len() {
Expr::Const(Value::Null)
} else {
Expr::Const(l.swap_remove(i))
}
}
Expr::List(mut l) => {
if i >= l.len() {
Expr::Const(Value::Null)
} else {
l.swap_remove(i)
}
}
v @ (Expr::IdxAcc(_, _)
| Expr::FieldAcc(_, _)
| Expr::TableCol(_, _)
| Expr::Apply(_, _)
| Expr::ApplyAgg(_, _, _)) => Expr::IdxAcc(i, v.into()),
v => return Err(EvalError::IndexAccess(i, Value::from(v).to_static())),
}
}
v @ (Expr::IdxAcc(_, _)
| Expr::FieldAcc(_, _)
| Expr::TableCol(_, _)
| Expr::Apply(_, _)
| Expr::ApplyAgg(_, _, _)) => Expr::IdxAcc(i, v.into()),
v => return Err(EvalError::IndexAccess(i, Value::from(v).to_static())),
}
}
Expr::Apply(op, args) => {

Loading…
Cancel
Save