mz_adapter/optimize/
peek.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 `SELECT` statements.
11
12use std::fmt::Debug;
13use std::sync::Arc;
14use std::time::{Duration, Instant};
15
16use mz_compute_types::ComputeInstanceId;
17use mz_compute_types::dataflows::IndexDesc;
18use mz_compute_types::plan::Plan;
19use mz_expr::{MirRelationExpr, MirScalarExpr, OptimizedMirRelationExpr, RowSetFinishing};
20use mz_ore::soft_assert_or_log;
21use mz_repr::explain::trace_plan;
22use mz_repr::{GlobalId, RelationType, Timestamp};
23use mz_sql::optimizer_metrics::OptimizerMetrics;
24use mz_sql::plan::HirRelationExpr;
25use mz_sql::session::metadata::SessionMetadata;
26use mz_transform::dataflow::DataflowMetainfo;
27use mz_transform::normalize_lets::normalize_lets;
28use mz_transform::typecheck::{SharedContext as TypecheckContext, empty_context};
29use mz_transform::{StatisticsOracle, TransformCtx};
30use timely::progress::Antichain;
31use tracing::debug_span;
32
33use crate::TimestampContext;
34use crate::catalog::Catalog;
35use crate::coord::peek::{PeekDataflowPlan, PeekPlan, create_fast_path_plan};
36use crate::optimize::dataflows::{
37    ComputeInstanceSnapshot, DataflowBuilder, EvalTime, ExprPrepStyle, prep_relation_expr,
38    prep_scalar_expr,
39};
40use crate::optimize::{
41    MirDataflowDescription, Optimize, OptimizeMode, OptimizerConfig, OptimizerError,
42    optimize_mir_local, trace_plan,
43};
44
45pub struct Optimizer {
46    /// A typechecking context to use throughout the optimizer pipeline.
47    typecheck_ctx: TypecheckContext,
48    /// A snapshot of the catalog state.
49    catalog: Arc<Catalog>,
50    /// A snapshot of the cluster that will run the dataflows.
51    compute_instance: ComputeInstanceSnapshot,
52    /// Optional row-set finishing to be applied to the final result.
53    finishing: RowSetFinishing,
54    /// A transient GlobalId to be used when constructing the dataflow.
55    select_id: GlobalId,
56    /// A transient GlobalId to be used when constructing a PeekPlan.
57    index_id: GlobalId,
58    /// Optimizer config.
59    config: OptimizerConfig,
60    /// Optimizer metrics.
61    metrics: OptimizerMetrics,
62    /// The time spent performing optimization so far.
63    duration: Duration,
64}
65
66impl Optimizer {
67    pub fn new(
68        catalog: Arc<Catalog>,
69        compute_instance: ComputeInstanceSnapshot,
70        finishing: RowSetFinishing,
71        select_id: GlobalId,
72        index_id: GlobalId,
73        config: OptimizerConfig,
74        metrics: OptimizerMetrics,
75    ) -> Self {
76        Self {
77            typecheck_ctx: empty_context(),
78            catalog,
79            compute_instance,
80            finishing,
81            select_id,
82            index_id,
83            config,
84            metrics,
85            duration: Default::default(),
86        }
87    }
88
89    pub fn cluster_id(&self) -> ComputeInstanceId {
90        self.compute_instance.instance_id()
91    }
92
93    pub fn finishing(&self) -> &RowSetFinishing {
94        &self.finishing
95    }
96
97    pub fn select_id(&self) -> GlobalId {
98        self.select_id
99    }
100
101    pub fn index_id(&self) -> GlobalId {
102        self.index_id
103    }
104
105    pub fn config(&self) -> &OptimizerConfig {
106        &self.config
107    }
108
109    pub fn metrics(&self) -> &OptimizerMetrics {
110        &self.metrics
111    }
112
113    pub fn duration(&self) -> Duration {
114        self.duration
115    }
116}
117
118// A bogey `Debug` implementation that hides fields. This is needed to make the
119// `event!` call in `sequence_peek_stage` not emit a lot of data.
120//
121// For now, we skip almost all fields, but we might revisit that bit if it turns
122// out that we really need those for debugging purposes.
123impl Debug for Optimizer {
124    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
125        f.debug_struct("OptimizePeek")
126            .field("config", &self.config)
127            .finish_non_exhaustive()
128    }
129}
130
131/// Marker type for [`LocalMirPlan`] representing an optimization result without
132/// context.
133pub struct Unresolved;
134
135/// The (sealed intermediate) result after HIR ⇒ MIR lowering and decorrelation
136/// and local MIR optimization.
137#[derive(Clone)]
138pub struct LocalMirPlan<T = Unresolved> {
139    expr: MirRelationExpr,
140    df_meta: DataflowMetainfo,
141    context: T,
142}
143
144/// Marker type for [`LocalMirPlan`] structs representing an optimization result
145/// with attached environment context required for the next optimization stage.
146pub struct Resolved<'s> {
147    timestamp_ctx: TimestampContext<Timestamp>,
148    stats: Box<dyn StatisticsOracle>,
149    session: &'s dyn SessionMetadata,
150}
151
152/// The (final) result after
153///
154/// 1. embedding a [`LocalMirPlan`] into a `DataflowDescription`,
155/// 2. transitively inlining referenced views,
156/// 3. timestamp resolution,
157/// 4. optimizing the resulting `DataflowDescription` with `MIR` plans.
158/// 5. MIR ⇒ LIR lowering, and
159/// 6. optimizing the resulting `DataflowDescription` with `LIR` plans.
160#[derive(Debug)]
161pub struct GlobalLirPlan {
162    peek_plan: PeekPlan,
163    df_meta: DataflowMetainfo,
164    typ: RelationType,
165}
166
167impl Optimize<HirRelationExpr> for Optimizer {
168    type To = LocalMirPlan;
169
170    fn optimize(&mut self, expr: HirRelationExpr) -> Result<Self::To, OptimizerError> {
171        let time = Instant::now();
172
173        // Trace the pipeline input under `optimize/raw`.
174        trace_plan!(at: "raw", &expr);
175
176        // HIR ⇒ MIR lowering and decorrelation
177        let expr = expr.lower(&self.config, Some(&self.metrics))?;
178
179        // MIR ⇒ MIR optimization (local)
180        let mut df_meta = DataflowMetainfo::default();
181        let mut transform_ctx = TransformCtx::local(
182            &self.config.features,
183            &self.typecheck_ctx,
184            &mut df_meta,
185            Some(&self.metrics),
186        );
187        let expr = optimize_mir_local(expr, &mut transform_ctx)?.into_inner();
188
189        self.duration += time.elapsed();
190
191        // Return the (sealed) plan at the end of this optimization step.
192        Ok(LocalMirPlan {
193            expr,
194            df_meta,
195            context: Unresolved,
196        })
197    }
198}
199
200impl LocalMirPlan<Unresolved> {
201    /// Produces the [`LocalMirPlan`] with [`Resolved`] contextual information
202    /// required for the next stage.
203    pub fn resolve(
204        self,
205        timestamp_ctx: TimestampContext<Timestamp>,
206        session: &dyn SessionMetadata,
207        stats: Box<dyn StatisticsOracle>,
208    ) -> LocalMirPlan<Resolved> {
209        LocalMirPlan {
210            expr: self.expr,
211            df_meta: self.df_meta,
212            context: Resolved {
213                timestamp_ctx,
214                session,
215                stats,
216            },
217        }
218    }
219}
220
221impl<'s> Optimize<LocalMirPlan<Resolved<'s>>> for Optimizer {
222    type To = GlobalLirPlan;
223
224    fn optimize(&mut self, plan: LocalMirPlan<Resolved<'s>>) -> Result<Self::To, OptimizerError> {
225        let time = Instant::now();
226
227        let LocalMirPlan {
228            expr,
229            mut df_meta,
230            context:
231                Resolved {
232                    timestamp_ctx,
233                    stats,
234                    session,
235                },
236        } = plan;
237
238        let expr = OptimizedMirRelationExpr(expr);
239
240        // We create a dataflow and optimize it, to determine if we can avoid building it.
241        // This can happen if the result optimizes to a constant, or to a `Get` expression
242        // around a maintained arrangement.
243        let typ = expr.typ();
244        let key = typ
245            .default_key()
246            .iter()
247            .map(|k| MirScalarExpr::Column(*k))
248            .collect();
249
250        // The assembled dataflow contains a view and an index of that view.
251        let mut df_builder = {
252            let catalog = self.catalog.state();
253            let compute = self.compute_instance.clone();
254            DataflowBuilder::new(catalog, compute).with_config(&self.config)
255        };
256
257        let debug_name = format!("oneshot-select-{}", self.select_id);
258        let mut df_desc = MirDataflowDescription::new(debug_name.to_string());
259
260        df_builder.import_view_into_dataflow(
261            &self.select_id,
262            &expr,
263            &mut df_desc,
264            &self.config.features,
265        )?;
266        df_builder.maybe_reoptimize_imported_views(&mut df_desc, &self.config)?;
267
268        // Resolve all unmaterializable function calls except mz_now(), because
269        // we don't yet have a timestamp.
270        let style = ExprPrepStyle::OneShot {
271            logical_time: EvalTime::Deferred,
272            session,
273            catalog_state: self.catalog.state(),
274        };
275        df_desc.visit_children(
276            |r| prep_relation_expr(r, style),
277            |s| prep_scalar_expr(s, style),
278        )?;
279
280        // TODO: Instead of conditioning here we should really
281        // reconsider how to render multi-plan peek dataflows. The main
282        // difficulty here is rendering the optional finishing bit.
283        if self.config.mode != OptimizeMode::Explain {
284            df_desc.export_index(
285                self.index_id,
286                IndexDesc {
287                    on_id: self.select_id,
288                    key,
289                },
290                typ.clone(),
291            );
292        }
293
294        // Set the `as_of` and `until` timestamps for the dataflow.
295        df_desc.set_as_of(timestamp_ctx.antichain());
296
297        // Use the opportunity to name an `until` frontier that will prevent
298        // work we needn't perform. By default, `until` will be
299        // `Antichain::new()`, which prevents no updates and is safe.
300        //
301        // If `timestamp_ctx.antichain()` is empty, `timestamp_ctx.timestamp()`
302        // will return `None` and we use the default (empty) `until`. Otherwise,
303        // we expect to be able to set `until = as_of + 1` without an overflow, unless
304        // we query at the maximum timestamp. In this case, the default empty `until`
305        // is the correct choice.
306        if let Some(until) = timestamp_ctx
307            .timestamp()
308            .and_then(Timestamp::try_step_forward)
309        {
310            df_desc.until = Antichain::from_elem(until);
311        }
312
313        // Construct TransformCtx for global optimization.
314        let mut transform_ctx = TransformCtx::global(
315            &df_builder,
316            &*stats,
317            &self.config.features,
318            &self.typecheck_ctx,
319            &mut df_meta,
320            Some(&self.metrics),
321        );
322
323        // Let's already try creating a fast path plan. If successful, we don't need to run the
324        // whole optimizer pipeline, but just a tiny subset of it. (But we'll need to run
325        // `create_fast_path_plan` later again, because, e.g., running `LiteralConstraints` is still
326        // ahead of us.)
327        let use_fast_path_optimizer = match create_fast_path_plan(
328            &mut df_desc,
329            self.select_id,
330            Some(&self.finishing),
331            self.config.features.persist_fast_path_limit,
332            self.config.persist_fast_path_order,
333        ) {
334            Ok(maybe_fast_path_plan) => maybe_fast_path_plan.is_some(),
335            Err(OptimizerError::UnsafeMfpPlan) => {
336                // This is expected, in that `create_fast_path_plan` can choke on `mz_now`, which we
337                // haven't removed yet.
338                false
339            }
340            Err(e) => {
341                return Err(e);
342            }
343        };
344
345        // Run global optimization.
346        mz_transform::optimize_dataflow(&mut df_desc, &mut transform_ctx, use_fast_path_optimizer)?;
347
348        if self.config.mode == OptimizeMode::Explain {
349            // Collect the list of indexes used by the dataflow at this point.
350            trace_plan!(at: "global", &df_meta.used_indexes(&df_desc));
351        }
352
353        // Get the single timestamp representing the `as_of` time.
354        let as_of = df_desc
355            .as_of
356            .clone()
357            .expect("as_of antichain")
358            .into_option()
359            .expect("unique as_of element");
360
361        // Resolve all unmaterializable function calls including mz_now().
362        let style = ExprPrepStyle::OneShot {
363            logical_time: EvalTime::Time(as_of),
364            session,
365            catalog_state: self.catalog.state(),
366        };
367        df_desc.visit_children(
368            |r| prep_relation_expr(r, style),
369            |s| prep_scalar_expr(s, style),
370        )?;
371
372        // TODO: use the following code once we can be sure that the
373        // index_exports always exist.
374        //
375        // let typ = self.df_desc
376        //     .index_exports
377        //     .first_key_value()
378        //     .map(|(_key, (_desc, typ))| typ.clone())
379        //     .expect("GlobalMirPlan type");
380
381        let peek_plan = match create_fast_path_plan(
382            &mut df_desc,
383            self.select_id,
384            Some(&self.finishing),
385            self.config.features.persist_fast_path_limit,
386            self.config.persist_fast_path_order,
387        )? {
388            Some(plan) if !self.config.no_fast_path => {
389                if self.config.mode == OptimizeMode::Explain {
390                    // Trace the `used_indexes` for the FastPathPlan.
391                    debug_span!(target: "optimizer", "fast_path").in_scope(|| {
392                        // Fast path plans come with an updated finishing.
393                        let finishing = if !self.finishing.is_trivial(typ.arity()) {
394                            Some(&self.finishing)
395                        } else {
396                            None
397                        };
398                        trace_plan(&plan.used_indexes(finishing));
399                    });
400                }
401                // Trace the FastPathPlan.
402                trace_plan!(at: "fast_path", &plan);
403
404                // Trace the pipeline output under `optimize`.
405                trace_plan(&plan);
406
407                // Build the PeekPlan
408                PeekPlan::FastPath(plan)
409            }
410            _ => {
411                soft_assert_or_log!(
412                    !use_fast_path_optimizer || self.config.no_fast_path,
413                    "The fast_path_optimizer shouldn't make a fast path plan slow path."
414                );
415
416                // Ensure all expressions are normalized before finalizing.
417                for build in df_desc.objects_to_build.iter_mut() {
418                    normalize_lets(&mut build.plan.0, &self.config.features)?
419                }
420
421                // Finalize the dataflow. This includes:
422                // - MIR ⇒ LIR lowering
423                // - LIR ⇒ LIR transforms
424                let df_desc = Plan::finalize_dataflow(df_desc, &self.config.features)?;
425
426                // Trace the pipeline output under `optimize`.
427                trace_plan(&df_desc);
428
429                // Build the PeekPlan
430                PeekPlan::SlowPath(PeekDataflowPlan::new(df_desc, self.index_id(), &typ))
431            }
432        };
433
434        self.duration += time.elapsed();
435        let label = match &peek_plan {
436            PeekPlan::FastPath(_) => "peek:fast_path",
437            PeekPlan::SlowPath(_) => "peek:slow_path",
438        };
439        self.metrics
440            .observe_e2e_optimization_time(label, self.duration);
441
442        Ok(GlobalLirPlan {
443            peek_plan,
444            df_meta,
445            typ,
446        })
447    }
448}
449
450impl GlobalLirPlan {
451    /// Unwraps the parts of the final result of the optimization pipeline.
452    pub fn unapply(self) -> (PeekPlan, DataflowMetainfo, RelationType) {
453        (self.peek_plan, self.df_meta, self.typ)
454    }
455}