Support aliasing columns

A table alias can specify new names for the columns within the aliased
table, in addition to a new name for the table itself.
This commit is contained in:
Nikhil Benesch 2019-05-30 17:23:05 -04:00
parent 73ed685879
commit 9abcac350e
No known key found for this signature in database
GPG key ID: F7386C5DEADABA7F
4 changed files with 60 additions and 14 deletions

View file

@ -1077,7 +1077,7 @@ fn parse_delimited_identifiers() {
with_hints,
} => {
assert_eq!(vec![r#""a table""#.to_string()], name.0);
assert_eq!(r#""alias""#, alias.unwrap());
assert_eq!(r#""alias""#, alias.unwrap().name);
assert!(args.is_empty());
assert!(with_hints.is_empty());
}
@ -1230,11 +1230,18 @@ fn parse_cross_join() {
);
}
fn table_alias(name: impl Into<String>) -> Option<TableAlias> {
Some(TableAlias {
name: name.into(),
columns: vec![],
})
}
#[test]
fn parse_joins_on() {
fn join_with_constraint(
relation: impl Into<String>,
alias: Option<SQLIdent>,
alias: Option<TableAlias>,
f: impl Fn(JoinConstraint) -> JoinOperator,
) -> Join {
Join {
@ -1256,7 +1263,7 @@ fn parse_joins_on() {
verified_only_select("SELECT * FROM t1 JOIN t2 AS foo ON c1 = c2").joins,
vec![join_with_constraint(
"t2",
Some("foo".to_string()),
table_alias("foo"),
JoinOperator::Inner
)]
);
@ -1287,7 +1294,7 @@ fn parse_joins_on() {
fn parse_joins_using() {
fn join_with_constraint(
relation: impl Into<String>,
alias: Option<SQLIdent>,
alias: Option<TableAlias>,
f: impl Fn(JoinConstraint) -> JoinOperator,
) -> Join {
Join {
@ -1305,7 +1312,7 @@ fn parse_joins_using() {
verified_only_select("SELECT * FROM t1 JOIN t2 AS foo USING(c1)").joins,
vec![join_with_constraint(
"t2",
Some("foo".to_string()),
table_alias("foo"),
JoinOperator::Inner
)]
);
@ -1465,6 +1472,12 @@ fn parse_derived_tables() {
let sql = "SELECT a.x, b.y FROM (SELECT x FROM foo) AS a CROSS JOIN (SELECT y FROM bar) AS b";
let _ = verified_only_select(sql);
//TODO: add assertions
let sql = "SELECT a.x, b.y \
FROM (SELECT x FROM foo) AS a (x) \
CROSS JOIN (SELECT y FROM bar) AS b (y)";
let _ = verified_only_select(sql);
//TODO: add assertions
}
#[test]
@ -1947,11 +1960,11 @@ fn lateral_derived() {
if let TableFactor::Derived {
lateral,
ref subquery,
ref alias,
alias: Some(ref alias),
} = select.joins[0].relation
{
assert_eq!(lateral_in, lateral);
assert_eq!(Some("order".to_string()), *alias);
assert_eq!("order".to_string(), alias.name);
assert_eq!(
subquery.to_string(),
"SELECT * FROM order WHERE order.customer = customer.id LIMIT 3"