Skip to content

Commit

Permalink
execinfrapb: unexport fields of exprHelper
Browse files Browse the repository at this point in the history
This commit tightens up the `ExprHelper` API by unexporting all exported
fields of `exprHelper`, which is embedded in `ExprHelper`.

Release note: None
  • Loading branch information
mgartner committed Jan 10, 2024
1 parent 1883c22 commit c76880c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 deletions.
5 changes: 1 addition & 4 deletions pkg/sql/colexec/colexecargs/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ func NewExprHelper() *ExprHelper {
// vectorized engine.
type ExprHelper struct {
SemaCtx *tree.SemaContext
helper execinfrapb.ExprHelper
}

// ProcessExpr processes the given expression and returns a well-typed
Expand All @@ -48,7 +47,5 @@ func (h *ExprHelper) ProcessExpr(
if expr.LocalExpr != nil {
return expr.LocalExpr, nil
}
h.helper.Types = typs
tempVars := tree.MakeIndexedVarHelper(&h.helper, len(typs))
return execinfrapb.DeserializeExpr(ctx, expr.Expr, h.SemaCtx, evalCtx, &tempVars)
return execinfrapb.DeserializeExprWithTypes(ctx, expr.Expr, typs, h.SemaCtx, evalCtx)
}
49 changes: 33 additions & 16 deletions pkg/sql/execinfrapb/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,24 @@ func DeserializeExpr(
return deserializedExpr, nil
}

// DeserializeExprWithTypes is similar to Deserialize. It can be used when the
// types of indexed vars are available, but a tree.IndexedVarHelper is not.
func DeserializeExprWithTypes(
ctx context.Context,
expr string,
typs []*types.T,
semaCtx *tree.SemaContext,
evalCtx *eval.Context,
) (tree.TypedExpr, error) {
if expr == "" {
return nil, nil
}
var eh exprHelper
eh.types = typs
tempVars := tree.MakeIndexedVarHelper(&eh, len(typs))
return DeserializeExpr(ctx, expr, semaCtx, evalCtx, &tempVars)
}

// 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 {
Expand Down Expand Up @@ -243,7 +261,7 @@ func (eh *MultiExprHelper) AddExpr(ctx context.Context, expr Expression, i int)
// TODO(mgartner): This is only used in projectSetProcessor and ideally it could
// be removed.
func (eh *MultiExprHelper) SetRow(row rowenc.EncDatumRow) {
eh.h.Row = row
eh.h.row = row
}

// EvalExpr evaluates the i-th expression with the given row and returns the
Expand Down Expand Up @@ -273,15 +291,15 @@ func (eh *MultiExprHelper) Reset() {
type exprHelper struct {
_ util.NoCopy

// Vars is used to generate IndexedVars that are "backed" by the values in
// `Row`.
Vars tree.IndexedVarHelper
// vars is used to generate IndexedVars that are "backed" by the values in
// row.
vars tree.IndexedVarHelper

evalCtx *eval.Context
semaCtx *tree.SemaContext

Types []*types.T
Row rowenc.EncDatumRow
types []*types.T
row rowenc.EncDatumRow
datumAlloc tree.DatumAlloc
}

Expand All @@ -290,18 +308,18 @@ var _ eval.IndexedVarContainer = &exprHelper{}

// IndexedVarResolvedType is part of the tree.IndexedVarContainer interface.
func (eh *exprHelper) IndexedVarResolvedType(idx int) *types.T {
return eh.Types[idx]
return eh.types[idx]
}

// IndexedVarEval is part of the eval.IndexedVarContainer interface.
func (eh *exprHelper) IndexedVarEval(
ctx context.Context, idx int, e tree.ExprEvaluator,
) (tree.Datum, error) {
err := eh.Row[idx].EnsureDecoded(eh.Types[idx], &eh.datumAlloc)
err := eh.row[idx].EnsureDecoded(eh.types[idx], &eh.datumAlloc)
if err != nil {
return nil, err
}
return eh.Row[idx].Datum.Eval(ctx, e)
return eh.row[idx].Datum.Eval(ctx, e)
}

// IndexedVarNodeFormatter is part of the tree.IndexedVarContainer interface.
Expand All @@ -316,8 +334,8 @@ func (eh *exprHelper) init(
) error {
eh.evalCtx = evalCtx
eh.semaCtx = semaCtx
eh.Types = types
eh.Vars = tree.MakeIndexedVarHelper(eh, len(types))
eh.types = types
eh.vars = tree.MakeIndexedVarHelper(eh, len(types))
if semaCtx.TypeResolver != nil {
for _, t := range types {
if err := typedesc.EnsureTypeIsHydrated(ctx, t, semaCtx.TypeResolver.(catalog.TypeDescriptorResolver)); err != nil {
Expand All @@ -334,18 +352,18 @@ func (eh *exprHelper) prepareExpr(ctx context.Context, expr Expression) (tree.Ty
return nil, nil
}
if expr.LocalExpr != nil {
eh.Vars.Rebind(expr.LocalExpr)
eh.vars.Rebind(expr.LocalExpr)
return expr.LocalExpr, nil
}
return DeserializeExpr(ctx, expr.Expr, eh.semaCtx, eh.evalCtx, &eh.Vars)
return DeserializeExpr(ctx, expr.Expr, eh.semaCtx, eh.evalCtx, &eh.vars)
}

// evalFilter is used for filter expressions; it evaluates the expression and
// returns whether the filter passes.
func (eh *exprHelper) evalFilter(
ctx context.Context, expr tree.TypedExpr, row rowenc.EncDatumRow,
) (bool, error) {
eh.Row = row
eh.row = row
eh.evalCtx.PushIVarContainer(eh)
pass, err := RunFilter(ctx, expr, eh.evalCtx)
eh.evalCtx.PopIVarContainer()
Expand All @@ -362,8 +380,7 @@ func (eh *exprHelper) evalFilter(
func (eh *exprHelper) eval(
ctx context.Context, expr tree.TypedExpr, row rowenc.EncDatumRow,
) (tree.Datum, error) {
eh.Row = row

eh.row = row
eh.evalCtx.PushIVarContainer(eh)
d, err := eval.Expr(ctx, eh.evalCtx, expr)
eh.evalCtx.PopIVarContainer()
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/sem/tree/indexed_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"github.com/cockroachdb/redact"
)

// IndexedVarContainer provides the implementation of TypeCheck, Eval, and
// String for IndexedVars.
// IndexedVarContainer provides the implementation of TypeCheck and formatting
// for IndexedVars.
type IndexedVarContainer interface {
IndexedVarResolvedType(idx int) *types.T
// IndexedVarNodeFormatter returns a NodeFormatter; if an object that
Expand Down

0 comments on commit c76880c

Please sign in to comment.