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