Skip to content

Commit 203a91e

Browse files
fix(bqjdbc): pass rowsInPage with TableResult (#13238)
b/511229401 This PR introduces the `rowsInPage` metadata to `TableResult` and ensures it is populated for all pages (both the first page and subsequent pages fetched via pagination). Previously, metadata about the number of rows in the page was not easily accessible or propagated across multiple paginated requests (e.g., when calling `getNextPage()`). This change: 1. Adds a `rowsInPage` field and builder method to the `@AutoValue` `TableResult` class. 2. Populates `rowsInPage` during initial page creation in `BigQueryImpl` and empty results in `Job.java`. 3. Updates `TableResult.getNextPage()` to calculate the size of values for subsequent pages via `Iterables.size` (which is $O(1)$ for materialized lists) and sets it on the returned `TableResult` pages. 4. Updates unit test assertions in `TableResultTest.java` and `SerializationTest.java` to verify that `rowsInPage` is populated and propagated correctly. ## Key Changes - **`TableResult.java`**: Added `getRowsInPage()`, builder method `setRowsInPage()`, and updated `getNextPage()` to pass down the calculated row count for the next page. - **`BigQueryImpl.java`**: Set `rowsInPage` on initial query results page creation. - **`TableResultTest.java`**: Added assertions to verify that `rowsInPage` is correctly populated for both the first page and subsequent pages. --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent da1ccb1 commit 203a91e

6 files changed

Lines changed: 33 additions & 5 deletions

File tree

java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryStatement.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1028,7 +1028,9 @@ private boolean meetsReadRatio(TableResult results) {
10281028
return false;
10291029
}
10301030

1031-
long pageSize = querySettings.getMaxResultPerPage();
1031+
Long rowsInPage = results.getRowsInPage();
1032+
long pageSize =
1033+
(rowsInPage != null && rowsInPage > 0) ? rowsInPage : querySettings.getMaxResultPerPage();
10321034

10331035
// Prevent division by zero due to potential overflows/empty sets:
10341036
if (pageSize <= 0) {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,6 +1691,7 @@ && getOptions().getOpenTelemetryTracer() != null) {
16911691
.setSchema(schema)
16921692
.setTotalRows(data.y())
16931693
.setPageNoSchema(data.x())
1694+
.setRowsInPage((long) Iterables.size(data.x().getValues()))
16941695
.build();
16951696
} finally {
16961697
if (tableDataList != null) {
@@ -2022,6 +2023,7 @@ public com.google.api.services.bigquery.model.QueryResponse call()
20222023
.setJobId(jobId)
20232024
.setQueryId(results.getQueryId())
20242025
.setJobCreationReason(JobCreationReason.fromPb(results.getJobCreationReason()))
2026+
.setRowsInPage(results.getRows() != null ? (long) results.getRows().size() : 0L)
20252027
.build();
20262028
}
20272029
// only 1 page of result
@@ -2041,6 +2043,7 @@ public com.google.api.services.bigquery.model.QueryResponse call()
20412043
results.getJobReference() != null ? JobId.fromPb(results.getJobReference()) : null)
20422044
.setQueryId(results.getQueryId())
20432045
.setJobCreationReason(JobCreationReason.fromPb(results.getJobCreationReason()))
2046+
.setRowsInPage(results.getRows() != null ? (long) results.getRows().size() : 0L)
20442047
.build();
20452048
}
20462049

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ public TableResult getQueryResults(QueryResultsOption... options)
424424
.setJobId(job.getJobId())
425425
.setTotalRows(0L)
426426
.setPageNoSchema(new PageImpl<FieldValueList>(null, "", null))
427+
.setRowsInPage(0L)
427428
.build();
428429
return emptyTableResult;
429430
}

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public abstract static class Builder {
4949

5050
public abstract TableResult.Builder setJobCreationReason(JobCreationReason jobCreationReason);
5151

52+
abstract TableResult.Builder setRowsInPage(Long rowsInPage);
53+
5254
/** Creates a @code TableResult} object. */
5355
public abstract TableResult build();
5456
}
@@ -81,6 +83,10 @@ public static Builder newBuilder() {
8183
@Nullable
8284
public abstract JobCreationReason getJobCreationReason();
8385

86+
/** Returns the number of rows in the current page of results. */
87+
@Nullable
88+
public abstract Long getRowsInPage();
89+
8490
@Override
8591
public boolean hasNextPage() {
8692
return getPageNoSchema().hasNextPage();
@@ -94,12 +100,15 @@ public String getNextPageToken() {
94100
@Override
95101
public TableResult getNextPage() {
96102
if (getPageNoSchema().hasNextPage()) {
103+
Page<FieldValueList> nextPageNoSchema = getPageNoSchema().getNextPage();
104+
long nextRows = (long) Iterables.size(nextPageNoSchema.getValues());
97105
return TableResult.newBuilder()
98106
.setSchema(getSchema())
99107
.setTotalRows(getTotalRows())
100-
.setPageNoSchema(getPageNoSchema().getNextPage())
108+
.setPageNoSchema(nextPageNoSchema)
101109
.setQueryId(getQueryId())
102110
.setJobCreationReason(getJobCreationReason())
111+
.setRowsInPage(nextRows)
103112
.build();
104113
}
105114
return null;
@@ -137,12 +146,14 @@ public String toString() {
137146
.add("totalRows", getTotalRows())
138147
.add("cursor", getNextPageToken())
139148
.add("queryId", getQueryId())
149+
.add("rowsInPage", getRowsInPage())
140150
.toString();
141151
}
142152

143153
@Override
144154
public final int hashCode() {
145-
return Objects.hash(getPageNoSchema(), getSchema(), getTotalRows(), getQueryId());
155+
return Objects.hash(
156+
getPageNoSchema(), getSchema(), getTotalRows(), getQueryId(), getRowsInPage());
146157
}
147158

148159
@Override
@@ -158,6 +169,7 @@ public final boolean equals(Object obj) {
158169
&& Iterators.elementsEqual(getValues().iterator(), response.getValues().iterator())
159170
&& Objects.equals(getSchema(), response.getSchema())
160171
&& getTotalRows() == response.getTotalRows()
161-
&& getQueryId() == response.getQueryId();
172+
&& Objects.equals(getQueryId(), response.getQueryId())
173+
&& Objects.equals(getRowsInPage(), response.getRowsInPage());
162174
}
163175
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ public class SerializationTest extends BaseSerializationTest {
210210
.setSchema(Schema.of())
211211
.setTotalRows(0L)
212212
.setPageNoSchema(new PageImpl(null, "", ImmutableList.of()))
213+
.setRowsInPage(0L)
213214
.build();
214215
private static final BigQuery BIGQUERY =
215216
BigQueryOptions.newBuilder().setProjectId("p1").build().getService();

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,15 @@ private static FieldValueList newFieldValueList(String s) {
5454
@Test
5555
void testNullSchema() {
5656
TableResult result =
57-
TableResult.newBuilder().setTotalRows(3L).setPageNoSchema(INNER_PAGE_0).build();
57+
TableResult.newBuilder()
58+
.setTotalRows(3L)
59+
.setPageNoSchema(INNER_PAGE_0)
60+
.setRowsInPage(2L)
61+
.build();
5862
assertThat(result.getSchema()).isNull();
5963
assertThat(result.hasNextPage()).isTrue();
6064
assertThat(result.getNextPageToken()).isNotNull();
65+
assertThat(result.getRowsInPage()).isEqualTo(2L);
6166
assertThat(result.getValues())
6267
.containsExactly(newFieldValueList("0"), newFieldValueList("1"))
6368
.inOrder();
@@ -66,6 +71,7 @@ void testNullSchema() {
6671
assertThat(next.getSchema()).isNull();
6772
assertThat(next.hasNextPage()).isFalse();
6873
assertThat(next.getNextPageToken()).isNull();
74+
assertThat(next.getRowsInPage()).isEqualTo(1L);
6975
assertThat(next.getValues()).containsExactly(newFieldValueList("2"));
7076
assertThat(next.getNextPage()).isNull();
7177

@@ -81,10 +87,12 @@ void testSchema() {
8187
.setSchema(SCHEMA)
8288
.setTotalRows(3L)
8389
.setPageNoSchema(INNER_PAGE_0)
90+
.setRowsInPage(2L)
8491
.build();
8592
assertThat(result.getSchema()).isEqualTo(SCHEMA);
8693
assertThat(result.hasNextPage()).isTrue();
8794
assertThat(result.getNextPageToken()).isNotNull();
95+
assertThat(result.getRowsInPage()).isEqualTo(2L);
8896
assertThat(result.getValues())
8997
.containsExactly(
9098
newFieldValueList("0").withSchema(SCHEMA.getFields()),
@@ -95,6 +103,7 @@ void testSchema() {
95103
assertThat(next.getSchema()).isEqualTo(SCHEMA);
96104
assertThat(next.hasNextPage()).isFalse();
97105
assertThat(next.getNextPageToken()).isNull();
106+
assertThat(next.getRowsInPage()).isEqualTo(1L);
98107
assertThat(next.getValues())
99108
.containsExactly(newFieldValueList("2").withSchema(SCHEMA.getFields()));
100109
assertThat(next.getNextPage()).isNull();

0 commit comments

Comments
 (0)