Class: Google::Cloud::Bigquery::Schema::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/bigquery/schema/field.rb

Overview

Schema Field

The fields of a table schema.

Examples:

require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new
dataset = bigquery.dataset "my_dataset"
table = dataset.table "my_table"

field = table.schema.field "name"
field.required? #=> true

See Also:

Instance Method Summary collapse

Instance Method Details

#bignumeric(name, description: nil, mode: :nullable, policy_tags: nil, precision: nil, scale: nil) ⇒ Object

Adds a bignumeric number field to the schema. BIGNUMERIC is a decimal type with fixed precision and scale. Precision is the number of digits that the number contains. Scale is how many of these digits appear after the decimal point. It supports:

Precision: 76.76 (the 77th digit is partial) Scale: 38 Min: -5.7896044618658097711785492504343953926634992332820282019728792003956564819968E+38 Max: 5.7896044618658097711785492504343953926634992332820282019728792003956564819967E+38

This type can represent decimal fractions exactly, and is suitable for financial calculations.

This can only be called on fields that are of type RECORD.

Parameters:

  • name (String)

    The field name. The name must contain only letters ([A-Za-z]), numbers ([0-9]), or underscores (_), and must start with a letter or underscore. The maximum length is 128 characters.

  • description (String) (defaults to: nil)

    A description of the field.

  • mode (Symbol) (defaults to: :nullable)

    The field's mode. The possible values are :nullable, :required, and :repeated. The default value is :nullable.

  • policy_tags (Array<String>, String) (defaults to: nil)

    The policy tag list or single policy tag for the field. Policy tag identifiers are of the form projects/*/locations/*/taxonomies/*/policyTags/*. At most 1 policy tag is currently allowed.

  • precision (Integer) (defaults to: nil)

    The precision (maximum number of total digits) for the field. Acceptable values for precision must be: 1 ≤ (precision - scale) ≤ 38. Values for scale must be: 0 ≤ scale ≤ 38. If the scale value is set, the precision value must be set as well.

  • scale (Integer) (defaults to: nil)

    The scale (maximum number of digits in the fractional part) for the field. Acceptable values for precision must be: 1 ≤ (precision - scale) ≤ 38. Values for scale must be: 0 ≤ scale ≤ 38. If the scale value is set, the precision value must be set as well.



747
748
749
750
751
752
753
754
755
756
757
# File 'lib/google/cloud/bigquery/schema/field.rb', line 747

def bignumeric name, description: nil, mode: :nullable, policy_tags: nil, precision: nil, scale: nil
  record_check!

  add_field name,
            :bignumeric,
            description: description,
            mode: mode,
            policy_tags: policy_tags,
            precision: precision,
            scale: scale
end

#bignumeric?Boolean

Checks if the type of the field is BIGNUMERIC.

Returns:

  • (Boolean)

    true when BIGNUMERIC, false otherwise.



420
421
422
# File 'lib/google/cloud/bigquery/schema/field.rb', line 420

def bignumeric?
  type == "BIGNUMERIC"
end

#boolean(name, description: nil, mode: :nullable, policy_tags: nil) ⇒ Object

Adds a boolean field to the nested schema of a record field.

This can only be called on fields that are of type RECORD.

Parameters:

  • name (String)

    The field name. The name must contain only letters ([A-Za-z]), numbers ([0-9]), or underscores (_), and must start with a letter or underscore. The maximum length is 128 characters.

  • description (String) (defaults to: nil)

    A description of the field.

  • mode (Symbol) (defaults to: :nullable)

    The field's mode. The possible values are :nullable, :required, and :repeated. The default value is :nullable.

  • policy_tags (Array<String>, String) (defaults to: nil)

    The policy tag list or single policy tag for the field. Policy tag identifiers are of the form projects/*/locations/*/taxonomies/*/policyTags/*. At most 1 policy tag is currently allowed.



777
778
779
780
781
# File 'lib/google/cloud/bigquery/schema/field.rb', line 777

def boolean name, description: nil, mode: :nullable, policy_tags: nil
  record_check!

  add_field name, :boolean, description: description, mode: mode, policy_tags: policy_tags
end

#boolean?Boolean

Checks if the type of the field is BOOLEAN.

Returns:

  • (Boolean)

    true when BOOLEAN, false otherwise.



429
430
431
# File 'lib/google/cloud/bigquery/schema/field.rb', line 429

def boolean?
  type == "BOOLEAN" || type == "BOOL"
end

#bytes(name, description: nil, mode: :nullable, policy_tags: nil, max_length: nil) ⇒ Object

Adds a bytes field to the nested schema of a record field.

This can only be called on fields that are of type RECORD.

Parameters:

  • name (String)

    The field name. The name must contain only letters ([A-Za-z]), numbers ([0-9]), or underscores (_), and must start with a letter or underscore. The maximum length is 128 characters.

  • description (String) (defaults to: nil)

    A description of the field.

  • mode (Symbol) (defaults to: :nullable)

    The field's mode. The possible values are :nullable, :required, and :repeated. The default value is :nullable.

  • policy_tags (Array<String>, String) (defaults to: nil)

    The policy tag list or single policy tag for the field. Policy tag identifiers are of the form projects/*/locations/*/taxonomies/*/policyTags/*. At most 1 policy tag is currently allowed.

  • max_length (Integer) (defaults to: nil)

    The maximum the maximum number of bytes in the field.



803
804
805
806
807
808
809
810
811
812
# File 'lib/google/cloud/bigquery/schema/field.rb', line 803

def bytes name, description: nil, mode: :nullable, policy_tags: nil, max_length: nil
  record_check!

  add_field name,
            :bytes,
            description: description,
            mode: mode,
            policy_tags: policy_tags,
            max_length: max_length
end

#bytes?Boolean

Checks if the type of the field is BYTES.

Returns:

  • (Boolean)

    true when BYTES, false otherwise.



438
439
440
# File 'lib/google/cloud/bigquery/schema/field.rb', line 438

def bytes?
  type == "BYTES"
end

#collationString?

The collation of the field.

Collation can be set only when the type of field is STRING. The following values are supported:

  • und:ci: undetermined locale, case insensitive.
  • (empty string): Default to case-sensitive behavior.

Returns:

  • (String, nil)

    The collation for the field, or nil.



365
366
367
# File 'lib/google/cloud/bigquery/schema/field.rb', line 365

def collation
  @gapi.collation
end

#collation=(new_collation) ⇒ Object

Updates the collation of the field.

Parameters:

  • new_collation (String)

    The new collation. See #collation for supported values.



375
376
377
# File 'lib/google/cloud/bigquery/schema/field.rb', line 375

def collation= new_collation
  @gapi.update! collation: new_collation
end

#date(name, description: nil, mode: :nullable, policy_tags: nil) ⇒ Object

Adds a date field to the nested schema of a record field.

This can only be called on fields that are of type RECORD.

Parameters:

  • name (String)

    The field name. The name must contain only letters ([A-Za-z]), numbers ([0-9]), or underscores (_), and must start with a letter or underscore. The maximum length is 128 characters.

  • description (String) (defaults to: nil)

    A description of the field.

  • mode (Symbol) (defaults to: :nullable)

    The field's mode. The possible values are :nullable, :required, and :repeated. The default value is :nullable.

  • policy_tags (Array<String>, String) (defaults to: nil)

    The policy tag list or single policy tag for the field. Policy tag identifiers are of the form projects/*/locations/*/taxonomies/*/policyTags/*. At most 1 policy tag is currently allowed.



904
905
906
907
908
# File 'lib/google/cloud/bigquery/schema/field.rb', line 904

def date name, description: nil, mode: :nullable, policy_tags: nil
  record_check!

  add_field name, :date, description: description, mode: mode, policy_tags: policy_tags
end

#date?Boolean

Checks if the type of the field is DATE.

Returns:

  • (Boolean)

    true when DATE, false otherwise.



474
475
476
# File 'lib/google/cloud/bigquery/schema/field.rb', line 474

def date?
  type == "DATE"
end

#datetime(name, description: nil, mode: :nullable, policy_tags: nil) ⇒ Object

Adds a datetime field to the nested schema of a record field.

This can only be called on fields that are of type RECORD.

Parameters:

  • name (String)

    The field name. The name must contain only letters ([A-Za-z]), numbers ([0-9]), or underscores (_), and must start with a letter or underscore. The maximum length is 128 characters.

  • description (String) (defaults to: nil)

    A description of the field.

  • mode (Symbol) (defaults to: :nullable)

    The field's mode. The possible values are :nullable, :required, and :repeated. The default value is :nullable.

  • policy_tags (Array<String>, String) (defaults to: nil)

    The policy tag list or single policy tag for the field. Policy tag identifiers are of the form projects/*/locations/*/taxonomies/*/policyTags/*. At most 1 policy tag is currently allowed.



880
881
882
883
884
# File 'lib/google/cloud/bigquery/schema/field.rb', line 880

def datetime name, description: nil, mode: :nullable, policy_tags: nil
  record_check!

  add_field name, :datetime, description: description, mode: mode, policy_tags: policy_tags
end

#datetime?Boolean

Checks if the type of the field is DATETIME.

Returns:

  • (Boolean)

    true when DATETIME, false otherwise.



465
466
467
# File 'lib/google/cloud/bigquery/schema/field.rb', line 465

def datetime?
  type == "DATETIME"
end

#default_value_expressionString

The default value of a field using a SQL expression. It can only be set for top level fields (columns). Default value for the entire struct or array is set using a struct or array expression. The valid SQL expressions are: - Literals for all data types, including STRUCT and ARRAY. - The following functions: CURRENT_TIMESTAMP CURRENT_TIME CURRENT_DATE CURRENT_DATETIME GENERATE_UUID RAND SESSION_USER ST_GEOPOINT - Struct or array composed with the above allowed functions, for example: "[CURRENT_DATE(), DATE '2020-01-01'"]

Returns:

  • (String)

    The default value expression of the field.



288
289
290
# File 'lib/google/cloud/bigquery/schema/field.rb', line 288

def default_value_expression
  @gapi.default_value_expression
end

#default_value_expression=(default_value_expression) ⇒ Object

Updates the default value expression of the field.

Parameters:

  • default_value_expression (String)

    The default value of a field using a SQL expression. It can only be set for top level fields (columns). Use a struct or array expression to specify default value for the entire struct or array. The valid SQL expressions are:

    • Literals for all data types, including STRUCT and ARRAY.
    • The following functions: CURRENT_TIMESTAMP CURRENT_TIME CURRENT_DATE CURRENT_DATETIME GENERATE_UUID RAND SESSION_USER ST_GEOPOINT
    • Struct or array composed with the above allowed functions, for example: "[CURRENT_DATE(), DATE '2020-01-01'"]


312
313
314
# File 'lib/google/cloud/bigquery/schema/field.rb', line 312

def default_value_expression= default_value_expression
  @gapi.update! default_value_expression: default_value_expression
end

#descriptionString

The description of the field.

Returns:

  • (String)

    The field description. The maximum length is 1,024 characters.



177
178
179
# File 'lib/google/cloud/bigquery/schema/field.rb', line 177

def description
  @gapi.description
end

#description=(new_description) ⇒ Object

Updates the description of the field.

Parameters:

  • new_description (String)

    The field description. The maximum length is 1,024 characters.



187
188
189
# File 'lib/google/cloud/bigquery/schema/field.rb', line 187

def description= new_description
  @gapi.update! description: new_description
end

#field(name) {|f| ... } ⇒ Field?

Retrieve a nested field by name, if the type property is set to RECORD. Will return nil otherwise.

Yields:

  • (f)

Returns:

  • (Field, nil)

    The nested schema field object, or nil.



569
570
571
572
573
574
# File 'lib/google/cloud/bigquery/schema/field.rb', line 569

def field name
  f = fields.find { |fld| fld.name == name.to_s }
  return nil if f.nil?
  yield f if block_given?
  f
end

#fieldsArray<Field>?

The nested fields if the type property is set to RECORD. Will be empty otherwise.

Returns:

  • (Array<Field>, nil)

    The nested schema fields if the type is set to RECORD.



513
514
515
516
517
518
519
# File 'lib/google/cloud/bigquery/schema/field.rb', line 513

def fields
  if frozen?
    Array(@gapi.fields).map { |f| Field.from_gapi(f).freeze }.freeze
  else
    Array(@gapi.fields).map { |f| Field.from_gapi f }
  end
end

#float(name, description: nil, mode: :nullable, policy_tags: nil) ⇒ Object

Adds a floating-point number field to the nested schema of a record field.

This can only be called on fields that are of type RECORD.

Parameters:

  • name (String)

    The field name. The name must contain only letters ([A-Za-z]), numbers ([0-9]), or underscores (_), and must start with a letter or underscore. The maximum length is 128 characters.

  • description (String) (defaults to: nil)

    A description of the field.

  • mode (Symbol) (defaults to: :nullable)

    The field's mode. The possible values are :nullable, :required, and :repeated. The default value is :nullable.

  • policy_tags (Array<String>, String) (defaults to: nil)

    The policy tag list or single policy tag for the field. Policy tag identifiers are of the form projects/*/locations/*/taxonomies/*/policyTags/*. At most 1 policy tag is currently allowed.



651
652
653
654
655
# File 'lib/google/cloud/bigquery/schema/field.rb', line 651

def float name, description: nil, mode: :nullable, policy_tags: nil
  record_check!

  add_field name, :float, description: description, mode: mode, policy_tags: policy_tags
end

#float?Boolean

Checks if the type of the field is FLOAT.

Returns:

  • (Boolean)

    true when FLOAT, false otherwise.



402
403
404
# File 'lib/google/cloud/bigquery/schema/field.rb', line 402

def float?
  type == "FLOAT" || type == "FLOAT64"
end

#geography(name, description: nil, mode: :nullable, policy_tags: nil) ⇒ Object

Adds a geography field to the nested schema of a record field.

Parameters:

  • name (String)

    The field name. The name must contain only letters ([A-Za-z]), numbers ([0-9]), or underscores (_), and must start with a letter or underscore. The maximum length is 128 characters.

  • description (String) (defaults to: nil)

    A description of the field.

  • mode (Symbol) (defaults to: :nullable)

    The field's mode. The possible values are :nullable, :required, and :repeated. The default value is :nullable.

  • policy_tags (Array<String>, String) (defaults to: nil)

    The policy tag list or single policy tag for the field. Policy tag identifiers are of the form projects/*/locations/*/taxonomies/*/policyTags/*. At most 1 policy tag is currently allowed.

See Also:



928
929
930
931
932
# File 'lib/google/cloud/bigquery/schema/field.rb', line 928

def geography name, description: nil, mode: :nullable, policy_tags: nil
  record_check!

  add_field name, :geography, description: description, mode: mode, policy_tags: policy_tags
end

#geography?Boolean

Checks if the type of the field is GEOGRAPHY.

Returns:

  • (Boolean)

    true when GEOGRAPHY, false otherwise.



483
484
485
# File 'lib/google/cloud/bigquery/schema/field.rb', line 483

def geography?
  type == "GEOGRAPHY"
end

#headersArray<Symbol>?

The names of the nested fields as symbols if the type property is set to RECORD. Will be empty otherwise.

Returns:

  • (Array<Symbol>, nil)

    The names of the nested schema fields if the type is set to RECORD.



528
529
530
# File 'lib/google/cloud/bigquery/schema/field.rb', line 528

def headers
  fields.map(&:name).map(&:to_sym)
end

#integer(name, description: nil, mode: :nullable, policy_tags: nil) ⇒ Object

Adds an integer field to the nested schema of a record field.

This can only be called on fields that are of type RECORD.

Parameters:

  • name (String)

    The field name. The name must contain only letters ([A-Za-z]), numbers ([0-9]), or underscores (_), and must start with a letter or underscore. The maximum length is 128 characters.

  • description (String) (defaults to: nil)

    A description of the field.

  • mode (Symbol) (defaults to: :nullable)

    The field's mode. The possible values are :nullable, :required, and :repeated. The default value is :nullable.

  • policy_tags (Array<String>, String) (defaults to: nil)

    The policy tag list or single policy tag for the field. Policy tag identifiers are of the form projects/*/locations/*/taxonomies/*/policyTags/*. At most 1 policy tag is currently allowed.



626
627
628
629
630
# File 'lib/google/cloud/bigquery/schema/field.rb', line 626

def integer name, description: nil, mode: :nullable, policy_tags: nil
  record_check!

  add_field name, :integer, description: description, mode: mode, policy_tags: policy_tags
end

#integer?Boolean

Checks if the type of the field is INTEGER.

Returns:

  • (Boolean)

    true when INTEGER, false otherwise.



393
394
395
# File 'lib/google/cloud/bigquery/schema/field.rb', line 393

def integer?
  type == "INTEGER" || type == "INT64"
end

#json(name, description: nil, mode: :nullable, policy_tags: nil) ⇒ Object

Adds a json field to the nested schema of a record field.

https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#json_type

Parameters:

  • name (String)

    The field name. The name must contain only letters (a-z, A-Z), numbers (0-9), or underscores (_), and must start with a letter or underscore. The maximum length is 128 characters.

  • description (String) (defaults to: nil)

    A description of the field.

  • mode (Symbol) (defaults to: :nullable)

    The field's mode. The possible values are :nullable, :required, and :repeated. The default value is :nullable.

  • policy_tags (Array<String>, String) (defaults to: nil)

    The policy tag list or single policy tag for the field. Policy tag identifiers are of the form projects/*/locations/*/taxonomies/*/policyTags/*. At most 1 policy tag is currently allowed.



952
953
954
955
956
# File 'lib/google/cloud/bigquery/schema/field.rb', line 952

def json name, description: nil, mode: :nullable, policy_tags: nil
  record_check!

  add_field name, :json, description: description, mode: mode, policy_tags: policy_tags
end

#json?Boolean

Checks if the type of the field is JSON.

Returns:

  • (Boolean)

    true when JSON, false otherwise.



492
493
494
# File 'lib/google/cloud/bigquery/schema/field.rb', line 492

def json?
  type == "JSON"
end

#max_lengthInteger?

The maximum length of values of this field for #string? or #bytes? fields. If max_length is not specified, no maximum length constraint is imposed on this field. If type = STRING, then max_length represents the maximum UTF-8 length of strings in this field. If type = BYTES, then max_length represents the maximum number of bytes in this field.

Returns:

  • (Integer, nil)

    The maximum length of values of this field, or nil.



324
325
326
# File 'lib/google/cloud/bigquery/schema/field.rb', line 324

def max_length
  @gapi.max_length
end

#modeString

The mode of the field.

Returns:

  • (String)

    The field mode. Possible values include NULLABLE, REQUIRED and REPEATED. The default value is NULLABLE.



197
198
199
# File 'lib/google/cloud/bigquery/schema/field.rb', line 197

def mode
  @gapi.mode
end

#mode=(new_mode) ⇒ Object

Updates the mode of the field.

Parameters:

  • new_mode (String)

    The field mode. Possible values include NULLABLE, REQUIRED and REPEATED. The default value is NULLABLE.



208
209
210
# File 'lib/google/cloud/bigquery/schema/field.rb', line 208

def mode= new_mode
  @gapi.update! mode: verify_mode(new_mode)
end

#nameString

The name of the field.

Returns:

  • (String)

    The field name. The name must contain only letters ([A-Za-z]), numbers ([0-9]), or underscores (_), and must start with a letter or underscore. The maximum length is 128 characters.



72
73
74
# File 'lib/google/cloud/bigquery/schema/field.rb', line 72

def name
  @gapi.name
end

#name=(new_name) ⇒ Object

Updates the name of the field.

Parameters:

  • new_name (String)

    The field name. The name must contain only letters ([A-Za-z]), numbers ([0-9]), or underscores (_), and must start with a letter or underscore. The maximum length is 128 characters.



84
85
86
# File 'lib/google/cloud/bigquery/schema/field.rb', line 84

def name= new_name
  @gapi.update! name: String(new_name)
end

#nullable?Boolean

Checks if the type of the field is NULLABLE.

Returns:

  • (Boolean)

    true when NULLABLE, false otherwise.



149
150
151
# File 'lib/google/cloud/bigquery/schema/field.rb', line 149

def nullable?
  mode == "NULLABLE"
end

#numeric(name, description: nil, mode: :nullable, policy_tags: nil, precision: nil, scale: nil) ⇒ Object

Adds a numeric number field to the schema. NUMERIC is a decimal type with fixed precision and scale. Precision is the number of digits that the number contains. Scale is how many of these digits appear after the decimal point. It supports:

Precision: 38 Scale: 9 Min: -9.9999999999999999999999999999999999999E+28 Max: 9.9999999999999999999999999999999999999E+28

This type can represent decimal fractions exactly, and is suitable for financial calculations.

This can only be called on fields that are of type RECORD.

Parameters:

  • name (String)

    The field name. The name must contain only letters ([A-Za-z]), numbers ([0-9]), or underscores (_), and must start with a letter or underscore. The maximum length is 128 characters.

  • description (String) (defaults to: nil)

    A description of the field.

  • mode (Symbol) (defaults to: :nullable)

    The field's mode. The possible values are :nullable, :required, and :repeated. The default value is :nullable.

  • policy_tags (Array<String>, String) (defaults to: nil)

    The policy tag list or single policy tag for the field. Policy tag identifiers are of the form projects/*/locations/*/taxonomies/*/policyTags/*. At most 1 policy tag is currently allowed.

  • precision (Integer) (defaults to: nil)

    The precision (maximum number of total digits) for the field. Acceptable values for precision must be: 1 ≤ (precision - scale) ≤ 29. Values for scale must be: 0 ≤ scale ≤ 9. If the scale value is set, the precision value must be set as well.

  • scale (Integer) (defaults to: nil)

    The scale (maximum number of digits in the fractional part) for the field. Acceptable values for precision must be: 1 ≤ (precision - scale) ≤ 29. Values for scale must be: 0 ≤ scale ≤ 9. If the scale value is set, the precision value must be set as well.



696
697
698
699
700
701
702
703
704
705
706
# File 'lib/google/cloud/bigquery/schema/field.rb', line 696

def numeric name, description: nil, mode: :nullable, policy_tags: nil, precision: nil, scale: nil
  record_check!

  add_field name,
            :numeric,
            description: description,
            mode: mode,
            policy_tags: policy_tags,
            precision: precision,
            scale: scale
end

#numeric?Boolean

Checks if the type of the field is NUMERIC.

Returns:

  • (Boolean)

    true when NUMERIC, false otherwise.



411
412
413
# File 'lib/google/cloud/bigquery/schema/field.rb', line 411

def numeric?
  type == "NUMERIC"
end

#param_typeSymbol, ...

The types of the field, using the same format as the optional query parameter types.

The parameter types are one of the following BigQuery type codes:

  • :BOOL
  • :INT64
  • :FLOAT64
  • :NUMERIC
  • :BIGNUMERIC
  • :STRING
  • :DATETIME
  • :DATE
  • :TIMESTAMP
  • :TIME
  • :BYTES
  • Array - Lists are specified by providing the type code in an array. For example, an array of integers are specified as [:INT64].
  • Hash - Types for STRUCT values (Hash objects) are specified using a Hash object, where the keys are the nested field names, and the values are the the nested field types.

Returns:

  • (Symbol, Array, Hash)

    The type.



556
557
558
559
560
561
# File 'lib/google/cloud/bigquery/schema/field.rb', line 556

def param_type
  param_type = type.to_sym
  param_type = fields.to_h { |field| [field.name.to_sym, field.param_type] } if record?
  param_type = [param_type] if repeated?
  param_type
end

#policy_tagsArray<String>?

The policy tag list for the field. Policy tag identifiers are of the form projects/*/locations/*/taxonomies/*/policyTags/*. At most 1 policy tag is currently allowed.

Examples:

require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new
dataset = bigquery.dataset "my_dataset"
table = dataset.table "my_table"

table.schema.field("age").policy_tags

Returns:

  • (Array<String>, nil)

    The policy tag list for the field, or nil.

See Also:



231
232
233
234
# File 'lib/google/cloud/bigquery/schema/field.rb', line 231

def policy_tags
  names = @gapi.policy_tags&.names
  names.to_a if names && !names.empty?
end

#policy_tags=(new_policy_tags) ⇒ Object

Updates the policy tag list for the field.

Examples:

require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new
dataset = bigquery.dataset "my_dataset"
table = dataset.table "my_table"

policy_tag = "projects/my-project/locations/us/taxonomies/my-taxonomy/policyTags/my-policy-tag"
table.schema do |schema|
  schema.field("age").policy_tags = policy_tag
end

table.schema.field("age").policy_tags

Parameters:

  • new_policy_tags (Array<String>, String, nil)

    The policy tag list or single policy tag for the field, or nil to remove the existing policy tags. Policy tag identifiers are of the form projects/*/locations/*/taxonomies/*/policyTags/*. At most 1 policy tag is currently allowed.

See Also:



261
262
263
264
265
266
267
# File 'lib/google/cloud/bigquery/schema/field.rb', line 261

def policy_tags= new_policy_tags
  # If new_policy_tags is nil, send an empty array.
  # Sending a nil value for policy_tags results in no change.
  new_policy_tags = Array(new_policy_tags)
  policy_tag_list = Google::Apis::BigqueryV2::TableFieldSchema::PolicyTags.new names: new_policy_tags
  @gapi.update! policy_tags: policy_tag_list
end

#precisionInteger?

The precision (maximum number of total digits) for NUMERIC or BIGNUMERIC types. For #numeric? fields, acceptable values for precision must be 1 ≤ (precision - scale) ≤ 29 and values for scale must be 0 ≤ scale ≤ 9. For #bignumeric? fields, acceptable values for precision must be 1 ≤ (precision - scale) ≤ 38 and values for scale must be 0 ≤ scale ≤ 38. If the scale value is set, the precision value must be set as well.

Returns:

  • (Integer, nil)

    The precision for the field, or nil.



337
338
339
# File 'lib/google/cloud/bigquery/schema/field.rb', line 337

def precision
  @gapi.precision
end

#record(name, description: nil, mode: nil) {|nested_schema| ... } ⇒ Object

Adds a record field to the nested schema of a record field. A block must be passed describing the nested fields of the record. For more information about nested and repeated records, see Preparing Data for BigQuery.

This can only be called on fields that are of type RECORD.

Examples:

require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new
dataset = bigquery.dataset "my_dataset"
table = dataset.create_table "my_table"

table.schema do |schema|
  schema.string "first_name", mode: :required
  schema.record "cities_lived", mode: :repeated do |cities_lived|
    cities_lived.record "city", mode: :required do |city|
      city.string "name", mode: :required
      city.string "country", mode: :required
    end
    cities_lived.integer "number_of_years", mode: :required
  end
end

Parameters:

  • name (String)

    The field name. The name must contain only letters ([A-Za-z]), numbers ([0-9]), or underscores (_), and must start with a letter or underscore. The maximum length is 128 characters.

  • description (String) (defaults to: nil)

    A description of the field.

  • mode (Symbol) (defaults to: nil)

    The field's mode. The possible values are :nullable, :required, and :repeated. The default value is :nullable.

Yields:

  • (nested_schema)

    a block for setting the nested schema

Yield Parameters:

  • nested_schema (Schema)

    the object accepting the nested schema

Raises:

  • (ArgumentError)


996
997
998
999
1000
1001
1002
1003
1004
1005
# File 'lib/google/cloud/bigquery/schema/field.rb', line 996

def record name, description: nil, mode: nil
  record_check!

  # TODO: do we need to raise if no block was given?
  raise ArgumentError, "a block is required" unless block_given?

  nested_field = add_field name, :record, description: description, mode: mode
  yield nested_field
  nested_field
end

#record?Boolean Also known as: struct?

Checks if the type of the field is RECORD.

Returns:

  • (Boolean)

    true when RECORD, false otherwise.



501
502
503
# File 'lib/google/cloud/bigquery/schema/field.rb', line 501

def record?
  type == "RECORD" || type == "STRUCT"
end

#repeated?Boolean

Checks if the type of the field is REPEATED.

Returns:

  • (Boolean)

    true when REPEATED, false otherwise.



167
168
169
# File 'lib/google/cloud/bigquery/schema/field.rb', line 167

def repeated?
  mode == "REPEATED"
end

#required?Boolean

Checks if the type of the field is REQUIRED.

Returns:

  • (Boolean)

    true when REQUIRED, false otherwise.



158
159
160
# File 'lib/google/cloud/bigquery/schema/field.rb', line 158

def required?
  mode == "REQUIRED"
end

#scaleInteger?

The scale (maximum number of digits in the fractional part) for NUMERIC or BIGNUMERIC types. For #numeric? fields, acceptable values for precision must be 1 ≤ (precision - scale) ≤ 29 and values for scale must be 0 ≤ scale ≤ 9. For #bignumeric? fields, acceptable values for precision must be 1 ≤ (precision - scale) ≤ 38 and values for scale must be 0 ≤ scale ≤ 38. If the scale value is set, the precision value must be set as well.

Returns:

  • (Integer, nil)

    The scale for the field, or nil.



350
351
352
# File 'lib/google/cloud/bigquery/schema/field.rb', line 350

def scale
  @gapi.scale
end

#string(name, description: nil, mode: :nullable, policy_tags: nil, max_length: nil, collation: nil) ⇒ Object

Adds a string field to the nested schema of a record field.

This can only be called on fields that are of type RECORD.

Parameters:

  • name (String)

    The field name. The name must contain only letters ([A-Za-z]), numbers ([0-9]), or underscores (_), and must start with a letter or underscore. The maximum length is 128 characters.

  • description (String) (defaults to: nil)

    A description of the field.

  • mode (Symbol) (defaults to: :nullable)

    The field's mode. The possible values are :nullable, :required, and :repeated. The default value is :nullable.

  • policy_tags (Array<String>, String) (defaults to: nil)

    The policy tag list or single policy tag for the field. Policy tag identifiers are of the form projects/*/locations/*/taxonomies/*/policyTags/*. At most 1 policy tag is currently allowed.

  • max_length (Integer) (defaults to: nil)

    The maximum UTF-8 length of strings allowed in the field.



596
597
598
599
600
601
602
603
604
605
606
# File 'lib/google/cloud/bigquery/schema/field.rb', line 596

def string name, description: nil, mode: :nullable, policy_tags: nil, max_length: nil, collation: nil
  record_check!

  add_field name,
            :string,
            description: description,
            mode: mode,
            policy_tags: policy_tags,
            max_length: max_length,
            collation: collation
end

#string?Boolean

Checks if the type of the field is STRING.

Returns:

  • (Boolean)

    true when STRING, false otherwise.



384
385
386
# File 'lib/google/cloud/bigquery/schema/field.rb', line 384

def string?
  type == "STRING"
end

#time(name, description: nil, mode: :nullable, policy_tags: nil) ⇒ Object

Adds a time field to the nested schema of a record field.

This can only be called on fields that are of type RECORD.

Parameters:

  • name (String)

    The field name. The name must contain only letters ([A-Za-z]), numbers ([0-9]), or underscores (_), and must start with a letter or underscore. The maximum length is 128 characters.

  • description (String) (defaults to: nil)

    A description of the field.

  • mode (Symbol) (defaults to: :nullable)

    The field's mode. The possible values are :nullable, :required, and :repeated. The default value is :nullable.

  • policy_tags (Array<String>, String) (defaults to: nil)

    The policy tag list or single policy tag for the field. Policy tag identifiers are of the form projects/*/locations/*/taxonomies/*/policyTags/*. At most 1 policy tag is currently allowed.



856
857
858
859
860
# File 'lib/google/cloud/bigquery/schema/field.rb', line 856

def time name, description: nil, mode: :nullable, policy_tags: nil
  record_check!

  add_field name, :time, description: description, mode: mode, policy_tags: policy_tags
end

#time?Boolean

Checks if the type of the field is TIME.

Returns:

  • (Boolean)

    true when TIME, false otherwise.



456
457
458
# File 'lib/google/cloud/bigquery/schema/field.rb', line 456

def time?
  type == "TIME"
end

#timestamp(name, description: nil, mode: :nullable, policy_tags: nil) ⇒ Object

Adds a timestamp field to the nested schema of a record field.

This can only be called on fields that are of type RECORD.

Parameters:

  • name (String)

    The field name. The name must contain only letters ([A-Za-z]), numbers ([0-9]), or underscores (_), and must start with a letter or underscore. The maximum length is 128 characters.

  • description (String) (defaults to: nil)

    A description of the field.

  • mode (Symbol) (defaults to: :nullable)

    The field's mode. The possible values are :nullable, :required, and :repeated. The default value is :nullable.

  • policy_tags (Array<String>, String) (defaults to: nil)

    The policy tag list or single policy tag for the field. Policy tag identifiers are of the form projects/*/locations/*/taxonomies/*/policyTags/*. At most 1 policy tag is currently allowed.



832
833
834
835
836
# File 'lib/google/cloud/bigquery/schema/field.rb', line 832

def timestamp name, description: nil, mode: :nullable, policy_tags: nil
  record_check!

  add_field name, :timestamp, description: description, mode: mode, policy_tags: policy_tags
end

#timestamp?Boolean

Checks if the type of the field is TIMESTAMP.

Returns:

  • (Boolean)

    true when TIMESTAMP, false otherwise.



447
448
449
# File 'lib/google/cloud/bigquery/schema/field.rb', line 447

def timestamp?
  type == "TIMESTAMP"
end

#typeString

The data type of the field.

Returns:

  • (String)

    The field data type. Possible values include:

    • BIGNUMERIC
    • BOOL
    • BOOLEAN (same as BOOL)
    • BYTES
    • DATE
    • DATETIME
    • FLOAT
    • FLOAT64 (same as FLOAT)
    • GEOGRAPHY
    • JSON
    • INTEGER
    • INT64 (same as INTEGER)
    • NUMERIC
    • RECORD (where RECORD indicates that the field contains a nested schema)
    • STRING
    • STRUCT (same as RECORD)
    • TIME
    • TIMESTAMP


112
113
114
# File 'lib/google/cloud/bigquery/schema/field.rb', line 112

def type
  @gapi.type
end

#type=(new_type) ⇒ Object

Updates the data type of the field.

Parameters:

  • new_type (String)

    The data type. Possible values include:

    • BIGNUMERIC
    • BOOL
    • BOOLEAN (same as BOOL)
    • BYTES
    • DATE
    • DATETIME
    • FLOAT
    • FLOAT64 (same as FLOAT)
    • GEOGRAPHY
    • JSON
    • INTEGER
    • INT64 (same as INTEGER)
    • NUMERIC
    • RECORD (where RECORD indicates that the field contains a nested schema)
    • STRING
    • STRUCT (same as RECORD)
    • TIME
    • TIMESTAMP


140
141
142
# File 'lib/google/cloud/bigquery/schema/field.rb', line 140

def type= new_type
  @gapi.update! type: verify_type(new_type)
end