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

pub(crate) mod text;

use std::collections::BTreeMap;

use mz_expr::explain::{enforce_linear_chains, ExplainContext, ExplainMultiPlan, ExplainSource};
use mz_expr::{MirRelationExpr, OptimizedMirRelationExpr};
use mz_repr::explain::{AnnotatedPlan, Explain, ExplainError, UnsupportedFormat};
use mz_repr::GlobalId;

use crate::plan::Plan;
use crate::types::dataflows::DataflowDescription;

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

    type Text = ExplainMultiPlan<'a, Plan>;

    type Json = ExplainMultiPlan<'a, Plan>;

    type Dot = UnsupportedFormat;

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

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

impl<'a> DataflowDescription<Plan> {
    fn as_explain_multi_plan(
        &'a mut self,
        context: &'a ExplainContext<'a>,
    ) -> Result<ExplainMultiPlan<'a, Plan>, ExplainError> {
        let export_ids = export_ids_for(self);
        let plans = self
            .objects_to_build
            .iter_mut()
            .rev()
            .map(|build_desc| {
                let public_id = export_ids
                    .get(&build_desc.id)
                    .unwrap_or(&build_desc.id)
                    .clone();
                let id = context
                    .humanizer
                    .humanize_id(public_id)
                    .unwrap_or_else(|| public_id.to_string());
                let plan = AnnotatedPlan {
                    plan: &build_desc.plan,
                    annotations: BTreeMap::default(),
                };
                (id, plan)
            })
            .collect::<Vec<_>>();

        let sources = self
            .source_imports
            .iter_mut()
            .filter_map(|(id, (source_desc, _))| {
                source_desc.arguments.operators.as_ref().map(|op| {
                    let id = context
                        .humanizer
                        .humanize_id(*id)
                        .unwrap_or_else(|| id.to_string());
                    ExplainSource::new(id, op, context)
                })
            })
            .collect::<Vec<_>>();

        Ok(ExplainMultiPlan {
            context,
            sources,
            plans,
        })
    }
}

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

    type Text = ExplainMultiPlan<'a, MirRelationExpr>;

    type Json = ExplainMultiPlan<'a, MirRelationExpr>;

    type Dot = UnsupportedFormat;

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

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

impl<'a> DataflowDescription<OptimizedMirRelationExpr> {
    fn as_explain_multi_plan(
        &'a mut self,
        context: &'a ExplainContext<'a>,
    ) -> Result<ExplainMultiPlan<'a, MirRelationExpr>, ExplainError> {
        let export_ids = export_ids_for(self);
        let plans = self
            .objects_to_build
            .iter_mut()
            .rev()
            .map(|build_desc| {
                // normalize the representation as linear chains
                // (this implies !context.config.raw_plans by construction)
                if context.config.linear_chains {
                    enforce_linear_chains(build_desc.plan.as_inner_mut())?;
                };

                let id = export_ids
                    .get(&build_desc.id)
                    .unwrap_or(&build_desc.id)
                    .clone();
                let id = context
                    .humanizer
                    .humanize_id(id)
                    .unwrap_or_else(|| id.to_string());
                let plan = AnnotatedPlan {
                    plan: build_desc.plan.as_inner(),
                    annotations: BTreeMap::default(),
                };
                Ok((id, plan))
            })
            .collect::<Result<Vec<_>, ExplainError>>()?;

        let sources = self
            .source_imports
            .iter_mut()
            .filter_map(|(id, (source_desc, _))| {
                source_desc.arguments.operators.as_ref().map(|op| {
                    let id = context
                        .humanizer
                        .humanize_id(*id)
                        .unwrap_or_else(|| id.to_string());
                    ExplainSource::new(id, op, context)
                })
            })
            .collect::<Vec<_>>();

        Ok(ExplainMultiPlan {
            context,
            sources,
            plans,
        })
    }
}

pub fn export_ids_for<P, S, T>(dd: &DataflowDescription<P, S, T>) -> BTreeMap<GlobalId, GlobalId> {
    let mut map = BTreeMap::<GlobalId, GlobalId>::default();

    // Dataflows created from a `CREATE MATERIALIZED VIEW` have:
    //
    // 1. Exactly one entry in `objects_to_build` representing the dataflow to
    //    be installed. This entry has a transient ID that changes whenever the
    //    dataflow is re-installed.
    // 2. Exactly one entry in `sink_exports` referencing the `objects_to_build`
    //    entry.
    // 3. No enties in index_exports.
    //
    // Because the user-facing ID is for the `sink_exports` entry in (2), we
    // create a mapping.
    if dd.sink_exports.len() == 1 && dd.objects_to_build.len() == 1 && dd.index_exports.is_empty() {
        for (public_id, export) in dd.sink_exports.iter() {
            map.insert(export.from, *public_id);
        }
    }

    // Dataflows created from a `CREATE INDEX` adhere to the following
    // constraints.
    //
    // 1. One or more entries in `objects_to_build`. The last entry arranges a
    //    `Get $id` where $id might be:
    //    1. The previous `objects_to_build` entry that corresponds to the
    //       dataflow of the indexed view (if we index a VIEW).
    //    2. A `source_imports` entry (if we index a SOURCE, TABLE, or
    //       MATERIALIZED VIEW).
    //    3. An `index_imports` entry (if we index a SOURCE, TABLE, or
    //       MATERIALIZED VIEW that is already indexed).
    // 2. Exactly one entry in `index_exports` identified by the same ID as the
    //    last `objects_to_build` entry.
    //
    // Because there are no transient IDs involved in the above configurations,
    // we don't need to add further entries to the `map` to account for these
    // cases.

    map
}