risingwave_frontend/optimizer/rule/
union_merge_rule.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 super::prelude::{PlanRef, *};
16use crate::optimizer::plan_node::{LogicalUnion, PlanTreeNode};
17
18pub struct UnionMergeRule {}
19impl Rule<Logical> for UnionMergeRule {
20    fn apply(&self, plan: PlanRef) -> Option<PlanRef> {
21        let top_union: &LogicalUnion = plan.as_logical_union()?;
22        let top_all = top_union.all();
23        let mut new_inputs = vec![];
24        let mut has_merge = false;
25        for input in top_union.inputs() {
26            if let Some(bottom_union) = input.as_logical_union()
27                && bottom_union.all() == top_all
28            {
29                new_inputs.extend(bottom_union.inputs());
30                has_merge = true;
31            } else {
32                new_inputs.push(input);
33            }
34        }
35
36        if has_merge {
37            Some(top_union.clone_with_inputs(&new_inputs))
38        } else {
39            None
40        }
41    }
42}
43
44impl UnionMergeRule {
45    pub fn create() -> BoxedRule {
46        Box::new(UnionMergeRule {})
47    }
48}