RoanHeNaN commented on PR #70552:
URL: https://github.com/apache/doris/pull/70552#issuecomment-7163423291
Some notes on the NULL / empty-vector semantics in this PR, plus a proposal
for the sparse TopN path.
### 1. Semantics as implemented (please confirm this is the intent)
- Write side: only `[]` is newly accepted, and only with
`enable_ann_index_empty_array=true`. A whole-array NULL is still impossible
because ANN columns must be NOT NULL. `[1, NULL, 3]` is now rejected on every
write path; previously the ANN writer did not check `null_map` and indexed the
payload under the NULL.
- Query side: the 7 array distance/similarity functions return NULL whenever
either side is NULL or `[]`, including `[]` vs `[]`. NULL elements still raise
an error, and the element check runs before the empty check, so
`l2_distance([1,NULL], [])` still errors. FE constant folding
(`ArrayArithmetic.cosineSimilarity`) behaves the same way.
Two side effects of this that I think should be called out explicitly:
- `l2_distance([1,2], [])` used to fail with a dimension mismatch and now
returns NULL. A dimension error involving `[]` is silently turned into a
missing value.
- The return type changes from FLOAT to Nullable(FLOAT).
`FunctionBuilderImpl::build` (`be/src/exprs/function/function.h`) strictly
compares FE and BE return types, nullability included. During a rolling
upgrade, any query using these functions fails with "function return type check
failed", whether or not the table has an ANN index. Given the `dev/4.1.x` /
`dev/4.2.x` labels, I don't think this part should be backported as is.
### 2. Sparse segments disable ANN TopN entirely
`segment_iterator.cpp` (`_apply_ann_topn_predicate`):
```cpp
if (cast_set<size_t>(ann_index_reader->get_num_rows_in_index()) !=
rows_of_segment) {
_column_states[src_cid].need_read_data = true;
_opts.stats->ann_fall_back_brute_force_cnt += 1;
return Status::OK();
}
```
- A single `[]` row makes the whole segment fall back to brute force. In
practice empty vectors are scattered across loads, so almost every segment is
sparse and ANN TopN is effectively never used.
- The user cannot work around it. The check compares FAISS `ntotal` with the
physical row count, so neither `WHERE size(v) > 0` nor `NULLS LAST` avoids it.
- The check runs after the index has already been loaded, so these segments
pay for both the index load and the brute-force scan.
- `ORDER BY l2_distance_approximate(...) LIMIT k` defaults to NULLS FIRST,
so the empty rows come first. That is correct SQL, but users won't expect it.
Range search is fine: NULL never satisfies a comparison, and the complement
path already intersects with `_indexed_rows`.
### 3. Proposal: fill NULL rows instead of falling back
NULL distances are mutually interchangeable, so the correct per-segment
top-k is always a subset of:
```
FAISS top-k over non-empty vectors ∪ any min(e, k) empty rows (e =
|_row_bitmap − indexed_rows|)
```
The global TopN above the scan is kept (`PushDownVectorTopNIntoOlapScan`
returns `topN.withChildren(...)`), so it does the final NULLS FIRST/LAST
ordering. A segment would emit at most 2k rows.
Sketch:
1. Run `evaluate_vector_ann_search` as usual.
2. If the segment is sparse, compute `empty_rows = _row_bitmap −
indexed_rows`. Reusing `_init_indexed_rows()` keeps the delete bitmap and
earlier index filters.
3. Append the selected empty rows to `_row_bitmap` / `row_ids`, with NULL
entries in the nullable distance column.
`VirtualColumnIterator::prepare_materialization` already accepts unsorted
labels. The result cache stays as is, since filling happens after the cached
FAISS result.
To keep approximate recall from turning into NULL pollution, use
`ann_sort_info.nulls_first`. `TSortInfo` field 3 is already sent, but
`olap_scan_operator.cpp` only reads `is_asc_order`:
| Order | Empty rows to add | Skip FAISS? |
|---|---|---|
| NULLS FIRST | `min(e, k)` | yes, when `e >= k` |
| NULLS LAST | `k − n` only when `n = \|_row_bitmap ∩ indexed_rows\| < k`
(based on the candidate count, not on how many rows FAISS returned) | no |
Tests worth adding: NULLS FIRST/LAST, `e < k` and `e >= k`, with a delete
bitmap / pre-filter, and HNSW + IVF.
--
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]