From a8f36049b3dd900d1b336783641bbf048fc252f5 Mon Sep 17 00:00:00 2001 From: Greg Russell Date: Thu, 14 Dec 2017 14:57:17 -0500 Subject: [PATCH] Make names consistent, use Query, and allow unpartitioned dest --- bqext/dataset.go | 24 +++++++++++++++--------- bqext/dataset_test.go | 6 +++--- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/bqext/dataset.go b/bqext/dataset.go index 74f1b0e..1c64ee8 100644 --- a/bqext/dataset.go +++ b/bqext/dataset.go @@ -145,7 +145,7 @@ func (dsExt Dataset) GetPartitionInfo(table string, partition string) (Partition // writing results to a table. // If dest is nil, then this will create a DryRun query. // TODO - should disposition be an opts... field instead? -func (dsExt *Dataset) DestinationQuery(query string, dest *bigquery.Table, disposition bigquery.TableWriteDisposition) *bigquery.Query { +func (dsExt *Dataset) DestQuery(query string, dest *bigquery.Table, disposition bigquery.TableWriteDisposition) *bigquery.Query { q := dsExt.BqClient.Query(query) if dest != nil { q.QueryConfig.Dst = dest @@ -161,9 +161,11 @@ func (dsExt *Dataset) DestinationQuery(query string, dest *bigquery.Table, dispo return q } -// ExecDestQuery constructs a destination query, executes it, and returns status or error. -func (dsExt *Dataset) ExecDestQuery(query string, disposition bigquery.TableWriteDisposition, destTable *bigquery.Table) (*bigquery.JobStatus, error) { - q := dsExt.DestinationQuery(query, destTable, disposition) +// ExecDestQuery executes a destination or dryrun query, and returns status or error. +func (dsExt *Dataset) ExecDestQuery(q *bigquery.Query) (*bigquery.JobStatus, error) { + if q.QueryConfig.Dst == nil && q.QueryConfig.DryRun == false { + return nil, errors.New("query must be a destination or dry run") + } job, err := q.Run(context.Background()) if err != nil { return nil, err @@ -200,14 +202,18 @@ var dedupTemplate = ` // `destTable` specifies the table to write to, typically created with // dsExt.BqClient.DatasetInProject(...).Table(...) // -// NOTE: destination table MUST include the partition suffix. This -// avoids accidentally overwriting the entire table. -// TODO(gfr) Support non-partitioned table destination. +// NOTE: If destination table is partitioned, destTable MUST include the partition +// suffix to avoid accidentally overwriting the entire table. func (dsExt *Dataset) Dedup_Alpha(src string, dedupOn string, destTable *bigquery.Table) (*bigquery.JobStatus, error) { if !strings.Contains(destTable.TableID, "$") { - return nil, errors.New("Destination table does not specify partition") + meta, err := destTable.Metadata(context.Background()) + if err == nil && meta.TimePartitioning != nil { + log.Println(err) + return nil, errors.New("Destination table must specify partition") + } } log.Printf("Removing dups (of %s) and writing to %s\n", dedupOn, destTable.TableID) queryString := fmt.Sprintf(dedupTemplate, dedupOn, src) - return dsExt.ExecDestQuery(queryString, bigquery.WriteTruncate, destTable) + query := dsExt.DestQuery(queryString, destTable, bigquery.WriteTruncate) + return dsExt.ExecDestQuery(query) } diff --git a/bqext/dataset_test.go b/bqext/dataset_test.go index bf07bc1..b7fae5a 100644 --- a/bqext/dataset_test.go +++ b/bqext/dataset_test.go @@ -130,7 +130,7 @@ func TestResultQuery(t *testing.T) { // This test only check very basic stuff. Intended mostly just to // improve coverage metrics. -func TestDestinationQuery(t *testing.T) { +func TestDestQuery(t *testing.T) { // Create a dummy client. opts := []option.ClientOption{option.WithHTTPClient(getOKClient())} dsExt, err := bqext.NewDataset("mock", "mock", opts...) @@ -138,7 +138,7 @@ func TestDestinationQuery(t *testing.T) { t.Fatal(err) } - q := dsExt.DestinationQuery("query string", nil, bigquery.WriteEmpty) + q := dsExt.DestQuery("query string", nil, bigquery.WriteEmpty) qc := q.QueryConfig if qc.Dst != nil { t.Error("Destination should be nil.") @@ -147,7 +147,7 @@ func TestDestinationQuery(t *testing.T) { t.Error("DryRun should be set.") } - q = dsExt.DestinationQuery("query string", dsExt.Table("foobar"), bigquery.WriteEmpty) + q = dsExt.DestQuery("query string", dsExt.Table("foobar"), bigquery.WriteEmpty) qc = q.QueryConfig if qc.Dst.TableID != "foobar" { t.Error("Destination should be foobar.")