Skip to main content

risingwave_stream/from_proto/
eowc_over_window.rs

1// Copyright 2023 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 std::sync::Arc;
16
17use risingwave_expr::window_function::WindowFuncCall;
18use risingwave_pb::stream_plan::PbEowcOverWindowNode;
19use risingwave_storage::StateStore;
20
21use super::ExecutorBuilder;
22use crate::common::table::state_table::{StateTableBuilder, StateTableOpConsistencyLevel};
23use crate::error::StreamResult;
24use crate::executor::{EowcOverWindowExecutor, EowcOverWindowExecutorArgs, Executor};
25use crate::task::ExecutorParams;
26
27pub struct EowcOverWindowExecutorBuilder;
28
29impl_stream_node_body!(EowcOverWindow(PbEowcOverWindowNode) => EowcOverWindowExecutorBuilder);
30
31impl ExecutorBuilder for EowcOverWindowExecutorBuilder {
32    type Node = PbEowcOverWindowNode;
33
34    async fn new_boxed_executor(
35        params: ExecutorParams,
36        node: &Self::Node,
37        store: impl StateStore,
38    ) -> StreamResult<Executor> {
39        let [input]: [_; 1] = params.input.try_into().unwrap();
40        let calls: Vec<_> = node
41            .get_calls()
42            .iter()
43            .map(WindowFuncCall::from_protobuf)
44            .try_collect()?;
45        let partition_key_indices = node
46            .get_partition_by()
47            .iter()
48            .map(|i| *i as usize)
49            .collect();
50        let order_key_index = node.get_order_by()[0].get_column_index() as usize;
51        let vnodes = Some(Arc::new(params.vnode_bitmap));
52        let state_table =
53            StateTableBuilder::new(node.get_state_table()?, store.clone(), vnodes.clone())
54                .with_op_consistency_level(StateTableOpConsistencyLevel::Inconsistent)
55                .enable_preload_all_rows_by_config(&params.config)
56                .build()
57                .await;
58
59        // Build optional intermediate state table for persisting window function states
60        let intermediate_state_table =
61            if let Some(table_pb) = node.intermediate_state_table.as_ref() {
62                Some(
63                    StateTableBuilder::new(table_pb, store.clone(), vnodes.clone())
64                        // Use ConsistentOldValue because we update existing rows
65                        .with_op_consistency_level(StateTableOpConsistencyLevel::ConsistentOldValue)
66                        .enable_preload_all_rows_by_config(&params.config)
67                        .build()
68                        .await,
69                )
70            } else {
71                None
72            };
73
74        let watermark_trigger_table = if let Some(table_pb) = node.watermark_trigger_table.as_ref()
75        {
76            Some(
77                StateTableBuilder::new(table_pb, store, vnodes)
78                    .with_op_consistency_level(StateTableOpConsistencyLevel::ConsistentOldValue)
79                    .enable_preload_all_rows_by_config(&params.config)
80                    .build()
81                    .await,
82            )
83        } else {
84            None
85        };
86
87        let exec = EowcOverWindowExecutor::new(EowcOverWindowExecutorArgs {
88            actor_ctx: params.actor_context,
89
90            input,
91
92            schema: params.info.schema.clone(),
93            calls,
94            partition_key_indices,
95            order_key_index,
96            state_table,
97            watermark_epoch: params.watermark_epoch,
98            intermediate_state_table,
99            watermark_trigger_table,
100            version: node.version(),
101            chunk_size: params.config.developer.chunk_size,
102        });
103        Ok((params.info, exec).into())
104    }
105}