Class: ClickhouseRuby::Types::Parser

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

Overview

AST-based parser for ClickHouse type strings

This parser correctly handles nested types like:

  • Array(Tuple(String, UInt64))

  • Map(String, Array(Nullable(Int32)))

  • Nullable(LowCardinality(String))

The parser does NOT validate types - it only handles syntax. Type validation is delegated to ClickHouse.

Grammar:

type := simple_type | parameterized_type
parameterized_type := identifier "(" type_list ")"
type_list := type ("," type)*
simple_type := identifier
identifier := [a-zA-Z_][a-zA-Z0-9_]*

Examples:

parser = Parser.new
parser.parse('String')
# => { type: 'String' }

parser.parse('Array(UInt64)')
# => { type: 'Array', args: [{ type: 'UInt64' }] }

parser.parse('Map(String, Array(Tuple(String, UInt64)))')
# => { type: 'Map', args: [
#      { type: 'String' },
#      { type: 'Array', args: [
#        { type: 'Tuple', args: [
#          { type: 'String' },
#          { type: 'UInt64' }
#        ]}
#      ]}
#    ]}

Defined Under Namespace

Classes: ParseError

Instance Method Summary collapse

Instance Method Details

#parse(type_string) ⇒ Hash

Parses a ClickHouse type string into an AST

Parameters:

  • type_string (String)

    the type string to parse

Returns:

  • (Hash)

    the parsed AST with :type and optional :args keys

Raises:



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/clickhouse_ruby/types/parser.rb', line 60

def parse(type_string)
  raise ParseError.new('Type string cannot be nil') if type_string.nil?

  @input = type_string.strip
  @pos = 0

  raise ParseError.new('Type string cannot be empty', input: type_string) if @input.empty?

  result = parse_type
  skip_whitespace

  # Ensure we consumed the entire input
  unless @pos >= @input.length
    raise ParseError.new("Unexpected character '#{@input[@pos]}'", position: @pos, input: type_string)
  end

  result
end