SOLR-14767 : Fix NumberFormatException when int/long field value is floating num (#1775)

This commit is contained in:
Apoorv Bhawsar 2020-09-29 19:23:33 +05:30 committed by GitHub
parent 1dba76c0d3
commit 63f0b6b706
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 55 additions and 12 deletions

View File

@ -251,6 +251,9 @@ Bug Fixes
* SOLR-14850: Fix ExactStatsCache NullPointerException when shards.tolerant=true.
(Yevhen Tienkaiev via ab)
* SOLR-14767: Fix NumberFormatException when integer/long field value is specified as floating number
(Apoorv Bhawsar, Munendra S N)
Other Changes
---------------------

View File

@ -51,8 +51,7 @@ public class IntPointField extends PointField implements IntValueFieldType {
try {
if (val instanceof CharSequence) return Integer.parseInt( val.toString());
} catch (NumberFormatException e) {
Float v = Float.parseFloat(val.toString());
return v.intValue();
return (int)Float.parseFloat(val.toString());
}
return super.toNativeType(val);
}
@ -146,7 +145,16 @@ public class IntPointField extends PointField implements IntValueFieldType {
@Override
public IndexableField createField(SchemaField field, Object value) {
int intValue = (value instanceof Number) ? ((Number) value).intValue() : Integer.parseInt(value.toString());
int intValue;
if (value instanceof Number) {
intValue = ((Number) value).intValue();
} else {
try {
intValue = Integer.parseInt(value.toString());
} catch (NumberFormatException e) {
intValue = (int) Float.parseFloat(value.toString());
}
}
return new IntPoint(field.getName(), intValue);
}

View File

@ -50,8 +50,7 @@ public class LongPointField extends PointField implements LongValueFieldType {
try {
if (val instanceof CharSequence) return Long.parseLong(val.toString());
} catch (NumberFormatException e) {
Double v = Double.parseDouble(val.toString());
return v.longValue();
return (long)Double.parseDouble(val.toString());
}
return super.toNativeType(val);
}
@ -151,7 +150,16 @@ public class LongPointField extends PointField implements LongValueFieldType {
@Override
public IndexableField createField(SchemaField field, Object value) {
long longValue = (value instanceof Number) ? ((Number) value).longValue() : Long.parseLong(value.toString());
long longValue;
if (value instanceof Number) {
longValue = ((Number) value).longValue();
} else {
try {
longValue = Long.parseLong(value.toString());
} catch (NumberFormatException e) {
longValue = (long) Double.parseDouble(value.toString());
}
}
return new LongPoint(field.getName(), longValue);
}

View File

@ -553,9 +553,16 @@ public class TrieField extends NumericFieldType {
switch (type) {
case INTEGER:
int i = (value instanceof Number)
? ((Number)value).intValue()
: Integer.parseInt(value.toString());
int i;
if (value instanceof Number) {
i = ((Number) value).intValue();
} else {
try {
i = Integer.parseInt(value.toString());
} catch (NumberFormatException e) {
i = (int) Float.parseFloat(value.toString());
}
}
f = new LegacyIntField(field.getName(), i, ft);
break;
case FLOAT:
@ -565,9 +572,16 @@ public class TrieField extends NumericFieldType {
f = new LegacyFloatField(field.getName(), fl, ft);
break;
case LONG:
long l = (value instanceof Number)
? ((Number)value).longValue()
: Long.parseLong(value.toString());
long l;
if (value instanceof Number) {
l = ((Number) value).longValue();
} else {
try {
l = Long.parseLong(value.toString());
} catch (NumberFormatException e) {
l = (long) Double.parseDouble(value.toString());
}
}
f = new LegacyLongField(field.getName(), l, ft);
break;
case DOUBLE:

View File

@ -238,4 +238,14 @@ public class TestUpdate extends SolrTestCaseJ4 {
fail();
}
@Test // SOLR-14767
public void testStringUpdateOnLongAndIntFields() throws Exception {
clearIndex();
addAndGetVersion(sdoc("id","1", "val_is", "42.0", "val_is", 12, "val_i", "12.1", "val_l",
"42.0", "val_ll", "12.1", "val_ll", "32"), null);
assertJQ(req("qt","/get", "id","1", "fl","id,val*")
,"=={'doc':{'id':'1', 'val_i':12, 'val_is':[42,12], 'val_l':42, 'val_ll':[12,32]}}"
);
}
}