Skip to content

Commit

Permalink
feat(bigquery): add ProjectID to JobIDConfig (#8405)
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarowolfx committed Aug 11, 2023
1 parent c25b422 commit 7fafd80
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
13 changes: 9 additions & 4 deletions bigquery/job.go
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 Google Cloud project associated with the job.
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
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
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

0 comments on commit 7fafd80

Please sign in to comment.