Skip to content

feat: support descending index columns with (col [desc]) - #1018

Merged
huydo862003 merged 5 commits into
holistics:masterfrom
Roshan1299:feat/172-index-desc
Oct 26, 2026
Merged

huydo862003 merged 5 commits into
holistics:masterfrom
Roshan1299:feat/172-index-desc

Conversation

@Roshan1299

@Roshan1299 Roshan1299 commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Index columns can now have a sort order, written as a per-column setting inside the parentheses, the syntax agreed in this PR:

Table orders {
  id integer [pk]
  customer_id integer
  status varchar
  created_at timestamp

  indexes {
    (created_at [desc])
    (customer_id [asc], created_at [desc]) [name: 'orders_customer_recent']
    (`lower(status)` [desc])
  }
}

The PostgreSQL exporter turns that into:

CREATE INDEX ON "orders" ("created_at" DESC);
CREATE INDEX "orders_customer_recent" ON "orders" ("customer_id", "created_at" DESC);
CREATE INDEX ON "orders" ((lower(status)) DESC);

The MySQL, SQL Server and Oracle exporters add DESC the same way, the DBML exporter writes (col [desc]) back, and the PostgreSQL, MySQL, SQL Server and Oracle importers read DESC into the new field. [asc] is accepted and means the default, so it isn't stored and isn't written back. Columns without a sort order are unchanged, so existing output stays the same.

A column with a sort order goes inside parentheses even on its own: created_at [desc] outside them is the index's own settings list, as before, and gives "Unknown index setting 'desc'".

A primary key index with a descending column, like (id, status [desc]) [pk], is an error ("A primary key index field cannot be descending"), since PostgreSQL and Oracle don't take a sort order in a PRIMARY KEY constraint. For the same reason, an imported PRIMARY KEY (a DESC) keeps its columns but drops the sort order.

Issue

Closes #172

Lasting Changes (Technical)

  • Parser: a [ after a space is now array indexing when it sits directly inside ( or [ (the innermost context), as discussed in feat: support descending index columns with (col [desc]) #1018, so (a [desc], b) and (a[desc], b) both parse. Outside them, a space before [ still starts a settings list (name varchar [not null], a [unique]).
  • Index columns in the parser output and in the @dbml/core model have an optional sortOrder: 'desc'. It's left out otherwise, so the JSON for existing schemas doesn't change.
  • destructureIndexNode returns { name, descending } entries. New helper getIndexColumnSortOrder in utils/validate.ts reads [asc] or [desc] (any case, one setting, no value). Anything else in that position, like (a [unique]), gives "An index column setting must be [asc] or [desc]".
  • The index binder takes the sort order off before looking up the column, so (missing [desc]) still reports the missing column. That change stays in indexes/bind.ts; the shared scanNonListNodeForBinding is untouched.
  • PostgreSQL importer: an index expression's value used to come from ctx.getText() on the whole element, which also took in any options after it (lower(status)DESC). It now takes only the expression. That changes the imported text only where an index expression was followed by an option.
  • parser/mysql-parse/output/expression_indexes.out.json: idx_year_desc now has "sortOrder": "desc".
  • New index_sort_order fixtures for the 4 SQL exporters and 4 SQL importers, interpreter tests (sort orders with and without the space, [asc], the primary key error and an ascending primary key, bad settings, a [desc] outside parentheses, -a, a missing column) and a parser test for the space inside ( and [.
  • Docs: an "Index Column Sort Order" section in docs.md, and llms.txt mentions it.

Checklist

Please check directly on the box once each of these are done

  • Documentation (if necessary)
  • Updated dbml-homepage/static/llms.txt (if docs/features changed)
  • Lint Checks Passed
  • Unit Tests Passed
  • Coverage Tests Passed
  • Integration Tests Passed
  • Code Review

Tests: dbml-parse 2132 and dbml-core 430 pass. With the source files at master, 5 of the 7 new interpreter tests and the parser test fail; the other two (a [desc] outside parentheses, -a) pass on both, since they guard what must not change. All 9 new or updated core fixtures fail with the core source at master. ESLint and tsc --noEmit are clean on the changed files apart from an unused import already on master.

The random-input property tests (__tests__/properties, __tests__/fuzz) failed 3 times in about 12 full dbml-parse runs on this branch, each time a different test (should not produce valid elements for garbage input, should return valid result structure on malformed input, should roundtrip with valid dbml injected with random strings), and none in 7 full runs with the source at master. None of them failed again in over 55 runs of those files alone, and the garbage-input one generates no [ at all; parse-then-print round-trips (a [desc], b), (a [desc] , b [ asc ]) and broken input like (a [ (b [c exactly. I couldn't pin a cause, so I'm noting it in case you see it in CI.

The MySQL importer fixture writes its expression as ((lower(status)) DESC) without backticks, because a backtick inside an index expression already breaks the DBML output on master.

Mark an index column or expression descending with -col. The parser rejects it in a pk index, the SQL exporters write DESC, the DBML exporter writes the - back, and the PostgreSQL, MySQL, SQL Server and Oracle importers read DESC.
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review 🔄 Running since 2026-10-22T06:37:41.324598Z 4708dbb PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@huydo862003

Copy link
Copy Markdown
Contributor

Hi @Roshan1299, thank you for your PR.

I've just discussed with the team and we've came up with a better syntax, which introduces some changes.

We're considering supporting a per-field setting:

(first_field [desc], second_field)

I think this is more extensible in case we support more settings. And fortunately, our parser already parses this as an array indexing.

So unfortunately, you may need to rework the PR a bit 🐱, or if you will, I can take it from here 😄

@Roshan1299

Copy link
Copy Markdown
Contributor Author

Sounds good, I'll rework it, most of the PR (model, exporters, importers) stays the same.

One thing I noticed trying it on the parser: it only parses as array indexing without the space. (a[desc], b) and a[desc] give an ArrayNode, but (a [desc], b) is a parse error ("Expect a comma ','"), and on a single column a [desc] is read as the index's own settings. Should I go with the no-space form, or do you want (a [desc], b) with the space to work too (that needs a parser change)?

Also, should [asc] be accepted as well, or only [desc]?

@huydo862003

huydo862003 commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Sounds good, I'll rework it, most of the PR (model, exporters, importers) stays the same.

One thing I noticed trying it on the parser: it only parses as array indexing without the space. (a[desc], b) and a[desc] give an ArrayNode, but (a [desc], b) is a parse error ("Expect a comma ','"), and on a single column a [desc] is read as the index's own settings. Should I go with the no-space form, or do you want (a [desc], b) with the space to work too (that needs a parser change)?

Also, should [asc] be accepted as well, or only [desc]?

I think we can modify the parser to allow space when inside (), [], much like how you did with disallowing expressions spanning multilines in {}, in ()and[]` there is no ambiguity so it's fine to allow space.

asc can be allowed I think 👀

You can also test the parser behavior more easily with: https://dbml.dbdiagram.io/playground/. It has an AST viewer mode.

As agreed in holistics#1018: a sort order is a per-column setting, `(a [desc], b)`,
and `[asc]` is accepted too. The parser now reads a `[` after a space as
indexing when it sits directly inside `(` or `[`, where no settings list
can follow an expression.
@Roshan1299 Roshan1299 changed the title feat: support descending index columns with a - prefix feat: support descending index columns with (col [desc]) Oct 24, 2026
@Roshan1299

Copy link
Copy Markdown
Contributor Author

Reworked to the new syntax: (a [desc], b), with [asc] accepted too (it's the default, so it isn't stored). The parser now treats a [ after a space as indexing when it's directly inside ( or [, and -col is gone. A single column still needs the parentheses, since a [desc] on its own is the index settings list. I updated the description with the details.

@huydo862003 huydo862003 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you for your contribution @Roshan1299, it would be nice if you can support DESC inside dbml-connector too 🐱

@huydo862003
huydo862003 merged commit 6674c48 into holistics:master Oct 26, 2026
4 of 7 checks passed
@huydo862003 huydo862003 added the PR: New Feature 🚀 A type of pull request used for changelog categories label Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: New Feature 🚀 A type of pull request used for changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request - Descending Fields in Indexing

2 participants