Apache Lucene open-source search software
Accumulate join Total/Avg scores in double precision (#16378)
Luca Cavannad96acfdfc095
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 9a00afb326e..5ae094c6ed6 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -360,8 +360,14 @@ Bug Fixesto the wrapped NumericDocValues, enabling optimized range evaluation forsingle-valued sorted numeric fields. (Costin Leau)+<<<<<<< join_util_scores+* GITHUB#16378: Accumulate join Total/Avg scores in double precision in+ TermsWithScoreCollector and JoinUtil's numeric point join, fixing+ intermittent failures caused by non-associative float addition. (Luca Cavanna)+=======* GITHUB#16296: Fix missing null check in RamUsageEstimator.sizeOf(Accountable)to be consistent with sizeOf(String) and sizeOf(Accountable[]). (Tim Grein)+>>>>>>> mainOther---------------------diff --git a/lucene/join/src/java/org/apache/lucene/search/join/JoinUtil.java b/lucene/join/src/java/org/apache/lucene/search/join/JoinUtil.javaindex e30aab554d0..df9271fd0fc 100644--- a/lucene/join/src/java/org/apache/lucene/search/join/JoinUtil.java+++ b/lucene/join/src/java/org/apache/lucene/search/join/JoinUtil.java@@ -35,9 +35,9 @@ import org.apache.lucene.index.SortedNumericDocValues;import org.apache.lucene.index.SortedSetDocValues;import org.apache.lucene.internal.hppc.LongArrayList;import org.apache.lucene.internal.hppc.LongCursor;-import org.apache.lucene.internal.hppc.LongFloatHashMap;import org.apache.lucene.internal.hppc.LongHashSet;import org.apache.lucene.internal.hppc.LongIntHashMap;+import org.apache.lucene.internal.hppc.LongObjectHashMap;import org.apache.lucene.search.Collector;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.MatchNoDocsQuery;@@ -149,7 +149,8 @@ public final class JoinUtil {ScoreMode scoreMode)throws IOException {LongHashSet joinValues = new LongHashSet();- LongFloatHashMap aggregatedScores = new LongFloatHashMap();+ // Maps a join value to its aggregated score cell; see ScoreCell for why double precision.+ LongObjectHashMap<ScoreCell> aggregatedScores = new LongObjectHashMap<>();LongIntHashMap occurrences = new LongIntHashMap();boolean needsScore = scoreMode != ScoreMode.None;LongFloatProcedure scoreAggregator;@@ -158,10 +159,12 @@ public final class JoinUtil {(key, score) -> {int index = aggregatedScores.indexOf(key);if (index < 0) {- aggregatedScores.indexInsert(index, key, score);+ ScoreCell cell = new ScoreCell();+ cell.value = score;+ aggregatedScores.indexInsert(index, key, cell);} else {- float currentScore = aggregatedScores.indexGet(index);- aggregatedScores.indexReplace(index, Math.max(currentScore, score));+ ScoreCell cell = aggregatedScores.indexGet(index);+ cell.value = Math.max(cell.value, score);}};} else if (scoreMode == ScoreMode.Min) {@@ -169,18 +172,37 @@ public final class JoinUtil {(key, score) -> {int index = aggregatedScores.indexOf(key);if (index < 0) {- aggregatedScores.indexInsert(index, key, score);+ ScoreCell cell = new ScoreCell();+ cell.value = score;+ aggregatedScores.indexInsert(index, key, cell);} else {- float currentScore = aggregatedScores.indexGet(index);- aggregatedScores.indexReplace(index, Math.min(currentScore, score));+ ScoreCell cell = aggregatedScores.indexGet(index);+ cell.value = Math.min(cell.value, score);}};} else if (scoreMode == ScoreMode.Total) {- scoreAggregator = aggregatedScores::addTo;+ scoreAggregator =+ (key, score) -> {+ int index = aggregatedScores.indexOf(key);+ if (index < 0) {+ ScoreCell cell = new ScoreCell();+ cell.value = score;+ aggregatedScores.indexInsert(index, key, cell);+ } else {+ aggregatedScores.indexGet(index).value += score;+ }+ };} else if (scoreMode == ScoreMode.Avg) {scoreAggregator =(key, score) -> {- aggregatedScores.addTo(key, score);+ int index = aggregatedScores.indexOf(key);+ if (index < 0) {+ ScoreCell cell = new ScoreCell();+ cell.value = score;+ aggregatedScores.indexInsert(index, key, cell);+ } else {+ aggregatedScores.indexGet(index).value += score;+ }occurrences.addTo(key, 1);};} else {@@ -194,12 +216,12 @@ public final class JoinUtil {if (scoreMode == ScoreMode.Avg) {joinScorer =(joinValue) -> {- float aggregatedScore = aggregatedScores.get(joinValue);+ double aggregatedScore = aggregatedScores.get(joinValue).value;int occurrence = occurrences.get(joinValue);- return aggregatedScore / occurrence;+ return (float) (aggregatedScore / occurrence);};} else {- joinScorer = aggregatedScores::get;+ joinScorer = (joinValue) -> (float) aggregatedScores.get(joinValue).value;}Collector collector;@@ -583,6 +605,15 @@ public final class JoinUtil {searcher.getTopReaderContext().id());}+ /**+ * A mutable double precision score accumulator, kept in double rather than float since float+ * addition isn't associative: summing the same per-document scores in a different order may round+ * to a different float.+ */+ private static final class ScoreCell {+ double value;+ }+/** Similar to {@link java.util.function.LongFunction} for primitive argument and result. */private interface LongFloatFunction {diff --git a/lucene/join/src/java/org/apache/lucene/search/join/TermsWithScoreCollector.java b/lucene/join/src/java/org/apache/lucene/search/join/TermsWithScoreCollector.javaindex 84252a7b15a..315f8fe85b1 100644--- a/lucene/join/src/java/org/apache/lucene/search/join/TermsWithScoreCollector.java+++ b/lucene/join/src/java/org/apache/lucene/search/join/TermsWithScoreCollector.java@@ -34,15 +34,17 @@ abstract class TermsWithScoreCollector<DV> extends DocValuesTermsCollector<DV>final ScoreMode scoreMode;Scorable scorer;- float[] scoreSums = new float[INITIAL_ARRAY_SIZE];+ // Accumulated in double precision because float addition isn't associative, so summing the+ // same per-document scores in a different order may round to a different float.+ double[] scoreSums = new double[INITIAL_ARRAY_SIZE];TermsWithScoreCollector(Function<DV> docValuesCall, ScoreMode scoreMode) {super(docValuesCall);this.scoreMode = scoreMode;if (scoreMode == ScoreMode.Min) {- Arrays.fill(scoreSums, Float.POSITIVE_INFINITY);+ Arrays.fill(scoreSums, Double.POSITIVE_INFINITY);} else if (scoreMode == ScoreMode.Max) {- Arrays.fill(scoreSums, Float.NEGATIVE_INFINITY);+ Arrays.fill(scoreSums, Double.NEGATIVE_INFINITY);}}@@ -53,7 +55,11 @@ abstract class TermsWithScoreCollector<DV> extends DocValuesTermsCollector<DV>@Overridepublic float[] getScoresPerTerm() {- return scoreSums;+ float[] scores = new float[scoreSums.length];+ for (int i = 0; i < scoreSums.length; i++) {+ scores[i] = (float) scoreSums[i];+ }+ return scores;}@Override@@ -119,16 +125,16 @@ abstract class TermsWithScoreCollector<DV> extends DocValuesTermsCollector<DV>int begin = scoreSums.length;scoreSums = ArrayUtil.grow(scoreSums);if (scoreMode == ScoreMode.Min) {- Arrays.fill(scoreSums, begin, scoreSums.length, Float.POSITIVE_INFINITY);+ Arrays.fill(scoreSums, begin, scoreSums.length, Double.POSITIVE_INFINITY);} else if (scoreMode == ScoreMode.Max) {- Arrays.fill(scoreSums, begin, scoreSums.length, Float.NEGATIVE_INFINITY);+ Arrays.fill(scoreSums, begin, scoreSums.length, Double.NEGATIVE_INFINITY);}}}float current = scorer.score();- float existing = scoreSums[ord];- if (Float.compare(existing, 0.0f) == 0) {+ double existing = scoreSums[ord];+ if (Double.compare(existing, 0.0d) == 0) {scoreSums[ord] = current;} else {switch (scoreMode) {@@ -180,8 +186,8 @@ abstract class TermsWithScoreCollector<DV> extends DocValuesTermsCollector<DV>}float current = scorer.score();- float existing = scoreSums[ord];- if (Float.compare(existing, 0.0f) == 0) {+ double existing = scoreSums[ord];+ if (Double.compare(existing, 0.0d) == 0) {scoreSums[ord] = current;scoreCounts[ord] = 1;} else {@@ -192,13 +198,11 @@ abstract class TermsWithScoreCollector<DV> extends DocValuesTermsCollector<DV>@Overridepublic float[] getScoresPerTerm() {- if (scoreCounts != null) {- for (int i = 0; i < scoreCounts.length; i++) {- scoreSums[i] = scoreSums[i] / scoreCounts[i];- }- scoreCounts = null;+ float[] scores = new float[scoreSums.length];+ for (int i = 0; i < scores.length; i++) {+ scores[i] = (float) (scoreSums[i] / scoreCounts[i]);}- return scoreSums;+ return scores;}}}@@ -222,9 +226,9 @@ abstract class TermsWithScoreCollector<DV> extends DocValuesTermsCollector<DV>int begin = scoreSums.length;scoreSums = ArrayUtil.grow(scoreSums);if (scoreMode == ScoreMode.Min) {- Arrays.fill(scoreSums, begin, scoreSums.length, Float.POSITIVE_INFINITY);+ Arrays.fill(scoreSums, begin, scoreSums.length, Double.POSITIVE_INFINITY);} else if (scoreMode == ScoreMode.Max) {- Arrays.fill(scoreSums, begin, scoreSums.length, Float.NEGATIVE_INFINITY);+ Arrays.fill(scoreSums, begin, scoreSums.length, Double.NEGATIVE_INFINITY);}}}@@ -278,13 +282,11 @@ abstract class TermsWithScoreCollector<DV> extends DocValuesTermsCollector<DV>@Overridepublic float[] getScoresPerTerm() {- if (scoreCounts != null) {- for (int i = 0; i < scoreCounts.length; i++) {- scoreSums[i] = scoreSums[i] / scoreCounts[i];- }- scoreCounts = null;+ float[] scores = new float[scoreSums.length];+ for (int i = 0; i < scores.length; i++) {+ scores[i] = (float) (scoreSums[i] / scoreCounts[i]);}- return scoreSums;+ return scores;}}}diff --git a/lucene/join/src/test/org/apache/lucene/search/join/TestJoinUtil.java b/lucene/join/src/test/org/apache/lucene/search/join/TestJoinUtil.javaindex 6ec736089ed..6e38a3d3323 100644--- a/lucene/join/src/test/org/apache/lucene/search/join/TestJoinUtil.java+++ b/lucene/join/src/test/org/apache/lucene/search/join/TestJoinUtil.java@@ -2046,7 +2046,10 @@ public class TestJoinUtil extends LuceneTestCase {float minScore = Float.POSITIVE_INFINITY;float maxScore = Float.NEGATIVE_INFINITY;- float total;+ // Accumulated in double precision, because float addition isn't associative, so summing the+ // same scores+ // in a different order may round to a different float.+ double total;int count;void addScore(float score) {@@ -2065,9 +2068,9 @@ public class TestJoinUtil extends LuceneTestCase {case None:return 1f;case Total:- return total;+ return (float) total;case Avg:- return total / count;+ return (float) (total / count);case Min:return minScore;case Max: