-
Notifications
You must be signed in to change notification settings - Fork 763
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add limit push down rule (#16403)
* feat: add limit push down rule * fix * fix test
- Loading branch information
Showing
6 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/query/sql/src/planner/optimizer/rule/rewrite/rule_push_down_limit.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// 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 databend_common_expression::Column; | ||
use databend_common_expression::DataField; | ||
use databend_common_expression::DataSchemaRefExt; | ||
|
||
use crate::optimizer::extract::Matcher; | ||
use crate::optimizer::rule::Rule; | ||
use crate::optimizer::rule::TransformResult; | ||
use crate::optimizer::RelExpr; | ||
use crate::optimizer::RuleID; | ||
use crate::optimizer::SExpr; | ||
use crate::plans::ConstantTableScan; | ||
use crate::plans::Limit; | ||
use crate::plans::Operator; | ||
use crate::plans::RelOp; | ||
use crate::plans::RelOperator; | ||
use crate::MetadataRef; | ||
|
||
pub struct RulePushDownLimit { | ||
id: RuleID, | ||
metadata: MetadataRef, | ||
matchers: Vec<Matcher>, | ||
} | ||
|
||
impl RulePushDownLimit { | ||
pub fn new(metadata: MetadataRef) -> Self { | ||
Self { | ||
id: RuleID::PushDownLimit, | ||
metadata, | ||
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 output_columns = limit | ||
.derive_relational_prop(&RelExpr::with_s_expr(s_expr))? | ||
.output_columns | ||
.clone(); | ||
let metadata = self.metadata.read(); | ||
let mut fields = Vec::with_capacity(output_columns.len()); | ||
for col in output_columns.iter() { | ||
fields.push(DataField::new( | ||
&col.to_string(), | ||
metadata.column(*col).data_type(), | ||
)); | ||
} | ||
let empty_scan = ConstantTableScan { | ||
values: vec![Column::Null { len: 0 }; output_columns.len()], | ||
num_rows: 0, | ||
schema: DataSchemaRefExt::create(fields), | ||
columns: output_columns, | ||
}; | ||
let result = SExpr::create_leaf(Arc::new(RelOperator::ConstantTableScan(empty_scan))); | ||
state.add_result(result); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn matchers(&self) -> &[Matcher] { | ||
&self.matchers | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters