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
// 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.

//! `EXPLAIN` support for structures defined in this crate.

use mz_expr::explain::{ExplainContext, ExplainSinglePlan};
use mz_expr::visit::{Visit, VisitChildren};
use mz_expr::{Id, LocalId};
use mz_ore::stack::RecursionLimitError;
use mz_repr::explain::{AnnotatedPlan, Explain, ExplainError, ScalarOps, UnsupportedFormat};
use mz_repr::RelationType;

use crate::plan::{HirRelationExpr, HirScalarExpr};

mod text;

impl<'a> Explain<'a> for HirRelationExpr {
    type Context = ExplainContext<'a>;

    type Text = ExplainSinglePlan<'a, HirRelationExpr>;

    type Json = ExplainSinglePlan<'a, HirRelationExpr>;

    type Dot = UnsupportedFormat;

    fn explain_text(&'a mut self, context: &'a Self::Context) -> Result<Self::Text, ExplainError> {
        self.as_explain_single_plan(context)
    }

    fn explain_json(&'a mut self, context: &'a Self::Context) -> Result<Self::Json, ExplainError> {
        self.as_explain_single_plan(context)
    }
}

impl<'a> HirRelationExpr {
    fn as_explain_single_plan(
        &'a mut self,
        context: &'a ExplainContext<'a>,
    ) -> Result<ExplainSinglePlan<'a, HirRelationExpr>, ExplainError> {
        // unless raw plans are explicitly requested
        // ensure that all nested subqueries are wrapped in Let blocks
        if !context.config.raw_plans {
            normalize_subqueries(self)?;
        }

        // TODO: use config values to infer requested
        // plan annotations
        let plan = AnnotatedPlan {
            plan: self,
            annotations: Default::default(),
        };
        Ok(ExplainSinglePlan { context, plan })
    }
}

/// Normalize the way subqueries appear in [`HirScalarExpr::Exists`]
/// or [`HirScalarExpr::Select`] variants.
///
/// After the transform is applied, subqueries are pulled as a value in
/// a let binding enclosing the [`HirRelationExpr`] parent of the
/// [`HirScalarExpr::Exists`] or [`HirScalarExpr::Select`] where the
/// subquery appears, and the corresponding variant references the
/// new binding with a [`HirRelationExpr::Get`].
pub fn normalize_subqueries<'a>(expr: &'a mut HirRelationExpr) -> Result<(), RecursionLimitError> {
    // A helper struct to represent accumulated `$local_id = $subquery`
    // bindings that need to be installed in `let ... in $expr` nodes
    // that wrap their parent $expr.
    struct Binding {
        local_id: LocalId,
        subquery: HirRelationExpr,
    }

    // Context for the transformation
    // - a stack of bindings
    let mut bindings = Vec::<Binding>::new();
    // - a generator of fresh local ids
    let mut id_gen = id_gen(expr)?.peekable();

    // Grow the `bindings` stack by collecting subqueries appearing in
    // one of the HirScalarExpr children at the given HirRelationExpr.
    // As part of this, the subquery is replaced by a `Get(id)` for a
    // fresh local id.
    let mut collect_subqueries = |expr: &mut HirRelationExpr, bindings: &mut Vec<Binding>| {
        expr.try_visit_mut_children(|expr: &mut HirScalarExpr| {
            use HirRelationExpr::Get;
            use HirScalarExpr::{Exists, Select};
            expr.visit_mut_post(&mut |expr: &mut HirScalarExpr| match expr {
                Exists(expr) | Select(expr) => match expr.as_mut() {
                    Get { .. } => (),
                    expr => {
                        // generate fresh local id
                        let local_id = id_gen.next().unwrap();
                        // generate a `Get(local_id)` to be used as a subquery replacement
                        let mut subquery = Get {
                            id: Id::Local(local_id.clone()),
                            typ: RelationType::empty(), // TODO (aalexandrov)
                        };
                        // swap the current subquery with the replacement
                        std::mem::swap(expr, &mut subquery);
                        // push a new $local_id = $subquery binding for a wrapping Let { ... }
                        bindings.push(Binding { local_id, subquery });
                    }
                },
                _ => (),
            })
        })
    };

    // Drain the `bindings` stack by wrapping the given `HirRelationExpr` with
    // a sequence of `Let { ... }` nodes, one for each binding.
    let insert_let_bindings = |expr: &mut HirRelationExpr, bindings: &mut Vec<Binding>| {
        for binding in bindings.drain(..) {
            let name = format!("subquery-{}", Into::<u64>::into(&binding.local_id));
            let id = binding.local_id;
            let value = Box::new(binding.subquery);
            let body = Box::new(expr.take());
            *expr = HirRelationExpr::Let {
                name,
                id,
                value,
                body,
            }
        }
    };

    expr.try_visit_mut_post(&mut |expr: &mut HirRelationExpr| {
        // first grow bindings stack
        collect_subqueries(expr, &mut bindings)?;
        // then drain bindings stack
        insert_let_bindings(expr, &mut bindings);
        // done!
        Ok(())
    })
}

// Create an [`Iterator`] for [`LocalId`] values that are guaranteed to be
// fresh within the scope of the given [`HirRelationExpr`].
fn id_gen(expr: &HirRelationExpr) -> Result<impl Iterator<Item = LocalId>, RecursionLimitError> {
    let mut max_id = 0_u64;

    expr.visit_pre(&mut |expr| {
        match expr {
            HirRelationExpr::Let { id, .. } => max_id = std::cmp::max(max_id, id.into()),
            _ => (),
        };
    })?;

    Ok((max_id + 1..).map(LocalId::new))
}

impl ScalarOps for HirScalarExpr {
    fn match_col_ref(&self) -> Option<usize> {
        match self {
            HirScalarExpr::Column(c) if c.level == 0 => Some(c.column),
            _ => None,
        }
    }

    fn references(&self, column: usize) -> bool {
        match self {
            HirScalarExpr::Column(c) => c.column == column && c.level == 0,
            _ => false,
        }
    }
}