Class: ClickhouseRuby::Types::LowCardinality

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

Overview

Type handler for ClickHouse LowCardinality type

LowCardinality is an optimization wrapper that stores string values in a dictionary for better compression and performance.

Examples:

type = LowCardinality.new('LowCardinality', element_type: String.new('String'))
type.cast('hello')
type.serialize('hello') # => "'hello'"

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

Returns a new instance of LowCardinality.

Parameters:

  • name (String)

    the type name

  • element_type (Base) (defaults to: nil)

    the wrapped type



21
22
23
24
# File 'lib/clickhouse_ruby/types/low_cardinality.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 wrapped type.

Returns:

  • (Base)

    the wrapped type



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

def element_type
  @element_type
end

Instance Method Details

#cast(value) ⇒ Object

Converts a Ruby value using the wrapped type

Parameters:

  • value (Object)

    the value to convert

Returns:

  • (Object)

    the converted value



30
31
32
# File 'lib/clickhouse_ruby/types/low_cardinality.rb', line 30

def cast(value)
  @element_type.cast(value)
end

#deserialize(value) ⇒ Object

Converts a value from ClickHouse using the wrapped type

Parameters:

  • value (Object)

    the value from ClickHouse

Returns:

  • (Object)

    the Ruby value



38
39
40
# File 'lib/clickhouse_ruby/types/low_cardinality.rb', line 38

def deserialize(value)
  @element_type.deserialize(value)
end

#serialize(value) ⇒ String

Converts a value to SQL literal using the wrapped type

Parameters:

  • value (Object)

    the value to serialize

Returns:

  • (String)

    the SQL literal



46
47
48
# File 'lib/clickhouse_ruby/types/low_cardinality.rb', line 46

def serialize(value)
  @element_type.serialize(value)
end

#to_sString

Returns the full type string

Returns:

  • (String)

    the type string



53
54
55
# File 'lib/clickhouse_ruby/types/low_cardinality.rb', line 53

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