Fix filter_result_iterator_t::valid not updating seq_id properly for complex filter expressions.

This commit is contained in:
Harpreet Sangar 2023-05-17 14:44:27 +05:30
parent be9975ac1d
commit 90c2b4f7e9
3 changed files with 17 additions and 14 deletions

View File

@ -1047,14 +1047,6 @@ int filter_result_iterator_t::valid(uint32_t id) {
if (filter_node->isOperator) {
auto left_valid = left_it->valid(id), right_valid = right_it->valid(id);
if (left_it->is_valid && right_it->is_valid) {
seq_id = std::min(left_it->seq_id, right_it->seq_id);
} else if (left_it->is_valid) {
seq_id = left_it->seq_id;
} else if (right_it->is_valid) {
seq_id = right_it->seq_id;
}
if (filter_node->filter_operator == AND) {
is_valid = left_it->is_valid && right_it->is_valid;
@ -1063,9 +1055,20 @@ int filter_result_iterator_t::valid(uint32_t id) {
return -1;
}
// id did not match the filter but both of the sub-iterators are still valid.
// Updating seq_id to the next potential match.
if (left_valid == 0 && right_valid == 0) {
seq_id = std::max(left_it->seq_id, right_it->seq_id);
} else if (left_valid == 0) {
seq_id = left_it->seq_id;
} else if (right_valid == 0) {
seq_id = right_it->seq_id;
}
return 0;
}
seq_id = id;
return 1;
} else {
is_valid = left_it->is_valid || right_it->is_valid;
@ -1075,9 +1078,13 @@ int filter_result_iterator_t::valid(uint32_t id) {
return -1;
}
// id did not match the filter but both of the sub-iterators are still valid.
// Next seq_id match would be the minimum of the two.
seq_id = std::min(left_it->seq_id, right_it->seq_id);
return 0;
}
seq_id = id;
return 1;
}
}

View File

@ -209,11 +209,7 @@ bool or_iterator_t::take_id(result_iter_state_t& istate, uint32_t id, bool& is_e
}
if (istate.fit != nullptr && istate.fit->approx_filter_ids_length > 0) {
if (istate.fit->valid(id) == -1) {
return false;
}
if (istate.fit->seq_id == id) {
if (istate.fit->valid(id) == 1) {
istate.fit->next();
return true;
}

View File

@ -284,7 +284,7 @@ TEST_F(FilterTest, FilterTreeIterator) {
auto iter_validate_ids_test = filter_result_iterator_t(coll->get_name(), coll->_get_index(), filter_tree_root);
ASSERT_TRUE(iter_validate_ids_test.init_status().ok());
std::vector<int> validate_ids = {0, 1, 2, 3, 4, 5, 6}, seq_ids = {0, 2, 2, 3, 4, 5, 5};
std::vector<int> validate_ids = {0, 1, 2, 3, 4, 5, 6}, seq_ids = {0, 2, 2, 4, 4, 5, 5};
expected = {1, 0, 1, 0, 1, 1, -1};
for (uint32_t i = 0; i < validate_ids.size(); i++) {
ASSERT_EQ(expected[i], iter_validate_ids_test.valid(validate_ids[i]));