You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "fmguerreiro (via GitHub)" <gi...@apache.org> on 2026/04/18 09:09:16 UTC
[PR] PostgreSQL: add support for CREATE FOREIGN TABLE and CREATE FOREIGN DATA WRAPPER [datafusion-sqlparser-rs]
fmguerreiro opened a new pull request, #2317:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/2317
## Summary
Adds PostgreSQL grammar for the two FDW-setup DDL statements used to define foreign data sources:
- \`CREATE FOREIGN DATA WRAPPER name [HANDLER fn | NO HANDLER] [VALIDATOR fn | NO VALIDATOR] [OPTIONS (...)]\` — https://www.postgresql.org/docs/current/sql-createforeigndatawrapper.html
- \`CREATE FOREIGN TABLE [IF NOT EXISTS] name (columns) SERVER server_name [OPTIONS (...)]\` — https://www.postgresql.org/docs/current/sql-createforeigntable.html
## Changes
- \`CreateForeignDataWrapper\`, \`CreateForeignTable\`, and \`FdwRoutineClause\` types in \`src/ast/ddl.rs\` with \`fmt::Display\` + \`From<_> for Statement\`.
- Two new \`Statement\` variants and re-exports.
- \`Spanned\` arms (\`Span::empty()\` stubs — happy to extend if reviewers prefer).
- Dispatcher in \`parse_create\`: \`CREATE FOREIGN DATA WRAPPER ...\` and \`CREATE FOREIGN TABLE ...\`.
- Round-trip tests in \`tests/sqlparser_postgres.rs\`: FDW with/without handlers, \`NO HANDLER\` / \`NO VALIDATOR\`, options list, foreign table with columns + SERVER + optional \`IF NOT EXISTS\` and table-level \`OPTIONS\`.
## Out of scope
\`ALTER FOREIGN TABLE\`, \`ALTER FOREIGN DATA WRAPPER\`, \`CREATE SERVER\`, \`CREATE USER MAPPING\` are not in this PR. \`CREATE SERVER\` actually landed in an earlier PR on the \`pgmold-sqlparser\` fork; happy to upstream that as a follow-up if this PR merges.
## Notes
Originally shipped via the \`pgmold-sqlparser\` fork; tested against real PostgreSQL 13–17 schemas in [pgmold](https://github.com/fmguerreiro/pgmold).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] PostgreSQL: add support for CREATE FOREIGN DATA WRAPPER [datafusion-sqlparser-rs]
Posted by "fmguerreiro (via GitHub)" <gi...@apache.org>.
fmguerreiro commented on PR #2317:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/2317#issuecomment-5692902634
split. `CREATE FOREIGN TABLE` is now #2515.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] PostgreSQL: Support CREATE FOREIGN DATA WRAPPER [datafusion-sqlparser-rs]
Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #2317:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/2317#issuecomment-5803430484
## [Codecov](https://app.codecov.io/gh/apache/datafusion-sqlparser-rs/pull/2317?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
:x: Patch coverage is `84.78261%` with `14 lines` in your changes missing coverage. Please review.
:white_check_mark: Project coverage is 80.98%. Comparing base ([`9ae00e7`](https://app.codecov.io/gh/apache/datafusion-sqlparser-rs/commit/9ae00e73b54ebbca2341c3de22312a8011e98757?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`188e26c`](https://app.codecov.io/gh/apache/datafusion-sqlparser-rs/commit/188e26c703ab598d501a1b0988a37d8d191a6d5c?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
| [Files with missing lines](https://app.codecov.io/gh/apache/datafusion-sqlparser-rs/pull/2317?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
|---|---|---|
| [src/parser/mod.rs](https://app.codecov.io/gh/apache/datafusion-sqlparser-rs/pull/2317?src=pr&el=tree&filepath=src%2Fparser%2Fmod.rs&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3JjL3BhcnNlci9tb2QucnM=) | 78.26% | [0 Missing and 10 partials :warning: ](https://app.codecov.io/gh/apache/datafusion-sqlparser-rs/pull/2317?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
| [src/ast/ddl.rs](https://app.codecov.io/gh/apache/datafusion-sqlparser-rs/pull/2317?src=pr&el=tree&filepath=src%2Fast%2Fddl.rs&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c3JjL2FzdC9kZGwucnM=) | 88.88% | [0 Missing and 4 partials :warning: ](https://app.codecov.io/gh/apache/datafusion-sqlparser-rs/pull/2317?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
<details><summary>Additional details and impacted files</summary>
```diff
@@ Coverage Diff @@
## main #2317 +/- ##
==========================================
+ Coverage 80.96% 80.98% +0.01%
==========================================
Files 42 42
Lines 33359 33441 +82
Branches 33359 33441 +82
==========================================
+ Hits 27009 27082 +73
Misses 2790 2790
- Partials 3560 3569 +9
```
</details>
[:umbrella: View full report in Codecov by Harness](https://app.codecov.io/gh/apache/datafusion-sqlparser-rs/pull/2317?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
<details><summary> :rocket: New features to boost your workflow: </summary>
- :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
</details>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] PostgreSQL: Support CREATE FOREIGN DATA WRAPPER [datafusion-sqlparser-rs]
Posted by "fmguerreiro (via GitHub)" <gi...@apache.org>.
fmguerreiro commented on code in PR #2317:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/2317#discussion_r4114434544
##########
src/parser/mod.rs:
##########
@@ -5400,6 +5400,8 @@ impl<'a> Parser<'a> {
);
}
self.parse_create_foreign_table().map(Into::into)
+ } else if self.parse_keywords(&[Keyword::FOREIGN, Keyword::DATA, Keyword::WRAPPER]) {
+ self.parse_create_foreign_data_wrapper().map(Into::into)
Review Comment:
fixed in ee0ded70.
##########
src/parser/mod.rs:
##########
Review Comment:
done in ee0ded70, both foreign arms share it now.
##########
src/parser/mod.rs:
##########
@@ -5400,6 +5400,8 @@ impl<'a> Parser<'a> {
);
}
Review Comment:
fixed in ee0ded70.
##########
tests/sqlparser_postgres.rs:
##########
@@ -10138,3 +10138,99 @@ fn parse_bitstring_literal_escaping() {
pg_and_generic().verified_stmt("SELECT B''''");
pg_and_generic().verified_stmt("SELECT B'it''s'");
}
+
+#[test]
+fn parse_create_foreign_data_wrapper() {
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(stmt.name.to_string(), "myfdw");
+ assert!(stmt.handler.is_none());
+ assert!(stmt.validator.is_none());
+ assert!(stmt.options.is_none());
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw HANDLER myhandler";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.handler,
+ Some(ForeignDataWrapperRoutineClause::Function(ObjectName::from(
+ vec!["myhandler".into()]
+ )))
+ );
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO HANDLER";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(stmt.handler, Some(ForeignDataWrapperRoutineClause::Absent));
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO VALIDATOR";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.validator,
+ Some(ForeignDataWrapperRoutineClause::Absent)
+ );
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw HANDLER myhandler VALIDATOR myvalidator OPTIONS (debug 'true')";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.options,
+ Some(vec![CreateServerOption {
+ key: "debug".into(),
+ value: Ident {
+ value: "true".to_string(),
+ quote_style: Some('\''),
+ span: Span::empty(),
+ },
+ }])
+ );
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO HANDLER NO VALIDATOR";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(stmt.handler, Some(ForeignDataWrapperRoutineClause::Absent));
+ assert_eq!(
+ stmt.validator,
+ Some(ForeignDataWrapperRoutineClause::Absent)
+ );
+
+ // A schema-qualified name is not valid: FDW names are bare identifiers.
+ assert!(matches!(
+ pg_and_generic().parse_sql_statements("CREATE FOREIGN DATA WRAPPER myschema.myfdw"),
+ Err(ParserError::ParserError(_))
+ ));
Review Comment:
done in ee0ded70.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] PostgreSQL: Support CREATE FOREIGN DATA WRAPPER [datafusion-sqlparser-rs]
Posted by "fmguerreiro (via GitHub)" <gi...@apache.org>.
fmguerreiro commented on code in PR #2317:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/2317#discussion_r4104409327
##########
src/ast/spans.rs:
##########
@@ -3163,4 +3164,19 @@ WHERE id = 1
Span::new(Location::new(2, 8), Location::new(4, 52))
);
}
+
+ #[test]
+ fn test_create_foreign_data_wrapper_span_includes_option_keys() {
+ let dialect = &crate::dialect::PostgreSqlDialect {};
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw HANDLER myhandler OPTIONS (debug 'true')";
+ let mut test = SpanTest::new(dialect, sql);
+
+ // Ends at the option key, not the statement: a quoted option value is an
+ // Ident with an empty span, so it contributes nothing to the union.
+ let stmt = test.0.parse_statement().unwrap();
+ assert_eq!(
+ test.get_source(stmt.span()),
+ "myfdw HANDLER myhandler OPTIONS (debug"
+ );
+ }
Review Comment:
dropped in 59608c62.
##########
tests/sqlparser_postgres.rs:
##########
@@ -9652,6 +9652,105 @@ fn parse_lock_table() {
}
}
+#[test]
+fn parse_create_foreign_data_wrapper() {
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(stmt.name.to_string(), "myfdw");
+ assert!(stmt.handler.is_none());
+ assert!(stmt.validator.is_none());
+ assert!(stmt.options.is_none());
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw HANDLER myhandler";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.handler,
+ Some(ForeignDataWrapperRoutineClause::Function(ObjectName::from(
+ vec!["myhandler".into()]
+ )))
+ );
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO HANDLER";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(stmt.handler, Some(ForeignDataWrapperRoutineClause::Absent));
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO VALIDATOR";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.validator,
+ Some(ForeignDataWrapperRoutineClause::Absent)
+ );
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw HANDLER myhandler VALIDATOR myvalidator OPTIONS (debug 'true')";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.options,
+ Some(vec![CreateServerOption {
+ key: "debug".into(),
+ value: Ident {
+ value: "true".to_string(),
+ quote_style: Some('\''),
+ span: Span::empty(),
+ },
+ }])
+ );
+
+ // A single hardcoded Display label would render this as `NO HANDLER NO HANDLER`.
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO HANDLER NO VALIDATOR";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(stmt.handler, Some(ForeignDataWrapperRoutineClause::Absent));
+ assert_eq!(
+ stmt.validator,
+ Some(ForeignDataWrapperRoutineClause::Absent)
+ );
+
+ // A schema-qualified name is not valid: FDW names are bare identifiers.
+ assert!(matches!(
+ pg_and_generic().parse_sql_statements("CREATE FOREIGN DATA WRAPPER myschema.myfdw"),
+ Err(ParserError::ParserError(_))
+ ));
+}
+
+#[test]
+fn parse_create_foreign_data_wrapper_clause_order() {
+ // PostgreSQL takes these in either order; Display normalizes to HANDLER first.
+ let canonical = "CREATE FOREIGN DATA WRAPPER myfdw HANDLER myhandler VALIDATOR myvalidator";
+ pg_and_generic().one_statement_parses_to(
+ "CREATE FOREIGN DATA WRAPPER myfdw VALIDATOR myvalidator HANDLER myhandler",
+ canonical,
+ );
+ pg_and_generic().one_statement_parses_to(
+ "CREATE FOREIGN DATA WRAPPER myfdw NO VALIDATOR NO HANDLER",
+ "CREATE FOREIGN DATA WRAPPER myfdw NO HANDLER NO VALIDATOR",
+ );
+
+ // Pinning the message keeps this from passing on the old positional parser,
+ // which also errored here but on the trailing tokens instead.
Review Comment:
removed in 59608c62.
##########
tests/sqlparser_postgres.rs:
##########
@@ -9652,6 +9652,105 @@ fn parse_lock_table() {
}
}
+#[test]
+fn parse_create_foreign_data_wrapper() {
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(stmt.name.to_string(), "myfdw");
+ assert!(stmt.handler.is_none());
+ assert!(stmt.validator.is_none());
+ assert!(stmt.options.is_none());
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw HANDLER myhandler";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.handler,
+ Some(ForeignDataWrapperRoutineClause::Function(ObjectName::from(
+ vec!["myhandler".into()]
+ )))
+ );
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO HANDLER";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(stmt.handler, Some(ForeignDataWrapperRoutineClause::Absent));
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO VALIDATOR";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.validator,
+ Some(ForeignDataWrapperRoutineClause::Absent)
+ );
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw HANDLER myhandler VALIDATOR myvalidator OPTIONS (debug 'true')";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.options,
+ Some(vec![CreateServerOption {
+ key: "debug".into(),
+ value: Ident {
+ value: "true".to_string(),
+ quote_style: Some('\''),
+ span: Span::empty(),
+ },
+ }])
+ );
+
+ // A single hardcoded Display label would render this as `NO HANDLER NO HANDLER`.
Review Comment:
removed in 59608c62.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] PostgreSQL: Support CREATE FOREIGN DATA WRAPPER [datafusion-sqlparser-rs]
Posted by "fmguerreiro (via GitHub)" <gi...@apache.org>.
fmguerreiro commented on PR #2317:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/2317#issuecomment-6396156309
> I found the following regression with main, apparently these changes broke the parsing of this mysql syntax:
thanks again! fixed
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] PostgreSQL: Support CREATE FOREIGN DATA WRAPPER [datafusion-sqlparser-rs]
Posted by "LucaCappelletti94 (via GitHub)" <gi...@apache.org>.
LucaCappelletti94 commented on code in PR #2317:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/2317#discussion_r4111346856
##########
src/parser/mod.rs:
##########
@@ -5400,6 +5400,8 @@ impl<'a> Parser<'a> {
);
}
self.parse_create_foreign_table().map(Into::into)
+ } else if self.parse_keywords(&[Keyword::FOREIGN, Keyword::DATA, Keyword::WRAPPER]) {
+ self.parse_create_foreign_data_wrapper().map(Into::into)
Review Comment:
You should reject `CREATE` modifiers here as the `FOREIGN TABLE` arm does. `CREATE TEMPORARY FOREIGN DATA WRAPPER w` and `CREATE OR ALTER FOREIGN DATA WRAPPER w` currently parse and render as `CREATE FOREIGN DATA WRAPPER w`, silently dropping the modifier.
I will propose a red test for these.
```suggestion
} else if self.parse_keywords(&[Keyword::FOREIGN, Keyword::DATA, Keyword::WRAPPER]) {
if has_modifier {
return parser_err!(
"CREATE FOREIGN DATA WRAPPER does not accept this modifier",
modifier_loc
);
}
self.parse_create_foreign_data_wrapper().map(Into::into)
```
##########
src/parser/mod.rs:
##########
Review Comment:
You could move the check so both `CREATE FOREIGN` arms share one list.
```suggestion
let create_view_params = self.parse_create_view_params()?;
// `or_replace` is caught by an earlier arm. It stays so that reordering cannot bypass it.
let has_modifier = or_replace
|| or_alter
|| temporary
|| global.is_some()
|| transient
|| volatile
|| multiset.is_some()
|| persistent
|| create_view_params.is_some();
```
##########
tests/sqlparser_postgres.rs:
##########
@@ -10138,3 +10138,99 @@ fn parse_bitstring_literal_escaping() {
pg_and_generic().verified_stmt("SELECT B''''");
pg_and_generic().verified_stmt("SELECT B'it''s'");
}
+
+#[test]
+fn parse_create_foreign_data_wrapper() {
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(stmt.name.to_string(), "myfdw");
+ assert!(stmt.handler.is_none());
+ assert!(stmt.validator.is_none());
+ assert!(stmt.options.is_none());
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw HANDLER myhandler";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.handler,
+ Some(ForeignDataWrapperRoutineClause::Function(ObjectName::from(
+ vec!["myhandler".into()]
+ )))
+ );
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO HANDLER";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(stmt.handler, Some(ForeignDataWrapperRoutineClause::Absent));
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO VALIDATOR";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.validator,
+ Some(ForeignDataWrapperRoutineClause::Absent)
+ );
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw HANDLER myhandler VALIDATOR myvalidator OPTIONS (debug 'true')";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.options,
+ Some(vec![CreateServerOption {
+ key: "debug".into(),
+ value: Ident {
+ value: "true".to_string(),
+ quote_style: Some('\''),
+ span: Span::empty(),
+ },
+ }])
+ );
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO HANDLER NO VALIDATOR";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(stmt.handler, Some(ForeignDataWrapperRoutineClause::Absent));
+ assert_eq!(
+ stmt.validator,
+ Some(ForeignDataWrapperRoutineClause::Absent)
+ );
+
+ // A schema-qualified name is not valid: FDW names are bare identifiers.
+ assert!(matches!(
+ pg_and_generic().parse_sql_statements("CREATE FOREIGN DATA WRAPPER myschema.myfdw"),
+ Err(ParserError::ParserError(_))
+ ));
Review Comment:
Here are some red tests:
```suggestion
));
for sql in [
"CREATE TEMPORARY FOREIGN DATA WRAPPER myfdw",
"CREATE OR ALTER FOREIGN DATA WRAPPER myfdw",
"CREATE GLOBAL TEMPORARY FOREIGN DATA WRAPPER myfdw",
] {
let err = pg_and_generic().parse_sql_statements(sql).unwrap_err();
assert!(
err.to_string()
.contains("CREATE FOREIGN DATA WRAPPER does not accept this modifier"),
"unexpected error for {sql}: {err}"
);
}
```
##########
src/parser/mod.rs:
##########
@@ -5400,6 +5400,8 @@ impl<'a> Parser<'a> {
);
}
Review Comment:
With the moved `has_modifier`, this arm reduces to the shared check.
```suggestion
if has_modifier {
return parser_err!(
"CREATE FOREIGN TABLE does not accept this modifier",
modifier_loc
);
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] PostgreSQL: add support for CREATE FOREIGN TABLE and CREATE FOREIGN DATA WRAPPER [datafusion-sqlparser-rs]
Posted by "LucaCappelletti94 (via GitHub)" <gi...@apache.org>.
LucaCappelletti94 commented on PR #2317:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/2317#issuecomment-5686991665
I suggest to split the PR into two, once for each statement
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] PostgreSQL: Support CREATE FOREIGN DATA WRAPPER [datafusion-sqlparser-rs]
Posted by "LucaCappelletti94 (via GitHub)" <gi...@apache.org>.
LucaCappelletti94 commented on code in PR #2317:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/2317#discussion_r4090961383
##########
src/ast/spans.rs:
##########
@@ -3163,4 +3164,19 @@ WHERE id = 1
Span::new(Location::new(2, 8), Location::new(4, 52))
);
}
+
+ #[test]
+ fn test_create_foreign_data_wrapper_span_includes_option_keys() {
+ let dialect = &crate::dialect::PostgreSqlDialect {};
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw HANDLER myhandler OPTIONS (debug 'true')";
+ let mut test = SpanTest::new(dialect, sql);
+
+ // Ends at the option key, not the statement: a quoted option value is an
+ // Ident with an empty span, so it contributes nothing to the union.
+ let stmt = test.0.parse_statement().unwrap();
+ assert_eq!(
+ test.get_source(stmt.span()),
+ "myfdw HANDLER myhandler OPTIONS (debug"
+ );
+ }
Review Comment:
You should drop this test. It asserts the truncated span `... OPTIONS (debug`, which comes from `parse_identifier` returning `Span::empty()` for single-quoted strings (`src/parser/mod.rs:13877`). The test turns that defect into a contract, so a later fix of the value span would break it.
```suggestion
```
##########
tests/sqlparser_postgres.rs:
##########
@@ -9652,6 +9652,105 @@ fn parse_lock_table() {
}
}
+#[test]
+fn parse_create_foreign_data_wrapper() {
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(stmt.name.to_string(), "myfdw");
+ assert!(stmt.handler.is_none());
+ assert!(stmt.validator.is_none());
+ assert!(stmt.options.is_none());
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw HANDLER myhandler";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.handler,
+ Some(ForeignDataWrapperRoutineClause::Function(ObjectName::from(
+ vec!["myhandler".into()]
+ )))
+ );
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO HANDLER";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(stmt.handler, Some(ForeignDataWrapperRoutineClause::Absent));
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO VALIDATOR";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.validator,
+ Some(ForeignDataWrapperRoutineClause::Absent)
+ );
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw HANDLER myhandler VALIDATOR myvalidator OPTIONS (debug 'true')";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.options,
+ Some(vec![CreateServerOption {
+ key: "debug".into(),
+ value: Ident {
+ value: "true".to_string(),
+ quote_style: Some('\''),
+ span: Span::empty(),
+ },
+ }])
+ );
+
+ // A single hardcoded Display label would render this as `NO HANDLER NO HANDLER`.
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO HANDLER NO VALIDATOR";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(stmt.handler, Some(ForeignDataWrapperRoutineClause::Absent));
+ assert_eq!(
+ stmt.validator,
+ Some(ForeignDataWrapperRoutineClause::Absent)
+ );
+
+ // A schema-qualified name is not valid: FDW names are bare identifiers.
+ assert!(matches!(
+ pg_and_generic().parse_sql_statements("CREATE FOREIGN DATA WRAPPER myschema.myfdw"),
+ Err(ParserError::ParserError(_))
+ ));
+}
+
+#[test]
+fn parse_create_foreign_data_wrapper_clause_order() {
+ // PostgreSQL takes these in either order; Display normalizes to HANDLER first.
+ let canonical = "CREATE FOREIGN DATA WRAPPER myfdw HANDLER myhandler VALIDATOR myvalidator";
+ pg_and_generic().one_statement_parses_to(
+ "CREATE FOREIGN DATA WRAPPER myfdw VALIDATOR myvalidator HANDLER myhandler",
+ canonical,
+ );
+ pg_and_generic().one_statement_parses_to(
+ "CREATE FOREIGN DATA WRAPPER myfdw NO VALIDATOR NO HANDLER",
+ "CREATE FOREIGN DATA WRAPPER myfdw NO HANDLER NO VALIDATOR",
+ );
+
+ // Pinning the message keeps this from passing on the old positional parser,
+ // which also errored here but on the trailing tokens instead.
Review Comment:
Differential comment with previous code.
```suggestion
```
##########
tests/sqlparser_postgres.rs:
##########
@@ -9652,6 +9652,105 @@ fn parse_lock_table() {
}
}
+#[test]
+fn parse_create_foreign_data_wrapper() {
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(stmt.name.to_string(), "myfdw");
+ assert!(stmt.handler.is_none());
+ assert!(stmt.validator.is_none());
+ assert!(stmt.options.is_none());
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw HANDLER myhandler";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.handler,
+ Some(ForeignDataWrapperRoutineClause::Function(ObjectName::from(
+ vec!["myhandler".into()]
+ )))
+ );
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO HANDLER";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(stmt.handler, Some(ForeignDataWrapperRoutineClause::Absent));
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO VALIDATOR";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.validator,
+ Some(ForeignDataWrapperRoutineClause::Absent)
+ );
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw HANDLER myhandler VALIDATOR myvalidator OPTIONS (debug 'true')";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.options,
+ Some(vec![CreateServerOption {
+ key: "debug".into(),
+ value: Ident {
+ value: "true".to_string(),
+ quote_style: Some('\''),
+ span: Span::empty(),
+ },
+ }])
+ );
+
+ // A single hardcoded Display label would render this as `NO HANDLER NO HANDLER`.
Review Comment:
Do not include comments describing the PR change, comments are for describing the code.
```suggestion
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] PostgreSQL: Support CREATE FOREIGN DATA WRAPPER [datafusion-sqlparser-rs]
Posted by "fmguerreiro (via GitHub)" <gi...@apache.org>.
fmguerreiro commented on code in PR #2317:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/2317#discussion_r4335697854
##########
tests/sqlparser_postgres.rs:
##########
@@ -10154,3 +10154,112 @@ fn parse_bitstring_literal_escaping() {
pg_and_generic().verified_stmt("SELECT B''''");
pg_and_generic().verified_stmt("SELECT B'it''s'");
}
+
+#[test]
+fn parse_create_foreign_data_wrapper() {
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(stmt.name.to_string(), "myfdw");
+ assert!(stmt.handler.is_none());
+ assert!(stmt.validator.is_none());
+ assert!(stmt.options.is_none());
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw HANDLER myhandler";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.handler,
+ Some(ForeignDataWrapperRoutineClause::Function(ObjectName::from(
+ vec!["myhandler".into()]
+ )))
+ );
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO HANDLER";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(stmt.handler, Some(ForeignDataWrapperRoutineClause::Absent));
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO VALIDATOR";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.validator,
+ Some(ForeignDataWrapperRoutineClause::Absent)
+ );
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw HANDLER myhandler VALIDATOR myvalidator OPTIONS (debug 'true')";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.options,
+ Some(vec![CreateServerOption {
+ key: "debug".into(),
+ value: Ident {
+ value: "true".to_string(),
+ quote_style: Some('\''),
+ span: Span::empty(),
+ },
+ }])
+ );
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO HANDLER NO VALIDATOR";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(stmt.handler, Some(ForeignDataWrapperRoutineClause::Absent));
+ assert_eq!(
+ stmt.validator,
+ Some(ForeignDataWrapperRoutineClause::Absent)
+ );
Review Comment:
fixed in 82fb8a4e.
##########
tests/sqlparser_postgres.rs:
##########
@@ -10154,3 +10154,112 @@ fn parse_bitstring_literal_escaping() {
pg_and_generic().verified_stmt("SELECT B''''");
pg_and_generic().verified_stmt("SELECT B'it''s'");
}
+
+#[test]
+fn parse_create_foreign_data_wrapper() {
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(stmt.name.to_string(), "myfdw");
+ assert!(stmt.handler.is_none());
+ assert!(stmt.validator.is_none());
+ assert!(stmt.options.is_none());
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw HANDLER myhandler";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.handler,
+ Some(ForeignDataWrapperRoutineClause::Function(ObjectName::from(
+ vec!["myhandler".into()]
+ )))
+ );
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO HANDLER";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(stmt.handler, Some(ForeignDataWrapperRoutineClause::Absent));
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO VALIDATOR";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.validator,
+ Some(ForeignDataWrapperRoutineClause::Absent)
+ );
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw HANDLER myhandler VALIDATOR myvalidator OPTIONS (debug 'true')";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.options,
+ Some(vec![CreateServerOption {
+ key: "debug".into(),
+ value: Ident {
+ value: "true".to_string(),
+ quote_style: Some('\''),
+ span: Span::empty(),
+ },
+ }])
+ );
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO HANDLER NO VALIDATOR";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(stmt.handler, Some(ForeignDataWrapperRoutineClause::Absent));
+ assert_eq!(
+ stmt.validator,
+ Some(ForeignDataWrapperRoutineClause::Absent)
+ );
+
+ // A schema-qualified name is not valid: FDW names are bare identifiers.
+ assert!(matches!(
+ pg_and_generic().parse_sql_statements("CREATE FOREIGN DATA WRAPPER myschema.myfdw"),
+ Err(ParserError::ParserError(_))
+ ));
Review Comment:
fixed in b7505f95.
##########
tests/sqlparser_postgres.rs:
##########
@@ -10154,3 +10154,112 @@ fn parse_bitstring_literal_escaping() {
pg_and_generic().verified_stmt("SELECT B''''");
pg_and_generic().verified_stmt("SELECT B'it''s'");
}
+
+#[test]
+fn parse_create_foreign_data_wrapper() {
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(stmt.name.to_string(), "myfdw");
+ assert!(stmt.handler.is_none());
+ assert!(stmt.validator.is_none());
+ assert!(stmt.options.is_none());
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw HANDLER myhandler";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.handler,
+ Some(ForeignDataWrapperRoutineClause::Function(ObjectName::from(
+ vec!["myhandler".into()]
+ )))
+ );
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO HANDLER";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(stmt.handler, Some(ForeignDataWrapperRoutineClause::Absent));
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO VALIDATOR";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.validator,
+ Some(ForeignDataWrapperRoutineClause::Absent)
+ );
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw HANDLER myhandler VALIDATOR myvalidator OPTIONS (debug 'true')";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.options,
+ Some(vec![CreateServerOption {
+ key: "debug".into(),
+ value: Ident {
+ value: "true".to_string(),
+ quote_style: Some('\''),
+ span: Span::empty(),
+ },
+ }])
+ );
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO HANDLER NO VALIDATOR";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(stmt.handler, Some(ForeignDataWrapperRoutineClause::Absent));
+ assert_eq!(
+ stmt.validator,
+ Some(ForeignDataWrapperRoutineClause::Absent)
+ );
+
+ // A schema-qualified name is not valid: FDW names are bare identifiers.
+ assert!(matches!(
+ pg_and_generic().parse_sql_statements("CREATE FOREIGN DATA WRAPPER myschema.myfdw"),
+ Err(ParserError::ParserError(_))
+ ));
+
+ for sql in [
+ "CREATE TEMPORARY FOREIGN DATA WRAPPER myfdw",
+ "CREATE OR ALTER FOREIGN DATA WRAPPER myfdw",
+ "CREATE GLOBAL TEMPORARY FOREIGN DATA WRAPPER myfdw",
+ ] {
+ let err = pg_and_generic().parse_sql_statements(sql).unwrap_err();
+ assert!(
+ err.to_string()
+ .contains("CREATE FOREIGN DATA WRAPPER does not accept this modifier"),
+ "unexpected error for {sql}: {err}"
+ );
+ }
+}
+
+#[test]
+fn parse_create_foreign_data_wrapper_clause_order() {
+ // PostgreSQL takes these in either order; Display normalizes to HANDLER first.
+ let canonical = "CREATE FOREIGN DATA WRAPPER myfdw HANDLER myhandler VALIDATOR myvalidator";
+ pg_and_generic().one_statement_parses_to(
+ "CREATE FOREIGN DATA WRAPPER myfdw VALIDATOR myvalidator HANDLER myhandler",
+ canonical,
+ );
+ pg_and_generic().one_statement_parses_to(
+ "CREATE FOREIGN DATA WRAPPER myfdw NO VALIDATOR NO HANDLER",
+ "CREATE FOREIGN DATA WRAPPER myfdw NO HANDLER NO VALIDATOR",
+ );
+
+ for sql in [
+ "CREATE FOREIGN DATA WRAPPER myfdw HANDLER h1 HANDLER h2",
+ "CREATE FOREIGN DATA WRAPPER myfdw NO HANDLER HANDLER h",
+ "CREATE FOREIGN DATA WRAPPER myfdw VALIDATOR v NO VALIDATOR",
+ ] {
+ let err = pg_and_generic().parse_sql_statements(sql).unwrap_err();
+ assert!(
+ err.to_string().contains("conflicting or redundant options"),
+ "unexpected error for {sql}: {err}"
+ );
+ }
+}
Review Comment:
fixed in e02c957a.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] PostgreSQL: Support CREATE FOREIGN DATA WRAPPER [datafusion-sqlparser-rs]
Posted by "LucaCappelletti94 (via GitHub)" <gi...@apache.org>.
LucaCappelletti94 merged PR #2317:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/2317
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] PostgreSQL: Support CREATE FOREIGN DATA WRAPPER [datafusion-sqlparser-rs]
Posted by "fmguerreiro (via GitHub)" <gi...@apache.org>.
fmguerreiro commented on code in PR #2317:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/2317#discussion_r4335695882
##########
src/parser/mod.rs:
##########
@@ -20756,6 +20763,53 @@ impl<'a> Parser<'a> {
})
}
+ /// Parse a `CREATE FOREIGN DATA WRAPPER` statement.
+ ///
+ /// See <https://www.postgresql.org/docs/current/sql-createforeigndatawrapper.html>
+ pub fn parse_create_foreign_data_wrapper(
+ &mut self,
+ ) -> Result<CreateForeignDataWrapper, ParserError> {
+ let name = self.parse_identifier()?;
+
+ // PostgreSQL accepts HANDLER and VALIDATOR in either order, so they are
+ // consumed in a loop rather than positionally.
+ let mut handler = None;
+ let mut validator = None;
+ loop {
+ let loc = self.peek_token_ref().span.start;
+ let (slot, clause) = if self.parse_keyword(Keyword::HANDLER) {
+ (
+ &mut handler,
+ ForeignDataWrapperRoutineClause::Function(self.parse_object_name(false)?),
+ )
+ } else if self.parse_keywords(&[Keyword::NO, Keyword::HANDLER]) {
+ (&mut handler, ForeignDataWrapperRoutineClause::Absent)
+ } else if self.parse_keyword(Keyword::VALIDATOR) {
+ (
+ &mut validator,
+ ForeignDataWrapperRoutineClause::Function(self.parse_object_name(false)?),
+ )
+ } else if self.parse_keywords(&[Keyword::NO, Keyword::VALIDATOR]) {
+ (&mut validator, ForeignDataWrapperRoutineClause::Absent)
+ } else {
+ break;
+ };
+ if slot.is_some() {
+ return parser_err!("conflicting or redundant options", loc);
+ }
+ *slot = Some(clause);
+ }
+
+ let options = self.parse_pg_options_clause()?;
+
+ Ok(CreateForeignDataWrapper {
+ name,
+ handler,
+ validator,
+ options,
+ })
Review Comment:
fixed in df5a604c.
##########
src/ast/ddl.rs:
##########
@@ -6056,6 +6056,60 @@ impl fmt::Display for CreateForeignTable {
}
}
+/// The handler/validator clause of a `CREATE FOREIGN DATA WRAPPER` statement.
+#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
+#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
+#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
+pub enum ForeignDataWrapperRoutineClause {
+ /// A named function, e.g. `HANDLER myhandler` or `VALIDATOR myvalidator`.
+ Function(ObjectName),
+ /// The `NO HANDLER` / `NO VALIDATOR` form.
+ Absent,
+}
+
+impl ForeignDataWrapperRoutineClause {
+ fn fmt_with_label(&self, f: &mut fmt::Formatter<'_>, label: &str) -> fmt::Result {
+ match self {
+ ForeignDataWrapperRoutineClause::Function(name) => write!(f, " {label} {name}"),
+ ForeignDataWrapperRoutineClause::Absent => write!(f, " NO {label}"),
+ }
+ }
+}
Review Comment:
fixed in 6c333723.
##########
src/ast/ddl.rs:
##########
@@ -6056,6 +6056,60 @@ impl fmt::Display for CreateForeignTable {
}
}
+/// The handler/validator clause of a `CREATE FOREIGN DATA WRAPPER` statement.
+#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
+#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
+#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
+pub enum ForeignDataWrapperRoutineClause {
+ /// A named function, e.g. `HANDLER myhandler` or `VALIDATOR myvalidator`.
+ Function(ObjectName),
+ /// The `NO HANDLER` / `NO VALIDATOR` form.
+ Absent,
+}
+
+impl ForeignDataWrapperRoutineClause {
+ fn fmt_with_label(&self, f: &mut fmt::Formatter<'_>, label: &str) -> fmt::Result {
+ match self {
+ ForeignDataWrapperRoutineClause::Function(name) => write!(f, " {label} {name}"),
+ ForeignDataWrapperRoutineClause::Absent => write!(f, " NO {label}"),
+ }
+ }
+}
+
+/// A `CREATE FOREIGN DATA WRAPPER` statement.
+///
+/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-createforeigndatawrapper.html)
+#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
+#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
+#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
+pub struct CreateForeignDataWrapper {
+ /// The name of the foreign-data wrapper. Per PostgreSQL this is a bare
+ /// identifier and cannot be schema-qualified.
+ pub name: Ident,
+ /// Optional `HANDLER handler_function` or `NO HANDLER` clause.
+ pub handler: Option<ForeignDataWrapperRoutineClause>,
+ /// Optional `VALIDATOR validator_function` or `NO VALIDATOR` clause.
+ pub validator: Option<ForeignDataWrapperRoutineClause>,
Review Comment:
fixed in 09072669.
##########
src/ast/ddl.rs:
##########
@@ -6056,6 +6056,60 @@ impl fmt::Display for CreateForeignTable {
}
}
+/// The handler/validator clause of a `CREATE FOREIGN DATA WRAPPER` statement.
+#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
+#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
+#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
+pub enum ForeignDataWrapperRoutineClause {
+ /// A named function, e.g. `HANDLER myhandler` or `VALIDATOR myvalidator`.
+ Function(ObjectName),
+ /// The `NO HANDLER` / `NO VALIDATOR` form.
+ Absent,
+}
+
+impl ForeignDataWrapperRoutineClause {
+ fn fmt_with_label(&self, f: &mut fmt::Formatter<'_>, label: &str) -> fmt::Result {
+ match self {
+ ForeignDataWrapperRoutineClause::Function(name) => write!(f, " {label} {name}"),
+ ForeignDataWrapperRoutineClause::Absent => write!(f, " NO {label}"),
+ }
+ }
+}
+
+/// A `CREATE FOREIGN DATA WRAPPER` statement.
+///
+/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-createforeigndatawrapper.html)
+#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
+#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
+#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
+pub struct CreateForeignDataWrapper {
+ /// The name of the foreign-data wrapper. Per PostgreSQL this is a bare
+ /// identifier and cannot be schema-qualified.
+ pub name: Ident,
+ /// Optional `HANDLER handler_function` or `NO HANDLER` clause.
+ pub handler: Option<ForeignDataWrapperRoutineClause>,
+ /// Optional `VALIDATOR validator_function` or `NO VALIDATOR` clause.
+ pub validator: Option<ForeignDataWrapperRoutineClause>,
+ /// Optional `OPTIONS (key 'value', ...)` clause.
+ pub options: Option<Vec<CreateServerOption>>,
+}
+
+impl fmt::Display for CreateForeignDataWrapper {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(f, "CREATE FOREIGN DATA WRAPPER {}", self.name)?;
+ if let Some(handler) = &self.handler {
+ handler.fmt_with_label(f, "HANDLER")?;
+ }
+ if let Some(validator) = &self.validator {
+ validator.fmt_with_label(f, "VALIDATOR")?;
+ }
Review Comment:
fixed in b62f3d9e.
##########
src/ast/ddl.rs:
##########
@@ -6078,3 +6132,29 @@ impl Spanned for CreateForeignTable {
)
}
}
+
+impl From<CreateForeignDataWrapper> for crate::ast::Statement {
+ fn from(v: CreateForeignDataWrapper) -> Self {
+ crate::ast::Statement::CreateForeignDataWrapper(v)
+ }
+}
+
+impl Spanned for CreateForeignDataWrapper {
+ fn span(&self) -> Span {
+ let routine_span = |clause: &Option<ForeignDataWrapperRoutineClause>| match clause {
+ Some(ForeignDataWrapperRoutineClause::Function(name)) => Some(name.span()),
+ _ => None,
+ };
+ Span::union_iter(
+ core::iter::once(self.name.span)
+ .chain(routine_span(&self.handler))
+ .chain(routine_span(&self.validator))
Review Comment:
fixed in b73f98fb.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] PostgreSQL: Support CREATE FOREIGN DATA WRAPPER [datafusion-sqlparser-rs]
Posted by "LucaCappelletti94 (via GitHub)" <gi...@apache.org>.
LucaCappelletti94 commented on code in PR #2317:
URL: https://github.com/apache/datafusion-sqlparser-rs/pull/2317#discussion_r4294232999
##########
src/ast/ddl.rs:
##########
@@ -6056,6 +6056,60 @@ impl fmt::Display for CreateForeignTable {
}
}
+/// The handler/validator clause of a `CREATE FOREIGN DATA WRAPPER` statement.
+#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
+#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
+#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
+pub enum ForeignDataWrapperRoutineClause {
+ /// A named function, e.g. `HANDLER myhandler` or `VALIDATOR myvalidator`.
+ Function(ObjectName),
+ /// The `NO HANDLER` / `NO VALIDATOR` form.
+ Absent,
+}
+
+impl ForeignDataWrapperRoutineClause {
+ fn fmt_with_label(&self, f: &mut fmt::Formatter<'_>, label: &str) -> fmt::Result {
+ match self {
+ ForeignDataWrapperRoutineClause::Function(name) => write!(f, " {label} {name}"),
+ ForeignDataWrapperRoutineClause::Absent => write!(f, " NO {label}"),
+ }
+ }
+}
Review Comment:
You should make each clause a variant, mirroring the four `fdw_option` productions, so a list can hold repeats and either order.
```suggestion
/// A handler or validator clause of a `CREATE FOREIGN DATA WRAPPER` statement.
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum ForeignDataWrapperRoutineClause {
/// `HANDLER handler_function`
Handler(ObjectName),
/// `NO HANDLER`
NoHandler,
/// `VALIDATOR validator_function`
Validator(ObjectName),
/// `NO VALIDATOR`
NoValidator,
}
impl fmt::Display for ForeignDataWrapperRoutineClause {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ForeignDataWrapperRoutineClause::Handler(name) => write!(f, "HANDLER {name}"),
ForeignDataWrapperRoutineClause::NoHandler => f.write_str("NO HANDLER"),
ForeignDataWrapperRoutineClause::Validator(name) => write!(f, "VALIDATOR {name}"),
ForeignDataWrapperRoutineClause::NoValidator => f.write_str("NO VALIDATOR"),
}
}
}
```
##########
src/parser/mod.rs:
##########
@@ -20756,6 +20763,53 @@ impl<'a> Parser<'a> {
})
}
+ /// Parse a `CREATE FOREIGN DATA WRAPPER` statement.
+ ///
+ /// See <https://www.postgresql.org/docs/current/sql-createforeigndatawrapper.html>
+ pub fn parse_create_foreign_data_wrapper(
+ &mut self,
+ ) -> Result<CreateForeignDataWrapper, ParserError> {
+ let name = self.parse_identifier()?;
+
+ // PostgreSQL accepts HANDLER and VALIDATOR in either order, so they are
+ // consumed in a loop rather than positionally.
+ let mut handler = None;
+ let mut validator = None;
+ loop {
+ let loc = self.peek_token_ref().span.start;
+ let (slot, clause) = if self.parse_keyword(Keyword::HANDLER) {
+ (
+ &mut handler,
+ ForeignDataWrapperRoutineClause::Function(self.parse_object_name(false)?),
+ )
+ } else if self.parse_keywords(&[Keyword::NO, Keyword::HANDLER]) {
+ (&mut handler, ForeignDataWrapperRoutineClause::Absent)
+ } else if self.parse_keyword(Keyword::VALIDATOR) {
+ (
+ &mut validator,
+ ForeignDataWrapperRoutineClause::Function(self.parse_object_name(false)?),
+ )
+ } else if self.parse_keywords(&[Keyword::NO, Keyword::VALIDATOR]) {
+ (&mut validator, ForeignDataWrapperRoutineClause::Absent)
+ } else {
+ break;
+ };
+ if slot.is_some() {
+ return parser_err!("conflicting or redundant options", loc);
+ }
+ *slot = Some(clause);
+ }
+
+ let options = self.parse_pg_options_clause()?;
+
+ Ok(CreateForeignDataWrapper {
+ name,
+ handler,
+ validator,
+ options,
+ })
Review Comment:
You should collect the clauses in source order instead of rejecting repeats. PostgreSQL's grammar takes `fdw_options` as a plain list and raises "conflicting or redundant options" only in `parse_func_options` when the statement executes. `pg_query` 6.2 accepts `CREATE FOREIGN DATA WRAPPER w HANDLER h1 HANDLER h2`, which this loop rejects, so `fuzz_postgres_accepts` panics on it and on any mutation of the new seeds that repeats a clause.
```suggestion
let mut routines = Vec::new();
loop {
let routine = if self.parse_keyword(Keyword::HANDLER) {
ForeignDataWrapperRoutineClause::Handler(self.parse_object_name(false)?)
} else if self.parse_keywords(&[Keyword::NO, Keyword::HANDLER]) {
ForeignDataWrapperRoutineClause::NoHandler
} else if self.parse_keyword(Keyword::VALIDATOR) {
ForeignDataWrapperRoutineClause::Validator(self.parse_object_name(false)?)
} else if self.parse_keywords(&[Keyword::NO, Keyword::VALIDATOR]) {
ForeignDataWrapperRoutineClause::NoValidator
} else {
break;
};
routines.push(routine);
}
let options = self.parse_pg_options_clause()?;
Ok(CreateForeignDataWrapper {
name,
routines,
options,
})
```
##########
src/ast/ddl.rs:
##########
@@ -6056,6 +6056,60 @@ impl fmt::Display for CreateForeignTable {
}
}
+/// The handler/validator clause of a `CREATE FOREIGN DATA WRAPPER` statement.
+#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
+#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
+#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
+pub enum ForeignDataWrapperRoutineClause {
+ /// A named function, e.g. `HANDLER myhandler` or `VALIDATOR myvalidator`.
+ Function(ObjectName),
+ /// The `NO HANDLER` / `NO VALIDATOR` form.
+ Absent,
+}
+
+impl ForeignDataWrapperRoutineClause {
+ fn fmt_with_label(&self, f: &mut fmt::Formatter<'_>, label: &str) -> fmt::Result {
+ match self {
+ ForeignDataWrapperRoutineClause::Function(name) => write!(f, " {label} {name}"),
+ ForeignDataWrapperRoutineClause::Absent => write!(f, " NO {label}"),
+ }
+ }
+}
+
+/// A `CREATE FOREIGN DATA WRAPPER` statement.
+///
+/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-createforeigndatawrapper.html)
+#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
+#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
+#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
+pub struct CreateForeignDataWrapper {
+ /// The name of the foreign-data wrapper. Per PostgreSQL this is a bare
+ /// identifier and cannot be schema-qualified.
+ pub name: Ident,
+ /// Optional `HANDLER handler_function` or `NO HANDLER` clause.
+ pub handler: Option<ForeignDataWrapperRoutineClause>,
+ /// Optional `VALIDATOR validator_function` or `NO VALIDATOR` clause.
+ pub validator: Option<ForeignDataWrapperRoutineClause>,
Review Comment:
```suggestion
/// `HANDLER` and `VALIDATOR` clauses in source order. PostgreSQL's grammar
/// accepts repeated clauses and rejects them only when executing the statement.
pub routines: Vec<ForeignDataWrapperRoutineClause>,
```
##########
src/ast/ddl.rs:
##########
@@ -6078,3 +6132,29 @@ impl Spanned for CreateForeignTable {
)
}
}
+
+impl From<CreateForeignDataWrapper> for crate::ast::Statement {
+ fn from(v: CreateForeignDataWrapper) -> Self {
+ crate::ast::Statement::CreateForeignDataWrapper(v)
+ }
+}
+
+impl Spanned for CreateForeignDataWrapper {
+ fn span(&self) -> Span {
+ let routine_span = |clause: &Option<ForeignDataWrapperRoutineClause>| match clause {
+ Some(ForeignDataWrapperRoutineClause::Function(name)) => Some(name.span()),
+ _ => None,
+ };
+ Span::union_iter(
+ core::iter::once(self.name.span)
+ .chain(routine_span(&self.handler))
+ .chain(routine_span(&self.validator))
Review Comment:
You should take the spans from the list.
```suggestion
Span::union_iter(
core::iter::once(self.name.span)
.chain(self.routines.iter().filter_map(|routine| match routine {
ForeignDataWrapperRoutineClause::Handler(name)
| ForeignDataWrapperRoutineClause::Validator(name) => Some(name.span()),
_ => None,
}))
```
##########
tests/sqlparser_postgres.rs:
##########
@@ -10154,3 +10154,112 @@ fn parse_bitstring_literal_escaping() {
pg_and_generic().verified_stmt("SELECT B''''");
pg_and_generic().verified_stmt("SELECT B'it''s'");
}
+
+#[test]
+fn parse_create_foreign_data_wrapper() {
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(stmt.name.to_string(), "myfdw");
+ assert!(stmt.handler.is_none());
+ assert!(stmt.validator.is_none());
+ assert!(stmt.options.is_none());
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw HANDLER myhandler";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.handler,
+ Some(ForeignDataWrapperRoutineClause::Function(ObjectName::from(
+ vec!["myhandler".into()]
+ )))
+ );
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO HANDLER";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(stmt.handler, Some(ForeignDataWrapperRoutineClause::Absent));
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO VALIDATOR";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.validator,
+ Some(ForeignDataWrapperRoutineClause::Absent)
+ );
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw HANDLER myhandler VALIDATOR myvalidator OPTIONS (debug 'true')";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.options,
+ Some(vec![CreateServerOption {
+ key: "debug".into(),
+ value: Ident {
+ value: "true".to_string(),
+ quote_style: Some('\''),
+ span: Span::empty(),
+ },
+ }])
+ );
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO HANDLER NO VALIDATOR";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(stmt.handler, Some(ForeignDataWrapperRoutineClause::Absent));
+ assert_eq!(
+ stmt.validator,
+ Some(ForeignDataWrapperRoutineClause::Absent)
+ );
+
+ // A schema-qualified name is not valid: FDW names are bare identifiers.
+ assert!(matches!(
+ pg_and_generic().parse_sql_statements("CREATE FOREIGN DATA WRAPPER myschema.myfdw"),
+ Err(ParserError::ParserError(_))
+ ));
+
+ for sql in [
+ "CREATE TEMPORARY FOREIGN DATA WRAPPER myfdw",
+ "CREATE OR ALTER FOREIGN DATA WRAPPER myfdw",
+ "CREATE GLOBAL TEMPORARY FOREIGN DATA WRAPPER myfdw",
+ ] {
+ let err = pg_and_generic().parse_sql_statements(sql).unwrap_err();
+ assert!(
+ err.to_string()
+ .contains("CREATE FOREIGN DATA WRAPPER does not accept this modifier"),
+ "unexpected error for {sql}: {err}"
+ );
+ }
+}
+
+#[test]
+fn parse_create_foreign_data_wrapper_clause_order() {
+ // PostgreSQL takes these in either order; Display normalizes to HANDLER first.
+ let canonical = "CREATE FOREIGN DATA WRAPPER myfdw HANDLER myhandler VALIDATOR myvalidator";
+ pg_and_generic().one_statement_parses_to(
+ "CREATE FOREIGN DATA WRAPPER myfdw VALIDATOR myvalidator HANDLER myhandler",
+ canonical,
+ );
+ pg_and_generic().one_statement_parses_to(
+ "CREATE FOREIGN DATA WRAPPER myfdw NO VALIDATOR NO HANDLER",
+ "CREATE FOREIGN DATA WRAPPER myfdw NO HANDLER NO VALIDATOR",
+ );
+
+ for sql in [
+ "CREATE FOREIGN DATA WRAPPER myfdw HANDLER h1 HANDLER h2",
+ "CREATE FOREIGN DATA WRAPPER myfdw NO HANDLER HANDLER h",
+ "CREATE FOREIGN DATA WRAPPER myfdw VALIDATOR v NO VALIDATOR",
+ ] {
+ let err = pg_and_generic().parse_sql_statements(sql).unwrap_err();
+ assert!(
+ err.to_string().contains("conflicting or redundant options"),
+ "unexpected error for {sql}: {err}"
+ );
+ }
+}
Review Comment:
You should pin that repeated and reordered clauses round-trip as `pg_query` accepts all five of these.
```suggestion
fn parse_create_foreign_data_wrapper_clause_order() {
for sql in [
"CREATE FOREIGN DATA WRAPPER myfdw VALIDATOR myvalidator HANDLER myhandler",
"CREATE FOREIGN DATA WRAPPER myfdw NO VALIDATOR NO HANDLER",
"CREATE FOREIGN DATA WRAPPER myfdw HANDLER h1 HANDLER h2",
"CREATE FOREIGN DATA WRAPPER myfdw NO HANDLER HANDLER h",
"CREATE FOREIGN DATA WRAPPER myfdw VALIDATOR v NO VALIDATOR",
] {
pg_and_generic().verified_stmt(sql);
}
}
```
##########
src/ast/ddl.rs:
##########
@@ -6056,6 +6056,60 @@ impl fmt::Display for CreateForeignTable {
}
}
+/// The handler/validator clause of a `CREATE FOREIGN DATA WRAPPER` statement.
+#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
+#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
+#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
+pub enum ForeignDataWrapperRoutineClause {
+ /// A named function, e.g. `HANDLER myhandler` or `VALIDATOR myvalidator`.
+ Function(ObjectName),
+ /// The `NO HANDLER` / `NO VALIDATOR` form.
+ Absent,
+}
+
+impl ForeignDataWrapperRoutineClause {
+ fn fmt_with_label(&self, f: &mut fmt::Formatter<'_>, label: &str) -> fmt::Result {
+ match self {
+ ForeignDataWrapperRoutineClause::Function(name) => write!(f, " {label} {name}"),
+ ForeignDataWrapperRoutineClause::Absent => write!(f, " NO {label}"),
+ }
+ }
+}
+
+/// A `CREATE FOREIGN DATA WRAPPER` statement.
+///
+/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-createforeigndatawrapper.html)
+#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
+#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
+#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
+pub struct CreateForeignDataWrapper {
+ /// The name of the foreign-data wrapper. Per PostgreSQL this is a bare
+ /// identifier and cannot be schema-qualified.
+ pub name: Ident,
+ /// Optional `HANDLER handler_function` or `NO HANDLER` clause.
+ pub handler: Option<ForeignDataWrapperRoutineClause>,
+ /// Optional `VALIDATOR validator_function` or `NO VALIDATOR` clause.
+ pub validator: Option<ForeignDataWrapperRoutineClause>,
+ /// Optional `OPTIONS (key 'value', ...)` clause.
+ pub options: Option<Vec<CreateServerOption>>,
+}
+
+impl fmt::Display for CreateForeignDataWrapper {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(f, "CREATE FOREIGN DATA WRAPPER {}", self.name)?;
+ if let Some(handler) = &self.handler {
+ handler.fmt_with_label(f, "HANDLER")?;
+ }
+ if let Some(validator) = &self.validator {
+ validator.fmt_with_label(f, "VALIDATOR")?;
+ }
Review Comment:
You should render the clauses in the order they were parsed, which also makes every accepted statement round-trip unchanged.
```suggestion
for routine in &self.routines {
write!(f, " {routine}")?;
}
```
##########
tests/sqlparser_postgres.rs:
##########
@@ -10154,3 +10154,112 @@ fn parse_bitstring_literal_escaping() {
pg_and_generic().verified_stmt("SELECT B''''");
pg_and_generic().verified_stmt("SELECT B'it''s'");
}
+
+#[test]
+fn parse_create_foreign_data_wrapper() {
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(stmt.name.to_string(), "myfdw");
+ assert!(stmt.handler.is_none());
+ assert!(stmt.validator.is_none());
+ assert!(stmt.options.is_none());
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw HANDLER myhandler";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.handler,
+ Some(ForeignDataWrapperRoutineClause::Function(ObjectName::from(
+ vec!["myhandler".into()]
+ )))
+ );
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO HANDLER";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(stmt.handler, Some(ForeignDataWrapperRoutineClause::Absent));
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO VALIDATOR";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.validator,
+ Some(ForeignDataWrapperRoutineClause::Absent)
+ );
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw HANDLER myhandler VALIDATOR myvalidator OPTIONS (debug 'true')";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.options,
+ Some(vec![CreateServerOption {
+ key: "debug".into(),
+ value: Ident {
+ value: "true".to_string(),
+ quote_style: Some('\''),
+ span: Span::empty(),
+ },
+ }])
+ );
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO HANDLER NO VALIDATOR";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(stmt.handler, Some(ForeignDataWrapperRoutineClause::Absent));
+ assert_eq!(
+ stmt.validator,
+ Some(ForeignDataWrapperRoutineClause::Absent)
+ );
Review Comment:
You should assert on `routines` here.
```suggestion
assert!(stmt.routines.is_empty());
assert!(stmt.options.is_none());
let sql = "CREATE FOREIGN DATA WRAPPER myfdw HANDLER myhandler";
let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
unreachable!()
};
assert_eq!(
stmt.routines,
vec![ForeignDataWrapperRoutineClause::Handler(ObjectName::from(
vec!["myhandler".into()]
))]
);
let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO HANDLER";
let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
unreachable!()
};
assert_eq!(
stmt.routines,
vec![ForeignDataWrapperRoutineClause::NoHandler]
);
let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO VALIDATOR";
let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
unreachable!()
};
assert_eq!(
stmt.routines,
vec![ForeignDataWrapperRoutineClause::NoValidator]
);
let sql = "CREATE FOREIGN DATA WRAPPER myfdw HANDLER myhandler VALIDATOR myvalidator OPTIONS (debug 'true')";
let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
unreachable!()
};
assert_eq!(
stmt.options,
Some(vec![CreateServerOption {
key: "debug".into(),
value: Ident {
value: "true".to_string(),
quote_style: Some('\''),
span: Span::empty(),
},
}])
);
let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO HANDLER NO VALIDATOR";
let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
unreachable!()
};
assert_eq!(
stmt.routines,
vec![
ForeignDataWrapperRoutineClause::NoHandler,
ForeignDataWrapperRoutineClause::NoValidator
]
);
```
##########
tests/sqlparser_postgres.rs:
##########
@@ -10154,3 +10154,112 @@ fn parse_bitstring_literal_escaping() {
pg_and_generic().verified_stmt("SELECT B''''");
pg_and_generic().verified_stmt("SELECT B'it''s'");
}
+
+#[test]
+fn parse_create_foreign_data_wrapper() {
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(stmt.name.to_string(), "myfdw");
+ assert!(stmt.handler.is_none());
+ assert!(stmt.validator.is_none());
+ assert!(stmt.options.is_none());
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw HANDLER myhandler";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.handler,
+ Some(ForeignDataWrapperRoutineClause::Function(ObjectName::from(
+ vec!["myhandler".into()]
+ )))
+ );
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO HANDLER";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(stmt.handler, Some(ForeignDataWrapperRoutineClause::Absent));
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO VALIDATOR";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.validator,
+ Some(ForeignDataWrapperRoutineClause::Absent)
+ );
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw HANDLER myhandler VALIDATOR myvalidator OPTIONS (debug 'true')";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(
+ stmt.options,
+ Some(vec![CreateServerOption {
+ key: "debug".into(),
+ value: Ident {
+ value: "true".to_string(),
+ quote_style: Some('\''),
+ span: Span::empty(),
+ },
+ }])
+ );
+
+ let sql = "CREATE FOREIGN DATA WRAPPER myfdw NO HANDLER NO VALIDATOR";
+ let Statement::CreateForeignDataWrapper(stmt) = pg_and_generic().verified_stmt(sql) else {
+ unreachable!()
+ };
+ assert_eq!(stmt.handler, Some(ForeignDataWrapperRoutineClause::Absent));
+ assert_eq!(
+ stmt.validator,
+ Some(ForeignDataWrapperRoutineClause::Absent)
+ );
+
+ // A schema-qualified name is not valid: FDW names are bare identifiers.
+ assert!(matches!(
+ pg_and_generic().parse_sql_statements("CREATE FOREIGN DATA WRAPPER myschema.myfdw"),
+ Err(ParserError::ParserError(_))
+ ));
Review Comment:
You should pin the exact error, as #2628 does for the other PostgreSQL negative tests. `matches!(.., Err(ParserError::ParserError(_)))` still passes if the statement starts failing earlier for an unrelated reason.
```suggestion
assert_eq!(
pg_and_generic().parse_sql_statements("CREATE FOREIGN DATA WRAPPER myschema.myfdw"),
Err(ParserError::ParserError(
"Expected: end of statement, found: .".to_string()
))
);
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org