Class: ClickhouseRuby::Types::Array
- 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)).
Instance Attribute Summary collapse
-
#element_type ⇒ Base
readonly
The type of array elements.
Attributes inherited from Base
Instance Method Summary collapse
-
#cast(value) ⇒ Array?
Converts a Ruby value to an array.
-
#deserialize(value) ⇒ Array?
Converts a value from ClickHouse to a Ruby Array.
-
#initialize(name, element_type: nil) ⇒ Array
constructor
A new instance of Array.
-
#serialize(value) ⇒ String
Converts an array to SQL literal.
-
#to_s ⇒ String
Returns the full type string including element type.
Methods inherited from Base
Constructor Details
#initialize(name, element_type: nil) ⇒ Array
Returns a new instance of Array.
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_type ⇒ Base (readonly)
Returns 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
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# 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_cast_error(value, "Cannot cast #{value.class} to Array") end arr.map { |v| @element_type.cast(v) } 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/array.rb', line 50 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
69 70 71 72 73 74 |
# File 'lib/clickhouse_ruby/types/array.rb', line 69 def serialize(value) return "NULL" if value.nil? elements = value.map { |v| @element_type.serialize(v) } "[#{elements.join(", ")}]" end |
#to_s ⇒ String
Returns the full type string including element type
79 80 81 |
# File 'lib/clickhouse_ruby/types/array.rb', line 79 def to_s "Array(#{@element_type})" end |