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
6 changes: 5 additions & 1 deletion src/binder/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ impl Binder {

if let Some(table) = table_name {
// handle table.col syntax
let table_catalog = self.context.tables.get(table).unwrap();
let table_catalog = self
.context
.tables
.get(table)
.ok_or_else(|| BindError::InvalidTable(table.clone()))?;
let column_catalog = table_catalog
.get_column_by_name(column_name)
.ok_or_else(|| BindError::InvalidColumn(column_name.clone()))?;
Expand Down
21 changes: 15 additions & 6 deletions src/binder/table/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl Binder {

pub fn bind_table_ref(&mut self, table: &TableFactor) -> Result<BoundTableRef, BindError> {
match table {
TableFactor::Table { name, alias: _, .. } => {
TableFactor::Table { name, alias, .. } => {
// ObjectName internal items: db.schema.table
let (_database, _schema, table) = match name.0.as_slice() {
[table] => (
Expand All @@ -114,18 +114,27 @@ impl Binder {
.catalog
.get_table_by_name(table)
.ok_or_else(|| BindError::InvalidTable(table_name.clone()))?;
self.context
.tables
.insert(table_name, table_catalog.clone());

if let Some(alias) = alias {
let table_alias = alias.to_string().to_lowercase();
self.context
.tables
.insert(table_alias, table_catalog.clone());
} else {
self.context
.tables
.insert(table_name, table_catalog.clone());
}
Ok(BoundTableRef::Table(table_catalog))
}
TableFactor::Derived {
lateral: _,
subquery,
alias: _,
alias,
} => {
let table = self.bind_select(subquery)?;
if let Some(alias) = alias {
todo!("alias for subquery {}", alias)
}
Ok(BoundTableRef::Subquery(Box::new(table)))
}
_other => panic!("unsupported table factor: {:?}", _other),
Expand Down
26 changes: 26 additions & 0 deletions tests/slt/alias.slt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# expression alias
query I
select a as c1 from t1 order by c1 desc limit 1;
----
Expand All @@ -14,3 +15,28 @@ select sum(b) as c1, a as c2 from t1 group by c2 order by c1 desc;
15 2
5 1
4 0

# table alias
query I
select t.a from t1 t where t.b > 1 order by t.a desc limit 1;
----
2

query I
select sum(t.a) as c1 from t1 as t
----
5

query I
select t.* from t1 t where t.b > 1 order by t.a desc limit 1;
----
2 7 9

query I
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;
----
2
2
0
1
2