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
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 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
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
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_s ⇒ String
Returns the full type string including element type
84 85 86 |
# File 'lib/clickhouse_ruby/types/array.rb', line 84 def to_s "Array(#{@element_type})" end |