Class: ClickhouseRuby::Types::Map

Inherits:
Base
  • Object
show all
Defined in:
lib/clickhouse_ruby/types/map.rb

Overview

Type handler for ClickHouse Map type

Maps are key-value collections where all keys share one type and all values share another type.

Examples:

type = Map.new('Map', arg_types: [String.new('String'), Integer.new('UInt64')])
type.cast({'a' => 1, 'b' => 2})
type.serialize({'a' => 1}) # => "{'a': 1}"

Instance Attribute Summary collapse

Attributes inherited from Base

#name

Instance Method Summary collapse

Methods inherited from Base

#==, #hash, #nullable?

Constructor Details

#initialize(name, arg_types: nil) ⇒ Map

Returns a new instance of Map.

Parameters:

  • name (String)

    the type name

  • arg_types (Array<Base>) (defaults to: nil)

    array of [key_type, value_type]



24
25
26
27
28
29
# File 'lib/clickhouse_ruby/types/map.rb', line 24

def initialize(name, arg_types: nil)
  super(name)
  arg_types ||= [Base.new("String"), Base.new("String")]
  @key_type = arg_types[0]
  @value_type = arg_types[1] || Base.new("String")
end

Instance Attribute Details

#key_typeBase (readonly)

Returns the key type.

Returns:

  • (Base)

    the key type



17
18
19
# File 'lib/clickhouse_ruby/types/map.rb', line 17

def key_type
  @key_type
end

#value_typeBase (readonly)

Returns the value type.

Returns:

  • (Base)

    the value type



20
21
22
# File 'lib/clickhouse_ruby/types/map.rb', line 20

def value_type
  @value_type
end

Instance Method Details

#cast(value) ⇒ Hash?

Converts a Ruby value to a map (Hash)

Parameters:

  • value (Object)

    the value to convert

Returns:

  • (Hash, nil)

    the hash value

Raises:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/clickhouse_ruby/types/map.rb', line 36

def cast(value)
  return nil if value.nil?

  hash = case value
         when ::Hash
           value
         when ::String
           parse_map_string(value)
         else
           raise_cast_error(value, "Cannot cast #{value.class} to Map")
         end

  hash.transform_keys { |k| @key_type.cast(k) }
    .transform_values { |v| @value_type.cast(v) }
end

#deserialize(value) ⇒ Hash?

Converts a value from ClickHouse to a Ruby Hash

Parameters:

  • value (Object)

    the value from ClickHouse

Returns:

  • (Hash, nil)

    the hash value



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/clickhouse_ruby/types/map.rb', line 56

def deserialize(value)
  return nil if value.nil?

  hash = case value
         when ::Hash
           value
         when ::String
           parse_map_string(value)
         else
           { value => nil }
         end

  hash.transform_keys { |k| @key_type.deserialize(k) }
    .transform_values { |v| @value_type.deserialize(v) }
end

#serialize(value) ⇒ String

Converts a hash to SQL literal

Parameters:

  • value (Hash, nil)

    the value to serialize

Returns:

  • (String)

    the SQL literal



76
77
78
79
80
81
82
83
84
# File 'lib/clickhouse_ruby/types/map.rb', line 76

def serialize(value)
  return "NULL" if value.nil?

  pairs = value.map do |k, v|
    "#{@key_type.serialize(k)}: #{@value_type.serialize(v)}"
  end

  "{#{pairs.join(", ")}}"
end

#to_sString

Returns the full type string including key and value types

Returns:

  • (String)

    the type string



89
90
91
# File 'lib/clickhouse_ruby/types/map.rb', line 89

def to_s
  "Map(#{@key_type}, #{@value_type})"
end