Skip to content

Commit de41d57

Browse files
authored
ESQL: Fix precision of scaled_float field values retrieved from stored source (#122586)
1 parent 521f855 commit de41d57

File tree

11 files changed

+35
-27
lines changed

11 files changed

+35
-27
lines changed

docs/changelog/122586.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 122586
2+
summary: "ESQL: Fix inconsistent results in using scaled_float field"
3+
area: ES|QL
4+
type: bug
5+
issues:
6+
- 122547

modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/ScaledFloatFieldMapper.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,7 @@ public BlockLoader blockLoader(BlockLoaderContext blContext) {
321321
return BlockLoader.CONSTANT_NULLS;
322322
}
323323
if (hasDocValues()) {
324-
double scalingFactorInverse = 1d / scalingFactor;
325-
return new BlockDocValuesReader.DoublesBlockLoader(name(), l -> l * scalingFactorInverse);
324+
return new BlockDocValuesReader.DoublesBlockLoader(name(), l -> l / scalingFactor);
326325
}
327326
if (isSyntheticSource) {
328327
return new FallbackSyntheticSourceBlockLoader(fallbackSyntheticSourceBlockLoaderReader(), name()) {

modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/ScaledFloatFieldBlockLoaderTests.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,6 @@ public ScaledFloatFieldBlockLoaderTests() {
2626
protected Double convert(Number value, Map<String, Object> fieldMapping) {
2727
var scalingFactor = ((Number) fieldMapping.get("scaling_factor")).doubleValue();
2828

29-
var docValues = (boolean) fieldMapping.getOrDefault("doc_values", false);
30-
31-
// There is a slight inconsistency between values that are read from doc_values and from source.
32-
// Due to how precision reduction is applied to source values so that they are consistent with doc_values.
33-
// See #122547.
34-
if (docValues) {
35-
var reverseScalingFactor = 1d / scalingFactor;
36-
return Math.round(value.doubleValue() * scalingFactor) * reverseScalingFactor;
37-
}
38-
3929
// Adjust values coming from source to the way they are stored in doc_values.
4030
// See mapper implementation.
4131
return Math.round(value.doubleValue() * scalingFactor) / scalingFactor;

x-pack/plugin/esql/qa/testFixtures/src/main/java/org/elasticsearch/xpack/esql/CsvTestUtils.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -660,8 +660,6 @@ private static StringBuilder trimOrPad(StringBuilder buffer) {
660660

661661
private static double scaledFloat(String value, String factor) {
662662
double scalingFactor = Double.parseDouble(factor);
663-
// this extra division introduces extra imprecision in the following multiplication, but this is how ScaledFloatFieldMapper works.
664-
double scalingFactorInverse = 1d / scalingFactor;
665-
return new BigDecimal(value).multiply(BigDecimal.valueOf(scalingFactor)).longValue() * scalingFactorInverse;
663+
return new BigDecimal(value).multiply(BigDecimal.valueOf(scalingFactor)).longValue() / scalingFactor;
666664
}
667665
}

x-pack/plugin/esql/qa/testFixtures/src/main/resources/floats.csv-spec

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,39 @@ a:double | b:double
88
;
99

1010
inDouble
11+
required_capability: fix_precision_of_scaled_float_fields
1112
from employees | keep emp_no, height, height.float, height.half_float, height.scaled_float | where height in (2.03, 2.0299999713897705, 2.029296875, 2.0300000000000002) | sort emp_no;
1213

1314
emp_no:integer |height:double |height.float:double |height.half_float:double |height.scaled_float:double
14-
10001 |2.03 |2.0299999713897705 |2.029296875 |2.0300000000000002
15-
10090 |2.03 |2.0299999713897705 |2.029296875 |2.0300000000000002
15+
10001 |2.03 |2.0299999713897705 |2.029296875 |2.03
16+
10090 |2.03 |2.0299999713897705 |2.029296875 |2.03
1617
;
1718

1819
inFloat
20+
required_capability: fix_precision_of_scaled_float_fields
1921
from employees | keep emp_no, height, height.float, height.half_float, height.scaled_float | where height.float in (2.03, 2.0299999713897705, 2.029296875, 2.0300000000000002) | sort emp_no;
2022

2123
emp_no:integer |height:double |height.float:double |height.half_float:double |height.scaled_float:double
22-
10001 |2.03 |2.0299999713897705 |2.029296875 |2.0300000000000002
23-
10090 |2.03 |2.0299999713897705 |2.029296875 |2.0300000000000002
24+
10001 |2.03 |2.0299999713897705 |2.029296875 |2.03
25+
10090 |2.03 |2.0299999713897705 |2.029296875 |2.03
2426
;
2527

2628
inHalfFloat
29+
required_capability: fix_precision_of_scaled_float_fields
2730
from employees | keep emp_no, height, height.float, height.half_float, height.scaled_float | where height.half_float in (2.03, 2.0299999713897705, 2.029296875, 2.0300000000000002) | sort emp_no;
2831

2932
emp_no:integer |height:double |height.float:double |height.half_float:double |height.scaled_float:double
30-
10001 |2.03 |2.0299999713897705 |2.029296875 |2.0300000000000002
31-
10090 |2.03 |2.0299999713897705 |2.029296875 |2.0300000000000002
33+
10001 |2.03 |2.0299999713897705 |2.029296875 |2.03
34+
10090 |2.03 |2.0299999713897705 |2.029296875 |2.03
3235
;
3336

3437
inScaledFloat
38+
required_capability: fix_precision_of_scaled_float_fields
3539
from employees | keep emp_no, height, height.float, height.half_float, height.scaled_float | where height.scaled_float in (2.03, 2.0299999713897705, 2.029296875, 2.0300000000000002) | sort emp_no;
3640

3741
emp_no:integer |height:double |height.float:double |height.half_float:double |height.scaled_float:double
38-
10001 |2.03 |2.0299999713897705 |2.029296875 |2.0300000000000002
39-
10090 |2.03 |2.0299999713897705 |2.029296875 |2.0300000000000002
42+
10001 |2.03 |2.0299999713897705 |2.029296875 |2.03
43+
10090 |2.03 |2.0299999713897705 |2.029296875 |2.03
4044
;
4145

4246
convertFromDouble

x-pack/plugin/esql/qa/testFixtures/src/main/resources/keep.csv-spec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,11 @@ height:double | languages.long:long | still_hired:boolean
266266
;
267267

268268
simpleEvalWithSortAndLimitOne
269+
required_capability: fix_precision_of_scaled_float_fields
269270
from employees | eval x = languages + 7 | sort x, avg_worked_seconds | limit 1;
270271

271272
avg_worked_seconds:long | birth_date:date | emp_no:integer | first_name:keyword | gender:keyword | height:double | height.float:double | height.half_float:double | height.scaled_float:double | hire_date:date | is_rehired:boolean | job_positions:keyword | languages:integer | languages.byte:integer | languages.long:long | languages.short:integer | last_name:keyword | salary:integer | salary_change:double | salary_change.int:integer | salary_change.keyword:keyword |salary_change.long:long | still_hired:boolean | x:integer
272-
208374744 |1956-11-14T00:00:00.000Z|10033 |null |M |1.63 |1.6299999952316284|1.6298828125 |1.6300000000000001 |1987-03-18T00:00:00.000Z|true |null |1 |1 |1 |1 |Merlo |70011 |null |null |null |null |false |8
273+
208374744 |1956-11-14T00:00:00.000Z|10033 |null |M |1.63 |1.6299999952316284|1.6298828125 |1.63 |1987-03-18T00:00:00.000Z|true |null |1 |1 |1 |1 |Merlo |70011 |null |null |null |null |false |8
273274
;
274275

275276
evalOfAverageValue

x-pack/plugin/esql/qa/testFixtures/src/main/resources/mv_expand.csv-spec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,10 +319,11 @@ a | a
319319

320320
//see https://github.com/elastic/elasticsearch/issues/103331
321321
keepStarMvExpand#[skip:-8.12.99]
322+
required_capability: fix_precision_of_scaled_float_fields
322323
from employees | where emp_no == 10001 | keep * | mv_expand first_name;
323324

324325
avg_worked_seconds:long | birth_date:date | emp_no:integer | first_name:keyword | gender:keyword | height:double | height.float:double | height.half_float:double | height.scaled_float:double | hire_date:date | is_rehired:boolean | job_positions:keyword | languages:integer | languages.byte:integer | languages.long:long | languages.short:integer | last_name:keyword | salary:integer | salary_change:double | salary_change.int:integer | salary_change.keyword:keyword | salary_change.long:long | still_hired:boolean
325-
268728049 | 1953-09-02T00:00:00.000Z | 10001 | Georgi | M | 2.03 | 2.0299999713897705 | 2.029296875 | 2.0300000000000002 | 1986-06-26T00:00:00.000Z | [false, true] | [Accountant, Senior Python Developer] | 2 | 2 | 2 | 2 | Facello | 57305 | 1.19 | 1 | 1.19 | 1 | true
326+
268728049 | 1953-09-02T00:00:00.000Z | 10001 | Georgi | M | 2.03 | 2.0299999713897705 | 2.029296875 | 2.03 | 1986-06-26T00:00:00.000Z | [false, true] | [Accountant, Senior Python Developer] | 2 | 2 | 2 | 2 | Facello | 57305 | 1.19 | 1 | 1.19 | 1 | true
326327
;
327328

328329

x-pack/plugin/esql/qa/testFixtures/src/main/resources/rename.csv-spec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,12 @@ y:integer | x:date
159159
;
160160

161161
renameIntertwinedWithSort
162+
required_capability: fix_precision_of_scaled_float_fields
162163
FROM employees | eval x = salary | rename x as y | rename y as x | sort x | rename x as y | limit 10;
163164

164165
avg_worked_seconds:l | birth_date:date | emp_no:i | first_name:s | gender:s | height:d | height.float:d | height.half_float:d | height.scaled_float:d| hire_date:date | is_rehired:bool | job_positions:s | languages:i | languages.byte:i | languages.long:l | languages.short:i | last_name:s | salary:i | salary_change:d | salary_change.int:i | salary_change.keyword:s | salary_change.long:l | still_hired:bool | y:i
165166

166-
390266432 | 1959-08-19T00:00:00.000Z | 10015 | Guoxiang | null | 1.66 | 1.659999966621399 | 1.66015625 | 1.6600000000000001 | 1987-07-02T00:00:00.000Z | [false, false, false, true]| [Head Human Resources, Junior Developer, Principal Support Engineer, Support Engineer] | 5 | 5 | 5 | 5 | Nooteboom | 25324 | [12.4, 14.25] | [12, 14] | [12.40, 14.25] | [12, 14] | true | 25324
167+
390266432 | 1959-08-19T00:00:00.000Z | 10015 | Guoxiang | null | 1.66 | 1.659999966621399 | 1.66015625 | 1.66 | 1987-07-02T00:00:00.000Z | [false, false, false, true]| [Head Human Resources, Junior Developer, Principal Support Engineer, Support Engineer] | 5 | 5 | 5 | 5 | Nooteboom | 25324 | [12.4, 14.25] | [12, 14] | [12.40, 14.25] | [12, 14] | true | 25324
167168
203838153 | 1953-02-08T00:00:00.000Z | 10035 | null | M | 1.81 | 1.809999942779541 | 1.8095703125 | 1.81 | 1988-09-05T00:00:00.000Z | false | [Data Scientist, Senior Python Developer] | 5 | 5 | 5 | 5 | Chappelet | 25945 | [-6.58, -2.54] | [-6, -2] | [-2.54, -6.58] | [-6, -2] | false | 25945
168169
313407352 | 1964-10-18T00:00:00.000Z | 10092 | Valdiodio | F | 1.75 | 1.75 | 1.75 | 1.75 | 1989-09-22T00:00:00.000Z | [false, false, true, true] | [Accountant, Junior Developer] | 1 | 1 | 1 | 1 | Niizuma | 25976 | [-6.77, 0.39, 8.3, 8.78] | [-6, 0, 8, 8] | [-6.77, 0.39, 8.30,8.78] | [-6, 0, 8, 8] | false | 25976
169170
248451647 | null | 10048 | Florian | M | 2.0 | 2.0 | 2.0 | 2.0 | 1985-02-24T00:00:00.000Z | [true, true] | Internship | 3 | 3 | 3 | 3 | Syrotiuk | 26436 | null | null | null | null | false | 26436

x-pack/plugin/esql/qa/testFixtures/src/main/resources/stats_count_distinct.csv-spec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ h:long
5252
;
5353

5454
countDistinctOfScaledFloat
55+
required_capability: fix_precision_of_scaled_float_fields
5556
// float becomes double until https://github.com/elastic/elasticsearch-internal/issues/724
5657
from employees | stats h = count_distinct(height.scaled_float);
5758

x-pack/plugin/esql/qa/testFixtures/src/main/resources/string.csv-spec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,10 +734,11 @@ null
734734
;
735735

736736
convertFromFloats
737+
required_capability: fix_precision_of_scaled_float_fields
737738
from employees | sort emp_no| eval double = to_string(height), float = to_string(height.float), scaled_float = to_string(height.scaled_float), half_float = to_string(height.half_float) | keep emp_no, double, float, scaled_float, half_float, height | limit 2;
738739

739740
emp_no:integer |double:keyword |float:keyword |scaled_float:keyword |half_float:keyword |height:double
740-
10001 |2.03 |2.0299999713897705|2.0300000000000002 |2.029296875 |2.03
741+
10001 |2.03 |2.0299999713897705|2.03 |2.029296875 |2.03
741742
10002 |2.08 |2.0799999237060547|2.08 |2.080078125 |2.08
742743
;
743744

0 commit comments

Comments
 (0)