Skip to content

Commit 7f4c913

Browse files
authored
Fix table alias parsing regression in 0.31.0 by backing out redshift column definition list (#827)
* Fix table alias parsing regression * Revert "Support redshift's columns definition list for system information functions (#769)" This reverts commit c35dcc9.
1 parent d69b875 commit 7f4c913

14 files changed

+37
-194
lines changed

src/ast/mod.rs

-29
Original file line numberDiff line numberDiff line change
@@ -4150,35 +4150,6 @@ impl fmt::Display for SearchModifier {
41504150
}
41514151
}
41524152

4153-
/// A result table definition i.e. `cols(view_schema name, view_name name, col_name name, col_type varchar, col_num int)`
4154-
/// used for redshift functions: pg_get_late_binding_view_cols, pg_get_cols, pg_get_grantee_by_iam_role,pg_get_iam_role_by_user
4155-
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4156-
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4157-
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4158-
pub struct TableAliasDefinition {
4159-
pub name: Ident,
4160-
pub args: Vec<IdentPair>,
4161-
}
4162-
4163-
impl fmt::Display for TableAliasDefinition {
4164-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4165-
write!(f, "{}({})", self.name, display_comma_separated(&self.args))?;
4166-
Ok(())
4167-
}
4168-
}
4169-
4170-
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4171-
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4172-
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4173-
pub struct IdentPair(pub Ident, pub Ident);
4174-
4175-
impl fmt::Display for IdentPair {
4176-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4177-
write!(f, "{} {}", self.0, self.1)?;
4178-
Ok(())
4179-
}
4180-
}
4181-
41824153
#[cfg(test)]
41834154
mod tests {
41844155
use super::*;

src/ast/query.rs

-7
Original file line numberDiff line numberDiff line change
@@ -631,9 +631,6 @@ pub enum TableFactor {
631631
/// vector of arguments, in the case of a table-valued function call,
632632
/// whereas it's `None` in the case of a regular table name.
633633
args: Option<Vec<FunctionArg>>,
634-
/// A table alias definition i.e. `cols(view_schema name, view_name name, col_name name, col_type varchar, col_num int)`
635-
/// used for redshift functions: pg_get_late_binding_view_cols, pg_get_cols, pg_get_grantee_by_iam_role,pg_get_iam_role_by_user)
636-
columns_definition: Option<TableAliasDefinition>,
637634
/// MSSQL-specific `WITH (...)` hints such as NOLOCK.
638635
with_hints: Vec<Expr>,
639636
},
@@ -682,7 +679,6 @@ impl fmt::Display for TableFactor {
682679
name,
683680
alias,
684681
args,
685-
columns_definition,
686682
with_hints,
687683
} => {
688684
write!(f, "{name}")?;
@@ -692,9 +688,6 @@ impl fmt::Display for TableFactor {
692688
if let Some(alias) = alias {
693689
write!(f, " AS {alias}")?;
694690
}
695-
if let Some(columns_definition) = columns_definition {
696-
write!(f, " {columns_definition}")?;
697-
}
698691
if !with_hints.is_empty() {
699692
write!(f, " WITH ({})", display_comma_separated(with_hints))?;
700693
}

src/keywords.rs

-2
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,6 @@ pub const RESERVED_FOR_TABLE_ALIAS: &[Keyword] = &[
671671
Keyword::OUTER,
672672
Keyword::SET,
673673
Keyword::QUALIFY,
674-
Keyword::AS,
675674
];
676675

677676
/// Can't be used as a column alias, so that `SELECT <expr> alias`
@@ -701,5 +700,4 @@ pub const RESERVED_FOR_COLUMN_ALIAS: &[Keyword] = &[
701700
// Reserved only as a column alias in the `SELECT` clause
702701
Keyword::FROM,
703702
Keyword::INTO,
704-
Keyword::AS,
705703
];

src/parser.rs

-46
Original file line numberDiff line numberDiff line change
@@ -5731,7 +5731,6 @@ impl<'a> Parser<'a> {
57315731
} else {
57325732
None
57335733
};
5734-
let columns_definition = self.parse_redshift_columns_definition_list()?;
57355734
let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
57365735
// MSSQL-specific table hints:
57375736
let mut with_hints = vec![];
@@ -5748,56 +5747,11 @@ impl<'a> Parser<'a> {
57485747
name,
57495748
alias,
57505749
args,
5751-
columns_definition,
57525750
with_hints,
57535751
})
57545752
}
57555753
}
57565754

5757-
fn parse_redshift_columns_definition_list(
5758-
&mut self,
5759-
) -> Result<Option<TableAliasDefinition>, ParserError> {
5760-
if !dialect_of!(self is RedshiftSqlDialect | GenericDialect) {
5761-
return Ok(None);
5762-
}
5763-
5764-
if let Some(col_definition_list_name) = self.parse_optional_columns_definition_list_alias()
5765-
{
5766-
if self.consume_token(&Token::LParen) {
5767-
let names = self.parse_comma_separated(Parser::parse_ident_pair)?;
5768-
self.expect_token(&Token::RParen)?;
5769-
Ok(Some(TableAliasDefinition {
5770-
name: col_definition_list_name,
5771-
args: names,
5772-
}))
5773-
} else {
5774-
self.prev_token();
5775-
Ok(None)
5776-
}
5777-
} else {
5778-
Ok(None)
5779-
}
5780-
}
5781-
5782-
fn parse_optional_columns_definition_list_alias(&mut self) -> Option<Ident> {
5783-
match self.next_token().token {
5784-
Token::Word(w) if !keywords::RESERVED_FOR_TABLE_ALIAS.contains(&w.keyword) => {
5785-
Some(w.to_ident())
5786-
}
5787-
_ => {
5788-
self.prev_token();
5789-
None
5790-
}
5791-
}
5792-
}
5793-
5794-
fn parse_ident_pair(&mut self) -> Result<IdentPair, ParserError> {
5795-
Ok(IdentPair(
5796-
self.parse_identifier()?,
5797-
self.parse_identifier()?,
5798-
))
5799-
}
5800-
58015755
pub fn parse_derived_table_factor(
58025756
&mut self,
58035757
lateral: IsLateral,

src/test_utils.rs

-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,6 @@ pub fn table(name: impl Into<String>) -> TableFactor {
185185
name: ObjectName(vec![Ident::new(name.into())]),
186186
alias: None,
187187
args: None,
188-
columns_definition: None,
189188
with_hints: vec![],
190189
}
191190
}

tests/sqlparser_bigquery.rs

-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ fn parse_table_identifiers() {
9999
name: ObjectName(expected),
100100
alias: None,
101101
args: None,
102-
columns_definition: None,
103102
with_hints: vec![],
104103
},
105104
joins: vec![]

tests/sqlparser_clickhouse.rs

-3
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ fn parse_map_access_expr() {
6060
name: ObjectName(vec![Ident::new("foos")]),
6161
alias: None,
6262
args: None,
63-
columns_definition: None,
6463
with_hints: vec![],
6564
},
6665
joins: vec![]
@@ -165,13 +164,11 @@ fn parse_delimited_identifiers() {
165164
name,
166165
alias,
167166
args,
168-
columns_definition,
169167
with_hints,
170168
} => {
171169
assert_eq!(vec![Ident::with_quote('"', "a table")], name.0);
172170
assert_eq!(Ident::with_quote('"', "alias"), alias.unwrap().name);
173171
assert!(args.is_none());
174-
assert!(columns_definition.is_none());
175172
assert!(with_hints.is_empty());
176173
}
177174
_ => panic!("Expecting TableFactor::Table"),

tests/sqlparser_common.rs

+37-21
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ fn parse_update_set_from() {
210210
name: ObjectName(vec![Ident::new("t1")]),
211211
alias: None,
212212
args: None,
213-
columns_definition: None,
214213
with_hints: vec![],
215214
},
216215
joins: vec![],
@@ -237,7 +236,6 @@ fn parse_update_set_from() {
237236
name: ObjectName(vec![Ident::new("t1")]),
238237
alias: None,
239238
args: None,
240-
columns_definition: None,
241239
with_hints: vec![],
242240
},
243241
joins: vec![],
@@ -300,7 +298,6 @@ fn parse_update_with_table_alias() {
300298
columns: vec![],
301299
}),
302300
args: None,
303-
columns_definition: None,
304301
with_hints: vec![],
305302
},
306303
joins: vec![],
@@ -333,6 +330,43 @@ fn parse_update_with_table_alias() {
333330
}
334331
}
335332

333+
#[test]
334+
fn parse_select_with_table_alias_as() {
335+
// AS is optional
336+
one_statement_parses_to(
337+
"SELECT a, b, c FROM lineitem l (A, B, C)",
338+
"SELECT a, b, c FROM lineitem AS l (A, B, C)",
339+
);
340+
}
341+
342+
#[test]
343+
fn parse_select_with_table_alias() {
344+
let select = verified_only_select("SELECT a, b, c FROM lineitem AS l (A, B, C)");
345+
assert_eq!(
346+
select.projection,
347+
vec![
348+
SelectItem::UnnamedExpr(Expr::Identifier(Ident::new("a")),),
349+
SelectItem::UnnamedExpr(Expr::Identifier(Ident::new("b")),),
350+
SelectItem::UnnamedExpr(Expr::Identifier(Ident::new("c")),),
351+
]
352+
);
353+
assert_eq!(
354+
select.from,
355+
vec![TableWithJoins {
356+
relation: TableFactor::Table {
357+
name: ObjectName(vec![Ident::new("lineitem")]),
358+
alias: Some(TableAlias {
359+
name: Ident::new("l"),
360+
columns: vec![Ident::new("A"), Ident::new("B"), Ident::new("C"),],
361+
}),
362+
args: None,
363+
with_hints: vec![],
364+
},
365+
joins: vec![],
366+
}]
367+
);
368+
}
369+
336370
#[test]
337371
fn parse_invalid_table_name() {
338372
let ast = all_dialects()
@@ -356,7 +390,6 @@ fn parse_delete_statement() {
356390
name: ObjectName(vec![Ident::with_quote('"', "table")]),
357391
alias: None,
358392
args: None,
359-
columns_definition: None,
360393
with_hints: vec![],
361394
},
362395
table_name
@@ -383,7 +416,6 @@ fn parse_where_delete_statement() {
383416
name: ObjectName(vec![Ident::new("foo")]),
384417
alias: None,
385418
args: None,
386-
columns_definition: None,
387419
with_hints: vec![],
388420
},
389421
table_name,
@@ -424,7 +456,6 @@ fn parse_where_delete_with_alias_statement() {
424456
columns: vec![],
425457
}),
426458
args: None,
427-
columns_definition: None,
428459
with_hints: vec![],
429460
},
430461
table_name,
@@ -438,7 +469,6 @@ fn parse_where_delete_with_alias_statement() {
438469
columns: vec![],
439470
}),
440471
args: None,
441-
columns_definition: None,
442472
with_hints: vec![],
443473
}),
444474
using
@@ -3465,7 +3495,6 @@ fn parse_interval_and_or_xor() {
34653495
}]),
34663496
alias: None,
34673497
args: None,
3468-
columns_definition: None,
34693498
with_hints: vec![],
34703499
},
34713500
joins: vec![],
@@ -3981,7 +4010,6 @@ fn parse_implicit_join() {
39814010
name: ObjectName(vec!["t1".into()]),
39824011
alias: None,
39834012
args: None,
3984-
columns_definition: None,
39854013
with_hints: vec![],
39864014
},
39874015
joins: vec![],
@@ -3991,7 +4019,6 @@ fn parse_implicit_join() {
39914019
name: ObjectName(vec!["t2".into()]),
39924020
alias: None,
39934021
args: None,
3994-
columns_definition: None,
39954022
with_hints: vec![],
39964023
},
39974024
joins: vec![],
@@ -4009,15 +4036,13 @@ fn parse_implicit_join() {
40094036
name: ObjectName(vec!["t1a".into()]),
40104037
alias: None,
40114038
args: None,
4012-
columns_definition: None,
40134039
with_hints: vec![],
40144040
},
40154041
joins: vec![Join {
40164042
relation: TableFactor::Table {
40174043
name: ObjectName(vec!["t1b".into()]),
40184044
alias: None,
40194045
args: None,
4020-
columns_definition: None,
40214046
with_hints: vec![],
40224047
},
40234048
join_operator: JoinOperator::Inner(JoinConstraint::Natural),
@@ -4028,15 +4053,13 @@ fn parse_implicit_join() {
40284053
name: ObjectName(vec!["t2a".into()]),
40294054
alias: None,
40304055
args: None,
4031-
columns_definition: None,
40324056
with_hints: vec![],
40334057
},
40344058
joins: vec![Join {
40354059
relation: TableFactor::Table {
40364060
name: ObjectName(vec!["t2b".into()]),
40374061
alias: None,
40384062
args: None,
4039-
columns_definition: None,
40404063
with_hints: vec![],
40414064
},
40424065
join_operator: JoinOperator::Inner(JoinConstraint::Natural),
@@ -4057,7 +4080,6 @@ fn parse_cross_join() {
40574080
name: ObjectName(vec![Ident::new("t2")]),
40584081
alias: None,
40594082
args: None,
4060-
columns_definition: None,
40614083
with_hints: vec![],
40624084
},
40634085
join_operator: JoinOperator::CrossJoin,
@@ -4078,7 +4100,6 @@ fn parse_joins_on() {
40784100
name: ObjectName(vec![Ident::new(relation.into())]),
40794101
alias,
40804102
args: None,
4081-
columns_definition: None,
40824103
with_hints: vec![],
40834104
},
40844105
join_operator: f(JoinConstraint::On(Expr::BinaryOp {
@@ -4148,7 +4169,6 @@ fn parse_joins_using() {
41484169
name: ObjectName(vec![Ident::new(relation.into())]),
41494170
alias,
41504171
args: None,
4151-
columns_definition: None,
41524172
with_hints: vec![],
41534173
},
41544174
join_operator: f(JoinConstraint::Using(vec!["c1".into()])),
@@ -4210,7 +4230,6 @@ fn parse_natural_join() {
42104230
name: ObjectName(vec![Ident::new("t2")]),
42114231
alias,
42124232
args: None,
4213-
columns_definition: None,
42144233
with_hints: vec![],
42154234
},
42164235
join_operator: f(JoinConstraint::Natural),
@@ -4475,7 +4494,6 @@ fn parse_derived_tables() {
44754494
name: ObjectName(vec!["t2".into()]),
44764495
alias: None,
44774496
args: None,
4478-
columns_definition: None,
44794497
with_hints: vec![],
44804498
},
44814499
join_operator: JoinOperator::Inner(JoinConstraint::Natural),
@@ -5767,7 +5785,6 @@ fn parse_merge() {
57675785
columns: vec![],
57685786
}),
57695787
args: None,
5770-
columns_definition: None,
57715788
with_hints: vec![],
57725789
}
57735790
);
@@ -5791,7 +5808,6 @@ fn parse_merge() {
57915808
name: ObjectName(vec![Ident::new("s"), Ident::new("foo")]),
57925809
alias: None,
57935810
args: None,
5794-
columns_definition: None,
57955811
with_hints: vec![],
57965812
},
57975813
joins: vec![],

0 commit comments

Comments
 (0)