|
| 1 | +// Copyright 2022 Datafuse 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 | + |
| 15 | +use crate::sql::optimizer::rule::Rule; |
| 16 | +use crate::sql::optimizer::rule::TransformState; |
| 17 | +use crate::sql::optimizer::RuleID; |
| 18 | +use crate::sql::optimizer::SExpr; |
| 19 | +use crate::sql::plans::JoinType; |
| 20 | +use crate::sql::plans::Limit; |
| 21 | +use crate::sql::plans::LogicalInnerJoin; |
| 22 | +use crate::sql::plans::PatternPlan; |
| 23 | +use crate::sql::plans::RelOp; |
| 24 | +use crate::sql::plans::RelOperator; |
| 25 | + |
| 26 | +/// Input: Limit |
| 27 | +/// | |
| 28 | +/// Left Outer Join |
| 29 | +/// / \ |
| 30 | +/// * * |
| 31 | +/// |
| 32 | +/// Output: |
| 33 | +/// Limit |
| 34 | +/// | |
| 35 | +/// Left Outer Join |
| 36 | +/// / \ |
| 37 | +/// Limit Limit |
| 38 | +/// / \ |
| 39 | +/// * * |
| 40 | +pub struct RulePushDownLimitOuterJoin { |
| 41 | + id: RuleID, |
| 42 | + pattern: SExpr, |
| 43 | +} |
| 44 | + |
| 45 | +impl RulePushDownLimitOuterJoin { |
| 46 | + pub fn new() -> Self { |
| 47 | + Self { |
| 48 | + id: RuleID::PushDownLimitOuterJoin, |
| 49 | + pattern: SExpr::create_unary( |
| 50 | + PatternPlan { |
| 51 | + plan_type: RelOp::Limit, |
| 52 | + } |
| 53 | + .into(), |
| 54 | + SExpr::create_binary( |
| 55 | + PatternPlan { |
| 56 | + plan_type: RelOp::LogicalInnerJoin, |
| 57 | + } |
| 58 | + .into(), |
| 59 | + SExpr::create_leaf( |
| 60 | + PatternPlan { |
| 61 | + plan_type: RelOp::Pattern, |
| 62 | + } |
| 63 | + .into(), |
| 64 | + ), |
| 65 | + SExpr::create_leaf( |
| 66 | + PatternPlan { |
| 67 | + plan_type: RelOp::Pattern, |
| 68 | + } |
| 69 | + .into(), |
| 70 | + ), |
| 71 | + ), |
| 72 | + ), |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +impl Rule for RulePushDownLimitOuterJoin { |
| 78 | + fn id(&self) -> RuleID { |
| 79 | + self.id |
| 80 | + } |
| 81 | + |
| 82 | + fn apply(&self, s_expr: &SExpr, state: &mut TransformState) -> common_exception::Result<()> { |
| 83 | + let limit: Limit = s_expr.plan().clone().try_into()?; |
| 84 | + if limit.limit.is_some() { |
| 85 | + let child = s_expr.child(0)?; |
| 86 | + let join: LogicalInnerJoin = child.plan().clone().try_into()?; |
| 87 | + match join.join_type { |
| 88 | + JoinType::Left | JoinType::Full => { |
| 89 | + state.add_result(s_expr.replace_children(vec![child.replace_children(vec![ |
| 90 | + SExpr::create_unary( |
| 91 | + RelOperator::Limit(limit.clone()), |
| 92 | + child.child(0)?.clone(), |
| 93 | + ), |
| 94 | + SExpr::create_unary(RelOperator::Limit(limit), child.child(1)?.clone()), |
| 95 | + ])])) |
| 96 | + } |
| 97 | + _ => {} |
| 98 | + } |
| 99 | + } |
| 100 | + Ok(()) |
| 101 | + } |
| 102 | + |
| 103 | + fn pattern(&self) -> &SExpr { |
| 104 | + &self.pattern |
| 105 | + } |
| 106 | +} |
0 commit comments