Skip to content

Allow foreign table constraint without columns #1608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 19, 2024

Conversation

ramnivas
Copy link
Contributor

@ramnivas ramnivas commented Dec 18, 2024

Postgres allows skipping the (referenced-columns) part in ALTER TABLE ... FOREIGN KEY .. REFERENCES <tablename> (referenced-columns) per https://www.postgresql.org/docs/current/sql-createtable.html#SQL-CREATETABLE-PARMS-REFERENCES.

Since the generic SQL grammar requires referenced columns (https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#referential-constraint-definition), this change applies only to Postgres.

Postgres makes skipping the `(referenced-columns)` part in `ALTER TABLE ... FOREIGN KEY .. REFERENCES <tablename> (referenced-columns)` per https://www.postgresql.org/docs/current/sql-createtable.html#SQL-CREATETABLE-PARMS-REFERENCES.

Since the generic SQL grammar requires referenced columns (https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#referential-constraint-definition), this change applies only to Postgres.
@ramnivas
Copy link
Contributor Author

An alternative design would be to add a new method in the Dialect trait with a default implementation and have PostgreSqlDialect override it. But, I see dialect_of!(self is ... usages , so I chose that. Let me know if I should implement that alternative.

let referred_columns = self.parse_parenthesized_column_list(Mandatory, false)?;
// PostgreSQL allows foreign key columns to be optional
// (https://www.postgresql.org/docs/current/sql-createtable.html#SQL-CREATETABLE-PARMS-REFERENCES)
let parenthesized_column_list_optional = if dialect_of!(self is PostgreSqlDialect) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this behavior is a superset of the previous I think we can do without the dialect check and always set to Optional?

Also could we add a test case to sqlparse_common.rs demonstrating the new behavior?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Made the change.

Also, fixes DDL generation when reference columns aren't specified and adds a test.
ramnivas added a commit to exograph/exograph that referenced this pull request Dec 19, 2024
Our migration tests have used strings in Rust files (for exo and expected SQL), which required string escaping, prefix stripping, and other modifications. This made it difficult to read and extend the test code (for example, consider `MigrationScope`s). Also, we relied on exact string matches, which required paying too much attention to the precise format.

This refactoring:
1. Move .exo and .sql files outside of rust code
2. Use `sqlparser` to match without considering the format

Note that in some cases, `sqlparser` doesn't parse valid syntax, so we rely on exact string comparison as a fallback/optimization. We have already contributed a couple of changes (apache/datafusion-sqlparser-rs#1608, apache/datafusion-sqlparser-rs#1610) and will continue to do so to remove this reliance.
Copy link
Contributor

@iffyio iffyio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks @ramnivas!
cc @alamb

@iffyio iffyio merged commit 6523dab into apache:main Dec 19, 2024
8 checks passed
@alamb
Copy link
Contributor

alamb commented Dec 19, 2024

Thank you for keeping up the review train @iffyio -- being part of the unsung heros of the Rust data ecosystem 🦾

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants