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

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

Overview

Table Schema

A builder for BigQuery table schemas, passed to block arguments to Dataset#create_table and Table#schema. Supports nested and repeated fields via a nested block.

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.string "place", mode: :required
    cities_lived.integer "number_of_years", mode: :required
  end
end

See Also:

Defined Under Namespace

Classes: Field

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.dump(schema, destination) ⇒ Schema

Write a schema as JSON to a file.

The JSON schema file is the same as for the bq CLI.

Examples:

require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new
dataset = bigquery.dataset "my_dataset"
table = dataset.table "my_table"
schema = Google::Cloud::Bigquery::Schema.dump(
  table.schema,
  "schema.json"
)

Parameters:

  • schema (Schema)

    A Google::Cloud::Bigquery::Schema.

  • destination (IO, String)

    An IO to which to write the schema, or a String containing the filename to write to.

Returns:

  • (Schema)

    The schema so that commands are chainable.



105
106
107
# File 'lib/google/cloud/bigquery/schema.rb', line 105

def dump schema, destination
  schema.dump destination
end

.load(source) ⇒ Schema

Load a schema from a JSON file.

The JSON schema file is the same as for the bq CLI consisting of an array of JSON objects containing the following:

  • name: The column name
  • type: The column's data type
  • description: (Optional) The column's description
  • mode: (Optional) The column's mode (if unspecified, mode defaults to NULLABLE)
  • fields: If type is RECORD, an array of objects defining child fields with these properties

Examples:

require "google/cloud/bigquery"

schema = Google::Cloud::Bigquery::Schema.load(
  File.read("schema.json")
)

Parameters:

  • source (IO, String, Array<Hash>)

    An IO containing the JSON schema, a String containing the JSON schema, or an Array of Hashes containing the schema details.

Returns:



77
78
79
# File 'lib/google/cloud/bigquery/schema.rb', line 77

def load source
  new.load source
end

Instance Method Details

#bignumeric(name, description: nil, mode: :nullable, policy_tags: nil, precision: nil, scale: nil, default_value_expression: 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.

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.

  • default_value_expression (String) (defaults to: nil)

    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'"]


529
530
531
532
533
534
535
536
537
538
# File 'lib/google/cloud/bigquery/schema.rb', line 529

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

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

Adds a boolean field to the schema.

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.

  • default_value_expression (String) (defaults to: nil)

    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'"]


572
573
574
575
576
577
578
579
# File 'lib/google/cloud/bigquery/schema.rb', line 572

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

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

Adds a bytes field to the schema.

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.

  • default_value_expression (String) (defaults to: nil)

    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'"]


615
616
617
618
619
620
621
622
623
# File 'lib/google/cloud/bigquery/schema.rb', line 615

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

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

Adds a date field to the schema.

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.

  • default_value_expression (String) (defaults to: nil)

    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'"]


780
781
782
783
784
785
786
787
# File 'lib/google/cloud/bigquery/schema.rb', line 780

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

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

Adds a datetime field to the schema.

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.

  • default_value_expression (String) (defaults to: nil)

    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'"]


739
740
741
742
743
744
745
746
# File 'lib/google/cloud/bigquery/schema.rb', line 739

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

#dump(destination) ⇒ Schema

Write the schema as JSON to a file.

The JSON schema file is the same as for the bq CLI.

Examples:

require "google/cloud/bigquery"

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

Parameters:

  • destination (IO, String)

    An IO to which to write the schema, or a String containing the filename to write to.

Returns:

  • (Schema)

    The schema so that commands are chainable.



275
276
277
278
279
280
281
282
283
284
# File 'lib/google/cloud/bigquery/schema.rb', line 275

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

#empty?Boolean

Whether the schema has no fields defined.

Returns:

  • (Boolean)

    true when there are no fields, false otherwise.



205
206
207
# File 'lib/google/cloud/bigquery/schema.rb', line 205

def empty?
  fields.empty?
end

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

Retrieve a field by name.

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

Yields:

  • (f)

Returns:

  • (Field)

    A field object.



193
194
195
196
197
198
# File 'lib/google/cloud/bigquery/schema.rb', line 193

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 fields of the table schema.

Examples:

require "google/cloud/bigquery"

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

schema = table.schema

schema.fields.each do |field|
  puts field.name
end

Returns:

  • (Array<Field>)

    An array of field objects.



127
128
129
130
131
132
133
# File 'lib/google/cloud/bigquery/schema.rb', line 127

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, default_value_expression: nil) ⇒ Object

Adds a floating-point number field to the schema.

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.

  • default_value_expression (String) (defaults to: nil)

    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'"]


403
404
405
406
407
408
409
410
# File 'lib/google/cloud/bigquery/schema.rb', line 403

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

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

Adds a geography field to the schema.

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.

  • default_value_expression (String) (defaults to: nil)

    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'"]

See Also:



823
824
825
826
827
828
829
830
# File 'lib/google/cloud/bigquery/schema.rb', line 823

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

#headersArray<Symbol>

The names of the fields as symbols.

Examples:

require "google/cloud/bigquery"

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

schema = table.schema

schema.headers.each do |header|
  puts header
end

Returns:

  • (Array<Symbol>)

    An array of column names.



153
154
155
# File 'lib/google/cloud/bigquery/schema.rb', line 153

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

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

Adds an integer field to the schema.

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.

  • default_value_expression (String) (defaults to: nil)

    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'"]


362
363
364
365
366
367
368
369
# File 'lib/google/cloud/bigquery/schema.rb', line 362

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

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

Adds a JSON field to the schema.

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.

  • default_value_expression (String) (defaults to: nil)

    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'"]

See Also:



866
867
868
869
870
871
872
873
# File 'lib/google/cloud/bigquery/schema.rb', line 866

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

#load(source) ⇒ Schema

Load the schema from a JSON file.

The JSON schema file is the same as for the bq CLI consisting of an array of JSON objects containing the following:

  • name: The column name
  • type: The column's data type
  • description: (Optional) The column's description
  • mode: (Optional) The column's mode (if unspecified, mode defaults to NULLABLE)
  • fields: If type is RECORD, an array of objects defining child fields with these properties

Examples:

require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new
dataset = bigquery.dataset "my_dataset"
table = dataset.table "my_table" do |t|
  t.schema.load File.read("path/to/schema.json")
end

Parameters:

  • source (IO, String, Array<Hash>)

    An IO containing the JSON schema, a String containing the JSON schema, or an Array of Hashes containing the schema details.

Returns:

  • (Schema)

    The schema so that commands are chainable.



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/google/cloud/bigquery/schema.rb', line 239

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

#numeric(name, description: nil, mode: :nullable, policy_tags: nil, precision: nil, scale: nil, default_value_expression: 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.

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.

  • default_value_expression (String) (defaults to: nil)

    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'"]


465
466
467
468
469
470
471
472
473
474
# File 'lib/google/cloud/bigquery/schema.rb', line 465

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

#param_typesHash

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

Examples:

require "google/cloud/bigquery"

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

schema = table.schema

schema.param_types

Returns:

  • (Hash)

    A hash with column names as keys, and types as values.



174
175
176
# File 'lib/google/cloud/bigquery/schema.rb', line 174

def param_types
  fields.to_h { |field| [field.name.to_sym, field.param_type] }
end

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

Adds a record field to the schema. A block must be passed describing the nested fields of the record. For more information about nested and repeated records, see Loading denormalized, nested, and repeated data .

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.string "place", mode: :required
    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.

  • default_value_expression (String) (defaults to: nil)

    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'"]

Yields:

  • (field)

    a block for setting the nested record's schema

Yield Parameters:

  • field (Field)

    the object accepting the nested schema

Raises:

  • (ArgumentError)


926
927
928
929
930
931
932
933
934
935
936
937
# File 'lib/google/cloud/bigquery/schema.rb', line 926

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

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

Adds a string field to the schema.

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.

  • default_value_expression (String) (defaults to: nil)

    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'"]


320
321
322
323
324
325
326
327
328
# File 'lib/google/cloud/bigquery/schema.rb', line 320

def string name, description: nil, mode: :nullable, policy_tags: nil,
           max_length: nil, default_value_expression: nil
  add_field name, :string,
            description: description,
            mode: mode,
            policy_tags: policy_tags,
            max_length: max_length,
            default_value_expression: default_value_expression
end

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

Adds a time field to the schema.

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.

  • default_value_expression (String) (defaults to: nil)

    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'"]


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

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

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

Adds a timestamp field to the schema.

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.

  • default_value_expression (String) (defaults to: nil)

    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'"]


657
658
659
660
661
662
663
664
# File 'lib/google/cloud/bigquery/schema.rb', line 657

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