Skip to content

Commit

Permalink
Infer table.col expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
emk committed Oct 26, 2023
1 parent 2385edd commit 0e9d5c9
Showing 1 changed file with 39 additions and 10 deletions.
49 changes: 39 additions & 10 deletions src/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,33 @@ impl InferTypes for ast::Expression {
};
Ok((ty.to_owned(), scope.clone()))
}
_ => todo!("expression"),
ast::Expression::TableAndColumnName(ast::TableAndColumnName {
table_name,
column_name,
..
}) => {
let table = ident_from_table_name(table_name)?;
let table_type = scope
.get(&table)
.ok_or_else(|| format_err!("table {:?} not found in scope", table_name))?;
let table_type = match table_type {
Type::Table(table_type) => table_type,
_ => Err(format_err!(
"table {:?} is not a table: {:?}",
table_name,
table_type
))?,
};
let column_type = table_type
.columns
.iter()
.find(|column_type| column_type.name == *column_name)
.ok_or_else(|| {
format_err!("column {:?} not found in table {:?}", column_name, table)
})?;
Ok((column_type.ty.to_owned(), scope.clone()))
}
_ => todo!("expression: {:?}", self),
}
}
}
Expand Down Expand Up @@ -392,6 +418,9 @@ impl InferColumnName for ast::Expression {
fn infer_column_name(&mut self) -> Option<Ident> {
match self {
ast::Expression::ColumnName(ident) => Some(ident.clone()),
ast::Expression::TableAndColumnName(ast::TableAndColumnName {
column_name, ..
}) => Some(column_name.clone()),
_ => None,
}
}
Expand Down Expand Up @@ -492,13 +521,13 @@ SELECT x FROM t2";
assert_defines!(scope, "foo", "TABLE<_f0 INT64, x INT64, _f1 INT64>");
}

// #[test]
// fn columns_scoped_by_table() {
// let sql = "
// CREATE TABLE foo AS
// WITH t AS (SELECT 'a' AS x)
// SELECT t.x FROM t";
// let (_, scope) = infer(sql).unwrap();
// assert_defines!(scope, "foo", "TABLE<x STRING>");
// }
#[test]
fn columns_scoped_by_table() {
let sql = "
CREATE TABLE foo AS
WITH t AS (SELECT 'a' AS x)
SELECT t.x FROM t";
let (_, scope) = infer(sql).unwrap();
assert_defines!(scope, "foo", "TABLE<x STRING>");
}
}

0 comments on commit 0e9d5c9

Please sign in to comment.