vlsi commented on a change in pull request #2552: URL: https://github.com/apache/calcite/pull/2552#discussion_r765160228
########## File path: core/src/main/java/org/apache/calcite/plan/volcano/VolcanoRuleMatchVisualizer.java ########## @@ -0,0 +1,312 @@ +/* + * 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.calcite.plan.volcano; + +import org.apache.calcite.plan.RelOptCost; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.metadata.RelMetadataQuery; +import org.apache.calcite.tools.visualizer.InputExcludedRelWriter; +import org.apache.calcite.tools.visualizer.VisualizerNodeInfo; +import org.apache.calcite.tools.visualizer.VisualizerRuleMatchInfo; +import org.apache.calcite.tools.visualizer.VolcanoRuleMatchVisualizerListener; + +import org.apache.commons.io.IOUtils; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.base.Charsets; + +import java.io.IOException; +import java.io.InputStream; +import java.io.UncheckedIOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Deque; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; +import java.util.TreeSet; +import java.util.function.Consumer; +import java.util.stream.Collectors; + +import static java.util.stream.Collectors.joining; + +/** + * This is tool to visualize the rule match process of the VolcanoPlanner. + * + * + * <p>To use the visualizer, add a listener before the VolcanoPlanner optimization phase. + * Then writes the output to a file after the optimization ends. + * + * <pre> + * // create the visualizer. This attaches a listener to VolcanoPlanner + * VolcanoRuleMatchVisualizer visualizer = VolcanoRuleMatchVisualizer.createAndAttach(planner); + * + * volcanoPlanner.findBestExpr(); + * + * // writes the output to files + * visualizer.writeToFile(outputDirectory, ""); + * </pre> + */ +public class VolcanoRuleMatchVisualizer { + + public static VolcanoRuleMatchVisualizer createAndAttach(VolcanoPlanner volcanoPlanner) { + VolcanoRuleMatchVisualizer vis = new VolcanoRuleMatchVisualizer(volcanoPlanner); + // add listener outside of the constructor to make checker-framework happy + volcanoPlanner.addListener(new VolcanoRuleMatchVisualizerListener(vis)); + return vis; + } + + VolcanoPlanner volcanoPlanner; + + // a sequence of ruleMatch ID to represent the order of rule match + List<String> ruleMatchSequence = new ArrayList<>(); + // map of ruleMatch ID and the info, including the state snapshot at the time of ruleMatch + Map<String, VisualizerRuleMatchInfo> ruleInfoMap = new TreeMap<>(); Review comment: Is there a need for `TreeMap`? `HashMap` is faster and it consumes less memory. If you want consistent iteration order, then prefer `LinkedHashMap`. -- 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]
