Distinguish between tables and nullary functions in FROM (#506)

This commit is contained in:
Riccardo Azzolini 2022-05-25 22:01:13 +02:00 committed by GitHub
parent cd66034a4a
commit 901f5b974f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 39 additions and 29 deletions

View file

@ -337,7 +337,11 @@ pub enum TableFactor {
/// Arguments of a table-valued function, as supported by Postgres /// Arguments of a table-valued function, as supported by Postgres
/// and MSSQL. Note that deprecated MSSQL `FROM foo (NOLOCK)` syntax /// and MSSQL. Note that deprecated MSSQL `FROM foo (NOLOCK)` syntax
/// will also be parsed as `args`. /// will also be parsed as `args`.
args: Vec<FunctionArg>, ///
/// This field's value is `Some(v)`, where `v` is a (possibly empty)
/// vector of arguments, in the case of a table-valued function call,
/// whereas it's `None` in the case of a regular table name.
args: Option<Vec<FunctionArg>>,
/// MSSQL-specific `WITH (...)` hints such as NOLOCK. /// MSSQL-specific `WITH (...)` hints such as NOLOCK.
with_hints: Vec<Expr>, with_hints: Vec<Expr>,
}, },
@ -370,7 +374,7 @@ impl fmt::Display for TableFactor {
with_hints, with_hints,
} => { } => {
write!(f, "{}", name)?; write!(f, "{}", name)?;
if !args.is_empty() { if let Some(args) = args {
write!(f, "({})", display_comma_separated(args))?; write!(f, "({})", display_comma_separated(args))?;
} }
if let Some(alias) = alias { if let Some(alias) = alias {

View file

@ -3623,9 +3623,9 @@ impl<'a> Parser<'a> {
let name = self.parse_object_name()?; let name = self.parse_object_name()?;
// Postgres, MSSQL: table-valued functions: // Postgres, MSSQL: table-valued functions:
let args = if self.consume_token(&Token::LParen) { let args = if self.consume_token(&Token::LParen) {
self.parse_optional_args()? Some(self.parse_optional_args()?)
} else { } else {
vec![] None
}; };
let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?; let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
// MSSQL-specific table hints: // MSSQL-specific table hints:

View file

@ -177,7 +177,7 @@ pub fn table(name: impl Into<String>) -> TableFactor {
TableFactor::Table { TableFactor::Table {
name: ObjectName(vec![Ident::new(name.into())]), name: ObjectName(vec![Ident::new(name.into())]),
alias: None, alias: None,
args: vec![], args: None,
with_hints: vec![], with_hints: vec![],
} }
} }

View file

@ -29,7 +29,7 @@ fn parse_table_identifiers() {
relation: TableFactor::Table { relation: TableFactor::Table {
name: ObjectName(expected), name: ObjectName(expected),
alias: None, alias: None,
args: vec![], args: None,
with_hints: vec![], with_hints: vec![],
}, },
joins: vec![] joins: vec![]

View file

@ -200,7 +200,7 @@ fn parse_update_with_table_alias() {
name: Ident::new("u"), name: Ident::new("u"),
columns: vec![] columns: vec![]
}), }),
args: vec![], args: None,
with_hints: vec![], with_hints: vec![],
}, },
joins: vec![] joins: vec![]
@ -2793,7 +2793,7 @@ fn parse_delimited_identifiers() {
} => { } => {
assert_eq!(vec![Ident::with_quote('"', "a table")], name.0); assert_eq!(vec![Ident::with_quote('"', "a table")], name.0);
assert_eq!(Ident::with_quote('"', "alias"), alias.unwrap().name); assert_eq!(Ident::with_quote('"', "alias"), alias.unwrap().name);
assert!(args.is_empty()); assert!(args.is_none());
assert!(with_hints.is_empty()); assert!(with_hints.is_empty());
} }
_ => panic!("Expecting TableFactor::Table"), _ => panic!("Expecting TableFactor::Table"),
@ -2912,6 +2912,12 @@ fn parse_from_advanced() {
let _select = verified_only_select(sql); let _select = verified_only_select(sql);
} }
#[test]
fn parse_nullary_table_valued_function() {
let sql = "SELECT * FROM fn()";
let _select = verified_only_select(sql);
}
#[test] #[test]
fn parse_implicit_join() { fn parse_implicit_join() {
let sql = "SELECT * FROM t1, t2"; let sql = "SELECT * FROM t1, t2";
@ -2922,7 +2928,7 @@ fn parse_implicit_join() {
relation: TableFactor::Table { relation: TableFactor::Table {
name: ObjectName(vec!["t1".into()]), name: ObjectName(vec!["t1".into()]),
alias: None, alias: None,
args: vec![], args: None,
with_hints: vec![], with_hints: vec![],
}, },
joins: vec![], joins: vec![],
@ -2931,7 +2937,7 @@ fn parse_implicit_join() {
relation: TableFactor::Table { relation: TableFactor::Table {
name: ObjectName(vec!["t2".into()]), name: ObjectName(vec!["t2".into()]),
alias: None, alias: None,
args: vec![], args: None,
with_hints: vec![], with_hints: vec![],
}, },
joins: vec![], joins: vec![],
@ -2948,14 +2954,14 @@ fn parse_implicit_join() {
relation: TableFactor::Table { relation: TableFactor::Table {
name: ObjectName(vec!["t1a".into()]), name: ObjectName(vec!["t1a".into()]),
alias: None, alias: None,
args: vec![], args: None,
with_hints: vec![], with_hints: vec![],
}, },
joins: vec![Join { joins: vec![Join {
relation: TableFactor::Table { relation: TableFactor::Table {
name: ObjectName(vec!["t1b".into()]), name: ObjectName(vec!["t1b".into()]),
alias: None, alias: None,
args: vec![], args: None,
with_hints: vec![], with_hints: vec![],
}, },
join_operator: JoinOperator::Inner(JoinConstraint::Natural), join_operator: JoinOperator::Inner(JoinConstraint::Natural),
@ -2965,14 +2971,14 @@ fn parse_implicit_join() {
relation: TableFactor::Table { relation: TableFactor::Table {
name: ObjectName(vec!["t2a".into()]), name: ObjectName(vec!["t2a".into()]),
alias: None, alias: None,
args: vec![], args: None,
with_hints: vec![], with_hints: vec![],
}, },
joins: vec![Join { joins: vec![Join {
relation: TableFactor::Table { relation: TableFactor::Table {
name: ObjectName(vec!["t2b".into()]), name: ObjectName(vec!["t2b".into()]),
alias: None, alias: None,
args: vec![], args: None,
with_hints: vec![], with_hints: vec![],
}, },
join_operator: JoinOperator::Inner(JoinConstraint::Natural), join_operator: JoinOperator::Inner(JoinConstraint::Natural),
@ -2992,7 +2998,7 @@ fn parse_cross_join() {
relation: TableFactor::Table { relation: TableFactor::Table {
name: ObjectName(vec![Ident::new("t2")]), name: ObjectName(vec![Ident::new("t2")]),
alias: None, alias: None,
args: vec![], args: None,
with_hints: vec![], with_hints: vec![],
}, },
join_operator: JoinOperator::CrossJoin join_operator: JoinOperator::CrossJoin
@ -3012,7 +3018,7 @@ fn parse_joins_on() {
relation: TableFactor::Table { relation: TableFactor::Table {
name: ObjectName(vec![Ident::new(relation.into())]), name: ObjectName(vec![Ident::new(relation.into())]),
alias, alias,
args: vec![], args: None,
with_hints: vec![], with_hints: vec![],
}, },
join_operator: f(JoinConstraint::On(Expr::BinaryOp { join_operator: f(JoinConstraint::On(Expr::BinaryOp {
@ -3065,7 +3071,7 @@ fn parse_joins_using() {
relation: TableFactor::Table { relation: TableFactor::Table {
name: ObjectName(vec![Ident::new(relation.into())]), name: ObjectName(vec![Ident::new(relation.into())]),
alias, alias,
args: vec![], args: None,
with_hints: vec![], with_hints: vec![],
}, },
join_operator: f(JoinConstraint::Using(vec!["c1".into()])), join_operator: f(JoinConstraint::Using(vec!["c1".into()])),
@ -3110,7 +3116,7 @@ fn parse_natural_join() {
relation: TableFactor::Table { relation: TableFactor::Table {
name: ObjectName(vec![Ident::new("t2")]), name: ObjectName(vec![Ident::new("t2")]),
alias: None, alias: None,
args: vec![], args: None,
with_hints: vec![], with_hints: vec![],
}, },
join_operator: f(JoinConstraint::Natural), join_operator: f(JoinConstraint::Natural),
@ -3348,7 +3354,7 @@ fn parse_derived_tables() {
relation: TableFactor::Table { relation: TableFactor::Table {
name: ObjectName(vec!["t2".into()]), name: ObjectName(vec!["t2".into()]),
alias: None, alias: None,
args: vec![], args: None,
with_hints: vec![], with_hints: vec![],
}, },
join_operator: JoinOperator::Inner(JoinConstraint::Natural), join_operator: JoinOperator::Inner(JoinConstraint::Natural),
@ -4431,7 +4437,7 @@ fn parse_merge() {
name: Ident::new("dest"), name: Ident::new("dest"),
columns: vec![] columns: vec![]
}), }),
args: vec![], args: None,
with_hints: vec![] with_hints: vec![]
} }
); );
@ -4452,7 +4458,7 @@ fn parse_merge() {
relation: TableFactor::Table { relation: TableFactor::Table {
name: ObjectName(vec![Ident::new("s"), Ident::new("foo")]), name: ObjectName(vec![Ident::new("s"), Ident::new("foo")]),
alias: None, alias: None,
args: vec![], args: None,
with_hints: vec![] with_hints: vec![]
}, },
joins: vec![] joins: vec![]

View file

@ -626,7 +626,7 @@ fn parse_update_with_joins() {
name: Ident::new("o"), name: Ident::new("o"),
columns: vec![] columns: vec![]
}), }),
args: vec![], args: None,
with_hints: vec![], with_hints: vec![],
}, },
joins: vec![Join { joins: vec![Join {
@ -636,7 +636,7 @@ fn parse_update_with_joins() {
name: Ident::new("c"), name: Ident::new("c"),
columns: vec![] columns: vec![]
}), }),
args: vec![], args: None,
with_hints: vec![], with_hints: vec![],
}, },
join_operator: JoinOperator::Inner(JoinConstraint::On(Expr::BinaryOp { join_operator: JoinOperator::Inner(JoinConstraint::On(Expr::BinaryOp {
@ -743,7 +743,7 @@ fn parse_substring_in_select() {
quote_style: None quote_style: None
}]), }]),
alias: None, alias: None,
args: vec![], args: None,
with_hints: vec![] with_hints: vec![]
}, },
joins: vec![] joins: vec![]

View file

@ -413,7 +413,7 @@ fn parse_update_set_from() {
relation: TableFactor::Table { relation: TableFactor::Table {
name: ObjectName(vec![Ident::new("t1")]), name: ObjectName(vec![Ident::new("t1")]),
alias: None, alias: None,
args: vec![], args: None,
with_hints: vec![], with_hints: vec![],
}, },
joins: vec![], joins: vec![],
@ -439,7 +439,7 @@ fn parse_update_set_from() {
relation: TableFactor::Table { relation: TableFactor::Table {
name: ObjectName(vec![Ident::new("t1")]), name: ObjectName(vec![Ident::new("t1")]),
alias: None, alias: None,
args: vec![], args: None,
with_hints: vec![], with_hints: vec![],
}, },
joins: vec![], joins: vec![],

View file

@ -43,7 +43,7 @@ fn test_square_brackets_over_db_schema_table_name() {
} }
]), ]),
alias: None, alias: None,
args: vec![], args: None,
with_hints: vec![], with_hints: vec![],
}, },
joins: vec![], joins: vec![],
@ -87,7 +87,7 @@ fn test_double_quotes_over_db_schema_table_name() {
} }
]), ]),
alias: None, alias: None,
args: vec![], args: None,
with_hints: vec![], with_hints: vec![],
}, },
joins: vec![], joins: vec![],

View file

@ -58,7 +58,7 @@ fn parse_map_access_expr() {
relation: Table { relation: Table {
name: ObjectName(vec![Ident::new("foos")]), name: ObjectName(vec![Ident::new("foos")]),
alias: None, alias: None,
args: vec![], args: None,
with_hints: vec![], with_hints: vec![],
}, },
joins: vec![] joins: vec![]