risingwave_frontend/optimizer/rule/stream/
bushy_tree_join_ordering_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 thiserror_ext::AsReport;
16
17use super::super::super::plan_node::*;
18use crate::optimizer::rule::prelude::{PlanRef, *};
19
20/// Reorders a multi join into a bushy tree shape join tree with a minimal height.
21pub struct BushyTreeJoinOrderingRule {}
22
23/// If inputs of a multi join reach the limit, fallback to a left deep tree, because the search
24/// space could be too large to search.
25const BUSHY_TREE_JOIN_UPPER_LIMIT: usize = 10;
26
27/// To construct a bushy tree with a height lower than the left deep tree, we need tt least four
28/// inputs.
29const BUSHY_TREE_JOIN_LOWER_LIMIT: usize = 4;
30
31impl Rule<Logical> for BushyTreeJoinOrderingRule {
32    fn apply(&self, plan: PlanRef) -> Option<PlanRef> {
33        let join = plan.as_logical_multi_join()?;
34        if join.inputs().len() >= BUSHY_TREE_JOIN_LOWER_LIMIT
35            && join.inputs().len() <= BUSHY_TREE_JOIN_UPPER_LIMIT
36        {
37            match join.as_bushy_tree_join() {
38                Ok(plan) => Some(plan),
39                Err(e) => {
40                    eprintln!("{}", e.as_report());
41                    None
42                }
43            }
44        } else {
45            // Too many inputs, so fallback to a left deep tree.
46            let join_ordering = join.heuristic_ordering().ok()?;
47            let left_deep_join = join.as_reordered_left_deep_join(&join_ordering);
48            Some(left_deep_join)
49        }
50    }
51}
52
53impl BushyTreeJoinOrderingRule {
54    pub fn create() -> BoxedRule {
55        Box::new(BushyTreeJoinOrderingRule {})
56    }
57}