From 363ba03e1c3c813749a65ff3c050877ce4f60016 Mon Sep 17 00:00:00 2001 From: shollyman Date: Mon, 14 Jun 2021 13:14:50 -0700 Subject: [PATCH] feat(bigquery): support job deletion (#3935) * feat(bigquery): support job deletion --- bigquery/integration_test.go | 23 +++++++++++++++++++++++ bigquery/job.go | 17 +++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/bigquery/integration_test.go b/bigquery/integration_test.go index 4206a44bc64..f9b160911f9 100644 --- a/bigquery/integration_test.go +++ b/bigquery/integration_test.go @@ -2582,6 +2582,29 @@ func TestIntegration_ListJobs(t *testing.T) { } } +func TestIntegration_DeleteJob(t *testing.T) { + if client == nil { + t.Skip("Integration tests skipped") + } + ctx := context.Background() + + q := client.Query("SELECT 17 as foo") + q.Location = "us-east1" + + job, err := q.Run(ctx) + if err != nil { + t.Fatalf("job Run failure: %v", err) + } + _, err = job.Wait(ctx) + if err != nil { + t.Fatalf("job completion failure: %v", err) + } + + if err := job.Delete(ctx); err != nil { + t.Fatalf("job.Delete failed: %v", err) + } +} + const tokyo = "asia-northeast1" func TestIntegration_Location(t *testing.T) { diff --git a/bigquery/job.go b/bigquery/job.go index 1e08e899c51..2d259f910b4 100644 --- a/bigquery/job.go +++ b/bigquery/job.go @@ -233,6 +233,23 @@ func (j *Job) Cancel(ctx context.Context) error { }) } +// Delete deletes the job. +func (j *Job) Delete(ctx context.Context) (err error) { + ctx = trace.StartSpan(ctx, "cloud.google.com/go/bigquery.Job.Delete") + defer func() { trace.EndSpan(ctx, err) }() + + call := j.c.bqs.Jobs.Delete(j.projectID, j.jobID).Context(ctx) + if j.location != "" { + call = call.Location(j.location) + } + setClientHeader(call.Header()) + + return runWithRetry(ctx, func() (err error) { + err = call.Do() + return err + }) +} + // Wait blocks until the job or the context is done. It returns the final status // of the job. // If an error occurs while retrieving the status, Wait returns that error. But