|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with 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, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use crate::error::Result; |
| 19 | +use crate::execution_plans::{ShuffleWriterExec, UnresolvedShuffleExec}; |
| 20 | + |
| 21 | +use datafusion::datasource::physical_plan::{CsvExec, ParquetExec}; |
| 22 | +use datafusion::physical_plan::aggregates::AggregateExec; |
| 23 | +use datafusion::physical_plan::coalesce_batches::CoalesceBatchesExec; |
| 24 | +use datafusion::physical_plan::coalesce_partitions::CoalescePartitionsExec; |
| 25 | +use datafusion::physical_plan::filter::FilterExec; |
| 26 | +use datafusion::physical_plan::joins::HashJoinExec; |
| 27 | +use datafusion::physical_plan::projection::ProjectionExec; |
| 28 | +use datafusion::physical_plan::sorts::sort::SortExec; |
| 29 | +use datafusion::physical_plan::ExecutionPlan; |
| 30 | +use std::fs::File; |
| 31 | +use std::io::{BufWriter, Write}; |
| 32 | +use std::sync::atomic::{AtomicUsize, Ordering}; |
| 33 | +use std::sync::Arc; |
| 34 | + |
| 35 | +pub fn produce_diagram(filename: &str, stages: &[Arc<ShuffleWriterExec>]) -> Result<()> { |
| 36 | + let write_file = File::create(filename)?; |
| 37 | + let mut w = BufWriter::new(&write_file); |
| 38 | + writeln!(w, "digraph G {{")?; |
| 39 | + |
| 40 | + // draw stages and entities |
| 41 | + for stage in stages { |
| 42 | + writeln!(w, "\tsubgraph cluster{} {{", stage.stage_id())?; |
| 43 | + writeln!(w, "\t\tlabel = \"Stage {}\";", stage.stage_id())?; |
| 44 | + let mut id = AtomicUsize::new(0); |
| 45 | + build_exec_plan_diagram( |
| 46 | + &mut w, |
| 47 | + stage.children()[0].as_ref(), |
| 48 | + stage.stage_id(), |
| 49 | + &mut id, |
| 50 | + true, |
| 51 | + )?; |
| 52 | + writeln!(w, "\t}}")?; |
| 53 | + } |
| 54 | + |
| 55 | + // draw relationships |
| 56 | + for stage in stages { |
| 57 | + let mut id = AtomicUsize::new(0); |
| 58 | + build_exec_plan_diagram( |
| 59 | + &mut w, |
| 60 | + stage.children()[0].as_ref(), |
| 61 | + stage.stage_id(), |
| 62 | + &mut id, |
| 63 | + false, |
| 64 | + )?; |
| 65 | + } |
| 66 | + |
| 67 | + write!(w, "}}")?; |
| 68 | + Ok(()) |
| 69 | +} |
| 70 | + |
| 71 | +fn build_exec_plan_diagram( |
| 72 | + w: &mut BufWriter<&File>, |
| 73 | + plan: &dyn ExecutionPlan, |
| 74 | + stage_id: usize, |
| 75 | + id: &mut AtomicUsize, |
| 76 | + draw_entity: bool, |
| 77 | +) -> Result<usize> { |
| 78 | + let operator_str = if plan.as_any().downcast_ref::<AggregateExec>().is_some() { |
| 79 | + "AggregateExec" |
| 80 | + } else if plan.as_any().downcast_ref::<SortExec>().is_some() { |
| 81 | + "SortExec" |
| 82 | + } else if plan.as_any().downcast_ref::<ProjectionExec>().is_some() { |
| 83 | + "ProjectionExec" |
| 84 | + } else if plan.as_any().downcast_ref::<HashJoinExec>().is_some() { |
| 85 | + "HashJoinExec" |
| 86 | + } else if plan.as_any().downcast_ref::<ParquetExec>().is_some() { |
| 87 | + "ParquetExec" |
| 88 | + } else if plan.as_any().downcast_ref::<CsvExec>().is_some() { |
| 89 | + "CsvExec" |
| 90 | + } else if plan.as_any().downcast_ref::<FilterExec>().is_some() { |
| 91 | + "FilterExec" |
| 92 | + } else if plan.as_any().downcast_ref::<ShuffleWriterExec>().is_some() { |
| 93 | + "ShuffleWriterExec" |
| 94 | + } else if plan |
| 95 | + .as_any() |
| 96 | + .downcast_ref::<UnresolvedShuffleExec>() |
| 97 | + .is_some() |
| 98 | + { |
| 99 | + "UnresolvedShuffleExec" |
| 100 | + } else if plan |
| 101 | + .as_any() |
| 102 | + .downcast_ref::<CoalesceBatchesExec>() |
| 103 | + .is_some() |
| 104 | + { |
| 105 | + "CoalesceBatchesExec" |
| 106 | + } else if plan |
| 107 | + .as_any() |
| 108 | + .downcast_ref::<CoalescePartitionsExec>() |
| 109 | + .is_some() |
| 110 | + { |
| 111 | + "CoalescePartitionsExec" |
| 112 | + } else { |
| 113 | + println!("Unknown: {plan:?}"); |
| 114 | + "Unknown" |
| 115 | + }; |
| 116 | + |
| 117 | + let node_id = id.load(Ordering::SeqCst); |
| 118 | + id.store(node_id + 1, Ordering::SeqCst); |
| 119 | + |
| 120 | + if draw_entity { |
| 121 | + writeln!( |
| 122 | + w, |
| 123 | + "\t\tstage_{stage_id}_exec_{node_id} [shape=box, label=\"{operator_str}\"];" |
| 124 | + )?; |
| 125 | + } |
| 126 | + for child in plan.children() { |
| 127 | + if let Some(shuffle) = child.as_any().downcast_ref::<UnresolvedShuffleExec>() { |
| 128 | + if !draw_entity { |
| 129 | + writeln!( |
| 130 | + w, |
| 131 | + "\tstage_{}_exec_1 -> stage_{}_exec_{};", |
| 132 | + shuffle.stage_id, stage_id, node_id |
| 133 | + )?; |
| 134 | + } |
| 135 | + } else { |
| 136 | + // relationships within same entity |
| 137 | + let child_id = |
| 138 | + build_exec_plan_diagram(w, child.as_ref(), stage_id, id, draw_entity)?; |
| 139 | + if draw_entity { |
| 140 | + writeln!( |
| 141 | + w, |
| 142 | + "\t\tstage_{stage_id}_exec_{child_id} -> stage_{stage_id}_exec_{node_id};" |
| 143 | + )?; |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + Ok(node_id) |
| 148 | +} |
0 commit comments