Class: ClickhouseRuby::Types::Tuple

Inherits:
Base
  • Object
show all
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.

Examples:

type = Tuple.new('Tuple', arg_types: [String.new('String'), Integer.new('UInt64')])
type.cast(['hello', 42])
type.serialize(['hello', 42]) # => "('hello', 42)"

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) ⇒ Tuple

Returns a new instance of Tuple.

Parameters:

  • name (String)

    the type name

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

    the element types



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

#element_typesArray<Base> (readonly)

Returns the types of each tuple element.

Returns:

  • (Array<Base>)

    the types of each tuple element



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

def element_types
  @element_types
end

Instance Method Details

#cast(value) ⇒ Array?

Converts a Ruby value to a tuple (Array)

Parameters:

  • value (Object)

    the value to convert

Returns:

  • (Array, nil)

    the tuple value

Raises:



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

Parameters:

  • value (Object)

    the value from ClickHouse

Returns:

  • (Array, nil)

    the tuple value



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

Parameters:

  • value (Array, nil)

    the value to serialize

Returns:

  • (String)

    the 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_sString

Returns the full type string including element types

Returns:

  • (String)

    the type string



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