Class: ClickhouseRuby::Types::Registry

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

Overview

Registry for ClickHouse type mappings

Manages the mapping between ClickHouse type strings and Ruby type classes. Supports both simple types (String, UInt64) and parameterized types (Array, Map, Nullable).

Examples:

Register a custom type

registry = Registry.new
registry.register('MyType', MyTypeClass)

Look up a type

type = registry.lookup('Array(String)')

Constant Summary collapse

WRAPPER_TYPES =

Types that wrap other types (parameterized types)

%w[Array Nullable LowCardinality].freeze
MULTI_ARG_TYPES =

Types that take multiple type arguments

%w[Map Tuple].freeze
DATETIME_TYPES =

DateTime types that take precision/timezone parameters

%w[DateTime DateTime64].freeze

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



28
29
30
31
# File 'lib/clickhouse_ruby/types/registry.rb', line 28

def initialize
  @types = {}
  @cache = {}
end

Instance Method Details

#lookup(type_string) ⇒ Base

Looks up a type by its ClickHouse type string

Parameters:

  • type_string (String)

    the full type string (e.g., ‘Array(String)’)

Returns:

  • (Base)

    the type instance



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/clickhouse_ruby/types/registry.rb', line 46

def lookup(type_string)
  # Check cache first
  return @cache[type_string] if @cache.key?(type_string)

  # Parse the type string
  ast = Parser.new.parse(type_string)

  # Build the type instance
  type = build_type(ast)

  # Cache for future lookups
  @cache[type_string] = type

  type
end

#register(name, type_class) ⇒ Object

Registers a type class for a type name

Parameters:

  • name (String)

    the type name (e.g., ‘String’, ‘UInt64’)

  • type_class (Class)

    the type class



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

def register(name, type_class)
  @types[name] = type_class
  @cache.clear  # Invalidate cache when types change
end

#register_defaultsObject

Registers all default ClickHouse types



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/clickhouse_ruby/types/registry.rb', line 63

def register_defaults
  # Integer types
  register('Int8', Integer)
  register('Int16', Integer)
  register('Int32', Integer)
  register('Int64', Integer)
  register('Int128', Integer)
  register('Int256', Integer)
  register('UInt8', Integer)
  register('UInt16', Integer)
  register('UInt32', Integer)
  register('UInt64', Integer)
  register('UInt128', Integer)
  register('UInt256', Integer)

  # Float types
  register('Float32', Float)
  register('Float64', Float)

  # String types
  register('String', String)
  register('FixedString', String)

  # Date/Time types
  register('Date', DateTime)
  register('Date32', DateTime)
  register('DateTime', DateTime)
  register('DateTime64', DateTime)

  # Other basic types
  register('UUID', UUID)
  register('Bool', Boolean)

  # Complex/wrapper types
  register('Array', Array)
  register('Map', Map)
  register('Tuple', Tuple)
  register('Nullable', Nullable)
  register('LowCardinality', LowCardinality)
end