Class: ClickhouseRuby::Types::Tuple
- Defined in:
- lib/clickhouse_ruby/types/tuple.rb
Overview
Type handler for ClickHouse Tuple type
Tuples are fixed-size collections where each position has its own type. Similar to Ruby arrays but heterogeneous and fixed-size.
Instance Attribute Summary collapse
-
#element_types ⇒ Array<Base>
readonly
The types of each tuple element.
Attributes inherited from Base
Instance Method Summary collapse
-
#cast(value) ⇒ Array?
Converts a Ruby value to a tuple (Array).
-
#deserialize(value) ⇒ Array?
Converts a value from ClickHouse to a Ruby Array.
-
#initialize(name, arg_types: nil) ⇒ Tuple
constructor
A new instance of Tuple.
-
#serialize(value) ⇒ String
Converts a tuple to SQL literal.
-
#to_s ⇒ String
Returns the full type string including element types.
Methods inherited from Base
Constructor Details
#initialize(name, arg_types: nil) ⇒ Tuple
Returns a new instance of Tuple.
21 22 23 24 |
# File 'lib/clickhouse_ruby/types/tuple.rb', line 21 def initialize(name, arg_types: nil) super(name) @element_types = arg_types || [] end |
Instance Attribute Details
Instance Method Details
#cast(value) ⇒ Array?
Converts a Ruby value to a tuple (Array)
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/clickhouse_ruby/types/tuple.rb', line 31 def cast(value) return nil if value.nil? arr = case value when ::Array value when ::String parse_tuple_string(value) else raise_cast_error(value, "Cannot cast #{value.class} to Tuple") end cast_elements(arr) end |
#deserialize(value) ⇒ Array?
Converts a value from ClickHouse to a Ruby Array
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/clickhouse_ruby/types/tuple.rb', line 50 def deserialize(value) return nil if value.nil? arr = case value when ::Array value when ::String parse_tuple_string(value) else [value] end deserialize_elements(arr) end |
#serialize(value) ⇒ String
Converts a tuple to SQL literal
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/clickhouse_ruby/types/tuple.rb', line 69 def serialize(value) return "NULL" if value.nil? elements = value.each_with_index.map do |v, i| type = @element_types[i] || Base.new("String") type.serialize(v) end "(#{elements.join(", ")})" end |
#to_s ⇒ String
Returns the full type string including element types
83 84 85 86 |
# File 'lib/clickhouse_ruby/types/tuple.rb', line 83 def to_s type_strs = @element_types.map(&:to_s).join(", ") "Tuple(#{type_strs})" end |