Skip to content

fix(deparser): emit CREATE INDEX trailing clauses in grammar order - #336

Merged
pyramation merged 1 commit into
mainfrom
fix/index-clause-ordering
Aug 2, 2026
Merged

fix(deparser): emit CREATE INDEX trailing clauses in grammar order#336
pyramation merged 1 commit into
mainfrom
fix/index-clause-ordering

Conversation

@pyramation

Copy link
Copy Markdown
Collaborator

Summary

IndexStmt emitted trailing clauses as ( params ) → INCLUDE → WHERE → WITH → NULLS NOT DISTINCT → TABLESPACE, which the PG grammar rejects: any partial index with NULLS NOT DISTINCT, or any index with both WHERE and WITH/TABLESPACE, deparsed into SQL that fails to reparse (syntax error at or near "NULLS"). This broke round-trip validation in @pgsql/transform, and with it generate:constructive in constructive-db (constructive-planning#1382).

Reordered to the grammar's order — nulls_not_distinct right after the column/INCLUDE list, whereClause last:

   INCLUDE (...)
-  whereClause → WITH → nulls_not_distinct → TABLESPACE
+  nulls_not_distinct → WITH → TABLESPACE → whereClause

Before / after for the real-world shape:

-- in
CREATE UNIQUE INDEX i ON t (a, b) NULLS NOT DISTINCT WHERE c IS NULL;
-- before: CREATE UNIQUE INDEX i ON t (a, b) WHERE c IS NULL NULLS NOT DISTINCT;  -- unparseable
-- after:  CREATE UNIQUE INDEX i ON t (a, b) NULLS NOT DISTINCT WHERE c IS NULL;

New round-trip fixtures in __fixtures__/kitchen-sink/misc/index-clause-ordering.sql (regenerated via npm run kitchen-sink) cover every clause combination — NULLS NOT DISTINCT alone, with WHERE, with INCLUDE/WITH/TABLESPACE, the WHERE-after-WITH case without NULLS, and the constraint path (UNIQUE NULLS NOT DISTINCT (a, b)) which was already correct.

Link to Devin session: https://app.devin.ai/sessions/6e8bd0b058a944eeba4a3dbad12d8487
Requested by: @pyramation

NULLS NOT DISTINCT now follows the column/INCLUDE list and WHERE moves
after WITH/TABLESPACE, so partial unique indexes survive round-trip.
@pyramation pyramation self-assigned this Aug 2, 2026
@devin-ai-integration

Copy link
Copy Markdown
Contributor

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

@pyramation
pyramation merged commit 90b753d into main Aug 2, 2026
15 checks passed
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