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.
@@ -98,6 +98,20 @@ module Google
98
98
  table.table_id
99
99
  end
100
100
 
101
+ ##
102
+ # Checks if the query job is using legacy sql.
103
+ def legacy_sql?
104
+ val = @gapi.configuration.query.use_legacy_sql
105
+ return true if val.nil?
106
+ val
107
+ end
108
+
109
+ ##
110
+ # Checks if the query job is using standard sql.
111
+ def standard_sql?
112
+ !legacy_sql?
113
+ end
114
+
101
115
  ##
102
116
  # Retrieves the query results for the job.
103
117
  #
@@ -19,6 +19,7 @@ require "google/apis/bigquery_v2"
19
19
  require "pathname"
20
20
  require "digest/md5"
21
21
  require "mime/types"
22
+ require "date"
22
23
 
23
24
  module Google
24
25
  module Cloud
@@ -380,12 +381,14 @@ module Google
380
381
  )
381
382
  end
382
383
 
384
+ # rubocop:disable all
385
+
383
386
  ##
384
387
  # Job description for query job
385
388
  def query_table_config query, options
386
389
  dest_table = table_ref_from options[:table]
387
390
  default_dataset = dataset_ref_from options[:dataset]
388
- API::Job.new(
391
+ req = API::Job.new(
389
392
  configuration: API::JobConfiguration.new(
390
393
  query: API::JobConfigurationQuery.new(
391
394
  query: query,
@@ -397,23 +400,168 @@ module Google
397
400
  write_disposition: write_disposition(options[:write]),
398
401
  allow_large_results: options[:large_results],
399
402
  flatten_results: options[:flatten],
400
- default_dataset: default_dataset
403
+ default_dataset: default_dataset,
404
+ use_legacy_sql: resolve_legacy_sql(options[:legacy_sql],
405
+ options[:standard_sql])
401
406
  )
402
407
  )
403
408
  )
409
+
410
+ if options[:params]
411
+ if Array === options[:params]
412
+ req.configuration.query.use_legacy_sql = false
413
+ req.configuration.query.parameter_mode = "POSITIONAL"
414
+ req.configuration.query.query_parameters = options[:params].map do |param|
415
+ to_query_param param
416
+ end
417
+ elsif Hash === options[:params]
418
+ req.configuration.query.use_legacy_sql = false
419
+ req.configuration.query.parameter_mode = "NAMED"
420
+ req.configuration.query.query_parameters = options[:params].map do |name, param|
421
+ to_query_param(param).tap do |named_param|
422
+ named_param.name = String name
423
+ end
424
+ end
425
+ else
426
+ fail "Query parameters must be an Array or a Hash."
427
+ end
428
+ end
429
+
430
+ req
404
431
  end
405
432
 
406
433
  def query_config query, options = {}
407
434
  dataset_config = dataset_ref_from options[:dataset], options[:project]
408
435
 
409
- API::QueryRequest.new(
436
+ req = API::QueryRequest.new(
410
437
  query: query,
411
438
  max_results: options[:max],
412
439
  default_dataset: dataset_config,
413
440
  timeout_ms: options[:timeout],
414
441
  dry_run: options[:dryrun],
415
- use_query_cache: options[:cache]
442
+ use_query_cache: options[:cache],
443
+ use_legacy_sql: resolve_legacy_sql(options[:legacy_sql],
444
+ options[:standard_sql])
416
445
  )
446
+
447
+ if options[:params]
448
+ if Array === options[:params]
449
+ req.use_legacy_sql = false
450
+ req.parameter_mode = "POSITIONAL"
451
+ req.query_parameters = options[:params].map do |param|
452
+ to_query_param param
453
+ end
454
+ elsif Hash === options[:params]
455
+ req.use_legacy_sql = false
456
+ req.parameter_mode = "NAMED"
457
+ req.query_parameters = options[:params].map do |name, param|
458
+ to_query_param(param).tap do |named_param|
459
+ named_param.name = String name
460
+ end
461
+ end
462
+ else
463
+ fail "Query parameters must be an Array or a Hash."
464
+ end
465
+ end
466
+
467
+ req
468
+ end
469
+
470
+ def to_query_param value
471
+ if TrueClass === value
472
+ return API::QueryParameter.new(
473
+ parameter_type: API::QueryParameterType.new(type: "BOOL"),
474
+ parameter_value: API::QueryParameterValue.new(value: true)
475
+ )
476
+ elsif FalseClass === value
477
+ return API::QueryParameter.new(
478
+ parameter_type: API::QueryParameterType.new(type: "BOOL"),
479
+ parameter_value: API::QueryParameterValue.new(value: false)
480
+ )
481
+ elsif Integer === value
482
+ return API::QueryParameter.new(
483
+ parameter_type: API::QueryParameterType.new(type: "INT64"),
484
+ parameter_value: API::QueryParameterValue.new(value: value)
485
+ )
486
+ elsif Float === value
487
+ return API::QueryParameter.new(
488
+ parameter_type: API::QueryParameterType.new(type: "FLOAT64"),
489
+ parameter_value: API::QueryParameterValue.new(value: value)
490
+ )
491
+ elsif String === value
492
+ return API::QueryParameter.new(
493
+ parameter_type: API::QueryParameterType.new(type: "STRING"),
494
+ parameter_value: API::QueryParameterValue.new(value: value)
495
+ )
496
+ elsif DateTime === value
497
+ return API::QueryParameter.new(
498
+ parameter_type: API::QueryParameterType.new(type: "DATETIME"),
499
+ parameter_value: API::QueryParameterValue.new(
500
+ value: value.strftime("%Y-%m-%d %H:%M:%S.%6N"))
501
+ )
502
+ elsif Date === value
503
+ return API::QueryParameter.new(
504
+ parameter_type: API::QueryParameterType.new(type: "DATE"),
505
+ parameter_value: API::QueryParameterValue.new(value: value.to_s)
506
+ )
507
+ elsif ::Time === value
508
+ return API::QueryParameter.new(
509
+ parameter_type: API::QueryParameterType.new(type: "TIMESTAMP"),
510
+ parameter_value: API::QueryParameterValue.new(
511
+ value: value.strftime("%Y-%m-%d %H:%M:%S.%6N%:z"))
512
+ )
513
+ elsif Bigquery::Time === value
514
+ return API::QueryParameter.new(
515
+ parameter_type: API::QueryParameterType.new(type: "TIME"),
516
+ parameter_value: API::QueryParameterValue.new(value: value.value)
517
+ )
518
+ elsif value.respond_to?(:read) && value.respond_to?(:rewind)
519
+ value.rewind
520
+ return API::QueryParameter.new(
521
+ parameter_type: API::QueryParameterType.new(type: "BYTES"),
522
+ parameter_value: API::QueryParameterValue.new(
523
+ value: value.read.force_encoding("ASCII-8BIT"))
524
+ )
525
+ elsif Array === value
526
+ array_params = value.map { |param| to_query_param param }
527
+ return API::QueryParameter.new(
528
+ parameter_type: API::QueryParameterType.new(
529
+ type: "ARRAY",
530
+ array_type: array_params.first.parameter_type
531
+ ),
532
+ parameter_value: API::QueryParameterValue.new(
533
+ array_values: array_params.map(&:parameter_value)
534
+ )
535
+ )
536
+ elsif Hash === value
537
+ struct_pairs = value.map do |name, param|
538
+ struct_param = to_query_param param
539
+ [API::QueryParameterType::StructType.new(
540
+ name: String(name),
541
+ type: struct_param.parameter_type
542
+ ), struct_param.parameter_value]
543
+ end
544
+
545
+ return API::QueryParameter.new(
546
+ parameter_type: API::QueryParameterType.new(
547
+ type: "STRUCT",
548
+ struct_types: struct_pairs.map(&:first)
549
+ ),
550
+ parameter_value: API::QueryParameterValue.new(
551
+ struct_values: struct_pairs.map(&:last)
552
+ )
553
+ )
554
+ else
555
+ fail "A query parameter of type #{value.class} is not supported."
556
+ end
557
+ v
558
+ end
559
+
560
+ # rubocop:enable all
561
+
562
+ def resolve_legacy_sql legacy_sql, standard_sql
563
+ return legacy_sql unless legacy_sql.nil?
564
+ return !standard_sql unless standard_sql.nil?
417
565
  end
418
566
 
419
567
  ##
@@ -247,7 +247,7 @@ module Google
247
247
  def created_at
248
248
  ensure_full_data!
249
249
  begin
250
- Time.at(Integer(@gapi.creation_time) / 1000.0)
250
+ ::Time.at(Integer(@gapi.creation_time) / 1000.0)
251
251
  rescue
252
252
  nil
253
253
  end
@@ -263,7 +263,7 @@ module Google
263
263
  def expires_at
264
264
  ensure_full_data!
265
265
  begin
266
- Time.at(Integer(@gapi.expiration_time) / 1000.0)
266
+ ::Time.at(Integer(@gapi.expiration_time) / 1000.0)
267
267
  rescue
268
268
  nil
269
269
  end
@@ -277,7 +277,7 @@ module Google
277
277
  def modified_at
278
278
  ensure_full_data!
279
279
  begin
280
- Time.at(Integer(@gapi.last_modified_time) / 1000.0)
280
+ ::Time.at(Integer(@gapi.last_modified_time) / 1000.0)
281
281
  rescue
282
282
  nil
283
283
  end
@@ -0,0 +1,59 @@
1
+ # Copyright 2016 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
+
15
+
16
+ module Google
17
+ module Cloud
18
+ module Bigquery
19
+ ##
20
+ # # Time
21
+ #
22
+ # A TIME data type represents a time, independent of a specific date.
23
+ #
24
+ # @attr_writer [String] value The BigQuery TIME.
25
+ #
26
+ # @example
27
+ # require "google/cloud/bigquery"
28
+ #
29
+ # bigquery = Google::Cloud::Bigquery.new
30
+ #
31
+ # fourpm = Google::Cloud::Bigquery::Time.new "16:00:00"
32
+ # data = bigquery.query "SELECT name " \
33
+ # "FROM [my_proj:my_data.my_table]" \
34
+ # "WHERE time_of_date = @time",
35
+ # params: { time: fourpm }
36
+ #
37
+ # data.each do |row|
38
+ # puts row["name"]
39
+ # end
40
+ #
41
+ # @example Create Time with fractional seconds:
42
+ # require "google/cloud/bigquery"
43
+ #
44
+ # bigquery = Google::Cloud::Bigquery.new
45
+ #
46
+ # precise_time = Google::Cloud::Bigquery::Time.new "16:35:15.376541"
47
+ # data = bigquery.query "SELECT name " \
48
+ # "FROM [my_proj:my_data.my_table]" \
49
+ # "WHERE time_of_date >= @time",
50
+ # params: { time: precise_time }
51
+ #
52
+ # data.each do |row|
53
+ # puts row["name"]
54
+ # end
55
+ #
56
+ Time = Struct.new :value
57
+ end
58
+ end
59
+ end
@@ -16,7 +16,7 @@
16
16
  module Google
17
17
  module Cloud
18
18
  module Bigquery
19
- VERSION = "0.21.0"
19
+ VERSION = "0.23.0"
20
20
  end
21
21
  end
22
22
  end
@@ -120,9 +120,9 @@ module Google
120
120
  #
121
121
  # bigquery = Google::Cloud::Bigquery.new
122
122
  # dataset = bigquery.dataset "my_dataset"
123
- # table = dataset.table "my_table"
123
+ # view = dataset.table "my_view"
124
124
  #
125
- # data = bigquery.query "SELECT name FROM #{table.query_id}"
125
+ # data = bigquery.query "SELECT name FROM #{view.query_id}"
126
126
  #
127
127
  # @!group Attributes
128
128
  #
@@ -197,7 +197,7 @@ module Google
197
197
  def created_at
198
198
  ensure_full_data!
199
199
  begin
200
- Time.at(Integer(@gapi.creation_time) / 1000.0)
200
+ ::Time.at(Integer(@gapi.creation_time) / 1000.0)
201
201
  rescue
202
202
  nil
203
203
  end
@@ -213,7 +213,7 @@ module Google
213
213
  def expires_at
214
214
  ensure_full_data!
215
215
  begin
216
- Time.at(Integer(@gapi.expiration_time) / 1000.0)
216
+ ::Time.at(Integer(@gapi.expiration_time) / 1000.0)
217
217
  rescue
218
218
  nil
219
219
  end
@@ -227,7 +227,7 @@ module Google
227
227
  def modified_at
228
228
  ensure_full_data!
229
229
  begin
230
- Time.at(Integer(@gapi.last_modified_time) / 1000.0)
230
+ ::Time.at(Integer(@gapi.last_modified_time) / 1000.0)
231
231
  rescue
232
232
  nil
233
233
  end
@@ -385,9 +385,9 @@ module Google
385
385
  #
386
386
  # bigquery = Google::Cloud::Bigquery.new
387
387
  # dataset = bigquery.dataset "my_dataset"
388
- # table = dataset.table "my_table"
388
+ # view = dataset.table "my_view"
389
389
  #
390
- # table.delete
390
+ # view.delete
391
391
  #
392
392
  # @!group Lifecycle
393
393
  #
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-cloud-bigquery
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.21.0
4
+ version: 0.23.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Moore
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-10-20 00:00:00.000000000 Z
12
+ date: 2016-12-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: google-cloud-core
@@ -31,14 +31,14 @@ dependencies:
31
31
  requirements:
32
32
  - - "~>"
33
33
  - !ruby/object:Gem::Version
34
- version: 0.9.11
34
+ version: 0.9.18
35
35
  type: :runtime
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
39
  - - "~>"
40
40
  - !ruby/object:Gem::Version
41
- version: 0.9.11
41
+ version: 0.9.18
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: minitest
44
44
  requirement: !ruby/object:Gem::Requirement
@@ -157,14 +157,14 @@ dependencies:
157
157
  requirements:
158
158
  - - "~>"
159
159
  - !ruby/object:Gem::Version
160
- version: 0.1.6
160
+ version: 0.1.8
161
161
  type: :development
162
162
  prerelease: false
163
163
  version_requirements: !ruby/object:Gem::Requirement
164
164
  requirements:
165
165
  - - "~>"
166
166
  - !ruby/object:Gem::Version
167
- version: 0.1.6
167
+ version: 0.1.8
168
168
  description: google-cloud-bigquery is the official library for Google BigQuery.
169
169
  email:
170
170
  - mike@blowmage.com
@@ -173,6 +173,9 @@ executables: []
173
173
  extensions: []
174
174
  extra_rdoc_files: []
175
175
  files:
176
+ - ".yardopts"
177
+ - LICENSE
178
+ - README.md
176
179
  - lib/google-cloud-bigquery.rb
177
180
  - lib/google/cloud/bigquery.rb
178
181
  - lib/google/cloud/bigquery/copy_job.rb
@@ -194,6 +197,7 @@ files:
194
197
  - lib/google/cloud/bigquery/service.rb
195
198
  - lib/google/cloud/bigquery/table.rb
196
199
  - lib/google/cloud/bigquery/table/list.rb
200
+ - lib/google/cloud/bigquery/time.rb
197
201
  - lib/google/cloud/bigquery/version.rb
198
202
  - lib/google/cloud/bigquery/view.rb
199
203
  homepage: http://googlecloudplatform.github.io/google-cloud-ruby/
@@ -216,7 +220,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
216
220
  version: '0'
217
221
  requirements: []
218
222
  rubyforge_project:
219
- rubygems_version: 2.6.4
223
+ rubygems_version: 2.4.5.1
220
224
  signing_key:
221
225
  specification_version: 4
222
226
  summary: API Client library for Google BigQuery