From 1883c22f778b14bfac8c6b0a4fdfafe6d74d8bf6 Mon Sep 17 00:00:00 2001 From: Marcus Gartner Date: Wed, 10 Jan 2024 16:58:17 -0500 Subject: [PATCH] execinfrapb: refactor DeserializeExpr and RunFilter This commit moves two functions that were tangled up in method definitions of a struct. Release note: None --- pkg/sql/execinfrapb/expr.go | 80 ++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/pkg/sql/execinfrapb/expr.go b/pkg/sql/execinfrapb/expr.go index d18976a0da7e..48d2cd1cb487 100644 --- a/pkg/sql/execinfrapb/expr.go +++ b/pkg/sql/execinfrapb/expr.go @@ -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. // @@ -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, @@ -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): //