Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add limit push down rule #16403

Merged
merged 3 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/query/sql/src/planner/optimizer/rule/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use crate::optimizer::rule::rewrite::RuleSplitAggregate;
use crate::optimizer::rule::transform::RuleCommuteJoinBaseTable;
use crate::optimizer::rule::transform::RuleEagerAggregation;
use crate::optimizer::rule::transform::RuleLeftExchangeJoin;
use crate::optimizer::rule::rewrite::RulePushDownLimit;
use crate::optimizer::rule::RuleID;
use crate::optimizer::rule::RulePtr;
use crate::MetadataRef;
Expand All @@ -63,6 +64,7 @@ impl RuleFactory {
RuleID::PushDownFilterScan => Ok(Box::new(RulePushDownFilterScan::new(metadata))),
RuleID::PushDownFilterSort => Ok(Box::new(RulePushDownFilterSort::new())),
RuleID::PushDownFilterProjectSet => Ok(Box::new(RulePushDownFilterProjectSet::new())),
RuleID::PushDownLimit => Ok(Box::new(RulePushDownLimit::new())),
RuleID::PushDownLimitUnion => Ok(Box::new(RulePushDownLimitUnion::new())),
RuleID::PushDownLimitScan => Ok(Box::new(RulePushDownLimitScan::new())),
RuleID::PushDownSortScan => Ok(Box::new(RulePushDownSortScan::new())),
Expand Down
2 changes: 2 additions & 0 deletions src/query/sql/src/planner/optimizer/rule/rewrite/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ mod rule_push_down_sort_scan;
mod rule_semi_to_inner_join;
mod rule_split_aggregate;
mod rule_try_apply_agg_index;
mod rule_push_down_limit;

pub use rule_commute_join::RuleCommuteJoin;
pub use rule_eliminate_eval_scalar::RuleEliminateEvalScalar;
Expand Down Expand Up @@ -72,3 +73,4 @@ pub use rule_push_down_sort_scan::RulePushDownSortScan;
pub use rule_semi_to_inner_join::RuleSemiToInnerJoin;
pub use rule_split_aggregate::RuleSplitAggregate;
pub use rule_try_apply_agg_index::RuleTryApplyAggIndex;
pub use rule_push_down_limit::RulePushDownLimit;
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2021 Datafuse Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::sync::Arc;

use databend_common_exception::Result;

use crate::optimizer::extract::Matcher;
use crate::optimizer::rule::Rule;
use crate::optimizer::rule::TransformResult;
use crate::optimizer::RuleID;
use crate::optimizer::SExpr;
use crate::plans::Limit;
use crate::plans::RelOp;
use crate::plans::RelOperator;
use crate::plans::DummyTableScan;

pub struct RulePushDownLimit {
id: RuleID,
matchers: Vec<Matcher>,
}

impl RulePushDownLimit {
pub fn new() -> Self {
Self {
id: RuleID::PushDownLimit,
matchers: vec![Matcher::MatchOp {
op_type: RelOp::Limit,
children: vec![Matcher::Leaf],
}],
}
}
}

impl Rule for RulePushDownLimit {
fn id(&self) -> RuleID {
self.id
}

fn apply(&self, s_expr: &SExpr, state: &mut TransformResult) -> Result<()> {
let limit: Limit = s_expr.plan().clone().try_into()?;

if let Some(limit_val) = limit.limit && limit_val == 0{
let dummy_scan = DummyTableScan;
let result = SExpr::create_leaf(Arc::new(RelOperator::DummyTableScan(dummy_scan)));
xudong963 marked this conversation as resolved.
Show resolved Hide resolved
state.add_result(result);
}

Ok(())
}

fn matchers(&self) -> &[Matcher] {
&self.matchers
}
}
2 changes: 2 additions & 0 deletions src/query/sql/src/planner/optimizer/rule/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub static DEFAULT_REWRITE_RULES: LazyLock<Vec<RuleID>> = LazyLock::new(|| {
RuleID::PushDownFilterUnion,
RuleID::PushDownFilterAggregate,
RuleID::PushDownFilterWindow,
RuleID::PushDownLimit,
RuleID::PushDownLimitUnion,
RuleID::PushDownLimitEvalScalar,
RuleID::PushDownLimitSort,
Expand Down Expand Up @@ -83,6 +84,7 @@ pub enum RuleID {
PushDownFilterSort,
PushDownFilterProjectSet,
PushDownFilterWindow,
PushDownLimit,
PushDownLimitUnion,
PushDownLimitOuterJoin,
PushDownLimitEvalScalar,
Expand Down
Loading