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

//! Transformations that bring relation expressions to their canonical form.
//!
//! This is achieved  by:
//! 1. Bringing enclosed scalar expressions to a canonical form,
//! 2. Converting / peeling off part of the enclosing relation expression into
//!    another relation expression that can represent the same concept.

mod flatmap_to_map;
mod projection_extraction;
mod topk_elision;

pub use flatmap_to_map::FlatMapToMap;
pub use projection_extraction::ProjectionExtraction;
pub use topk_elision::TopKElision;

use mz_expr::MirRelationExpr;

use crate::analysis::{DerivedBuilder, RelationType};
use crate::TransformCtx;

/// A transform that visits each AST node and reduces scalar expressions.
#[derive(Debug)]
pub struct ReduceScalars;

impl crate::Transform for ReduceScalars {
    #[mz_ore::instrument(
        target = "optimizer",
        level = "debug",
        fields(path.segment = "reduce_scalars")
    )]
    fn transform(
        &self,
        relation: &mut MirRelationExpr,
        ctx: &mut TransformCtx,
    ) -> Result<(), crate::TransformError> {
        let mut builder = DerivedBuilder::new(ctx.features);
        builder.require(RelationType);
        let derived = builder.visit(&*relation);

        // Descend the AST, reducing scalar expressions.
        let mut todo = vec![(&mut *relation, derived.as_view())];
        while let Some((expr, view)) = todo.pop() {
            match expr {
                MirRelationExpr::Constant { .. }
                | MirRelationExpr::Get { .. }
                | MirRelationExpr::Let { .. }
                | MirRelationExpr::LetRec { .. }
                | MirRelationExpr::Project { .. }
                | MirRelationExpr::Union { .. }
                | MirRelationExpr::Threshold { .. }
                | MirRelationExpr::Negate { .. } => {
                    // No expressions to reduce
                }
                MirRelationExpr::ArrangeBy { .. } => {
                    // Has expressions, but we aren't brave enough to reduce these yet.
                }
                MirRelationExpr::Filter { predicates, .. } => {
                    let input_type = view
                        .last_child()
                        .value::<RelationType>()
                        .expect("RelationType required")
                        .as_ref()
                        .unwrap();
                    for predicate in predicates.iter_mut() {
                        predicate.reduce(input_type);
                    }
                    predicates.retain(|p| !p.is_literal_true());
                }
                MirRelationExpr::FlatMap { exprs, .. } => {
                    let input_type = view
                        .last_child()
                        .value::<RelationType>()
                        .expect("RelationType required")
                        .as_ref()
                        .unwrap();
                    for expr in exprs.iter_mut() {
                        expr.reduce(input_type);
                    }
                }
                MirRelationExpr::Map { scalars, .. } => {
                    // Use the output type, to incorporate the types of `scalars` as they land.
                    let output_type = view
                        .value::<RelationType>()
                        .expect("RelationType required")
                        .as_ref()
                        .unwrap();
                    let input_arity = output_type.len() - scalars.len();
                    for (index, scalar) in scalars.iter_mut().enumerate() {
                        scalar.reduce(&output_type[..input_arity + index]);
                    }
                }
                MirRelationExpr::Join { equivalences, .. } => {
                    let mut children: Vec<_> = view.children_rev().collect::<Vec<_>>();
                    children.reverse();
                    let input_types = children
                        .iter()
                        .flat_map(|c| {
                            c.value::<RelationType>()
                                .expect("RelationType required")
                                .as_ref()
                                .unwrap()
                                .iter()
                                .cloned()
                        })
                        .collect::<Vec<_>>();

                    for class in equivalences.iter_mut() {
                        for expr in class.iter_mut() {
                            expr.reduce(&input_types[..]);
                        }
                        class.sort();
                        class.dedup();
                    }
                    equivalences.retain(|e| e.len() > 1);
                    equivalences.sort();
                    equivalences.dedup();
                }
                MirRelationExpr::Reduce {
                    group_key,
                    aggregates,
                    ..
                } => {
                    let input_type = view
                        .last_child()
                        .value::<RelationType>()
                        .expect("RelationType required")
                        .as_ref()
                        .unwrap();
                    for key in group_key.iter_mut() {
                        key.reduce(input_type);
                    }
                    for aggregate in aggregates.iter_mut() {
                        aggregate.expr.reduce(input_type);
                    }
                }
                MirRelationExpr::TopK { limit, .. } => {
                    let input_type = view
                        .last_child()
                        .value::<RelationType>()
                        .expect("RelationType required")
                        .as_ref()
                        .unwrap();
                    if let Some(limit) = limit {
                        limit.reduce(input_type);
                    }
                }
            }
            todo.extend(expr.children_mut().rev().zip(view.children_rev()))
        }

        mz_repr::explain::trace_plan(&*relation);
        Ok(())
    }
}