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

#boolean(name, description: nil, mode: :nullable) ⇒ 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-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.



432
433
434
435
436
# File 'lib/google/cloud/bigquery/schema/field.rb', line 432

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

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

#boolean?Boolean

Checks if the type of the field is BOOLEAN.

Returns:

  • (Boolean)

    true when BOOLEAN, false otherwise.



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

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

#bytes(name, description: nil, mode: :nullable) ⇒ 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-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.



452
453
454
455
456
# File 'lib/google/cloud/bigquery/schema/field.rb', line 452

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

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

#bytes?Boolean

Checks if the type of the field is BYTES.

Returns:

  • (Boolean)

    true when BYTES, false otherwise.



216
217
218
# File 'lib/google/cloud/bigquery/schema/field.rb', line 216

def bytes?
  type == "BYTES"
end

#date(name, description: nil, mode: :nullable) ⇒ 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-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.



532
533
534
535
536
# File 'lib/google/cloud/bigquery/schema/field.rb', line 532

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

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

#date?Boolean

Checks if the type of the field is DATE.

Returns:

  • (Boolean)

    true when DATE, false otherwise.



252
253
254
# File 'lib/google/cloud/bigquery/schema/field.rb', line 252

def date?
  type == "DATE"
end

#datetime(name, description: nil, mode: :nullable) ⇒ 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-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.



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

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

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

#datetime?Boolean

Checks if the type of the field is DATETIME.

Returns:

  • (Boolean)

    true when DATETIME, false otherwise.



243
244
245
# File 'lib/google/cloud/bigquery/schema/field.rb', line 243

def datetime?
  type == "DATETIME"
end

#descriptionString

The description of the field.

Returns:

  • (String)

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



131
132
133
# File 'lib/google/cloud/bigquery/schema/field.rb', line 131

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.



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

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.



328
329
330
331
332
333
# File 'lib/google/cloud/bigquery/schema/field.rb', line 328

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.



273
274
275
276
277
278
279
# File 'lib/google/cloud/bigquery/schema/field.rb', line 273

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) ⇒ 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-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.



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

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

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

#float?Boolean

Checks if the type of the field is FLOAT.

Returns:

  • (Boolean)

    true when FLOAT, false otherwise.



189
190
191
# File 'lib/google/cloud/bigquery/schema/field.rb', line 189

def float?
  type == "FLOAT" || type == "FLOAT64"
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.



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

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

#integer(name, description: nil, mode: :nullable) ⇒ 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-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.



369
370
371
372
373
# File 'lib/google/cloud/bigquery/schema/field.rb', line 369

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

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

#integer?Boolean

Checks if the type of the field is INTEGER.

Returns:

  • (Boolean)

    true when INTEGER, false otherwise.



180
181
182
# File 'lib/google/cloud/bigquery/schema/field.rb', line 180

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

#modeString

The mode of the field.

Returns:

  • (String)

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



151
152
153
# File 'lib/google/cloud/bigquery/schema/field.rb', line 151

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.



162
163
164
# File 'lib/google/cloud/bigquery/schema/field.rb', line 162

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-z, A-Z), numbers (0-9), or underscores (_), and must start with a letter or underscore. The maximum length is 128 characters.



54
55
56
# File 'lib/google/cloud/bigquery/schema/field.rb', line 54

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-z, A-Z), numbers (0-9), or underscores (_), and must start with a letter or underscore. The maximum length is 128 characters.



66
67
68
# File 'lib/google/cloud/bigquery/schema/field.rb', line 66

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.



103
104
105
# File 'lib/google/cloud/bigquery/schema/field.rb', line 103

def nullable?
  mode == "NULLABLE"
end

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

Adds a numeric number field to the schema. Numeric is a fixed-precision numeric type with 38 decimal digits, 9 that follow the decimal point.

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-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.



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

def numeric name, description: nil, mode: :nullable
  record_check!

  add_field name, :numeric, description: description, mode: mode
end

#numeric?Boolean

Checks if the type of the field is NUMERIC.

Returns:

  • (Boolean)

    true when NUMERIC, false otherwise.



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

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
  • :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.



315
316
317
318
319
320
# File 'lib/google/cloud/bigquery/schema/field.rb', line 315

def param_type
  param_type = type.to_sym
  param_type = Hash[fields.map { |field| [field.name.to_sym, field.param_type] }] if record?
  param_type = [param_type] if repeated?
  param_type
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-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: 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)


576
577
578
579
580
581
582
583
584
585
# File 'lib/google/cloud/bigquery/schema/field.rb', line 576

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.



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

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.



121
122
123
# File 'lib/google/cloud/bigquery/schema/field.rb', line 121

def repeated?
  mode == "REPEATED"
end

#required?Boolean

Checks if the type of the field is REQUIRED.

Returns:

  • (Boolean)

    true when REQUIRED, false otherwise.



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

def required?
  mode == "REQUIRED"
end

#string(name, description: nil, mode: :nullable) ⇒ 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-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.



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

def string name, description: nil, mode: :nullable
  record_check!

  add_field name, :string, description: description, mode: mode
end

#string?Boolean

Checks if the type of the field is STRING.

Returns:

  • (Boolean)

    true when STRING, false otherwise.



171
172
173
# File 'lib/google/cloud/bigquery/schema/field.rb', line 171

def string?
  type == "STRING"
end

#time(name, description: nil, mode: :nullable) ⇒ 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-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.



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

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

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

#time?Boolean

Checks if the type of the field is TIME.

Returns:

  • (Boolean)

    true when TIME, false otherwise.



234
235
236
# File 'lib/google/cloud/bigquery/schema/field.rb', line 234

def time?
  type == "TIME"
end

#timestamp(name, description: nil, mode: :nullable) ⇒ 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-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.



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

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

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

#timestamp?Boolean

Checks if the type of the field is TIMESTAMP.

Returns:

  • (Boolean)

    true when TIMESTAMP, false otherwise.



225
226
227
# File 'lib/google/cloud/bigquery/schema/field.rb', line 225

def timestamp?
  type == "TIMESTAMP"
end

#typeString

The data type of the field.

Returns:

  • (String)

    The field data type. Possible values include STRING, BYTES, INTEGER, INT64 (same as INTEGER), FLOAT, FLOAT64 (same as FLOAT), NUMERIC, BOOLEAN, BOOL (same as BOOLEAN), TIMESTAMP, DATE, TIME, DATETIME, RECORD (where RECORD indicates that the field contains a nested schema) or STRUCT (same as RECORD).



80
81
82
# File 'lib/google/cloud/bigquery/schema/field.rb', line 80

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 STRING, BYTES, INTEGER, INT64 (same as INTEGER), FLOAT, FLOAT64 (same as FLOAT), NUMERIC, BOOLEAN, BOOL (same as BOOLEAN), TIMESTAMP, DATE, TIME, DATETIME, RECORD (where RECORD indicates that the field contains a nested schema) or STRUCT (same as RECORD).



94
95
96
# File 'lib/google/cloud/bigquery/schema/field.rb', line 94

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