Skip to content

Commit

Permalink
execinfrapb: refactor DeserializeExpr and RunFilter
Browse files Browse the repository at this point in the history
This commit moves two functions that were tangled up in method
definitions of a struct.

Release note: None
  • Loading branch information
mgartner committed Jan 10, 2024
1 parent 4168612 commit 1883c22
Showing 1 changed file with 40 additions and 40 deletions.
80 changes: 40 additions & 40 deletions pkg/sql/execinfrapb/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,46 @@ func (v *ivarBinder) VisitPre(expr tree.Expr) (recurse bool, newExpr tree.Expr)

func (*ivarBinder) VisitPost(expr tree.Expr) tree.Expr { return expr }

// DeserializeExpr deserializes expr, binds the indexed variables to the
// provided IndexedVarHelper, and evaluates any constants in the expression.
//
// evalCtx will not be mutated.
func DeserializeExpr(
ctx context.Context,
expr string,
semaCtx *tree.SemaContext,
evalCtx *eval.Context,
vars *tree.IndexedVarHelper,
) (tree.TypedExpr, error) {
if expr == "" {
return nil, nil
}

deserializedExpr, err := processExpression(ctx, Expression{Expr: expr}, evalCtx, semaCtx, vars)
if err != nil {
return deserializedExpr, err
}
var t transform.ExprTransformContext
if t.AggregateInExpr(ctx, deserializedExpr, evalCtx.SessionData().SearchPath) {
return nil, errors.Errorf("expression '%s' has aggregate", deserializedExpr)
}
return deserializedExpr, nil
}

// RunFilter runs a filter expression and returns whether the filter passes.
func RunFilter(ctx context.Context, filter tree.TypedExpr, evalCtx *eval.Context) (bool, error) {
if filter == nil {
return true, nil
}

d, err := eval.Expr(ctx, evalCtx, filter)
if err != nil {
return false, err
}

return d == tree.DBoolTrue, nil
}

// processExpression parses the string expression inside an Expression,
// and associates ordinal references (@1, @2, etc) with the given helper.
//
Expand Down Expand Up @@ -270,32 +310,6 @@ func (eh *exprHelper) IndexedVarNodeFormatter(idx int) tree.NodeFormatter {
return &n
}

// DeserializeExpr deserializes expr, binds the indexed variables to the
// provided IndexedVarHelper, and evaluates any constants in the expression.
//
// evalCtx will not be mutated.
func DeserializeExpr(
ctx context.Context,
expr string,
semaCtx *tree.SemaContext,
evalCtx *eval.Context,
vars *tree.IndexedVarHelper,
) (tree.TypedExpr, error) {
if expr == "" {
return nil, nil
}

deserializedExpr, err := processExpression(ctx, Expression{Expr: expr}, evalCtx, semaCtx, vars)
if err != nil {
return deserializedExpr, err
}
var t transform.ExprTransformContext
if t.AggregateInExpr(ctx, deserializedExpr, evalCtx.SessionData().SearchPath) {
return nil, errors.Errorf("expression '%s' has aggregate", deserializedExpr)
}
return deserializedExpr, nil
}

// Init initializes the exprHelper.
func (eh *exprHelper) init(
ctx context.Context, types []*types.T, semaCtx *tree.SemaContext, evalCtx *eval.Context,
Expand Down Expand Up @@ -338,20 +352,6 @@ func (eh *exprHelper) evalFilter(
return pass, err
}

// RunFilter runs a filter expression and returns whether the filter passes.
func RunFilter(ctx context.Context, filter tree.TypedExpr, evalCtx *eval.Context) (bool, error) {
if filter == nil {
return true, nil
}

d, err := eval.Expr(ctx, evalCtx, filter)
if err != nil {
return false, err
}

return d == tree.DBoolTrue, nil
}

// eval - given an expression and a row - evaluates the wrapped expression and
// returns the resulting datum. For example, given a row (1, 2, 3, 4, 5):
//
Expand Down

0 comments on commit 1883c22

Please sign in to comment.