Skip to content

Commit 0e731b4

Browse files
committed
Improve PARTITION OF error handling and add tests.
- Add explicit check for FOR/DEFAULT after PARTITION OF with clear error message - Document intentional removal of dialect check for multi-dialect tool support - Add negative test cases for malformed PARTITION OF syntax
1 parent 4f4cda6 commit 0e731b4

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

src/parser/mod.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7888,6 +7888,9 @@ impl<'a> Parser<'a> {
78887888
let table_name = self.parse_object_name(allow_unquoted_hyphen)?;
78897889

78907890
// PostgreSQL PARTITION OF for child partition tables
7891+
// Note: This is a PostgreSQL-specific feature, but the dialect check was intentionally
7892+
// removed to allow GenericDialect and other dialects to parse this syntax. This enables
7893+
// multi-dialect SQL tools to work with PostgreSQL-specific DDL statements.
78917894
let partition_of = if self.parse_keywords(&[Keyword::PARTITION, Keyword::OF]) {
78927895
Some(self.parse_object_name(allow_unquoted_hyphen)?)
78937896
} else {
@@ -7920,7 +7923,14 @@ impl<'a> Parser<'a> {
79207923

79217924
// PostgreSQL PARTITION OF: partition bound specification
79227925
let for_values = if partition_of.is_some() {
7923-
Some(self.parse_partition_for_values()?)
7926+
if self.peek_keyword(Keyword::FOR) || self.peek_keyword(Keyword::DEFAULT) {
7927+
Some(self.parse_partition_for_values()?)
7928+
} else {
7929+
return self.expected(
7930+
"FOR VALUES or DEFAULT after PARTITION OF",
7931+
self.peek_token(),
7932+
);
7933+
}
79247934
} else {
79257935
None
79267936
};

tests/sqlparser_postgres.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8112,3 +8112,42 @@ CONSTRAINT check_date CHECK (order_date >= '2023-01-01')\
81128112
_ => panic!("Expected CreateTable"),
81138113
}
81148114
}
8115+
8116+
#[test]
8117+
fn parse_create_table_partition_of_errors() {
8118+
let sql = "CREATE TABLE p PARTITION OF parent";
8119+
let result = pg_and_generic().parse_sql_statements(sql);
8120+
assert!(result.is_err());
8121+
let err = result.unwrap_err().to_string();
8122+
assert!(
8123+
err.contains("FOR VALUES or DEFAULT"),
8124+
"Expected error about FOR VALUES, got: {err}"
8125+
);
8126+
8127+
let sql = "CREATE TABLE p PARTITION OF parent WITH (fillfactor = 70)";
8128+
let result = pg_and_generic().parse_sql_statements(sql);
8129+
assert!(result.is_err());
8130+
let err = result.unwrap_err().to_string();
8131+
assert!(
8132+
err.contains("FOR VALUES or DEFAULT"),
8133+
"Expected error about FOR VALUES, got: {err}"
8134+
);
8135+
8136+
let sql = "CREATE TABLE p PARTITION OF parent FOR VALUES RANGE (1, 10)";
8137+
let result = pg_and_generic().parse_sql_statements(sql);
8138+
assert!(result.is_err());
8139+
let err = result.unwrap_err().to_string();
8140+
assert!(
8141+
err.contains("IN, FROM, or WITH"),
8142+
"Expected error about invalid keyword after FOR VALUES, got: {err}"
8143+
);
8144+
8145+
let sql = "CREATE TABLE p PARTITION OF parent FOR VALUES FROM (1)";
8146+
let result = pg_and_generic().parse_sql_statements(sql);
8147+
assert!(result.is_err());
8148+
let err = result.unwrap_err().to_string();
8149+
assert!(
8150+
err.contains("TO"),
8151+
"Expected error about missing TO clause, got: {err}"
8152+
);
8153+
}

0 commit comments

Comments
 (0)