1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

//! Generates a graphviz graph from a Query Graph Model.
//!
//! The public interface consists of the [`Model::as_dot`] method.

use crate::query_model::model::{
    BoxId, BoxType, ColumnReference, Quantifier, QuantifierId, QueryBox,
};
use crate::query_model::Model;
use itertools::Itertools;
use mz_ore::str::separated;
use mz_repr::explain_new::ExprHumanizer;
use std::collections::{BTreeMap, HashSet};
use std::fmt::{self, Write};

use super::attribute::core::{Attribute, RequiredAttributes};
use super::attribute::relation_type::RelationType;

impl Model {
    pub fn as_dot<'a>(
        &mut self,
        label: &str,
        expr_humanizer: &'a dyn ExprHumanizer,
        with_types: bool,
    ) -> Result<String, anyhow::Error> {
        DotGenerator::new(expr_humanizer, with_types).generate(self, label)
    }
}

/// Generates a graphviz graph from a Query Graph Model, defined in the DOT language.
/// See <https://graphviz.org/doc/info/lang.html>.
#[derive(Debug)]
struct DotGenerator<'a> {
    output: String,
    indent: u32,
    expr_humanizer: &'a dyn ExprHumanizer,
    with_types: bool,
}

/// Generates a label for a graphviz graph.
#[derive(Debug)]
enum DotLabel<'a> {
    /// Plain label
    SingleRow(&'a str),
    /// A single-column table that has a row for each string in the array.
    MultiRow(&'a [String]),
}

/// Generates a string that escapes characters that would problematic inside the
/// specification for a label.
/// The set of escaped characters is "|{}.
struct DotLabelEscapedString<'a>(&'a str);

impl<'a> DotGenerator<'a> {
    fn new(expr_humanizer: &'a dyn ExprHumanizer, with_types: bool) -> Self {
        Self {
            output: String::new(),
            indent: 0,
            expr_humanizer,
            with_types,
        }
    }

    /// Derive attributes required for rendering the given [`Model`].
    fn derive_required_attributes(&self, model: &mut Model, start_box: BoxId) {
        // collect a set of required attributes for rendering
        let mut attributes = HashSet::<Box<dyn Attribute>>::new();
        if self.with_types {
            attributes.insert(Box::new(RelationType));
        }
        // derive the required derived attributes
        RequiredAttributes::from(attributes).derive(model, start_box);
    }

    /// Generates a graphviz graph for the given model, labeled with `label`.
    fn generate(self, model: &mut Model, label: &str) -> Result<String, anyhow::Error> {
        self.generate_subgraph(model, model.top_box, label)
    }

    /// Generates a graphviz graph for the given subgraph of the model, labeled with `label`.
    fn generate_subgraph(
        mut self,
        model: &mut Model,
        start_box: BoxId,
        label: &str,
    ) -> Result<String, anyhow::Error> {
        self.derive_required_attributes(model, start_box);

        self.new_line("digraph G {");
        self.inc();
        self.new_line("compound = true");
        self.new_line("labeljust = l");
        self.new_line(&DotLabel::SingleRow(label.trim()).to_string());
        self.new_line("node [ shape = box ]");

        // list of quantifiers for adding the edges connecting them to their
        // input boxes after all boxes have been processed
        let mut quantifiers = Vec::new();

        model
            .try_visit_pre_post_descendants(
                &mut |m, box_id| -> Result<(), ()> {
                    let b = m.get_box(*box_id);
                    self.new_line(&format!("subgraph cluster{} {{", box_id));
                    self.inc();
                    self.new_line(
                        &DotLabel::SingleRow(&format!("Box{}:{}", box_id, Self::get_box_title(&b)))
                            .to_string(),
                    );
                    self.new_line(&format!(
                        "boxhead{} [ shape = record, {} ]",
                        box_id,
                        self.get_box_head(&b)
                    ));

                    self.new_line("{");
                    self.inc();
                    self.new_line("rank = same");

                    if b.input_quantifiers().count() > 0 {
                        self.new_line("node [ shape = circle ]");
                    }

                    for q in b.input_quantifiers() {
                        quantifiers.push(q.id);

                        self.new_line(&format!(
                            "Q{0} [ {1} ]",
                            q.id,
                            DotLabel::SingleRow(&format!(
                                "Q{0}({1}){2}",
                                q.id,
                                q.quantifier_type,
                                Self::get_quantifier_alias(&q)
                            ))
                        ));
                    }

                    self.add_correlation_info(b.correlation_info());

                    self.dec();
                    self.new_line("}");
                    self.dec();
                    self.new_line("}");

                    Ok(())
                },
                &mut |_, _| Ok(()),
                start_box,
            )
            .unwrap();

        if quantifiers.len() > 0 {
            self.new_line("edge [ arrowhead = none, style = dashed ]");
            for q_id in quantifiers.iter() {
                let q = model.get_quantifier(*q_id);
                self.new_line(&format!(
                    "Q{0} -> boxhead{1} [ lhead = cluster{1} ]",
                    q_id, q.input_box
                ));
            }
        }

        self.dec();
        self.new_line("}");
        self.new_line(""); // final empty line
        Ok(self.output)
    }

    fn get_box_title(b: &QueryBox) -> &'static str {
        b.box_type.get_box_type_str()
    }

    fn get_box_head(&self, b: &QueryBox) -> String {
        let mut rows = Vec::new();

        rows.push(format!("Distinct: {:?}", b.distinct));

        // The projection of the box
        if self.with_types {
            let relation_type = b.attributes.get::<RelationType>();
            for (i, c) in b.columns.iter().enumerate() {
                let typ = self.expr_humanizer.humanize_column_type(&relation_type[i]);
                if let Some(alias) = &c.alias {
                    rows.push(format!("{}: {} ({}) as {}", i, c.expr, typ, alias.as_str()));
                } else {
                    rows.push(format!("{}: {} ({})", i, c.expr, typ));
                }
            }
        } else {
            // The projection of the box
            for (i, c) in b.columns.iter().enumerate() {
                if let Some(alias) = &c.alias {
                    rows.push(format!("{}: {} as {}", i, c.expr, alias.as_str()));
                } else {
                    rows.push(format!("{}: {}", i, c.expr));
                }
            }
        }

        // Per-type internal properties.
        match &b.box_type {
            BoxType::Select(select) => {
                if let Some(order_key) = &select.order_key {
                    rows.push(format!("ORDER BY: {}", separated(", ", order_key.iter())))
                }
            }
            BoxType::Grouping(grouping) => {
                if !grouping.key.is_empty() {
                    rows.push(format!(
                        "GROUP BY: {}",
                        separated(", ", grouping.key.iter())
                    ))
                }
            }
            BoxType::Values(values) => {
                for row in values.rows.iter() {
                    rows.push(format!("ROW: {}", separated(", ", row.iter())))
                }
            }
            BoxType::CallTable(call_table) => {
                // display function call
                let func = &call_table.func;
                let args = &call_table.exprs;
                rows.push(format!("CALL: {}({})", func, separated(", ", args)));
            }
            _ => {}
        }

        // @todo predicates as arrows
        if let Some(predicates) = match &b.box_type {
            BoxType::Select(select) => Some(&select.predicates),
            BoxType::OuterJoin(outer_join) => Some(&outer_join.predicates),
            _ => None,
        } {
            rows.extend(predicates.iter().map(|p| p.to_string()));
        }

        if let Some(unique_keys) = get_unique_keys(b) {
            for key in unique_keys {
                let key = key.iter().map(|column| format!("C{}", column));
                rows.push(format!("UNIQUE KEY: {}", separated(", ", key)));
            }
        }

        DotLabel::MultiRow(&rows).to_string()
    }

    /// Adds red arrows from correlated quantifiers to the sibling quantifiers they
    /// are correlated with.
    fn add_correlation_info(
        &mut self,
        correlation_info: BTreeMap<QuantifierId, HashSet<ColumnReference>>,
    ) {
        let q_correlation_info = correlation_info.into_iter().map(|(id, column_refs)| {
            (
                id,
                column_refs
                    .iter()
                    .map(|c| c.quantifier_id)
                    .sorted()
                    .unique()
                    .collect::<Vec<_>>(),
            )
        });

        for (correlated_q, quantifiers) in q_correlation_info {
            for q in quantifiers.iter() {
                self.new_line(&format!(
                    "Q{0} -> Q{1} [ {2}, style = filled, color = red ]",
                    correlated_q,
                    q,
                    DotLabel::SingleRow("correlation")
                ));
            }
        }
    }

    fn get_quantifier_alias(q: &Quantifier) -> String {
        if let Some(alias) = &q.alias {
            format!(" as {}", alias)
        } else {
            "".to_string()
        }
    }

    fn inc(&mut self) {
        self.indent += 1;
    }

    fn dec(&mut self) {
        self.indent -= 1;
    }

    fn new_line(&mut self, s: &str) {
        if !self.output.is_empty() && self.output.rfind('\n') != Some(self.output.len()) {
            self.end_line();
            for _ in 0..self.indent * 4 {
                self.output.push(' ');
            }
        }
        self.output.push_str(s);
    }

    fn end_line(&mut self) {
        self.output.push('\n');
    }
}

fn get_unique_keys(b: &QueryBox) -> Option<Vec<Vec<usize>>> {
    // TODO: return value of UniqueKeys attribute if present and
    // fallback to the Get and CallTable base cases otherwise
    // (TBD once the attribute has been added to the codebase)
    match &b.box_type {
        BoxType::CallTable(call_table) => Some(call_table.func.output_type().keys),
        BoxType::Get(get) => Some(get.unique_keys.clone()),
        _ => None,
    }
}

impl<'a> fmt::Display for DotLabelEscapedString<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        for c in self.0.chars() {
            match c {
                '"' => f.write_str("\\\"")?,
                '|' => f.write_str("\\|")?,
                '{' => f.write_str("\\{")?,
                '}' => f.write_str("\\}")?,
                '>' => f.write_str("\\>")?,
                '<' => f.write_str("\\<")?,
                _ => f.write_char(c)?,
            }
        }
        Ok(())
    }
}

impl<'a> fmt::Display for DotLabel<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("label = \"")?;
        match self {
            DotLabel::SingleRow(str) => f.write_str(&DotLabelEscapedString(str).to_string()),
            DotLabel::MultiRow(strs) => {
                f.write_str("{ ")?;
                f.write_str(&format!(
                    "{}",
                    separated("| ", strs.into_iter().map(|str| DotLabelEscapedString(str)))
                ))?;
                f.write_str(" }")
            }
        }?;
        f.write_char('\"')
    }
}