query_op.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2015 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package bigquery
  15. import (
  16. "fmt"
  17. "golang.org/x/net/context"
  18. bq "google.golang.org/api/bigquery/v2"
  19. )
  20. type queryOption interface {
  21. customizeQuery(conf *bq.JobConfigurationQuery, projectID string)
  22. }
  23. // DisableQueryCache returns an Option that prevents results being fetched from the query cache.
  24. // If this Option is not used, results are fetched from the cache if they are available.
  25. // The query cache is a best-effort cache that is flushed whenever tables in the query are modified.
  26. // Cached results are only available when TableID is unspecified in the query's destination Table.
  27. // For more information, see https://cloud.google.com/bigquery/querying-data#querycaching
  28. func DisableQueryCache() Option { return disableQueryCache{} }
  29. type disableQueryCache struct{}
  30. func (opt disableQueryCache) implementsOption() {}
  31. func (opt disableQueryCache) customizeQuery(conf *bq.JobConfigurationQuery, projectID string) {
  32. f := false
  33. conf.UseQueryCache = &f
  34. }
  35. // JobPriority returns an Option that causes a query to be scheduled with the specified priority.
  36. // The default priority is InteractivePriority.
  37. // For more information, see https://cloud.google.com/bigquery/querying-data#batchqueries
  38. func JobPriority(priority string) Option { return jobPriority(priority) }
  39. type jobPriority string
  40. func (opt jobPriority) implementsOption() {}
  41. func (opt jobPriority) customizeQuery(conf *bq.JobConfigurationQuery, projectID string) {
  42. conf.Priority = string(opt)
  43. }
  44. const (
  45. BatchPriority = "BATCH"
  46. InteractivePriority = "INTERACTIVE"
  47. )
  48. // TODO(mcgreevy): support large results.
  49. // TODO(mcgreevy): support non-flattened results.
  50. func (c *Client) query(ctx context.Context, dst *Table, src *Query, options []Option) (*Job, error) {
  51. job, options := initJobProto(c.projectID, options)
  52. payload := &bq.JobConfigurationQuery{}
  53. dst.customizeQueryDst(payload, c.projectID)
  54. src.customizeQuerySrc(payload, c.projectID)
  55. for _, opt := range options {
  56. o, ok := opt.(queryOption)
  57. if !ok {
  58. return nil, fmt.Errorf("option (%#v) not applicable to dst/src pair: dst: %T ; src: %T", opt, dst, src)
  59. }
  60. o.customizeQuery(payload, c.projectID)
  61. }
  62. job.Configuration = &bq.JobConfiguration{
  63. Query: payload,
  64. }
  65. j, err := c.service.insertJob(ctx, job, c.projectID)
  66. if err != nil {
  67. return nil, err
  68. }
  69. j.isQuery = true
  70. return j, nil
  71. }