class Google::Cloud::Bigquery::Schema


end
end
cities_lived.integer “number_of_years”, mode: :required
cities_lived.string “place”, mode: :required
schema.record “cities_lived”, mode: :repeated do |cities_lived|
schema.string “first_name”, mode: :required
table.schema do |schema|
table = dataset.create_table “my_table”
dataset = bigquery.dataset “my_dataset”
bigquery = Google::Cloud::Bigquery.new
require “google/cloud/bigquery”
@example
Loading denormalized, nested, and repeated data
@see cloud.google.com/bigquery/docs/loading-data#loading_denormalized_nested_and_repeated_data<br><br>repeated fields via a nested block.
{Dataset#create_table} and {Table#schema}. Supports nested and
A builder for BigQuery table schemas, passed to block arguments to
# Table Schema
#

def self.from_gapi gapi = nil

Parameters:
  • gapi (Google::Apis::BigqueryV2::TableSchema, nil) -- Returns an

Other tags:
    Private: -
def self.from_gapi gapi = nil
  gapi ||= Google::Apis::BigqueryV2::TableSchema.new fields: []
  gapi.fields ||= []
  new.tap do |s|
    s.instance_variable_set :@gapi, gapi
    s.instance_variable_set :@original_json, gapi.to_json
  end
end

def == other

Other tags:
    Private: -
def == other
  return false unless other.is_a? Schema
  to_gapi.to_json == other.to_gapi.to_json
end

def add_field name,

def add_field name,
              type,
              description: nil,
              mode: :nullable,
              policy_tags: nil,
              max_length: nil,
              precision: nil,
              scale: nil,
              default_value_expression: nil,
              collation: nil
  frozen_check!
  new_gapi = Google::Apis::BigqueryV2::TableFieldSchema.new(
    name:        String(name),
    type:        verify_type(type),
    description: description,
    mode:        verify_mode(mode),
    fields:      []
  )
  if policy_tags
    policy_tags = Array(policy_tags)
    new_gapi.policy_tags = Google::Apis::BigqueryV2::TableFieldSchema::PolicyTags.new names: policy_tags
  end
  new_gapi.max_length = max_length if max_length
  new_gapi.precision = precision if precision
  new_gapi.scale = scale if scale
  new_gapi.default_value_expression = default_value_expression if default_value_expression
  new_gapi.collation = collation if collation
  # Remove any existing field of this name
  @gapi.fields ||= []
  @gapi.fields.reject! { |f| f.name == new_gapi.name }
  # Add to the nested fields
  @gapi.fields << new_gapi
  # return the public API object
  Field.from_gapi new_gapi
end

def bignumeric name, description: nil, mode: :nullable, policy_tags: nil,

Parameters:
  • default_value_expression (String) -- The default value of a field
  • scale (Integer) -- The scale (maximum number of digits in the
  • precision (Integer) -- The precision (maximum number of total
  • policy_tags (Array, String) -- The policy tag list or
  • mode (Symbol) -- The field's mode. The possible values are
  • description (String) -- A description of the field.
  • name (String) -- The field name. The name must contain only
def bignumeric name, description: nil, mode: :nullable, policy_tags: nil,
               precision: nil, scale: nil, default_value_expression: nil
  add_field name, :bignumeric,
            description: description,
            mode: mode,
            policy_tags: policy_tags,
            precision: precision,
            scale: scale,
            default_value_expression: default_value_expression
end

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

Parameters:
  • default_value_expression (String) -- The default value of a field
  • policy_tags (Array, String) -- The policy tag list or
  • mode (Symbol) -- The field's mode. The possible values are
  • description (String) -- A description of the field.
  • name (String) -- The field name. The name must contain only
def boolean name, description: nil, mode: :nullable, policy_tags: nil,
            default_value_expression: nil
  add_field name, :boolean,
            description: description,
            mode: mode,
            policy_tags: policy_tags,
            default_value_expression: default_value_expression
end

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

Parameters:
  • default_value_expression (String) -- The default value of a field
  • max_length (Integer) -- The maximum the maximum number of
  • policy_tags (Array, String) -- The policy tag list or
  • mode (Symbol) -- The field's mode. The possible values are
  • description (String) -- A description of the field.
  • name (String) -- The field name. The name must contain only
def bytes name, description: nil, mode: :nullable,
          policy_tags: nil, max_length: nil, default_value_expression: nil
  add_field name, :bytes,
            description: description,
            mode: mode,
            policy_tags: policy_tags,
            max_length: max_length,
            default_value_expression: default_value_expression
end

def changed?

Other tags:
    Private: -
def changed?
  return false if frozen?
  @original_json != @gapi.to_json
end

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

Parameters:
  • default_value_expression (String) -- The default value of a field
  • policy_tags (Array, String) -- The policy tag list or
  • mode (Symbol) -- The field's mode. The possible values are
  • description (String) -- A description of the field.
  • name (String) -- The field name. The name must contain only
def date name, description: nil, mode: :nullable,
         policy_tags: nil, default_value_expression: nil
  add_field name, :date,
            description: description,
            mode: mode,
            policy_tags: policy_tags,
            default_value_expression: default_value_expression
end

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

Parameters:
  • default_value_expression (String) -- The default value of a field
  • policy_tags (Array, String) -- The policy tag list or
  • mode (Symbol) -- The field's mode. The possible values are
  • description (String) -- A description of the field.
  • name (String) -- The field name. The name must contain only
def datetime name, description: nil, mode: :nullable,
             policy_tags: nil, default_value_expression: nil
  add_field name, :datetime,
            description: description,
            mode: mode,
            policy_tags: policy_tags,
            default_value_expression: default_value_expression
end

def dump schema, destination

Returns:
  • (Schema) - The schema so that commands are chainable.

Parameters:
  • destination (IO, String) -- An `IO` to which to write the
  • schema (Schema) -- A `Google::Cloud::Bigquery::Schema`.
def dump schema, destination
  schema.dump destination
end

def dump destination

Returns:
  • (Schema) - The schema so that commands are chainable.

Parameters:
  • destination (IO, String) -- An `IO` to which to write the schema,
def dump destination
  if destination.respond_to?(:rewind) && destination.respond_to?(:write)
    destination.rewind
    destination.write JSON.dump(fields.map(&:to_hash))
  else
    File.write String(destination), JSON.dump(fields.map(&:to_hash))
  end
  self
end

def empty?

Returns:
  • (Boolean) - `true` when there are no fields, `false` otherwise.
def empty?
  fields.empty?
end

def field name

Returns:
  • (Field) - A field object.
def field name
  f = fields.find { |fld| fld.name == name.to_s }
  return nil if f.nil?
  yield f if block_given?
  f
end

def fields

Returns:
  • (Array) - An array of field objects.
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

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

Parameters:
  • default_value_expression (String) -- The default value of a field
  • policy_tags (Array, String) -- The policy tag list or
  • mode (Symbol) -- The field's mode. The possible values are
  • description (String) -- A description of the field.
  • name (String) -- The field name. The name must contain only
def float name, description: nil, mode: :nullable,
          policy_tags: nil, default_value_expression: nil
  add_field name, :float,
            description: description,
            mode: mode,
            policy_tags: policy_tags,
            default_value_expression: default_value_expression
end

def frozen_check!

def frozen_check!
  return unless frozen?
  raise ArgumentError, "Cannot modify a frozen schema"
end

def geography name, description: nil, mode: :nullable,

Parameters:
  • default_value_expression (String) -- The default value of a field
  • policy_tags (Array, String) -- The policy tag list or
  • mode (Symbol) -- The field's mode. The possible values are
  • description (String) -- A description of the field.
  • name (String) -- The field name. The name must contain only

Other tags:
    See: https://cloud.google.com/bigquery/docs/gis-data - Working with BigQuery GIS data
def geography name, description: nil, mode: :nullable,
              policy_tags: nil, default_value_expression: nil
  add_field name, :geography,
            description: description,
            mode: mode,
            policy_tags: policy_tags,
            default_value_expression: default_value_expression
end

def headers

Returns:
  • (Array) - An array of column names.
def headers
  fields.map(&:name).map(&:to_sym)
end

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

Parameters:
  • default_value_expression (String) -- The default value of a field
  • policy_tags (Array, String) -- The policy tag list or
  • mode (Symbol) -- The field's mode. The possible values are
  • description (String) -- A description of the field.
  • name (String) -- The field name. The name must contain only
def integer name, description: nil, mode: :nullable,
            policy_tags: nil, default_value_expression: nil
  add_field name, :integer,
            description: description,
            mode: mode,
            policy_tags: policy_tags,
            default_value_expression: default_value_expression
end

def json name, description: nil, mode: :nullable,

Parameters:
  • default_value_expression (String) -- The default value of a field
  • policy_tags (Array, String) -- The policy tag list or
  • mode (Symbol) -- The field's mode. The possible values are
  • description (String) -- A description of the field.
  • name (String) -- The field name. The name must contain only

Other tags:
    See: https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#json_type -
def json name, description: nil, mode: :nullable,
         policy_tags: nil, default_value_expression: nil
  add_field name, :json,
            description: description,
            mode: mode,
            policy_tags: policy_tags,
            default_value_expression: default_value_expression
end

def load source

Returns:
  • (Schema) - A schema.

Parameters:
  • source (IO, String, Array) -- An `IO` containing the JSON
def load source
  new.load source
end

def load source

Returns:
  • (Schema) - The schema so that commands are chainable.

Parameters:
  • source (IO, String, Array) -- An `IO` containing the JSON
def load source
  if source.respond_to?(:rewind) && source.respond_to?(:read)
    source.rewind
    schema_json = String source.read
  elsif source.is_a? Array
    schema_json = JSON.dump source
  else
    schema_json = String source
  end
  schema_json = %({"fields":#{schema_json}})
  @gapi = Google::Apis::BigqueryV2::TableSchema.from_json schema_json
  self
end

def numeric name, description: nil, mode: :nullable, policy_tags: nil,

Parameters:
  • default_value_expression (String) -- The default value of a field
  • scale (Integer) -- The scale (maximum number of digits in the
  • precision (Integer) -- The precision (maximum number of total
  • policy_tags (Array, String) -- The policy tag list or
  • mode (Symbol) -- The field's mode. The possible values are
  • description (String) -- A description of the field.
  • name (String) -- The field name. The name must contain only
def numeric name, description: nil, mode: :nullable, policy_tags: nil,
            precision: nil, scale: nil, default_value_expression: nil
  add_field name, :numeric,
            description: description,
            mode: mode,
            policy_tags: policy_tags,
            precision: precision,
            scale: scale,
            default_value_expression: default_value_expression
end

def param_types

Returns:
  • (Hash) - A hash with column names as keys, and types as values.
def param_types
  fields.to_h { |field| [field.name.to_sym, field.param_type] }
end

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

Other tags:
    Yieldparam: field - the object accepting the

Other tags:
    Yield: - a block for setting the nested record's schema

Parameters:
  • default_value_expression (String) -- The default value of a field
  • mode (Symbol) -- The field's mode. The possible values are
  • description (String) -- A description of the field.
  • name (String) -- The field name. The name must contain only
def record name, description: nil, mode: nil,
           default_value_expression: nil
  # 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,
                           default_value_expression: default_value_expression
  yield nested_field
  nested_field
end

def string name, description: nil, mode: :nullable, policy_tags: nil,

Parameters:
  • default_value_expression (String) -- The default value of a field
  • max_length (Integer) -- The maximum UTF-8 length of strings
  • policy_tags (Array, String) -- The policy tag list or
  • mode (Symbol) -- The field's mode. The possible values are
  • description (String) -- A description of the field.
  • name (String) -- The field name. The name must contain only
def string name, description: nil, mode: :nullable, policy_tags: nil,
           max_length: nil, default_value_expression: nil, collation: nil
  add_field name, :string,
            description: description,
            mode: mode,
            policy_tags: policy_tags,
            max_length: max_length,
            default_value_expression: default_value_expression,
            collation: collation
end

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

Parameters:
  • default_value_expression (String) -- The default value of a field
  • policy_tags (Array, String) -- The policy tag list or
  • mode (Symbol) -- The field's mode. The possible values are
  • description (String) -- A description of the field.
  • name (String) -- The field name. The name must contain only
def time name, description: nil, mode: :nullable,
         policy_tags: nil, default_value_expression: nil
  add_field name, :time,
            description: description,
            mode: mode,
            policy_tags: policy_tags,
            default_value_expression: default_value_expression
end

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

Parameters:
  • default_value_expression (String) -- The default value of a field
  • policy_tags (Array, String) -- The policy tag list or
  • mode (Symbol) -- The field's mode. The possible values are
  • description (String) -- A description of the field.
  • name (String) -- The field name. The name must contain only
def timestamp name, description: nil, mode: :nullable,
              policy_tags: nil, default_value_expression: nil
  add_field name, :timestamp,
            description: description,
            mode: mode,
            policy_tags: policy_tags,
            default_value_expression: default_value_expression
end

def to_gapi

Other tags:
    Private: -
def to_gapi
  @gapi
end

def verify_mode mode

def verify_mode mode
  mode = :nullable if mode.nil?
  mode = mode.to_s.upcase
  raise ArgumentError "Unable to determine mode for '#{mode}'" unless Field::MODES.include? mode
  mode
end

def verify_type type

def verify_type type
  type = type.to_s.upcase
  raise ArgumentError, "Type '#{type}' not found" unless Field::TYPES.include? type
  type
end