risingwave_frontend/optimizer/rule/
except_to_anti_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 risingwave_pb::plan_common::JoinType;
16
17use super::prelude::{PlanRef, *};
18use crate::optimizer::plan_node::generic::Agg;
19use crate::optimizer::plan_node::{LogicalExcept, LogicalJoin, PlanTreeNode};
20use crate::optimizer::rule::IntersectToSemiJoinRule;
21
22pub struct ExceptToAntiJoinRule {}
23impl Rule<Logical> for ExceptToAntiJoinRule {
24    fn apply(&self, plan: PlanRef) -> Option<PlanRef> {
25        let logical_except: &LogicalExcept = plan.as_logical_except()?;
26        let all = logical_except.all();
27        if all {
28            return None;
29        }
30
31        let inputs = logical_except.inputs();
32        let join = inputs
33            .into_iter()
34            .fold(None, |left, right| match left {
35                None => Some(right),
36                Some(left) => {
37                    let on =
38                        IntersectToSemiJoinRule::gen_null_safe_equal(left.clone(), right.clone());
39                    Some(LogicalJoin::create(left, right, JoinType::LeftAnti, on))
40                }
41            })
42            .unwrap();
43
44        Some(Agg::new(vec![], (0..join.schema().len()).collect(), join).into())
45    }
46}
47
48impl ExceptToAntiJoinRule {
49    pub fn create() -> BoxedRule {
50        Box::new(ExceptToAntiJoinRule {})
51    }
52}