Class: Trino::Client::ColumnValueParser

Inherits:
Object
  • Object
show all
Defined in:
lib/trino/client/column_value_parser.rb

Constant Summary collapse

INSIDE_MATCHING_PARENS_REGEX =
/\((?>[^)(]+|\g<0>)*\)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column, scalar_parser = nil) ⇒ ColumnValueParser

Returns a new instance of ColumnValueParser.



7
8
9
10
11
# File 'lib/trino/client/column_value_parser.rb', line 7

def initialize(column, scalar_parser = nil)
  @name = column.name
  @type = prepare_type_for_parsing(column.type)
  @scalar_parser = scalar_parser
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/trino/client/column_value_parser.rb', line 5

def name
  @name
end

#scalar_parserObject (readonly)

Returns the value of attribute scalar_parser.



5
6
7
# File 'lib/trino/client/column_value_parser.rb', line 5

def scalar_parser
  @scalar_parser
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/trino/client/column_value_parser.rb', line 5

def type
  @type
end

Instance Method Details

#value(data, dtype = type) ⇒ Object

Public: Parse the value of a row’s field by using its column’s Trino type. Trino types can be scalars like VARCHAR and TIMESTAMP or complex types like ARRAY and ROW. ROW types are treated as objects. An ARRAY column’s type is an array of types as you’d expect. A ROW column’s type is a comma-separated list of space-separated (name, type) tuples.

data - The value of a row’s field. Can be a string, number, an array of those,

or an arrays of arrays, etc.

dtype - The Trino type string of the column. See above explanation.

Returns:

  • The given value for strings and numbers

  • A Time for timestamps

  • A Hash of { field1 => value1, field2 => value2, …etc } for row types

  • An array of the above for array types



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/trino/client/column_value_parser.rb', line 28

def value(data, dtype = type)
  # Convert Trino ARRAY elements into Ruby Arrays
  if starts_with?(dtype, 'array(')
    return parse_array_element(data, dtype)

  # Convert Trino ROW elements into Ruby Hashes
  elsif starts_with?(dtype, 'row(')
    return parse_row_element(data, dtype)

  # If defined, use scalar_parser to convert scalar types
  elsif !scalar_parser.nil?
    return scalar_parser.call(data, dtype)
  end

  # Otherwise, values are returned unaltered
  data
end