Skip to content

Distinguish between SELECT * FROM foo and SELECT * FROM foo() #506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 25, 2022
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
8 changes: 6 additions & 2 deletions src/ast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,11 @@ pub enum TableFactor {
/// Arguments of a table-valued function, as supported by Postgres
/// and MSSQL. Note that deprecated MSSQL `FROM foo (NOLOCK)` syntax
/// will also be parsed as `args`.
args: Vec<FunctionArg>,
///
/// This field's value is `Some(v)`, where `v` is a (possibly empty)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

/// 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.
with_hints: Vec<Expr>,
},
Expand Down Expand Up @@ -370,7 +374,7 @@ impl fmt::Display for TableFactor {
with_hints,
} => {
write!(f, "{}", name)?;
if !args.is_empty() {
if let Some(args) = args {
write!(f, "({})", display_comma_separated(args))?;
}
if let Some(alias) = alias {
Expand Down
4 changes: 2 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3620,9 +3620,9 @@ impl<'a> Parser<'a> {
let name = self.parse_object_name()?;
// Postgres, MSSQL: table-valued functions:
let args = if self.consume_token(&Token::LParen) {
self.parse_optional_args()?
Some(self.parse_optional_args()?)
} else {
vec![]
None
};
let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
// MSSQL-specific table hints:
Expand Down
2 changes: 1 addition & 1 deletion src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ pub fn table(name: impl Into<String>) -> TableFactor {
TableFactor::Table {
name: ObjectName(vec![Ident::new(name.into())]),
alias: None,
args: vec![],
args: None,
with_hints: vec![],
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/sqlparser_bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn parse_table_identifiers() {
relation: TableFactor::Table {
name: ObjectName(expected),
alias: None,
args: vec![],
args: None,
with_hints: vec![],
},
joins: vec![]
Expand Down
36 changes: 21 additions & 15 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ fn parse_update_with_table_alias() {
name: Ident::new("u"),
columns: vec![]
}),
args: vec![],
args: None,
with_hints: vec![],
},
joins: vec![]
Expand Down Expand Up @@ -2793,7 +2793,7 @@ fn parse_delimited_identifiers() {
} => {
assert_eq!(vec![Ident::with_quote('"', "a table")], name.0);
assert_eq!(Ident::with_quote('"', "alias"), alias.unwrap().name);
assert!(args.is_empty());
assert!(args.is_none());
assert!(with_hints.is_empty());
}
_ => panic!("Expecting TableFactor::Table"),
Expand Down Expand Up @@ -2912,6 +2912,12 @@ fn parse_from_advanced() {
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]
fn parse_implicit_join() {
let sql = "SELECT * FROM t1, t2";
Expand All @@ -2922,7 +2928,7 @@ fn parse_implicit_join() {
relation: TableFactor::Table {
name: ObjectName(vec!["t1".into()]),
alias: None,
args: vec![],
args: None,
with_hints: vec![],
},
joins: vec![],
Expand All @@ -2931,7 +2937,7 @@ fn parse_implicit_join() {
relation: TableFactor::Table {
name: ObjectName(vec!["t2".into()]),
alias: None,
args: vec![],
args: None,
with_hints: vec![],
},
joins: vec![],
Expand All @@ -2948,14 +2954,14 @@ fn parse_implicit_join() {
relation: TableFactor::Table {
name: ObjectName(vec!["t1a".into()]),
alias: None,
args: vec![],
args: None,
with_hints: vec![],
},
joins: vec![Join {
relation: TableFactor::Table {
name: ObjectName(vec!["t1b".into()]),
alias: None,
args: vec![],
args: None,
with_hints: vec![],
},
join_operator: JoinOperator::Inner(JoinConstraint::Natural),
Expand All @@ -2965,14 +2971,14 @@ fn parse_implicit_join() {
relation: TableFactor::Table {
name: ObjectName(vec!["t2a".into()]),
alias: None,
args: vec![],
args: None,
with_hints: vec![],
},
joins: vec![Join {
relation: TableFactor::Table {
name: ObjectName(vec!["t2b".into()]),
alias: None,
args: vec![],
args: None,
with_hints: vec![],
},
join_operator: JoinOperator::Inner(JoinConstraint::Natural),
Expand All @@ -2992,7 +2998,7 @@ fn parse_cross_join() {
relation: TableFactor::Table {
name: ObjectName(vec![Ident::new("t2")]),
alias: None,
args: vec![],
args: None,
with_hints: vec![],
},
join_operator: JoinOperator::CrossJoin
Expand All @@ -3012,7 +3018,7 @@ fn parse_joins_on() {
relation: TableFactor::Table {
name: ObjectName(vec![Ident::new(relation.into())]),
alias,
args: vec![],
args: None,
with_hints: vec![],
},
join_operator: f(JoinConstraint::On(Expr::BinaryOp {
Expand Down Expand Up @@ -3065,7 +3071,7 @@ fn parse_joins_using() {
relation: TableFactor::Table {
name: ObjectName(vec![Ident::new(relation.into())]),
alias,
args: vec![],
args: None,
with_hints: vec![],
},
join_operator: f(JoinConstraint::Using(vec!["c1".into()])),
Expand Down Expand Up @@ -3110,7 +3116,7 @@ fn parse_natural_join() {
relation: TableFactor::Table {
name: ObjectName(vec![Ident::new("t2")]),
alias: None,
args: vec![],
args: None,
with_hints: vec![],
},
join_operator: f(JoinConstraint::Natural),
Expand Down Expand Up @@ -3348,7 +3354,7 @@ fn parse_derived_tables() {
relation: TableFactor::Table {
name: ObjectName(vec!["t2".into()]),
alias: None,
args: vec![],
args: None,
with_hints: vec![],
},
join_operator: JoinOperator::Inner(JoinConstraint::Natural),
Expand Down Expand Up @@ -4431,7 +4437,7 @@ fn parse_merge() {
name: Ident::new("dest"),
columns: vec![]
}),
args: vec![],
args: None,
with_hints: vec![]
}
);
Expand All @@ -4452,7 +4458,7 @@ fn parse_merge() {
relation: TableFactor::Table {
name: ObjectName(vec![Ident::new("s"), Ident::new("foo")]),
alias: None,
args: vec![],
args: None,
with_hints: vec![]
},
joins: vec![]
Expand Down
6 changes: 3 additions & 3 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ fn parse_update_with_joins() {
name: Ident::new("o"),
columns: vec![]
}),
args: vec![],
args: None,
with_hints: vec![],
},
joins: vec![Join {
Expand All @@ -636,7 +636,7 @@ fn parse_update_with_joins() {
name: Ident::new("c"),
columns: vec![]
}),
args: vec![],
args: None,
with_hints: vec![],
},
join_operator: JoinOperator::Inner(JoinConstraint::On(Expr::BinaryOp {
Expand Down Expand Up @@ -743,7 +743,7 @@ fn parse_substring_in_select() {
quote_style: None
}]),
alias: None,
args: vec![],
args: None,
with_hints: vec![]
},
joins: vec![]
Expand Down
4 changes: 2 additions & 2 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ fn parse_update_set_from() {
relation: TableFactor::Table {
name: ObjectName(vec![Ident::new("t1")]),
alias: None,
args: vec![],
args: None,
with_hints: vec![],
},
joins: vec![],
Expand All @@ -439,7 +439,7 @@ fn parse_update_set_from() {
relation: TableFactor::Table {
name: ObjectName(vec![Ident::new("t1")]),
alias: None,
args: vec![],
args: None,
with_hints: vec![],
},
joins: vec![],
Expand Down
4 changes: 2 additions & 2 deletions tests/sqlparser_redshift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn test_square_brackets_over_db_schema_table_name() {
}
]),
alias: None,
args: vec![],
args: None,
with_hints: vec![],
},
joins: vec![],
Expand Down Expand Up @@ -87,7 +87,7 @@ fn test_double_quotes_over_db_schema_table_name() {
}
]),
alias: None,
args: vec![],
args: None,
with_hints: vec![],
},
joins: vec![],
Expand Down
2 changes: 1 addition & 1 deletion tests/sqpparser_clickhouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn parse_map_access_expr() {
relation: Table {
name: ObjectName(vec![Ident::new("foos")]),
alias: None,
args: vec![],
args: None,
with_hints: vec![],
},
joins: vec![]
Expand Down