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(optimizer): index accelerating TopN #7726

Merged
merged 26 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
398d938
mimic logical_scan
y-wei Feb 6, 2023
4bf7616
let topn using index, first trial
y-wei Feb 6, 2023
ff9d519
remove useless code
y-wei Feb 6, 2023
4a9efc6
remove useless code
y-wei Feb 6, 2023
93ef95c
solve the order problem
y-wei Feb 6, 2023
3d6798d
have default any order
y-wei Feb 6, 2023
f9fcc0a
rename and replace with input()
y-wei Feb 7, 2023
baf8e6c
add license
y-wei Feb 7, 2023
495fa8e
using index_table.pk rather than index_items
y-wei Feb 7, 2023
978a480
Merge branch 'topn_acc' of github.com:ClearloveWei/risingwave_e into …
y-wei Feb 7, 2023
616dc8a
update planner test
y-wei Feb 7, 2023
fbd1b3d
add e2e tests
y-wei Feb 7, 2023
3e7679e
seq_scan_node specifies chunk size at executor
y-wei Feb 7, 2023
ff4eaa8
check and dashboard prost
y-wei Feb 7, 2023
5e51615
support primary key
y-wei Feb 8, 2023
8265643
format
y-wei Feb 8, 2023
e277ee8
make chunk_size optional, replace satisfies with supersets
y-wei Feb 8, 2023
37ce43c
apply s2p mapping before comparing index and topn order
y-wei Feb 8, 2023
fc94781
translate index column_idx to topn_col_idx
y-wei Feb 8, 2023
cb87843
workaround for optional in proto
y-wei Feb 8, 2023
8f352b3
check
y-wei Feb 8, 2023
aeb41e4
replace required to output and include it in the primary key part
y-wei Feb 8, 2023
e7cb28d
apply optimization iff predicate is always true
y-wei Feb 9, 2023
f537a4f
Merge branch 'main' into topn_acc
y-wei Feb 9, 2023
cd261db
update planner test
y-wei Feb 9, 2023
260834c
Merge branch 'main' into topn_acc
mergify[bot] Feb 9, 2023
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
7 changes: 7 additions & 0 deletions src/frontend/src/optimizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,13 @@ impl PlanRoot {
ApplyOrder::TopDown,
);

plan = self.optimize_by_rules(
plan,
"Agg on Index".to_string(),
vec![AggOnIndexRule::create()],
ApplyOrder::TopDown,
);

#[cfg(debug_assertions)]
InputRefValidator.validate(plan.clone());

Expand Down
11 changes: 9 additions & 2 deletions src/frontend/src/optimizer/plan_node/batch_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,15 @@ impl BatchLimit {
let new_offset = 0;
let logical_partial_limit = LogicalLimit::new(input, new_limit, new_offset);
let batch_partial_limit = Self::new(logical_partial_limit);
let ensure_single_dist = RequiredDist::single()
.enforce_if_not_satisfies(batch_partial_limit.into(), &Order::any())?;
let any_order = Order::any();
let ensure_single_dist = RequiredDist::single().enforce_if_not_satisfies(
batch_partial_limit.into(),
if self.order().field_order.is_empty() {
&any_order
} else {
&self.order()
},
)?;
let batch_global_limit = self.clone_with_input(ensure_single_dist);
Ok(batch_global_limit.into())
}
Expand Down
5 changes: 5 additions & 0 deletions src/frontend/src/optimizer/plan_node/logical_topn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ impl LogicalTopN {
&self.core.group_key
}

// For Optimizing TopN
pub fn get_child(&self) -> PlanRef {
self.core.input.clone()
}

Copy link
Contributor

@chenzl25 chenzl25 Feb 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can just use existing method input().

pub(super) fn fmt_with_name(&self, f: &mut fmt::Formatter<'_>, name: &str) -> fmt::Result {
let mut builder = f.debug_struct(name);
let input = self.input();
Expand Down
60 changes: 60 additions & 0 deletions src/frontend/src/optimizer/rule/agg_on_index_rule.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use super::{BoxedRule, Rule};
use crate::optimizer::plan_node::{LogicalLimit, LogicalScan, LogicalTopN};
use crate::optimizer::property::Direction::Asc;
use crate::optimizer::property::{FieldOrder, Order};
use crate::optimizer::PlanRef;

pub struct AggOnIndexRule {}

impl Rule for AggOnIndexRule {
fn apply(&self, plan: PlanRef) -> Option<PlanRef> {
let logical_topn: &LogicalTopN = plan.as_logical_top_n()?;
let logical_scan: LogicalScan = logical_topn.get_child().as_logical_scan()?.to_owned();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This rule name is inconsistent with its match pattern. Maybe TopNOnIndexRule is more appropriate.

let order = logical_topn.topn_order();
if order.field_order.is_empty() {
return None;
}
let index = logical_scan.indexes().iter().find(|idx| {
Order {
field_order: idx
.index_item
.iter()
.map(|idx_item| FieldOrder {
index: idx_item.index,
direct: Asc,
})
.collect(),
}
.satisfies(order)
})?;

let p2s_mapping = index.primary_to_secondary_mapping();

let index_scan = if logical_scan
.required_col_idx()
.iter()
.all(|x| p2s_mapping.contains_key(x))
{
Some(logical_scan.to_index_scan(
&index.name,
index.index_table.table_desc().into(),
p2s_mapping,
))
} else {
None
}?;

let logical_limit = LogicalLimit::create(
index_scan.into(),
logical_topn.limit(),
logical_topn.offset(),
);
Some(logical_limit)
}
}

impl AggOnIndexRule {
pub fn create() -> BoxedRule {
Box::new(AggOnIndexRule {})
}
}
3 changes: 3 additions & 0 deletions src/frontend/src/optimizer/rule/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ mod dag_to_tree_rule;
pub use dag_to_tree_rule::*;
mod apply_share_eliminate_rule;
pub use apply_share_eliminate_rule::*;
mod agg_on_index_rule;
pub use agg_on_index_rule::*;
mod stream;
pub use stream::filter_with_now_to_join_rule::*;

Expand Down Expand Up @@ -115,6 +117,7 @@ macro_rules! for_all_rules {
,{DagToTreeRule}
,{AggDedupGroupKeyRule}
,{FilterWithNowToJoinRule}
,{AggOnIndexRule}
}
};
}
Expand Down