Skip to content

Support MySQL index prefix a(N) in UNIQUE and CREATE INDEX to close #406 - #2606

Merged
mathiasrw merged 2 commits into
developfrom
fix/406
Oct 19, 2026
Merged

mathiasrw merged 2 commits into
developfrom
fix/406

Conversation

@mathiasrw

@mathiasrw mathiasrw commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

Closes #406

MySQL's index prefix a(N) (index only the first N characters of column a) now works in UNIQUE, PRIMARY KEY and INDEX constraints in CREATE TABLE, and in CREATE [UNIQUE] INDEX.

The parse error from the issue was already gone on develop: the column list of UNIQUE and CREATE INDEX is a list of expressions, so a(1) parsed as a call to a function a. CREATE TABLE then succeeded, and every INSERT failed.

Query develop This PR
CREATE TABLE t7(a TEXT, UNIQUE (a(1))) 1 1
then INSERT INTO t7 VALUES ('ab') throws alasql.fn.a is not a function 1
then INSERT INTO t7 VALUES ('ac') throws alasql.fn.a is not a function throws Cannot insert record, because it already exists in unique index
CREATE UNIQUE INDEX ux ON t (a(2)), then INSERT throws alasql.fn.a is not a function prefix of 2 characters is unique
CREATE TABLE t(a TEXT, PRIMARY KEY (a(2))) parse error prefix of 2 characters is the key
CREATE TABLE t(a TEXT, INDEX k (a(1))) parse error 1

Change

  • UNIQUE (...) and CREATE INDEX take expressions, so a(N) parses as a function call. When a is a column of the table and N a number, it now compiles to the first N characters of a (String(value).substr(0, N), NULL stays NULL). Any other expression compiles as before, so UNIQUE (UPPER(a)) and UNIQUE (MID(a, 1, 2)) still work.
  • PRIMARY KEY (...) and INDEX name (...) take a plain column list. The grammar now accepts a(N) there (IndexColsList), and the primary key uses the prefix. The grammar has the same 282 conflicts as develop.

Files: src/60createtable.js (columnPrefixToJS, indexColumnToJS), src/65createindex.js, src/alasqlparser.jison (and the generated src/alasqlparser.js).

Tests

test/test406-B.js (test406.js exists): the query from the issue, several columns with and without a prefix, UNIQUE KEY name (...), CREATE UNIQUE INDEX and CREATE INDEX, NULL and number values, functions in UNIQUE, PRIMARY KEY with a prefix, and INDEX in CREATE TABLE with a prefix.

Full suite: 2259 pass, 213 skip, 0 fail.

Replaces #2536, which has no code changes.

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.

Parse error CREATE TABLE UNIQUE (a(1))

1 participant