|
33 | 33 | import com.google.cloud.bigquery.storage.test.SchemaTest;
|
34 | 34 | import com.google.cloud.bigquery.storage.test.Test.FlexibleType;
|
35 | 35 | import com.google.cloud.bigquery.storage.test.Test.FooType;
|
| 36 | +import com.google.cloud.bigquery.storage.test.Test.RepetitionType; |
36 | 37 | import com.google.cloud.bigquery.storage.test.Test.UpdatedFooType;
|
37 | 38 | import com.google.cloud.bigquery.storage.v1.ConnectionWorkerPool.Settings;
|
38 | 39 | import com.google.cloud.bigquery.storage.v1.Exceptions.AppendSerializtionError;
|
39 | 40 | import com.google.cloud.bigquery.storage.v1.TableFieldSchema.Mode;
|
| 41 | +import com.google.protobuf.ByteString; |
40 | 42 | import com.google.protobuf.Descriptors.DescriptorValidationException;
|
41 | 43 | import com.google.protobuf.Int64Value;
|
42 | 44 | import com.google.protobuf.Timestamp;
|
43 | 45 | import io.grpc.Status;
|
44 | 46 | import io.grpc.StatusRuntimeException;
|
45 | 47 | import java.io.IOException;
|
| 48 | +import java.math.BigDecimal; |
| 49 | +import java.math.RoundingMode; |
46 | 50 | import java.util.Arrays;
|
47 | 51 | import java.util.Map;
|
48 | 52 | import java.util.UUID;
|
|
63 | 67 | @RunWith(JUnit4.class)
|
64 | 68 | public class JsonStreamWriterTest {
|
65 | 69 | private static final Logger LOG = Logger.getLogger(JsonStreamWriterTest.class.getName());
|
| 70 | + private static int NUMERIC_SCALE = 9; |
66 | 71 | private static final String TEST_STREAM = "projects/p/datasets/d/tables/t/streams/_default";
|
67 | 72 | private static final String TEST_STREAM_2 = "projects/p/datasets/d2/tables/t2/streams/_default";
|
68 | 73 | private static final String TEST_TABLE = "projects/p/datasets/d/tables/t";
|
@@ -329,6 +334,137 @@ public void testSpecialTypeAppend() throws Exception {
|
329 | 334 | }
|
330 | 335 | }
|
331 | 336 |
|
| 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 | + |
332 | 468 | @Test
|
333 | 469 | public void testSingleAppendMultipleSimpleJson() throws Exception {
|
334 | 470 | FooType expectedProto = FooType.newBuilder().setFoo("allen").build();
|
|
0 commit comments