Skip to main content

mz_adapter/optimize/
subscribe.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 `SUBSCRIBE` statements.
11
12use std::marker::PhantomData;
13use std::sync::Arc;
14use std::time::{Duration, Instant};
15
16use differential_dataflow::lattice::Lattice;
17use mz_compute_types::ComputeInstanceId;
18use mz_compute_types::plan::LirRelationExpr;
19use mz_compute_types::sinks::{ComputeSinkConnection, ComputeSinkDesc, SubscribeSinkConnection};
20use mz_expr::{ColumnOrder, MirRelationExpr};
21use mz_ore::soft_assert_or_log;
22use mz_repr::{GlobalId, RelationDesc, Timestamp};
23use mz_sql::optimizer_metrics::OptimizerMetrics;
24use mz_sql::plan::{HirToMirConfig, SubscribeFrom, SubscribePlan};
25use mz_transform::TransformCtx;
26use mz_transform::dataflow::{DataflowMetainfo, optimize_dataflow_snapshot};
27use mz_transform::normalize_lets::normalize_lets;
28use mz_transform::typecheck::{SharedTypecheckingContext, empty_typechecking_context};
29use timely::progress::Antichain;
30
31use crate::CollectionIdBundle;
32use crate::optimize::dataflows::{
33    ComputeInstanceSnapshot, DataflowBuilder, ExprPrep, ExprPrepMaintained,
34    dataflow_import_id_bundle,
35};
36use crate::optimize::{
37    LirDataflowDescription, MirDataflowDescription, Optimize, OptimizeMode, OptimizerCatalog,
38    OptimizerConfig, OptimizerError, optimize_mir_local, trace_plan,
39};
40
41pub struct Optimizer {
42    /// A representation typechecking context to use throughout the optimizer pipeline.
43    typecheck_ctx: SharedTypecheckingContext,
44    /// A snapshot of the catalog state.
45    catalog: Arc<dyn OptimizerCatalog>,
46    /// A snapshot of the cluster that will run the dataflows.
47    compute_instance: ComputeInstanceSnapshot,
48    /// A transient GlobalId to be used for the exported sink.
49    sink_id: GlobalId,
50    /// A transient GlobalId to be used when constructing a dataflow for
51    /// `SUBSCRIBE FROM <SELECT>` variants.
52    view_id: GlobalId,
53    /// Should the plan produce an initial snapshot?
54    with_snapshot: bool,
55    /// Sink timestamp.
56    up_to: Option<Timestamp>,
57    /// A human-readable name exposed internally (useful for debugging).
58    debug_name: String,
59    /// Optimizer config.
60    config: OptimizerConfig,
61    /// Optimizer metrics.
62    metrics: OptimizerMetrics,
63    /// The time spent performing optimization so far.
64    duration: Duration,
65}
66
67// A bogey `Debug` implementation that hides fields. This is needed to make the
68// `event!` call in `sequence_peek_stage` not emit a lot of data.
69//
70// For now, we skip almost all fields, but we might revisit that bit if it turns
71// out that we really need those for debugging purposes.
72impl std::fmt::Debug for Optimizer {
73    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
74        f.debug_struct("Optimizer")
75            .field("config", &self.config)
76            .finish_non_exhaustive()
77    }
78}
79
80impl Optimizer {
81    pub fn new(
82        catalog: Arc<dyn OptimizerCatalog>,
83        compute_instance: ComputeInstanceSnapshot,
84        view_id: GlobalId,
85        sink_id: GlobalId,
86        with_snapshot: bool,
87        up_to: Option<Timestamp>,
88        debug_name: String,
89        config: OptimizerConfig,
90        metrics: OptimizerMetrics,
91    ) -> Self {
92        Self {
93            typecheck_ctx: empty_typechecking_context(),
94            catalog,
95            compute_instance,
96            view_id,
97            sink_id,
98            with_snapshot,
99            up_to,
100            debug_name,
101            config,
102            metrics,
103            duration: Default::default(),
104        }
105    }
106
107    pub fn cluster_id(&self) -> ComputeInstanceId {
108        self.compute_instance.instance_id()
109    }
110
111    pub fn up_to(&self) -> Option<Timestamp> {
112        self.up_to.clone()
113    }
114
115    pub fn sink_id(&self) -> GlobalId {
116        self.sink_id
117    }
118
119    /// Optimizes a subscribe over an already-lowered MIR expression, for
120    /// callers that build their own MIR (such as the frontend read-then-write
121    /// path, which applies the mutation in MIR).
122    ///
123    /// `output` is the sink's row ordering, as produced by
124    /// [`mz_sql::plan::SubscribeOutput::row_order`]. Empty means the sink emits
125    /// raw diffs.
126    pub fn optimize_query(
127        &mut self,
128        expr: MirRelationExpr,
129        from_desc: RelationDesc,
130        output: Vec<ColumnOrder>,
131    ) -> Result<GlobalMirPlan<Unresolved>, OptimizerError> {
132        self.optimize_inner(SubscribeSource::Query { expr, from_desc }, output)
133    }
134
135    /// The single subscribe optimization pipeline. Every subscribe, whatever it
136    /// reads from, goes through here, so a prep or metainfo step added here
137    /// applies to all of them.
138    fn optimize_inner(
139        &mut self,
140        source: SubscribeSource,
141        output: Vec<ColumnOrder>,
142    ) -> Result<GlobalMirPlan<Unresolved>, OptimizerError> {
143        let time = Instant::now();
144
145        let mut df_builder = {
146            let compute = self.compute_instance.clone();
147            DataflowBuilder::new(&*self.catalog, compute).with_config(&self.config)
148        };
149        let mut df_desc = MirDataflowDescription::new(self.debug_name.clone());
150        let mut df_meta = DataflowMetainfo::default();
151
152        let (from, from_desc) = match source {
153            SubscribeSource::Id(from_id) => {
154                let from_desc = self
155                    .catalog
156                    .get_entry(&from_id)
157                    .relation_desc()
158                    .expect("subscribes can only be run on items with descs")
159                    .into_owned();
160
161                df_builder.import_into_dataflow(&from_id, &mut df_desc, &self.config.features)?;
162
163                (from_id, from_desc)
164            }
165            SubscribeSource::Query { expr, from_desc } => {
166                // MIR ⇒ MIR optimization (local)
167                let mut transform_ctx = TransformCtx::local(
168                    &self.config.features,
169                    &self.typecheck_ctx,
170                    &mut df_meta,
171                    Some(&mut self.metrics),
172                    Some(self.view_id),
173                );
174                let expr = optimize_mir_local(expr, &mut transform_ctx)?;
175
176                df_builder.import_view_into_dataflow(
177                    &self.view_id,
178                    &expr,
179                    &mut df_desc,
180                    &self.config.features,
181                )?;
182
183                (self.view_id, from_desc)
184            }
185        };
186        df_builder.maybe_reoptimize_imported_views(&mut df_desc, &self.config)?;
187
188        // Make SinkDesc
189        let sink_description = ComputeSinkDesc {
190            from,
191            from_desc,
192            connection: ComputeSinkConnection::Subscribe(SubscribeSinkConnection { output }),
193            with_snapshot: self.with_snapshot,
194            up_to: self.up_to.map(Antichain::from_elem).unwrap_or_default(),
195            // No `FORCE NOT NULL` for subscribes
196            non_null_assertions: vec![],
197            // No `REFRESH` for subscribes
198            refresh_schedule: None,
199        };
200        df_desc.export_sink(self.sink_id, sink_description);
201
202        // Prepare expressions in the assembled dataflow.
203        let style = ExprPrepMaintained;
204        df_desc.visit_children(
205            |r| style.prep_relation_expr(r),
206            |s| style.prep_scalar_expr(s),
207        )?;
208
209        // Construct TransformCtx for global optimization.
210        let mut transform_ctx = TransformCtx::global(
211            &df_builder,
212            &mz_transform::EmptyStatisticsOracle, // TODO: wire proper stats
213            &self.config.features,
214            &self.typecheck_ctx,
215            &mut df_meta,
216            Some(&mut self.metrics),
217        );
218        // Run global optimization.
219        mz_transform::optimize_dataflow(&mut df_desc, &mut transform_ctx, false)?;
220
221        if self.config.mode == OptimizeMode::Explain {
222            // Collect the list of indexes used by the dataflow at this point.
223            trace_plan!(at: "global", &df_meta.used_indexes(&df_desc));
224        }
225
226        self.duration += time.elapsed();
227
228        // Return the (sealed) plan at the end of this optimization step.
229        Ok(GlobalMirPlan {
230            df_desc,
231            df_meta,
232            phantom: PhantomData::<Unresolved>,
233        })
234    }
235}
236
237/// What a subscribe reads from, with any HIR ⇒ MIR lowering already done.
238enum SubscribeSource {
239    /// An existing collection, imported by id.
240    Id(GlobalId),
241    /// A query, imported into the dataflow as a view under `view_id`.
242    Query {
243        expr: MirRelationExpr,
244        from_desc: RelationDesc,
245    },
246}
247
248/// The (sealed intermediate) result after:
249///
250/// 1. embedding a [`SubscribeFrom`] plan into a [`MirDataflowDescription`],
251/// 2. transitively inlining referenced views, and
252/// 3. jointly optimizing the `MIR` plans in the [`MirDataflowDescription`].
253#[derive(Clone, Debug)]
254pub struct GlobalMirPlan<T: Clone> {
255    df_desc: MirDataflowDescription,
256    df_meta: DataflowMetainfo,
257    phantom: PhantomData<T>,
258}
259
260impl<T: Clone> GlobalMirPlan<T> {
261    /// Computes the [`CollectionIdBundle`] of the wrapped dataflow.
262    pub fn id_bundle(&self, compute_instance_id: ComputeInstanceId) -> CollectionIdBundle {
263        dataflow_import_id_bundle(&self.df_desc, compute_instance_id)
264    }
265}
266
267/// The (final) result after MIR ⇒ LIR lowering and optimizing the resulting
268/// `DataflowDescription` with `LIR` plans.
269#[derive(Clone, Debug)]
270pub struct GlobalLirPlan {
271    df_desc: LirDataflowDescription,
272    df_meta: DataflowMetainfo,
273}
274
275impl GlobalLirPlan {
276    /// Returns the id of the dataflow's sink export.
277    ///
278    /// # Panics
279    ///
280    /// Panics if the dataflow has no sink exports or has more than one.
281    pub fn sink_id(&self) -> GlobalId {
282        self.df_desc.sink_id()
283    }
284}
285
286/// Marker type for [`GlobalMirPlan`] structs representing an optimization
287/// result without a resolved timestamp.
288#[derive(Clone, Debug)]
289pub struct Unresolved;
290
291/// Marker type for [`GlobalMirPlan`] structs representing an optimization
292/// result with a resolved timestamp.
293///
294/// The actual timestamp value is set in the [`MirDataflowDescription`] of the
295/// surrounding [`GlobalMirPlan`] when we call `resolve()`.
296#[derive(Clone, Debug)]
297pub struct Resolved;
298
299impl Optimize<SubscribePlan> for Optimizer {
300    type To = GlobalMirPlan<Unresolved>;
301
302    fn optimize(&mut self, plan: SubscribePlan) -> Result<Self::To, OptimizerError> {
303        let output = plan.output.row_order().to_vec();
304
305        match plan.from {
306            SubscribeFrom::Id(from_id) => self.optimize_inner(SubscribeSource::Id(from_id), output),
307            SubscribeFrom::Query { expr, desc } => {
308                // TODO: Change the `expr` type to be `HirRelationExpr` and run
309                // HIR ⇒ MIR lowering and decorrelation here. This would allow
310                // us implement something like `EXPLAIN RAW PLAN FOR SUBSCRIBE.`
311                //
312                // let typ = expr.top_level_typ();
313
314                // Lowering counts towards the reported optimization time, so we
315                // time it here and `optimize_inner` times the rest.
316                let time = Instant::now();
317                let expr = expr.lower(HirToMirConfig::from(&self.config), None)?;
318                self.duration += time.elapsed();
319
320                self.optimize_inner(
321                    SubscribeSource::Query {
322                        expr,
323                        from_desc: desc,
324                    },
325                    output,
326                )
327            }
328        }
329    }
330}
331
332impl GlobalMirPlan<Unresolved> {
333    /// Produces the [`GlobalMirPlan`] with [`Resolved`] timestamp.
334    ///
335    /// We need to resolve timestamps before the `GlobalMirPlan ⇒ GlobalLirPlan`
336    /// optimization stage in order to profit from possible single-time
337    /// optimizations in the `Plan::finalize_dataflow` call.
338    pub fn resolve(mut self, as_of: Antichain<Timestamp>) -> GlobalMirPlan<Resolved> {
339        // A dataflow description for a `SUBSCRIBE` statement should not have
340        // index exports.
341        soft_assert_or_log!(
342            self.df_desc.index_exports.is_empty(),
343            "unexpectedly setting until for a DataflowDescription with an index",
344        );
345
346        // Set the `as_of` timestamp for the dataflow.
347        self.df_desc.set_as_of(as_of);
348
349        // The only outputs of the dataflow are sinks, so we might be able to
350        // turn off the computation early, if they all have non-trivial
351        // `up_to`s.
352        self.df_desc.until = Antichain::from_elem(Timestamp::MIN);
353        for (_, sink) in &self.df_desc.sink_exports {
354            self.df_desc.until.join_assign(&sink.up_to);
355        }
356
357        GlobalMirPlan {
358            df_desc: self.df_desc,
359            df_meta: self.df_meta,
360            phantom: PhantomData::<Resolved>,
361        }
362    }
363}
364
365impl Optimize<GlobalMirPlan<Resolved>> for Optimizer {
366    type To = GlobalLirPlan;
367
368    fn optimize(&mut self, plan: GlobalMirPlan<Resolved>) -> Result<Self::To, OptimizerError> {
369        let time = Instant::now();
370
371        let GlobalMirPlan {
372            mut df_desc,
373            df_meta,
374            phantom: _,
375        } = plan;
376
377        // Ensure all expressions are normalized before finalizing.
378        for build in df_desc.objects_to_build.iter_mut() {
379            normalize_lets(&mut build.plan.0, &self.config.features)?
380        }
381
382        if self.config.subscribe_snapshot_optimization {
383            // Determine whether we can elide any snapshots for this subscribe.
384            optimize_dataflow_snapshot(&mut df_desc)?;
385        }
386
387        // Finalize the dataflow. This includes:
388        // - MIR ⇒ LIR lowering
389        // - LIR ⇒ LIR transforms
390        let df_desc = LirRelationExpr::finalize_dataflow(
391            df_desc,
392            &self.config.features,
393            Some(self.metrics.lowering()),
394        )?;
395
396        self.duration += time.elapsed();
397        self.metrics
398            .observe_e2e_optimization_time("subscribe", self.duration);
399
400        // Return the plan at the end of this `optimize` step.
401        Ok(GlobalLirPlan { df_desc, df_meta })
402    }
403}
404
405impl GlobalLirPlan {
406    /// Unwraps the parts of the final result of the optimization pipeline.
407    pub fn unapply(self) -> (LirDataflowDescription, DataflowMetainfo) {
408        (self.df_desc, self.df_meta)
409    }
410}