gortiz commented on code in PR #18387: URL: https://github.com/apache/pinot/pull/18387#discussion_r3275223227
########## pinot-query-planner/src/main/java/org/apache/pinot/query/planner/rules/PinotRuleSet.java: ########## @@ -0,0 +1,100 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.pinot.query.planner.rules; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.EnumMap; +import java.util.List; +import java.util.Map; +import java.util.ServiceLoader; +import org.apache.calcite.plan.RelOptRule; + + +/// Owns the multi-stage planner's per-phase Calcite rule lists. Constructed +/// once at broker startup; lists are immutable for the process lifetime. +/// +/// Construction sequence: +/// +/// 1. Allocate an empty mutable list per [Phase]. +/// 2. For every [RuleSetCustomizer] (in supplied order), call +/// `customize(phase, list)` once per phase. The OSS defaults are themselves +/// contributed by [DefaultRuleSetCustomizer] which is registered as a +/// `ServiceLoader` entry; plugin customizers run after and observe a list +/// pre-populated with the OSS defaults. +/// 3. Defensively copy each per-phase list to an immutable [List] and freeze +/// the map. +/// +/// `QueryEnvironment` reads `rulesFor(phase)` and applies per-query +/// `usePlannerRules` / `skipPlannerRules` filters on top. +public final class PinotRuleSet { + + private final Map<Phase, List<RelOptRule>> _rulesByPhase; + + /// Builds a rule set from the supplied customizers. Customizers run in the + /// order of the iterable; the framework freezes the per-phase lists after + /// every customizer has run. + public PinotRuleSet(Iterable<RuleSetCustomizer> customizers) { + EnumMap<Phase, List<RelOptRule>> mutable = new EnumMap<>(Phase.class); + for (Phase phase : Phase.values()) { + mutable.put(phase, new ArrayList<>()); + } + for (RuleSetCustomizer customizer : customizers) { + for (Phase phase : Phase.values()) { + customizer.customize(phase, mutable.get(phase)); + } + } + EnumMap<Phase, List<RelOptRule>> frozen = new EnumMap<>(Phase.class); + for (Map.Entry<Phase, List<RelOptRule>> entry : mutable.entrySet()) { + frozen.put(entry.getKey(), List.copyOf(entry.getValue())); + } + _rulesByPhase = Collections.unmodifiableMap(frozen); + } + + /// Discovers every [RuleSetCustomizer] via [ServiceLoader] and builds a + /// rule set from them. Used by [#defaultInstance()] and by callers that + /// don't have an externally-managed customizer list. + public static PinotRuleSet loadFromServiceLoader() { Review Comment: Fixed in `PinotRuleSet.loadFromServiceLoader()`. Discovery now runs in two passes: 1. **Context classloader** — `ServiceLoader.load(RuleSetCustomizer.class)` picks up `DefaultRuleSetCustomizer` and anything bundled in the broker classpath. 2. **Plugin classloaders** — iterates `PluginManager.get().getPluginClassLoaders()` and runs a second `ServiceLoader.load(RuleSetCustomizer.class, pluginClassLoader)` for each registered plugin realm, picking up any `META-INF/services` entries in plugin JARs. A class-name dedup set prevents double-registration in case a fat-jar and a plugin realm both see the same services file. On the class-visibility side, `PluginManager` now explicitly exports `org.apache.pinot.query.planner.spi` (and `org.apache.calcite.plan` for `RelOptRule`) into every plugin realm's import list, so plugin JARs can implement `RuleSetCustomizer` without bundling or shading those classes. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
