Skip to content

Commit f2799dd

Browse files
author
Praful Makani
authored
fix: schema and totalRows for duplicate request ids (#804)
* fix: schema and totalRows for duplicate request ids * fix: modify code and test case * fix: address comment
1 parent 137eeaa commit f2799dd

File tree

2 files changed

+61
-8
lines changed

2 files changed

+61
-8
lines changed

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,8 @@ public TableResult query(QueryJobConfiguration configuration, JobOption... optio
12391239
}
12401240

12411241
private TableResult queryRpc(
1242-
final String projectId, final QueryRequest content, JobOption... options) {
1242+
final String projectId, final QueryRequest content, JobOption... options)
1243+
throws InterruptedException {
12431244
com.google.api.services.bigquery.model.QueryResponse results;
12441245
try {
12451246
results =
@@ -1265,14 +1266,22 @@ public com.google.api.services.bigquery.model.QueryResponse call() {
12651266
throw new BigQueryException(bigQueryErrors);
12661267
}
12671268

1268-
Schema schema = results.getSchema() == null ? null : Schema.fromPb(results.getSchema());
1269-
Long numRows;
1270-
if (results.getNumDmlAffectedRows() == null && results.getTotalRows() == null) {
1271-
numRows = 0L;
1272-
} else if (results.getNumDmlAffectedRows() != null) {
1273-
numRows = results.getNumDmlAffectedRows();
1269+
long numRows;
1270+
Schema schema;
1271+
if (results.getSchema() == null && results.getJobComplete()) {
1272+
JobId jobId = JobId.fromPb(results.getJobReference());
1273+
Job job = getJob(jobId, options);
1274+
TableResult tableResult = job.getQueryResults();
1275+
return tableResult;
12741276
} else {
1275-
numRows = results.getTotalRows().longValue();
1277+
schema = results.getSchema() == null ? null : Schema.fromPb(results.getSchema());
1278+
if (results.getNumDmlAffectedRows() == null && results.getTotalRows() == null) {
1279+
numRows = 0L;
1280+
} else if (results.getNumDmlAffectedRows() != null) {
1281+
numRows = results.getNumDmlAffectedRows();
1282+
} else {
1283+
numRows = results.getTotalRows().longValue();
1284+
}
12761285
}
12771286

12781287
if (results.getPageToken() != null) {

google-cloud-bigquery/src/test/java/com/google/cloud/bigquery/it/ITBigQueryTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,6 +1608,35 @@ public void testFastQueryMultipleRuns() throws InterruptedException {
16081608
assertFalse(result2.hasNextPage());
16091609
}
16101610

1611+
@Test
1612+
public void testFastQuerySinglePageDuplicateRequestIds() throws InterruptedException {
1613+
String query =
1614+
"SELECT TimestampField, StringField, BooleanField FROM " + TABLE_ID_FASTQUERY.getTable();
1615+
QueryJobConfiguration config =
1616+
QueryJobConfiguration.newBuilder(query).setDefaultDataset(DatasetId.of(DATASET)).build();
1617+
TableResult result = bigquery.query(config);
1618+
assertEquals(QUERY_RESULT_SCHEMA, result.getSchema());
1619+
assertEquals(2, result.getTotalRows());
1620+
assertNull(result.getNextPage());
1621+
assertNull(result.getNextPageToken());
1622+
assertFalse(result.hasNextPage());
1623+
1624+
TableResult result1 = bigquery.query(config);
1625+
assertEquals(QUERY_RESULT_SCHEMA, result1.getSchema());
1626+
assertEquals(2, result1.getTotalRows());
1627+
assertNull(result1.getNextPage());
1628+
assertNull(result1.getNextPageToken());
1629+
assertFalse(result1.hasNextPage());
1630+
1631+
config.toBuilder().setQuery(query).build();
1632+
TableResult result2 = bigquery.query(config);
1633+
assertEquals(QUERY_RESULT_SCHEMA, result2.getSchema());
1634+
assertEquals(2, result2.getTotalRows());
1635+
assertNull(result2.getNextPage());
1636+
assertNull(result2.getNextPageToken());
1637+
assertFalse(result2.hasNextPage());
1638+
}
1639+
16111640
@Test
16121641
public void testFastSQLQuery() throws InterruptedException {
16131642
String query =
@@ -1650,6 +1679,21 @@ public void testFastSQLQueryMultiPage() throws InterruptedException {
16501679
assertNotNull(result.getNextPage());
16511680
assertNotNull(result.getNextPageToken());
16521681
assertTrue(result.hasNextPage());
1682+
1683+
TableResult result1 = bigquery.query(config);
1684+
assertEquals(LARGE_TABLE_SCHEMA, result.getSchema());
1685+
assertEquals(313348, result.getTotalRows());
1686+
assertNotNull(result1.getNextPage());
1687+
assertNotNull(result1.getNextPageToken());
1688+
assertTrue(result1.hasNextPage());
1689+
1690+
config.toBuilder().setQuery(query).build();
1691+
TableResult result2 = bigquery.query(config);
1692+
assertEquals(LARGE_TABLE_SCHEMA, result2.getSchema());
1693+
assertEquals(313348, result2.getTotalRows());
1694+
assertNotNull(result2.getNextPage());
1695+
assertNotNull(result2.getNextPageToken());
1696+
assertTrue(result2.hasNextPage());
16531697
}
16541698

16551699
@Test

0 commit comments

Comments
 (0)