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

refactor(binder): support BoundTableRef enum #41

Merged
merged 1 commit into from
Oct 14, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/binder/statement/select.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use crate::binder::BoundTableRef;
use crate::parser::{Query, SelectItem, SetExpr};

#[derive(Debug, PartialEq, Clone)]
Expand Down Expand Up @@ -30,6 +31,11 @@ impl Binder {
};
// Bind table ref
let mut from_table = vec![];
// We don't support cross join now.
// The cross join will have multiple TableWithJoin in "from" struct.
// Other types of join will onyl have one TableWithJoin in "from" struct.
assert!(select.from.len() <= 1);

for table_ref in select.from.iter() {
let table_ref = self.bind_table_ref(&table_ref.relation)?;
from_table.push(table_ref);
Expand Down Expand Up @@ -65,12 +71,15 @@ impl Binder {

// Add referred columns for base table reference
for table_ref in from_table.iter_mut() {
table_ref.column_ids = self
.context
.column_ids
.get(&table_ref.table_name)
.unwrap()
.clone();
match table_ref {
BoundTableRef::BaseTableRef {
ref_id: _,
table_name,
column_ids,
} => {
*column_ids = self.context.column_ids.get(table_name).unwrap().clone();
}
}
}

Ok(Box::new(BoundSelect {
Expand Down Expand Up @@ -136,7 +145,7 @@ mod tests {
return_type: Some(DataTypeKind::Int.not_null()),
},
],
from_table: vec![BoundTableRef {
from_table: vec![BoundTableRef::BaseTableRef {
ref_id: TableRefId::new(0, 0, 0),
table_name: "t".into(),
column_ids: vec![1, 0],
Expand Down
19 changes: 13 additions & 6 deletions src/binder/table_ref/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use super::*;
use crate::parser::TableFactor;

#[derive(Debug, PartialEq, Clone)]
pub struct BoundTableRef {
pub ref_id: TableRefId,
pub table_name: String,
pub column_ids: Vec<ColumnId>,
pub enum BoundTableRef {
BaseTableRef {
ref_id: TableRefId,
table_name: String,
column_ids: Vec<ColumnId>,
},
}

impl Binder {
Expand Down Expand Up @@ -33,12 +34,18 @@ impl Binder {
self.context
.column_ids
.insert(table_name.into(), Vec::new());
Ok(BoundTableRef {
Ok(BoundTableRef::BaseTableRef {
ref_id,
table_name: table_name.into(),
column_ids: vec![],
})
}
TableFactor::NestedJoin(table_with_joins) => {
let bounded_table_ref = self.bind_table_ref(&table_with_joins.relation)?;
// We only support cross join now.
assert_eq!(table_with_joins.joins.len(), 0);
Ok(bounded_table_ref)
}
_ => panic!("bind table ref"),
}
}
Expand Down
18 changes: 13 additions & 5 deletions src/logical_plan/select.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
use super::*;
use crate::binder::BoundSelect;
use crate::binder::{BoundSelect, BoundTableRef};

impl LogicalPlaner {
pub fn plan_select(&self, stmt: Box<BoundSelect>) -> Result<LogicalPlan, LogicalPlanError> {
let mut plan = LogicalPlan::Dummy;
if let Some(table_ref) = stmt.from_table.get(0) {
plan = LogicalPlan::SeqScan(LogicalSeqScan {
table_ref_id: table_ref.ref_id,
column_ids: table_ref.column_ids.clone(),
});
match table_ref {
BoundTableRef::BaseTableRef {
ref_id,
table_name: _,
column_ids,
} => {
plan = LogicalPlan::SeqScan(LogicalSeqScan {
table_ref_id: *ref_id,
column_ids: column_ids.to_vec(),
});
}
}
}
if let Some(expr) = stmt.where_clause {
plan = LogicalPlan::Filter(LogicalFilter {
Expand Down