diff --git a/bigquery/storage_client.go b/bigquery/storage_client.go index a1fae7674b7e..8c48ed308ca1 100644 --- a/bigquery/storage_client.go +++ b/bigquery/storage_client.go @@ -107,15 +107,8 @@ func (c *readClient) sessionForTable(ctx context.Context, table *Table, rsProjec settings.maxStreamCount = 1 } - // configure where the read session is created - readSessionProjectID := table.ProjectID - if useClientProject { - readSessionProjectID = c.projectID - } - rs := &readSession{ ctx: ctx, - readSessionProjectID: readSessionProjectID, table: table, tableID: tableID, projectID: rsProjectID, diff --git a/bigquery/storage_integration_test.go b/bigquery/storage_integration_test.go index 492561c8a488..d1fac66add5b 100644 --- a/bigquery/storage_integration_test.go +++ b/bigquery/storage_integration_test.go @@ -112,6 +112,15 @@ func TestIntegration_StorageReadClientProject(t *testing.T) { if !strings.HasPrefix(session.bqSession.Name, expectedPrefix) { t.Fatalf("expected read session to have prefix %q: but found %s:", expectedPrefix, session.bqSession.Name) } + + it = table.Read(ctx, WithReadSessionProject("bigquery-public-data")) + _, err = countIteratorRows(it) + if err != nil { + t.Fatal(err) + } + if it.IsAccelerated() { + t.Fatal("expected storage api to not be used") + } } func TestIntegration_StorageReadFromSources(t *testing.T) { diff --git a/bigquery/table.go b/bigquery/table.go index 012d58aba790..56058f46daf5 100644 --- a/bigquery/table.go +++ b/bigquery/table.go @@ -968,17 +968,16 @@ func (t *Table) Delete(ctx context.Context) (err error) { } type tableReadOption struct { - useClientProject bool + readSessionProject string } // TableReadOption allows requests to alter the behavior of reading from a table. type TableReadOption func(*tableReadOption) -// WithClientProject allows the read session to be created from the client project -// when reading from the table, instead of the table's project. -func WithClientProject() TableReadOption { +// WithReadSessionProject allows to create the read session with the specified project that has the necessary permissions to do so. +func WithReadSessionProject(project string) TableReadOption { return func(tro *tableReadOption) { - tro.useClientProject = true + tro.readSessionProject = project } } @@ -988,13 +987,17 @@ func (t *Table) Read(ctx context.Context, opts ...TableReadOption) *RowIterator } func (t *Table) read(ctx context.Context, pf pageFetcher, opts ...TableReadOption) *RowIterator { - tro := &tableReadOption{useClientProject: false} + tro := &tableReadOption{} for _, o := range opts { o(tro) } + if tro.readSessionProject == "" { + tro.readSessionProject = t.c.projectID + } + if t.c.isStorageReadAvailable() { - it, err := newStorageRowIteratorFromTable(ctx, t, false, tro.useClientProject) + it, err := newStorageRowIteratorFromTable(ctx, t, tro.readSessionProject, false) if err == nil { return it }