mz_adapter/optimize/
index.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10//! Optimizer implementation for `CREATE INDEX` statements.
11//!
12//! Note that, in contrast to other optimization pipelines, timestamp selection is not part of
13//! index optimization. Instead users are expected to separately set the as-of on the optimized
14//! `DataflowDescription` received from `GlobalLirPlan::unapply`. Reasons for choosing to exclude
15//! timestamp selection from the index optimization pipeline are:
16//!
17//!  (a) Indexes don't support non-empty `until` frontiers, so they don't provide opportunity for
18//!      optimizations based on the selected timestamp.
19//!  (b) We want to generate dataflow plans early during environment bootstrapping, before we have
20//!      access to all information required for timestamp selection.
21//!
22//! None of this is set in stone though. If we find an opportunity for optimizing indexes based on
23//! their timestamps, we'll want to make timestamp selection part of the index optimization again
24//! and find a different approach to bootstrapping.
25//!
26//! See also MaterializeInc/materialize#22940.
27
28use std::sync::Arc;
29use std::time::{Duration, Instant};
30
31use mz_compute_types::dataflows::IndexDesc;
32use mz_compute_types::plan::Plan;
33use mz_repr::GlobalId;
34use mz_repr::explain::trace_plan;
35use mz_sql::names::QualifiedItemName;
36use mz_sql::optimizer_metrics::OptimizerMetrics;
37use mz_transform::TransformCtx;
38use mz_transform::dataflow::DataflowMetainfo;
39use mz_transform::normalize_lets::normalize_lets;
40use mz_transform::notice::{IndexAlreadyExists, IndexKeyEmpty};
41use mz_transform::typecheck::{SharedContext as TypecheckContext, empty_context};
42
43use crate::optimize::dataflows::{
44    ComputeInstanceSnapshot, DataflowBuilder, ExprPrepStyle, prep_relation_expr, prep_scalar_expr,
45};
46use crate::optimize::{
47    LirDataflowDescription, MirDataflowDescription, Optimize, OptimizeMode, OptimizerCatalog,
48    OptimizerConfig, OptimizerError, trace_plan,
49};
50
51pub struct Optimizer {
52    /// A typechecking context to use throughout the optimizer pipeline.
53    typecheck_ctx: TypecheckContext,
54    /// A snapshot of the catalog state.
55    catalog: Arc<dyn OptimizerCatalog>,
56    /// A snapshot of the cluster that will run the dataflows.
57    compute_instance: ComputeInstanceSnapshot,
58    /// A durable GlobalId to be used with the exported index arrangement.
59    exported_index_id: GlobalId,
60    /// Optimizer config.
61    config: OptimizerConfig,
62    /// Optimizer metrics.
63    metrics: OptimizerMetrics,
64    /// The time spent performing optimization so far.
65    duration: Duration,
66}
67
68impl Optimizer {
69    pub fn new(
70        catalog: Arc<dyn OptimizerCatalog>,
71        compute_instance: ComputeInstanceSnapshot,
72        exported_index_id: GlobalId,
73        config: OptimizerConfig,
74        metrics: OptimizerMetrics,
75    ) -> Self {
76        Self {
77            typecheck_ctx: empty_context(),
78            catalog,
79            compute_instance,
80            exported_index_id,
81            config,
82            metrics,
83            duration: Default::default(),
84        }
85    }
86}
87
88/// A wrapper of index parts needed to start the optimization process.
89pub struct Index {
90    name: QualifiedItemName,
91    on: GlobalId,
92    keys: Vec<mz_expr::MirScalarExpr>,
93}
94
95impl Index {
96    /// Construct a new [`Index`]. Arguments are recorded as-is.
97    pub fn new(name: QualifiedItemName, on: GlobalId, keys: Vec<mz_expr::MirScalarExpr>) -> Self {
98        Self { name, on, keys }
99    }
100}
101
102/// The (sealed intermediate) result after:
103///
104/// 1. embedding an [`Index`] into a [`MirDataflowDescription`],
105/// 2. transitively inlining referenced views, and
106/// 3. jointly optimizing the `MIR` plans in the [`MirDataflowDescription`].
107#[derive(Clone, Debug)]
108pub struct GlobalMirPlan {
109    df_desc: MirDataflowDescription,
110    df_meta: DataflowMetainfo,
111}
112
113impl GlobalMirPlan {
114    pub fn df_desc(&self) -> &MirDataflowDescription {
115        &self.df_desc
116    }
117}
118
119/// The (final) result after MIR ⇒ LIR lowering and optimizing the resulting
120/// `DataflowDescription` with `LIR` plans.
121#[derive(Clone, Debug)]
122pub struct GlobalLirPlan {
123    df_desc: LirDataflowDescription,
124    df_meta: DataflowMetainfo,
125}
126
127impl GlobalLirPlan {
128    pub fn df_desc(&self) -> &LirDataflowDescription {
129        &self.df_desc
130    }
131
132    pub fn df_meta(&self) -> &DataflowMetainfo {
133        &self.df_meta
134    }
135}
136
137impl Optimize<Index> for Optimizer {
138    type To = GlobalMirPlan;
139
140    fn optimize(&mut self, index: Index) -> Result<Self::To, OptimizerError> {
141        let time = Instant::now();
142
143        let on_entry = self.catalog.get_entry(&index.on);
144        let full_name = self
145            .catalog
146            .resolve_full_name(&index.name, on_entry.conn_id());
147        let on_desc = on_entry
148            .desc(&full_name)
149            .expect("can only create indexes on items with a valid description");
150
151        let mut df_builder = {
152            let compute = self.compute_instance.clone();
153            DataflowBuilder::new(&*self.catalog, compute).with_config(&self.config)
154        };
155        let mut df_desc = MirDataflowDescription::new(full_name.to_string());
156
157        df_builder.import_into_dataflow(&index.on, &mut df_desc, &self.config.features)?;
158        df_builder.maybe_reoptimize_imported_views(&mut df_desc, &self.config)?;
159
160        let index_desc = IndexDesc {
161            on_id: index.on,
162            key: index.keys.clone(),
163        };
164        df_desc.export_index(self.exported_index_id, index_desc, on_desc.typ().clone());
165
166        // Prepare expressions in the assembled dataflow.
167        let style = ExprPrepStyle::Index;
168        df_desc.visit_children(
169            |r| prep_relation_expr(r, style),
170            |s| prep_scalar_expr(s, style),
171        )?;
172
173        // Construct TransformCtx for global optimization.
174        let mut df_meta = DataflowMetainfo::default();
175        let mut transform_ctx = TransformCtx::global(
176            &df_builder,
177            &mz_transform::EmptyStatisticsOracle, // TODO: wire proper stats
178            &self.config.features,
179            &self.typecheck_ctx,
180            &mut df_meta,
181            Some(&self.metrics),
182        );
183        // Run global optimization.
184        mz_transform::optimize_dataflow(&mut df_desc, &mut transform_ctx, false)?;
185
186        if self.config.mode == OptimizeMode::Explain {
187            // Collect the list of indexes used by the dataflow at this point.
188            trace_plan!(at: "global", &df_meta.used_indexes(&df_desc));
189        }
190
191        // Emit a notice if we are trying to create an empty index.
192        if index.keys.is_empty() {
193            df_meta.push_optimizer_notice_dedup(IndexKeyEmpty);
194        }
195
196        // Emit a notice for each available index identical to the one we are
197        // currently optimizing.
198        for (index_id, idx) in df_builder
199            .indexes_on(index.on)
200            .filter(|(_id, idx)| idx.keys.as_ref() == &index.keys)
201        {
202            df_meta.push_optimizer_notice_dedup(IndexAlreadyExists {
203                index_id,
204                index_key: idx.keys.to_vec(),
205                index_on_id: idx.on,
206                exported_index_id: self.exported_index_id,
207            });
208        }
209
210        self.duration += time.elapsed();
211
212        // Return the (sealed) plan at the end of this optimization step.
213        Ok(GlobalMirPlan { df_desc, df_meta })
214    }
215}
216
217impl Optimize<GlobalMirPlan> for Optimizer {
218    type To = GlobalLirPlan;
219
220    fn optimize(&mut self, plan: GlobalMirPlan) -> Result<Self::To, OptimizerError> {
221        let time = Instant::now();
222
223        let GlobalMirPlan {
224            mut df_desc,
225            df_meta,
226        } = plan;
227
228        // Ensure all expressions are normalized before finalizing.
229        for build in df_desc.objects_to_build.iter_mut() {
230            normalize_lets(&mut build.plan.0, &self.config.features)?
231        }
232
233        // Finalize the dataflow. This includes:
234        // - MIR ⇒ LIR lowering
235        // - LIR ⇒ LIR transforms
236        let df_desc = Plan::finalize_dataflow(df_desc, &self.config.features)?;
237
238        // Trace the pipeline output under `optimize`.
239        trace_plan(&df_desc);
240
241        self.duration += time.elapsed();
242        self.metrics
243            .observe_e2e_optimization_time("index", self.duration);
244
245        // Return the plan at the end of this `optimize` step.
246        Ok(GlobalLirPlan { df_desc, df_meta })
247    }
248}
249
250impl GlobalLirPlan {
251    /// Unwraps the parts of the final result of the optimization pipeline.
252    pub fn unapply(self) -> (LirDataflowDescription, DataflowMetainfo) {
253        (self.df_desc, self.df_meta)
254    }
255}