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
# 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_cast_error(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

Raises:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/clickhouse_ruby/types/float.rb', line 39

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

  case value
  when ::Float
    value
  when ::String
    parse_string(value)
  when ::Integer, ::BigDecimal, ::Rational
    value.to_f
  else
    raise_cast_error(value, "Cannot deserialize #{value.class} to #{name}")
  end
end

#serialize(value) ⇒ String

Converts a float to SQL literal

Parameters:

  • value (Float, nil)

    the value to serialize

Returns:

  • (String)

    the SQL literal



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

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