Skip to content

[Refactoring]: replace TVF with better plan #26977

Closed

Description

@cpegeric

Why do you want to refactor this code?

Overall, seems using an TVF is really not a good idea. We should replace the TVF with really a plan and splice the plan with the whole query plan, so that optimization can correctly, efficiently, optimize whole query -- right now the TVF is a separate pass and cross boundary, causing many troubles.

Describe the solution you'd like

splice the plan with the whole query plan, so that optimization can correctly, efficiently, optimize whole query -- right now the TVF is a separate pass and cross boundary, causing many troubles.

Describe alternatives you've considered

No response

Additional information

No response

Activity

  1. added
    severity/s0
    on Aug 12, 2026
  2. cpegeric commented on Aug 12, 2026

    @cpegeric
    ContributorAuthor

    For thie refactoring, I don't know much about the plan. You'd better discuss with Feng who is the best person to change the Plan.

  3. assigned and unassigned on Aug 21, 2026
  4. slyunnn commented on Sep 1, 2026

    @slyunnn

    Regression and extended verification for #26977 (2026-09-01)

    Verification Environment

    • Source: latest origin/main at 33af6ae3c0c23e4f277137460577ea64678f7ecf — perf(plan): avoid wide HashBuild for full indexed UPDATE (#27920).
    • Related fix: 5735684d69c9b171c4d7a5ebee3ebcc539ffc8cc — refactor: replace IVF search TVF with vector index scan (#27297). The merge commit is an ancestor of the tested origin/main.
    • Topology: isolated Linux Docker runtime built from that exact source checkout, with 1 Log + 1 TN + 2 CN + 1 Proxy in the same container.
    • Test endpoints: Proxy 127.0.0.1:18000, CN1 127.0.0.1:18001, and CN2 127.0.0.1:18002; all completed MySQL initial handshake and a post-test SELECT 1.
    • Client protocol: MySQL wire protocol over TCP, using the mysql client.

    What Was Covered

    • OK — Prepared IVF query reuse through the Proxy: EXECUTE with k = 2 then k = 3 selected Vector Index Scan and returned the changed result cardinality.
    • OK — Correlated CROSS APPLY with an INCLUDE(category) pre-filter: two prepared executions rebound concat(?, '') from x to y, returned different expected ids, and retained Vector Index Scan.
    • OK — Historical account snapshot: the snapshot query selected Vector Index Scan and returned the snapshot-visible row rather than the post-snapshot nearest row.
    • OK — Multi-CN execution through the Proxy: EXPLAIN and EXPLAIN ANALYZE reported AP QUERY PLAN ON MULTICN and Vector Index Scan; integer, string, composite, and sparse-id results matched their exact-result controls.
    • OK — Direct CN coverage: CN1 replayed the IVF l1_distance and l2_distance_sq cases; CN2 replayed the vector op_type matrix, including l2_distance, l2_distance_sq, l1_distance, inner_product, and cosine_distance controls.
    • OK — Unhappy paths: expected ERROR 20301 dimension mismatch and invalid op_type diagnostics were returned; the BVT continued and subsequent valid cases passed.
    • OK — INCLUDE mode controls: include, post, and pre plans each selected the intended access path and returned the same filtered top-3 rows.
    • OK — Focused current-main unit checks for immutable prepared generations, APPLY transaction offsets, pre-filter rebinding, reader cleanup, snapshot/publisher identity, non-historical snapshots, MaxUint64, filtering-before-Top-K, planner rewrite, pagination, and catalog dependencies.
    • OK — Bounded scale regression: a 16,384-row IVF table on the multi-CN Proxy path returned 20 rows with Vector Index Scan and an index primary-key filter.
    • Not covered — The full 407/407 vector BVT aggregate job and race suite were not rerun in this local session; their exact-head results are recorded in the merged PR.

    Findings

    The ivf_search TVF replacement is operational on latest origin/main: optimizer-visible VECTOR_INDEX_SCAN appears in the public plan, survives prepared-plan reuse and correlated APPLY, and preserves snapshot, account, filter, pagination, and multi-CN behavior.

    Focused product BVT SQL files were replayed through the MySQL client: vector_prepared_limit.sql, vector_ivf_snapshot.sql, vector_ivf_multicn_search.sql, vector_ivf_l1_l2sq.sql, vector_index_optype_matrix.sql, and vector_ivfflat_include_mode_comparison.sql. Expected-error statements were continued deliberately; no unexpected SQL failure occurred. Test databases were absent after cleanup.

    MOTR / BVT Coverage

    No MOTR change is needed for this refactoring. The related fix already introduced/updated the owning product vector BVT coverage. This verification directly replayed the focused SQL files, but did not invoke the full mo-tester aggregate runner; therefore the aggregate 407/407 result is not claimed as a new local run.

    Large-data regression was required in bounded form because candidate scanning and Top-K execution can depend on row/batch boundaries. The 16,384-row multi-CN case was used as the minimum stable scale check and crossed a non-trivial scan boundary. A larger memory/spill or throughput campaign is not needed for this conclusion: no issue-specific failure depends on resource exhaustion, data skew, network transfer, or a configured spill threshold.

    Final Conclusion

    Fixed. Latest origin/main contains the linked refactor and passed direct product SQL, focused unit, multi-CN, Proxy, prepared, APPLY, snapshot, unhappy-path, operator-control, and bounded-scale verification. No MatrixOne source or MOTR test was modified in this verification.

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

Metadata

Metadata

Assignees

Labels

kind/refactoringseverity/s0

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions