risingwave_frontend/optimizer/plan_node/
to_prost.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 anyhow::anyhow;
16use risingwave_pb::batch_plan::plan_node as pb_batch_node;
17use risingwave_pb::stream_plan::stream_node as pb_stream_node;
18
19use super::*;
20
21pub trait TryToBatchPb {
22    fn try_to_batch_prost_body(&self) -> SchedulerResult<pb_batch_node::NodeBody> {
23        // Originally we panic in the following way
24        // panic!("convert into distributed is only allowed on batch plan")
25        Err(anyhow!(
26            "Node {} cannot be convert to batch node",
27            std::any::type_name::<Self>()
28        )
29        .into())
30    }
31}
32
33pub trait ToBatchPb {
34    fn to_batch_prost_body(&self) -> pb_batch_node::NodeBody;
35}
36
37impl<T: ToBatchPb> TryToBatchPb for T {
38    fn try_to_batch_prost_body(&self) -> SchedulerResult<pb_batch_node::NodeBody> {
39        Ok(self.to_batch_prost_body())
40    }
41}
42
43pub trait TryToStreamPb {
44    fn try_to_stream_prost_body(
45        &self,
46        _state: &mut BuildFragmentGraphState,
47    ) -> SchedulerResult<pb_stream_node::NodeBody> {
48        // Originally we panic in the following way
49        // panic!("convert into distributed is only allowed on stream plan")
50        Err(anyhow!(
51            "Node {} cannot be convert to stream node",
52            std::any::type_name::<Self>()
53        )
54        .into())
55    }
56}
57
58impl<T: StreamNode> TryToStreamPb for T {
59    fn try_to_stream_prost_body(
60        &self,
61        state: &mut BuildFragmentGraphState,
62    ) -> SchedulerResult<pb_stream_node::NodeBody> {
63        Ok(self.to_stream_prost_body(state))
64    }
65}
66
67pub trait StreamNode {
68    fn to_stream_prost_body(&self, state: &mut BuildFragmentGraphState)
69    -> pb_stream_node::NodeBody;
70}