google-cloud-bigquery 0.21.0 → 0.23.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -74,6 +74,7 @@ module Google
74
74
  # require "google/cloud/bigquery"
75
75
  #
76
76
  # bigquery = Google::Cloud::Bigquery.new
77
+ # dataset = bigquery.dataset "my_dataset"
77
78
  # table = dataset.table "my_table"
78
79
  #
79
80
  # data = table.data
@@ -94,6 +95,7 @@ module Google
94
95
  # require "google/cloud/bigquery"
95
96
  #
96
97
  # bigquery = Google::Cloud::Bigquery.new
98
+ # dataset = bigquery.dataset "my_dataset"
97
99
  # table = dataset.table "my_table"
98
100
  #
99
101
  # data = table.data
@@ -129,6 +131,7 @@ module Google
129
131
  # require "google/cloud/bigquery"
130
132
  #
131
133
  # bigquery = Google::Cloud::Bigquery.new
134
+ # dataset = bigquery.dataset "my_dataset"
132
135
  # table = dataset.table "my_table"
133
136
  #
134
137
  # table.data.all do |row|
@@ -139,6 +142,7 @@ module Google
139
142
  # require "google/cloud/bigquery"
140
143
  #
141
144
  # bigquery = Google::Cloud::Bigquery.new
145
+ # dataset = bigquery.dataset "my_dataset"
142
146
  # table = dataset.table "my_table"
143
147
  #
144
148
  # words = table.data.all.map do |row|
@@ -149,6 +153,7 @@ module Google
149
153
  # require "google/cloud/bigquery"
150
154
  #
151
155
  # bigquery = Google::Cloud::Bigquery.new
156
+ # dataset = bigquery.dataset "my_dataset"
152
157
  # table = dataset.table "my_table"
153
158
  #
154
159
  # table.data.all(request_limit: 10) do |row|
@@ -179,7 +179,7 @@ module Google
179
179
  def created_at
180
180
  ensure_full_data!
181
181
  begin
182
- Time.at(Integer(@gapi.creation_time) / 1000.0)
182
+ ::Time.at(Integer(@gapi.creation_time) / 1000.0)
183
183
  rescue
184
184
  nil
185
185
  end
@@ -193,7 +193,7 @@ module Google
193
193
  def modified_at
194
194
  ensure_full_data!
195
195
  begin
196
- Time.at(Integer(@gapi.last_modified_time) / 1000.0)
196
+ ::Time.at(Integer(@gapi.last_modified_time) / 1000.0)
197
197
  rescue
198
198
  nil
199
199
  end
@@ -229,14 +229,14 @@ module Google
229
229
  # bigquery = Google::Cloud::Bigquery.new
230
230
  # dataset = bigquery.dataset "my_dataset"
231
231
  #
232
- # dataset.access #=> [{"role"=>"OWNER",
233
- # # "specialGroup"=>"projectOwners"},
234
- # # {"role"=>"WRITER",
235
- # # "specialGroup"=>"projectWriters"},
236
- # # {"role"=>"READER",
237
- # # "specialGroup"=>"projectReaders"},
238
- # # {"role"=>"OWNER",
239
- # # "userByEmail"=>"123456789-...com"}]
232
+ # dataset.access # [{"role"=>"OWNER",
233
+ # # "specialGroup"=>"projectOwners"},
234
+ # # {"role"=>"WRITER",
235
+ # # "specialGroup"=>"projectWriters"},
236
+ # # {"role"=>"READER",
237
+ # # "specialGroup"=>"projectReaders"},
238
+ # # {"role"=>"OWNER",
239
+ # # "userByEmail"=>"123456789-...com"}]
240
240
  #
241
241
  # @example Manage the access rules by passing a block:
242
242
  # require "google/cloud/bigquery"
@@ -322,7 +322,7 @@ module Google
322
322
  #
323
323
  # bigquery = Google::Cloud::Bigquery.new
324
324
  # dataset = bigquery.dataset "my_dataset"
325
- # table = dataset.create_table "my_table"
325
+ # table = dataset.create_table "my_table",
326
326
  # name: "My Table",
327
327
  # description: "A description of table."
328
328
  #
@@ -336,7 +336,7 @@ module Google
336
336
  # Google::Cloud::Bigquery::Schema::Field.new(
337
337
  # "first_name", :string, mode: :required),
338
338
  # Google::Cloud::Bigquery::Schema::Field.new(
339
- # "cities_lived", :record, mode: :repeated
339
+ # "cities_lived", :record, mode: :repeated,
340
340
  # fields: [
341
341
  # Google::Cloud::Bigquery::Schema::Field.new(
342
342
  # "place", :string, mode: :required),
@@ -520,10 +520,37 @@ module Google
520
520
  # Sets the current dataset as the default dataset in the query. Useful
521
521
  # for using unqualified table names.
522
522
  #
523
+ # When using standard SQL and passing arguments using `params`, Ruby
524
+ # types are mapped to BigQuery types as follows:
525
+ #
526
+ # | BigQuery | Ruby | Notes |
527
+ # |-------------|----------------|---|
528
+ # | `BOOL` | `true`/`false` | |
529
+ # | `INT64` | `Integer` | |
530
+ # | `FLOAT64` | `Float` | |
531
+ # | `STRING` | `STRING` | |
532
+ # | `DATETIME` | `DateTime` | `DATETIME` does not support time zone. |
533
+ # | `DATE` | `Date` | |
534
+ # | `TIMESTAMP` | `Time` | |
535
+ # | `TIME` | `Google::Cloud::BigQuery::Time` | |
536
+ # | `BYTES` | `File`, `IO`, `StringIO`, or similar | |
537
+ # | `ARRAY` | `Array` | Nested arrays, `nil` values are not supported. |
538
+ # | `STRUCT` | `Hash` | Hash keys may be strings or symbols. |
539
+ #
540
+ # See [Data Types](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types)
541
+ # for an overview of each BigQuery data type, including allowed values.
542
+ #
523
543
  # @param [String] query A query string, following the BigQuery [query
524
544
  # syntax](https://cloud.google.com/bigquery/query-reference), of the
525
545
  # query to execute. Example: "SELECT count(f1) FROM
526
546
  # [myProjectId:myDatasetId.myTableId]".
547
+ # @param [Array, Hash] params Standard SQL only. Used to pass query
548
+ # arguments when the `query` string contains either positional (`?`)
549
+ # or named (`@myparam`) query parameters. If value passed is an array
550
+ # `["foo"]`, the query must use positional query parameters. If value
551
+ # passed is a hash `{ myparam: "foo" }`, the query must use named
552
+ # query parameters. When set, `legacy_sql` will automatically be set
553
+ # to false and `standard_sql` to true.
527
554
  # @param [String] priority Specifies a priority for the query. Possible
528
555
  # values include `INTERACTIVE` and `BATCH`. The default value is
529
556
  # `INTERACTIVE`.
@@ -558,6 +585,25 @@ module Google
558
585
  # @param [Boolean] flatten Flattens all nested and repeated fields in
559
586
  # the query results. The default value is `true`. `large_results`
560
587
  # parameter must be `true` if this is set to `false`.
588
+ # @param [Boolean] legacy_sql Specifies whether to use BigQuery's
589
+ # [legacy
590
+ # SQL](https://cloud.google.com/bigquery/docs/reference/legacy-sql)
591
+ # dialect for this query. If set to false, the query will use
592
+ # BigQuery's [standard
593
+ # SQL](https://cloud.google.com/bigquery/docs/reference/standard-sql/)
594
+ # When set to false, the values of `large_results` and `flatten` are
595
+ # ignored; the query will be run as if `large_results` is true and
596
+ # `flatten` is false. Optional. The default value is true.
597
+ # @param [Boolean] standard_sql Specifies whether to use BigQuery's
598
+ # [standard
599
+ # SQL](https://cloud.google.com/bigquery/docs/reference/standard-sql/)
600
+ # dialect for this query. If set to true, the query will use standard
601
+ # SQL rather than the [legacy
602
+ # SQL](https://cloud.google.com/bigquery/docs/reference/legacy-sql)
603
+ # dialect. When set to true, the values of `large_results` and
604
+ # `flatten` are ignored; the query will be run as if `large_results`
605
+ # is true and `flatten` is false. Optional. The default value is
606
+ # false.
561
607
  #
562
608
  # @return [Google::Cloud::Bigquery::QueryJob]
563
609
  #
@@ -575,13 +621,61 @@ module Google
575
621
  # end
576
622
  # end
577
623
  #
624
+ # @example Query using standard SQL:
625
+ # require "google/cloud/bigquery"
626
+ #
627
+ # bigquery = Google::Cloud::Bigquery.new
628
+ #
629
+ # job = bigquery.query_job "SELECT name FROM my_table",
630
+ # standard_sql: true
631
+ #
632
+ # job.wait_until_done!
633
+ # if !job.failed?
634
+ # job.query_results.each do |row|
635
+ # puts row["name"]
636
+ # end
637
+ # end
638
+ #
639
+ # @example Query using positional query parameters:
640
+ # require "google/cloud/bigquery"
641
+ #
642
+ # bigquery = Google::Cloud::Bigquery.new
643
+ #
644
+ # job = bigquery.query_job "SELECT name FROM my_table WHERE id = ?",
645
+ # params: [1]
646
+ #
647
+ # job.wait_until_done!
648
+ # if !job.failed?
649
+ # job.query_results.each do |row|
650
+ # puts row["name"]
651
+ # end
652
+ # end
653
+ #
654
+ # @example Query using named query parameters:
655
+ # require "google/cloud/bigquery"
656
+ #
657
+ # bigquery = Google::Cloud::Bigquery.new
658
+ #
659
+ # job = bigquery.query_job "SELECT name FROM my_table WHERE id = @id",
660
+ # params: { id: 1 }
661
+ #
662
+ # job.wait_until_done!
663
+ # if !job.failed?
664
+ # job.query_results.each do |row|
665
+ # puts row["name"]
666
+ # end
667
+ # end
668
+ #
578
669
  # @!group Data
579
670
  #
580
- def query_job query, priority: "INTERACTIVE", cache: true, table: nil,
581
- create: nil, write: nil, large_results: nil, flatten: nil
671
+ def query_job query, params: nil, priority: "INTERACTIVE", cache: true,
672
+ table: nil, create: nil, write: nil, large_results: nil,
673
+ flatten: nil, legacy_sql: nil, standard_sql: nil
582
674
  options = { priority: priority, cache: cache, table: table,
583
675
  create: create, write: write,
584
- large_results: large_results, flatten: flatten }
676
+ large_results: large_results, flatten: flatten,
677
+ legacy_sql: legacy_sql, standard_sql: standard_sql,
678
+ params: params }
585
679
  options[:dataset] ||= self
586
680
  ensure_service!
587
681
  gapi = service.query_job query, options
@@ -595,10 +689,37 @@ module Google
595
689
  # Sets the current dataset as the default dataset in the query. Useful
596
690
  # for using unqualified table names.
597
691
  #
692
+ # When using standard SQL and passing arguments using `params`, Ruby
693
+ # types are mapped to BigQuery types as follows:
694
+ #
695
+ # | BigQuery | Ruby | Notes |
696
+ # |-------------|----------------|---|
697
+ # | `BOOL` | `true`/`false` | |
698
+ # | `INT64` | `Integer` | |
699
+ # | `FLOAT64` | `Float` | |
700
+ # | `STRING` | `STRING` | |
701
+ # | `DATETIME` | `DateTime` | `DATETIME` does not support time zone. |
702
+ # | `DATE` | `Date` | |
703
+ # | `TIMESTAMP` | `Time` | |
704
+ # | `TIME` | `Google::Cloud::BigQuery::Time` | |
705
+ # | `BYTES` | `File`, `IO`, `StringIO`, or similar | |
706
+ # | `ARRAY` | `Array` | Nested arrays, `nil` values are not supported. |
707
+ # | `STRUCT` | `Hash` | Hash keys may be strings or symbols. |
708
+ #
709
+ # See [Data Types](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types)
710
+ # for an overview of each BigQuery data type, including allowed values.
711
+ #
598
712
  # @param [String] query A query string, following the BigQuery [query
599
713
  # syntax](https://cloud.google.com/bigquery/query-reference), of the
600
714
  # query to execute. Example: "SELECT count(f1) FROM
601
715
  # [myProjectId:myDatasetId.myTableId]".
716
+ # @param [Array, Hash] params Standard SQL only. Used to pass query
717
+ # arguments when the `query` string contains either positional (`?`)
718
+ # or named (`@myparam`) query parameters. If value passed is an array
719
+ # `["foo"]`, the query must use positional query parameters. If value
720
+ # passed is a hash `{ myparam: "foo" }`, the query must use named
721
+ # query parameters. When set, `legacy_sql` will automatically be set
722
+ # to false and `standard_sql` to true.
602
723
  # @param [Integer] max The maximum number of rows of data to return per
603
724
  # page of results. Setting this flag to a small value such as 1000 and
604
725
  # then paging through results might improve reliability when the query
@@ -620,6 +741,25 @@ module Google
620
741
  # whenever tables in the query are modified. The default value is
621
742
  # true. For more information, see [query
622
743
  # caching](https://developers.google.com/bigquery/querying-data).
744
+ # @param [Boolean] legacy_sql Specifies whether to use BigQuery's
745
+ # [legacy
746
+ # SQL](https://cloud.google.com/bigquery/docs/reference/legacy-sql)
747
+ # dialect for this query. If set to false, the query will use
748
+ # BigQuery's [standard
749
+ # SQL](https://cloud.google.com/bigquery/docs/reference/standard-sql/)
750
+ # When set to false, the values of `large_results` and `flatten` are
751
+ # ignored; the query will be run as if `large_results` is true and
752
+ # `flatten` is false. Optional. The default value is true.
753
+ # @param [Boolean] standard_sql Specifies whether to use BigQuery's
754
+ # [standard
755
+ # SQL](https://cloud.google.com/bigquery/docs/reference/standard-sql/)
756
+ # dialect for this query. If set to true, the query will use standard
757
+ # SQL rather than the [legacy
758
+ # SQL](https://cloud.google.com/bigquery/docs/reference/legacy-sql)
759
+ # dialect. When set to true, the values of `large_results` and
760
+ # `flatten` are ignored; the query will be run as if `large_results`
761
+ # is true and `flatten` is false. Optional. The default value is
762
+ # false.
623
763
  #
624
764
  # @return [Google::Cloud::Bigquery::QueryData]
625
765
  #
@@ -629,14 +769,54 @@ module Google
629
769
  # bigquery = Google::Cloud::Bigquery.new
630
770
  #
631
771
  # data = bigquery.query "SELECT name FROM my_table"
772
+ #
773
+ # data.each do |row|
774
+ # puts row["name"]
775
+ # end
776
+ #
777
+ # @example Query using standard SQL:
778
+ # require "google/cloud/bigquery"
779
+ #
780
+ # bigquery = Google::Cloud::Bigquery.new
781
+ #
782
+ # data = bigquery.query "SELECT name FROM my_table",
783
+ # standard_sql: true
784
+ #
785
+ # data.each do |row|
786
+ # puts row["name"]
787
+ # end
788
+ #
789
+ # @example Query using positional query parameters:
790
+ # require "google/cloud/bigquery"
791
+ #
792
+ # bigquery = Google::Cloud::Bigquery.new
793
+ #
794
+ # data = bigquery.query "SELECT name FROM my_table WHERE id = ?",
795
+ # params: [1]
796
+ #
797
+ # data.each do |row|
798
+ # puts row["name"]
799
+ # end
800
+ #
801
+ # @example Query using named query parameters:
802
+ # require "google/cloud/bigquery"
803
+ #
804
+ # bigquery = Google::Cloud::Bigquery.new
805
+ #
806
+ # data = bigquery.query "SELECT name FROM my_table WHERE id = @id",
807
+ # params: { id: 1 }
808
+ #
632
809
  # data.each do |row|
633
810
  # puts row["name"]
634
811
  # end
635
812
  #
636
813
  # @!group Data
637
814
  #
638
- def query query, max: nil, timeout: 10000, dryrun: nil, cache: true
639
- options = { max: max, timeout: timeout, dryrun: dryrun, cache: cache }
815
+ def query query, params: nil, max: nil, timeout: 10000, dryrun: nil,
816
+ cache: true, legacy_sql: nil, standard_sql: nil
817
+ options = { max: max, timeout: timeout, dryrun: dryrun, cache: cache,
818
+ legacy_sql: legacy_sql, standard_sql: standard_sql,
819
+ params: params }
640
820
  options[:dataset] ||= dataset_id
641
821
  options[:project] ||= project_id
642
822
  ensure_service!
@@ -126,7 +126,7 @@ module Google
126
126
  ##
127
127
  # The time when the job was created.
128
128
  def created_at
129
- Time.at(Integer(@gapi.statistics.creation_time) / 1000.0)
129
+ ::Time.at(Integer(@gapi.statistics.creation_time) / 1000.0)
130
130
  rescue
131
131
  nil
132
132
  end
@@ -136,7 +136,7 @@ module Google
136
136
  # This field is present after the job's state changes from `PENDING`
137
137
  # to either `RUNNING` or `DONE`.
138
138
  def started_at
139
- Time.at(Integer(@gapi.statistics.start_time) / 1000.0)
139
+ ::Time.at(Integer(@gapi.statistics.start_time) / 1000.0)
140
140
  rescue
141
141
  nil
142
142
  end
@@ -145,7 +145,7 @@ module Google
145
145
  # The time when the job ended.
146
146
  # This field is present when the job's state is `DONE`.
147
147
  def ended_at
148
- Time.at(Integer(@gapi.statistics.end_time) / 1000.0)
148
+ ::Time.at(Integer(@gapi.statistics.end_time) / 1000.0)
149
149
  rescue
150
150
  nil
151
151
  end
@@ -21,6 +21,7 @@ require "google/cloud/bigquery/dataset"
21
21
  require "google/cloud/bigquery/job"
22
22
  require "google/cloud/bigquery/query_data"
23
23
  require "google/cloud/bigquery/project/list"
24
+ require "google/cloud/bigquery/time"
24
25
 
25
26
  module Google
26
27
  module Cloud
@@ -72,11 +73,11 @@ module Google
72
73
  # require "google/cloud/bigquery"
73
74
  #
74
75
  # bigquery = Google::Cloud::Bigquery.new(
75
- # project: "my-todo-project",
76
+ # project: "my-project-id",
76
77
  # keyfile: "/path/to/keyfile.json"
77
78
  # )
78
79
  #
79
- # bigquery.project #=> "my-todo-project"
80
+ # bigquery.project #=> "my-project-id"
80
81
  #
81
82
  def project
82
83
  service.project
@@ -95,10 +96,37 @@ module Google
95
96
  # Queries data using the [asynchronous
96
97
  # method](https://cloud.google.com/bigquery/querying-data).
97
98
  #
99
+ # When using standard SQL and passing arguments using `params`, Ruby
100
+ # types are mapped to BigQuery types as follows:
101
+ #
102
+ # | BigQuery | Ruby | Notes |
103
+ # |-------------|----------------|---|
104
+ # | `BOOL` | `true`/`false` | |
105
+ # | `INT64` | `Integer` | |
106
+ # | `FLOAT64` | `Float` | |
107
+ # | `STRING` | `STRING` | |
108
+ # | `DATETIME` | `DateTime` | `DATETIME` does not support time zone. |
109
+ # | `DATE` | `Date` | |
110
+ # | `TIMESTAMP` | `Time` | |
111
+ # | `TIME` | `Google::Cloud::BigQuery::Time` | |
112
+ # | `BYTES` | `File`, `IO`, `StringIO`, or similar | |
113
+ # | `ARRAY` | `Array` | Nested arrays, `nil` values are not supported. |
114
+ # | `STRUCT` | `Hash` | Hash keys may be strings or symbols. |
115
+ #
116
+ # See [Data Types](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types)
117
+ # for an overview of each BigQuery data type, including allowed values.
118
+ #
98
119
  # @param [String] query A query string, following the BigQuery [query
99
120
  # syntax](https://cloud.google.com/bigquery/query-reference), of the
100
121
  # query to execute. Example: "SELECT count(f1) FROM
101
122
  # [myProjectId:myDatasetId.myTableId]".
123
+ # @param [Array, Hash] params Standard SQL only. Used to pass query
124
+ # arguments when the `query` string contains either positional (`?`)
125
+ # or named (`@myparam`) query parameters. If value passed is an array
126
+ # `["foo"]`, the query must use positional query parameters. If value
127
+ # passed is a hash `{ myparam: "foo" }`, the query must use named
128
+ # query parameters. When set, `legacy_sql` will automatically be set
129
+ # to false and `standard_sql` to true.
102
130
  # @param [String] priority Specifies a priority for the query. Possible
103
131
  # values include `INTERACTIVE` and `BATCH`. The default value is
104
132
  # `INTERACTIVE`.
@@ -135,6 +163,25 @@ module Google
135
163
  # parameter must be `true` if this is set to `false`.
136
164
  # @param [Dataset, String] dataset Specifies the default dataset to use
137
165
  # for unqualified table names in the query.
166
+ # @param [Boolean] legacy_sql Specifies whether to use BigQuery's
167
+ # [legacy
168
+ # SQL](https://cloud.google.com/bigquery/docs/reference/legacy-sql)
169
+ # dialect for this query. If set to false, the query will use
170
+ # BigQuery's [standard
171
+ # SQL](https://cloud.google.com/bigquery/docs/reference/standard-sql/)
172
+ # When set to false, the values of `large_results` and `flatten` are
173
+ # ignored; the query will be run as if `large_results` is true and
174
+ # `flatten` is false. Optional. The default value is true.
175
+ # @param [Boolean] standard_sql Specifies whether to use BigQuery's
176
+ # [standard
177
+ # SQL](https://cloud.google.com/bigquery/docs/reference/standard-sql/)
178
+ # dialect for this query. If set to true, the query will use standard
179
+ # SQL rather than the [legacy
180
+ # SQL](https://cloud.google.com/bigquery/docs/reference/legacy-sql)
181
+ # dialect. When set to true, the values of `large_results` and
182
+ # `flatten` are ignored; the query will be run as if `large_results`
183
+ # is true and `flatten` is false. Optional. The default value is
184
+ # false.
138
185
  #
139
186
  # @return [Google::Cloud::Bigquery::QueryJob]
140
187
  #
@@ -153,14 +200,66 @@ module Google
153
200
  # end
154
201
  # end
155
202
  #
156
- def query_job query, priority: "INTERACTIVE", cache: true, table: nil,
157
- create: nil, write: nil, large_results: nil, flatten: nil,
158
- dataset: nil
203
+ # @example Query using standard SQL:
204
+ # require "google/cloud/bigquery"
205
+ #
206
+ # bigquery = Google::Cloud::Bigquery.new
207
+ #
208
+ # job = bigquery.query_job "SELECT name FROM " \
209
+ # "`my_proj.my_data.my_table`",
210
+ # standard_sql: true
211
+ #
212
+ # job.wait_until_done!
213
+ # if !job.failed?
214
+ # job.query_results.each do |row|
215
+ # puts row["name"]
216
+ # end
217
+ # end
218
+ #
219
+ # @example Query using positional query parameters:
220
+ # require "google/cloud/bigquery"
221
+ #
222
+ # bigquery = Google::Cloud::Bigquery.new
223
+ #
224
+ # job = bigquery.query_job "SELECT name FROM " \
225
+ # "`my_proj.my_data.my_table`" \
226
+ # " WHERE id = ?",
227
+ # params: [1]
228
+ #
229
+ # job.wait_until_done!
230
+ # if !job.failed?
231
+ # job.query_results.each do |row|
232
+ # puts row["name"]
233
+ # end
234
+ # end
235
+ #
236
+ # @example Query using named query parameters:
237
+ # require "google/cloud/bigquery"
238
+ #
239
+ # bigquery = Google::Cloud::Bigquery.new
240
+ #
241
+ # job = bigquery.query_job "SELECT name FROM " \
242
+ # "`my_proj.my_data.my_table`" \
243
+ # " WHERE id = @id",
244
+ # params: { id: 1 }
245
+ #
246
+ # job.wait_until_done!
247
+ # if !job.failed?
248
+ # job.query_results.each do |row|
249
+ # puts row["name"]
250
+ # end
251
+ # end
252
+ #
253
+ def query_job query, params: nil, priority: "INTERACTIVE", cache: true,
254
+ table: nil, create: nil, write: nil, large_results: nil,
255
+ flatten: nil, dataset: nil, legacy_sql: nil,
256
+ standard_sql: nil
159
257
  ensure_service!
160
258
  options = { priority: priority, cache: cache, table: table,
161
259
  create: create, write: write,
162
260
  large_results: large_results, flatten: flatten,
163
- dataset: dataset }
261
+ dataset: dataset, legacy_sql: legacy_sql,
262
+ standard_sql: standard_sql, params: params }
164
263
  gapi = service.query_job query, options
165
264
  Job.from_gapi gapi, service
166
265
  end
@@ -169,10 +268,37 @@ module Google
169
268
  # Queries data using the [synchronous
170
269
  # method](https://cloud.google.com/bigquery/querying-data).
171
270
  #
271
+ # When using standard SQL and passing arguments using `params`, Ruby
272
+ # types are mapped to BigQuery types as follows:
273
+ #
274
+ # | BigQuery | Ruby | Notes |
275
+ # |-------------|----------------|---|
276
+ # | `BOOL` | `true`/`false` | |
277
+ # | `INT64` | `Integer` | |
278
+ # | `FLOAT64` | `Float` | |
279
+ # | `STRING` | `STRING` | |
280
+ # | `DATETIME` | `DateTime` | `DATETIME` does not support time zone. |
281
+ # | `DATE` | `Date` | |
282
+ # | `TIMESTAMP` | `Time` | |
283
+ # | `TIME` | `Google::Cloud::BigQuery::Time` | |
284
+ # | `BYTES` | `File`, `IO`, `StringIO`, or similar | |
285
+ # | `ARRAY` | `Array` | Nested arrays, `nil` values are not supported. |
286
+ # | `STRUCT` | `Hash` | Hash keys may be strings or symbols. |
287
+ #
288
+ # See [Data Types](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types)
289
+ # for an overview of each BigQuery data type, including allowed values.
290
+ #
172
291
  # @param [String] query A query string, following the BigQuery [query
173
292
  # syntax](https://cloud.google.com/bigquery/query-reference), of the
174
293
  # query to execute. Example: "SELECT count(f1) FROM
175
294
  # [myProjectId:myDatasetId.myTableId]".
295
+ # @param [Array, Hash] params Standard SQL only. Used to pass query
296
+ # arguments when the `query` string contains either positional (`?`)
297
+ # or named (`@myparam`) query parameters. If value passed is an array
298
+ # `["foo"]`, the query must use positional query parameters. If value
299
+ # passed is a hash `{ myparam: "foo" }`, the query must use named
300
+ # query parameters. When set, `legacy_sql` will automatically be set
301
+ # to false and `standard_sql` to true.
176
302
  # @param [Integer] max The maximum number of rows of data to return per
177
303
  # page of results. Setting this flag to a small value such as 1000 and
178
304
  # then paging through results might improve reliability when the query
@@ -201,6 +327,25 @@ module Google
201
327
  # @param [String] project Specifies the default projectId to assume for
202
328
  # any unqualified table names in the query. Only used if `dataset`
203
329
  # option is set.
330
+ # @param [Boolean] legacy_sql Specifies whether to use BigQuery's
331
+ # [legacy
332
+ # SQL](https://cloud.google.com/bigquery/docs/reference/legacy-sql)
333
+ # dialect for this query. If set to false, the query will use
334
+ # BigQuery's [standard
335
+ # SQL](https://cloud.google.com/bigquery/docs/reference/standard-sql/)
336
+ # When set to false, the values of `large_results` and `flatten` are
337
+ # ignored; the query will be run as if `large_results` is true and
338
+ # `flatten` is false. Optional. The default value is true.
339
+ # @param [Boolean] standard_sql Specifies whether to use BigQuery's
340
+ # [standard
341
+ # SQL](https://cloud.google.com/bigquery/docs/reference/standard-sql/)
342
+ # dialect for this query. If set to true, the query will use standard
343
+ # SQL rather than the [legacy
344
+ # SQL](https://cloud.google.com/bigquery/docs/reference/legacy-sql)
345
+ # dialect. When set to true, the values of `large_results` and
346
+ # `flatten` are ignored; the query will be run as if `large_results`
347
+ # is true and `flatten` is false. Optional. The default value is
348
+ # false.
204
349
  #
205
350
  # @return [Google::Cloud::Bigquery::QueryData]
206
351
  #
@@ -210,6 +355,19 @@ module Google
210
355
  # bigquery = Google::Cloud::Bigquery.new
211
356
  #
212
357
  # data = bigquery.query "SELECT name FROM [my_proj:my_data.my_table]"
358
+ #
359
+ # data.each do |row|
360
+ # puts row["name"]
361
+ # end
362
+ #
363
+ # @example Query using standard SQL:
364
+ # require "google/cloud/bigquery"
365
+ #
366
+ # bigquery = Google::Cloud::Bigquery.new
367
+ #
368
+ # data = bigquery.query "SELECT name FROM `my_proj.my_data.my_table`",
369
+ # standard_sql: true
370
+ #
213
371
  # data.each do |row|
214
372
  # puts row["name"]
215
373
  # end
@@ -220,15 +378,47 @@ module Google
220
378
  # bigquery = Google::Cloud::Bigquery.new
221
379
  #
222
380
  # data = bigquery.query "SELECT name FROM [my_proj:my_data.my_table]"
381
+ #
223
382
  # data.all do |row|
224
383
  # puts row["name"]
225
384
  # end
226
385
  #
227
- def query query, max: nil, timeout: 10000, dryrun: nil, cache: true,
228
- dataset: nil, project: nil
386
+ # @example Query using positional query parameters:
387
+ # require "google/cloud/bigquery"
388
+ #
389
+ # bigquery = Google::Cloud::Bigquery.new
390
+ #
391
+ # data = bigquery.query "SELECT name " \
392
+ # "FROM [my_proj:my_data.my_table]" \
393
+ # "WHERE id = ?",
394
+ # params: [1]
395
+ #
396
+ # data.each do |row|
397
+ # puts row["name"]
398
+ # end
399
+ #
400
+ # @example Query using named query parameters:
401
+ # require "google/cloud/bigquery"
402
+ #
403
+ # bigquery = Google::Cloud::Bigquery.new
404
+ #
405
+ # data = bigquery.query "SELECT name " \
406
+ # "FROM [my_proj:my_data.my_table]" \
407
+ # "WHERE id = @id",
408
+ # params: { id: 1 }
409
+ #
410
+ # data.each do |row|
411
+ # puts row["name"]
412
+ # end
413
+ #
414
+ def query query, params: nil, max: nil, timeout: 10000, dryrun: nil,
415
+ cache: true, dataset: nil, project: nil, legacy_sql: nil,
416
+ standard_sql: nil
229
417
  ensure_service!
230
418
  options = { max: max, timeout: timeout, dryrun: dryrun, cache: cache,
231
- dataset: dataset, project: project }
419
+ dataset: dataset, project: project,
420
+ legacy_sql: legacy_sql, standard_sql: standard_sql,
421
+ params: params }
232
422
  gapi = service.query query, options
233
423
  QueryData.from_gapi gapi, service
234
424
  end
@@ -273,7 +463,8 @@ module Google
273
463
  # should reside. Possible values include `EU` and `US`. The default
274
464
  # value is `US`.
275
465
  # @yield [access] a block for setting rules
276
- # @yieldparam [Dataset::Access] access the object accepting rules
466
+ # @yieldparam [Google::Cloud::Bigquery::Dataset] access the object
467
+ # accepting rules
277
468
  #
278
469
  # @return [Google::Cloud::Bigquery::Dataset]
279
470
  #
@@ -293,21 +484,13 @@ module Google
293
484
  # name: "My Dataset",
294
485
  # description: "This is my Dataset"
295
486
  #
296
- # @example Access rules can be provided with the `access` option:
297
- # require "google/cloud/bigquery"
298
- #
299
- # bigquery = Google::Cloud::Bigquery.new
300
- #
301
- # dataset = bigquery.create_dataset "my_dataset",
302
- # access: [{"role"=>"WRITER", "userByEmail"=>"writers@example.com"}]
303
- #
304
487
  # @example Or, configure access with a block: (See {Dataset::Access})
305
488
  # require "google/cloud/bigquery"
306
489
  #
307
490
  # bigquery = Google::Cloud::Bigquery.new
308
491
  #
309
- # dataset = bigquery.create_dataset "my_dataset" do |access|
310
- # access.add_writer_user "writers@example.com"
492
+ # dataset = bigquery.create_dataset "my_dataset" do |table|
493
+ # table.access.add_writer_user "writers@example.com"
311
494
  # end
312
495
  #
313
496
  def create_dataset dataset_id, name: nil, description: nil,
@@ -509,6 +692,51 @@ module Google
509
692
  Project::List.from_gapi gapi, service, max
510
693
  end
511
694
 
695
+ ##
696
+ # Creates a Bigquery::Time object to represent a time, independent of a
697
+ # specific date.
698
+ #
699
+ # @param [Integer] hour Hour, valid values from 0 to 23.
700
+ # @param [Integer] minute Minute, valid values from 0 to 59.
701
+ # @param [Integer, Float] second Second, valid values from 0 to 59. Can
702
+ # contain microsecond precision.
703
+ #
704
+ # @return [Bigquery::Time]
705
+ #
706
+ # @example
707
+ # require "google/cloud/bigquery"
708
+ #
709
+ # bigquery = Google::Cloud::Bigquery.new
710
+ #
711
+ # fourpm = bigquery.time 16, 0, 0
712
+ # data = bigquery.query "SELECT name " \
713
+ # "FROM [my_proj:my_data.my_table]" \
714
+ # "WHERE time_of_date = @time",
715
+ # params: { time: fourpm }
716
+ #
717
+ # data.each do |row|
718
+ # puts row["name"]
719
+ # end
720
+ #
721
+ # @example Create Time with fractional seconds:
722
+ # require "google/cloud/bigquery"
723
+ #
724
+ # bigquery = Google::Cloud::Bigquery.new
725
+ #
726
+ # precise_time = bigquery.time 16, 35, 15.376541
727
+ # data = bigquery.query "SELECT name " \
728
+ # "FROM [my_proj:my_data.my_table]" \
729
+ # "WHERE time_of_date >= @time",
730
+ # params: { time: precise_time }
731
+ #
732
+ # data.each do |row|
733
+ # puts row["name"]
734
+ # end
735
+ #
736
+ def time hour, minute, second
737
+ Bigquery::Time.new "#{hour}:#{minute}:#{second}"
738
+ end
739
+
512
740
  ##
513
741
  # @private New Project from a Google API Client object, using the
514
742
  # same Credentials as this project.