apache / apache/beam

AvroRecordSchema().toRowFunction throws a NPE when avro record contain nulls

Open
#19,329 0 comments 0 reactions 0 assignees View on GitHub
bug core java P3
Dominant language
Java
Stars
8.7k
Forks
4.7k
Avg merge
1d 20h
Merged PRs (30d)
196

Description

When generating a `SerializableFunction[SomeClass, Row]`
where `Someclass` was generated and has nullable fields, the generated function will throw a NPE.

For example given the following avro schema:
```

{
"type": "record",
"name": "TestRecord",
"namespace": "com.spotify.scio.avro",

"doc": "Record for testing",
"fields": [
{"name": "int_field", "type": ["null", "int"],
"default": null},
{"name": "long_field", "type": ["null", "long"], "default": null},

{"name": "float_field", "type": ["null", "float"], "default": null},
{"name": "double_field",
"type": ["null", "double"], "default": null},
{"name": "boolean_field", "type": ["null", "boolean"],
"default": null},
{"name": "string_field", "type": ["null", "string"], "default": null},

{"name": "array_field", "type": {"type": "array", "items": "string"}, "default": null}
]
}

```

the following code witl throw:
```

val clazz = classTag[avro.TestRecord].runtimeClass.asInstanceOf[Class[avro.TestRecord]]
val
toRow = new AvroRecordSchema().toRowFunction(TypeDescriptor.of(clazz))
avroWithNullable.map(x =>
toRow(x)).foreach(println)

```

Stacktrace:
```

[info] java.lang.NullPointerException:
[info] at com.spotify.scio.avro.FieldValueGetter$SchemaCodeGen$4CLQpLwl.get(Unknown
Source)
[info] at com.spotify.scio.avro.FieldValueGetter$SchemaCodeGen$4CLQpLwl.get(Unknown Source)
[info]
at org.apache.beam.sdk.values.RowWithGetters.lambda$getValues$2(RowWithGetters.java:117)
[info]
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
[info] at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382)

```

Imported from Jira [BEAM-6742](https://issues.apache.org/jira/browse/BEAM-6742). Original Jira may contain additional context.
Reported by: jto.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.