Skip to content
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
18 changes: 17 additions & 1 deletion src/binder/table/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub use join::*;
use sqlparser::ast::{TableFactor, TableWithJoins};

use super::{BindError, Binder, BoundSelect};
use crate::binder::BoundExpr::ColumnRef;
use crate::catalog::{ColumnCatalog, ColumnId, TableCatalog, TableId};

pub static DEFAULT_DATABASE_NAME: &str = "postgres";
Expand Down Expand Up @@ -131,9 +132,24 @@ impl Binder {
subquery,
alias,
} => {
// handle subquery table
let table = self.bind_select(subquery)?;
if let Some(alias) = alias {
todo!("alias for subquery {}", alias)
// add subquery into context
let columns = table
.select_list
.iter()
.map(|expr| match expr {
ColumnRef(col) => col.column_catalog.clone(),
_ => {
unreachable!("subquery select list should only contains column ref")
}
})
.collect::<Vec<_>>();
let table_alias = alias.to_string().to_lowercase();
let table_catalog =
TableCatalog::new_from_columns(table_alias.clone(), columns);
self.context.tables.insert(table_alias, table_catalog);
}
Ok(BoundTableRef::Subquery(Box::new(table)))
}
Expand Down
15 changes: 15 additions & 0 deletions src/catalog/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ impl TableCatalog {
.map(|id| self.columns.get(id).cloned().unwrap())
.collect()
}

pub fn new_from_columns(table_id: String, columns: Vec<ColumnCatalog>) -> Self {
let mut columns_tree = BTreeMap::new();
let mut column_ids = Vec::new();
for c in columns {
column_ids.push(c.column_id.clone());
columns_tree.insert(c.column_id.clone(), c);
}
TableCatalog {
id: table_id.to_string(),
name: table_id,
column_ids,
columns: columns_tree,
}
}
}

/// use column name as id for simplicity
Expand Down
11 changes: 11 additions & 0 deletions tests/slt/alias.slt
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,14 @@ select t_1.a from t1 t_1 left join t2 t_2 on t_1.a=t_2.b and t_1.c > t_2.c;
0
1
2

# subquery alias
query I
select t.a from (select * from t1 where a > 1) t where t.b > 7;
----
2

query III
select t.* from (select * from t1 where a > 1) t where t.b > 7;
----
2 8 1