Class: ClickhouseRuby::Types::Integer

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

Overview

Type handler for ClickHouse integer types

Handles: Int8, Int16, Int32, Int64, Int128, Int256

UInt8, UInt16, UInt32, UInt64, UInt128, UInt256

ClickHouse integers are exact - no floating point issues. Large integers (128, 256 bit) use Ruby’s BigInteger.

Constant Summary collapse

LIMITS =

Size limits for each integer type

{
  'Int8' => { min: -128, max: 127 },
  'Int16' => { min: -32_768, max: 32_767 },
  'Int32' => { min: -2_147_483_648, max: 2_147_483_647 },
  'Int64' => { min: -9_223_372_036_854_775_808, max: 9_223_372_036_854_775_807 },
  'Int128' => { min: -(2**127), max: (2**127) - 1 },
  'Int256' => { min: -(2**255), max: (2**255) - 1 },
  'UInt8' => { min: 0, max: 255 },
  'UInt16' => { min: 0, max: 65_535 },
  'UInt32' => { min: 0, max: 4_294_967_295 },
  'UInt64' => { min: 0, max: 18_446_744_073_709_551_615 },
  'UInt128' => { min: 0, max: (2**128) - 1 },
  'UInt256' => { min: 0, max: (2**256) - 1 }
}.freeze

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

#bit_sizeInteger

Returns the bit size of this integer type

Returns:

  • (Integer)

    the bit size (8, 16, 32, 64, 128, or 256)



103
104
105
# File 'lib/clickhouse_ruby/types/integer.rb', line 103

def bit_size
  name.gsub(/[^0-9]/, '').to_i
end

#cast(value) ⇒ Integer?

Converts a Ruby value to an integer

Parameters:

  • value (Object)

    the value to convert

Returns:

  • (Integer, nil)

    the integer value

Raises:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/clickhouse_ruby/types/integer.rb', line 35

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

  case value
  when ::Integer
    validate_range!(value)
    value
  when ::Float
    int_value = value.to_i
    validate_range!(int_value)
    int_value
  when ::String
    int_value = parse_string(value)
    validate_range!(int_value)
    int_value
  when true
    1
  when false
    0
  else
    raise TypeCastError.new(
      "Cannot cast #{value.class} to #{name}",
      from_type: value.class.name,
      to_type: name,
      value: value
    )
  end
end

#deserialize(value) ⇒ Integer?

Converts a value from ClickHouse to Ruby Integer

Parameters:

  • value (Object)

    the value from ClickHouse

Returns:

  • (Integer, nil)

    the integer value



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/clickhouse_ruby/types/integer.rb', line 68

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

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

#serialize(value) ⇒ String

Converts an integer to SQL literal

Parameters:

  • value (Integer, nil)

    the value to serialize

Returns:

  • (String)

    the SQL literal



87
88
89
90
91
# File 'lib/clickhouse_ruby/types/integer.rb', line 87

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

  value.to_s
end

#unsigned?Boolean

Returns whether this is an unsigned integer type

Returns:



96
97
98
# File 'lib/clickhouse_ruby/types/integer.rb', line 96

def unsigned?
  name.start_with?('U')
end