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): add ProjectID to JobIDConfig #8405

Merged
merged 3 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 9 additions & 4 deletions bigquery/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (c *Client) JobFromIDLocation(ctx context.Context, id, location string) (j
return c.JobFromProject(ctx, c.projectID, id, location)
}

// JobFromProject creates a Job which refers to an existing BigQuery job. The job
// JobFromProject creates a Job which refers to an existing BigQuery job. The job
// need not have been created by this package, nor does it need to reside within the same
// project or location as the instantiated client.
func (c *Client) JobFromProject(ctx context.Context, projectID, jobID, location string) (j *Job, err error) {
Expand Down Expand Up @@ -170,17 +170,22 @@ type JobIDConfig struct {

// Location is the location for the job.
Location string

// ProjectID is the Gooogle Cloud project associated with the job.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/Gooogle/Google/

ProjectID string
}

// createJobRef creates a JobReference.
func (j *JobIDConfig) createJobRef(c *Client) *bq.JobReference {
// We don't check whether projectID is empty; the server will return an
// error when it encounters the resulting JobReference.
projectID := j.ProjectID
if projectID == "" { // Use Client.ProjectID as a default.
projectID = c.projectID
}
loc := j.Location
if loc == "" { // Use Client.Location as a default.
loc = c.Location
}
jr := &bq.JobReference{ProjectId: c.projectID, Location: loc}
jr := &bq.JobReference{ProjectId: projectID, Location: loc}
if j.JobID == "" {
jr.JobId = randomIDFn()
} else if j.AddJobIDSuffix {
Expand Down
8 changes: 7 additions & 1 deletion bigquery/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,19 @@ func TestCreateJobRef(t *testing.T) {
client: cLoc,
want: &bq.JobReference{JobId: "foo", Location: "loc"},
},
{
in: JobIDConfig{JobID: "foo", ProjectID: "anotherProj"},
want: &bq.JobReference{JobId: "foo", ProjectId: "anotherProj"},
},
} {
client := test.client
if client == nil {
client = cNoLoc
}
got := test.in.createJobRef(client)
test.want.ProjectId = "projectID"
if test.want.ProjectId == "" {
test.want.ProjectId = "projectID"
}
if !testutil.Equal(got, test.want) {
t.Errorf("%+v: got %+v, want %+v", test.in, got, test.want)
}
Expand Down
25 changes: 20 additions & 5 deletions bigquery/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ func TestQuery(t *testing.T) {
return j
}(),
},
{
dst: c.DatasetInProject("another-project", "dataset-id").Table("table-id"),
jobIDConfig: JobIDConfig{JobID: "jobID", ProjectID: "another-project"},
src: &QueryConfig{Q: "query string"},
want: func() *bq.Job {
j := defaultQueryJob()
j.Configuration.Query.DefaultDataset = nil
j.Configuration.Query.DestinationTable.ProjectId = "another-project"
j.JobReference.JobId = "jobID"
j.JobReference.ProjectId = "another-project"
return j
}(),
},
{
dst: &Table{},
src: defaultQuery,
Expand Down Expand Up @@ -415,10 +428,9 @@ func TestProbeFastPath(t *testing.T) {
}
pfalse := false
testCases := []struct {
inCfg QueryConfig
inJobCfg JobIDConfig
wantReq *bq.QueryRequest
wantErr bool
inCfg QueryConfig
wantReq *bq.QueryRequest
wantErr bool
}{
{
inCfg: QueryConfig{
Expand Down Expand Up @@ -490,7 +502,10 @@ func TestProbeFastPath(t *testing.T) {
},
}
for i, tc := range testCases {
in := &Query{tc.inJobCfg, tc.inCfg, c}
in := &Query{
QueryConfig: tc.inCfg,
client: c,
}
gotReq, err := in.probeFastPath()
if tc.wantErr && err == nil {
t.Errorf("case %d wanted error, got nil", i)
Expand Down