Skip to content

Commit 12d34d5

Browse files
committed
Write struct extend unit test for struct array
1 parent 69765bf commit 12d34d5

2 files changed

Lines changed: 17 additions & 11 deletions

File tree

spark-3.1-spanner-lib/src/main/java/com/google/cloud/spark/spanner/SpannerWriterUtils.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ private static Value convertField(InternalRow row, int index, DataType type) {
268268

269269
final ArrayData arrayData = row.getArray(index);
270270
final int numElements = arrayData.numElements();
271+
if (numElements == 0) return Value.structArray(Type.struct(), null);
271272
List<Struct> convertedList = new ArrayList<>(numElements);
272273

273274
for (int j = 0; j < numElements; j++) {
@@ -280,10 +281,7 @@ private static Value convertField(InternalRow row, int index, DataType type) {
280281
}
281282
}
282283

283-
return Value.structArray(
284-
Type.struct(Type.StructField.of("field", Type.string())),
285-
convertedList); // TODO: the struct element type should represent the schema of the
286-
// struct.
284+
return Value.structArray(convertedList.get(0).getType(), convertedList);
287285
}
288286
}
289287

spark-3.1-spanner-lib/src/test/java/com/google/cloud/spark/spanner/SpannerWriterUtilsTest.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -355,23 +355,31 @@ public static Collection<Object[]> data() {
355355
{
356356
"struct_array",
357357
new StructType()
358-
.add(
359-
"field",
360-
DataTypes.StringType), // Add at least one field to match getStruct(i, 1)
358+
.add("str_field", DataTypes.StringType)
359+
.add("bool_field", DataTypes.BooleanType),
361360
new InternalRow[] {
362361
mock(InternalRow.class)
363362
}, // Use InternalRow instead of Spanner Struct
364363
Value.structArray(
365-
Type.struct(Type.StructField.of("field", Type.string())),
366-
Collections.singletonList(Struct.newBuilder().set("field").to("value").build())),
364+
Type.struct(
365+
Type.StructField.of("str_field", Type.string()),
366+
Type.StructField.of("bool_field", Type.bool())),
367+
Collections.singletonList(
368+
Struct.newBuilder()
369+
.set("str_field")
370+
.to("str_value")
371+
.set("bool_field")
372+
.to(true)
373+
.build())),
367374
(BiConsumer<ArrayData, Object>)
368375
(ad, d) ->
369376
setupArrayMock(
370377
ad,
371378
(InternalRow[]) d, // Cast to InternalRow array
372379
(i, val) -> {
373-
when(ad.getStruct(i, 1)).thenReturn((InternalRow) val);
374-
when(((InternalRow) val).getString(0)).thenReturn("value");
380+
when(ad.getStruct(i, 2)).thenReturn((InternalRow) val);
381+
when(((InternalRow) val).getString(0)).thenReturn("str_value");
382+
when(((InternalRow) val).getBoolean(1)).thenReturn(true);
375383
})
376384
}
377385
});

0 commit comments

Comments
 (0)