这是indexloc提供的服务,不要输入任何密码
Skip to content

fix(bigquery): cache total rows count #12230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions bigquery/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,31 @@ func TestIntegration_QueryContextTimeout(t *testing.T) {
}
}

func TestIntegration_QueryCachedTotalRows(t *testing.T) {
if client == nil {
t.Skip("Integration tests skipped")
}
ctx := context.Background()

q := client.Query("select * from unnest(generate_array(1,1000000))") // force multiple pages
it, err := q.Read(ctx)
if err != nil {
t.Errorf("Read() error: %v", err)
}
var values []Value
err = it.Next(&values)
if err == iterator.Done {
t.Error("Expected iterator to have values, but found iterator.Done")
}
if err != nil {
t.Errorf("Expected iterator to read a value without errors, but found: %v", err)
}
expectedTotal := uint64(1000000)
if it.TotalRows != expectedTotal {
t.Errorf("Expected total rows to be %d, but found: %d", expectedTotal, it.TotalRows)
}
}

func TestIntegration_SnapshotRestoreClone(t *testing.T) {

if client == nil {
Expand Down
7 changes: 6 additions & 1 deletion bigquery/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ type rowSource struct {
cachedRows []*bq.TableRow
cachedSchema *bq.TableSchema
cachedNextToken string
cachedTotalRows uint64
}

// fetchPageResult represents a page of rows returned from the backend.
Expand Down Expand Up @@ -382,6 +383,7 @@ func fetchCachedPage(src *rowSource, schema Schema, startIndex uint64, pageSize
// We can't progress with no schema, destroy references and return a miss.
src.cachedRows = nil
src.cachedNextToken = ""
src.cachedTotalRows = 0
return nil, errNoCacheData
}
schema = bqToSchema(src.cachedSchema)
Expand All @@ -400,23 +402,26 @@ func fetchCachedPage(src *rowSource, schema Schema, startIndex uint64, pageSize
src.cachedRows = nil
src.cachedSchema = nil
src.cachedNextToken = ""
src.cachedTotalRows = 0
return nil, err
}
result := &fetchPageResult{
pageToken: src.cachedNextToken,
rows: converted,
schema: schema,
totalRows: uint64(len(converted)),
totalRows: src.cachedTotalRows,
}
// clear cache references and return response.
src.cachedRows = nil
src.cachedSchema = nil
src.cachedNextToken = ""
src.cachedTotalRows = 0
return result, nil
}
// All other cases are invalid. Destroy any cache references on the way out the door.
src.cachedRows = nil
src.cachedSchema = nil
src.cachedNextToken = ""
src.cachedTotalRows = 0
return nil, errNoCacheData
}
6 changes: 4 additions & 2 deletions bigquery/iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ func TestRowIteratorCacheBehavior(t *testing.T) {
{
// primary success case: schema in cache
inSource: &rowSource{
cachedSchema: testSchema,
cachedRows: testRows,
cachedSchema: testSchema,
cachedRows: testRows,
cachedTotalRows: uint64(len(convertedRows)),
},
wantResult: &fetchPageResult{
totalRows: uint64(len(convertedRows)),
Expand All @@ -94,6 +95,7 @@ func TestRowIteratorCacheBehavior(t *testing.T) {
inSource: &rowSource{
cachedRows: testRows,
cachedNextToken: "foo",
cachedTotalRows: uint64(len(convertedRows)),
},
inSchema: convertedSchema,
wantResult: &fetchPageResult{
Expand Down
1 change: 1 addition & 0 deletions bigquery/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ func (q *Query) Read(ctx context.Context) (it *RowIterator, err error) {
cachedRows: resp.Rows,
cachedSchema: resp.Schema,
cachedNextToken: resp.PageToken,
cachedTotalRows: resp.TotalRows,
}
return newRowIterator(ctx, rowSource, fetchPage), nil
}
Expand Down
Loading