query_op.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. // DisableFlattenedResults returns an Option that prevents results being flattened.
  36. // If this Option is not used, results from nested and repeated fields are flattened.
  37. // DisableFlattenedResults implies AllowLargeResults
  38. // For more information, see https://cloud.google.com/bigquery/docs/data#nested
  39. func DisableFlattenedResults() Option { return disableFlattenedResults{} }
  40. type disableFlattenedResults struct{}
  41. func (opt disableFlattenedResults) implementsOption() {}
  42. func (opt disableFlattenedResults) customizeQuery(conf *bq.JobConfigurationQuery, projectID string) {
  43. f := false
  44. conf.FlattenResults = &f
  45. // DisableFlattenedResults implies AllowLargeResults
  46. allowLargeResults{}.customizeQuery(conf, projectID)
  47. }
  48. // AllowLargeResults returns an Option that allows the query to produce arbitrarily large result tables.
  49. // The destination must be a table.
  50. // When using this option, queries will take longer to execute, even if the result set is small.
  51. // For additional limitations, see https://cloud.google.com/bigquery/querying-data#largequeryresults
  52. func AllowLargeResults() Option { return allowLargeResults{} }
  53. type allowLargeResults struct{}
  54. func (opt allowLargeResults) implementsOption() {}
  55. func (opt allowLargeResults) customizeQuery(conf *bq.JobConfigurationQuery, projectID string) {
  56. conf.AllowLargeResults = true
  57. }
  58. // JobPriority returns an Option that causes a query to be scheduled with the specified priority.
  59. // The default priority is InteractivePriority.
  60. // For more information, see https://cloud.google.com/bigquery/querying-data#batchqueries
  61. func JobPriority(priority string) Option { return jobPriority(priority) }
  62. type jobPriority string
  63. func (opt jobPriority) implementsOption() {}
  64. func (opt jobPriority) customizeQuery(conf *bq.JobConfigurationQuery, projectID string) {
  65. conf.Priority = string(opt)
  66. }
  67. const (
  68. BatchPriority = "BATCH"
  69. InteractivePriority = "INTERACTIVE"
  70. )
  71. func (c *Client) query(ctx context.Context, dst *Table, src *Query, options []Option) (*Job, error) {
  72. job, options := initJobProto(c.projectID, options)
  73. payload := &bq.JobConfigurationQuery{}
  74. dst.customizeQueryDst(payload, c.projectID)
  75. src.customizeQuerySrc(payload, c.projectID)
  76. for _, opt := range options {
  77. o, ok := opt.(queryOption)
  78. if !ok {
  79. return nil, fmt.Errorf("option (%#v) not applicable to dst/src pair: dst: %T ; src: %T", opt, dst, src)
  80. }
  81. o.customizeQuery(payload, c.projectID)
  82. }
  83. job.Configuration = &bq.JobConfiguration{
  84. Query: payload,
  85. }
  86. j, err := c.service.insertJob(ctx, job, c.projectID)
  87. if err != nil {
  88. return nil, err
  89. }
  90. j.isQuery = true
  91. return j, nil
  92. }