Skip to main content

risingwave_frontend/optimizer/plan_node/
stream_changelog.rs

1// Copyright 2024 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use risingwave_pb::stream_plan::ChangeLogNode;
16use risingwave_pb::stream_plan::change_log_node::{Keyed, Mode};
17use risingwave_pb::stream_plan::stream_node::PbNodeBody;
18
19use super::expr_visitable::ExprVisitable;
20use super::stream::StreamPlanNodeMetadata;
21use super::stream::prelude::PhysicalPlanRef;
22use super::utils::impl_distill_by_unit;
23use super::{ExprRewritable, PlanBase, PlanTreeNodeUnary, Stream, StreamNode, generic};
24use crate::optimizer::StreamPlanRef as PlanRef;
25use crate::optimizer::property::{Distribution, MonotonicityMap, StreamKind};
26use crate::stream_fragmenter::BuildFragmentGraphState;
27
28#[derive(Debug, Clone, PartialEq, Eq, Hash)]
29pub struct StreamChangeLog {
30    pub base: PlanBase<Stream>,
31    core: generic::ChangeLog<PlanRef>,
32    distribution_keys: Vec<u32>,
33}
34
35impl StreamChangeLog {
36    pub fn new_with_dist(
37        core: generic::ChangeLog<PlanRef>,
38        dist: Distribution,
39        distribution_keys: Vec<u32>,
40    ) -> Self {
41        let input = core.input.clone();
42        let base = PlanBase::new_stream_with_core(
43            &core,
44            dist,
45            // The changelog will convert all delete/update to insert, so it must be append-only here.
46            StreamKind::AppendOnly,
47            input.emit_on_window_close(),
48            input.watermark_columns().clone(),
49            MonotonicityMap::new(), // TODO: derive monotonicity
50        );
51        StreamChangeLog {
52            base,
53            core,
54            distribution_keys,
55        }
56    }
57}
58
59impl PlanTreeNodeUnary<Stream> for StreamChangeLog {
60    fn input(&self) -> PlanRef {
61        self.core.input.clone()
62    }
63
64    fn clone_with_input(&self, input: PlanRef) -> Self {
65        let mut core = self.core.clone();
66        core.input = input;
67        Self::new_with_dist(
68            core,
69            self.base.distribution().clone(),
70            self.distribution_keys.clone(),
71        )
72    }
73}
74
75impl_plan_tree_node_for_unary! { Stream, StreamChangeLog }
76impl_distill_by_unit!(StreamChangeLog, core, "StreamChangeLog");
77
78impl StreamNode for StreamChangeLog {
79    fn to_stream_prost_body(&self, _state: &mut BuildFragmentGraphState) -> PbNodeBody {
80        let mode = self
81            .core
82            .key_indices
83            .is_some()
84            .then_some(Mode::Keyed(Keyed {}));
85        PbNodeBody::Changelog(Box::new(ChangeLogNode {
86            need_op: self.core.need_op,
87            distribution_keys: self.distribution_keys.clone(),
88            mode,
89            stream_keys: self
90                .core
91                .input
92                .expect_stream_key()
93                .iter()
94                .map(|&index| index as u32)
95                .collect(),
96        }))
97    }
98}
99
100impl ExprRewritable<Stream> for StreamChangeLog {}
101
102impl ExprVisitable for StreamChangeLog {}