Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fastserde] Fix issue with generating schema fingerprint when NaN is default value. #511

Merged
merged 1 commit into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"type": "record",
"name": "FloatWithDefaultNaN",
"namespace": "com.linkedin.avro.fastserde.generated.avro",
"doc": "Used to verify Utils.getSchemaFingerprint() method",
"fields": [
{
"name": "floatWithDefaultNaN",
"type": "float",
"default": "NaN"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@
import org.testng.Assert;
import org.testng.annotations.Test;

import com.linkedin.avro.fastserde.generated.avro.FloatWithDefaultNaN;


public class UtilsTest {

@Test (groups = "deserializationTest")
public void testGenerateSourcePathFromPackageName() {
Assert.assertEquals(Utils.generateSourcePathFromPackageName("com.linkedin.avro"), Utils.fixSeparatorsToMatchOS("/com/linkedin/avro/"));
}

@Test
void testFingerprintOfSchemaWithDefaultNaN() {
// expect no exception is thrown
Utils.getSchemaFingerprint(FloatWithDefaultNaN.SCHEMA$);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,20 +210,19 @@ public static boolean isRoundNumber(JsonNode node) {
*/
public static JsonNode enforceUniformNumericDefaultValues(Schema.Field field) {
JsonNode defaultValue = field.defaultValue();
BigDecimal numericValue = defaultValue.getDecimalValue();
Schema schema = field.schema();
// a default value for a union, must match the first element of the union
Schema.Type defaultType = schema.getType() == UNION ? schema.getTypes().get(0).getType() : schema.getType();

switch (defaultType) {
case INT:
if (!isAMathematicalInteger(numericValue)) {
if (!isAMathematicalInteger(defaultValue.getDecimalValue())) {
Comment on lines -220 to +219
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the fix? I don't understand... isn't the code equivalent?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If defaultType is DOUBLE or FLOAT and genericDefaultValue represents NaN then calling genericDefaultValue.decimalValue() is

  • not needed
  • throws NumberFormatException

In other words it's called too early.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooh I see. Ok, cool. Thanks for explaining, good catch.

LOGGER.warn(String.format("Invalid default value: %s for \"int\" field: %s", field.defaultValue(), field.name()));
return defaultValue;
}
return new IntNode(defaultValue.getNumberValue().intValue());
case LONG:
if (!isAMathematicalInteger(numericValue)) {
if (!isAMathematicalInteger(defaultValue.getDecimalValue())) {
LOGGER.warn(String.format("Invalid default value: %s for \"long\" field: %s", field.defaultValue(), field.name()));
return defaultValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,19 @@ public static boolean isRoundNumber(JsonNode node) {
* Enforces uniform numeric default values across Avro versions
*/
public static JsonNode enforceUniformNumericDefaultValues(Schema.Field field, JsonNode genericDefaultValue) {
BigDecimal numericDefaultValue = genericDefaultValue.decimalValue();
Schema schema = field.schema();
// a default value for a union, must match the first element of the union
Schema.Type defaultType = schema.getType() == UNION ? schema.getTypes().get(0).getType() : schema.getType();

switch (defaultType) {
case INT:
if (!isAMathematicalInteger(numericDefaultValue)) {
if (!isAMathematicalInteger(genericDefaultValue.decimalValue())) {
LOGGER.warn(String.format("Invalid default value: %s for \"int\" field: %s", genericDefaultValue, field.name()));
return genericDefaultValue;
}
return new IntNode(genericDefaultValue.intValue());
case LONG:
if (!isAMathematicalInteger(numericDefaultValue)) {
if (!isAMathematicalInteger(genericDefaultValue.decimalValue())) {
LOGGER.warn(String.format("Invalid default value: %s for \"long\" field: %s", genericDefaultValue, field.name()));
return genericDefaultValue;
}
Expand Down
Loading