From 74a4517cc99b2a3d5c3505baeb607e249f7aab82 Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Thu, 13 Dec 2018 02:24:16 +0530 Subject: [PATCH] Fix setProjectId for insertAll (#4196) --- .../google/cloud/bigquery/BigQueryImpl.java | 8 ++- .../cloud/bigquery/BigQueryImplTest.java | 51 +++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java index 2c151a74e14e..9f2d46dbf46f 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java +++ b/google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java @@ -539,7 +539,13 @@ public Table apply(com.google.api.services.bigquery.model.Table table) { @Override public InsertAllResponse insertAll(InsertAllRequest request) { - final TableId tableId = request.getTable().setProjectId(getOptions().getProjectId()); + final TableId tableId = + request + .getTable() + .setProjectId( + Strings.isNullOrEmpty(request.getTable().getProject()) + ? getOptions().getProjectId() + : request.getTable().getProject()); final TableDataInsertAllRequest requestPb = new TableDataInsertAllRequest(); requestPb.setIgnoreUnknownValues(request.ignoreUnknownValues()); requestPb.setSkipInvalidRows(request.skipInvalidRows()); diff --git a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java index 5d274bead3ec..9b67445acd13 100644 --- a/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java +++ b/google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java @@ -945,6 +945,57 @@ public TableDataInsertAllRequest.Rows apply(RowToInsert rowToInsert) { assertEquals("ErrorMessage", response.getErrorsFor(0L).get(0).getMessage()); } + @Test + public void testInsertAllWithProjectInTable() { + Map row1 = ImmutableMap.of("field", "value1"); + Map row2 = ImmutableMap.of("field", "value2"); + List rows = + ImmutableList.of(new RowToInsert("row1", row1), new RowToInsert("row2", row2)); + TableId tableId = TableId.of("project-different-from-option", DATASET, TABLE); + InsertAllRequest request = + InsertAllRequest.newBuilder(tableId) + .setRows(rows) + .setSkipInvalidRows(false) + .setIgnoreUnknownValues(true) + .setTemplateSuffix("suffix") + .build(); + TableDataInsertAllRequest requestPb = + new TableDataInsertAllRequest() + .setRows( + Lists.transform( + rows, + new Function() { + @Override + public TableDataInsertAllRequest.Rows apply(RowToInsert rowToInsert) { + return new TableDataInsertAllRequest.Rows() + .setInsertId(rowToInsert.getId()) + .setJson(rowToInsert.getContent()); + } + })) + .setSkipInvalidRows(false) + .setIgnoreUnknownValues(true) + .setTemplateSuffix("suffix"); + TableDataInsertAllResponse responsePb = + new TableDataInsertAllResponse() + .setInsertErrors( + ImmutableList.of( + new TableDataInsertAllResponse.InsertErrors() + .setIndex(0L) + .setErrors(ImmutableList.of(new ErrorProto().setMessage("ErrorMessage"))))); + EasyMock.expect( + bigqueryRpcMock.insertAll("project-different-from-option", DATASET, TABLE, requestPb)) + .andReturn(responsePb); + EasyMock.replay(bigqueryRpcMock); + BigQueryOptions bigQueryOptions = + createBigQueryOptionsForProject(OTHER_PROJECT, rpcFactoryMock); + bigquery = bigQueryOptions.getService(); + InsertAllResponse response = bigquery.insertAll(request); + assertNotNull(response.getErrorsFor(0L)); + assertNull(response.getErrorsFor(1L)); + assertEquals(1, response.getErrorsFor(0L).size()); + assertEquals("ErrorMessage", response.getErrorsFor(0L).get(0).getMessage()); + } + @Test public void testListTableData() { EasyMock.expect(bigqueryRpcMock.listTableData(PROJECT, DATASET, TABLE, EMPTY_RPC_OPTIONS))