morningman opened a new issue, #70970:
URL: https://github.com/apache/doris/issues/70970

   ### Background
   
   Lance datasets carry vector indexes (IVF_FLAT, IVF_PQ, IVF_SQ, IVF_HNSW_*) 
and full-text (inverted) indexes. branch-4.1 and branch-4.2 expose them through 
two table functions, `vector_search(...)` and `full_text_search(...)`: each 
reads a table of a `lance` catalog, returns the table's columns plus 
`_distance` or `_score`, splits the search into one range per index segment 
(and per unindexed fragment), and lets a Doris TopN merge the ranges' 
candidates. Large columns can be fetched after the merge by row location 
(two-phase lazy materialization), and `vector_search` works in server-side 
prepared statements.
   
   On master, Lance is a connector plugin 
(`fe/fe-connector/fe-connector-lance`) and fe-core must hold no Lance code, so 
the 4.x implementation, which lives in fe-core, cannot be copied. master's BE 
already executes Lance searches (`TLanceScanParams.external_search_request`); 
the FE side, the fixtures and the suites are missing.
   
   ### Current behavior
   
   master has neither function:
   
   ```sql
   SELECT row_id, _distance
   FROM vector_search("table" = "lance_ctl.doris.vs_ivf_pq_f32", "column" = 
"embedding",
                      "query_vector" = 
"[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]", "top_k" = "5");
   ```
   
   fails at analysis with `Can not found function 'vector_search'` 
(`FunctionRegistry`), and the same holds for `full_text_search`.
   
   ### Expected behavior
   
   The statement above returns the five nearest rows with their `_distance`, as 
on branch-4.2, with the same argument names, defaults, limits, error messages 
and EXPLAIN keys; `full_text_search` returns rows ranked by `_score`.
   
   Three behaviors of the 4.x implementation are not carried over, because they 
return wrong rows or leak data:
   
   - Every filter that ends up right above the merge TopN is moved below it on 
4.x, so it filters the candidates instead of the result. Besides the WHERE 
written on the function, that catches an outer WHERE over the user's own `ORDER 
BY ... LIMIT`, the filters of a CTE used twice, a WHERE over a join of the 
search and predicates the optimizer infers, and only when no column pruning 
separates the TopN from the function, so the rows depend on the select list. 
Measured on the master port before it changed this: `SELECT v.*, w.row_id AS 
w_row_id FROM vector_search(... "top_k" = "3", "use_index" = "false") v JOIN 
lance_ctl.doris.vs_ivf_pq_f32 w ON v.row_id = w.row_id WHERE w.row_id > 2` 
returns rows 3, 513 and 514; selecting three columns of `v` instead returns row 
3, which is right.
   - Row policies, data masks and column privileges of the searched table are 
not applied: a user whose row policy is `row_id > 2` reads rows 1, 2, 3 and 7 
through `full_text_search`, and rows 3 and 4 through a scan.
   
   ### Proposed design
   
   A generic, optional connector SPI, so that the engine owns the two functions 
and any connector can serve them:
   
   - `ConnectorSearchOps#prepareSearch(session, tableHandle, 
ConnectorSearchRequest)` on the connector metadata. Implementing the interface 
is the declaration, as with `ConnectorPassthroughSqlOps` and 
`ConnectorFileSourceOps`. The request carries the function 
(`ConnectorSearchFunction.VECTOR_SEARCH` / `FULL_TEXT_SEARCH`), every argument 
except `table` under its lower-case name, and the names deferred to EXECUTE by 
a PREPARE. The answer (`ConnectorSearch`) holds the scan handle, the columns 
ending with the result column, `top_k`, `offset`, and whether the engine may 
fetch top-level columns after the merge (`ConnectorSearchLazyMaterialization`).
   - `ConnectorMetadata#applyTopnLazyMaterialization(session, handle, 
lazyColumns)`, an overload that tells the connector which columns the engine 
fetches by row id.
   - fe-core resolves `table` (a fully qualified name), checks SELECT on it, 
routes to the table's catalog when its connector implements the SPI (otherwise 
`Catalog 'x' does not support vector_search()`), and wraps the relation in a 
TopN ordered by the result column (`_distance` ascending, `_score` descending). 
It requires SELECT on every column of the table, since the arguments (the 
searched column, a `filter`) may read any, and refuses the search while a row 
policy or a data mask applies to the user, since a search can apply neither. 
While the statement is analyzed, the WHERE written on the function (`FROM 
vector_search(...) [alias] WHERE ...`) moves below the merge TopN and filters 
the candidates; no rewrite rule moves any other filter there, so all others 
apply to the result. Lazy materialization is admitted as the connector says, 
and which arguments may be `?` in a PREPARE is the connector's decision.
   - The Lance connector implements the SPI with branch-4.2's argument rules, 
index selection and split planning, and plans lazy materialization only when 
every backend reads Lance row locations, which a new BE exec version 20 
announces.
   
   Alternatives rejected: letting plugins register table function names (one 
name could then belong to one plugin only, while Paimon is to serve 
`vector_search` too, branch-4.2 #69493); having the engine parse the common 
arguments (Lance and Paimon differ in ranges and in what `filter` means); 
reusing exec version 19 (master BEs built before the Lance row-location reader 
landed already report 19); keeping 4.x's rewrite-time filter rule (its results 
depend on plan shape, see Expected behavior); applying row policies and masks 
to the search (a policy predicate is Doris SQL while the search's own `filter` 
is in the connector's dialect, so the engine cannot make the search consider 
only the rows the policy admits; filtering the candidates or the result instead 
ranks rows among rows the user may not read).
   
   ### Impact
   
   Users of `lance` catalogs on builds from master. Shared-nothing and cloud 
modes behave the same (the feature has no per-mode logic). No existing 
statement, catalog or table changes behavior.
   
   ### Reproduction or evidence
   
   The statement in Current behavior, against the `doris.vs_ivf_pq_f32` 
regression fixture 
(`docker/thirdparties/docker-compose/iceberg/scripts/preinstalled_data/lance`).
   
   ### Scope and non-goals
   
   In scope: branch-4.2's search as of the port snapshot plus distance bounds 
(`distance_lower_bound` / `distance_upper_bound`, #68815) and `prefilter` 
(#68813). Out of scope, for later work: searching historical versions, tags and 
branches (#68707), `approx_mode` (#69852; master's BE does not read it yet), 
and the other changes after the snapshot (#69625).
   
   ### Acceptance criteria
   
   - branch-4.2's search regression suites pass on master with identical `.out` 
results: `test_lance_vector_search`, `_two_phase`, `_element_types`, 
`_distance_range`, `test_lance_full_text_search`, 
`test_lance_prepared_vector_search`, and the two-phase section of 
`test_lance_runtime_filter_pushdown`; then the coverage suites over the large 
fixtures (`_index_types`, `_ivf_flat`, `_index_matrix`, 
`_indexed_element_types`, `_metrics`, the multivector suites).
   - A suite pins which filters filter the candidates and that a row policy 
refuses the search for its user only.
   - Unit tests for the SPI, the engine routing, binding, filter placement and 
privilege checks, and the Lance argument parsing, encoding and split planning.
   
   ### Risks and compatibility
   
   - Rolling upgrade: below BE exec version 20 the Lance connector plans no 
lazy materialization and the search keeps the plain TopN, with the same rows.
   - The connector API version moves from 36.0 to 37.0 (new SPI surface), so 
plugins built against 36.0 are refused by the version gate, as for every 
surface change.
   - No Thrift or Protobuf change; the BE request fields already exist on 
master.
   - Users coming from 4.1 or 4.2 see the two differences under Expected 
behavior: filters other than the function's own WHERE apply to the result, and 
a search under a row policy, a data mask or a column denial is refused.
   
   ### Related
   
   - branch-4.2 implementation: #65730, #66512, #66841, #67039, #67289, #68028, 
#68162, #68813, #68815
   - The Lance connector on master: #70680
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to