Skip to content

fix(ivf): qualify INCLUDE entries before reader Top-K on 4.2-dev - #33590

Merged
XuPeng-SH merged 5 commits into
matrixorigin:4.2-devfrom
LeftHandCold:fix/4.2-ivf-include-topk-residual
Dec 2, 2026
Merged

XuPeng-SH merged 5 commits into
matrixorigin:4.2-devfrom
LeftHandCold:fix/4.2-ivf-include-topk-residual

Conversation

@LeftHandCold

@LeftHandCold LeftHandCold commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

What type of PR is this?

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

Which issue(s) this PR fixes:

Fixes #33585

What this PR does / why we need it:

On 4.2-dev, IVF INCLUDE could rank entries before applying the business filter. The nearest nonqualifying rows could consume an entire page. There was also a wrong-result path when an indexed query combined an included column with a distance range. In the one-list example from review, both SELECT and a committed INSERT … SELECT returned IDs 1 and 2 even though the WHERE clause required IDs 5 and 6.

The entries query now checks candidate eligibility before each LIMIT, including when reading exact primary-key chunks. The current request supplies the distance function and thresholds, so alternating L2 and squared L2 on a warm index preserves the correct units. SQL evaluates the distance predicate at its original precision. NULL vectors cannot take slots in the internal candidate query. Quantized or asynchronously maintained entries fall back to the original relational plan for extracted distance bounds. The planner retains the original WHERE on a declined rewrite.

Unfiltered queries and supported centroid-prefix probes keep the storage Top-K optimization. Queries with a residual distance or business condition may score more entries to produce the correct page. There is no additional SQL round trip or change to the index format.

The Ubuntu/x86 UT failure occurred during fixture writes in TestS3FSPrefetchBoundsStagedFullFiles. Setup formerly shared the worker's five-second deadline. The smaller fixture still fills all four slots and exercises two overflow objects. Each synchronization wait has its own deadline.

Verification

  • Rebased onto 4.2-dev at 167a1a9199. A native service built from 4b119ed955 matched the pre-index reference for 9 SQL cases and 18 precision/quantization cases. A second connection saw committed IDs 5 and 6. EXPLAIN ANALYZE confirmed reader Top-K on the unfiltered query.
  • Two CNs served 16 interleaved L2 and squared-L2 requests on the same index. All returned their expected IDs. A read on the other CN verified the committed page.
  • The vector SQL regression suite passed 353/353 checks. The three suites affected by the final range-extraction cleanup passed again at the final revision, 181/181.
  • The complete planner and IVF search packages passed in normal and race modes. Focused race tests passed for compiler, table-function and reader consumers. The two cache tests passed ten race runs each.
  • gofmt, go vet and incremental golangci-lint passed. The 13 molint diagnostics belong to files identical to 4.2-dev.

The pre-existing default NULL ordering and signed-zero primary-key issues are tracked separately in #33586 and #33587.

@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 →

@mergify

mergify Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

This pull request is merged.

@XuPeng-SH

XuPeng-SH commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: 0a41bce2ebe278d394c3343b2d8ea3ccb8a5e093

Review completed. Read the assessment.

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

mo-bot review · mo-bot-local · review

Commit: 0a41bce2ebe278d394c3343b2d8ea3ccb8a5e093

Assessment: request_changes

The guard fixes the reported residual-filter ordering, but it introduces a wrong-result regression when an IVF INCLUDE query also has a distance-range predicate. The retained SQL sort does not enforce a bound that the outer planner has already moved into reader parameters.

Model: gpt-6.1-terra / max

Session: 01a22b07-00b6-7e32-b0a1-3099d0b867c7 · slot 1 · generation 39

  • P1 pkg/sql/plan/pushdown.go:837: A default-mode IVF query filters an included column and also constrains the same distance expression used by ORDER BY, for example category = 20 AND l2_distance(v, '[0,0]') >= 5.
    Impact: The indexed query can return rows that violate the distance predicate. An INSERT ... SELECT using that query can persist those incorrect rows.
    Evidence: The outer IVF rewrite removes the distance predicate from the base scan and stores it in IndexReaderParam.DistRange. When this guard declines Top-K, the entries scan has no reader OrderBy or Limit, so reader.SetIndexParam returns without enforcing that range; the generated entries SQL contains no copy of it. In a one-list, one-CN MySQL probe, the pre-index and forced-scan controls returned IDs 5 and 6, while the candidate's IVF query returned 1 and 2. Replacing only pushdown.go with the base version returned 5 and 6 through IVF. Preserve an enforcing distance predicate when declining reader Top-K, for example in the entries SQL or another applicable filter.

Validation:

  • Private-runtime preflight and native-input audit passed; the current CGo library was built in the diagnostic directory.

  • The focused planner run selected and passed the committed guard tests and the unfiltered control. Its added distance-range assertion failed as described: checks/1796122138967344377-3.json and review-range.stdout.log.

  • The public SQL counterexample failed on the candidate: checks/1796122288565370798-3.json and review-public.stdout.log. The same fixture passed with the base planner file overlaid: checks/1796122392429227041-3.json and review-public-base.stdout.log.

  • The supplied CI snapshot reports successful Ubuntu unit tests and multi-CN BVT.

Limitations:

  • The SQL counterexample used an isolated one-CN embedded cluster; the base comparison overlaid only the changed planner implementation onto the assessed head.

  • The author's reported full planner and repeated mo-tester runs were not independently repeated.

@LeftHandCold

Copy link
Copy Markdown
Collaborator Author

Follow-up eb9c7d2: ordinary INCLUDE queries now qualify unresolved business predicates before the candidate limit. In a fresh default session on the final binary, IN (2,7) returned IDs 21,24,27,30; a genuine Go driver prepared group_id=? returned 21,24,27. Both INSERT SELECT destinations matched their unindexed controls on an independent connection. The full planner package and 42/42 statements of the SQL regression passed. The four explicitly requested single-round POST comparisons remain approximate; no ordinary-query mismatches in the 148-comparison matrix. No index rebuild or storage format change.

@matrix-meow matrix-meow added size/M Denotes a PR that changes [100,499] lines and removed size/S Denotes a PR that changes [10,99] lines labels Dec 1, 2026

@LeftHandCold LeftHandCold left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Changes needed on 4.2-dev: the prior distance-range wrong-result finding is not closed by this head. The new residual fallback does help non-serializable conditions, but a serializable INCLUDE equality plus a bound on the same vector distance takes a different path. The ordinary business-row WHERE must remain enforced somewhere even when reader Top-K is declined.

The previous SQL replay and one-file control actually returned IDs 1,2 vs 5,6 on the preceding head. On this SHA I re-traced range extraction, residual fallback, entries SQL generation and the reader guard; I did not rerun that SQL. The 42/42 targeted BVT does not exercise this conjunction. Please add an ordinary, unhinted, single-list case asserting the actual IDs against the exact scan (including a committed INSERT … SELECT if possible), not just plan shape or row count.

Comment thread pkg/sql/plan/apply_indices_ivfflat.go Resolved
@XuPeng-SH

XuPeng-SH commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: eb9c7d220efc85e031a53c290ec0649063c0a554

Review completed. Read the assessment.

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

mo-bot review · mo-bot-local · review

Commit: eb9c7d220efc85e031a53c290ec0649063c0a554

Assessment: request_changes

The change reuses the existing prefilter and reader paths, and it fixes the tested business-filter cases. The previously reported distance-range regression remains open: an ordinary indexed query can return and persist rows that fail its WHERE clause.

Model: gpt-6.1-terra / max

Session: 01a22b07-00b6-7e32-b0a1-3099d0b867c7 · slot 1 · generation 39

  • P1 pkg/sql/plan/pushdown.go:837: An ordinary IVF INCLUDE query has a serializable included-column equality and a distance predicate, for example category = 20 AND l2_distance(v, '[0,0]') >= 5, followed by ORDER BY that distance LIMIT 2.
    Impact: The indexed SELECT can return rows outside the requested distance range, and INSERT ... SELECT can commit those incorrect rows. With one IVF list and no distance ties, the current head returned and persisted IDs [1,2] instead of [5,6].
    Evidence: The outer rewrite extracts the distance predicate into IndexReaderParam.DistRange, but the new fallback at apply_indices_ivfflat.go:1355 only applies when a business predicate remains unserialized. This guard then declines reader Top-K for the included-column filter. The entries SQL does not independently apply the distance range, and reader.SetIndexParam ignores it without an OrderBy and Limit. The same public SQL probe passed with the two planner files overlaid from the frozen base. Preserve or enforce the range before the candidate limit, or decline the IVF rewrite for this shape, and cover the SELECT and committed INSERT results with a regression test.

Validation:

  • Private-runtime preflight passed; native prerequisites were built from inputs matching the frozen head.

  • Four focused planner tests passed, including the changed residual-filter and entries Top-K tests; planner-focused.log, receipt 1796133067214830792-4.json.

  • A one-CN public SQL probe passed the original equality, IN, prepared-parameter and committed INSERT cases; feature-probe.log, receipt 1796133151497208261-4.json.

  • The distance-range SQL probe failed on the current head with [1,2] versus the unindexed and forced controls [5,6]; range-probe.log, receipt 1796132735355634463-4.json. The same probe passed using frozen-base planner files; range-base.log, receipt 1796132844290646830-4.json.

  • git diff --check passed; receipt 1796133182012306488-3.json.

Limitations:

  • The local SQL probes used a single embedded CN. Full mo-tester comparison, multi-CN behavior and performance were not independently rerun; the supplied local_validation list contains no receipts for the author-reported broader runs.

This branch was successfully deployed

No deployments
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/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants