Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 71e7d48

Browse files
authoredOct 13, 2020
fix: fix start index with page size for list rows (#793)
1 parent d786194 commit 71e7d48

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed
 

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -1089,9 +1089,11 @@ public TableDataList call() {
10891089
EXCEPTION_HANDLER,
10901090
serviceOptions.getClock());
10911091
String cursor = result.getPageToken();
1092+
Map<BigQueryRpc.Option, ?> pageOptionMap =
1093+
Strings.isNullOrEmpty(cursor) ? optionsMap : optionMap(TableDataListOption.startIndex(0));
10921094
return Tuple.of(
10931095
new PageImpl<>(
1094-
new TableDataPageFetcher(tableId, schema, serviceOptions, cursor, optionsMap),
1096+
new TableDataPageFetcher(tableId, schema, serviceOptions, cursor, pageOptionMap),
10951097
cursor,
10961098
transformTableData(result.getRows(), schema)),
10971099
result.getTotalRows());

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

+35
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import static org.junit.Assert.assertTrue;
2727
import static org.junit.Assert.fail;
2828
import static org.mockito.Mockito.any;
29+
import static org.mockito.Mockito.doReturn;
2930
import static org.mockito.Mockito.eq;
3031
import static org.mockito.Mockito.mock;
3132
import static org.mockito.Mockito.times;
@@ -1470,6 +1471,40 @@ public void testListTableDataWithOptions() {
14701471
verify(bigqueryRpcMock).listTableData(PROJECT, DATASET, TABLE, TABLE_DATA_LIST_OPTIONS);
14711472
}
14721473

1474+
@Test
1475+
public void testListTableDataWithNextPage() {
1476+
doReturn(TABLE_DATA_PB)
1477+
.when(bigqueryRpcMock)
1478+
.listTableData(PROJECT, DATASET, TABLE, TABLE_DATA_LIST_OPTIONS);
1479+
bigquery = options.getService();
1480+
TableResult page =
1481+
bigquery.listTableData(
1482+
DATASET,
1483+
TABLE,
1484+
TABLE_DATA_LIST_PAGE_SIZE,
1485+
TABLE_DATA_LIST_PAGE_TOKEN,
1486+
TABLE_DATA_LIST_START_INDEX);
1487+
assertEquals(CURSOR, page.getNextPageToken());
1488+
verify(bigqueryRpcMock).listTableData(PROJECT, DATASET, TABLE, TABLE_DATA_LIST_OPTIONS);
1489+
assertArrayEquals(TABLE_DATA.toArray(), Iterables.toArray(page.getValues(), List.class));
1490+
Map<BigQueryRpc.Option, ?> SECOND_TABLE_DATA_LIST_OPTIONS =
1491+
ImmutableMap.of(BigQueryRpc.Option.PAGE_TOKEN, CURSOR, BigQueryRpc.Option.START_INDEX, 0L);
1492+
doReturn(
1493+
new TableDataList()
1494+
.setPageToken(null)
1495+
.setTotalRows(1L)
1496+
.setRows(
1497+
ImmutableList.of(
1498+
new TableRow().setF(ImmutableList.of(new TableCell().setV("Value3"))),
1499+
new TableRow().setF(ImmutableList.of(new TableCell().setV("Value4"))))))
1500+
.when(bigqueryRpcMock)
1501+
.listTableData(PROJECT, DATASET, TABLE, SECOND_TABLE_DATA_LIST_OPTIONS);
1502+
assertTrue(page.hasNextPage());
1503+
page = page.getNextPage();
1504+
assertNull(page.getNextPageToken());
1505+
verify(bigqueryRpcMock).listTableData(PROJECT, DATASET, TABLE, SECOND_TABLE_DATA_LIST_OPTIONS);
1506+
}
1507+
14731508
// The "minimally initialized" Job that lets Job.fromPb run without throwing.
14741509
private static com.google.api.services.bigquery.model.Job newJobPb() {
14751510
return new com.google.api.services.bigquery.model.Job()

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

+23
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
import java.nio.ByteBuffer;
113113
import java.nio.charset.StandardCharsets;
114114
import java.nio.file.FileSystems;
115+
import java.util.Collection;
115116
import java.util.Collections;
116117
import java.util.HashMap;
117118
import java.util.HashSet;
@@ -365,6 +366,9 @@ public class ITBigQueryTest {
365366
private static final Set<String> PUBLIC_DATASETS =
366367
ImmutableSet.of("github_repos", "hacker_news", "noaa_gsod", "samples", "usa_names");
367368

369+
private static final String PUBLIC_PROJECT = "bigquery-public-data";
370+
private static final String PUBLIC_DATASET = "census_bureau_international";
371+
368372
private static BigQuery bigquery;
369373
private static Storage storage;
370374

@@ -1342,6 +1346,25 @@ public void testListAllTableData() {
13421346
assertEquals(2, rowCount);
13431347
}
13441348

1349+
@Test
1350+
public void testListPageWithStartIndex() {
1351+
String tableName = "midyear_population_agespecific";
1352+
TableId tableId = TableId.of(PUBLIC_PROJECT, PUBLIC_DATASET, tableName);
1353+
Table table = bigquery.getTable(tableId);
1354+
long numRows = table.getNumRows().longValue();
1355+
Page<FieldValueList> tableResult =
1356+
bigquery.listTableData(
1357+
tableId,
1358+
BigQuery.TableDataListOption.startIndex(numRows - 300_000L),
1359+
BigQuery.TableDataListOption.pageSize(600_000L));
1360+
assertNotNull(tableResult.getNextPageToken());
1361+
long totalRows = ((Collection<?>) tableResult.getValues()).size();
1362+
tableResult = tableResult.getNextPage();
1363+
totalRows = totalRows + ((Collection<?>) tableResult.getValues()).size();
1364+
assertNull(tableResult.getNextPageToken());
1365+
assertEquals(300_000L, totalRows);
1366+
}
1367+
13451368
@Test
13461369
public void testModelLifecycle() throws InterruptedException {
13471370

0 commit comments

Comments
 (0)
Failed to load comments.