Class: ClickhouseRuby::Types::Map
- 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.
Instance Attribute Summary collapse
-
#key_type ⇒ Base
readonly
The key type.
-
#value_type ⇒ Base
readonly
The value type.
Attributes inherited from Base
Instance Method Summary collapse
-
#cast(value) ⇒ Hash?
Converts a Ruby value to a map (Hash).
-
#deserialize(value) ⇒ Hash?
Converts a value from ClickHouse to a Ruby Hash.
-
#initialize(name, arg_types: nil) ⇒ Map
constructor
A new instance of Map.
-
#serialize(value) ⇒ String
Converts a hash to SQL literal.
-
#to_s ⇒ String
Returns the full type string including key and value types.
Methods inherited from Base
Constructor Details
#initialize(name, arg_types: nil) ⇒ Map
Returns a new instance of Map.
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_type ⇒ Base (readonly)
Returns the key type.
17 18 19 |
# File 'lib/clickhouse_ruby/types/map.rb', line 17 def key_type @key_type end |
#value_type ⇒ Base (readonly)
Returns 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)
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
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
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_s ⇒ String
Returns the full type string including key and value types
89 90 91 |
# File 'lib/clickhouse_ruby/types/map.rb', line 89 def to_s "Map(#{@key_type}, #{@value_type})" end |