fix: query mismatch with non-intersecting OR and nested AND condition (#5452)

#### What type of PR is this?
/kind bug
/area core
/milestone 2.14.x

#### What this PR does / why we need it:
修复了索引查询使用含有 OR 条件嵌套 AND 条件时若匹配数据集间无交集导致的查询结果不正确的问题

如伪查询:`or(query, and(otherQuery1, otherQuery2))`
问题描述:当 `query` 匹配的结果与 `and(otherQuery1, otherQuery2)` 匹配的结果无交集时 and 会将自身不匹配的数据剔除导致 `query` 无法匹配到而出现缺少数据的问题


#### Does this PR introduce a user-facing change?

```release-note
None
```
pull/5462/head
guqing 2024-03-06 17:24:08 +08:00 committed by GitHub
parent 71d97b20c0
commit 5281eb01bb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 2 deletions

View File

@ -25,8 +25,6 @@ public class And extends LogicalQuery {
NavigableSet<String> resultSet = null; NavigableSet<String> resultSet = null;
for (Query query : childQueries) { for (Query query : childQueries) {
NavigableSet<String> currentResult = query.matches(indexView); NavigableSet<String> currentResult = query.matches(indexView);
// Trim unneeded rows to shrink the dataset for the next query
indexView.removeByIdNotIn(currentResult);
if (resultSet == null) { if (resultSet == null) {
resultSet = Sets.newTreeSet(currentResult); resultSet = Sets.newTreeSet(currentResult);
} else { } else {

View File

@ -94,4 +94,23 @@ public class AndTest {
var resultSet = query.matches(indexView); var resultSet = query.matches(indexView);
assertThat(resultSet).containsExactly("100"); assertThat(resultSet).containsExactly("100");
} }
@Test
void orAndMatch() {
var indexView = IndexViewDataSet.createEmployeeIndexView();
// test the case when the data matched by the query does not intersect with the data
// matched by the and query
// or(query, and(otherQuery1, otherQuery2))
var query = or(
// matched with id 101
and(equal("lastName", "Day"), equal("managerId", "102")),
// matched with id 100, 103
and(
equal("hireDate", "17"),
greaterThan("salary", "1800")
)
);
var resultSet = query.matches(indexView);
assertThat(resultSet).containsExactly("100", "101", "103");
}
} }