Skip to content

Support CREATE INDEX with functional index parts#753

Merged
morgo merged 2 commits into
block:mainfrom
morgo:fix-functional-index-create
May 1, 2026
Merged

Support CREATE INDEX with functional index parts#753
morgo merged 2 commits into
block:mainfrom
morgo:fix-functional-index-create

Conversation

@morgo
Copy link
Copy Markdown
Collaborator

@morgo morgo commented May 1, 2026

Summary

  • CREATE INDEX idx ON t1 ((a IS NULL)) was rejected with "cannot convert functional index to ALTER TABLE statement". The TiDB parser already exposes the expression via IndexPartSpecification.Expr, so the rewrite can handle it.
  • Replaced the manual column-name building loop in convertCreateIndexToAlterTable with IndexPartSpecification.Restore(), which uniformly handles plain columns (with prefix lengths) and (expr) parts.

Fixes #444.

Test plan

  • go test ./pkg/statement/ passes, including a new case for CREATE INDEX idx ON t1 ((a IS NULL)) and a mixed (a, (LOWER(b))) case.
  • Existing CREATE INDEX rewrite tests (plain columns, prefix lengths, UNIQUE, quoted identifiers) still pass unchanged.

🤖 Generated with Claude Code

Previously CREATE INDEX with a functional index (e.g. `((`a` IS NULL))`)
was rejected. The parser already exposes the expression via
IndexPartSpecification.Expr, so use IndexPartSpecification.Restore()
which handles both column refs (with prefix lengths) and expressions.

Fixes block#444.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR extends the CREATE INDEXALTER TABLE ... ADD INDEX rewrite so functional index parts (expression-based key parts) are supported, leveraging TiDB parser’s IndexPartSpecification.Expr and Restore().

Changes:

  • Rewrite convertCreateIndexToAlterTable to build index parts via IndexPartSpecification.Restore() (supports both column parts with prefix lengths and (expr) parts).
  • Add/adjust statement parsing tests to validate functional index and mixed functional + column index rewrites.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
pkg/statement/statement.go Uses TiDB AST restore to serialize index parts, enabling functional index support in CREATE INDEX rewrite.
pkg/statement/statement_test.go Updates coverage to assert successful rewrite for functional and mixed functional/plain index parts.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@morgo morgo merged commit d810ac6 into block:main May 1, 2026
12 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.

Spirit does not support CREATE INDEX statement with a functional index

3 participants