Skip to content

fix: read PostgreSQL ALTER TABLE actions without a dialect - #2958

Open
minleejae wants to merge 1 commit into
JSQLParser:masterfrom
minleejae:fix/alter-table-pg-actions-default-20261124
Open

minleejae wants to merge 1 commit into
JSQLParser:masterfrom
minleejae:fix/alter-table-pg-actions-default-20261124

Conversation

@minleejae

Copy link
Copy Markdown
Contributor

PostgreSQL's ALTER TABLE actions such as OWNER TO, SET (...), RESET (...), REPLICA IDENTITY, SET TABLESPACE, SET LOGGED | UNLOGGED, CLUSTER ON, [NO] INHERIT and ENABLE | DISABLE TRIGGER were structured only with Dialect.POSTGRESQL. Without a dialect they fell to the generic fallback, which keeps the action and every action after it as one UNSPECIFIC text. ALTER TABLE t OWNER TO bob, ADD COLUMN c int therefore gave no ADD expression, so callers that parse DDL from several databases without choosing a dialect lost the actions that followed. ALTER CONSTRAINT c [NO] INHERIT and ALTER COLUMN c {SET | RESET} (...) were parse errors without a dialect. VALIDATE CONSTRAINT and ENABLE | DISABLE RULE are already read in every dialect.

This change reads these actions with Dialect.POSTGRESQL and without a dialect, never with another dialect. They go through the existing PostgreSqlRelationAction production, so the result is the same RelationAlterAction as with Dialect.POSTGRESQL:

  • OWNER TO, SET (...), RESET (...), REPLICA IDENTITY, SET SCHEMA, SET TABLESPACE, SET ACCESS METHOD, SET WITHOUT CLUSTER | OIDS, SET LOGGED | UNLOGGED, CLUSTER ON, OF, NOT OF, INHERIT, NO INHERIT, ENABLE | DISABLE [ALWAYS | REPLICA] TRIGGER
  • ALTER CONSTRAINT name [NO] INHERIT, ALTER [COLUMN] c SET (...), ALTER [COLUMN] c RESET (...)

Without a dialect, three readings stay as they were:

  • ALTER COLUMN c SET STATISTICS | STORAGE | COMPRESSION and ALTER COLUMN c DROP EXPRESSION stay with the generic ALTER COLUMN branch (AlterOperation.ALTER), which already reads them.
  • Snowflake's CLUSTER BY (...) is not taken as CLUSTER ON.
  • SQL Server's trigger list ENABLE | DISABLE TRIGGER t1, t2 is not split into a PostgreSQL action and an unspecified remainder.

The decision lives in isPostgreSqlRelationActionAhead(), with a small isTriggerNameListAhead() lookahead for the SQL Server list.

Compatibility: without a dialect, statements that gave one UNSPECIFIC expression now give a RelationAlterAction followed by the expressions of the actions after it; toString() differs only in whitespace. The four column and constraint forms above change from parse errors to parsed statements, and PostgreSQL accepts all of them. Dialect.POSTGRESQL is unchanged, and MySQL, SQL Server and the other dialects keep the generic reading.

Reference: PostgreSQL 18 ALTER TABLE ("All the forms of ALTER TABLE that act on a single table, except RENAME, SET SCHEMA, ATTACH PARTITION, and DETACH PARTITION can be combined into a list of multiple alterations to apply together.")

Validation

  • PostgreSqlRelationAlterTest:
    • 27 actions, each followed by , ADD COLUMN z INT, parse without a dialect into the same RelationAlterAction kind and text as with Dialect.POSTGRESQL, keep the ADD expression and round-trip.
    • SET SCHEMA is structured without a dialect when it is the only action.
    • These 28 cases fail on the base (ba9291f1f); the cases below pass on both.
    • OWNER TO keeps the generic reading with Dialect.MYSQL and Dialect.SQLSERVER; CLUSTER BY (a) and SQL Server trigger lists keep it without a dialect; ALTER COLUMN ... SET STATISTICS | STORAGE | COMPRESSION and DROP EXPRESSION keep AlterOperation.ALTER.
  • PostgreSQL 18.6 accepted every action used in the tests combined with ADD COLUMN; the only errors were about missing objects (type, trigger, parent table) or the constraint type. SET SCHEMA gives a syntax error in a list, as documented, so it is tested alone. The parser still accepts it in a list, as it already did with Dialect.POSTGRESQL and, as one UNSPECIFIC text, without a dialect.
  • Java 17 gradle check (Spotless, Checkstyle, PMD, JaCoCo verification): 16,795 tests, 0 failures, 25 skipped.

AI assistance: prepared with Claude Code.

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

Copy link
Copy Markdown
Contributor

Please fix grammar ambiguities.

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.

2 participants