Skip to content

Commit 943aad2

Browse files
committed
Preserve integer-exact representation of numbers and allow conversion of floating point values to integers
This differentiates floating point values that are within the range of exact integer correspondence and renders them as integers rather than using the default java toString method which formats floating point values in E-notation when the absolute value is at least 1e6. The new formatting is used as the input string for the INTEGER and LONG convert methods enabling conversion from floating point to integer numbers.
1 parent dfe1357 commit 943aad2

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/ConvertProcessor.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ enum Type {
3434
@Override
3535
public Object convert(Object value) {
3636
try {
37-
String strValue = value.toString();
37+
String strValue = STRING.convert(value);
3838
if (strValue.startsWith("0x") || strValue.startsWith("-0x")) {
3939
return Integer.decode(strValue);
4040
}
@@ -49,7 +49,7 @@ public Object convert(Object value) {
4949
@Override
5050
public Object convert(Object value) {
5151
try {
52-
String strValue = value.toString();
52+
String strValue = STRING.convert(value);
5353
if (strValue.startsWith("0x") || strValue.startsWith("-0x")) {
5454
return Long.decode(strValue);
5555
}
@@ -102,6 +102,9 @@ public Object convert(Object value) {
102102
STRING {
103103
@Override
104104
public Object convert(Object value) {
105+
if (isExactIntegerDouble(value) || isExactIntegerFloat(value)) {
106+
return ((long)value).toString();
107+
}
105108
return value.toString();
106109
}
107110
},
@@ -149,6 +152,16 @@ public static Type fromString(String processorTag, String propertyName, String t
149152
);
150153
}
151154
}
155+
156+
private static final float ABS_MAX_EXACT_FLOAT = 0x1p24-1;
157+
private static boolean isExactIntegerFloat(Object value) {
158+
return value instanceof float && value == (long)value && -ABS_MAX_EXACT_FLOAT <= value && value <= ABS_MAX_EXACT_FLOAT;
159+
}
160+
161+
private static final double ABS_MAX_EXACT_DOUBLE = 0x1p53-1;
162+
private static boolean isExactIntegerDouble(Object value) {
163+
return value instanceof double && value == (long)value && -ABS_MAX_EXACT_DOUBLE <= value && value <= ABS_MAX_EXACT_DOUBLE;
164+
}
152165
}
153166

154167
public static final String TYPE = "convert";

0 commit comments

Comments
 (0)