class Google::Cloud::Bigquery::StandardSql::DataType

The type of a variable, e.g., a function argument. See {Routine} and {Argument}.

@example

require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new
dataset = bigquery.dataset "my_dataset"
routine = dataset.create_routine "my_routine" do |r|
  r.routine_type = "SCALAR_FUNCTION"
  r.language = :SQL
  r.body = "(SELECT SUM(IF(elem.name = \"foo\",elem.val,null)) FROM UNNEST(arr) AS elem)"
  r.arguments = [
    Google::Cloud::Bigquery::Argument.new(
      name: "arr",
      argument_kind: "FIXED_TYPE",
      data_type: Google::Cloud::Bigquery::StandardSql::DataType.new(
        type_kind: "ARRAY",
        array_element_type: Google::Cloud::Bigquery::StandardSql::DataType.new(
          type_kind: "STRUCT",
          struct_type: Google::Cloud::Bigquery::StandardSql::StructType.new(
            fields: [
              Google::Cloud::Bigquery::StandardSql::Field.new(
                name: "name",
                type: Google::Cloud::Bigquery::StandardSql::DataType.new(type_kind: "STRING")
              ),
              Google::Cloud::Bigquery::StandardSql::Field.new(
                name: "val",
                type: Google::Cloud::Bigquery::StandardSql::DataType.new(type_kind: "INT64")
              )
            ]
          )
        )
      )
    )
  ]
end

@see cloud.google.com/bigquery/docs/reference/standard-sql/data-types Standard SQL Data Types

Public Class Methods

from_gapi(gapi) click to toggle source

@private New StandardSql::DataType from a Google::Apis::BigqueryV2::StandardSqlDataType object.

# File lib/google/cloud/bigquery/standard_sql.rb, line 405
def self.from_gapi gapi
  new.tap do |f|
    f.instance_variable_set :@gapi, gapi
  end
end
gapi_from_string_or_data_type(data_type) click to toggle source

@private New Google::Apis::BigqueryV2::StandardSqlDataType from a String or StandardSql::DataType object.

# File lib/google/cloud/bigquery/standard_sql.rb, line 413
def self.gapi_from_string_or_data_type data_type
  return if data_type.nil?
  case data_type
  when StandardSql::DataType
    data_type.to_gapi
  when Hash
    data_type
  when String, Symbol
    Google::Apis::BigqueryV2::StandardSqlDataType.new type_kind: data_type.to_s.upcase
  else
    raise ArgumentError, "Unable to convert #{data_type} to Google::Apis::BigqueryV2::StandardSqlDataType"
  end
end
new(**kwargs) click to toggle source

Creates a new, immutable StandardSql::DataType object.

@overload initialize(type_kind, array_element_type, struct_type)

@param [String] type_kind The top level type of this field. Required. Can be [any standard SQL data
  type](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types) (e.g., `INT64`, `DATE`,
  `ARRAY`).
@param [DataType, String] array_element_type The type of the array's elements, if {#type_kind} is `ARRAY`.
  See {#array?}. Optional.
@param [StructType] struct_type The fields of the struct, in order, if {#type_kind} is `STRUCT`. See
  {#struct?}. Optional.
# File lib/google/cloud/bigquery/standard_sql.rb, line 201
def initialize **kwargs
  # Convert client object kwargs to a gapi object
  if kwargs[:array_element_type]
    kwargs[:array_element_type] = self.class.gapi_from_string_or_data_type kwargs[:array_element_type]
  end
  kwargs[:struct_type] = kwargs[:struct_type].to_gapi if kwargs[:struct_type]

  @gapi = Google::Apis::BigqueryV2::StandardSqlDataType.new(**kwargs)
end

Public Instance Methods

array?() click to toggle source

Checks if the {#type_kind} of the field is `ARRAY`.

@return [Boolean] `true` when `ARRAY`, `false` otherwise.

@!group Helpers

# File lib/google/cloud/bigquery/standard_sql.rb, line 382
def array?
  type_kind == "ARRAY".freeze
end
array_element_type() click to toggle source

The type of the array's elements, if {#type_kind} is `ARRAY`. See {#array?}. Optional.

@return [DataType, nil]

# File lib/google/cloud/bigquery/standard_sql.rb, line 228
def array_element_type
  return if @gapi.array_element_type.nil?
  DataType.from_gapi @gapi.array_element_type
end
bignumeric?() click to toggle source

Checks if the {#type_kind} of the field is `BIGNUMERIC`.

@return [Boolean] `true` when `BIGNUMERIC`, `false` otherwise.

@!group Helpers

# File lib/google/cloud/bigquery/standard_sql.rb, line 283
def bignumeric?
  type_kind == "BIGNUMERIC".freeze
end
boolean?() click to toggle source

Checks if the {#type_kind} of the field is `BOOL`.

@return [Boolean] `true` when `BOOL`, `false` otherwise.

@!group Helpers

# File lib/google/cloud/bigquery/standard_sql.rb, line 294
def boolean?
  type_kind == "BOOL".freeze
end
bytes?() click to toggle source

Checks if the {#type_kind} of the field is `BYTES`.

@return [Boolean] `true` when `BYTES`, `false` otherwise.

@!group Helpers

# File lib/google/cloud/bigquery/standard_sql.rb, line 316
def bytes?
  type_kind == "BYTES".freeze
end
date?() click to toggle source

Checks if the {#type_kind} of the field is `DATE`.

@return [Boolean] `true` when `DATE`, `false` otherwise.

@!group Helpers

# File lib/google/cloud/bigquery/standard_sql.rb, line 327
def date?
  type_kind == "DATE".freeze
end
datetime?() click to toggle source

Checks if the {#type_kind} of the field is `DATETIME`.

@return [Boolean] `true` when `DATETIME`, `false` otherwise.

@!group Helpers

# File lib/google/cloud/bigquery/standard_sql.rb, line 338
def datetime?
  type_kind == "DATETIME".freeze
end
float?() click to toggle source

Checks if the {#type_kind} of the field is `FLOAT64`.

@return [Boolean] `true` when `FLOAT64`, `false` otherwise.

@!group Helpers

# File lib/google/cloud/bigquery/standard_sql.rb, line 261
def float?
  type_kind == "FLOAT64".freeze
end
geography?() click to toggle source

Checks if the {#type_kind} of the field is `GEOGRAPHY`.

@return [Boolean] `true` when `GEOGRAPHY`, `false` otherwise.

@!group Helpers

# File lib/google/cloud/bigquery/standard_sql.rb, line 349
def geography?
  type_kind == "GEOGRAPHY".freeze
end
int?() click to toggle source

Checks if the {#type_kind} of the field is `INT64`.

@return [Boolean] `true` when `INT64`, `false` otherwise.

@!group Helpers

# File lib/google/cloud/bigquery/standard_sql.rb, line 250
def int?
  type_kind == "INT64".freeze
end
numeric?() click to toggle source

Checks if the {#type_kind} of the field is `NUMERIC`.

@return [Boolean] `true` when `NUMERIC`, `false` otherwise.

@!group Helpers

# File lib/google/cloud/bigquery/standard_sql.rb, line 272
def numeric?
  type_kind == "NUMERIC".freeze
end
string?() click to toggle source

Checks if the {#type_kind} of the field is `STRING`.

@return [Boolean] `true` when `STRING`, `false` otherwise.

@!group Helpers

# File lib/google/cloud/bigquery/standard_sql.rb, line 305
def string?
  type_kind == "STRING".freeze
end
struct?() click to toggle source

Checks if the {#type_kind} of the field is `STRUCT`.

@return [Boolean] `true` when `STRUCT`, `false` otherwise.

@!group Helpers

# File lib/google/cloud/bigquery/standard_sql.rb, line 393
def struct?
  type_kind == "STRUCT".freeze
end
struct_type() click to toggle source

The fields of the struct, in order, if {#type_kind} is `STRUCT`. See {#struct?}. Optional.

@return [StructType, nil]

# File lib/google/cloud/bigquery/standard_sql.rb, line 238
def struct_type
  return if @gapi.struct_type.nil?
  StructType.from_gapi @gapi.struct_type
end
time?() click to toggle source

Checks if the {#type_kind} of the field is `TIME`.

@return [Boolean] `true` when `TIME`, `false` otherwise.

@!group Helpers

# File lib/google/cloud/bigquery/standard_sql.rb, line 360
def time?
  type_kind == "TIME".freeze
end
timestamp?() click to toggle source

Checks if the {#type_kind} of the field is `TIMESTAMP`.

@return [Boolean] `true` when `TIMESTAMP`, `false` otherwise.

@!group Helpers

# File lib/google/cloud/bigquery/standard_sql.rb, line 371
def timestamp?
  type_kind == "TIMESTAMP".freeze
end
to_gapi() click to toggle source

@private New Google::Apis::BigqueryV2::StandardSqlDataType object.

# File lib/google/cloud/bigquery/standard_sql.rb, line 399
def to_gapi
  @gapi
end
type_kind() click to toggle source

The top level type of this field. Required. Can be any standard SQL data type (e.g., `INT64`, `DATE`, `ARRAY`).

@see cloud.google.com/bigquery/docs/reference/standard-sql/data-types Standard SQL Data Types

@return [String] The upper case type.

# File lib/google/cloud/bigquery/standard_sql.rb, line 219
def type_kind
  @gapi.type_kind
end