-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implment column ref as logical property (#66)
Derive base table column ref for logical nodes, so that we can know which columns the `ColumnRef` objects in a predicate refers to. Tested manually on the two demos. Not sure how to unit test it.
- Loading branch information
Showing
8 changed files
with
140 additions
and
12 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
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod column_ref; | ||
pub mod schema; |
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,116 @@ | ||
use std::{ops::Deref, sync::Arc}; | ||
|
||
use optd_core::property::PropertyBuilder; | ||
|
||
use crate::plan_nodes::OptRelNodeTyp; | ||
|
||
use super::schema::Catalog; | ||
|
||
#[derive(Clone, Debug)] | ||
pub enum ColumnRef { | ||
BaseTableColumnRef { table: String, col_idx: usize }, | ||
ChildColumnRef { col_idx: usize }, | ||
Derived, | ||
} | ||
|
||
pub type GroupColumnRefs = Vec<ColumnRef>; | ||
|
||
pub struct ColumnRefPropertyBuilder { | ||
catalog: Arc<dyn Catalog>, | ||
} | ||
|
||
impl ColumnRefPropertyBuilder { | ||
pub fn new(catalog: Arc<dyn Catalog>) -> Self { | ||
Self { catalog } | ||
} | ||
|
||
fn concat_children_properties(children: &[&GroupColumnRefs]) -> GroupColumnRefs { | ||
children.iter().flat_map(Deref::deref).cloned().collect() | ||
} | ||
} | ||
|
||
impl PropertyBuilder<OptRelNodeTyp> for ColumnRefPropertyBuilder { | ||
type Prop = GroupColumnRefs; | ||
|
||
fn derive( | ||
&self, | ||
typ: OptRelNodeTyp, | ||
data: Option<optd_core::rel_node::Value>, | ||
children: &[&Self::Prop], | ||
) -> Self::Prop { | ||
match typ { | ||
// Should account for PhysicalScan. | ||
OptRelNodeTyp::Scan => { | ||
let table_name = data.unwrap().as_str().to_string(); | ||
let schema = self.catalog.get(&table_name); | ||
let column_cnt = schema.fields.len(); | ||
(0..column_cnt) | ||
.map(|i| ColumnRef::BaseTableColumnRef { | ||
table: table_name.clone(), | ||
col_idx: i, | ||
}) | ||
.collect() | ||
} | ||
OptRelNodeTyp::ColumnRef => { | ||
let col_idx = data.unwrap().as_i64(); | ||
vec![ColumnRef::ChildColumnRef { | ||
col_idx: col_idx as usize, | ||
}] | ||
} | ||
OptRelNodeTyp::List => { | ||
// Concatentate the children properties. | ||
Self::concat_children_properties(children) | ||
} | ||
OptRelNodeTyp::Projection => children[1] | ||
.iter() | ||
.map(|p| match p { | ||
ColumnRef::ChildColumnRef { col_idx } => children[0][*col_idx].clone(), | ||
ColumnRef::Derived => ColumnRef::Derived, | ||
_ => panic!("projection expr must be Derived or ChildColumnRef"), | ||
}) | ||
.collect(), | ||
// Should account for all physical join types. | ||
OptRelNodeTyp::Join(_) => { | ||
// Concatenate left and right children properties. | ||
Self::concat_children_properties(&children[0..2]) | ||
} | ||
OptRelNodeTyp::Agg => { | ||
// Group by columns first. | ||
let mut group_by_col_refs: Vec<_> = children[2] | ||
.iter() | ||
.map(|p| { | ||
let col_idx = match p { | ||
ColumnRef::ChildColumnRef { col_idx } => *col_idx, | ||
_ => panic!("group by expr must be ColumnRef"), | ||
}; | ||
children[0][col_idx].clone() | ||
}) | ||
.collect(); | ||
// Then the aggregate expressions. These columns, (e.g. SUM, COUNT, etc.) are derived columns. | ||
let agg_expr_cnt = children[1].len(); | ||
group_by_col_refs.extend((0..agg_expr_cnt).map(|_| ColumnRef::Derived)); | ||
group_by_col_refs | ||
} | ||
OptRelNodeTyp::Filter | ||
| OptRelNodeTyp::Sort | ||
| OptRelNodeTyp::Limit | ||
| OptRelNodeTyp::SortOrder(_) => children[0].clone(), | ||
OptRelNodeTyp::Cast => { | ||
// FIXME: we just assume the column value does not change. | ||
children[0].clone() | ||
} | ||
OptRelNodeTyp::Constant(_) | ||
| OptRelNodeTyp::Func(_) | ||
| OptRelNodeTyp::BinOp(_) | ||
| OptRelNodeTyp::Between | ||
| OptRelNodeTyp::EmptyRelation => { | ||
vec![ColumnRef::Derived] | ||
} | ||
_ => unimplemented!("Unsupported rel node type {:?}", typ), | ||
} | ||
} | ||
|
||
fn property_name(&self) -> &'static str { | ||
"column_ref" | ||
} | ||
} |
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