Skip to content

Commit f034a99

Browse files
authored
fix: bug fix for get method of Bigquery Dataset (#1379)
* Adding the projectId used of getting the DataSet as a parameter for the issue: #1369 * Adding the projectId used of getting the DataSet as a parameter for the issue: #1369 * Added testcase for getTable with projectId.
1 parent 4a6906a commit f034a99

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/Dataset.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.cloud.bigquery.BigQuery.DatasetOption;
2424
import com.google.cloud.bigquery.BigQuery.TableListOption;
2525
import com.google.cloud.bigquery.BigQuery.TableOption;
26+
import com.google.common.base.Strings;
2627
import java.io.IOException;
2728
import java.io.ObjectInputStream;
2829
import java.util.List;
@@ -275,7 +276,13 @@ public Page<Table> list(TableListOption... options) {
275276
* @throws BigQueryException upon failure
276277
*/
277278
public Table get(String tableId, TableOption... options) {
278-
return bigquery.getTable(TableId.of(getDatasetId().getDataset(), tableId), options);
279+
// Adding the projectId used of getting the DataSet as a parameter for the issue:
280+
// https://github.com/googleapis/java-bigquery/issues/1369
281+
TableId tabId =
282+
Strings.isNullOrEmpty(getDatasetId().getProject())
283+
? TableId.of(getDatasetId().getDataset(), tableId)
284+
: TableId.of(getDatasetId().getProject(), getDatasetId().getDataset(), tableId);
285+
return bigquery.getTable(tabId, options);
279286
}
280287

281288
/**

google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/DatasetTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ public class DatasetTest {
7777
TableInfo.newBuilder(TableId.of("dataset", "table2"), VIEW_DEFINITION).build();
7878
private static final TableInfo TABLE_INFO3 =
7979
TableInfo.newBuilder(TableId.of("dataset", "table3"), EXTERNAL_TABLE_DEFINITION).build();
80+
private static final String NEW_PROJECT_ID = "projectId2";
81+
private static final TableId TABLE_ID1 = TableId.of(NEW_PROJECT_ID, "dataset", "table3");
82+
private static final TableInfo TABLE_INFO4 =
83+
TableInfo.newBuilder(
84+
TableId.of(NEW_PROJECT_ID, "dataset", "table3"), EXTERNAL_TABLE_DEFINITION)
85+
.build();
8086

8187
@Rule public MockitoRule rule;
8288

@@ -255,6 +261,16 @@ public void testGet() {
255261
verify(bigquery).getTable(TABLE_INFO1.getTableId());
256262
}
257263

264+
@Test
265+
public void testGetTableWithNewProjectId() {
266+
Table expectedTable = new Table(bigquery, new TableInfo.BuilderImpl(TABLE_INFO4));
267+
when(bigquery.getTable(TABLE_ID1, null)).thenReturn(expectedTable);
268+
Table table = bigquery.getTable(TABLE_ID1, null);
269+
assertNotNull(table);
270+
assertEquals(table.getTableId().getProject(), NEW_PROJECT_ID);
271+
verify(bigquery).getTable(TABLE_ID1, null);
272+
}
273+
258274
@Test
259275
public void testGetNull() {
260276
when(bigquery.getTable(TABLE_INFO1.getTableId())).thenReturn(null);

0 commit comments

Comments
 (0)