risingwave_frontend/optimizer/plan_node/generic/
share.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::cell::RefCell;
16use std::hash::Hash;
17
18use risingwave_common::catalog::Schema;
19
20use super::{GenericPlanNode, GenericPlanRef};
21use crate::OptimizerContextRef;
22use crate::optimizer::property::FunctionalDependencySet;
23
24#[derive(Debug, Clone, PartialEq, Eq)]
25pub struct Share<PlanRef> {
26    pub input: RefCell<PlanRef>,
27}
28
29impl<P: Hash> Hash for Share<P> {
30    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
31        self.input.borrow().hash(state);
32    }
33}
34
35impl<PlanRef: GenericPlanRef> Share<PlanRef> {
36    pub fn new(input: PlanRef) -> Self {
37        Self {
38            input: RefCell::new(input),
39        }
40    }
41
42    pub fn replace_input(&self, plan: PlanRef) {
43        *self.input.borrow_mut() = plan;
44    }
45}
46
47impl<PlanRef: GenericPlanRef> GenericPlanNode for Share<PlanRef> {
48    fn schema(&self) -> Schema {
49        self.input.borrow().schema().clone()
50    }
51
52    fn stream_key(&self) -> Option<Vec<usize>> {
53        Some(self.input.borrow().stream_key()?.to_vec())
54    }
55
56    fn ctx(&self) -> OptimizerContextRef {
57        self.input.borrow().ctx()
58    }
59
60    fn functional_dependency(&self) -> FunctionalDependencySet {
61        self.input.borrow().functional_dependency().clone()
62    }
63}