Skip to content

Commit f661171

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 7f7e6ba commit f661171

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

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
@@ -8104,3 +8104,42 @@ CONSTRAINT check_date CHECK (order_date >= '2023-01-01')\
81048104
_ => panic!("Expected CreateTable"),
81058105
}
81068106
}
8107+
8108+
#[test]
8109+
fn parse_create_table_partition_of_errors() {
8110+
let sql = "CREATE TABLE p PARTITION OF parent";
8111+
let result = pg_and_generic().parse_sql_statements(sql);
8112+
assert!(result.is_err());
8113+
let err = result.unwrap_err().to_string();
8114+
assert!(
8115+
err.contains("FOR VALUES or DEFAULT"),
8116+
"Expected error about FOR VALUES, got: {err}"
8117+
);
8118+
8119+
let sql = "CREATE TABLE p PARTITION OF parent WITH (fillfactor = 70)";
8120+
let result = pg_and_generic().parse_sql_statements(sql);
8121+
assert!(result.is_err());
8122+
let err = result.unwrap_err().to_string();
8123+
assert!(
8124+
err.contains("FOR VALUES or DEFAULT"),
8125+
"Expected error about FOR VALUES, got: {err}"
8126+
);
8127+
8128+
let sql = "CREATE TABLE p PARTITION OF parent FOR VALUES RANGE (1, 10)";
8129+
let result = pg_and_generic().parse_sql_statements(sql);
8130+
assert!(result.is_err());
8131+
let err = result.unwrap_err().to_string();
8132+
assert!(
8133+
err.contains("IN, FROM, or WITH"),
8134+
"Expected error about invalid keyword after FOR VALUES, got: {err}"
8135+
);
8136+
8137+
let sql = "CREATE TABLE p PARTITION OF parent FOR VALUES FROM (1)";
8138+
let result = pg_and_generic().parse_sql_statements(sql);
8139+
assert!(result.is_err());
8140+
let err = result.unwrap_err().to_string();
8141+
assert!(
8142+
err.contains("TO"),
8143+
"Expected error about missing TO clause, got: {err}"
8144+
);
8145+
}

0 commit comments

Comments
 (0)