Skip to content

Commit b817cfa

Browse files
authored
Tolerate floating point precision in multi node tests (#110238) (#110283)
Some assertions with floating-point values are failing on serverless because we run tests with three shards with serverless. This can cause variations in precision because data may arrive in different orders. For example, sum([a, b, c]) can yield a different precision than sum([a, c, b]). This change introduces tolerance for precision differences beyond e-10, which should be acceptable for ESQL.
1 parent 7aecf65 commit b817cfa

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

x-pack/plugin/esql/qa/server/mixed-cluster/src/javaRestTest/java/org/elasticsearch/xpack/esql/qa/mixed/MixedClusterEsqlSpecIT.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,9 @@ protected void shouldSkipTest(String testName) throws IOException {
7373
protected boolean supportsAsync() {
7474
return oldClusterHasFeature(ASYNC_QUERY_FEATURE_ID);
7575
}
76+
77+
@Override
78+
protected boolean enableRoundingDoubleValuesOnAsserting() {
79+
return true;
80+
}
7681
}

x-pack/plugin/esql/qa/server/multi-clusters/src/javaRestTest/java/org/elasticsearch/xpack/esql/ccq/MultiClusterSpecIT.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,9 @@ static boolean hasIndexMetadata(String query) {
231231
}
232232
return false;
233233
}
234+
235+
@Override
236+
protected boolean enableRoundingDoubleValuesOnAsserting() {
237+
return true;
238+
}
234239
}

x-pack/plugin/esql/qa/server/multi-node/src/javaRestTest/java/org/elasticsearch/xpack/esql/qa/multi_node/EsqlSpecIT.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,9 @@ protected String getTestRestCluster() {
2424
public EsqlSpecIT(String fileName, String groupName, String testName, Integer lineNumber, CsvTestCase testCase, Mode mode) {
2525
super(fileName, groupName, testName, lineNumber, testCase, mode);
2626
}
27+
28+
@Override
29+
protected boolean enableRoundingDoubleValuesOnAsserting() {
30+
return true;
31+
}
2732
}

x-pack/plugin/esql/qa/server/src/main/java/org/elasticsearch/xpack/esql/qa/rest/EsqlSpecTestCase.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
import org.junit.Before;
3131

3232
import java.io.IOException;
33+
import java.math.BigDecimal;
34+
import java.math.MathContext;
35+
import java.math.RoundingMode;
3336
import java.net.URL;
3437
import java.util.ArrayList;
3538
import java.util.List;
@@ -187,10 +190,10 @@ protected void assertResults(
187190
Logger logger
188191
) {
189192
assertMetadata(expected, actualColumns, logger);
190-
assertData(expected, actualValues, testCase.ignoreOrder, logger, EsqlSpecTestCase::valueMapper);
193+
assertData(expected, actualValues, testCase.ignoreOrder, logger, this::valueMapper);
191194
}
192195

193-
private static Object valueMapper(CsvTestUtils.Type type, Object value) {
196+
private Object valueMapper(CsvTestUtils.Type type, Object value) {
194197
if (value == null) {
195198
return "null";
196199
}
@@ -205,9 +208,30 @@ private static Object valueMapper(CsvTestUtils.Type type, Object value) {
205208
} catch (Throwable ignored) {}
206209
}
207210
}
211+
if (type == CsvTestUtils.Type.DOUBLE && enableRoundingDoubleValuesOnAsserting()) {
212+
if (value instanceof List<?> vs) {
213+
List<Object> values = new ArrayList<>();
214+
for (Object v : vs) {
215+
values.add(valueMapper(type, v));
216+
}
217+
return values;
218+
} else if (value instanceof Double d) {
219+
return new BigDecimal(d).round(new MathContext(10, RoundingMode.DOWN)).doubleValue();
220+
} else if (value instanceof String s) {
221+
return new BigDecimal(s).round(new MathContext(10, RoundingMode.DOWN)).doubleValue();
222+
}
223+
}
208224
return value.toString();
209225
}
210226

227+
/**
228+
* Rounds double values when asserting double values returned in queries.
229+
* By default, no rounding is performed.
230+
*/
231+
protected boolean enableRoundingDoubleValuesOnAsserting() {
232+
return false;
233+
}
234+
211235
private static String normalizedPoint(CsvTestUtils.Type type, double x, double y) {
212236
if (type == CsvTestUtils.Type.GEO_POINT) {
213237
return normalizedGeoPoint(x, y);

0 commit comments

Comments
 (0)