thomasmueller commented on code in PR #2540:
URL: https://github.com/apache/jackrabbit-oak/pull/2540#discussion_r2379402723
##########
oak-core/src/main/java/org/apache/jackrabbit/oak/query/UnionQueryImpl.java:
##########
@@ -324,7 +324,21 @@ public Iterator<ResultRowImpl> getRows() {
rightIter = ((MeasuringIterator) rightRows).getDelegate();
}
if (orderBy == null) {
- it = IteratorUtils.chainedIterator(leftIter, rightIter);
+ boolean sortByScoreEnabled =
settings.isSortUnionQueryByScoreEnabled();
+ boolean leftHasScore = isScorePresent(left);
+ boolean rightHasScore = isScorePresent(right);
+
+ if (sortByScoreEnabled && leftHasScore && rightHasScore) {
+ // Both sides have scores => merge
+ Comparator<ResultRowImpl> scoreComparator =
createScoreBasedComparator();
+ it = IteratorUtils.mergeSorted(List.of(leftIter, rightIter),
scoreComparator);
+ } else if (sortByScoreEnabled && rightHasScore) {
+ // Only right has score => start with right
+ it = IteratorUtils.chainedIterator(rightIter, leftIter);
+ } else {
+ // Default old behavior, only left has score, or neither has
scores => start with left
+ it = IteratorUtils.chainedIterator(leftIter, rightIter);
+ }
Review Comment:
I usually start with the case where the feature is disabled, so that no new
code (that might have some bugs / forgotten edge cases) can cause issues;
something like this:
```suggestion
boolean sortByScoreEnabled =
settings.isSortUnionQueryByScoreEnabled();
if (!sortByScoreEnabled) {
it = IteratorUtils.chainedIterator(leftIter, rightIter);
} else {
boolean leftHasScore = isScorePresent(left);
boolean rightHasScore = isScorePresent(right);
if (leftHasScore && rightHasScore) {
// Both sides have scores => merge
Comparator<ResultRowImpl> scoreComparator =
createScoreBasedComparator();
it = IteratorUtils.mergeSorted(List.of(leftIter,
rightIter), scoreComparator);
} else if (rightHasScore) {
// Only right has score => start with right
it = IteratorUtils.chainedIterator(rightIter, leftIter);
} else {
// only left has score, or neither has scores => start
with left
it = IteratorUtils.chainedIterator(leftIter, rightIter);
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]