Skip to content

Index entries: whitespace-separated fields form one composite index, but the docs don't define this #1008

Closed

Description

@kojifukada

Summary

In an indexes block, the docs only show two kinds of entry: a single field (booking_date) and a parenthesized, comma-separated tuple ((country, booking_date)). The parser also accepts several fields on one line separated by spaces or tabs, and turns them into one composite index. This grammar is not described anywhere in the docs, so a reader can't tell whether a b means one index or two.

Could you clarify the intended grammar of an index entry, and document it?

Environment

  • @dbml/core 10.3.1 / @dbml/parse 10.3.1 (the latest stable releases at the time of checking), Node v26.8.1. Checked on 2026-10-17.
  • The same results with @dbml/core 8.3.1 and 10.1.1.
  • At the time of checking, packages/dbml-parse/src/core/parser/parser.ts and .../global_modules/indexes/interpret.ts on master were the same files as in v10.3.1.

Repro

// npm install --save-exact @dbml/[email protected] @dbml/[email protected] && node repro.cjs
const { Parser, ModelExporter } = require('@dbml/core');

const bodies = [
  'a b',
  '(a) (b)',
  'a (b)',
  '(a, b) (c)',
  '(a, b)(c)',
  '(a)\n    (b)',
  '(a, b)',
];

for (const body of bodies) {
  const src = `Table t {\n  a int\n  b int\n  c int\n  indexes {\n    ${body}\n  }\n}\n`;
  const db = new Parser().parse(src, 'dbmlv2');
  const cols = db.schemas[0].tables[0].indexes.map((ix) => ix.columns.map((c) => c.value));
  const ddl = ModelExporter.export(db, 'postgres', false)
    .split('\n').filter((l) => l.startsWith('CREATE INDEX'));
  console.log(JSON.stringify(body), '=>', JSON.stringify(cols));
  for (const l of ddl) console.log('   ', l);
}

Output:

"a b" => [["a","b"]]
    CREATE INDEX ON "t" ("a", "b");
"(a) (b)" => [["b","a"]]
    CREATE INDEX ON "t" ("b", "a");
"a (b)" => [["b","a"]]
    CREATE INDEX ON "t" ("b", "a");
"(a, b) (c)" => [["c","a","b"]]
    CREATE INDEX ON "t" ("c", "a", "b");
"(a, b)(c)" => [["c","a","b"]]
    CREATE INDEX ON "t" ("c", "a", "b");
"(a)\n    (b)" => [["a"],["b"]]
    CREATE INDEX ON "t" ("a");
    CREATE INDEX ON "t" ("b");
"(a, b)" => [["a","b"]]
    CREATE INDEX ON "t" ("a", "b");

More results from the same harness, on the same table:

indexes body Result
a ⏎ b 2 indexes: [a], [b]
a, TAB, b 1 index: [a, b]
a b c 1 index: [a, b, c]
(a) b 1 index: [a, b]
a b [unique] 1 unique index: [a, b]. The setting applies to the whole entry
(a, ⏎ b) 1 index: [a, b]
a, b Error: An index field must be an identifier, a quoted identifier, a functional expression or a tuple of such
(a b) Error: Expect a comma ','

What the docs say

Index Definition gives examples but no grammar. Every multi-field example is a parenthesized, comma-separated tuple, and every entry is on its own line:

indexes {
  (id, country) [pk] // composite primary key
  created_at [name: 'created_at_index', note: 'Date']
  booking_date
  (country, booking_date) [unique]
  booking_date [type: hash]
  (`id*2`)
  (`id*3`,`getdate()`)
  (`id*3`,id)
}

The examples don't show what a b or (a) (b) on one line should mean. They could be one composite index, two indexes, or an error.

What the implementation appears to do

This is our reading of the source and the results above. Please correct us if any of it is wrong.

  1. Outside parentheses, an entry ends at a newline. (a, ⏎ b) is still one entry. A ( at the start of a line begins a new entry, which is why (a) ⏎ (b) gives two indexes.
  2. Within one line, fields separated by spaces or tabs become the arguments of one FunctionApplicationNode. interpret.ts collects the callee and all arguments into the columns of a single index.
  3. A field written directly before (…), as in a (b) or (a) (b), is parsed as a CallExpressionNode. The flatten step then reverses the field order. For the related reorder of expression fields, see Index columns change order when a functional expression comes after a column #1005.
  4. In the cases we tried, (a, b) and (a, ⏎ b) are accepted. a, b (a comma outside parentheses) and (a b) (no comma inside parentheses) are rejected.

The comment in interpret.ts (// Flatten call expressions like (id, name)(age, weight)) suggests that at least the juxtaposed-tuple form is intended.

Why this matters

Requests

  1. Clarify the intended grammar of an index entry. Several fields on one line, separated by spaces or tabs, could mean one composite index, separate indexes, or an error. We leave that choice to you, and would like the docs to state it.
  2. Whichever reading you choose, it would help if the docs also stated:
    • what ends an entry (a newline?)
    • how bare fields and tuples combine on one line ((a, b) c, a (b, c), (a) (b))
    • which fields a trailing [...] setting applies to
    • where a comma is allowed
  3. If the current composite reading is kept, the field order should follow the source order (see Index columns change order when a functional expression comes after a column #1005).

Activity

  1. huydo862003 commented on Oct 19, 2026

    @huydo862003
    Contributor

    Hi @kojifukada, thank you for your report.

    Regarding the out-of-order index column issue, we'll release a fix in the next few days.

    Regarding the behavior where index entries separated by whitespaces form a composite one, it is indeed an oversight. Since we don't specify this syntax in the document, we suggest you follow the documented syntax: Always separate different indexes with a newline. Composite indexes are enclosed in () like python tuples.

    This legacy, ambiguous syntax shall be deprecated in the near future.

  2. kojifukada commented on Oct 19, 2026

    @kojifukada
    Author

    Thanks for the clarification, @huydo862003, and for the upcoming fix!

    This is very helpful for us as a downstream consumer. We'll follow the documented syntax.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions