risingwave_frontend/optimizer/plan_node/generic/
delete.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::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 pk_indices: Vec<usize>,
37    pub input: PlanRef,
38    pub returning: bool,
39}
40
41impl<PlanRef: GenericPlanRef> Delete<PlanRef> {
42    pub fn clone_with_input<OtherPlanRef: Eq + Hash>(
43        &self,
44        input: OtherPlanRef,
45    ) -> Delete<OtherPlanRef> {
46        Delete {
47            table_name: self.table_name.clone(),
48            table_id: self.table_id,
49            table_version_id: self.table_version_id,
50            pk_indices: self.pk_indices.clone(),
51            input,
52            returning: self.returning,
53        }
54    }
55
56    pub fn output_len(&self) -> usize {
57        if self.returning {
58            self.input.schema().len()
59        } else {
60            1
61        }
62    }
63}
64
65impl<PlanRef: GenericPlanRef> GenericPlanNode for Delete<PlanRef> {
66    fn functional_dependency(&self) -> FunctionalDependencySet {
67        FunctionalDependencySet::new(self.output_len())
68    }
69
70    fn schema(&self) -> Schema {
71        if self.returning {
72            self.input.schema().clone()
73        } else {
74            Schema::new(vec![Field::unnamed(DataType::Int64)])
75        }
76    }
77
78    fn stream_key(&self) -> Option<Vec<usize>> {
79        if self.returning {
80            Some(self.input.stream_key()?.to_vec())
81        } else {
82            Some(vec![])
83        }
84    }
85
86    fn ctx(&self) -> OptimizerContextRef {
87        self.input.ctx()
88    }
89}
90
91impl<PlanRef: Eq + Hash> Delete<PlanRef> {
92    pub fn new(
93        input: PlanRef,
94        table_name: String,
95        table_id: TableId,
96        table_version_id: TableVersionId,
97        pk_indices: Vec<usize>,
98        returning: bool,
99    ) -> Self {
100        Self {
101            table_name,
102            table_id,
103            table_version_id,
104            pk_indices,
105            input,
106            returning,
107        }
108    }
109}
110
111impl<PlanRef: Eq + Hash> DistillUnit for Delete<PlanRef> {
112    fn distill_with_name<'a>(&self, name: impl Into<Str<'a>>) -> XmlNode<'a> {
113        let mut vec = Vec::with_capacity(if self.returning { 2 } else { 1 });
114        vec.push(("table", Pretty::from(self.table_name.clone())));
115        if self.returning {
116            vec.push(("returning", Pretty::display(&true)));
117        }
118        childless_record(name, vec)
119    }
120}