Skip to content

Commit 22dfd2b

Browse files
committed
fix(bigquery): Fix totalRows returned by fetchCachedPage
The RowIterator.TotalRows is wrong when hitting the cached page on the fast path (e.g. not changing start index or updating the page info). fixes: #11873
1 parent 4b7209d commit 22dfd2b

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

bigquery/iterator.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ type rowSource struct {
229229
cachedRows []*bq.TableRow
230230
cachedSchema *bq.TableSchema
231231
cachedNextToken string
232+
cachedTotalRows uint64
232233
}
233234

234235
// fetchPageResult represents a page of rows returned from the backend.
@@ -382,6 +383,7 @@ func fetchCachedPage(src *rowSource, schema Schema, startIndex uint64, pageSize
382383
// We can't progress with no schema, destroy references and return a miss.
383384
src.cachedRows = nil
384385
src.cachedNextToken = ""
386+
src.cachedTotalRows = 0
385387
return nil, errNoCacheData
386388
}
387389
schema = bqToSchema(src.cachedSchema)
@@ -400,23 +402,26 @@ func fetchCachedPage(src *rowSource, schema Schema, startIndex uint64, pageSize
400402
src.cachedRows = nil
401403
src.cachedSchema = nil
402404
src.cachedNextToken = ""
405+
src.cachedTotalRows = 0
403406
return nil, err
404407
}
405408
result := &fetchPageResult{
406409
pageToken: src.cachedNextToken,
407410
rows: converted,
408411
schema: schema,
409-
totalRows: uint64(len(converted)),
412+
totalRows: src.cachedTotalRows,
410413
}
411414
// clear cache references and return response.
412415
src.cachedRows = nil
413416
src.cachedSchema = nil
414417
src.cachedNextToken = ""
418+
src.cachedTotalRows = 0
415419
return result, nil
416420
}
417421
// All other cases are invalid. Destroy any cache references on the way out the door.
418422
src.cachedRows = nil
419423
src.cachedSchema = nil
420424
src.cachedNextToken = ""
425+
src.cachedTotalRows = 0
421426
return nil, errNoCacheData
422427
}

bigquery/iterator_test.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,12 @@ func TestRowIteratorCacheBehavior(t *testing.T) {
8080
{
8181
// primary success case: schema in cache
8282
inSource: &rowSource{
83-
cachedSchema: testSchema,
84-
cachedRows: testRows,
83+
cachedSchema: testSchema,
84+
cachedRows: testRows,
85+
cachedTotalRows: 10,
8586
},
8687
wantResult: &fetchPageResult{
87-
totalRows: uint64(len(convertedRows)),
88+
totalRows: uint64(10),
8889
schema: convertedSchema,
8990
rows: convertedRows,
9091
},
@@ -94,10 +95,11 @@ func TestRowIteratorCacheBehavior(t *testing.T) {
9495
inSource: &rowSource{
9596
cachedRows: testRows,
9697
cachedNextToken: "foo",
98+
cachedTotalRows: 20,
9799
},
98100
inSchema: convertedSchema,
99101
wantResult: &fetchPageResult{
100-
totalRows: uint64(len(convertedRows)),
102+
totalRows: uint64(20),
101103
schema: convertedSchema,
102104
rows: convertedRows,
103105
pageToken: "foo",

bigquery/query.go

+1
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ func (q *Query) Read(ctx context.Context) (it *RowIterator, err error) {
418418
cachedRows: resp.Rows,
419419
cachedSchema: resp.Schema,
420420
cachedNextToken: resp.PageToken,
421+
cachedTotalRows: resp.TotalRows,
421422
}
422423
return newRowIterator(ctx, rowSource, fetchPage), nil
423424
}

0 commit comments

Comments
 (0)