Class: ClickhouseRuby::Types::Array

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

Overview

Type handler for ClickHouse Array type

Arrays in ClickHouse are homogeneous - all elements must be the same type. Supports nested arrays like Array(Array(String)).

Examples:

type = Array.new('Array', element_type: String.new('String'))
type.cast(['a', 'b', 'c'])  # => ['a', 'b', 'c']
type.serialize(['a', 'b']) # => "['a', 'b']"

Instance Attribute Summary collapse

Attributes inherited from Base

#name

Instance Method Summary collapse

Methods inherited from Base

#==, #hash, #nullable?

Constructor Details

#initialize(name, element_type: nil) ⇒ Array

Returns a new instance of Array.

Parameters:

  • name (String)

    the type name

  • element_type (Base) (defaults to: nil)

    the element type



21
22
23
24
# File 'lib/clickhouse_ruby/types/array.rb', line 21

def initialize(name, element_type: nil)
  super(name)
  @element_type = element_type || Base.new('String')
end

Instance Attribute Details

#element_typeBase (readonly)

Returns the type of array elements.

Returns:

  • (Base)

    the type of array elements



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

def element_type
  @element_type
end

Instance Method Details

#cast(value) ⇒ Array?

Converts a Ruby value to an array

Parameters:

  • value (Object)

    the value to convert

Returns:

  • (Array, nil)

    the array value

Raises:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/clickhouse_ruby/types/array.rb', line 31

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

  arr = case value
        when ::Array
          value
        when ::String
          parse_array_string(value)
        else
          raise TypeCastError.new(
            "Cannot cast #{value.class} to Array",
            from_type: value.class.name,
            to_type: to_s,
            value: value
          )
        end

  arr.map { |v| @element_type.cast(v) }
end

#deserialize(value) ⇒ Array?

Converts a value from ClickHouse to a Ruby Array

Parameters:

  • value (Object)

    the value from ClickHouse

Returns:

  • (Array, nil)

    the array value



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

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

  arr = case value
        when ::Array
          value
        when ::String
          parse_array_string(value)
        else
          [value]
        end

  arr.map { |v| @element_type.deserialize(v) }
end

#serialize(value) ⇒ String

Converts an array to SQL literal

Parameters:

  • value (Array, nil)

    the value to serialize

Returns:

  • (String)

    the SQL literal



74
75
76
77
78
79
# File 'lib/clickhouse_ruby/types/array.rb', line 74

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

  elements = value.map { |v| @element_type.serialize(v) }
  "[#{elements.join(', ')}]"
end

#to_sString

Returns the full type string including element type

Returns:

  • (String)

    the type string



84
85
86
# File 'lib/clickhouse_ruby/types/array.rb', line 84

def to_s
  "Array(#{@element_type})"
end