Skip to content

Commit fb3f377

Browse files
Read untyped PARTITION OF columns in CREATE FOREIGN TABLE
1 parent 522494b commit fb3f377

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
@@ -3377,6 +3377,19 @@ SETTINGS update_parallel_mode = 'auto'"#;
33773377
assert_eq!(test.get_source(options[0].value.span), "'public'");
33783378
}
33793379

3380+
#[test]
3381+
fn test_create_foreign_table_partition_of_span() {
3382+
let dialect = &crate::dialect::PostgreSqlDialect {};
3383+
let sql =
3384+
"CREATE FOREIGN TABLE part PARTITION OF root (id NOT NULL) FOR VALUES IN (1) SERVER s";
3385+
let test = SpanTest::new(dialect, sql);
3386+
let r = Parser::parse_sql(dialect, sql).unwrap();
3387+
assert_eq!(
3388+
test.get_source(r[0].span()),
3389+
"part PARTITION OF root (id NOT NULL) FOR VALUES IN (1) SERVER s"
3390+
);
3391+
}
3392+
33803393
#[test]
33813394
fn test_alter_table_modify_order_by_span() {
33823395
let dialect = &crate::dialect::ClickHouseDialect {};

‎src/parser/mod.rs‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21404,13 +21404,16 @@ impl<'a> Parser<'a> {
2140421404
} else {
2140521405
None
2140621406
};
21407-
if partition_of.is_none() && self.peek_token_ref().token != Token::LParen {
21407+
let (columns, constraints) = if partition_of.is_some() {
21408+
self.parse_partition_of_columns()?
21409+
} else if self.peek_token_ref().token == Token::LParen {
21410+
self.parse_columns()?
21411+
} else {
2140821412
return self.expected_ref(
2140921413
"'(' before the column list of CREATE FOREIGN TABLE",
2141021414
self.peek_token_ref(),
2141121415
);
21412-
}
21413-
let (columns, constraints) = self.parse_columns()?;
21416+
};
2141421417
// `INHERITS` belongs to the column-list form only.
2141521418
let inherits = if partition_of.is_none() && self.parse_keyword(Keyword::INHERITS) {
2141621419
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
@@ -11815,7 +11815,17 @@ fn parse_create_foreign_table_partition_of() {
1181511815
let Statement::CreateForeignTable(stmt) = pg_and_generic().verified_stmt(sql) else {
1181611816
unreachable!()
1181711817
};
11818-
assert_eq!(stmt.columns.len(), 1);
11818+
assert_eq!(
11819+
stmt.columns,
11820+
vec![ColumnDef {
11821+
name: Ident::new("id"),
11822+
data_type: DataType::Unspecified,
11823+
options: vec![ColumnOptionDef {
11824+
name: None,
11825+
option: ColumnOption::NotNull,
11826+
}],
11827+
}]
11828+
);
1181911829
assert_eq!(stmt.constraints.len(), 1);
1182011830
assert_eq!(
1182111831
stmt.for_values,
@@ -11834,6 +11844,11 @@ fn parse_create_foreign_table_partition_of() {
1183411844
pg_and_generic().verified_stmt(sql);
1183511845
}
1183611846

11847+
pg_and_generic().one_statement_parses_to(
11848+
"CREATE FOREIGN TABLE part PARTITION OF root (id WITH OPTIONS NOT NULL) DEFAULT SERVER s",
11849+
"CREATE FOREIGN TABLE part PARTITION OF root (id NOT NULL) DEFAULT SERVER s",
11850+
);
11851+
1183711852
for (sql, expected) in [
1183811853
(
1183911854
"CREATE FOREIGN TABLE part PARTITION OF root SERVER s",
@@ -11867,6 +11882,14 @@ fn parse_create_foreign_table_partition_of() {
1186711882
"CREATE FOREIGN TABLE part (id INT) PARTITION OF root DEFAULT SERVER s",
1186811883
"Expected: SERVER, found: PARTITION",
1186911884
),
11885+
(
11886+
"CREATE FOREIGN TABLE part PARTITION OF root (id INT) DEFAULT SERVER s",
11887+
"Expected: ',' or ')' after column definition, found: INT",
11888+
),
11889+
(
11890+
"CREATE FOREIGN TABLE part (id) SERVER s",
11891+
"Expected: a data type name, found: )",
11892+
),
1187011893
] {
1187111894
assert_eq!(
1187211895
pg_and_generic()

0 commit comments

Comments
 (0)