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

fix(optimizer): fix logical scan pk derive #3171

Merged
merged 4 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions src/frontend/src/optimizer/plan_node/logical_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ impl LogicalProject {
let i2o = Self::i2o_col_mapping_inner(input_schema.len(), exprs);
input_pk
.iter()
.filter_map(|pk_col| i2o.try_map(*pk_col))
.collect::<Vec<_>>()
.map(|pk_col| i2o.try_map(*pk_col))
.collect::<Option<Vec<_>>>()
.unwrap_or_default()
Copy link
Contributor

Choose a reason for hiding this comment

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

.expect("pk lost during optimization") instead of returning an empty pk set?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the batch query does not need the pk... I am thinking if we can move the pk property only in stream plan node.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

.expect("pk lost during optimization")

I will add it as an assert when stream plan node created

}

pub fn exprs(&self) -> &Vec<ExprImpl> {
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/optimizer/plan_node/logical_scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ impl ToStream for LogicalScan {
true => {
let mut col_ids = HashSet::new();

for idx in &self.required_col_idx {
for idx in &self.output_col_idx {
col_ids.insert(self.table_desc.columns[*idx].column_id);
}
let mut col_id_to_tb_idx = HashMap::new();
Expand Down
3 changes: 1 addition & 2 deletions src/frontend/src/optimizer/property/distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,8 @@ impl Distribution {
/// valid.
pub fn dist_column_indices(&self) -> &[usize] {
match self {
Distribution::Single => Default::default(),
Distribution::Single | Distribution::SomeShard => Default::default(),
Distribution::HashShard(dists) => dists,
_ => unreachable!(),
}
}
}
Expand Down
38 changes: 24 additions & 14 deletions src/frontend/test_runner/tests/testdata/pk_derive.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@
StreamHashAgg { group_keys: [$0], aggs: [count, min($1)] }
StreamExchange { dist: HashShard([0]) }
StreamTableScan { table: t, columns: [id, v, _row_id], pk_indices: [2] }
- sql: |
create table t (v1 varchar, v2 varchar, v3 varchar);
select
*
from
t
group by
v1,
v2,
v3;
optimized_logical_plan: |
LogicalAgg { group_keys: [0, 1, 2], agg_calls: [] }
LogicalScan { table: t, columns: [v1, v2, v3] }
stream_plan: |
StreamMaterialize { columns: [v1, v2, v3, agg#0(hidden)], pk_columns: [v1, v2, v3] }
StreamHashAgg { group_keys: [$0, $1, $2], aggs: [count] }
StreamExchange { dist: HashShard([0, 1, 2]) }
StreamTableScan { table: t, columns: [v1, v2, v3, _row_id], pk_indices: [3] }
- sql: |
create table t (v1 varchar, v2 varchar, v3 varchar);
create materialized view mv as
Expand All @@ -74,22 +92,14 @@
v2,
v3;
select
max(v1) as v1
v1
from
mv
where
v3 = 'world' or v3 = 'hello';
batch_plan: |
BatchSimpleAgg { aggs: [max($0)] }
BatchExchange { order: [], dist: Single }
BatchSimpleAgg { aggs: [max($0)] }
BatchProject { exprs: [$0] }
BatchFilter { predicate: (($1 = 'world':Varchar) OR ($1 = 'hello':Varchar)) }
BatchScan { table: mv, columns: [v1, v3] }
optimized_logical_plan: |
LogicalScan { table: mv, output_columns: [v1], required_columns: [$0:v1, $2:v3], predicate: (($2 = 'world':Varchar) OR ($2 = 'hello':Varchar)) }
stream_plan: |
StreamMaterialize { columns: [agg#0(hidden), v1], pk_columns: [agg#0, v1] }
StreamSimpleAgg { aggs: [count, max($0)] }
StreamExchange { dist: Single }
StreamProject { exprs: [$0, $1] }
StreamFilter { predicate: (($2 = 'world':Varchar) OR ($2 = 'hello':Varchar)) }
StreamTableScan { table: mv, columns: [v1, v2, v3], pk_indices: [0, 1, 2] }
StreamMaterialize { columns: [v1, v2(hidden), v3(hidden)], pk_columns: [v1, v2, v3] }
StreamFilter { predicate: (($2 = 'world':Varchar) OR ($2 = 'hello':Varchar)) }
StreamTableScan { table: mv, columns: [v1, v2, v3], pk_indices: [0, 1, 2] }