Repository navigation
fix: harden regular prefix index correctness - #26829
Conversation
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
XuPeng-SH
left a comment
There was a problem hiding this comment.
Requesting changes for two P1 prefix-index correctness blockers.
-
[P1] Gate
prefix_lengths_v2on the cluster protocol version.For delimiter-bearing names,
SetIndexPrefixLengthsInParamMapdeletesprefix_lengthsand emits onlyprefix_lengths_v2(code). A base-version CN reads onlyprefix_lengths(base reader), so during a rolling upgrade old and new CNs interpret the same index differently. They can write full and truncated keys into the same hidden table, violating unique-prefix semantics and making lookups miss rows. Since the legacy format cannot encode these names losslessly, please reject the affected CREATE/ALTER operations until all CNs advertise v2 support, and add a mixed-version test. -
[P1] Apply prefix-index safety to non-equality predicates.
Prefix transformation is applied only by
replaceEqualCondition(code).getIndexForNonEquiCondcan still select the prefix index forIN,BETWEEN, and range predicates, whilereplaceNonEqualConditioncompares full constants against truncated stored keys (code). Forindex i(a(4)), a stored valueabcdxhas keyabcd, soWHERE a IN ('abcdx')can discard the row before residual evaluation. Please conservatively disable these index joins for prefix indexes until each predicate is safely widened, and add forced-index versus ignore-index coverage.
Independent verification passed: focused regression tests, go test ./pkg/catalog ./pkg/sql/plan, go vet ./pkg/catalog ./pkg/sql/plan, and git diff --check.
XuPeng-SH
left a comment
There was a problem hiding this comment.
I found two correctness gaps that still allow silent false negatives in the prefix-index access path.
-
[P1] Apply prefix-key candidate semantics to non-equality lookups as well (
pkg/sql/plan/apply_indices.go:1854-1904)This PR truncates equality RHS values through
makeIndexLookupPartExpr, butgetIndexForNonEquiCondcan still select the same prefix index forIN,BETWEEN,in_range, and scalar ranges, whilereplaceNonEqualConditionandreplaceRangePairConditioncompare the truncated hidden key with the original full value. A concrete witness isUNIQUE INDEX uq_a(a(4))with rowa='abcdx':... FORCE INDEX(uq_a) WHERE a IN ('abcdx')probes storedabcdwith fullabcdxand can return no row. Range boundaries have the same problem; e.g. values sharingabcdcan be dropped around a strict bound.Please either disable prefix indexes for these predicate classes, or transform every lookup value to its persisted prefix and conservatively widen strict hidden-key bounds (
>->>=,<-><=) while retaining the original base-table residual. Add default/FORCE/IGNORE coverage for IN and range boundaries, same-prefix neighbors, prepared values, Unicode, and post-flush reads. -
[P1] Do not fail open to a full-value probe when prefix metadata is invalid or stale (
pkg/sql/plan/apply_indices.go:1809-1819)On metadata parse or expression-binding failure,
makeIndexLookupPartExprreturns the unmodified full input and the caller still builds the index scan. If the hidden table physically contains a prefix, probingabcdwithabcdxloses the candidate before the correct base residual can run. This is particularly relevant to persisted stale metadata from the pre-fix RENAME bug: a valid prefix entry whose key no longer appears inIndexDef.Partsalso silently takes this path.tryIndexOnlyScanalready fails closed on parse errors; the index-join path needs the same invariant. Please propagate eligibility/error and skip or reject the index, and validate metadata keys against the logical parts.
The focused new tests, full pkg/catalog and pkg/sql/plan suites, and go vet for both owning packages passed locally.
2798296 to
2337feb
Compare
## What type of PR is this? - [ ] API-change - [ ] BUG - [ ] Improvement - [ ] Documentation - [ ] Feature - [x] Test and CI - [ ] Code Refactoring ## Which issue(s) this PR covers: Regression coverage for #26841. The production correction merged independently in #26829 while this PR was under review. ## What this PR does / why we need it: This branch is rebased on current `main` and intentionally retains only coverage not already present upstream: - white-box planner cases for declared prefix metadata across BETWEEN, scalar and paired ranges, IN, in_range, OR, unique/non-unique, malformed metadata, and a complete-index control; - FORCE INDEX FOR ORDER BY and FORCE INDEX FOR GROUP BY planning checks; - an end-to-end BVT covering same-prefix neighbors, forced versus ignored access, runtime bounds, flush, a complete-index control, RENAME COLUMN, and INSERT/UPDATE/DELETE after rename. During conflict resolution, the old production implementation was deliberately not replayed: current main contains the stronger #26829 implementation, including V2 prefix-metadata support. Reintroducing the old code would duplicate validation and reject valid delimiter-bearing column names. ## Validation - focused and complete controlled-CGo `./pkg/sql/plan` tests; - `go vet ./pkg/sql/plan` with the project CGo include paths; - fresh `make build`; - `prefix_index_range` BVT: 51/51; - existing `prefix_index_non_equality` BVT: 52/52; - `git diff --check`. --------- Co-authored-by: XuPeng-SH <[email protected]>
## What type of PR is this? - [ ] API-change - [x] BUG - [ ] Improvement - [ ] Documentation - [ ] Feature - [ ] Test and CI - [ ] Code Refactoring ## Which issue(s) this PR fixes: Fixes matrixorigin#26813 Fixes matrixorigin#26818 Fixes matrixorigin#26822 Fixes matrixorigin#26823 Fixes matrixorigin#26824 ## What this PR does / why we need it: Regular prefix indexes had correctness gaps across planning, metadata maintenance, DDL validation, and rolling upgrades: - Use the stored prefix only for equality candidate lookup, retain the full base-table residual, and reject lossy index-only reconstruction. - Disable prefix indexes for IN, BETWEEN, scalar/paired ranges, ORDER/GROUP reconstruction, and relational index joins until those paths have prefix-aware candidate semantics. - Fail closed when prefix metadata is malformed or no longer matches the logical index parts. Optional reads skip the index; INSERT, UPDATE, DELETE, and REPLACE reject the stale index with a rebuild instruction. - Preserve prefix lengths through RENAME COLUMN and CHANGE COLUMN, including an all-index preflight so protocol or metadata failures occur before the first TableDef mutation. - Keep ordinary identifiers on the legacy metadata representation, and gate lossless `prefix_lengths_v2` emission for delimiter-bearing identifiers on deployment protocol version 13. - Reserve the internal `__mo_alias_` namespace across explicit CREATE TABLE, CTAS, ADD COLUMN, CHANGE COLUMN, and RENAME COLUMN paths. ## Review follow-up: actual versus theoretical The P1 findings are real: - On the pre-fix PR binary after flushing hidden-index blocks, `FORCE INDEX` prefix IN returned no rows while `IGNORE INDEX` returned ids 3 and 6. Prefix BETWEEN and range also disagreed with the base-table oracle. - `FORCE INDEX FOR ORDER BY` returned truncated values (`abcd`) and chose the wrong LIMIT 2 rows; `IGNORE INDEX` returned the full values in the correct order. - A table created and renamed by the base binary persisted `column_name=b` with `{"prefix_lengths":"a:4"}`. The pre-fix PR still selected the index and missed the row. The updated binary returns the row through a base-table scan. - The base-version metadata reader was run against an actual v2 payload and returned no prefix lengths, confirming the rolling-upgrade incompatibility rather than assuming it. - On that same stale catalog, the updated binary rejects INSERT, UPDATE, DELETE, and REPLACE before execution and leaves the rows unchanged. Before the fix, non-unique DELETE returned success without deleting the row, while REPLACE admitted two unique-index values with the same stored prefix. A forced relational equality JOIN did select the prefix index on the pre-fix binary, but the tested result remained correct. This PR still disables that unproven path as requested; it is not presented as a reproduced wrong-result case. ## Validation - Rebased cleanly onto current `main` at `415724ff30cfb1dd96fa2eb75110402fbc4e7239`. - Protocol boundary regressions prove v12 rejects and v13 accepts lossless prefix metadata on both CREATE and RENAME.\n- `make build`. - Controlled CGo tests: full `./pkg/sql/plan`; `./pkg/catalog`, `./pkg/common/runtime`, `./pkg/sql/plan/function/ctl`. - Protocol-adjacent tests: `./pkg/frontend`, `./pkg/sql/compile`, `./pkg/txn/rpc`, and `./pkg/vm/engine/tae/db/dbutils`. - `go vet`: `./pkg/catalog`, `./pkg/common/runtime`, `./pkg/sql/plan`, and `./pkg/sql/plan/function/ctl`. - Focused BVT on the final binary: 200/200 statements across six scripts. - The new 52-statement matrix covers default/FORCE/IGNORE, unique and non-unique prefixes, IN/BETWEEN/paired ranges, same-prefix neighbors, prepared values, Unicode, JOIN/ORDER hints, and reads before and after flush. - `git diff --check`. --------- Co-authored-by: XuPeng-SH <[email protected]>
## What type of PR is this? - [ ] API-change - [ ] BUG - [ ] Improvement - [ ] Documentation - [ ] Feature - [x] Test and CI - [ ] Code Refactoring ## Which issue(s) this PR covers: Regression coverage for matrixorigin#26841. The production correction merged independently in matrixorigin#26829 while this PR was under review. ## What this PR does / why we need it: This branch is rebased on current `main` and intentionally retains only coverage not already present upstream: - white-box planner cases for declared prefix metadata across BETWEEN, scalar and paired ranges, IN, in_range, OR, unique/non-unique, malformed metadata, and a complete-index control; - FORCE INDEX FOR ORDER BY and FORCE INDEX FOR GROUP BY planning checks; - an end-to-end BVT covering same-prefix neighbors, forced versus ignored access, runtime bounds, flush, a complete-index control, RENAME COLUMN, and INSERT/UPDATE/DELETE after rename. During conflict resolution, the old production implementation was deliberately not replayed: current main contains the stronger matrixorigin#26829 implementation, including V2 prefix-metadata support. Reintroducing the old code would duplicate validation and reject valid delimiter-bearing column names. ## Validation - focused and complete controlled-CGo `./pkg/sql/plan` tests; - `go vet ./pkg/sql/plan` with the project CGo include paths; - fresh `make build`; - `prefix_index_range` BVT: 51/51; - existing `prefix_index_non_equality` BVT: 52/52; - `git diff --check`. --------- Co-authored-by: XuPeng-SH <[email protected]>
## What type of PR is this? - [ ] API-change - [x] BUG - [ ] Improvement - [ ] Documentation - [ ] Feature - [ ] Test and CI - [ ] Code Refactoring ## Which issue(s) this PR fixes: Fixes matrixorigin#26813 Fixes matrixorigin#26818 Fixes matrixorigin#26822 Fixes matrixorigin#26823 Fixes matrixorigin#26824 ## What this PR does / why we need it: Regular prefix indexes had correctness gaps across planning, metadata maintenance, DDL validation, and rolling upgrades: - Use the stored prefix only for equality candidate lookup, retain the full base-table residual, and reject lossy index-only reconstruction. - Disable prefix indexes for IN, BETWEEN, scalar/paired ranges, ORDER/GROUP reconstruction, and relational index joins until those paths have prefix-aware candidate semantics. - Fail closed when prefix metadata is malformed or no longer matches the logical index parts. Optional reads skip the index; INSERT, UPDATE, DELETE, and REPLACE reject the stale index with a rebuild instruction. - Preserve prefix lengths through RENAME COLUMN and CHANGE COLUMN, including an all-index preflight so protocol or metadata failures occur before the first TableDef mutation. - Keep ordinary identifiers on the legacy metadata representation, and gate lossless `prefix_lengths_v2` emission for delimiter-bearing identifiers on deployment protocol version 13. - Reserve the internal `__mo_alias_` namespace across explicit CREATE TABLE, CTAS, ADD COLUMN, CHANGE COLUMN, and RENAME COLUMN paths. ## Review follow-up: actual versus theoretical The P1 findings are real: - On the pre-fix PR binary after flushing hidden-index blocks, `FORCE INDEX` prefix IN returned no rows while `IGNORE INDEX` returned ids 3 and 6. Prefix BETWEEN and range also disagreed with the base-table oracle. - `FORCE INDEX FOR ORDER BY` returned truncated values (`abcd`) and chose the wrong LIMIT 2 rows; `IGNORE INDEX` returned the full values in the correct order. - A table created and renamed by the base binary persisted `column_name=b` with `{"prefix_lengths":"a:4"}`. The pre-fix PR still selected the index and missed the row. The updated binary returns the row through a base-table scan. - The base-version metadata reader was run against an actual v2 payload and returned no prefix lengths, confirming the rolling-upgrade incompatibility rather than assuming it. - On that same stale catalog, the updated binary rejects INSERT, UPDATE, DELETE, and REPLACE before execution and leaves the rows unchanged. Before the fix, non-unique DELETE returned success without deleting the row, while REPLACE admitted two unique-index values with the same stored prefix. A forced relational equality JOIN did select the prefix index on the pre-fix binary, but the tested result remained correct. This PR still disables that unproven path as requested; it is not presented as a reproduced wrong-result case. ## Validation - Rebased cleanly onto current `main` at `415724ff30cfb1dd96fa2eb75110402fbc4e7239`. - Protocol boundary regressions prove v12 rejects and v13 accepts lossless prefix metadata on both CREATE and RENAME.\n- `make build`. - Controlled CGo tests: full `./pkg/sql/plan`; `./pkg/catalog`, `./pkg/common/runtime`, `./pkg/sql/plan/function/ctl`. - Protocol-adjacent tests: `./pkg/frontend`, `./pkg/sql/compile`, `./pkg/txn/rpc`, and `./pkg/vm/engine/tae/db/dbutils`. - `go vet`: `./pkg/catalog`, `./pkg/common/runtime`, `./pkg/sql/plan`, and `./pkg/sql/plan/function/ctl`. - Focused BVT on the final binary: 200/200 statements across six scripts. - The new 52-statement matrix covers default/FORCE/IGNORE, unique and non-unique prefixes, IN/BETWEEN/paired ranges, same-prefix neighbors, prepared values, Unicode, JOIN/ORDER hints, and reads before and after flush. - `git diff --check`. --------- Co-authored-by: XuPeng-SH <[email protected]>
## What type of PR is this? - [ ] API-change - [ ] BUG - [ ] Improvement - [ ] Documentation - [ ] Feature - [x] Test and CI - [ ] Code Refactoring ## Which issue(s) this PR covers: Regression coverage for matrixorigin#26841. The production correction merged independently in matrixorigin#26829 while this PR was under review. ## What this PR does / why we need it: This branch is rebased on current `main` and intentionally retains only coverage not already present upstream: - white-box planner cases for declared prefix metadata across BETWEEN, scalar and paired ranges, IN, in_range, OR, unique/non-unique, malformed metadata, and a complete-index control; - FORCE INDEX FOR ORDER BY and FORCE INDEX FOR GROUP BY planning checks; - an end-to-end BVT covering same-prefix neighbors, forced versus ignored access, runtime bounds, flush, a complete-index control, RENAME COLUMN, and INSERT/UPDATE/DELETE after rename. During conflict resolution, the old production implementation was deliberately not replayed: current main contains the stronger matrixorigin#26829 implementation, including V2 prefix-metadata support. Reintroducing the old code would duplicate validation and reject valid delimiter-bearing column names. ## Validation - focused and complete controlled-CGo `./pkg/sql/plan` tests; - `go vet ./pkg/sql/plan` with the project CGo include paths; - fresh `make build`; - `prefix_index_range` BVT: 51/51; - existing `prefix_index_non_equality` BVT: 52/52; - `git diff --check`. --------- Co-authored-by: XuPeng-SH <[email protected]>
## What type of PR is this? - [ ] API-change - [x] BUG - [ ] Improvement - [ ] Documentation - [ ] Feature - [ ] Test and CI - [ ] Code Refactoring ## Which issue(s) this PR fixes: Fixes matrixorigin#26813 Fixes matrixorigin#26818 Fixes matrixorigin#26822 Fixes matrixorigin#26823 Fixes matrixorigin#26824 ## What this PR does / why we need it: Regular prefix indexes had correctness gaps across planning, metadata maintenance, DDL validation, and rolling upgrades: - Use the stored prefix only for equality candidate lookup, retain the full base-table residual, and reject lossy index-only reconstruction. - Disable prefix indexes for IN, BETWEEN, scalar/paired ranges, ORDER/GROUP reconstruction, and relational index joins until those paths have prefix-aware candidate semantics. - Fail closed when prefix metadata is malformed or no longer matches the logical index parts. Optional reads skip the index; INSERT, UPDATE, DELETE, and REPLACE reject the stale index with a rebuild instruction. - Preserve prefix lengths through RENAME COLUMN and CHANGE COLUMN, including an all-index preflight so protocol or metadata failures occur before the first TableDef mutation. - Keep ordinary identifiers on the legacy metadata representation, and gate lossless `prefix_lengths_v2` emission for delimiter-bearing identifiers on deployment protocol version 13. - Reserve the internal `__mo_alias_` namespace across explicit CREATE TABLE, CTAS, ADD COLUMN, CHANGE COLUMN, and RENAME COLUMN paths. ## Review follow-up: actual versus theoretical The P1 findings are real: - On the pre-fix PR binary after flushing hidden-index blocks, `FORCE INDEX` prefix IN returned no rows while `IGNORE INDEX` returned ids 3 and 6. Prefix BETWEEN and range also disagreed with the base-table oracle. - `FORCE INDEX FOR ORDER BY` returned truncated values (`abcd`) and chose the wrong LIMIT 2 rows; `IGNORE INDEX` returned the full values in the correct order. - A table created and renamed by the base binary persisted `column_name=b` with `{"prefix_lengths":"a:4"}`. The pre-fix PR still selected the index and missed the row. The updated binary returns the row through a base-table scan. - The base-version metadata reader was run against an actual v2 payload and returned no prefix lengths, confirming the rolling-upgrade incompatibility rather than assuming it. - On that same stale catalog, the updated binary rejects INSERT, UPDATE, DELETE, and REPLACE before execution and leaves the rows unchanged. Before the fix, non-unique DELETE returned success without deleting the row, while REPLACE admitted two unique-index values with the same stored prefix. A forced relational equality JOIN did select the prefix index on the pre-fix binary, but the tested result remained correct. This PR still disables that unproven path as requested; it is not presented as a reproduced wrong-result case. ## Validation - Rebased cleanly onto current `main` at `415724ff30cfb1dd96fa2eb75110402fbc4e7239`. - Protocol boundary regressions prove v12 rejects and v13 accepts lossless prefix metadata on both CREATE and RENAME.\n- `make build`. - Controlled CGo tests: full `./pkg/sql/plan`; `./pkg/catalog`, `./pkg/common/runtime`, `./pkg/sql/plan/function/ctl`. - Protocol-adjacent tests: `./pkg/frontend`, `./pkg/sql/compile`, `./pkg/txn/rpc`, and `./pkg/vm/engine/tae/db/dbutils`. - `go vet`: `./pkg/catalog`, `./pkg/common/runtime`, `./pkg/sql/plan`, and `./pkg/sql/plan/function/ctl`. - Focused BVT on the final binary: 200/200 statements across six scripts. - The new 52-statement matrix covers default/FORCE/IGNORE, unique and non-unique prefixes, IN/BETWEEN/paired ranges, same-prefix neighbors, prepared values, Unicode, JOIN/ORDER hints, and reads before and after flush. - `git diff --check`. --------- Co-authored-by: XuPeng-SH <[email protected]>
## What type of PR is this? - [ ] API-change - [ ] BUG - [ ] Improvement - [ ] Documentation - [ ] Feature - [x] Test and CI - [ ] Code Refactoring ## Which issue(s) this PR covers: Regression coverage for matrixorigin#26841. The production correction merged independently in matrixorigin#26829 while this PR was under review. ## What this PR does / why we need it: This branch is rebased on current `main` and intentionally retains only coverage not already present upstream: - white-box planner cases for declared prefix metadata across BETWEEN, scalar and paired ranges, IN, in_range, OR, unique/non-unique, malformed metadata, and a complete-index control; - FORCE INDEX FOR ORDER BY and FORCE INDEX FOR GROUP BY planning checks; - an end-to-end BVT covering same-prefix neighbors, forced versus ignored access, runtime bounds, flush, a complete-index control, RENAME COLUMN, and INSERT/UPDATE/DELETE after rename. During conflict resolution, the old production implementation was deliberately not replayed: current main contains the stronger matrixorigin#26829 implementation, including V2 prefix-metadata support. Reintroducing the old code would duplicate validation and reject valid delimiter-bearing column names. ## Validation - focused and complete controlled-CGo `./pkg/sql/plan` tests; - `go vet ./pkg/sql/plan` with the project CGo include paths; - fresh `make build`; - `prefix_index_range` BVT: 51/51; - existing `prefix_index_non_equality` BVT: 52/52; - `git diff --check`. --------- Co-authored-by: XuPeng-SH <[email protected]>
What type of PR is this?
Which issue(s) this PR fixes:
Fixes #26813
Fixes #26818
Fixes #26822
Fixes #26823
Fixes #26824
What this PR does / why we need it:
Regular prefix indexes had correctness gaps across planning, metadata maintenance, DDL validation, and rolling upgrades:
prefix_lengths_v2emission for delimiter-bearing identifiers on deployment protocol version 13.__mo_alias_namespace across explicit CREATE TABLE, CTAS, ADD COLUMN, CHANGE COLUMN, and RENAME COLUMN paths.Review follow-up: actual versus theoretical
The P1 findings are real:
FORCE INDEXprefix IN returned no rows whileIGNORE INDEXreturned ids 3 and 6. Prefix BETWEEN and range also disagreed with the base-table oracle.FORCE INDEX FOR ORDER BYreturned truncated values (abcd) and chose the wrong LIMIT 2 rows;IGNORE INDEXreturned the full values in the correct order.column_name=bwith{"prefix_lengths":"a:4"}. The pre-fix PR still selected the index and missed the row. The updated binary returns the row through a base-table scan.A forced relational equality JOIN did select the prefix index on the pre-fix binary, but the tested result remained correct. This PR still disables that unproven path as requested; it is not presented as a reproduced wrong-result case.
Validation
mainat415724ff30cfb1dd96fa2eb75110402fbc4e7239.make build../pkg/sql/plan;./pkg/catalog,./pkg/common/runtime,./pkg/sql/plan/function/ctl../pkg/frontend,./pkg/sql/compile,./pkg/txn/rpc, and./pkg/vm/engine/tae/db/dbutils.go vet:./pkg/catalog,./pkg/common/runtime,./pkg/sql/plan, and./pkg/sql/plan/function/ctl.git diff --check.