Skip to content

Commit 8392197

Browse files
Read untyped PARTITION OF columns in CREATE FOREIGN TABLE
1 parent 23ccf5a commit 8392197

3 files changed

Lines changed: 43 additions & 4 deletions

File tree

‎src/ast/spans.rs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3400,6 +3400,19 @@ SETTINGS update_parallel_mode = 'auto'"#;
34003400
assert_eq!(test.get_source(options[0].value.span), "'public'");
34013401
}
34023402

3403+
#[test]
3404+
fn test_create_foreign_table_partition_of_span() {
3405+
let dialect = &crate::dialect::PostgreSqlDialect {};
3406+
let sql =
3407+
"CREATE FOREIGN TABLE part PARTITION OF root (id NOT NULL) FOR VALUES IN (1) SERVER s";
3408+
let test = SpanTest::new(dialect, sql);
3409+
let r = Parser::parse_sql(dialect, sql).unwrap();
3410+
assert_eq!(
3411+
test.get_source(r[0].span()),
3412+
"part PARTITION OF root (id NOT NULL) FOR VALUES IN (1) SERVER s"
3413+
);
3414+
}
3415+
34033416
#[test]
34043417
fn test_alter_table_modify_order_by_span() {
34053418
let dialect = &crate::dialect::ClickHouseDialect {};

‎src/parser/mod.rs‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21467,13 +21467,16 @@ impl<'a> Parser<'a> {
2146721467
} else {
2146821468
None
2146921469
};
21470-
if partition_of.is_none() && self.peek_token_ref().token != Token::LParen {
21470+
let (columns, constraints) = if partition_of.is_some() {
21471+
self.parse_partition_of_columns()?
21472+
} else if self.peek_token_ref().token == Token::LParen {
21473+
self.parse_columns()?
21474+
} else {
2147121475
return self.expected_ref(
2147221476
"'(' before the column list of CREATE FOREIGN TABLE",
2147321477
self.peek_token_ref(),
2147421478
);
21475-
}
21476-
let (columns, constraints) = self.parse_columns()?;
21479+
};
2147721480
// `INHERITS` belongs to the column-list form only.
2147821481
let inherits = if partition_of.is_none() && self.parse_keyword(Keyword::INHERITS) {
2147921482
Some(self.parse_parenthesized_qualified_column_list(IsOptional::Mandatory, false)?)

‎tests/sqlparser_postgres.rs‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12237,7 +12237,17 @@ fn parse_create_foreign_table_partition_of() {
1223712237
let Statement::CreateForeignTable(stmt) = pg_and_generic().verified_stmt(sql) else {
1223812238
unreachable!()
1223912239
};
12240-
assert_eq!(stmt.columns.len(), 1);
12240+
assert_eq!(
12241+
stmt.columns,
12242+
vec![ColumnDef {
12243+
name: Ident::new("id"),
12244+
data_type: DataType::Unspecified,
12245+
options: vec![ColumnOptionDef {
12246+
name: None,
12247+
option: ColumnOption::NotNull,
12248+
}],
12249+
}]
12250+
);
1224112251
assert_eq!(stmt.constraints.len(), 1);
1224212252
assert_eq!(
1224312253
stmt.for_values,
@@ -12256,6 +12266,11 @@ fn parse_create_foreign_table_partition_of() {
1225612266
pg_and_generic().verified_stmt(sql);
1225712267
}
1225812268

12269+
pg_and_generic().one_statement_parses_to(
12270+
"CREATE FOREIGN TABLE part PARTITION OF root (id WITH OPTIONS NOT NULL) DEFAULT SERVER s",
12271+
"CREATE FOREIGN TABLE part PARTITION OF root (id NOT NULL) DEFAULT SERVER s",
12272+
);
12273+
1225912274
for (sql, expected) in [
1226012275
(
1226112276
"CREATE FOREIGN TABLE part PARTITION OF root SERVER s",
@@ -12289,6 +12304,14 @@ fn parse_create_foreign_table_partition_of() {
1228912304
"CREATE FOREIGN TABLE part (id INT) PARTITION OF root DEFAULT SERVER s",
1229012305
"Expected: SERVER, found: PARTITION",
1229112306
),
12307+
(
12308+
"CREATE FOREIGN TABLE part PARTITION OF root (id INT) DEFAULT SERVER s",
12309+
"Expected: ',' or ')' after column definition, found: INT",
12310+
),
12311+
(
12312+
"CREATE FOREIGN TABLE part (id) SERVER s",
12313+
"Expected: a data type name, found: )",
12314+
),
1229212315
] {
1229312316
assert_eq!(
1229412317
pg_and_generic()

0 commit comments

Comments
 (0)