From 8f6b3b4c72419d2aed97b4be91dfa79c6611bd6f Mon Sep 17 00:00:00 2001 From: Gonzalo Ortiz <1913993+gortiz@users.noreply.github.com> Date: Fri, 22 May 2026 14:20:32 +0200 Subject: [PATCH] Make PlannerContext implement Calcite Context for rule-time access PlannerContext now implements org.apache.calcite.plan.Context and passes itself as the context to both the opt planner (LogicalPlanner backed by optProgram) and the trait planner (LogicalPlanner backed by traitProgram). This allows Calcite rules contributed via the new RuleSetCustomizer SPI (see #18387) to read per-query options at match time without reflection: PlannerContext ctx = call.getPlanner().getContext().unwrap(PlannerContext.class); String runtime = ctx.getOptions().get("workerRuntime"); unwrap() also delegates to _envConfig, so any rule that currently unwraps QueryEnvironment.Config continues to work unchanged. A package-private constructor and a public forTesting() factory are added so unit tests can construct a PlannerContext without going through QueryEnvironment (which requires a full broker setup). --- .../pinot/query/context/PlannerContext.java | 65 +++++++++++++-- .../query/context/PlannerContextTest.java | 79 +++++++++++++++++++ 2 files changed, 136 insertions(+), 8 deletions(-) create mode 100644 pinot-query-planner/src/test/java/org/apache/pinot/query/context/PlannerContextTest.java diff --git a/pinot-query-planner/src/main/java/org/apache/pinot/query/context/PlannerContext.java b/pinot-query-planner/src/main/java/org/apache/pinot/query/context/PlannerContext.java index 6e5abd8f660d..5bfdfc7d2483 100644 --- a/pinot-query-planner/src/main/java/org/apache/pinot/query/context/PlannerContext.java +++ b/pinot-query-planner/src/main/java/org/apache/pinot/query/context/PlannerContext.java @@ -18,13 +18,15 @@ */ package org.apache.pinot.query.context; +import com.google.common.annotations.VisibleForTesting; import java.util.Collections; import java.util.HashMap; import java.util.Map; import javax.annotation.Nullable; -import org.apache.calcite.plan.Contexts; +import org.apache.calcite.plan.Context; import org.apache.calcite.plan.RelOptPlanner; import org.apache.calcite.plan.hep.HepProgram; +import org.apache.calcite.plan.hep.HepProgramBuilder; import org.apache.calcite.prepare.PlannerImpl; import org.apache.calcite.prepare.Prepare; import org.apache.calcite.rel.RelDistributionTraitDef; @@ -38,12 +40,16 @@ /** - * PlannerContext is an object that holds all contextual information during planning phase. + * Holds all per-query contextual information used during the planning phase. * - * TODO: currently we don't support option or query rewrite. - * It is used to hold per query context for query planning, which cannot be shared across queries. + *
This class implements {@link Context} so that Calcite rules can retrieve it directly from the + * planner: {@code call.getPlanner().getContext().unwrap(PlannerContext.class)}. Both the opt planner + * and the trait planner expose this instance as their context. + * + *
Callers may also unwrap {@link QueryEnvironment.Config} to access broker-wide defaults:
+ * {@code call.getPlanner().getContext().unwrap(QueryEnvironment.Config.class)}.
*/
-public class PlannerContext implements AutoCloseable {
+public class PlannerContext implements AutoCloseable, Context {
private final PlannerImpl _planner;
private final SqlValidator _validator;
@@ -63,16 +69,42 @@ public PlannerContext(FrameworkConfig config, Prepare.CatalogReader catalogReade
SqlExplainFormat sqlExplainFormat, @Nullable PhysicalPlannerContext physicalPlannerContext) {
_planner = new PlannerImpl(config);
_validator = new Validator(config.getOperatorTable(), catalogReader, typeFactory);
- _relOptPlanner = new LogicalPlanner(optProgram, Contexts.EMPTY_CONTEXT, config.getTraitDefs());
- _relTraitPlanner = new LogicalPlanner(traitProgram, Contexts.of(envConfig),
- Collections.singletonList(RelDistributionTraitDef.INSTANCE));
_options = options;
_envConfig = envConfig;
+ _relOptPlanner = new LogicalPlanner(optProgram, this, config.getTraitDefs());
+ _relTraitPlanner = new LogicalPlanner(traitProgram, this,
+ Collections.singletonList(RelDistributionTraitDef.INSTANCE));
_plannerOutput = new HashMap<>();
_sqlExplainFormat = sqlExplainFormat;
_physicalPlannerContext = physicalPlannerContext;
}
+ /**
+ * Test factory: creates a minimal {@link PlannerContext} without going through
+ * {@link org.apache.pinot.query.QueryEnvironment}, suitable for unit tests.
+ */
+ @VisibleForTesting
+ public static PlannerContext forTesting(Map