Skip to content

Commit

Permalink
default null array to empty array (#32604)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedabu98 authored Oct 1, 2024
1 parent aadb695 commit 301286f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
Expand Down Expand Up @@ -272,6 +273,8 @@ private static Object messageValueFromRowValue(
if (value == null) {
if (fieldDescriptor.isOptional()) {
return null;
} else if (fieldDescriptor.isRepeated()) {
return Collections.emptyList();
} else {
throw new IllegalArgumentException(
"Received null value for non-nullable field " + fieldDescriptor.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.Collections;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.beam.sdk.schemas.Schema;
Expand Down Expand Up @@ -71,6 +72,7 @@ public class BeamRowToStorageApiProtoTest {
.addField("booleanValue", FieldType.BOOLEAN.withNullable(true))
.addField("bytesValue", FieldType.BYTES.withNullable(true))
.addField("arrayValue", FieldType.array(FieldType.STRING))
.addField("arrayNullValue", FieldType.array(FieldType.STRING).withNullable(true))
.addField("iterableValue", FieldType.array(FieldType.STRING))
.addField("sqlDateValue", FieldType.logicalType(SqlTypes.DATE).withNullable(true))
.addField("sqlTimeValue", FieldType.logicalType(SqlTypes.TIME).withNullable(true))
Expand Down Expand Up @@ -168,43 +170,50 @@ public class BeamRowToStorageApiProtoTest {
.build())
.addField(
FieldDescriptorProto.newBuilder()
.setName("iterablevalue")
.setName("arraynullvalue")
.setNumber(13)
.setType(Type.TYPE_STRING)
.setLabel(Label.LABEL_REPEATED)
.build())
.addField(
FieldDescriptorProto.newBuilder()
.setName("sqldatevalue")
.setName("iterablevalue")
.setNumber(14)
.setType(Type.TYPE_STRING)
.setLabel(Label.LABEL_REPEATED)
.build())
.addField(
FieldDescriptorProto.newBuilder()
.setName("sqldatevalue")
.setNumber(15)
.setType(Type.TYPE_INT32)
.setLabel(Label.LABEL_OPTIONAL)
.build())
.addField(
FieldDescriptorProto.newBuilder()
.setName("sqltimevalue")
.setNumber(15)
.setNumber(16)
.setType(Type.TYPE_INT64)
.setLabel(Label.LABEL_OPTIONAL)
.build())
.addField(
FieldDescriptorProto.newBuilder()
.setName("sqldatetimevalue")
.setNumber(16)
.setNumber(17)
.setType(Type.TYPE_INT64)
.setLabel(Label.LABEL_OPTIONAL)
.build())
.addField(
FieldDescriptorProto.newBuilder()
.setName("sqltimestampvalue")
.setNumber(17)
.setNumber(18)
.setType(Type.TYPE_INT64)
.setLabel(Label.LABEL_OPTIONAL)
.build())
.addField(
FieldDescriptorProto.newBuilder()
.setName("enumvalue")
.setNumber(18)
.setNumber(19)
.setType(Type.TYPE_STRING)
.setLabel(Label.LABEL_OPTIONAL)
.build())
Expand All @@ -225,6 +234,7 @@ public class BeamRowToStorageApiProtoTest {
.withFieldValue("booleanValue", true)
.withFieldValue("bytesValue", BYTES)
.withFieldValue("arrayValue", ImmutableList.of("one", "two", "red", "blue"))
.withFieldValue("arrayNullValue", null)
.withFieldValue("iterableValue", ImmutableList.of("blue", "red", "two", "one"))
.withFieldValue("sqlDateValue", LocalDate.now())
.withFieldValue("sqlTimeValue", LocalTime.now())
Expand All @@ -248,6 +258,7 @@ public class BeamRowToStorageApiProtoTest {
.put("booleanvalue", true)
.put("bytesvalue", ByteString.copyFrom(BYTES))
.put("arrayvalue", ImmutableList.of("one", "two", "red", "blue"))
.put("arraynullvalue", Collections.emptyList())
.put("iterablevalue", ImmutableList.of("blue", "red", "two", "one"))
.put(
"sqldatevalue",
Expand Down

0 comments on commit 301286f

Please sign in to comment.