Scale doubles to floats when necessary to match the field (#78344)#8
Scale doubles to floats when necessary to match the field (#78344)#8MitchLewis930 wants to merge 1 commit intopr_018_beforefrom
Conversation
) This fixes a bug where the range aggregation always treats the range end points as doubles, even if the field value doesn't have enough resolution to fill a double. This was creating issues where the range would have a "more precise" approximation of an unrepresentable number than the field, causing the value to fall in the wrong bucket. Note 1: This does not resolve the case where we have a long value that is not precisely representable as a double. Since the wire format sends the range bounds as doubles, by the time we get to where this fix is operating, we've already lost the precision to act on a big long. Fixing that problem will require a wire format change, and I'm not convinced it's worth it right now. Note 2: This is probably still broken for ScaledFloats, since they don't implement NumberFieldType. Resolves elastic#77033
PR Compliance Guide 🔍Below is a summary of compliance checks for this PR:
Compliance status legend🟢 - Fully Compliant🟡 - Partial Compliant 🔴 - Not Compliant ⚪ - Requires Further Human Verification 🏷️ - Compliance label |
||||||||||||||||||||||||
PR Code Suggestions ✨Explore these optional code suggestions:
|
||||||||||||
User description
PR_018
PR Type
Bug fix
Description
Add
reduceToStoredPrecision()method toNumberFieldTypeto adjust double values to field's precisionApply precision reduction to range aggregation bounds before bucketing
Fix float/double precision mismatch causing values to fall in wrong buckets
Add comprehensive tests for float, double, and half_float range aggregations
Diagram Walkthrough
File Walkthrough
NumberFieldMapper.java
Add precision reduction method to NumberFieldTypeserver/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java
reduceToStoredPrecision(double)method toNumberFieldTypeclassfield's precision
ValuesSourceConfig.java
Add precision reduction function accessorserver/src/main/java/org/elasticsearch/search/aggregations/support/ValuesSourceConfig.java
NumberFieldMapperandDoubleUnaryOperatorreduceToStoredPrecisionFunction()method that returns precisionadjustment function
NumberFieldTypeNumberFieldType.reduceToStoredPrecision()when applicableRangeAggregationBuilder.java
Apply precision reduction to range boundsserver/src/main/java/org/elasticsearch/search/aggregations/bucket/range/RangeAggregationBuilder.java
DoubleUnaryOperatorfor precision adjustment functionValuesSourceConfigfromandto) beforeprocessing ranges
RangeAggregatorTests.java
Add precision tests and clean up importsserver/src/test/java/org/elasticsearch/search/aggregations/bucket/range/RangeAggregatorTests.java
DirectoryReader,IndexReader,IndexSearcher,Directory)NumericUtilsimport for double-to-sortable-long conversionendpoint exclusivity
(Consumer>)in existingtests for clarity
testCase()helper method (moved to parent class)20_typed_keys.yml
Update float range test expectationsrest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/msearch/20_typed_keys.yml
19.2499999to19.25to matchprecision-adjusted value
"*-19.2499999"to"*-19.25"inassertions
40_range.yml
Add float and half_float range aggregation testsrest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/search.aggregation/40_range.yml
floatandhalf_floatfield type mappings to test indexbucketing
half_float range bucketing
precision-adjusted boundaries