thomasrebele commented on a change in pull request #2552: URL: https://github.com/apache/calcite/pull/2552#discussion_r770628227
########## File path: core/src/main/java/org/apache/calcite/plan/visualizer/RuleMatchVisualizer.java ########## @@ -0,0 +1,479 @@ +/* + * 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.visualizer; + +import org.apache.calcite.plan.RelOptCost; +import org.apache.calcite.plan.RelOptListener; +import org.apache.calcite.plan.RelOptPlanner; +import org.apache.calcite.plan.RelOptRuleCall; +import org.apache.calcite.plan.hep.HepRelVertex; +import org.apache.calcite.plan.volcano.RelSubset; +import org.apache.calcite.rel.RelNode; +import org.apache.calcite.rel.metadata.RelMetadataQuery; + +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 org.checkerframework.checker.nullness.qual.Nullable; + +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.text.DecimalFormat; +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * This is a tool to visualize the rule match process of a RelOptPlanner. + * + * <pre> + * // create the visualizer + * RuleMatchVisualizer viz = new RuleMatchVisualizer("/path/to/output/dir", "file-name-suffix"); + * viz.attachTo(planner) + * + * planner.findBestExpr(); + * + * // extra step for HepPlanner: write the output to files + * // a VolcanoPlanner will call it automatically + * viz.writeToFile(); + * </pre> + */ +public class RuleMatchVisualizer implements RelOptListener { + + private static final String INITIAL = "INITIAL"; + private static final String FINAL = "FINAL"; + public static final String DEFAULT_SET = "default"; + + // default HTML template can be edited at + // core/src/main/resources/volcano-viz/viz-template.html + private final String templateDirectory = "volcano-viz"; + private final String outputDirectory; + private final String outputSuffix; + + private String latestRuleID = ""; + private int latestRuleTransformCount = 1; + private boolean initialized = false; + + private @Nullable RelOptPlanner planner = null; Review comment: It is also needed for getting the cost, so I would leave it as it is. -- 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]
