google-cloud-bigquery 1.17.0 → 1.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -611,7 +611,7 @@ module Google
611
611
  )
612
612
 
613
613
  updater = QueryJob::Updater.new service, req
614
- updater.params = options[:params] if options[:params]
614
+ updater.set_params_and_types options[:params], options[:types] if options[:params]
615
615
  updater.create = options[:create]
616
616
  updater.write = options[:write]
617
617
  updater.table = options[:table]
@@ -723,30 +723,113 @@ module Google
723
723
  ##
724
724
  # Sets the query parameters. Standard SQL only.
725
725
  #
726
- # @param [Array, Hash] params Used to pass query arguments when the
727
- # `query` string contains either positional (`?`) or named
728
- # (`@myparam`) query parameters. If value passed is an array
729
- # `["foo"]`, the query must use positional query parameters. If
730
- # value passed is a hash `{ myparam: "foo" }`, the query must use
731
- # named query parameters. When set, `legacy_sql` will automatically
732
- # be set to false and `standard_sql` to true.
726
+ # Use {set_params_and_types} to set both params and types.
727
+ #
728
+ # @param [Array, Hash] params Standard SQL only. Used to pass query arguments when the `query` string contains
729
+ # either positional (`?`) or named (`@myparam`) query parameters. If value passed is an array `["foo"]`, the
730
+ # query must use positional query parameters. If value passed is a hash `{ myparam: "foo" }`, the query must
731
+ # use named query parameters. When set, `legacy_sql` will automatically be set to false and `standard_sql`
732
+ # to true.
733
+ #
734
+ # Ruby types are mapped to BigQuery types as follows:
735
+ #
736
+ # | BigQuery | Ruby | Notes |
737
+ # |-------------|--------------------------------------|------------------------------------------------|
738
+ # | `BOOL` | `true`/`false` | |
739
+ # | `INT64` | `Integer` | |
740
+ # | `FLOAT64` | `Float` | |
741
+ # | `NUMERIC` | `BigDecimal` | Will be rounded to 9 decimal places |
742
+ # | `STRING` | `String` | |
743
+ # | `DATETIME` | `DateTime` | `DATETIME` does not support time zone. |
744
+ # | `DATE` | `Date` | |
745
+ # | `TIMESTAMP` | `Time` | |
746
+ # | `TIME` | `Google::Cloud::BigQuery::Time` | |
747
+ # | `BYTES` | `File`, `IO`, `StringIO`, or similar | |
748
+ # | `ARRAY` | `Array` | Nested arrays, `nil` values are not supported. |
749
+ # | `STRUCT` | `Hash` | Hash keys may be strings or symbols. |
750
+ #
751
+ # See [Data Types](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types) for an overview
752
+ # of each BigQuery data type, including allowed values.
733
753
  #
734
754
  # @!group Attributes
735
755
  def params= params
756
+ set_params_and_types params
757
+ end
758
+
759
+ ##
760
+ # Sets the query parameters. Standard SQL only.
761
+ #
762
+ # @param [Array, Hash] params Standard SQL only. Used to pass query arguments when the `query` string contains
763
+ # either positional (`?`) or named (`@myparam`) query parameters. If value passed is an array `["foo"]`, the
764
+ # query must use positional query parameters. If value passed is a hash `{ myparam: "foo" }`, the query must
765
+ # use named query parameters. When set, `legacy_sql` will automatically be set to false and `standard_sql`
766
+ # to true.
767
+ #
768
+ # Ruby types are mapped to BigQuery types as follows:
769
+ #
770
+ # | BigQuery | Ruby | Notes |
771
+ # |-------------|--------------------------------------|------------------------------------------------|
772
+ # | `BOOL` | `true`/`false` | |
773
+ # | `INT64` | `Integer` | |
774
+ # | `FLOAT64` | `Float` | |
775
+ # | `NUMERIC` | `BigDecimal` | Will be rounded to 9 decimal places |
776
+ # | `STRING` | `String` | |
777
+ # | `DATETIME` | `DateTime` | `DATETIME` does not support time zone. |
778
+ # | `DATE` | `Date` | |
779
+ # | `TIMESTAMP` | `Time` | |
780
+ # | `TIME` | `Google::Cloud::BigQuery::Time` | |
781
+ # | `BYTES` | `File`, `IO`, `StringIO`, or similar | |
782
+ # | `ARRAY` | `Array` | Nested arrays, `nil` values are not supported. |
783
+ # | `STRUCT` | `Hash` | Hash keys may be strings or symbols. |
784
+ #
785
+ # See [Data Types](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types) for an overview
786
+ # of each BigQuery data type, including allowed values.
787
+ # @param [Array, Hash] types Standard SQL only. Types of the SQL parameters in `params`. It is not always to
788
+ # infer the right SQL type from a value in `params`. In these cases, `types` must be used to specify the SQL
789
+ # type for these values.
790
+ #
791
+ # Must match the value type passed to `params`. This must be an `Array` when the query uses positional query
792
+ # parameters. This must be an `Hash` when the query uses named query parameters. The values should be
793
+ # BigQuery type codes from the following list:
794
+ #
795
+ # * `:BOOL`
796
+ # * `:INT64`
797
+ # * `:FLOAT64`
798
+ # * `:NUMERIC`
799
+ # * `:STRING`
800
+ # * `:DATETIME`
801
+ # * `:DATE`
802
+ # * `:TIMESTAMP`
803
+ # * `:TIME`
804
+ # * `:BYTES`
805
+ # * `Array` - Lists are specified by providing the type code in an array. For example, an array of integers
806
+ # are specified as `[:INT64]`.
807
+ # * `Hash` - Types for STRUCT values (`Hash` objects) are specified using a `Hash` object, where the keys
808
+ # match the `params` hash, and the values are the types value that matches the data.
809
+ #
810
+ # Types are optional.
811
+ #
812
+ # @!group Attributes
813
+ def set_params_and_types params, types = nil
814
+ types ||= params.class.new
815
+ raise ArgumentError, "types must use the same format as params" if types.class != params.class
816
+
736
817
  case params
737
818
  when Array then
738
819
  @gapi.configuration.query.use_legacy_sql = false
739
820
  @gapi.configuration.query.parameter_mode = "POSITIONAL"
740
- @gapi.configuration.query.query_parameters = params.map { |param| Convert.to_query_param param }
821
+ @gapi.configuration.query.query_parameters = params.zip(types).map do |param, type|
822
+ Convert.to_query_param param, type
823
+ end
741
824
  when Hash then
742
825
  @gapi.configuration.query.use_legacy_sql = false
743
826
  @gapi.configuration.query.parameter_mode = "NAMED"
744
- @gapi.configuration.query.query_parameters =
745
- params.map do |name, param|
746
- Convert.to_query_param(param).tap { |named_param| named_param.name = String name }
747
- end
827
+ @gapi.configuration.query.query_parameters = params.map do |name, param|
828
+ type = types[name]
829
+ Convert.to_query_param(param, type).tap { |named_param| named_param.name = String name }
830
+ end
748
831
  else
749
- raise "Query parameters must be an Array or a Hash."
832
+ raise ArgumentError, "params must be an Array or a Hash"
750
833
  end
751
834
  end
752
835
 
@@ -154,6 +154,27 @@ module Google
154
154
  fields.map(&:name).map(&:to_sym)
155
155
  end
156
156
 
157
+ ##
158
+ # The types of the fields, using the same format as the optional query
159
+ # parameter types.
160
+ #
161
+ # @return [Hash] A hash with column names as keys, and types as values.
162
+ #
163
+ # @example
164
+ # require "google/cloud/bigquery"
165
+ #
166
+ # bigquery = Google::Cloud::Bigquery.new
167
+ # dataset = bigquery.dataset "my_dataset"
168
+ # table = dataset.create_table "my_table"
169
+ #
170
+ # schema = table.schema
171
+ #
172
+ # schema.param_types
173
+ #
174
+ def param_types
175
+ Hash[fields.map { |field| [field.name.to_sym, field.param_type] }]
176
+ end
177
+
157
178
  ##
158
179
  # Retrieve a field by name.
159
180
  #
@@ -261,6 +261,7 @@ module Google
261
261
  def record?
262
262
  type == "RECORD" || type == "STRUCT"
263
263
  end
264
+ alias struct? record?
264
265
 
265
266
  ##
266
267
  # The nested fields if the type property is set to `RECORD`. Will be
@@ -288,6 +289,36 @@ module Google
288
289
  fields.map(&:name).map(&:to_sym)
289
290
  end
290
291
 
292
+ ##
293
+ # The types of the field, using the same format as the optional query
294
+ # parameter types.
295
+ #
296
+ # The parameter types are one of the following BigQuery type codes:
297
+ #
298
+ # * `:BOOL`
299
+ # * `:INT64`
300
+ # * `:FLOAT64`
301
+ # * `:NUMERIC`
302
+ # * `:STRING`
303
+ # * `:DATETIME`
304
+ # * `:DATE`
305
+ # * `:TIMESTAMP`
306
+ # * `:TIME`
307
+ # * `:BYTES`
308
+ # * `Array` - Lists are specified by providing the type code in an array. For example, an array of integers
309
+ # are specified as `[:INT64]`.
310
+ # * `Hash` - Types for STRUCT values (`Hash` objects) are specified using a `Hash` object, where the keys
311
+ # are the nested field names, and the values are the the nested field types.
312
+ #
313
+ # @return [Symbol, Array, Hash] The type.
314
+ #
315
+ def param_type
316
+ param_type = type.to_sym
317
+ param_type = Hash[fields.map { |field| [field.name.to_sym, field.param_type] }] if record?
318
+ param_type = [param_type] if repeated?
319
+ param_type
320
+ end
321
+
291
322
  ##
292
323
  # Retrieve a nested field by name, if the type property is
293
324
  # set to `RECORD`. Will return `nil` otherwise.
@@ -870,6 +870,26 @@ module Google
870
870
  schema.headers
871
871
  end
872
872
 
873
+ ##
874
+ # The types of the fields in the table, obtained from its schema.
875
+ # Types use the same format as the optional query parameter types.
876
+ #
877
+ # @return [Hash] A hash with field names as keys, and types as values.
878
+ #
879
+ # @example
880
+ # require "google/cloud/bigquery"
881
+ #
882
+ # bigquery = Google::Cloud::Bigquery.new
883
+ # dataset = bigquery.dataset "my_dataset"
884
+ # table = dataset.table "my_table"
885
+ #
886
+ # table.param_types
887
+ #
888
+ def param_types
889
+ return nil if reference?
890
+ schema.param_types
891
+ end
892
+
873
893
  ##
874
894
  # The {EncryptionConfiguration} object that represents the custom
875
895
  # encryption method used to protect the table. If not set,
@@ -16,7 +16,7 @@
16
16
  module Google
17
17
  module Cloud
18
18
  module Bigquery
19
- VERSION = "1.17.0".freeze
19
+ VERSION = "1.18.0".freeze
20
20
  end
21
21
  end
22
22
  end
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: 1.17.0
4
+ version: 1.18.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: 2019-10-30 00:00:00.000000000 Z
12
+ date: 2019-11-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: concurrent-ruby