diff --git a/docs/changelog/141049.yaml b/docs/changelog/141049.yaml new file mode 100644 index 0000000000000..2e97fbd5ad87c --- /dev/null +++ b/docs/changelog/141049.yaml @@ -0,0 +1,5 @@ +pr: 141049 +summary: Use Double.compare to compare doubles in tdigest.Sort +area: Aggregations +type: bug +issues: [] diff --git a/libs/tdigest/src/main/java/org/elasticsearch/tdigest/Sort.java b/libs/tdigest/src/main/java/org/elasticsearch/tdigest/Sort.java index 1c54b0327690c..6abf24ee38e14 100644 --- a/libs/tdigest/src/main/java/org/elasticsearch/tdigest/Sort.java +++ b/libs/tdigest/src/main/java/org/elasticsearch/tdigest/Sort.java @@ -91,12 +91,15 @@ private static void stableQuickSort(TDigestIntArray order, TDigestDoubleArray va i++; } low++; - } else if (vi > pivotValue || (vi == pivotValue && pi > pv)) { - high--; - swap(order, i, high); } else { - // vi < pivotValue || (vi == pivotValue && pi < pv) - i++; + int c = Double.compare(vi, pivotValue); + if (c > 0 || (c == 0 && pi > pv)) { + high--; + swap(order, i, high); + } else { + // vi < pivotValue || (vi == pivotValue && pi < pv) + i++; + } } } // invariant: (values[order[k]],order[k]) == (pivotValue, pv) for k in [0..low) @@ -169,7 +172,8 @@ private static void stableInsertionSort(TDigestIntArray order, TDigestDoubleArra // values in [start, i) are ordered // scan backwards to find where to stick t for (int j = i; j >= m; j--) { - if (j == 0 || values.get(order.get(j - 1)) < v || (values.get(order.get(j - 1)) == v && (order.get(j - 1) <= vi))) { + int c = (j == 0) ? -1 : Double.compare(values.get(order.get(j - 1)), v); + if (c < 0 || (c == 0 && (order.get(j - 1) <= vi))) { if (j < i) { order.set(j + 1, order, j, i - j); order.set(j, t);