feat(vector): implement adaptive mode for IVF vector search#23650
feat(vector): implement adaptive mode for IVF vector search#23650iamlinjunhong wants to merge 4 commits intomatrixorigin:mainfrom
Conversation
PR Compliance Guide 🔍Below is a summary of compliance checks for this PR:
Compliance status legend🟢 - Fully Compliant🟡 - Partial Compliant 🔴 - Not Compliant ⚪ - Requires Further Human Verification 🏷️ - Compliance label |
|||||||||||||||||||||||||
PR Code Suggestions ✨Explore these optional code suggestions:
|
||||||||||||||||||||||||||||||
a86468b to
ad7421a
Compare
This commit introduces an intelligent adaptive mode for IVF vector search that automatically selects the optimal execution strategy based on runtime statistics and query characteristics. Key features: - Add 'mode=auto' option for automatic mode selection - Implement shouldUseForceMode() for small dataset optimization - Add calculateAutoModeOverFetchFactor() for dynamic over-fetching based on selectivity - Add calculateAdaptiveNprobe() for dynamic nprobe adjustment - Implement adaptive fallback: retry with 'pre' mode when 'post' returns empty results - Support exact PK filter in runtime filter for improved accuracy with small result sets - Add session variable 'enable_vector_auto_mode_by_default' Implementation details: - Phase 1: mode=auto recognizes and routes to appropriate strategy - Phase 2: Small dataset detection triggers force mode (skip index) - Phase 3-4: Dynamic parameter adjustment based on filter selectivity - Phase 5: Adaptive retry mechanism via ErrVectorNeedRetryWithPreMode Testing: - Add comprehensive unit tests for all new functions - Add integration tests for adaptive mode scenarios
5ba2596 to
e302117
Compare
e302117 to
0f6e46c
Compare
User description
This commit introduces an intelligent adaptive mode for IVF vector search that automatically selects the optimal execution strategy based on runtime statistics and query characteristics.
Key features:
Implementation details:
Testing:
What type of PR is this?
Which issue(s) this PR fixes:
issue #23580
What this PR does / why we need it:
feat(vector): implement adaptive mode for IVF vector search
PR Type
Enhancement, Tests
Description
Test Plan: Adaptive Mode for IVF Vector Search
1. Overview
The goal of this test plan is to verify the Adaptive Mode for IVF vector search. This feature introduces an intelligent execution strategy that automatically selects between Index-based search (
mode=post), Filter-first search (mode=pre), and Brute-force search (mode=force) based on data statistics and runtime results.2. Prerequisites
set experimental_ivf_index = 1;(Required for IVF index)set enable_vector_auto_mode_by_default = 1;(Optional, for testing default behavior)lists=...during index creation to control cluster density.set probe_limit = 1;to force low recall scenarios for testing the retry mechanism.3. Test Scenarios
Phase 1: Syntax & Integration
Objective: Verify that
mode=autois recognized and behaves like a "smart" search.WITH OPTION 'mode=auto'.WHEREfilters andLIMIT.mode=autoandmode=pre. They should be identical in terms of correctness.Phase 2: Smart Selection (Small Dataset Optimization)
Objective: Verify that the system skips the index (uses
mode=force) when the dataset is too small to benefit from a vector index.Table Rows < LIMIT * 2.mode=autoandLIMIT 10.EXPLAINto check if aFUNCTION_SCAN(ivf_search)node exists. If it does not exist, the system correctly selectedmode=forceto avoid index overhead.Phase 3 & 4: Dynamic Parameter Adjustment
Objective: Verify that the system adjusts internal parameters (
nprobeandover-fetch factor) when filters are highly selective.mode=autoincreasesnprobeto improve recall.category=Aand 1% hascategory=B.WHERE category=B.mode=autoreturns the correct nearest neighbor (compare withmode=force), whereas a non-adaptivemode=postmight return empty/wrong results due to low recall in selective filters.Phase 5: Adaptive Fallback (Retry Mechanism)
Objective: Verify that if
mode=postreturns fewer results than requested, the system automatically retries withmode=pre.WHEREclause, and they reside in different clusters.probe_limit = 1(search only 1 cluster).mode=post: Should return empty (because it only checked the nearest cluster and found no matches after filtering).mode=auto: Should return the correct results.mode=pre, and reruns. The user should see results without manually intervention.Phase 6: Session Default Behavior
Objective: Verify the new session variable
enable_vector_auto_mode_by_default.enable_vector_auto_mode_by_default = 1. Run a search without an explicitWITH OPTIONclause. It should behave likemode=auto.mode=preormode=postshould override the session default.4. Troubleshooting & Observations
mode=post(fails) vsmode=auto(succeeds) on the same dataset.postmode, or just correct.ANALYZE TABLE. Ensure statistics are updated if the system doesn't trigger force mode as expected.5. Summary Table for Testers
mode=autoSyntaxEXPLAINivf_searchin planmode=postautosucceeds wherepostreturns emptyautomodeDiagram Walkthrough
flowchart LR Query["Query with mode=auto"] Resolve["resolveVectorSearchMode()"] SmallData["shouldUseForceMode()"] Force["Force Mode<br/>Full Table Scan"] Dynamic["calculateAdaptiveNprobe()<br/>calculateAutoModeOverFetchFactor()"] Execute["Execute with<br/>optimized params"] Output["Output Operator<br/>tracks rowCount"] Empty{"rowCount == 0<br/>& IsAdaptive?"} Retry["Retry with<br/>mode=pre"] Result["Final Results"] Query --> Resolve Resolve --> SmallData SmallData -->|Small Dataset| Force SmallData -->|Large Dataset| Dynamic Force --> Execute Dynamic --> Execute Execute --> Output Output --> Empty Empty -->|Yes| Retry Empty -->|No| Result Retry --> ResultFile Walkthrough
6 files
apply_indices_ivfflat_test.go
Add unit tests for adaptive vector search functionspkg/sql/plan/apply_indices_ivfflat_test.go
calculateAdaptiveNprobe()functionwith various selectivity scenarios
prepareIvfIndexContext()with adaptivenprobe logic in auto mode
shouldUseForceMode()to validate small datasetdetection
resolveVectorSearchMode()covering all moderesolution scenarios
compile2_test.go
Add tests for auto mode rewrite and adaptive detectionpkg/sql/compile/compile2_test.go
rewriteAutoModeToPre()function covering Select,ExplainStmt, Insert, Replace statements
forceModePre()function to validate forced pre-modesetting
isAdaptiveVectorSearch()to detect auto mode in queryplans
apply_indices_test.go
Add tests for auto mode over-fetch factor calculationpkg/sql/plan/apply_indices_test.go
calculateAutoModeOverFetchFactor()with variousselectivity and limit combinations
MaxOverFetchFactor capping
ivf_search_test.go
Update tests for runtime filter refactoringpkg/sql/colexec/table_function/ivf_search_test.go
TestWaitBloomFilterForTableFunction()toTestWaitRuntimeFilterForTableFunction()ivfRuntimeFilterstructbloomFilterandexactPkFilterfields in returned structvector_ivf_retry.result
Add integration tests for adaptive vector searchtest/distributed/cases/vector/vector_ivf_retry.result
retry mechanism
vector_ivf_retry.sql
Add comprehensive adaptive mode IVF vector search teststest/distributed/cases/vector/vector_ivf_retry.sql
phases covering mode=auto syntax, smart mode selection, dynamic
parameter adjustment, and adaptive fallback mechanisms
mode=autosyntax acceptance and basic executionwith filters
datasets with rare filter values
adjustment for high/low selectivity scenarios
mode returns insufficient results, automatically retrying with pre
mode
enable_vector_auto_mode_by_defaultfor enabling auto mode globallyrows, and queries without filter conditions
14 files
apply_indices_ivfflat.go
Implement adaptive mode for IVF vector searchpkg/sql/plan/apply_indices_ivfflat.go
shouldUseForceMode()to detect small datasets and trigger fulltable scan optimization
resolveVectorSearchMode()to determine optimal execution strategy(pre/post/force/auto)
calculateAdaptiveNprobe()to dynamically adjust nprobe based onfilter selectivity
detection, nprobe amplification
isAutoModeandinitialStrategyfields toivfIndexContextstructivf_search.go
Support exact PK filter in runtime filter for IVF searchpkg/sql/colexec/table_function/ivf_search.go
waitBloomFilterForTableFunction()towaitRuntimeFilterForTableFunction()and extend to handle bothBloomFilter and exact PK filters
ivfRuntimeFilterstruct to encapsulate both filter typesbuildExactPkFilter()to convert IN runtime filter vectors toSQL literals
appendVectorSQLLiteral(),appendHex(), etc.) for all supported data typesivfSearchStateto store bothbloomFilterandexactPkFiltercompile2.go
Implement adaptive retry mechanism with AST rewritingpkg/sql/compile/compile2.go
ErrVectorNeedRetryWithPreModeerror inRun()method
rewriteAutoModeToPre()to recursively rewrite 'mode=auto' to'mode=pre' in AST
forceModePre()to force pre-mode when implicit auto mode isenabled
and table expressions
prepareRetry()to sync plan after retry and update analyzerquery reference
handleQueryPlanAnalyze()to use final (retry) scopes forexplain analyze output
output.go
Implement adaptive fallback mechanism in output operatorpkg/sql/colexec/output/output.go
rowCounttracking in container to count output rowsErrVectorNeedRetryWithPreModewhenIsAdaptive=trueandrowCount==0execution stop)
with retry results
search.go
Add exact PK filter path to IVF searchpkg/vectorindex/ivfflat/search.go
sqlproc.ExactPkFilterisset, skip centroid finding and use direct IN clause
filter paths
result merging
compile.go
Add adaptive vector search detection and plan retrievalpkg/sql/compile/compile.go
GetPlan()method to retrieve current plan after retrycanRetry()to recognizeErrVectorNeedRetryWithPreModeasretryable error
isAdaptiveVectorSearch()method to detect auto mode in query planscompileSteps()to setIsAdaptiveflag on Output operator whenadaptive vector search is detected
apply_indices.go
Add auto mode over-fetch factor calculationpkg/sql/plan/apply_indices.go
MaxOverFetchFactorconstant (100.0) to cap over-fetch multipliercalculateAutoModeOverFetchFactor()using selectivity-basedcompensation:
max(baseFactor, 1/selectivity)MaxOverFetchFactorto prevent excessive memory usagebuild.go
Add exact PK filter threshold for hash buildpkg/sql/colexec/hashbuild/build.go
exactPkFilterThresholdconstant (100) to switch from BloomFilterto exact IN list for small PK sets
threshold
types.go
Add adaptive vector search fields to output operatorpkg/sql/colexec/output/types.go
rowCountfield to container struct to track output row countIsAdaptivefield to Output struct to enable adaptive vector searchfallback
WithAdaptive()method to set adaptive mode flagdeepcopy.go
Add RankOption deep copy supportpkg/sql/plan/deepcopy.go
DeepCopyRankOption()function to deep copy RankOption structDeepCopyNode()to include RankOption in node copyingquery_builder.go
Support auto mode in rank option parsingpkg/sql/plan/query_builder.go
parseRankOption()to accept "auto" mode in addition to "pre","post", and "force"
mysql_cmd_executor.go
Sync plan after execution for retry supportpkg/frontend/mysql_cmd_executor.go
Run()execution to capture plan changes fromretry
TxnComputationWrapper.planto point to final plan afterpotential retries
sqlexec.go
Add exact PK filter field to SQL processpkg/vectorindex/sqlexec/sqlexec.go
ExactPkFilterfield toSqlProcessstruct to carry exact PK filterlist
computation_wrapper.go
Sync plan after compilation for retry supportpkg/frontend/computation_wrapper.go
Run()execution inTxnComputationWrapper.Run()1 files
plan.pb.go
Update protobuf generated code commentspkg/pb/plan/plan.pb.go
Expr,TableDef_DefType,TransationControl,Plan,DataControl, andDataDefinitionmessages2 files
error.go
Add vector search retry error codepkg/common/moerr/error.go
ErrVectorNeedRetryWithPreMode(22301) in Group 15:Vector Search
error_no_ctx.go
Add no-context error constructor for vector retrypkg/common/moerr/error_no_ctx.go
NewVectorNeedRetryWithPreModeNoCtx()function to create errorwithout context
1 files
variables.go
Add session variable for auto mode defaultpkg/frontend/variables.go
enable_vector_auto_mode_by_defaultwithdefault value 0
specified in query
1 files
plan.proto
Update protobuf documentation for RankOptionproto/plan.proto
modes