Skip to content

Commit a5714f6

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 a5714f6

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

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

Lines changed: 23 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,24 @@ public static Type fromString(String processorTag, String propertyName, String t
149152
);
150153
}
151154
}
155+
156+
private static boolean isExactIntegerFloat(Object value) {
157+
final float ABS_MAX_EXACT_FLOAT = 0x1p24-1;
158+
if (!(value instanceof Float)) {
159+
return false;
160+
}
161+
float v = ((Float) value).floatValue();
162+
return v == (long) v && -ABS_MAX_EXACT_FLOAT <= v && v <= ABS_MAX_EXACT_FLOAT;
163+
}
164+
165+
private static boolean isExactIntegerDouble(Object value) {
166+
final double ABS_MAX_EXACT_DOUBLE = 0x1p53-1;
167+
if (!(value instanceof Double)) {
168+
return false;
169+
}
170+
double v = ((Double) value).doubleValue();
171+
return v == (long) v && -ABS_MAX_EXACT_DOUBLE <= v && v <= ABS_MAX_EXACT_DOUBLE;
172+
}
152173
}
153174

154175
public static final String TYPE = "convert";

0 commit comments

Comments
 (0)