From 046365da75aa04d8917251d3157fc6f13d85457b Mon Sep 17 00:00:00 2001 From: shuvamk Date: Tue, 4 Aug 2026 16:21:07 +0530 Subject: [PATCH] MySQL: print a space before PARTITION when displaying a table factor `SELECT * FROM employees PARTITION (p0, p2)` rendered back as `SELECT * FROM employeesPARTITION (p0, p2)`, which re-parses as a table function named `employeesPARTITION` with arguments `p0` and `p2` rather than a partition selection on `employees`. The round trip therefore changed the AST, not just the whitespace. `Display for TableFactor::Table` emitted `PARTITION (...)` without a leading space, unlike the sibling clauses in the same arm. Add it. The regression test also pins the `UPDATE`, `DELETE`, alias, join and missing-parenthesis cases, none of which had coverage. Co-Authored-By: Claude Opus 5 --- src/ast/query.rs | 2 +- tests/sqlparser_mysql.rs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/ast/query.rs b/src/ast/query.rs index c683d66c0..eb47ad27f 100644 --- a/src/ast/query.rs +++ b/src/ast/query.rs @@ -2216,7 +2216,7 @@ impl fmt::Display for TableFactor { json_path.fmt(f)?; } if !partitions.is_empty() { - write!(f, "PARTITION ({})", display_comma_separated(partitions))?; + write!(f, " PARTITION ({})", display_comma_separated(partitions))?; } if let Some(args) = args { write!(f, "(")?; diff --git a/tests/sqlparser_mysql.rs b/tests/sqlparser_mysql.rs index 797a12551..d08fad3ed 100644 --- a/tests/sqlparser_mysql.rs +++ b/tests/sqlparser_mysql.rs @@ -4946,3 +4946,19 @@ fn parse_adjacent_string_literal_concatenation() { fn parse_group_by_with_rollup() { mysql().verified_stmt("SELECT * FROM tbl GROUP BY col1, col2 WITH ROLLUP"); } + +#[test] +fn parse_table_partition_selection() { + mysql_and_generic().verified_stmt("SELECT * FROM employees PARTITION (p0, p2)"); + mysql_and_generic().verified_stmt("SELECT * FROM employees PARTITION (p0) AS e"); + mysql_and_generic().verified_stmt( + "SELECT * FROM employees PARTITION (p0) JOIN departments PARTITION (p1) ON employees.dept_id = departments.id", + ); + mysql_and_generic().verified_stmt("UPDATE employees PARTITION (p0) SET salary = 1"); + mysql_and_generic().verified_stmt("DELETE FROM employees PARTITION (p0) WHERE id = 1"); + + let err = mysql_and_generic() + .parse_sql_statements("SELECT * FROM employees PARTITION") + .expect_err("expected an error"); + assert_matches!(err, ParserError::ParserError(_)); +}