-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Depends on #58
- Loading branch information
Showing
5 changed files
with
89 additions
and
2 deletions.
There are no files selected for viewing
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
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,49 @@ | ||
use std::collections::HashMap; | ||
|
||
use optd_core::rules::{Rule, RuleMatcher}; | ||
use optd_core::{optimizer::Optimizer, rel_node::RelNode}; | ||
|
||
use crate::plan_nodes::{ | ||
ConstantExpr, ConstantType, LogicalEmptyRelation, OptRelNode, OptRelNodeTyp, | ||
}; | ||
|
||
use super::macros::define_rule; | ||
|
||
define_rule!( | ||
EliminateLimitRule, | ||
apply_eliminate_limit, | ||
(Limit, child, [skip], [fetch]) | ||
); | ||
|
||
/// Transformations: | ||
/// - Limit with skip 0 and no fetch -> Eliminate from the tree | ||
/// - Limit with limit 0 -> EmptyRelation | ||
fn apply_eliminate_limit( | ||
_optimizer: &impl Optimizer<OptRelNodeTyp>, | ||
EliminateLimitRulePicks { child, skip, fetch }: EliminateLimitRulePicks, | ||
) -> Vec<RelNode<OptRelNodeTyp>> { | ||
if let OptRelNodeTyp::Constant(ConstantType::UInt64) = skip.typ { | ||
if let OptRelNodeTyp::Constant(ConstantType::UInt64) = fetch.typ { | ||
let skip_val = ConstantExpr::from_rel_node(skip.into()) | ||
.unwrap() | ||
.value() | ||
.as_u64(); | ||
|
||
let fetch_val = ConstantExpr::from_rel_node(fetch.into()) | ||
.unwrap() | ||
.value() | ||
.as_u64(); | ||
|
||
// Bad convention to have u64 max represent None | ||
let fetch_is_none = fetch_val == u64::MAX; | ||
|
||
if fetch_is_none && skip_val == 0 { | ||
return vec![child]; | ||
} else if fetch_val == 0 { | ||
let node = LogicalEmptyRelation::new(false); | ||
return vec![node.into_rel_node().as_ref().clone()]; | ||
} | ||
} | ||
} | ||
vec![] | ||
} |
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,21 @@ | ||
-- (no id or description) | ||
create table t1(t1v1 int, t1v2 int); | ||
create table t2(t2v1 int, t2v3 int); | ||
insert into t1 values (0, 0), (1, 1), (2, 2); | ||
insert into t2 values (0, 200), (1, 201), (2, 202); | ||
|
||
/* | ||
3 | ||
3 | ||
*/ | ||
|
||
-- Test EliminateLimitRule (with 0 limit clause) | ||
select * from t1 LIMIT 0; | ||
|
||
/* | ||
LogicalLimit { skip: 0, fetch: 0 } | ||
└── LogicalProjection { exprs: [ #0, #1 ] } | ||
└── LogicalScan { table: t1 } | ||
PhysicalEmptyRelation { produce_one_row: false } | ||
*/ | ||
|
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,14 @@ | ||
|
||
- sql: | | ||
create table t1(t1v1 int, t1v2 int); | ||
create table t2(t2v1 int, t2v3 int); | ||
insert into t1 values (0, 0), (1, 1), (2, 2); | ||
insert into t2 values (0, 200), (1, 201), (2, 202); | ||
tasks: | ||
- execute | ||
- sql: | | ||
select * from t1 LIMIT 0; | ||
desc: Test EliminateLimitRule (with 0 limit clause) | ||
tasks: | ||
- explain:logical_optd,physical_optd | ||
- execute |