risingwave_frontend/optimizer/plan_node/generic/
delete.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::hash::Hash;
16
17use educe::Educe;
18use pretty_xmlish::{Pretty, Str, XmlNode};
19use risingwave_common::catalog::{Field, Schema, TableVersionId};
20use risingwave_common::types::DataType;
21
22use super::{DistillUnit, GenericPlanNode, GenericPlanRef};
23use crate::OptimizerContextRef;
24use crate::catalog::TableId;
25use crate::optimizer::plan_node::utils::childless_record;
26use crate::optimizer::property::FunctionalDependencySet;
27
28#[derive(Debug, Clone, Educe)]
29#[educe(PartialEq, Eq, Hash)]
30pub struct Delete<PlanRef: Eq + Hash> {
31    #[educe(PartialEq(ignore))]
32    #[educe(Hash(ignore))]
33    pub table_name: String, // explain-only
34    pub table_id: TableId,
35    pub table_version_id: TableVersionId,
36    pub input: PlanRef,
37    pub returning: bool,
38}
39
40impl<PlanRef: GenericPlanRef> Delete<PlanRef> {
41    pub fn clone_with_input<OtherPlanRef: Eq + Hash>(
42        &self,
43        input: OtherPlanRef,
44    ) -> Delete<OtherPlanRef> {
45        Delete {
46            table_name: self.table_name.clone(),
47            table_id: self.table_id,
48            table_version_id: self.table_version_id,
49            input,
50            returning: self.returning,
51        }
52    }
53
54    pub fn output_len(&self) -> usize {
55        if self.returning {
56            self.input.schema().len()
57        } else {
58            1
59        }
60    }
61}
62
63impl<PlanRef: GenericPlanRef> GenericPlanNode for Delete<PlanRef> {
64    fn functional_dependency(&self) -> FunctionalDependencySet {
65        FunctionalDependencySet::new(self.output_len())
66    }
67
68    fn schema(&self) -> Schema {
69        if self.returning {
70            self.input.schema().clone()
71        } else {
72            Schema::new(vec![Field::unnamed(DataType::Int64)])
73        }
74    }
75
76    fn stream_key(&self) -> Option<Vec<usize>> {
77        if self.returning {
78            Some(self.input.stream_key()?.to_vec())
79        } else {
80            Some(vec![])
81        }
82    }
83
84    fn ctx(&self) -> OptimizerContextRef {
85        self.input.ctx()
86    }
87}
88
89impl<PlanRef: Eq + Hash> Delete<PlanRef> {
90    pub fn new(
91        input: PlanRef,
92        table_name: String,
93        table_id: TableId,
94        table_version_id: TableVersionId,
95        returning: bool,
96    ) -> Self {
97        Self {
98            table_name,
99            table_id,
100            table_version_id,
101            input,
102            returning,
103        }
104    }
105}
106
107impl<PlanRef: Eq + Hash> DistillUnit for Delete<PlanRef> {
108    fn distill_with_name<'a>(&self, name: impl Into<Str<'a>>) -> XmlNode<'a> {
109        let mut vec = Vec::with_capacity(if self.returning { 2 } else { 1 });
110        vec.push(("table", Pretty::from(self.table_name.clone())));
111        if self.returning {
112            vec.push(("returning", Pretty::display(&true)));
113        }
114        childless_record(name, vec)
115    }
116}