diff --git a/bigquery/job.go b/bigquery/job.go index e6be579c07a1..0ce79c111689 100644 --- a/bigquery/job.go +++ b/bigquery/job.go @@ -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) { @@ -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 { diff --git a/bigquery/job_test.go b/bigquery/job_test.go index fbde305a85a1..c4e28abfb129 100644 --- a/bigquery/job_test.go +++ b/bigquery/job_test.go @@ -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) } diff --git a/bigquery/query_test.go b/bigquery/query_test.go index 39d121b8f253..7a3e22f5feb1 100644 --- a/bigquery/query_test.go +++ b/bigquery/query_test.go @@ -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, @@ -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{ @@ -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)