Repository navigation
Fix numeric $in and $nin metadata filters - #139
Open
OsamaAnsar wants to merge 1 commit into
Open
OsamaAnsar wants to merge 1 commit into
OsamaAnsar wants to merge 1 commit into
Conversation
$in and $nin only compared a numeric metadata value against the string
elements of the filter array, and only as a substring. As a result
{ year: { $in: [2019, 2020] } } matched nothing, $nin with numbers
excluded nothing, a number matched as a substring of a string element
(42 matched 'x42y'), and $nin excluded strings that were substrings of
an element ('b' was excluded by ['abc']).
Both operators now test exact membership with strict equality. Booleans
still never match, and array-valued metadata matches when any element
is in the filter array ($in) or excluded when any element is ($nin).
The two existing $in/$nin tests asserted the old substring behavior
("matches implementation"), so they are replaced with tests for the
documented semantics.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was wrong
In
ItemSelector.metadataFilter,$in/$ninonly ever compared a numeric metadata value against the string elements of the filter array, and only as a substring (val.includes(value)). Numbers in the filter array were never compared. The$ninbranch also applied the substring test to string values. This looks like a leftover from the array-metadata fixes in #36 / #55.types.tsdocuments both operators as(number | string)[]anddocs/api-reference.mdsays "Value in array".Observed on current
main(builtlib/, Node 22):After this change the same script prints
okfor every case ande2e startPos $in [0,1]: matches = 5,e2e endPos $nin [20]: matches = 0.The fix
$in/$ninnow test exact membership (Array.prototype.includes, strict equality) via a small private helperisInList:42does not match'42'or'x42y')categories: ['one']case from $in filter not working as expected #35 / Fix $in filter #36 / Fix issue with $nin query similar to $in fix from PR #36 #55) keeps working and now works for any number of elements:$inmatches if any element is in the filter array,$ninexcludes if any element is. Previously this only worked for single-element arrays, by way of the array being coerced to a string byincludes.Tests: two existing tests were changed
Two existing characterization tests in
src/ItemSelector.spec.tsasserted the old behavior and are replaced by this PR, because they directly encode the bug:$in behavior matches implementation (... numbers via substring in array strings)asserted that{ val: 42 }matches$in: ['x42y', 'baz'].$nin behavior matches implementation (...)asserted that{ val: 'oo' }is excluded by$nin: ['foobar', 'baz'], and (labelled "implementation detail") that{ val: 42 }is kept by$nin: [42].They are replaced by tests for exact string membership, numeric membership, "substring no longer matches", array-valued metadata, missing fields, and an end-to-end
listItemsByMetadata/queryItemstest inLocalIndex.spec.ts. If you would rather keep the old substring semantics for strings, I am happy to narrow this to numbers only, but substring matching for$ninlooks unintentional to me.Verification:
yarn lint: 0 errors (warnings in the three changed.tsfiles: 12 onmain, 10 here)yarn test: 598 passingsrc/ItemSelector.tsreverted tomain, 5 of the new tests fail ($in compares numbers by value,$nin excludes only exact members ...,$nin compares numbers by value,$in and $nin match array-valued metadata ..., andsupports numeric $in and $nin filters); with the fix restored all 598 pass.Docs:
docs/changelog.md(new Unreleased entry),llms.txt, and the operator table indocs/api-reference.mdare updated.CONTRIBUTING suggests opening an issue for bugs; I went straight to a PR with the repro above, but I'm happy to open an issue as well if you prefer.