Skip to content
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

feat(bigquery): expose query id on row iterator if available #9224

Merged
merged 3 commits into from
Jan 5, 2024
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
13 changes: 11 additions & 2 deletions bigquery/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ func (ri *RowIterator) SourceJob() *Job {
}
}

// QueryID returns a query ID if available, or an empty string.
func (ri *RowIterator) QueryID() string {
if ri.src == nil {
return ""
}
return ri.src.queryID
}

// We declare a function signature for fetching results. The primary reason
// for this is to enable us to swap out the fetch function with alternate
// implementations (e.g. to enable testing).
Expand Down Expand Up @@ -210,8 +218,9 @@ func (it *RowIterator) fetch(pageSize int, pageToken string) (string, error) {
// want to retain the data unnecessarily, and we expect that the backend
// can always provide them if needed.
type rowSource struct {
j *Job
t *Table
j *Job
t *Table
queryID string

cachedRows []*bq.TableRow
cachedSchema *bq.TableSchema
Expand Down
33 changes: 33 additions & 0 deletions bigquery/iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,3 +510,36 @@ func TestIteratorSourceJob(t *testing.T) {
}
}
}

func TestIteratorQueryID(t *testing.T) {
testcases := []struct {
description string
src *rowSource
want string
}{
{
description: "nil source",
src: nil,
want: "",
},
{
description: "empty source",
src: &rowSource{},
want: "",
},
{
description: "populated id",
src: &rowSource{queryID: "foo"},
want: "foo",
},
}

for _, tc := range testcases {
// Don't pass a page func, we're not reading from the iterator.
it := newRowIterator(context.Background(), tc.src, nil)
got := it.QueryID()
if got != tc.want {
t.Errorf("%s: mismatch queryid, got %q want %q", tc.description, got, tc.want)
}
}
}
3 changes: 2 additions & 1 deletion bigquery/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,8 @@ func (q *Query) Read(ctx context.Context) (it *RowIterator, err error) {
}
}
rowSource := &rowSource{
j: minimalJob,
j: minimalJob,
queryID: resp.QueryId,
// RowIterator can precache results from the iterator to save a lookup.
cachedRows: resp.Rows,
cachedSchema: resp.Schema,
Expand Down
Loading