risingwave_stream/from_proto/
gap_fill.rs

1// Copyright 2025 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::collections::HashMap;
16
17use itertools::Itertools;
18use risingwave_common::gap_fill::FillStrategy;
19use risingwave_expr::expr::build_non_strict_from_prost;
20use risingwave_pb::stream_plan::GapFillNode;
21use risingwave_storage::StateStore;
22
23use super::ExecutorBuilder;
24use crate::common::table::state_table::StateTableBuilder;
25use crate::error::StreamResult;
26use crate::executor::{Executor, GapFillExecutor, GapFillExecutorArgs};
27use crate::task::ExecutorParams;
28
29pub struct GapFillExecutorBuilder;
30
31impl ExecutorBuilder for GapFillExecutorBuilder {
32    type Node = GapFillNode;
33
34    async fn new_boxed_executor(
35        params: ExecutorParams,
36        node: &GapFillNode,
37        store: impl StateStore,
38    ) -> StreamResult<Executor> {
39        let [input]: [_; 1] = params.input.try_into().unwrap();
40
41        let time_column_index = node.get_time_column_index() as usize;
42
43        // Parse interval from ExprNode
44        let interval_expr_node = node.get_interval()?;
45        let interval_expr =
46            build_non_strict_from_prost(interval_expr_node, params.eval_error_report)?;
47
48        let fill_columns: Vec<usize> = node
49            .get_fill_columns()
50            .iter()
51            .map(|&x| x as usize)
52            .collect();
53
54        let fill_strategies: Vec<FillStrategy> = node
55            .get_fill_strategies()
56            .iter()
57            .map(|s| match s.as_str() {
58                "locf" => Ok(FillStrategy::Locf),
59                "interpolate" => Ok(FillStrategy::Interpolate),
60                "null" => Ok(FillStrategy::Null),
61                _ => anyhow::bail!("unknown fill strategy: {}", s),
62            })
63            .collect::<anyhow::Result<_>>()?;
64
65        let fill_columns_with_strategies: HashMap<usize, FillStrategy> =
66            fill_columns.into_iter().zip_eq(fill_strategies).collect();
67
68        let state_table =
69            StateTableBuilder::new(node.get_state_table().as_ref().unwrap(), store, None)
70                .forbid_preload_all_rows()
71                .build()
72                .await;
73
74        let exec = GapFillExecutor::new(GapFillExecutorArgs {
75            ctx: params.actor_context,
76            input,
77            schema: params.info.schema.clone(),
78            chunk_size: params.env.config().developer.chunk_size,
79            time_column_index,
80            fill_columns: fill_columns_with_strategies,
81            gap_interval: interval_expr,
82            state_table,
83        });
84
85        Ok((params.info, exec).into())
86    }
87}