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
// 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 [`HirRelationExpr`] from a [`Model`].
//!
//! The public interface consists of the [`From<Model>`]
//! implementation for [`HirRelationExpr`].

use std::collections::{HashMap, HashSet};

use crate::plan::expr::{ColumnRef, JoinKind};
use crate::plan::{HirRelationExpr, HirScalarExpr};
use crate::query_model::attribute::relation_type::RelationType as BoxRelationType;
use crate::query_model::error::*;
use crate::query_model::model::*;
use crate::query_model::{BoxId, Model, QGMError, QuantifierId};

use itertools::Itertools;

use mz_ore::id_gen::IdGen;

impl TryFrom<Model> for HirRelationExpr {
    type Error = QGMError;
    fn try_from(model: Model) -> Result<Self, Self::Error> {
        FromModel::default().generate(model)
    }
}

#[derive(Default)]
struct FromModel {
    /// Generates [`mz_expr::LocalId`] as an alias for common expressions.
    id_gen: IdGen,
    // TODO: Add the current correlation information.
    // Inspired by FromHir::context_stack. Subject to change.
    // context_stack: Vec<QuantifierId>,
    /// Stack of [HirRelationExpr] that have been created through visiting nodes
    /// of QGM in post-order.
    converted: Vec<HirRelationExpr>,
    /// Common expressions that have been given a [`mz_expr::LocalId`], from
    /// oldest to most recently identified.
    lets: Vec<(mz_expr::LocalId, HirRelationExpr, mz_repr::RelationType)>,
    /// Map of ([BoxId]s whose HIR representation has been given a
    /// [`mz_expr::LocalId`]) -> (position of its HIR representation in `lets`)
    common_subgraphs: HashMap<BoxId, usize>,
}

/// Tracks the column ranges in a [HirRelationExpr] in corresponding the
/// quantifiers in a box.
struct ColumnMap {
    quantifier_to_input: HashMap<QuantifierId, usize>,
    input_mapper: mz_expr::JoinInputMapper,
}

impl FromModel {
    fn generate(mut self, mut model: Model) -> Result<HirRelationExpr, QGMError> {
        // Derive the RelationType of each box.
        use crate::query_model::attribute::core::{Attribute, RequiredAttributes};
        let attributes = HashSet::from_iter(std::iter::once::<Box<dyn Attribute>>(Box::new(
            BoxRelationType,
        )));
        let root = model.top_box;
        RequiredAttributes::from(attributes).derive(&mut model, root);

        // Convert QGM to HIR.
        model.try_visit_pre_post(
            &mut |_, _| -> Result<(), QGMError> {
                // TODO: add to the context stack
                Ok(())
            },
            &mut |model, box_id| -> Result<(), QGMError> { self.convert_subgraph(model, box_id) },
        )?;

        debug_assert!(self.converted.len() == 1);
        // Retrieve the HIR and put `let`s around it.
        let mut result = self.converted.pop().unwrap();
        while let Some((id, value, _)) = self.lets.pop() {
            if !matches!(value, HirRelationExpr::Get { .. }) {
                result = HirRelationExpr::Let {
                    name: "".to_string(),
                    id,
                    value: Box::new(value),
                    body: Box::new(result),
                }
            }
        }
        Ok(result)
    }

    /// Convert subgraph rooted at `box_id` to [HirRelationExpr].
    ///
    /// If the box corresponding to `box_id` has multiple ranging quantifiers,
    /// the [HirRelationExpr] will be in `self.lets`, and it can be looked up in
    /// `self.common_subgraphs`. Otherwise, it will be pushed to `self.converted`.
    fn convert_subgraph(&mut self, model: &Model, box_id: &BoxId) -> Result<(), QGMError> {
        let r#box = model.get_box(*box_id);
        if self.common_subgraphs.get(box_id).is_some() {
            // Subgraph has already been converted.
            return Ok(());
        }
        let hir = match &r#box.box_type {
            BoxType::Get(Get { id, unique_keys }) => {
                let typ = mz_repr::RelationType::new(
                    r#box.attributes.get::<BoxRelationType>().to_owned(),
                )
                .with_keys(unique_keys.clone());
                HirRelationExpr::Get {
                    id: mz_expr::Id::Global(*id),
                    typ,
                }
            }
            BoxType::Values(values) => HirRelationExpr::constant(
                // Map `Vec<Vec<BoxScalarExpr>` to `Vec<Vec<Datum>>`
                values
                    .rows
                    .iter()
                    .map(|row| {
                        row.iter()
                            .map(|expr| {
                                if let BoxScalarExpr::Literal(datum, _) = expr {
                                    Ok(datum.unpack_first())
                                } else {
                                    // TODO: The validator should make this branch
                                    // unreachable.
                                    Err(QGMError::from(UnsupportedBoxScalarExpr {
                                        context: "HIR conversion".to_string(),
                                        scalar: expr.clone(),
                                        explanation: Some("Only literal expressions are supported in Values boxes.".to_string())
                                    }))
                                }
                            })
                            .try_collect()
                    })
                    .try_collect()?,
                mz_repr::RelationType::new(r#box.attributes.get::<BoxRelationType>().to_owned()),
            ),
            BoxType::Select(select) => {
                let (rels, scalars) = self.convert_quantifiers(&r#box)?;
                let (join_q_ids, mut join_inputs): (Vec<_>, Vec<_>) = rels.into_iter().unzip();
                let (map_q_ids, maps): (Vec<_>, Vec<_>) = scalars.into_iter().unzip();

                let mut result = if let Some(first) = join_inputs.pop() {
                    // TODO (#11375): determine if this situation should be allowable.
                    first
                } else {
                    HirRelationExpr::constant(vec![vec![]], mz_repr::RelationType::new(vec![]))
                };

                while let Some(join_input) = join_inputs.pop() {
                    result = HirRelationExpr::Join {
                        left: Box::new(result),
                        right: Box::new(join_input),
                        kind: JoinKind::Inner,
                        on: HirScalarExpr::literal_true(),
                    }
                }
                if !maps.is_empty() {
                    result = result.map(maps);
                }
                let column_map = ColumnMap::new(
                    Iterator::chain(join_q_ids.into_iter().rev(), map_q_ids.into_iter().rev()),
                    model,
                );
                if !select.predicates.is_empty() {
                    let filter = select
                        .predicates
                        .iter()
                        .map(|pred| self.convert_scalar(pred, &column_map))
                        .filter(|pred| pred != &HirScalarExpr::literal_true())
                        .collect_vec();
                    if !filter.is_empty() {
                        result = result.filter(filter);
                    }
                }
                let mut result = self.convert_projection(&r#box, &column_map, result);
                if r#box.distinct == DistinctOperation::Enforce {
                    result = result.distinct();
                }
                if select.limit.is_some() || select.offset.is_some() || select.order_key.is_some() {
                    // TODO: put a TopK around the result
                    unimplemented!()
                }
                result
            }
            BoxType::OuterJoin(outer_join) => {
                let (mut rels, _) = self.convert_quantifiers(&r#box)?;
                let (_, left) = rels.pop().unwrap();
                let (_, right) = rels.pop().unwrap();
                let mut quantifier_iter = r#box.input_quantifiers();
                let left_preserved = matches!(
                    quantifier_iter.next().unwrap().quantifier_type,
                    QuantifierType::PRESERVED_FOREACH
                );
                let right_preserved = matches!(
                    quantifier_iter.next().unwrap().quantifier_type,
                    QuantifierType::PRESERVED_FOREACH
                );
                let kind = if left_preserved {
                    if right_preserved {
                        JoinKind::FullOuter
                    } else {
                        JoinKind::LeftOuter
                    }
                } else if right_preserved {
                    JoinKind::RightOuter
                } else {
                    JoinKind::Inner
                };
                let column_map = ColumnMap::new(r#box.input_quantifiers().map(|q| q.id), model);
                let converted_predicates = outer_join
                    .predicates
                    .iter()
                    .map(|pred| self.convert_scalar(pred, &column_map))
                    .collect_vec();
                let on = HirScalarExpr::variadic_and(converted_predicates);
                let result = HirRelationExpr::Join {
                    left: Box::new(left),
                    right: Box::new(right),
                    kind,
                    on,
                };
                self.convert_projection(&r#box, &column_map, result)
            }
            _ => unimplemented!(),
        };

        if r#box.ranging_quantifiers().count() > 1 {
            let id = mz_expr::LocalId::new(self.id_gen.allocate_id());
            let typ =
                mz_repr::RelationType::new(r#box.attributes.get::<BoxRelationType>().to_owned());
            self.common_subgraphs.insert(r#box.id, self.lets.len());
            self.lets.push((id, hir, typ));
        } else {
            self.converted.push(hir);
        }
        Ok(())
    }

    /// Convert the quantifiers of `box` into [HirRelationExpr] and [HirScalarExpr].
    ///
    /// Results are returned in reverse quantifier order; the last entry in the
    /// second vector corresponds to the first subquery quantifier in the box.
    fn convert_quantifiers<'a>(
        &mut self,
        r#box: &BoundRef<'a, QueryBox>,
    ) -> Result<
        (
            Vec<(QuantifierId, HirRelationExpr)>,
            Vec<(QuantifierId, HirScalarExpr)>,
        ),
        QGMError,
    > {
        let mut rels = Vec::new();
        let mut scalars = Vec::new();
        for q in r#box.input_quantifiers().rev() {
            // Retrive the HIR corresponding to the input box of the quantifier
            let inner_relation = if let Some(let_pos) = self.common_subgraphs.get(&q.input_box) {
                if matches!(self.lets[*let_pos].1, HirRelationExpr::Get { .. }) {
                    self.lets[*let_pos].1.clone()
                } else {
                    HirRelationExpr::Get {
                        id: mz_expr::Id::Local(self.lets[*let_pos].0),
                        typ: self.lets[*let_pos].2.clone(),
                    }
                }
            } else {
                self.converted.pop().unwrap()
            };

            match q.quantifier_type {
                QuantifierType::FOREACH | QuantifierType::PRESERVED_FOREACH => {
                    rels.push((q.id, inner_relation))
                }
                QuantifierType::EXISTENTIAL => {
                    scalars.push((q.id, HirScalarExpr::Exists(Box::new(inner_relation))))
                }
                QuantifierType::SCALAR => {
                    scalars.push((q.id, HirScalarExpr::Select(Box::new(inner_relation))))
                }
                _ => {
                    return Err(QGMError::from(UnsupportedQuantifierType {
                        quantifier_type: q.quantifier_type,
                        context: "HIR conversion".to_string(),
                    }));
                }
            }
        }
        Ok((rels, scalars))
    }

    /// Convert the projection of a box into a Map + Project around the
    /// [HirRelationExpr] representing the rest of the box.
    fn convert_projection<'a>(
        &mut self,
        r#box: &BoundRef<'a, QueryBox>,
        column_map: &ColumnMap,
        mut result: HirRelationExpr,
    ) -> HirRelationExpr {
        let mut map = Vec::new();
        let mut project = Vec::new();

        for column in &r#box.columns {
            let hir_scalar = self.convert_scalar(&column.expr, column_map);
            if let HirScalarExpr::Column(ColumnRef { level: 0, column }) = hir_scalar {
                // We can skip making a copy of the column.
                project.push(column);
            } else {
                project.push(column_map.arity() + map.len());
                map.push(hir_scalar);
            }
        }

        if !map.is_empty() {
            result = result.map(map);
        }
        result.project(project)
    }

    /// Convert a [BoxScalarExpr] to [HirScalarExpr].
    fn convert_scalar(&mut self, expr: &BoxScalarExpr, column_map: &ColumnMap) -> HirScalarExpr {
        let mut results = Vec::new();
        expr.visit_post(&mut |e| match e {
            BoxScalarExpr::BaseColumn(BaseColumn { position, .. }) => {
                results.push(HirScalarExpr::Column(ColumnRef {
                    level: 0,
                    column: *position,
                }));
            }
            BoxScalarExpr::ColumnReference(ColumnReference {
                quantifier_id,
                position,
            }) => {
                let converted = if let Some(column) =
                    column_map.lookup_level_0_col(*quantifier_id, *position)
                {
                    HirScalarExpr::Column(ColumnRef { level: 0, column })
                } else {
                    // TODO: calculate the level for a correlated subquery.
                    unimplemented!()
                };
                results.push(converted);
            }
            BoxScalarExpr::Literal(row, typ) => {
                results.push(HirScalarExpr::Literal(row.clone(), typ.clone()))
            }
            BoxScalarExpr::CallUnmaterializable(func) => {
                results.push(HirScalarExpr::CallUnmaterializable(func.clone()))
            }
            BoxScalarExpr::CallUnary { func, .. } => {
                let expr = Box::new(results.pop().unwrap());
                results.push(HirScalarExpr::CallUnary {
                    func: func.clone(),
                    expr,
                })
            }
            BoxScalarExpr::CallBinary { func, .. } => {
                let expr2 = Box::new(results.pop().unwrap());
                let expr1 = Box::new(results.pop().unwrap());
                results.push(HirScalarExpr::CallBinary {
                    func: func.clone(),
                    expr1,
                    expr2,
                })
            }
            BoxScalarExpr::CallVariadic { func, exprs } => {
                let exprs = results.drain((results.len() - exprs.len())..).collect_vec();
                results.push(HirScalarExpr::CallVariadic {
                    func: func.clone(),
                    exprs,
                })
            }
            BoxScalarExpr::If { .. } => {
                let els = Box::new(results.pop().unwrap());
                let then = Box::new(results.pop().unwrap());
                let cond = Box::new(results.pop().unwrap());
                results.push(HirScalarExpr::If { els, then, cond });
            }
            _ => unimplemented!(),
        });
        debug_assert!(results.len() == 1);
        results.pop().unwrap()
    }
}

impl ColumnMap {
    fn new<I>(quantifiers: I, model: &Model) -> Self
    where
        I: Iterator<Item = QuantifierId>,
    {
        let (quantifier_to_input, arities): (HashMap<_, _>, Vec<_>) = quantifiers
            .enumerate()
            .map(|(index, q_id)| ((q_id, index), model.get_quantifier(q_id).output_arity()))
            .unzip();
        let input_mapper = mz_expr::JoinInputMapper::new_from_input_arities(arities.into_iter());
        Self {
            quantifier_to_input,
            input_mapper,
        }
    }

    fn lookup_level_0_col(&self, q_id: QuantifierId, position: usize) -> Option<usize> {
        if let Some(input) = self.quantifier_to_input.get(&q_id) {
            Some(self.input_mapper.map_column_to_global(position, *input))
        } else {
            None
        }
    }

    #[inline(always)]
    fn arity(&self) -> usize {
        self.input_mapper.total_columns()
    }
}