|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to you under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.calcite.util; |
| 18 | + |
| 19 | +import org.apache.calcite.config.CalciteConnectionConfig; |
| 20 | +import org.apache.calcite.jdbc.CalciteConnection; |
| 21 | +import org.apache.calcite.plan.RelOptPlanner; |
| 22 | +import org.apache.calcite.plan.hep.HepPlanner; |
| 23 | +import org.apache.calcite.plan.visualizer.RuleMatchVisualizer; |
| 24 | +import org.apache.calcite.runtime.Hook; |
| 25 | + |
| 26 | +import java.io.File; |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.Locale; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.concurrent.atomic.AtomicInteger; |
| 31 | +import java.util.function.Consumer; |
| 32 | + |
| 33 | +/** |
| 34 | + * Utility class to enable RuleMatchVisualizer for Calcite connections. |
| 35 | + * |
| 36 | + * <p>This class provides hooks to automatically attach a RuleMatchVisualizer |
| 37 | + * to planners when a connection specifies the ruleVisualizerDir property. |
| 38 | + * |
| 39 | + * <p>Usage in JDBC URL: |
| 40 | + * <blockquote><pre> |
| 41 | + * jdbc:calcite:ruleVisualizerDir=/tmp/calcite-viz |
| 42 | + * </pre></blockquote> |
| 43 | + * |
| 44 | + * <p>Or programmatically: |
| 45 | + * <blockquote><pre> |
| 46 | + * RuleMatchVisualizerHook.enable("/tmp/calcite-viz"); |
| 47 | + * </pre></blockquote> |
| 48 | + */ |
| 49 | +public class RuleMatchVisualizerHook { |
| 50 | + public static final RuleMatchVisualizerHook INSTANCE = new RuleMatchVisualizerHook(); |
| 51 | + |
| 52 | + private final Map<RelOptPlanner, RuleMatchVisualizer> visualizerMap = new HashMap<>(); |
| 53 | + private final AtomicInteger queryCounter = new AtomicInteger(0); |
| 54 | + |
| 55 | + private Hook.Closeable hookCloseable = Hook.Closeable.EMPTY; |
| 56 | + |
| 57 | + /** Private constructor to prevent instantiation. */ |
| 58 | + private RuleMatchVisualizerHook() {} |
| 59 | + |
| 60 | + /** |
| 61 | + * Enables the visualizer for all subsequent queries with the specified output directory. |
| 62 | + * |
| 63 | + * @param outputDir Directory where visualization files will be created |
| 64 | + */ |
| 65 | + public synchronized void enable(String outputDir) { |
| 66 | + hookCloseable.close(); |
| 67 | + |
| 68 | + // Ensure the output directory exists |
| 69 | + File dir = new File(outputDir); |
| 70 | + if (!dir.exists()) { |
| 71 | + boolean madeDir = dir.mkdirs(); |
| 72 | + assert madeDir : "Failed to create directory: " + outputDir; |
| 73 | + } |
| 74 | + |
| 75 | + // Install the hook |
| 76 | + hookCloseable = Hook.PLANNER.addThread((Consumer<RelOptPlanner>) planner -> { |
| 77 | + attachVisualizer(planner, outputDir); |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Enables the visualizer using the connection's configuration. |
| 83 | + * This method checks if the connection has the ruleVisualizerDir property set. |
| 84 | + * |
| 85 | + * @param connection The Calcite connection |
| 86 | + */ |
| 87 | + public synchronized void enableFromConnection(CalciteConnection connection) { |
| 88 | + CalciteConnectionConfig config = connection.config(); |
| 89 | + String vizDir = config.ruleVisualizerDir(); |
| 90 | + |
| 91 | + if (vizDir != null && !vizDir.isEmpty()) { |
| 92 | + enable(vizDir); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Disables the visualizer. |
| 98 | + */ |
| 99 | + public synchronized void disable() { |
| 100 | + hookCloseable.close(); |
| 101 | + |
| 102 | + // Write any pending visualizations |
| 103 | + for (RuleMatchVisualizer viz : visualizerMap.values()) { |
| 104 | + viz.writeToFile(); |
| 105 | + } |
| 106 | + visualizerMap.clear(); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Attaches a visualizer to the given planner. |
| 111 | + */ |
| 112 | + private void attachVisualizer(RelOptPlanner planner, String outputDir) { |
| 113 | + |
| 114 | + // Check if we've already attached a visualizer to this planner |
| 115 | + if (visualizerMap.containsKey(planner)) { |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + int queryNum = queryCounter.incrementAndGet(); |
| 120 | + int queryStart = (int) System.currentTimeMillis() / 1000; |
| 121 | + String suffix = String.format(Locale.ROOT, "query_%d_%d", queryNum, queryStart); |
| 122 | + |
| 123 | + // Create and attach the visualizer |
| 124 | + RuleMatchVisualizer visualizer = new RuleMatchVisualizer(outputDir, suffix); |
| 125 | + visualizer.attachTo(planner); |
| 126 | + visualizerMap.put(planner, visualizer); |
| 127 | + |
| 128 | + // For HepPlanner, we need to manually write the output |
| 129 | + if (planner instanceof HepPlanner) { |
| 130 | + // Add a hook to write the visualization after the planner finishes |
| 131 | + Hook.PLAN_BEFORE_IMPLEMENTATION.addThread(relRoot -> { |
| 132 | + RuleMatchVisualizer viz = visualizerMap.get(planner); |
| 133 | + if (viz != null) { |
| 134 | + viz.writeToFile(); |
| 135 | + visualizerMap.remove(planner); |
| 136 | + } |
| 137 | + }); |
| 138 | + } |
| 139 | + // VolcanoPlanner automatically calls writeToFile() when done |
| 140 | + |
| 141 | + System.out.println("RuleMatchVisualizer enabled: Output will be written to " |
| 142 | + + outputDir + File.separator + suffix + "*"); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Checks the system property and enables visualization if set. |
| 147 | + * This can be called at application startup. |
| 148 | + */ |
| 149 | + public void checkSystemProperty() { |
| 150 | + String vizDir = System.getProperty("calcite.visualizer.dir"); |
| 151 | + if (vizDir != null && !vizDir.isEmpty()) { |
| 152 | + enable(vizDir); |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments