Skip to content

Commit 0263b00

Browse files
fix: support ByteString values on repeated fields (#1996)
* fix: support ByteString values on repeated fields * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 1caec8f commit 0263b00

File tree

3 files changed

+148
-1
lines changed

3 files changed

+148
-1
lines changed

google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/JsonToProtoMessage.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,9 @@ private static void fillRepeatedField(
608608
}
609609
}
610610
if (!added) {
611-
if (val instanceof byte[]) {
611+
if (val instanceof ByteString) {
612+
protoMsg.addRepeatedField(fieldDescriptor, ((ByteString) val).toByteArray());
613+
} else if (val instanceof byte[]) {
612614
protoMsg.addRepeatedField(fieldDescriptor, val);
613615
} else if (val instanceof JSONArray) {
614616
try {

google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/JsonStreamWriterTest.java

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,20 @@
3333
import com.google.cloud.bigquery.storage.test.SchemaTest;
3434
import com.google.cloud.bigquery.storage.test.Test.FlexibleType;
3535
import com.google.cloud.bigquery.storage.test.Test.FooType;
36+
import com.google.cloud.bigquery.storage.test.Test.RepetitionType;
3637
import com.google.cloud.bigquery.storage.test.Test.UpdatedFooType;
3738
import com.google.cloud.bigquery.storage.v1.ConnectionWorkerPool.Settings;
3839
import com.google.cloud.bigquery.storage.v1.Exceptions.AppendSerializtionError;
3940
import com.google.cloud.bigquery.storage.v1.TableFieldSchema.Mode;
41+
import com.google.protobuf.ByteString;
4042
import com.google.protobuf.Descriptors.DescriptorValidationException;
4143
import com.google.protobuf.Int64Value;
4244
import com.google.protobuf.Timestamp;
4345
import io.grpc.Status;
4446
import io.grpc.StatusRuntimeException;
4547
import java.io.IOException;
48+
import java.math.BigDecimal;
49+
import java.math.RoundingMode;
4650
import java.util.Arrays;
4751
import java.util.Map;
4852
import java.util.UUID;
@@ -63,6 +67,7 @@
6367
@RunWith(JUnit4.class)
6468
public class JsonStreamWriterTest {
6569
private static final Logger LOG = Logger.getLogger(JsonStreamWriterTest.class.getName());
70+
private static int NUMERIC_SCALE = 9;
6671
private static final String TEST_STREAM = "projects/p/datasets/d/tables/t/streams/_default";
6772
private static final String TEST_STREAM_2 = "projects/p/datasets/d2/tables/t2/streams/_default";
6873
private static final String TEST_TABLE = "projects/p/datasets/d/tables/t";
@@ -329,6 +334,137 @@ public void testSpecialTypeAppend() throws Exception {
329334
}
330335
}
331336

337+
@Test
338+
public void testRepeatedByteStringAppend() throws Exception {
339+
TableFieldSchema NON_REPEATED_A =
340+
TableFieldSchema.newBuilder()
341+
.setType(TableFieldSchema.Type.NUMERIC)
342+
.setMode(TableFieldSchema.Mode.REQUIRED)
343+
.setName("a")
344+
.build();
345+
346+
TableFieldSchema NON_REPEATED_B =
347+
TableFieldSchema.newBuilder()
348+
.setType(TableFieldSchema.Type.BYTES)
349+
.setMode(TableFieldSchema.Mode.REQUIRED)
350+
.setName("b")
351+
.build();
352+
353+
TableFieldSchema NON_REPEATED_C =
354+
TableFieldSchema.newBuilder()
355+
.setType(TableFieldSchema.Type.BYTES)
356+
.setMode(TableFieldSchema.Mode.REQUIRED)
357+
.setName("c")
358+
.build();
359+
360+
TableFieldSchema REPEATED_A =
361+
TableFieldSchema.newBuilder()
362+
.setType(TableFieldSchema.Type.NUMERIC)
363+
.setMode(TableFieldSchema.Mode.REPEATED)
364+
.setName("aa")
365+
.build();
366+
367+
TableFieldSchema REPEATED_B =
368+
TableFieldSchema.newBuilder()
369+
.setType(TableFieldSchema.Type.BYTES)
370+
.setMode(TableFieldSchema.Mode.REPEATED)
371+
.setName("bb")
372+
.build();
373+
374+
TableFieldSchema REPEATED_C =
375+
TableFieldSchema.newBuilder()
376+
.setType(TableFieldSchema.Type.BYTES)
377+
.setMode(TableFieldSchema.Mode.REPEATED)
378+
.setName("cc")
379+
.build();
380+
381+
TableSchema tableSchema =
382+
TableSchema.newBuilder()
383+
.addFields(0, NON_REPEATED_A)
384+
.addFields(1, NON_REPEATED_B)
385+
.addFields(2, NON_REPEATED_C)
386+
.addFields(3, REPEATED_A)
387+
.addFields(4, REPEATED_B)
388+
.addFields(5, REPEATED_C)
389+
.build();
390+
391+
BigDecimal bigDecimal1 = new BigDecimal(1.1);
392+
if (bigDecimal1.scale() > NUMERIC_SCALE) {
393+
bigDecimal1 = bigDecimal1.setScale(NUMERIC_SCALE, RoundingMode.HALF_UP);
394+
}
395+
BigDecimal bigDecimal2 = new BigDecimal(2.2);
396+
if (bigDecimal2.scale() > NUMERIC_SCALE) {
397+
bigDecimal2 = bigDecimal2.setScale(NUMERIC_SCALE, RoundingMode.HALF_UP);
398+
}
399+
JSONArray aaValue = new JSONArray();
400+
aaValue.put(BigDecimalByteStringEncoder.encodeToNumericByteString(bigDecimal1));
401+
aaValue.put(BigDecimalByteStringEncoder.encodeToNumericByteString(bigDecimal2));
402+
403+
byte[] byteArray1 = "bb1".getBytes("UTF-8");
404+
byte[] byteArray2 = "bb2".getBytes("UTF-8");
405+
JSONArray bbValue = new JSONArray();
406+
bbValue.put(ByteString.copyFrom(byteArray1));
407+
bbValue.put(ByteString.copyFrom(byteArray2));
408+
409+
ByteString byteString1 = ByteString.copyFrom("cc1", "UTF-8");
410+
ByteString byteString2 = ByteString.copyFrom("cc2", "UTF-8");
411+
JSONArray ccValue = new JSONArray();
412+
ccValue.put(byteString1);
413+
ccValue.put(byteString2);
414+
415+
JSONObject foo = new JSONObject();
416+
foo.put("a", BigDecimalByteStringEncoder.encodeToNumericByteString(bigDecimal1));
417+
foo.put("b", ByteString.copyFrom(byteArray1));
418+
foo.put("c", byteString1);
419+
foo.put("aa", aaValue);
420+
foo.put("bb", bbValue);
421+
foo.put("cc", ccValue);
422+
JSONArray jsonArr = new JSONArray();
423+
jsonArr.put(foo);
424+
425+
RepetitionType expectedProto =
426+
RepetitionType.newBuilder()
427+
.setA(BigDecimalByteStringEncoder.encodeToNumericByteString(bigDecimal1))
428+
.setB(ByteString.copyFrom(byteArray1))
429+
.setC(byteString1)
430+
.addAa(BigDecimalByteStringEncoder.encodeToNumericByteString(bigDecimal1))
431+
.addAa(BigDecimalByteStringEncoder.encodeToNumericByteString(bigDecimal2))
432+
.addBb(ByteString.copyFrom(byteArray1))
433+
.addBb(ByteString.copyFrom(byteArray2))
434+
.addCc(byteString1)
435+
.addCc(byteString2)
436+
.build();
437+
try (JsonStreamWriter writer =
438+
getTestJsonStreamWriterBuilder(TEST_STREAM, tableSchema).build()) {
439+
440+
testBigQueryWrite.addResponse(
441+
AppendRowsResponse.newBuilder()
442+
.setAppendResult(
443+
AppendRowsResponse.AppendResult.newBuilder().setOffset(Int64Value.of(0)).build())
444+
.build());
445+
446+
ApiFuture<AppendRowsResponse> appendFuture = writer.append(jsonArr);
447+
assertEquals(0L, appendFuture.get().getAppendResult().getOffset().getValue());
448+
appendFuture.get();
449+
assertEquals(
450+
1,
451+
testBigQueryWrite
452+
.getAppendRequests()
453+
.get(0)
454+
.getProtoRows()
455+
.getRows()
456+
.getSerializedRowsCount());
457+
assertEquals(
458+
testBigQueryWrite
459+
.getAppendRequests()
460+
.get(0)
461+
.getProtoRows()
462+
.getRows()
463+
.getSerializedRows(0),
464+
expectedProto.toByteString());
465+
}
466+
}
467+
332468
@Test
333469
public void testSingleAppendMultipleSimpleJson() throws Exception {
334470
FooType expectedProto = FooType.newBuilder().setFoo("allen").build();

google-cloud-bigquerystorage/src/test/proto/test.proto

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,12 @@ message FlexibleType {
9393
optional string col_dGVzdC3liJc = 1
9494
[(.google.cloud.bigquery.storage.v1.column_name) = "test-列"];
9595
}
96+
97+
message RepetitionType {
98+
required bytes a = 1;
99+
required bytes b = 2;
100+
required bytes c = 3;
101+
repeated bytes aa = 4;
102+
repeated bytes bb = 5;
103+
repeated bytes cc = 6;
104+
}

0 commit comments

Comments
 (0)