Beispiel #1
0
// Eval is a helper function evaluates expression v and do a panic if evaluating error.
func Eval(v expression.Expression, ctx context.Context, env map[interface{}]interface{}) (y interface{}) {
	var err error
	y, err = v.Eval(ctx, env)
	if err != nil {
		panic(err) // panic ok here
	}
	return
}
Beispiel #2
0
// EvalBoolExpr evaluates an expression and convert its return value to bool.
func EvalBoolExpr(ctx context.Context, expr expression.Expression, m map[interface{}]interface{}) (bool, error) {
	val, err := expr.Eval(ctx, m)
	if err != nil {
		return false, err
	}
	if val == nil {
		return false, nil
	}

	x, err := types.ToBool(val)
	if err != nil {
		return false, err
	}

	return x != 0, nil
}
Beispiel #3
0
func staticExpr(e expression.Expression) (expression.Expression, error) {
	if e.IsStatic() {
		v, err := e.Eval(nil, nil)
		if err != nil {
			return nil, err
		}

		if v == nil {
			return Value{nil}, nil
		}

		return Value{v}, nil
	}

	return e, nil
}
Beispiel #4
0
func (r *WhereRset) planStatic(ctx context.Context, e expression.Expression) (plan.Plan, error) {
	val, err := e.Eval(nil, nil)
	if err != nil {
		return nil, err
	}
	if val == nil {
		// like `select * from t where null`.
		return &plans.NullPlan{Fields: r.Src.GetFields()}, nil
	}

	n, err := types.ToBool(val)
	if err != nil {
		return nil, err
	}
	if n == 0 {
		// like `select * from t where 0`.
		return &plans.NullPlan{Fields: r.Src.GetFields()}, nil
	}

	return &plans.FilterDefaultPlan{Plan: r.Src, Expr: e}, nil
}