Skip to content

Commit

Permalink
sql: Plumb tree.SemaContext when constructing execbuilder.
Browse files Browse the repository at this point in the history
With the introduction of custom FunctionDefinitionResolver,
callers may want to use custom resolver when building expressions.

Ensure function wrapping logic takes custom resolvers into consideration
by passing tree.SemaContext when constructing execbuilder.

Release notes: None

Release note (<category, see below>): <what> <show> <why>
  • Loading branch information
Yevgeniy Miretskiy committed Nov 16, 2022
1 parent 7c7622f commit 296a06a
Show file tree
Hide file tree
Showing 14 changed files with 23 additions and 15 deletions.
1 change: 0 additions & 1 deletion pkg/ccl/changefeedccl/cdceval/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ go_library(
"//pkg/sql/sem/normalize",
"//pkg/sql/sem/tree",
"//pkg/sql/sem/volatility",
"//pkg/sql/sessiondata",
"//pkg/sql/sessiondatapb",
"//pkg/sql/types",
"//pkg/util/hlc",
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (p *planner) ConstrainPrimaryIndexSpanByExpr(
remainingFilter = tree.DBoolTrue
} else {
eb := execbuilder.New(ctx, newExecFactory(ctx, p), &p.optPlanningCtx.optimizer,
nf.Memo(), &oc, &remaining, evalCtx, false, p.IsANSIDML())
nf.Memo(), &oc, &remaining, evalCtx, semaCtx, false, p.IsANSIDML())
eb.SetBuiltinFuncWrapper(semaCtx.FunctionResolver)
expr, err := eb.BuildScalar()
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/sql/opt/bench/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,7 @@ func (h *harness) runSimple(tb testing.TB, query benchQuery, phase Phase) {
nil, /* catalog */
root,
&h.evalCtx,
&h.semaCtx,
true, /* allowAutoCommit */
stmt.IsANSIDML(),
)
Expand Down Expand Up @@ -767,6 +768,7 @@ func (h *harness) runPrepared(tb testing.TB, phase Phase) {
nil, /* catalog */
root,
&h.evalCtx,
&h.semaCtx,
true, /* allowAutoCommit */
false, /* isANSIDML */
)
Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/opt/exec/execbuilder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type Builder struct {
e opt.Expr
disableTelemetry bool
evalCtx *eval.Context
semaCtx *tree.SemaContext

// subqueries accumulates information about subqueries that are part of scalar
// expressions we built. Each entry is associated with a tree.Subquery
Expand Down Expand Up @@ -206,6 +207,7 @@ func New(
catalog cat.Catalog,
e opt.Expr,
evalCtx *eval.Context,
semaCtx *tree.SemaContext,
allowAutoCommit bool,
isANSIDML bool,
) *Builder {
Expand All @@ -217,6 +219,7 @@ func New(
e: e,
ctx: ctx,
evalCtx: evalCtx,
semaCtx: semaCtx,
allowAutoCommit: allowAutoCommit,
initialAllowAutoCommit: allowAutoCommit,
IsANSIDML: isANSIDML,
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/opt/exec/execbuilder/cascades.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,8 @@ func (cb *cascadeBuilder) planCascade(
}

// 5. Execbuild the optimized expression.
eb := New(ctx, execFactory, &o, factory.Memo(), cb.b.catalog, optimizedExpr, evalCtx, allowAutoCommit,
evalCtx.Planner.IsANSIDML())
eb := New(ctx, execFactory, &o, factory.Memo(), cb.b.catalog, optimizedExpr,
evalCtx, semaCtx, allowAutoCommit, evalCtx.Planner.IsANSIDML())
if bufferRef != nil {
// Set up the With binding.
eb.addBuiltWithExpr(cascadeInputWithID, bufferColMap, bufferRef)
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/opt/exec/execbuilder/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func fmtInterceptor(f *memo.ExprFmtCtx, scalar opt.ScalarExpr) string {
nil, /* catalog */
scalar,
nil, /* evalCtx */
nil, /* semaCtx */
false, /* allowAutoCommit */
false, /* isANSIDML */
)
Expand Down
3 changes: 2 additions & 1 deletion pkg/sql/opt/exec/execbuilder/relational.go
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,8 @@ func (b *Builder) buildApplyJoin(join memo.RelExpr) (execPlan, error) {
return nil, err
}

eb := New(ctx, ef, &o, f.Memo(), b.catalog, newRightSide, b.evalCtx, false /* allowAutoCommit */, b.IsANSIDML)
eb := New(ctx, ef, &o, f.Memo(), b.catalog, newRightSide, b.evalCtx,
b.semaCtx, false /* allowAutoCommit */, b.IsANSIDML)
eb.disableTelemetry = true
eb.withExprs = withExprs
plan, err := eb.Build()
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/opt/exec/execbuilder/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ func (b *Builder) buildExplain(explainExpr *memo.ExplainExpr) (execPlan, error)
ef := explain.NewFactory(gf)

explainBld := New(
b.ctx, ef, b.optimizer, b.mem, b.catalog, explainExpr.Input, b.evalCtx, b.initialAllowAutoCommit,
b.IsANSIDML,
b.ctx, ef, b.optimizer, b.mem, b.catalog, explainExpr.Input, b.evalCtx, b.semaCtx,
b.initialAllowAutoCommit, b.IsANSIDML,
)
explainBld.disableTelemetry = true
plan, err := explainBld.Build()
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/opt/idxconstraint/index_constraints_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestIndexConstraints(t *testing.T) {
if !remainingFilter.IsTrue() {
execBld := execbuilder.New(
context.Background(), nil /* execFactory */, nil /* optimizer */, f.Memo(), nil, /* catalog */
&remainingFilter, &evalCtx, false, /* allowAutoCommit */
&remainingFilter, &evalCtx, &semaCtx, false, /* allowAutoCommit */
false, /* isANSIDML */
)
expr, err := execBld.BuildScalar()
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/opt/lookupjoin/constraint_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func makeFiltersExpr(
func formatScalar(e opt.Expr, f *norm.Factory, evalCtx *eval.Context) string {
execBld := execbuilder.New(
context.Background(), nil /* execFactory */, nil /* optimizer */, f.Memo(), nil, /* catalog */
e, evalCtx, false, /* allowAutoCommit */
e, evalCtx, nil /*semaCtx */, false, /* allowAutoCommit */
false, /* isANSIDML */
)
expr, err := execBld.BuildScalar()
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/opt/partialidx/implicator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func TestImplicator(t *testing.T) {
} else {
execBld := execbuilder.New(
context.Background(), nil /* factory */, nil /* optimizer */, f.Memo(), nil, /* catalog */
&remainingFilters, &evalCtx, false, /* allowAutoCommit */
&remainingFilters, &evalCtx, &semaCtx, false, /* allowAutoCommit */
false, /* isANSIDML */
)
expr, err := execBld.BuildScalar()
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/opt/testutils/opttester/opt_tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -2304,8 +2304,8 @@ func (ot *OptTester) ExecBuild(f exec.Factory, mem *memo.Memo, expr opt.Expr) (e
db := kv.NewDB(log.MakeTestingAmbientCtxWithNewTracer(), factory, clock, stopper)
ot.evalCtx.Txn = kv.NewTxn(context.Background(), db, 1)
}
bld := execbuilder.New(context.Background(), f, ot.makeOptimizer(), mem, ot.catalog, expr, &ot.evalCtx, true,
false, /* isANSIDML */
bld := execbuilder.New(context.Background(), f, ot.makeOptimizer(), mem, ot.catalog, expr,
&ot.evalCtx, &ot.semaCtx, true, false, /* isANSIDML */
)
return bld.Build()
}
6 changes: 4 additions & 2 deletions pkg/sql/plan_opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,8 @@ func (opc *optPlanningCtx) runExecBuilder(
}
var bld *execbuilder.Builder
if !planTop.instrumentation.ShouldBuildExplainPlan() {
bld = execbuilder.New(ctx, f, &opc.optimizer, mem, &opc.catalog, mem.RootExpr(), evalCtx, allowAutoCommit, stmt.IsANSIDML())
bld = execbuilder.New(ctx, f, &opc.optimizer, mem, &opc.catalog, mem.RootExpr(),
evalCtx, &opc.p.semaCtx, allowAutoCommit, stmt.IsANSIDML())
plan, err := bld.Build()
if err != nil {
return err
Expand All @@ -628,7 +629,8 @@ func (opc *optPlanningCtx) runExecBuilder(
} else {
// Create an explain factory and record the explain.Plan.
explainFactory := explain.NewFactory(f)
bld = execbuilder.New(ctx, explainFactory, &opc.optimizer, mem, &opc.catalog, mem.RootExpr(), evalCtx, allowAutoCommit, stmt.IsANSIDML())
bld = execbuilder.New(ctx, explainFactory, &opc.optimizer, mem, &opc.catalog, mem.RootExpr(),
evalCtx, &opc.p.semaCtx, allowAutoCommit, stmt.IsANSIDML())
plan, err := bld.Build()
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/sem/eval/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func optBuildScalar(evalCtx *eval.Context, e tree.Expr) (tree.TypedExpr, error)

bld := execbuilder.New(
ctx, nil /* factory */, &o, o.Memo(), nil /* catalog */, o.Memo().RootExpr(),
evalCtx, false, /* allowAutoCommit */
evalCtx, &semaCtx, false, /* allowAutoCommit */
false, /* isANSIDML */
)
expr, err := bld.BuildScalar()
Expand Down

0 comments on commit 296a06a

Please sign in to comment.