RoanHeNaN commented on PR #70552:
URL: https://github.com/apache/doris/pull/70552#issuecomment-7180856143

   Follow-up review, based on the target semantics we would like Doris to 
provide. This supersedes the TopN part of my earlier comment: under these 
semantics, sparse segments no longer need either the brute-force fallback or 
the NULL-fill proposal.
   
   ### 0. Target semantics
   
   - ANN indexes may be built on Nullable `ARRAY<FLOAT>`.
   - `[]` and NULL are both accepted on write, with no BE config.
   - Vectors containing NULL elements are rejected.
   - `[]` and NULL are "missing vectors": they are stored as written but never 
recalled by vector search.
   
   | Value | Write | In ANN index | Scalar distance | Range `dist < r` | TopN 
`ORDER BY dist LIMIT k` |
   |---|---|---|---|---|---|
   | valid vector | ✅ | ✅ | value | by distance | recalled |
   | `[]` | ✅ (no config) | ❌ | NULL | never matches | **not recalled** |
   | NULL (nullable column) | ✅ | ❌ | NULL | never matches | **not recalled** |
   | `[1, NULL, 3]` | ❌ error | — | error | — | — |
   | non-empty, wrong dim | ❌ error | — | error | — | — |
   
   Storage keeps the original value: `SELECT vec` still returns `[]` or NULL. 
Only the index treats both as missing.
   
   ### 1. Write path
   
   1. Remove `enable_ann_index_empty_array` and every check or message that 
depends on it.
   2. `AnnIndexColumnWriter::add_array_values`: always skip `count == 0` rows. 
Keep the dimension and NULL-element checks for non-empty rows.
   3. **Outer NULL rows.** `add_nulls` / `add_array_nulls` currently return 
"Ann index should not be used on nullable column". They must instead treat 
those rows as missing vectors.
      - The catch: `feed_array_index` (`array_index_input.cpp`) calls 
`add_array_values` **before** `add_array_nulls`, and the payload under a NULL 
row is not guaranteed to be empty (e.g. `if(cond, NULL, arr)`).
      - So the writer has to know the outer null map while it handles 
`add_array_values`. Otherwise it may index garbage, or reject a NULL row on the 
dimension check.
      - Suggest passing the outer null map into `add_array_values`, or 
reordering the two calls. The BUILD INDEX path (`index_builder.cpp`) needs the 
same fix.
   4. Group Commit:
      - Drop the "`[]` requires config" check. Keep the dimension and 
NULL-element checks, but skip rows whose outer value is NULL. 
`_validate_ann_vectors` currently applies `remove_nullable` and would misjudge 
them.
      - Consider moving this validation into `OlapTableBlockConvertor` so 
regular loads and Group Commit share one implementation.
   5. MoW partial update needs nothing extra. Omitted vectors are filled with 
NULL (nullable column) or `[]` (NOT NULL column), and both are accepted by (2) 
and (3).
   
   ### 2. Storage / index
   
   Keep the existing machinery, keyed on "missing vector" (`[]` or NULL) 
instead of "empty array":
   
   - sparse row IDs: IVF `add_with_ids`, HNSW wrapped in `IndexIDMap`, the 
writer's row-ID buffer;
   - all-missing segments;
   - complement range search intersecting with `_indexed_rows`.
   
   ### 3. Query path (largest change)
   
   **3.1 TopN: exclude missing vectors instead of falling back.**
   
   - The ANN path already satisfies "not recalled", because FAISS only returns 
indexed rows. So the sparse-segment fallback in 
`SegmentIterator::_apply_ann_topn_predicate` (`ntotal != rows_of_segment`) can 
be **deleted**, and sparse segments use ANN normally.
   - Every brute-force path must give the same result: segments without an 
index, segments below `ann_index_build_min_segment_rows`, and filters or common 
exprs that force fallback. With ASC defaulting to NULLS FIRST, missing vectors 
would otherwise be sorted to the top.
   
   To make the result independent of the plan, I suggest:
   
   - **FE**: for TopN ordered by `l2_distance_approximate` / 
`inner_product_approximate`, add an implicit `dist IS NOT NULL` filter before 
`PushDownVectorTopNIntoOlapScan`, so the semantics hold whether or not the TopN 
is pushed down.
   - **BE**: once pushed down, recognize `IS NOT NULL` on the ANN virtual 
column as implied by ANN results and skip it on the ANN path. Otherwise it 
counts as a common expr and forces fallback. On fallback it is evaluated 
normally.
   
   The simpler alternative (exclude missing vectors only in the pushed-down 
path) makes results depend on the plan, so I would avoid it. Either way, 
"approximate-distance TopN implicitly drops NULL distances" is Doris-specific 
behavior and needs to be agreed on and documented. In a plain projection the 
functions still return NULL.
   
   **3.2 Range search.** No change. A NULL comparison never matches, and the 
complement already excludes missing rows, so index and scalar paths agree.
   
   **3.3 NULL / `[]` query vector.** Every distance is NULL, so TopN and range 
search should return an empty result directly instead of the current 
`has_missing_query_vector()` brute-force fallback. With 3.1 in place the 
fallback would also return empty; this is only an optimization.
   
   **3.4 Nullable column on the query path.** Audit BE code that `assert_cast`s 
the indexed column to `ColumnArray` assuming NOT NULL: brute-force reads, 
virtual column materialization, and the code around `extract_query_vector`.
   
   ### 4. Distance functions
   
   Keep the current change: the 7 array distance functions return nullable 
FLOAT, with NULL for NULL/`[]` input; FE constant folding matches; the MAP 
overload of `inner_product` is unchanged. NULL is now a legal stored value, so 
this is required.
   
   ### 5. FE
   
   - Remove the "ANN index must be built on a column that is not nullable" 
check in `IndexDefinition.checkColumn` (both overloads), for CREATE TABLE, 
CREATE INDEX and ALTER.
   - Add the implicit TopN filter from 3.1.
   - Confirm `PushDownVectorTopNIntoOlapScan` pushes down nullable slots. It 
already strips the MV `Nullable` wrapper.
   - Docs: the semantics table above, plus the implicit TopN filter.
   
   ### 6. Suggest splitting out
   
   These are independent of the semantics above and easier to review on their 
own:
   
   1. Range-search `<=`/`<` boundary fix plus the cache-key change. Resolve the 
open DOUBLE-to-FLOAT threshold issue there.
   2. Rejecting NULL elements in the ANN writer. This is a standalone bug fix 
and can be backported.
   3. Group Commit partial-acknowledgement atomicity (the open P1).
   
   ### 7. Tests
   
   - **Remove**: `setBeConfigTemporary([enable_ann_index_empty_array: true])` 
and the config-off rejection cases, e.g. 
`IndexBuilderTest.BuildAnnIndexRejectsAllEmptyBlock`.
   - **Add**:
     - nullable ANN column with mixed NULL / `[]` / valid rows, for HNSW, IVF 
and IVF_ON_DISK;
     - NULL rows whose array payload is non-empty, e.g. `INSERT ... SELECT 
if(cond, NULL, arr)`; such rows must not be indexed;
     - TopN never returns missing vectors on all three paths: ANN, brute-force 
fallback (a segment below the min-rows threshold), and not pushed down (e.g. 
with a join);
     - a profile check that sparse segments use ANN 
(`ann_fall_back_brute_force_cnt == 0`);
     - range search including the complement direction (`>`, `>=`);
     - MoW partial update: NULL fill (nullable column), `[]` fill (NOT NULL 
column), sequence-column losers, partial update of a deleted key, flexible 
partial update;
     - results before and after cumulative compaction;
     - Group Commit with NULL rows, `[]` and NULL elements.
   - **Regenerate** the `.out` files. Rows that used to sort first under NULLS 
FIRST must no longer appear in TopN results.
   
   ### 8. Open decisions
   
   1. Implicit TopN filter in FE (plan-independent, recommended) versus 
pushed-down path only.
   2. How to change the `add_array_values` interface: an extra outer-null-map 
parameter, or reordering it with `add_array_nulls`. This also affects the 
inverted-index writers.
   
   Points 1.3 and 3.4 come from static reading and have not been checked at 
runtime.
   


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