Skip to content

Commit 5d44e5a

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 f74fd1a commit 5d44e5a

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
@@ -7834,6 +7834,9 @@ impl<'a> Parser<'a> {
78347834
let table_name = self.parse_object_name(allow_unquoted_hyphen)?;
78357835

78367836
// PostgreSQL PARTITION OF for child partition tables
7837+
// Note: This is a PostgreSQL-specific feature, but the dialect check was intentionally
7838+
// removed to allow GenericDialect and other dialects to parse this syntax. This enables
7839+
// multi-dialect SQL tools to work with PostgreSQL-specific DDL statements.
78377840
let partition_of = if self.parse_keywords(&[Keyword::PARTITION, Keyword::OF]) {
78387841
Some(self.parse_object_name(allow_unquoted_hyphen)?)
78397842
} else {
@@ -7866,7 +7869,14 @@ impl<'a> Parser<'a> {
78667869

78677870
// PostgreSQL PARTITION OF: partition bound specification
78687871
let for_values = if partition_of.is_some() {
7869-
Some(self.parse_partition_for_values()?)
7872+
if self.peek_keyword(Keyword::FOR) || self.peek_keyword(Keyword::DEFAULT) {
7873+
Some(self.parse_partition_for_values()?)
7874+
} else {
7875+
return self.expected(
7876+
"FOR VALUES or DEFAULT after PARTITION OF",
7877+
self.peek_token(),
7878+
);
7879+
}
78707880
} else {
78717881
None
78727882
};

tests/sqlparser_postgres.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7682,3 +7682,42 @@ CONSTRAINT check_date CHECK (order_date >= '2023-01-01')\
76827682
_ => panic!("Expected CreateTable"),
76837683
}
76847684
}
7685+
7686+
#[test]
7687+
fn parse_create_table_partition_of_errors() {
7688+
let sql = "CREATE TABLE p PARTITION OF parent";
7689+
let result = pg_and_generic().parse_sql_statements(sql);
7690+
assert!(result.is_err());
7691+
let err = result.unwrap_err().to_string();
7692+
assert!(
7693+
err.contains("FOR VALUES or DEFAULT"),
7694+
"Expected error about FOR VALUES, got: {err}"
7695+
);
7696+
7697+
let sql = "CREATE TABLE p PARTITION OF parent WITH (fillfactor = 70)";
7698+
let result = pg_and_generic().parse_sql_statements(sql);
7699+
assert!(result.is_err());
7700+
let err = result.unwrap_err().to_string();
7701+
assert!(
7702+
err.contains("FOR VALUES or DEFAULT"),
7703+
"Expected error about FOR VALUES, got: {err}"
7704+
);
7705+
7706+
let sql = "CREATE TABLE p PARTITION OF parent FOR VALUES RANGE (1, 10)";
7707+
let result = pg_and_generic().parse_sql_statements(sql);
7708+
assert!(result.is_err());
7709+
let err = result.unwrap_err().to_string();
7710+
assert!(
7711+
err.contains("IN, FROM, or WITH"),
7712+
"Expected error about invalid keyword after FOR VALUES, got: {err}"
7713+
);
7714+
7715+
let sql = "CREATE TABLE p PARTITION OF parent FOR VALUES FROM (1)";
7716+
let result = pg_and_generic().parse_sql_statements(sql);
7717+
assert!(result.is_err());
7718+
let err = result.unwrap_err().to_string();
7719+
assert!(
7720+
err.contains("TO"),
7721+
"Expected error about missing TO clause, got: {err}"
7722+
);
7723+
}

0 commit comments

Comments
 (0)