Skip to content

Commit e53621b

Browse files
Praful Makanichingor13
authored andcommitted
BigQuery: Fix TableId behaviour for non default BigQueryClient project (#4115)
* Fixes create, update and delete methods * remove comments * Fix formatting
1 parent 8da11b0 commit e53621b

2 files changed

Lines changed: 55 additions & 3 deletions

File tree

google-cloud-clients/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/BigQueryImpl.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,12 @@ public com.google.api.services.bigquery.model.Dataset call() {
161161
@Override
162162
public Table create(TableInfo tableInfo, TableOption... options) {
163163
final com.google.api.services.bigquery.model.Table tablePb =
164-
tableInfo.setProjectId(getOptions().getProjectId()).toPb();
164+
tableInfo
165+
.setProjectId(
166+
Strings.isNullOrEmpty(tableInfo.getTableId().getProject())
167+
? getOptions().getProjectId()
168+
: tableInfo.getTableId().getProject())
169+
.toPb();
165170
final Map<BigQueryRpc.Option, ?> optionsMap = optionMap(options);
166171
try {
167172
return Table.fromPb(this,
@@ -345,7 +350,11 @@ public boolean delete(String datasetId, String tableId) {
345350

346351
@Override
347352
public boolean delete(TableId tableId) {
348-
final TableId completeTableId = tableId.setProjectId(getOptions().getProjectId());
353+
final TableId completeTableId = tableId.setProjectId(
354+
Strings.isNullOrEmpty(tableId.getProject())
355+
? getOptions().getProjectId()
356+
: tableId.getProject()
357+
);
349358
try {
350359
return runWithRetries(new Callable<Boolean>() {
351360
@Override
@@ -380,7 +389,12 @@ public com.google.api.services.bigquery.model.Dataset call() {
380389
@Override
381390
public Table update(TableInfo tableInfo, TableOption... options) {
382391
final com.google.api.services.bigquery.model.Table tablePb =
383-
tableInfo.setProjectId(getOptions().getProjectId()).toPb();
392+
tableInfo
393+
.setProjectId(
394+
Strings.isNullOrEmpty(tableInfo.getTableId().getProject())
395+
? getOptions().getProjectId()
396+
: tableInfo.getTableId().getProject())
397+
.toPb();
384398
final Map<BigQueryRpc.Option, ?> optionsMap = optionMap(options);
385399
try {
386400
return Table.fromPb(this,

google-cloud-clients/google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/BigQueryImplTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,20 @@ public void testCreateTable() {
552552
assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(tableInfo)), table);
553553
}
554554

555+
@Test
556+
public void testCreateTableWithoutProject() {
557+
TableInfo tableInfo = TABLE_INFO.setProjectId(PROJECT);
558+
TableId tableId = TableId.of("", TABLE_ID.getDataset(), TABLE_ID.getTable());
559+
tableInfo.toBuilder().setTableId(tableId);
560+
EasyMock.expect(bigqueryRpcMock.create(tableInfo.toPb(), EMPTY_RPC_OPTIONS))
561+
.andReturn(tableInfo.toPb());
562+
EasyMock.replay(bigqueryRpcMock);
563+
BigQueryOptions bigQueryOptions = createBigQueryOptionsForProject(PROJECT, rpcFactoryMock);
564+
bigquery = bigQueryOptions.getService();
565+
Table table = bigquery.create(tableInfo);
566+
assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(tableInfo)), table);
567+
}
568+
555569
@Test
556570
public void testCreateTableWithSelectedFields() {
557571
Capture<Map<BigQueryRpc.Option, Object>> capturedOptions = Capture.newInstance();
@@ -728,6 +742,16 @@ public void testDeleteTableFromTableIdWithProject() {
728742
assertTrue(bigquery.delete(tableId));
729743
}
730744

745+
@Test
746+
public void testDeleteTableFromTableIdWithoutProject() {
747+
TableId tableId = TableId.of("", TABLE_ID.getDataset(), TABLE_ID.getTable());
748+
EasyMock.expect(bigqueryRpcMock.deleteTable(PROJECT, DATASET, TABLE)).andReturn(true);
749+
EasyMock.replay(bigqueryRpcMock);
750+
BigQueryOptions bigQueryOptions = createBigQueryOptionsForProject(PROJECT, rpcFactoryMock);
751+
bigquery = bigQueryOptions.getService();
752+
assertTrue(bigquery.delete(tableId));
753+
}
754+
731755
@Test
732756
public void testUpdateTable() {
733757
TableInfo updatedTableInfo =
@@ -741,6 +765,20 @@ public void testUpdateTable() {
741765
assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(updatedTableInfo)), table);
742766
}
743767

768+
@Test
769+
public void testUpdateTableWithoutProject() {
770+
TableInfo tableInfo = TABLE_INFO.setProjectId(PROJECT);
771+
TableId tableId = TableId.of("", TABLE_ID.getDataset(), TABLE_ID.getTable());
772+
tableInfo.toBuilder().setTableId(tableId);
773+
EasyMock.expect(bigqueryRpcMock.patch(tableInfo.toPb(), EMPTY_RPC_OPTIONS))
774+
.andReturn(tableInfo.toPb());
775+
EasyMock.replay(bigqueryRpcMock);
776+
BigQueryOptions bigQueryOptions = createBigQueryOptionsForProject(PROJECT, rpcFactoryMock);
777+
bigquery = bigQueryOptions.getService();
778+
Table table = bigquery.update(tableInfo);
779+
assertEquals(new Table(bigquery, new TableInfo.BuilderImpl(tableInfo)), table);
780+
}
781+
744782
@Test
745783
public void testUpdateTableWithSelectedFields() {
746784
Capture<Map<BigQueryRpc.Option, Object>> capturedOptions = Capture.newInstance();

0 commit comments

Comments
 (0)