Skip to main content

risingwave_frontend/optimizer/plan_node/
batch_hop_window.rs

1// Copyright 2022 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::batch_plan::HopWindowNode;
16use risingwave_pb::batch_plan::plan_node::NodeBody;
17
18use super::batch::prelude::*;
19use super::utils::impl_distill_by_unit;
20use super::{
21    BatchPlanRef as PlanRef, ExprRewritable, PlanBase, PlanTreeNodeUnary, ToBatchPb,
22    ToDistributedBatch, generic,
23};
24use crate::error::Result;
25use crate::expr::{Expr, ExprImpl, ExprRewriter, ExprVisitor};
26use crate::optimizer::plan_node::ToLocalBatch;
27use crate::optimizer::plan_node::expr_visitable::ExprVisitable;
28use crate::optimizer::property::{Order, RequiredDist};
29use crate::utils::ColIndexMappingRewriteExt;
30
31/// `BatchHopWindow` implements [`super::LogicalHopWindow`] to evaluate specified expressions on
32/// input rows
33#[derive(Debug, Clone, PartialEq, Eq, Hash)]
34pub struct BatchHopWindow {
35    pub base: PlanBase<Batch>,
36    core: generic::HopWindow<PlanRef>,
37    window_start_exprs: Vec<ExprImpl>,
38    window_end_exprs: Vec<ExprImpl>,
39}
40
41impl BatchHopWindow {
42    pub fn new(
43        core: generic::HopWindow<PlanRef>,
44        window_start_exprs: Vec<ExprImpl>,
45        window_end_exprs: Vec<ExprImpl>,
46    ) -> Self {
47        let distribution = core
48            .i2o_col_mapping()
49            .rewrite_provided_distribution(core.input.distribution());
50        let orders = if core.window_slide == core.window_size {
51            // A single window is emitted for each input chunk, so orders on pass-through columns
52            // are preserved.
53            core.input
54                .orders()
55                .into_iter()
56                .map(|order| core.i2o_col_mapping().rewrite_provided_order(&order))
57                .collect()
58        } else {
59            // `HopWindowExecutor` emits one whole input chunk for each overlapping window. An input
60            // ordered as `[1, 2]` is therefore emitted as `[1, 2, 1, 2, ...]`, so even orders on
61            // pass-through columns are not preserved.
62            vec![Order::any()]
63        };
64        let base = PlanBase::new_batch_with_core_and_orders(&core, distribution, orders);
65        BatchHopWindow {
66            base,
67            core,
68            window_start_exprs,
69            window_end_exprs,
70        }
71    }
72}
73impl_distill_by_unit!(BatchHopWindow, core, "BatchHopWindow");
74
75impl PlanTreeNodeUnary<Batch> for BatchHopWindow {
76    fn input(&self) -> PlanRef {
77        self.core.input.clone()
78    }
79
80    fn clone_with_input(&self, input: PlanRef) -> Self {
81        let mut core = self.core.clone();
82        core.input = input;
83        Self::new(
84            core,
85            self.window_start_exprs.clone(),
86            self.window_end_exprs.clone(),
87        )
88    }
89}
90
91impl_plan_tree_node_for_unary! { Batch, BatchHopWindow }
92
93impl ToDistributedBatch for BatchHopWindow {
94    fn to_distributed(&self) -> Result<PlanRef> {
95        self.to_distributed_with_required(&Order::any(), &RequiredDist::Any)
96    }
97
98    fn to_distributed_with_required(
99        &self,
100        required_order: &Order,
101        required_dist: &RequiredDist,
102    ) -> Result<PlanRef> {
103        // The hop operator will generate a multiplication of its input rows,
104        // so shuffling its input instead of its output will reduce the shuffling data
105        // communication.
106        // We pass the required dist to its input.
107        let input_required = self
108            .core
109            .o2i_col_mapping()
110            .rewrite_required_distribution(required_dist);
111        let input_required_order = if self.core.window_slide == self.core.window_size {
112            self.core
113                .o2i_col_mapping()
114                .rewrite_required_order(required_order)
115                .unwrap_or_else(Order::any)
116        } else {
117            // An overlapping hop window does not preserve input order, so sorting its input cannot
118            // satisfy an output order requirement. Enforce the requirement on the output below.
119            Order::any()
120        };
121        let new_input = self
122            .input()
123            .to_distributed_with_required(&input_required_order, &input_required)?;
124        let mut new_logical = self.core.clone();
125        new_logical.input = new_input;
126        let batch_plan = BatchHopWindow::new(
127            new_logical,
128            self.window_start_exprs.clone(),
129            self.window_end_exprs.clone(),
130        );
131        let batch_plan = required_order.enforce_if_not_satisfies(batch_plan.into())?;
132        required_dist.batch_enforce_if_not_satisfies(batch_plan, required_order)
133    }
134}
135
136impl ToBatchPb for BatchHopWindow {
137    fn to_batch_prost_body(&self) -> NodeBody {
138        NodeBody::HopWindow(HopWindowNode {
139            time_col: self.core.time_col.index() as _,
140            window_slide: Some(self.core.window_slide.into()),
141            window_size: Some(self.core.window_size.into()),
142            output_indices: self.core.output_indices.iter().map(|&x| x as u32).collect(),
143            window_start_exprs: self
144                .window_start_exprs
145                .clone()
146                .iter()
147                .map(|x| x.to_expr_proto())
148                .collect(),
149            window_end_exprs: self
150                .window_end_exprs
151                .clone()
152                .iter()
153                .map(|x| x.to_expr_proto())
154                .collect(),
155        })
156    }
157}
158
159impl ToLocalBatch for BatchHopWindow {
160    fn to_local(&self) -> Result<PlanRef> {
161        let new_input = self.input().to_local()?;
162        Ok(self.clone_with_input(new_input).into())
163    }
164}
165
166impl ExprRewritable<Batch> for BatchHopWindow {
167    fn has_rewritable_expr(&self) -> bool {
168        true
169    }
170
171    fn rewrite_exprs(&self, r: &mut dyn ExprRewriter) -> PlanRef {
172        Self::new(
173            self.core.clone(),
174            self.window_start_exprs
175                .clone()
176                .into_iter()
177                .map(|e| r.rewrite_expr(e))
178                .collect(),
179            self.window_end_exprs
180                .clone()
181                .into_iter()
182                .map(|e| r.rewrite_expr(e))
183                .collect(),
184        )
185        .into()
186    }
187}
188
189impl ExprVisitable for BatchHopWindow {
190    fn visit_exprs(&self, v: &mut dyn ExprVisitor) {
191        self.window_start_exprs.iter().for_each(|e| v.visit_expr(e));
192        self.window_end_exprs.iter().for_each(|e| v.visit_expr(e));
193    }
194}