Skip to content

fix(plan): support enum and prefix text indexes - #24809

Merged
mergify[bot] merged 20 commits into
matrixorigin:4.0-devfrom
ck89119:issue-24740
Jun 17, 2026
Merged

mergify[bot] merged 20 commits into
matrixorigin:4.0-devfrom
ck89119:issue-24740

Conversation

@ck89119

@ck89119 ck89119 commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

Fixes #24740
Fixes #24729

What this PR does / why we need it:

  • Allows ENUM columns in secondary, unique, and composite indexes while keeping ENUM primary keys rejected.
  • Allows TEXT/BLOB prefix indexes and keeps plain TEXT/BLOB indexes rejected.
  • Persists index prefix lengths in index metadata and applies prefix expressions during index backfill, insert, unique dedup, and lock-key generation.
  • Adds focused unit coverage and BVT cases for ENUM indexes and TEXT/BLOB prefix indexes.

Validation:

  • go test ./pkg/sql/plan -run 'TestBuildIndexAllowsEnumAndTextBlobPrefix|TestBuildIndexRejectsTextBlobPlainIndex' -count=1
  • go test ./pkg/sql/compile -run 'TestGenInsertIndexTableSql_UsesPrefixExpression|TestBuildCreateUniqueIndexDuplicateCheckSQL' -count=1
  • go test ./pkg/sql/colexec/preinsertunique ./pkg/sql/colexec/preinsertsecondaryindex -count=1
  • ./run.sh -p /Users/LoveYY/Develop/matrixorigin/matrixone/.worktrees/issue-24740/test/distributed/cases/ddl/create_index.sql -g -n
  • ./run.sh -p /Users/LoveYY/Develop/matrixorigin/matrixone/.worktrees/issue-24740/test/distributed/cases/dtype/enum.sql -g -n
  • git diff --check
  • make
  • make static-check

@ck89119
ck89119 requested a review from a user June 3, 2026 08:52
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@matrix-meow matrix-meow added the size/XL Denotes a PR that changes [1000, 1999] lines label Jun 12, 2026

@XuPeng-SH XuPeng-SH 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.

I found a substantive issue in the current prefix-index implementation.

Prefix length is persisted in metadata, but SHOW CREATE TABLE still drops it, so the feature does not round-trip.

  • Prefix lengths are stored in indexDef.IndexAlgoParams via prefix_lengths (pkg/catalog/secondary_index_utils.go:187-203, pkg/sql/plan/build_ddl.go:2076-2079,2456-2459).
  • But when SHOW CREATE TABLE renders index definitions, it only prints raw part names and then appends catalog.IndexParamsToStringList(...) (pkg/sql/plan/build_show_util.go:278-303). That renderer never reconstructs key-part suffixes like (100), and IndexParamsToStringList does not output prefix_lengths either (pkg/catalog/secondary_index_utils.go:104-160).

So a table created as INDEX x3(a(100)) will still be shown as if it were just INDEX x3(a). That is not just cosmetic: SHOW CREATE TABLE no longer reproduces the original schema, and MatrixOne's snapshot/PITR/restore flows fetch DDL through show create table (pkg/frontend/snapshot_restore_with_ts.go:252-268, pkg/frontend/pitr.go:1583-1592, pkg/frontend/snapshot.go:2072-2080). In other words, a restored table can silently lose its prefix-index semantics or even fail when replaying the downgraded DDL on TEXT/BLOB keys.

I think this needs to be fixed before approval: SHOW CREATE TABLE / restoreDDL must reconstruct key-part lengths from prefix_lengths and emit col(length) again.

@XuPeng-SH XuPeng-SH 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.

Requesting changes for a prefix-index maintenance bug on the delete/update path.

The backfill / insert / unique-lock paths were updated to materialize prefix index keys with substring(..., 1, n), but appendDeleteIndexTablePlan in build_dml_util.go still builds the lookup key from the full source column value (or serial(...) of full values).

That means for any indexed value longer than the prefix length, DELETE / UPDATE will fail to match the old hidden index row. On secondary indexes that leaves stale index rows behind; on unique prefix indexes it can later surface as false duplicate conflicts.

I think the delete-maintenance path needs to apply the same prefix expression/cast as the insert/backfill path before joining to the index table, and it would be good to add an end-to-end UPDATE/DELETE case with values longer than the prefix length.

aunjgr

This comment was marked as low quality.

@XuPeng-SH XuPeng-SH 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.

✅ APPROVED after comprehensive multi-angle review

Analysis Summary: 2576 lines across 25 files • 21-item systematic checklist • Zero blocking issues

Key Strengths:

  1. ✅ Excellent refactoring - centralized validation in checkIndexColumnSupportability()
  2. ✅ Comprehensive test coverage - 87 planner + 64 BVT lines covering happy/unhappy paths
  3. ✅ First-principles correctness - substring/cast applied at all DML entry points (INSERT/UPDATE/DELETE)
  4. ✅ Robust error handling - validates prefix lengths at parse/serialization/execution
  5. ✅ Backward compatible - existing indexes continue working, no migration required
  6. ✅ Critical bug fix - preinsertunique.go now uses PreInsertCtx.UkType (prevents type mismatches)

Unhappy Path Validation:

  • ✅ Zero/negative prefix lengths → rejected at parse time
  • ✅ Prefix > data length → substring() handles gracefully
  • ✅ NULL values → filtered in index maintenance
  • ✅ Mixed prefix/non-prefix columns → each checked independently
  • ✅ Upgrade scenario → (nil, nil) return for old metadata
  • ✅ GEOMETRY32 → RTREE validation preserved

First-Principles Check:

  • Core design: PREFIX INDEX stores substring(col, 1, len) instead of full TEXT/BLOB
  • Type safety: TEXT→VARCHAR(65535), BLOB→VARBINARY(65535) with proper casts
  • Metadata: JSON extensible format in IndexAlgoParams
  • DML completeness: 12 function usages across INSERT/DELETE/UPDATE paths

See full review document: /tmp/pr_review_24809.md

This PR is production-ready and represents a significant feature addition with excellent quality.

ck89119 and others added 2 commits June 17, 2026 14:46
Add unit tests for the prefix-length param helpers in catalog and the
pure index helpers in build_index_util, raising their coverage to 100%.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@mergify

mergify Bot commented Jun 17, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-06-17 09:37 UTC · Rule: release-4.0
  • ✅ Checks passed · in-place
  • ✅ Merged — 2026-06-17 11:33 UTC · at 616355d08eb35d56e5f0a25e0effe4a7709ee6c0 · squash

This pull request spent 1 hour 56 minutes 3 seconds in the queue, including 1 hour 5 minutes 57 seconds running CI.

Required conditions to merge
  • #approved-reviews-by >= 1 [🛡 GitHub branch protection]
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-decision = APPROVED [🛡 GitHub branch protection]
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-neutral = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
    • check-skipped = Matrixone Standlone CI / Multi-CN e2e BVT Test on Linux/x64(LAUNCH, PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / SCA Test on Ubuntu/x86
    • check-neutral = Matrixone CI / SCA Test on Ubuntu/x86
    • check-skipped = Matrixone CI / SCA Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH)
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(Optimistic/PUSH)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH,Optimistic)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
    • check-neutral = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
    • check-skipped = Matrixone Upgrade CI / Compatibility Test With Target on Linux/x64(LAUNCH)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Utils CI / Coverage
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86

This branch was previously deployed

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

Labels

kind/bug Something isn't working kind/test-ci size/XXL Denotes a PR that changes 2000+ lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants