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

//! Definition and helper structs for the [`ColumnNames`] attribute.

use std::ops::Range;

use mz_expr::{Id, MirRelationExpr, MirScalarExpr};
use mz_repr::explain::ExprHumanizer;

use crate::attribute::subtree_size::SubtreeSize;
use crate::attribute::{Attribute, DerivedAttributes, DerivedAttributesBuilder, Env};

/// Compute the column types of each subtree of a [MirRelationExpr] from the
/// bottom-up.
#[allow(missing_debug_implementations)]
pub struct ColumnNames<'c> {
    humanizer: &'c dyn ExprHumanizer,
    /// Environment of computed values for this attribute
    env: Env<Self>,
    /// A vector of results for all nodes in the visited tree in
    /// post-visit order. An empty string denotes a missing value.
    pub results: Vec<Vec<String>>,
}

impl<'c> ColumnNames<'c> {
    /// Construct a new attribute instance.
    pub fn new(humanizer: &'c dyn ExprHumanizer) -> Self {
        Self {
            humanizer,
            env: Env::empty(),
            results: Default::default(),
        }
    }

    fn infer<'a, I>(&self, expr: &MirRelationExpr, mut input_results: I) -> Vec<String>
    where
        I: Iterator<Item = &'a Vec<String>>,
    {
        use MirRelationExpr::*;

        match expr {
            Constant { rows: _, typ } => {
                // Fallback to an anonymous schema for constants.
                ColumnNames::anonymous(0..typ.arity()).collect()
            }
            Get {
                id: Id::Global(id),
                typ,
                access_strategy: _,
            } => {
                if let Some(column_names) = self.humanizer.column_names_for_id(*id) {
                    column_names
                } else {
                    // Possible as some ExprHumanizer impls still return None.
                    ColumnNames::anonymous(0..typ.arity()).collect()
                }
            }
            Get {
                id: Id::Local(id),
                typ,
                access_strategy: _,
            } => {
                if let Some(column_names) = self.env.get(id) {
                    column_names.clone()
                } else {
                    // Possible because we infer LetRec bindings in order. This
                    // can be improved by introducing a fixpoint loop in the
                    // Env<A>::schedule_tasks LetRec handling block.
                    ColumnNames::anonymous(0..typ.arity()).collect()
                }
            }
            Let {
                id: _,
                value: _,
                body: _,
            } => {
                // Return the column names of the `body`.
                input_results.last().unwrap().clone()
            }
            LetRec {
                ids: _,
                values: _,
                limits: _,
                body: _,
            } => {
                // Return the column names of the `body`.
                input_results.last().unwrap().clone()
            }
            Project { input: _, outputs } => {
                // Permute the column names of the input.
                let input_column_names = input_results.next().unwrap();
                let mut column_names = vec![];
                for col in outputs {
                    column_names.push(input_column_names[*col].clone());
                }
                column_names
            }
            Map { input: _, scalars } => {
                // Extend the column names of the input with anonymous columns.
                let mut column_names = input_results.next().unwrap().clone();
                ColumnNames::extend_with_scalars(&mut column_names, scalars);
                column_names
            }
            FlatMap {
                input: _,
                func,
                exprs: _,
            } => {
                // Extend the column names of the input with anonymous columns.
                let mut column_names = input_results.next().unwrap().clone();
                let func_output_start = column_names.len();
                let func_output_end = column_names.len() + func.output_arity();
                column_names.extend(ColumnNames::anonymous(func_output_start..func_output_end));
                column_names
            }
            Filter {
                input: _,
                predicates: _,
            } => {
                // Return the column names of the `input`.
                input_results.next().unwrap().clone()
            }
            Join {
                inputs: _,
                equivalences: _,
                implementation: _,
            } => {
                let mut column_names = vec![];
                for input_column_names in input_results {
                    column_names.extend(input_column_names.iter().cloned());
                }
                column_names
            }
            Reduce {
                input: _,
                group_key,
                aggregates,
                monotonic: _,
                expected_group_size: _,
            } => {
                // We clone and extend the input vector and then remove the part
                // associated with the input at the end.
                let mut column_names = input_results.next().unwrap().clone();
                let input_arity = column_names.len();

                // Infer the group key part.
                ColumnNames::extend_with_scalars(&mut column_names, group_key);
                // Infer the aggregates part.
                let aggs_start = group_key.len();
                let aggs_end = group_key.len() + aggregates.len();
                column_names.extend(ColumnNames::anonymous(aggs_start..aggs_end));
                // Remove the prefix associated with the input
                column_names.drain(0..input_arity);

                column_names
            }
            TopK {
                input: _,
                group_key: _,
                order_key: _,
                limit: _,
                offset: _,
                monotonic: _,
                expected_group_size: _,
            } => {
                // Return the column names of the `input`.
                input_results.next().unwrap().clone()
            }
            Negate { input: _ } => {
                // Return the column names of the `input`.
                input_results.next().unwrap().clone()
            }
            Threshold { input: _ } => {
                // Return the column names of the `input`.
                input_results.next().unwrap().clone()
            }
            Union { base: _, inputs: _ } => {
                // Use the first non-empty column across all inputs.
                let mut column_names = vec![];

                let base_results = input_results.next().unwrap();
                let inputs_results = input_results.collect::<Vec<_>>();

                for (i, mut column_name) in base_results.iter().cloned().enumerate() {
                    for input_results in inputs_results.iter() {
                        if column_name.is_empty() && !input_results[i].is_empty() {
                            column_name = input_results[i].clone();
                            break;
                        }
                    }
                    column_names.push(column_name);
                }

                column_names
            }
            ArrangeBy { input: _, keys: _ } => {
                // Return the column names of the `input`.
                input_results.next().unwrap().clone()
            }
        }
    }

    /// fallback schema consisting of ordinal column names: #0, #1, ...
    fn anonymous(range: Range<usize>) -> impl Iterator<Item = String> {
        range.map(|_| String::new())
    }

    /// fallback schema consisting of ordinal column names: #0, #1, ...
    fn extend_with_scalars(column_names: &mut Vec<String>, scalars: &Vec<MirScalarExpr>) {
        for scalar in scalars {
            column_names.push(match scalar {
                MirScalarExpr::Column(c) => column_names[*c].clone(),
                _ => String::new(),
            });
        }
    }
}

impl<'a> Attribute for ColumnNames<'a> {
    type Value = Vec<String>;

    fn derive(&mut self, expr: &MirRelationExpr, deps: &DerivedAttributes) {
        let n = self.results.len();
        let mut offsets = Vec::new();
        let mut offset = 1;
        for _ in 0..expr.num_inputs() {
            offsets.push(n - offset);
            offset += &deps.get_results::<SubtreeSize>()[n - offset];
        }
        let input_schemas = offsets.into_iter().rev().map(|o| &self.results[o]);
        self.results.push(self.infer(expr, input_schemas));
    }

    fn schedule_env_tasks(&mut self, expr: &MirRelationExpr) {
        self.env.schedule_tasks(expr);
    }

    fn handle_env_tasks(&mut self) {
        self.env.handle_tasks(&self.results);
    }

    fn add_dependencies(builder: &mut DerivedAttributesBuilder)
    where
        Self: Sized,
    {
        builder.require(SubtreeSize::default());
    }

    fn get_results(&self) -> &Vec<Self::Value> {
        &self.results
    }

    fn get_results_mut(&mut self) -> &mut Vec<Self::Value> {
        &mut self.results
    }

    fn take(self) -> Vec<Self::Value> {
        self.results
    }
}