Skip to content

fix: read PostgreSQL NOT NULL and USING INDEX table constraints without a dialect - #2942

Open
minleejae wants to merge 1 commit into
JSQLParser:masterfrom
minleejae:fix/alter-add-pg-constraints-default-20261119
Open

minleejae wants to merge 1 commit into
JSQLParser:masterfrom
minleejae:fix/alter-add-pg-constraints-default-20261119

Conversation

@minleejae

Copy link
Copy Markdown
Contributor

PostgreSQL's table-level [CONSTRAINT name] NOT NULL column and [CONSTRAINT name] {UNIQUE | PRIMARY KEY} USING INDEX index constraints were read only with Dialect.POSTGRESQL. Without a dialect, ALTER TABLE t ADD CONSTRAINT nn NOT NULL id and ALTER TABLE t ADD CONSTRAINT uk UNIQUE USING INDEX t_idx were parse errors, while the other PostgreSQL constraint forms (NOT VALID, EXCLUDE, DEFERRABLE INITIALLY DEFERRED, VALIDATE CONSTRAINT) have parsed without a dialect since #2847. Callers that parse DDL from several databases without choosing a dialect had to special-case these two.

This change reads both forms with Dialect.POSTGRESQL and without a dialect, never with another dialect:

  • ALTER TABLE ... ADD [CONSTRAINT name] NOT NULL column [NO INHERIT] [NOT VALID] and the same table constraint in CREATE TABLE produce a NotNullConstraint.
  • ALTER TABLE ... ADD [CONSTRAINT name] {UNIQUE | PRIMARY KEY} USING INDEX index [attributes] produces a ConstraintUsingIndex.

Both reuse the existing PostgreSqlNotNullConstraint and PostgreSqlConstraintUsingIndex productions, so the result is the same object model as with Dialect.POSTGRESQL. The lookahead for the NOT NULL form lives in the existing nested AlterConstraintLookahead class.

Compatibility: only statements that were parse errors without a dialect are affected. Statements that already parsed keep their representation (ADD CONSTRAINT pk PRIMARY KEY (id) USING INDEX pk, ADD UNIQUE USING BTREE (c), ADD c INT NOT NULL). MySQL, Oracle and the other dialects still reject both forms. The test postgreSqlOnlyFormsStillRequireThePostgreSqlDialect added in #2847 pinned the old behaviour and is replaced.

Reference: PostgreSQL 18 ALTER TABLE (ADD table_constraint [ NOT VALID ], ADD table_constraint_using_index), CREATE TABLE (table_constraint: NOT NULL column_name [ NO INHERIT ]).

Validation

  • AlterAddTableConstraintTest: the two forms and their variants (unnamed, NO INHERIT, NOT VALID, PRIMARY KEY USING INDEX, several actions in one statement) parse without a dialect, round-trip, and give the same constraint classes as with Dialect.POSTGRESQL; CREATE TABLE with a table-level NOT NULL; both forms are still rejected with Dialect.MYSQL and Dialect.ORACLE. These statements fail on the base (ed17b37f4).
  • PostgreSQL 18.6 accepted every form used in the tests (ADD CONSTRAINT n NOT NULL c, ADD NOT NULL c, ... NO INHERIT, ... NOT VALID, ADD CONSTRAINT n UNIQUE USING INDEX i, ADD PRIMARY KEY USING INDEX i, CREATE TABLE t (id int, CONSTRAINT n NOT NULL id NO INHERIT)).
  • Java 17 gradle check (Spotless, Checkstyle, PMD, JaCoCo verification): 16,654 tests, 0 failures, 25 skipped.

AI assistance: prepared with Claude Code.

…ut a dialect

Signed-off-by: minleejae <mmj9808@gmail.com>

This branch has not been deployed

No deployments
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.

1 participant