risingwave_frontend/optimizer/rule/
apply_to_join_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::{LogicalApply, LogicalJoin};
17
18/// Convert `LogicalApply` to `LogicalJoin` if it is uncorrelated and doesn't require max one row
19/// restriction.
20pub struct ApplyToJoinRule {}
21impl Rule<Logical> for ApplyToJoinRule {
22    fn apply(&self, plan: PlanRef) -> Option<PlanRef> {
23        let apply: &LogicalApply = plan.as_logical_apply()?;
24        let (left, right, on, join_type, _correlated_id, correlated_indices, max_one_row) =
25            apply.clone().decompose();
26
27        if !max_one_row && correlated_indices.is_empty() {
28            Some(LogicalJoin::new(left, right, join_type, on).into())
29        } else {
30            None
31        }
32    }
33}
34
35impl ApplyToJoinRule {
36    pub fn create() -> BoxedRule {
37        Box::new(ApplyToJoinRule {})
38    }
39}