Jackie-Jiang commented on code in PR #15192: URL: https://github.com/apache/pinot/pull/15192#discussion_r1980514148
########## pinot-query-planner/src/main/java/org/apache/pinot/query/context/RuleTimingPlannerListener.java: ########## @@ -0,0 +1,93 @@ +package org.apache.pinot.query.context; + +/** + * 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. + */ + +import java.util.HashMap; +import java.util.Map; +import org.apache.calcite.plan.RelOptListener; +import org.apache.calcite.plan.RelOptRule; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class RuleTimingPlannerListener implements RelOptListener { + private static final Logger LOGGER = LoggerFactory.getLogger(RuleTimingPlannerListener.class); + public static final String RULE_TIMINGS = "RULE_TIMINGS"; + + private final PlannerContext _plannerContext; + private final Map<RelOptRule, Long> _ruleStartTimes = new HashMap<>(); + private final Map<RelOptRule, Long> _ruleDurations = new HashMap<>(); + + public RuleTimingPlannerListener(PlannerContext plannerContext) { + _plannerContext = plannerContext; + } + + @Override + public void ruleAttempted(RuleAttemptedEvent event) { + // Capture start time when a rule is attempted + if (event.isBefore()) { + _ruleStartTimes.put(event.getRuleCall().getRule(), System.nanoTime()); + } else { + if (_ruleStartTimes.containsKey(event.getRuleCall().getRule())) { + long duration = System.nanoTime() - _ruleStartTimes.get(event.getRuleCall().getRule()); + _ruleDurations.put(event.getRuleCall().getRule(), + _ruleDurations.getOrDefault(event.getRuleCall().getRule(), 0L) + duration); + } + } + } + + @Override + Review Comment: (minor) Remove empty lines ########## pinot-query-planner/src/main/java/org/apache/pinot/query/QueryEnvironment.java: ########## @@ -249,16 +253,26 @@ public QueryPlannerResult explainQuery(String sqlQuery, SqlNodeAndOptions sqlNod RelNode explainedNode = MultiStageExplainAskingServersUtils.modifyRel(relRoot.rel, dispatchableSubPlan.getQueryStages(), nodeTracker, serversExplainer); - String explainStr = PlannerUtils.explainPlan(explainedNode, format, level); - - return new QueryPlannerResult(null, explainStr, dispatchableSubPlan.getTableNames()); + return getQueryPlannerResult(plannerContext, dispatchableSubPlan, + PlannerUtils.explainPlan(explainedNode, format, level), dispatchableSubPlan.getTableNames()); } } } catch (Exception e) { throw new RuntimeException("Error explain query plan for: " + sqlQuery, e); } } + @NotNull Review Comment: (minor) Let's not add `NotNull` annotation, and treat everything as non-null if not annotated ########## pinot-query-planner/src/main/java/org/apache/pinot/query/QueryEnvironment.java: ########## @@ -485,7 +515,8 @@ public boolean useSpools(Map<String, String> options) { public interface Config { String getDatabase(); - @Nullable // In theory nullable only in tests. We should fix LiteralOnlyBrokerRequestTest to not need this. + @Nullable + // In theory nullable only in tests. We should fix LiteralOnlyBrokerRequestTest to not need this. Review Comment: (minor) Revert? ########## pinot-query-planner/src/main/java/org/apache/pinot/query/QueryEnvironment.java: ########## @@ -391,7 +411,17 @@ private RelNode optimize(RelRoot relRoot, PlannerContext plannerContext) { try { RelOptPlanner optPlanner = plannerContext.getRelOptPlanner(); optPlanner.setRoot(relRoot.rel); + RuleTimingPlannerListener listener = null; + if (plannerContext.getOptions() + .containsKey(CommonConstants.Broker.Request.QueryOptionKey.PROFILE_RULE_OPTIMIZER)) { Review Comment: (minor) Extract this into `QueryOptionsUtils`. `containsKey()` is not enough. We should check the value using `Boolean.parseBoolean()` ########## pinot-query-planner/src/main/java/org/apache/pinot/query/context/RuleTimingPlannerListener.java: ########## @@ -0,0 +1,93 @@ +package org.apache.pinot.query.context; + +/** + * 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. + */ + +import java.util.HashMap; +import java.util.Map; +import org.apache.calcite.plan.RelOptListener; +import org.apache.calcite.plan.RelOptRule; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class RuleTimingPlannerListener implements RelOptListener { + private static final Logger LOGGER = LoggerFactory.getLogger(RuleTimingPlannerListener.class); + public static final String RULE_TIMINGS = "RULE_TIMINGS"; + + private final PlannerContext _plannerContext; + private final Map<RelOptRule, Long> _ruleStartTimes = new HashMap<>(); + private final Map<RelOptRule, Long> _ruleDurations = new HashMap<>(); + + public RuleTimingPlannerListener(PlannerContext plannerContext) { + _plannerContext = plannerContext; + } + + @Override + public void ruleAttempted(RuleAttemptedEvent event) { + // Capture start time when a rule is attempted + if (event.isBefore()) { + _ruleStartTimes.put(event.getRuleCall().getRule(), System.nanoTime()); + } else { + if (_ruleStartTimes.containsKey(event.getRuleCall().getRule())) { + long duration = System.nanoTime() - _ruleStartTimes.get(event.getRuleCall().getRule()); + _ruleDurations.put(event.getRuleCall().getRule(), + _ruleDurations.getOrDefault(event.getRuleCall().getRule(), 0L) + duration); + } + } + } + + @Override + + public void ruleProductionSucceeded(RuleProductionEvent event) { + } + + @Override + + public void relEquivalenceFound(RelEquivalenceEvent event) { + /* Not used */ + } + + @Override + + public void relDiscarded(RelDiscardedEvent event) { + /* Not used */ + } + + @Override + + public void relChosen(RelChosenEvent event) { + /* Not used */ + } + + public void printRuleTimings() { + String ruleTimings = getRuleTimings(); + LOGGER.info(ruleTimings); + _plannerContext.getOptions().put(RULE_TIMINGS, ruleTimings); Review Comment: Don't put it into the query options. This will be propagated to the servers -- 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]
