Class: ClickhouseRuby::Types::Float

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

Overview

Type handler for ClickHouse floating point types

Handles: Float32, Float64

Note: ClickHouse also supports special values: inf, -inf, nan

Instance Attribute Summary

Attributes inherited from Base

#name

Instance Method Summary collapse

Methods inherited from Base

#==, #hash, #initialize, #nullable?, #to_s

Constructor Details

This class inherits a constructor from ClickhouseRuby::Types::Base

Instance Method Details

#cast(value) ⇒ Float?

Converts a Ruby value to a float

Parameters:

  • value (Object)

    the value to convert

Returns:

  • (Float, nil)

    the float value

Raises:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/clickhouse_ruby/types/float.rb', line 17

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

  case value
  when ::Float
    value
  when ::Integer
    value.to_f
  when ::String
    parse_string(value)
  when ::BigDecimal
    value.to_f
  else
    raise TypeCastError.new(
      "Cannot cast #{value.class} to #{name}",
      from_type: value.class.name,
      to_type: name,
      value: value
    )
  end
end

#deserialize(value) ⇒ Float?

Converts a value from ClickHouse to Ruby Float

Parameters:

  • value (Object)

    the value from ClickHouse

Returns:

  • (Float, nil)

    the float value



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/clickhouse_ruby/types/float.rb', line 43

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

  case value
  when ::Float
    value
  when ::String
    parse_string(value)
  else
    value.to_f
  end
end

#serialize(value) ⇒ String

Converts a float to SQL literal

Parameters:

  • value (Float, nil)

    the value to serialize

Returns:

  • (String)

    the SQL literal



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/clickhouse_ruby/types/float.rb', line 60

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

  if value.nan?
    'nan'
  elsif value.infinite? == 1
    'inf'
  elsif value.infinite? == -1
    '-inf'
  else
    value.to_s
  end
end