Skip to content
Open
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
4 changes: 4 additions & 0 deletions src/dialect/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ impl Dialect for GenericDialect {
true
}

fn supports_comma_join_constraint(&self) -> bool {
true
}

fn supports_comment_on(&self) -> bool {
true
}
Expand Down
8 changes: 8 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,14 @@ pub trait Dialect: Debug + Any {
false
}

/// Returns true if the dialect supports `ON`/`USING` after a comma-joined table,
/// e.g. `SELECT * FROM t, u ON t.a = u.a`.
///
/// [SQLite](https://www.sqlite.org/syntax/join-clause.html)
fn supports_comma_join_constraint(&self) -> bool {
false
}

/// Returns true if the dialect supports CONNECT BY.
fn supports_connect_by(&self) -> bool {
false
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,8 @@ impl Dialect for SQLiteDialect {
fn supports_parens_around_table_factor(&self) -> bool {
true
}

fn supports_comma_join_constraint(&self) -> bool {
true
}
}
32 changes: 28 additions & 4 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5086,13 +5086,37 @@ impl<'a> Parser<'a> {

/// Parse a list of [TableWithJoins]
fn parse_table_with_joins(&mut self) -> Result<Vec<TableWithJoins>, ParserError> {
let trailing_commas = self.dialect.supports_from_trailing_commas();
let dialect = self.dialect;
let trailing_commas = dialect.supports_from_trailing_commas();
let mut tables: Vec<TableWithJoins> = vec![];

self.parse_comma_separated_with_trailing_commas(
Parser::parse_table_and_joins,
|parser| {
let table = parser.parse_table_and_joins()?;
match tables.last_mut() {
Some(previous)
if dialect.supports_comma_join_constraint()
&& table.joins.is_empty()
&& parser
.peek_one_of_keywords(&[Keyword::ON, Keyword::USING])
.is_some() =>
{
let constraint = parser.parse_join_constraint(false)?;
previous.joins.push(Join {
relation: table.relation,
global: false,
join_operator: JoinOperator::Join(constraint),
});
previous.joins.extend(parser.parse_joins()?);
}
_ => tables.push(table),
}
Ok(())
},
trailing_commas,
|kw, parser| !self.dialect.is_table_factor(kw, parser),
)
|kw, parser| !dialect.is_table_factor(kw, parser),
)?;
Ok(tables)
}

/// Parse the comma of a comma-separated syntax element.
Expand Down
38 changes: 38 additions & 0 deletions tests/sqlparser_sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,44 @@ fn parse_parenthesized_table_factor() {
);
}

#[test]
fn parse_comma_join_constraint() {
let dialects = all_dialects_where(|d| d.supports_comma_join_constraint());
for (sql, canonical) in [
(
"SELECT * FROM t, u ON t.a = u.a",
"SELECT * FROM t JOIN u ON t.a = u.a",
),
(
"SELECT * FROM t, u USING (a)",
"SELECT * FROM t JOIN u USING(a)",
),
(
"SELECT * FROM t, u ON t.a = u.a, v ON v.a = u.a",
"SELECT * FROM t JOIN u ON t.a = u.a JOIN v ON v.a = u.a",
),
(
"SELECT * FROM t, u ON t.a = u.a JOIN v ON v.a = u.a",
"SELECT * FROM t JOIN u ON t.a = u.a JOIN v ON v.a = u.a",
),
(
"UPDATE t SET a = 1 FROM u, v ON u.a = v.a",
"UPDATE t SET a = 1 FROM u JOIN v ON u.a = v.a",
),
] {
dialects.one_statement_parses_to(sql, canonical);
}
dialects.verified_stmt("SELECT * FROM t, u");

assert_eq!(
all_dialects_where(|d| !d.supports_comma_join_constraint())
.parse_sql_statements("SELECT * FROM t, u ON t.a = u.a"),
Err(ParserError::ParserError(
"Expected: end of statement, found: ON".to_string()
))
);
}

fn sqlite() -> TestedDialects {
TestedDialects::new(vec![Box::new(SQLiteDialect {})])
}
Expand Down
Loading