diff --git a/faiss/IndexIDMap.cpp b/faiss/IndexIDMap.cpp index f2817ad4d..1a17df305 100644 --- a/faiss/IndexIDMap.cpp +++ b/faiss/IndexIDMap.cpp @@ -228,14 +228,14 @@ void IndexIDMapTemplate::range_search( typename IndexT::distance_t radius, RangeSearchResult* result, const SearchParameters* params) const { - if (params) { + if (params && params->sel) { SearchParameters internal_search_parameters; IDSelectorTranslated id_selector_translated(id_map, params->sel); internal_search_parameters.sel = &id_selector_translated; index->range_search(n, x, radius, result, &internal_search_parameters); } else { - index->range_search(n, x, radius, result); + index->range_search(n, x, radius, result, params); } const idx_t id_map_size = static_cast(id_map.size()); diff --git a/tests/test_search_params.py b/tests/test_search_params.py index c92446293..37fbb5a4f 100644 --- a/tests/test_search_params.py +++ b/tests/test_search_params.py @@ -337,6 +337,32 @@ class TestSelector(unittest.TestCase): np.testing.assert_array_equal(Iref, Inew) np.testing.assert_array_almost_equal(Dref, Dnew, decimal=5) + def test_idmap_range_empty_params(self): + # This test crashes without checking both params && params->sel + # are non null in IndexIDMap::range_search(). + index = faiss.IndexIDMap(faiss.IndexFlatL2(2)) + + xb = np.array( + [ + [0, 0], + [1, 0], + [0, 2], + ], + dtype="float32", + ) + ids = np.array([10, 20, 30], dtype="int64") + index.add_with_ids(xb, ids) + xq = np.array([[0, 0]], dtype="float32") + + lims_ref, D_ref, I_ref = index.range_search(xq, 5.0) + lims, D, I = index.range_search( + xq, 5.0, params=faiss.SearchParameters() + ) + + np.testing.assert_array_equal(lims_ref, lims) + np.testing.assert_array_equal(sorted(I_ref), sorted(I)) + np.testing.assert_array_equal(sorted(D_ref), sorted(D)) + def test_bounds(self): # https://github.com/facebookresearch/faiss/issues/3156 d = 64 # dimension