mz_adapter/optimize/
copy_to.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 `COPY TO` 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::plan::Plan;
18use mz_compute_types::sinks::{
19    ComputeSinkConnection, ComputeSinkDesc, CopyToS3OneshotSinkConnection,
20};
21use mz_expr::{MirRelationExpr, OptimizedMirRelationExpr};
22use mz_repr::explain::trace_plan;
23use mz_repr::{GlobalId, Timestamp};
24use mz_sql::optimizer_metrics::OptimizerMetrics;
25use mz_sql::plan::HirRelationExpr;
26use mz_sql::session::metadata::SessionMetadata;
27use mz_storage_types::connections::Connection;
28use mz_storage_types::sinks::S3UploadInfo;
29use mz_transform::dataflow::DataflowMetainfo;
30use mz_transform::normalize_lets::normalize_lets;
31use mz_transform::reprtypecheck::{
32    SharedContext as ReprTypecheckContext, empty_context as empty_repr_context,
33};
34use mz_transform::typecheck::{SharedContext as TypecheckContext, empty_context};
35use mz_transform::{StatisticsOracle, TransformCtx};
36use timely::progress::Antichain;
37use tracing::warn;
38
39use crate::TimestampContext;
40use crate::catalog::Catalog;
41use crate::coord::CopyToContext;
42use crate::optimize::dataflows::{
43    ComputeInstanceSnapshot, DataflowBuilder, EvalTime, ExprPrepStyle, prep_relation_expr,
44    prep_scalar_expr,
45};
46use crate::optimize::{
47    LirDataflowDescription, MirDataflowDescription, Optimize, OptimizeMode, OptimizerConfig,
48    OptimizerError, optimize_mir_local, trace_plan,
49};
50
51pub struct Optimizer {
52    /// A typechecking context to use throughout the optimizer pipeline.
53    typecheck_ctx: TypecheckContext,
54    /// A representation typechecking context to use throughout the optimizer pipeline.
55    repr_typecheck_ctx: ReprTypecheckContext,
56    /// A snapshot of the catalog state.
57    catalog: Arc<Catalog>,
58    /// A snapshot of the cluster that will run the dataflows.
59    compute_instance: ComputeInstanceSnapshot,
60    /// A transient GlobalId to be used when constructing the dataflow.
61    select_id: GlobalId,
62    /// Data required to do a COPY TO query.
63    copy_to_context: CopyToContext,
64    /// Optimizer config.
65    config: OptimizerConfig,
66    /// Optimizer metrics.
67    metrics: OptimizerMetrics,
68    /// The time spent performing optimization so far.
69    duration: Duration,
70}
71
72impl Optimizer {
73    pub fn new(
74        catalog: Arc<Catalog>,
75        compute_instance: ComputeInstanceSnapshot,
76        select_id: GlobalId,
77        copy_to_context: CopyToContext,
78        config: OptimizerConfig,
79        metrics: OptimizerMetrics,
80    ) -> Self {
81        Self {
82            typecheck_ctx: empty_context(),
83            repr_typecheck_ctx: empty_repr_context(),
84            catalog,
85            compute_instance,
86            select_id,
87            copy_to_context,
88            config,
89            metrics,
90            duration: Default::default(),
91        }
92    }
93
94    pub fn cluster_id(&self) -> ComputeInstanceId {
95        self.compute_instance.instance_id()
96    }
97}
98
99// A bogey `Debug` implementation that hides fields. This is needed to make the
100// `event!` call in `sequence_peek_stage` not emit a lot of data.
101//
102// For now, we skip almost all fields, but we might revisit that bit if it turns
103// out that we really need those for debugging purposes.
104impl Debug for Optimizer {
105    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
106        f.debug_struct("OptimizePeek")
107            .field("config", &self.config)
108            .finish_non_exhaustive()
109    }
110}
111
112/// Marker type for [`LocalMirPlan`] representing an optimization result without
113/// context.
114pub struct Unresolved;
115
116/// The (sealed intermediate) result after HIR ⇒ MIR lowering and decorrelation
117/// and MIR optimization.
118#[derive(Clone)]
119pub struct LocalMirPlan<T = Unresolved> {
120    expr: MirRelationExpr,
121    df_meta: DataflowMetainfo,
122    context: T,
123}
124
125/// Marker type for [`LocalMirPlan`] structs representing an optimization result
126/// with attached environment context required for the next optimization stage.
127pub struct Resolved<'s> {
128    timestamp_ctx: TimestampContext<Timestamp>,
129    stats: Box<dyn StatisticsOracle>,
130    session: &'s dyn SessionMetadata,
131}
132
133/// The (final) result after
134///
135/// 1. embedding a [`LocalMirPlan`] into a `DataflowDescription`,
136/// 2. transitively inlining referenced views,
137/// 3. timestamp resolution,
138/// 4. optimizing the resulting `DataflowDescription` with `MIR` plans.
139/// 5. MIR ⇒ LIR lowering, and
140/// 6. optimizing the resulting `DataflowDescription` with `LIR` plans.
141#[derive(Debug)]
142pub struct GlobalLirPlan {
143    df_desc: LirDataflowDescription,
144    df_meta: DataflowMetainfo,
145}
146
147impl GlobalLirPlan {
148    pub fn df_desc(&self) -> &LirDataflowDescription {
149        &self.df_desc
150    }
151
152    pub fn sink_id(&self) -> GlobalId {
153        let sink_exports = &self.df_desc.sink_exports;
154        let sink_id = sink_exports.keys().next().expect("valid sink");
155        *sink_id
156    }
157}
158
159impl Optimize<HirRelationExpr> for Optimizer {
160    type To = LocalMirPlan;
161
162    fn optimize(&mut self, expr: HirRelationExpr) -> Result<Self::To, OptimizerError> {
163        let time = Instant::now();
164
165        // Trace the pipeline input under `optimize/raw`.
166        trace_plan!(at: "raw", &expr);
167
168        // HIR ⇒ MIR lowering and decorrelation
169        let expr = expr.lower(&self.config, Some(&self.metrics))?;
170
171        // MIR ⇒ MIR optimization (local)
172        let mut df_meta = DataflowMetainfo::default();
173        let mut transform_ctx = TransformCtx::local(
174            &self.config.features,
175            &self.typecheck_ctx,
176            &self.repr_typecheck_ctx,
177            &mut df_meta,
178            Some(&self.metrics),
179            Some(self.select_id),
180        );
181        let expr = optimize_mir_local(expr, &mut transform_ctx)?.into_inner();
182
183        self.duration += time.elapsed();
184
185        // Return the (sealed) plan at the end of this optimization step.
186        Ok(LocalMirPlan {
187            expr,
188            df_meta,
189            context: Unresolved,
190        })
191    }
192}
193
194impl LocalMirPlan<Unresolved> {
195    /// Produces the [`LocalMirPlan`] with [`Resolved`] contextual information
196    /// required for the next stage.
197    pub fn resolve(
198        self,
199        timestamp_ctx: TimestampContext<Timestamp>,
200        session: &dyn SessionMetadata,
201        stats: Box<dyn StatisticsOracle>,
202    ) -> LocalMirPlan<Resolved<'_>> {
203        LocalMirPlan {
204            expr: self.expr,
205            df_meta: self.df_meta,
206            context: Resolved {
207                timestamp_ctx,
208                session,
209                stats,
210            },
211        }
212    }
213}
214
215impl<'s> Optimize<LocalMirPlan<Resolved<'s>>> for Optimizer {
216    type To = GlobalLirPlan;
217
218    fn optimize(&mut self, plan: LocalMirPlan<Resolved<'s>>) -> Result<Self::To, OptimizerError> {
219        let time = Instant::now();
220
221        let LocalMirPlan {
222            expr,
223            mut df_meta,
224            context:
225                Resolved {
226                    timestamp_ctx,
227                    stats,
228                    session,
229                },
230        } = plan;
231
232        let expr = OptimizedMirRelationExpr(expr);
233
234        // The assembled dataflow contains a view and a sink on that view.
235        let mut df_builder = {
236            let catalog = self.catalog.state();
237            let compute = self.compute_instance.clone();
238            DataflowBuilder::new(catalog, compute).with_config(&self.config)
239        };
240
241        let debug_name = format!("copy-to-{}", self.select_id);
242        let mut df_desc = MirDataflowDescription::new(debug_name.to_string());
243
244        df_builder.import_view_into_dataflow(
245            &self.select_id,
246            &expr,
247            &mut df_desc,
248            &self.config.features,
249        )?;
250        df_builder.maybe_reoptimize_imported_views(&mut df_desc, &self.config)?;
251
252        // Creating an S3 sink as currently only s3 sinks are supported. It
253        // might be possible in the future for COPY TO to write to different
254        // sinks, which should be set here depending upon the url scheme.
255        let connection = match &self.copy_to_context.connection {
256            Connection::Aws(aws_connection) => {
257                ComputeSinkConnection::CopyToS3Oneshot(CopyToS3OneshotSinkConnection {
258                    upload_info: S3UploadInfo {
259                        uri: self.copy_to_context.uri.to_string(),
260                        max_file_size: self.copy_to_context.max_file_size,
261                        desc: self.copy_to_context.desc.clone(),
262                        format: self.copy_to_context.format.clone(),
263                    },
264                    aws_connection: aws_connection.clone(),
265                    connection_id: self.copy_to_context.connection_id,
266                    output_batch_count: self
267                        .copy_to_context
268                        .output_batch_count
269                        .expect("output_batch_count should be set in sequencer"),
270                })
271            }
272            _ => {
273                // Currently only s3 sinks are supported. It was already validated in planning that this
274                // is an aws connection.
275                let msg = "only aws connection is supported in COPY TO";
276                return Err(OptimizerError::Internal(msg.to_string()));
277            }
278        };
279        let sink_description = ComputeSinkDesc {
280            from_desc: self.copy_to_context.desc.clone(),
281            from: self.select_id,
282            connection,
283            with_snapshot: true,
284            // This will get updated  when the GlobalMirPlan is resolved with as_of below.
285            up_to: Default::default(),
286            // No `FORCE NOT NULL` for copy_to.
287            non_null_assertions: Vec::new(),
288            // No `REFRESH` for copy_to.
289            refresh_schedule: None,
290        };
291        df_desc.export_sink(self.select_id, sink_description);
292
293        // Prepare expressions in the assembled dataflow.
294        //
295        // Resolve all unmaterializable function calls except mz_now(), because
296        // we don't yet have a timestamp.
297        let style = ExprPrepStyle::OneShot {
298            logical_time: EvalTime::Deferred,
299            session,
300            catalog_state: self.catalog.state(),
301        };
302        df_desc.visit_children(
303            |r| prep_relation_expr(r, style),
304            |s| prep_scalar_expr(s, style),
305        )?;
306
307        // Set the `as_of` and `until` timestamps for the dataflow.
308        df_desc.set_as_of(timestamp_ctx.antichain());
309
310        // Get the single timestamp representing the `as_of` time.
311        let as_of = df_desc
312            .as_of
313            .clone()
314            .expect("as_of antichain")
315            .into_option()
316            .expect("unique as_of element");
317
318        // Resolve all unmaterializable function calls including mz_now().
319        let style = ExprPrepStyle::OneShot {
320            logical_time: EvalTime::Time(as_of),
321            session,
322            catalog_state: self.catalog.state(),
323        };
324        df_desc.visit_children(
325            |r| prep_relation_expr(r, style),
326            |s| prep_scalar_expr(s, style),
327        )?;
328
329        // Use the opportunity to name an `until` frontier that will prevent
330        // work we needn't perform. By default, `until` will be
331        // `Antichain::new()`, which prevents no updates and is safe.
332        //
333        // If `timestamp_ctx.antichain()` is empty, `timestamp_ctx.timestamp()`
334        // will return `None` and we use the default (empty) `until`. Otherwise,
335        // we expect to be able to set `until = as_of + 1` without an overflow.
336        if let Some(as_of) = timestamp_ctx.timestamp() {
337            if let Some(until) = as_of.checked_add(1) {
338                df_desc.until = Antichain::from_elem(until);
339                // Also updating the sink up_to
340                for (_, sink) in &mut df_desc.sink_exports {
341                    sink.up_to.clone_from(&df_desc.until);
342                }
343            } else {
344                warn!(as_of = %as_of, "as_of + 1 overflow");
345            }
346        }
347
348        // Construct TransformCtx for global optimization.
349        let mut transform_ctx = TransformCtx::global(
350            &df_builder,
351            &*stats,
352            &self.config.features,
353            &self.typecheck_ctx,
354            &self.repr_typecheck_ctx,
355            &mut df_meta,
356            Some(&self.metrics),
357        );
358        // Run global optimization.
359        mz_transform::optimize_dataflow(&mut df_desc, &mut transform_ctx, false)?;
360
361        if self.config.mode == OptimizeMode::Explain {
362            // Collect the list of indexes used by the dataflow at this point.
363            trace_plan!(at: "global", &df_meta.used_indexes(&df_desc));
364        }
365
366        // Ensure all expressions are normalized before finalizing.
367        for build in df_desc.objects_to_build.iter_mut() {
368            normalize_lets(&mut build.plan.0, &self.config.features)?
369        }
370
371        // Finalize the dataflow. This includes:
372        // - MIR ⇒ LIR lowering
373        // - LIR ⇒ LIR transforms
374        let df_desc = Plan::finalize_dataflow(df_desc, &self.config.features)?;
375
376        // Trace the pipeline output under `optimize`.
377        trace_plan(&df_desc);
378
379        self.duration += time.elapsed();
380        self.metrics
381            .observe_e2e_optimization_time("copy_to", self.duration);
382
383        Ok(GlobalLirPlan { df_desc, df_meta })
384    }
385}
386
387impl GlobalLirPlan {
388    /// Unwraps the parts of the final result of the optimization pipeline.
389    pub fn unapply(self) -> (LirDataflowDescription, DataflowMetainfo) {
390        (self.df_desc, self.df_meta)
391    }
392}