mz_adapter/
continual_task.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//! Common methods for `CONTINUAL TASK`s.
11
12use std::sync::Arc;
13
14use mz_catalog::memory::objects::ContinualTask;
15use mz_expr::Id;
16use mz_expr::visit::Visit;
17use mz_repr::GlobalId;
18use mz_sql::names::ResolvedIds;
19use mz_sql::plan::{self, HirRelationExpr};
20use timely::progress::Antichain;
21
22use crate::AdapterError;
23
24pub fn ct_item_from_plan(
25    plan: plan::CreateContinualTaskPlan,
26    global_id: GlobalId,
27    resolved_ids: ResolvedIds,
28) -> Result<ContinualTask, AdapterError> {
29    let plan::CreateContinualTaskPlan {
30        name: _,
31        placeholder_id,
32        desc,
33        input_id,
34        with_snapshot,
35        continual_task:
36            plan::MaterializedView {
37                create_sql,
38                cluster_id,
39                expr: mut raw_expr,
40                dependencies,
41                column_names: _,
42                replacement_target: _,
43                non_null_assertions: _,
44                compaction_window: _,
45                refresh_schedule: _,
46                as_of,
47            },
48    } = plan;
49
50    // Replace any placeholder LocalIds.
51    if let Some(placeholder_id) = placeholder_id {
52        raw_expr.visit_mut_post(&mut |expr| match expr {
53            HirRelationExpr::Get { id, .. } if *id == Id::Local(placeholder_id) => {
54                *id = Id::Global(global_id);
55            }
56            _ => {}
57        })?;
58    }
59    // TODO(alter_table): `dependencies` doesn't include the `CatalogItemId` for self and we can't
60    // look it up in the Catalog from it's `GlobalId` because we haven't yet added this item.
61
62    Ok(ContinualTask {
63        create_sql,
64        global_id,
65        input_id,
66        with_snapshot,
67        raw_expr: Arc::new(raw_expr),
68        desc,
69        resolved_ids,
70        dependencies,
71        cluster_id,
72        initial_as_of: as_of.map(Antichain::from_elem),
73    })
74}