Repository navigation
fix(metadata): Infinity push-down must reject multi-valued-unsafe negative ops (≠, not in) - #16943
Conversation
…ative ops (≠, not in)
The Elasticsearch and Infinity metadata push-down translators are meant to be
interchangeable pre-filters that both mirror the in-memory `meta_filter`
fallback -- the shared test section is even titled "is_pushdown_supported
pre-check (same logic for both backends)". But the two `is_pushdown_supported`
implementations diverge: `metadata_es_filter` defines
`MULTIVALUE_UNSAFE_NEGATIVE_OPS = frozenset({"≠", "not in"})` and refuses
push-down for those operators, while `metadata_infinity_filter` has no such
guard and happily pushes them down.
Why that guard exists: `meta_fields.<key>` can hold a JSON array, and the
in-memory `meta_filter` matches a document when ANY of its values satisfies the
predicate (per-value-bucket semantics). A document whose `tag` is `[a, b]`
therefore still matches `tag ≠ a` -- bucket `b` satisfies it. The Infinity
push-down emits `NOT JSON_CONTAINS(meta_fields, '$.tag', '"a"')`, which means
"the array contains no `a` at all", so it silently drops that document. Same
divergence for `not in`. The ES backend avoids this by forcing the whole
request to the in-memory fallback; the Infinity backend did not, so
`tag ≠ a` / `tag not in (...)` under-counted results for any doc that has the
excluded value alongside others.
This is on a live production path: `DocMetadataService._filter_doc_ids_by_
metadata_infinity` (`api/db/services/doc_metadata_service.py:948`) calls this
exact `is_pushdown_supported` as the sole gate before building the Infinity
SQL, mirroring the ES branch at line 880 which uses the guarded ES version.
Fix: add the same `MULTIVALUE_UNSAFE_NEGATIVE_OPS` set to
`metadata_infinity_filter` and reject those operators in
`is_pushdown_supported`, so a single such filter forces the whole request to
the in-memory path -- the only place the per-bucket semantics are reproduced.
`not contains` is intentionally still allowed, matching the ES backend
(`all(not contains)` == `not any(contains)`, which the push-down expresses
correctly on multi-valued fields).
Verified by importing both real modules and confirming
`is_pushdown_supported([{op}])` now returns `False` for `≠` and `not in` on
both backends (previously ES=False, Infinity=True). Added
`test_pushdown_check_rejects_multivalue_unsafe_negative_ops`, which asserts
both backends reject these ops; confirmed red->green (fails on the pre-fix
Infinity module, passes after). `ruff check` / `ruff format --check` clean.
The `≠` / `not in` translators themselves are unchanged -- they remain correct
for the in-memory-fallback path; only the push-down eligibility gate is fixed.
Note: open PR infiniflow#16833 also edits `test/unit_test/common/test_metadata_filter.py`
but only appends at the end of the file (line 640+) and changes
`metadata_es_filter.py` / `metadata_utils.py`, not the Infinity module -- no
overlap with this change.
Signed-off-by: Andrew Chen <[email protected]>
Co-authored-by: Claude Opus 4.8 <[email protected]>
Signed-off-by: chuenchen309 <[email protected]>
📝 WalkthroughWalkthroughChangesMetadata pushdown eligibility
Estimated code review effort: 2 (Simple) | ~10 minutes Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #16943 +/- ##
==========================================
- Coverage 94.56% 93.16% -1.40%
==========================================
Files 10 10
Lines 717 717
Branches 118 118
==========================================
- Hits 678 668 -10
- Misses 25 29 +4
- Partials 14 20 +6 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Excellent root-cause analysis — the per-value-bucket quantifier shift is the crux and you nailed exactly why 1. Silent under-counting on negation is the worst-case asymmetry for audit scope. A privilege filter 2. The asymmetry is quantifier-level, not backend-level. Positive ops ( 3. 4. Companion — cross-backend parity assertion + evaluation-path stamp. The shared test section titled "same logic for both backends" shows the intent was parity, but the divergence survived because there was no runtime parity assertion on a multi-valued test corpus. A This connects to #16832 (ES |
|
Thanks @zeweihan — the audit-scope framing is spot on. Silent under-counting on |
|
@qinling0210 Would you please review this PR? |
What problem does this PR solve?
The Elasticsearch and Infinity metadata push-down translators are meant to be
interchangeable pre-filters that both mirror the in-memory
meta_filterfallback — the shared test section is even titled "is_pushdown_supported
pre-check (same logic for both backends)". But the two
is_pushdown_supportedimplementations diverge:
common/metadata_es_filter.pydefinesMULTIVALUE_UNSAFE_NEGATIVE_OPS = frozenset({"≠", "not in"})and refusespush-down for those operators.
common/metadata_infinity_filter.pyhas no such guard and pushes themdown.
Why the guard exists:
meta_fields.<key>can hold a JSON array, and thein-memory
meta_filtermatches a document when any of its values satisfiesthe predicate (per-value-bucket semantics). A document whose
tagis[a, b]therefore still matches
tag ≠ a— bucketbsatisfies it. The Infinitypush-down emits
NOT JSON_CONTAINS(meta_fields, '$.tag', '"a"'), which means"the array contains no
aat all", so it silently drops that document.Same divergence for
not in. The result:tag ≠ a/tag not in (...)under-counts results for any document that has the excluded value alongside
others, but only on the Infinity backend.
This is on a live production path:
DocMetadataService._filter_doc_ids_by_metadata_infinity(
api/db/services/doc_metadata_service.py:948) calls this exactis_pushdown_supportedas the sole gate before building the Infinity SQL,mirroring the ES branch at line 880 which uses the guarded ES version.
Reproduction (both real modules, no services needed):
Type of change
Fix
Add the same
MULTIVALUE_UNSAFE_NEGATIVE_OPSset tometadata_infinity_filterand reject those operators inis_pushdown_supported, so a single such filter forces the whole request tothe in-memory path — the only place the per-bucket semantics are reproduced.
not containsis intentionally still allowed, matching the ES backend(
all(not contains)==not any(contains), which the push-down expressescorrectly on multi-valued fields). The
≠/not intranslators themselvesare unchanged — they remain correct for the in-memory-fallback path; only the
push-down eligibility gate is fixed.
Testing
is_pushdown_supported([{op}])now returnsFalsefor≠andnot inon both backends (previously ES=False, Infinity=True).test_pushdown_check_rejects_multivalue_unsafe_negative_ops, whichasserts both backends reject these ops. Confirmed red→green: it fails against
the pre-fix Infinity module, passes after.
ruff check/ruff format --checkclean.Note on overlap
Open PR #16833 also edits
test/unit_test/common/test_metadata_filter.py, butonly appends at the end of the file (line 640+) and changes
metadata_es_filter.py/metadata_utils.py, not the Infinity module — nooverlap with this change.
Disclosure
AI-assisted (Claude Code): the divergence was surfaced by an AI-assisted review
pass, but I independently reproduced it against the real modules, confirmed the
production call path, and verified the fix and test before submitting.