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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// Bad casts in this module SHOULD NOT result in a SQL injection
// https://github.com/launchbadge/sqlx/issues/3440
#![allow(
    clippy::cast_possible_truncation,
    clippy::cast_possible_wrap,
    clippy::cast_sign_loss
)]

use crate::connection::intmap::IntMap;
use std::collections::HashSet;
use std::fmt::Debug;
use std::hash::Hash;

pub(crate) use sqlx_core::logger::*;

#[derive(Debug)]
pub(crate) enum BranchResult<R: Debug + 'static> {
    Result(R),
    Dedup(BranchParent),
    Halt,
    Error,
    GasLimit,
    LoopLimit,
    Branched,
}

#[derive(Debug, Clone, Copy, PartialEq, Hash, Eq, Ord, PartialOrd)]
pub(crate) struct BranchParent {
    pub id: i64,
    pub idx: i64,
}

#[derive(Debug)]
pub(crate) struct InstructionHistory<S: Debug + DebugDiff> {
    pub program_i: usize,
    pub state: S,
}

pub(crate) trait DebugDiff {
    fn diff(&self, prev: &Self) -> String;
}

pub struct QueryPlanLogger<'q, R: Debug + 'static, S: Debug + DebugDiff + 'static, P: Debug> {
    sql: &'q str,
    unknown_operations: HashSet<usize>,
    branch_origins: IntMap<BranchParent>,
    branch_results: IntMap<BranchResult<R>>,
    branch_operations: IntMap<IntMap<InstructionHistory<S>>>,
    program: &'q [P],
}

/// convert a string into dot format
fn dot_escape_string(value: impl AsRef<str>) -> String {
    value
        .as_ref()
        .replace('\\', r#"\\"#)
        .replace('"', "'")
        .replace('\n', r#"\n"#)
        .to_string()
}

impl<R: Debug, S: Debug + DebugDiff, P: Debug> core::fmt::Display for QueryPlanLogger<'_, R, S, P> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        //writes query plan history in dot format
        f.write_str("digraph {\n")?;

        f.write_str("subgraph operations {\n")?;
        f.write_str("style=\"rounded\";\nnode [shape=\"point\"];\n")?;

        let all_states: std::collections::HashMap<BranchParent, &InstructionHistory<S>> = self
            .branch_operations
            .iter_entries()
            .flat_map(
                |(branch_id, instructions): (i64, &IntMap<InstructionHistory<S>>)| {
                    instructions.iter_entries().map(
                        move |(idx, ih): (i64, &InstructionHistory<S>)| {
                            (BranchParent { id: branch_id, idx }, ih)
                        },
                    )
                },
            )
            .collect();

        let mut instruction_uses: IntMap<Vec<BranchParent>> = Default::default();
        for (k, state) in all_states.iter() {
            let entry = instruction_uses.get_mut_or_default(&(state.program_i as i64));
            entry.push(*k);
        }

        let mut branch_children: std::collections::HashMap<BranchParent, Vec<BranchParent>> =
            Default::default();

        let mut branched_with_state: std::collections::HashSet<BranchParent> = Default::default();

        for (branch_id, branch_parent) in self.branch_origins.iter_entries() {
            let entry = branch_children.entry(*branch_parent).or_default();
            entry.push(BranchParent {
                id: branch_id,
                idx: 0,
            });
        }

        for (idx, instruction) in self.program.iter().enumerate() {
            let escaped_instruction = dot_escape_string(format!("{:?}", instruction));
            write!(
                f,
                "subgraph cluster_{} {{ label=\"{}\"",
                idx, escaped_instruction
            )?;

            if self.unknown_operations.contains(&idx) {
                f.write_str(" style=dashed")?;
            }

            f.write_str(";\n")?;

            let mut state_list: std::collections::BTreeMap<
                String,
                Vec<(BranchParent, Option<BranchParent>)>,
            > = Default::default();

            write!(f, "i{}[style=invis];", idx)?;

            if let Some(this_instruction_uses) = instruction_uses.get(&(idx as i64)) {
                for curr_ref in this_instruction_uses.iter() {
                    if let Some(curr_state) = all_states.get(curr_ref) {
                        let next_ref = BranchParent {
                            id: curr_ref.id,
                            idx: curr_ref.idx + 1,
                        };

                        if let Some(next_state) = all_states.get(&next_ref) {
                            let state_diff = next_state.state.diff(&curr_state.state);

                            state_list
                                .entry(state_diff)
                                .or_default()
                                .push((*curr_ref, Some(next_ref)));
                        } else {
                            state_list
                                .entry(Default::default())
                                .or_default()
                                .push((*curr_ref, None));
                        };

                        if let Some(children) = branch_children.get(curr_ref) {
                            for next_ref in children {
                                if let Some(next_state) = all_states.get(next_ref) {
                                    let state_diff = next_state.state.diff(&curr_state.state);

                                    if !state_diff.is_empty() {
                                        branched_with_state.insert(*next_ref);
                                    }

                                    state_list
                                        .entry(state_diff)
                                        .or_default()
                                        .push((*curr_ref, Some(*next_ref)));
                                }
                            }
                        };
                    }
                }

                for curr_ref in this_instruction_uses {
                    if branch_children.contains_key(curr_ref) {
                        write!(f, "\"b{}p{}\";", curr_ref.id, curr_ref.idx)?;
                    }
                }
            } else {
                write!(f, "i{}->i{}[style=invis];", idx - 1, idx)?;
            }

            for (state_num, (state_diff, ref_list)) in state_list.iter().enumerate() {
                if !state_diff.is_empty() {
                    let escaped_state = dot_escape_string(state_diff);
                    write!(
                        f,
                        "subgraph \"cluster_i{}s{}\" {{\nlabel=\"{}\"\n",
                        idx, state_num, escaped_state
                    )?;
                }

                for (curr_ref, next_ref) in ref_list {
                    if let Some(next_ref) = next_ref {
                        let next_program_i = all_states
                            .get(next_ref)
                            .map(|s| s.program_i.to_string())
                            .unwrap_or_default();

                        if branched_with_state.contains(next_ref) {
                            write!(
                                f,
                                "\"b{}p{}_b{}p{}\"[tooltip=\"next:{}\"];",
                                curr_ref.id,
                                curr_ref.idx,
                                next_ref.id,
                                next_ref.idx,
                                next_program_i
                            )?;
                            continue;
                        } else {
                            write!(
                                f,
                                "\"b{}p{}\"[tooltip=\"next:{}\"];",
                                curr_ref.id, curr_ref.idx, next_program_i
                            )?;
                        }
                    } else {
                        write!(f, "\"b{}p{}\";", curr_ref.id, curr_ref.idx)?;
                    }
                }

                if !state_diff.is_empty() {
                    f.write_str("}\n")?;
                }
            }

            f.write_str("}\n")?;
        }

        f.write_str("};\n")?; //subgraph operations

        let max_branch_id: i64 = [
            self.branch_operations.last_index().unwrap_or(0),
            self.branch_results.last_index().unwrap_or(0),
            self.branch_results.last_index().unwrap_or(0),
        ]
        .into_iter()
        .max()
        .unwrap_or(0);

        f.write_str("subgraph branches {\n")?;
        for branch_id in 0..=max_branch_id {
            write!(f, "subgraph b{}{{", branch_id)?;

            let branch_num = branch_id as usize;
            let color_names = [
                "blue",
                "red",
                "cyan",
                "yellow",
                "green",
                "magenta",
                "orange",
                "purple",
                "orangered",
                "sienna",
                "olivedrab",
                "pink",
            ];
            let color_name_root = color_names[branch_num % color_names.len()];
            let color_name_suffix = match (branch_num / color_names.len()) % 4 {
                0 => "1",
                1 => "4",
                2 => "3",
                3 => "2",
                _ => "",
            }; //colors are easily confused after color_names.len() * 2, and outright reused after color_names.len() * 4
            write!(
                f,
                "edge [colorscheme=x11 color={}{}];",
                color_name_root, color_name_suffix
            )?;

            let mut instruction_list: Vec<(BranchParent, &InstructionHistory<S>)> = Vec::new();
            if let Some(parent) = self.branch_origins.get(&branch_id) {
                if let Some(parent_state) = all_states.get(parent) {
                    instruction_list.push((*parent, parent_state));
                }
            }
            if let Some(instructions) = self.branch_operations.get(&branch_id) {
                for instruction in instructions.iter_entries() {
                    instruction_list.push((
                        BranchParent {
                            id: branch_id,
                            idx: instruction.0,
                        },
                        instruction.1,
                    ))
                }
            }

            let mut instructions_iter = instruction_list.into_iter();

            if let Some((cur_ref, _)) = instructions_iter.next() {
                let mut prev_ref = cur_ref;

                for (cur_ref, _) in instructions_iter {
                    if branched_with_state.contains(&cur_ref) {
                        writeln!(
                            f,
                            "\"b{}p{}\" -> \"b{}p{}_b{}p{}\" -> \"b{}p{}\"",
                            prev_ref.id,
                            prev_ref.idx,
                            prev_ref.id,
                            prev_ref.idx,
                            cur_ref.id,
                            cur_ref.idx,
                            cur_ref.id,
                            cur_ref.idx
                        )?;
                    } else {
                        write!(
                            f,
                            "\"b{}p{}\" -> \"b{}p{}\";",
                            prev_ref.id, prev_ref.idx, cur_ref.id, cur_ref.idx
                        )?;
                    }
                    prev_ref = cur_ref;
                }

                //draw edge to the result of this branch
                if let Some(result) = self.branch_results.get(&branch_id) {
                    if let BranchResult::Dedup(dedup_ref) = result {
                        write!(
                            f,
                            "\"b{}p{}\"->\"b{}p{}\" [style=dotted]",
                            prev_ref.id, prev_ref.idx, dedup_ref.id, dedup_ref.idx
                        )?;
                    } else {
                        let escaped_result = dot_escape_string(format!("{:?}", result));
                        write!(
                            f,
                            "\"b{}p{}\" ->\"{}\"; \"{}\" [shape=box];",
                            prev_ref.id, prev_ref.idx, escaped_result, escaped_result
                        )?;
                    }
                } else {
                    write!(
                        f,
                        "\"b{}p{}\" ->\"NoResult\"; \"NoResult\" [shape=box];",
                        prev_ref.id, prev_ref.idx
                    )?;
                }
            }
            f.write_str("};\n")?;
        }
        f.write_str("};\n")?; //branches

        f.write_str("}\n")?;
        Ok(())
    }
}

impl<'q, R: Debug, S: Debug + DebugDiff, P: Debug> QueryPlanLogger<'q, R, S, P> {
    pub fn new(sql: &'q str, program: &'q [P]) -> Self {
        Self {
            sql,
            unknown_operations: HashSet::new(),
            branch_origins: IntMap::new(),
            branch_results: IntMap::new(),
            branch_operations: IntMap::new(),
            program,
        }
    }

    pub fn log_enabled(&self) -> bool {
        log::log_enabled!(target: "sqlx::explain", log::Level::Trace)
            || private_tracing_dynamic_enabled!(target: "sqlx::explain", tracing::Level::TRACE)
    }

    pub fn add_branch<I: Copy>(&mut self, state: I, parent: &BranchParent)
    where
        BranchParent: From<I>,
    {
        if !self.log_enabled() {
            return;
        }
        let branch: BranchParent = BranchParent::from(state);
        self.branch_origins.insert(branch.id, *parent);
    }

    pub fn add_operation<I: Copy>(&mut self, program_i: usize, state: I)
    where
        BranchParent: From<I>,
        S: From<I>,
    {
        if !self.log_enabled() {
            return;
        }
        let branch: BranchParent = BranchParent::from(state);
        let state: S = S::from(state);
        self.branch_operations
            .get_mut_or_default(&branch.id)
            .insert(branch.idx, InstructionHistory { program_i, state });
    }

    pub fn add_result<I>(&mut self, state: I, result: BranchResult<R>)
    where
        BranchParent: for<'a> From<&'a I>,
        S: From<I>,
    {
        if !self.log_enabled() {
            return;
        }
        let branch: BranchParent = BranchParent::from(&state);
        self.branch_results.insert(branch.id, result);
    }

    pub fn add_unknown_operation(&mut self, operation: usize) {
        if !self.log_enabled() {
            return;
        }
        self.unknown_operations.insert(operation);
    }

    pub fn finish(&self) {
        if !self.log_enabled() {
            return;
        }

        let mut summary = parse_query_summary(self.sql);

        let sql = if summary != self.sql {
            summary.push_str(" …");
            format!(
                "\n\n{}\n",
                sqlformat::format(
                    self.sql,
                    &sqlformat::QueryParams::None,
                    sqlformat::FormatOptions::default()
                )
            )
        } else {
            String::new()
        };

        sqlx_core::private_tracing_dynamic_event!(
            target: "sqlx::explain",
            tracing::Level::TRACE,
            "{}; program:\n{}\n\n{:?}", summary, self, sql
        );
    }
}

impl<'q, R: Debug, S: Debug + DebugDiff, P: Debug> Drop for QueryPlanLogger<'q, R, S, P> {
    fn drop(&mut self) {
        self.finish();
    }
}