Skip to content

Commit

Permalink
change to WithReadSessionProject option in anticipation of #10932
Browse files Browse the repository at this point in the history
  • Loading branch information
juanli16 committed Oct 1, 2024
1 parent 3aa58b0 commit 9d5da3c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
7 changes: 0 additions & 7 deletions bigquery/storage_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions bigquery/storage_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 10 additions & 7 deletions bigquery/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -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
}
Expand Down

0 comments on commit 9d5da3c

Please sign in to comment.