Class: ClickhouseRuby::Result

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/clickhouse_ruby/result.rb

Overview

Query result container that provides access to columns, types, and rows

Result implements Enumerable for easy iteration over rows. Each row is returned as a Hash with column names as keys and properly deserialized values.

Examples:

Iterating over results

result = client.execute('SELECT id, name FROM users')
result.columns  # => ['id', 'name']
result.types    # => ['UInt64', 'String']

result.each do |row|
  puts "#{row['id']}: #{row['name']}"
end

Array-like access

result.rows[0]       # => { 'id' => 1, 'name' => 'Alice' }
result.first         # => { 'id' => 1, 'name' => 'Alice' }
result.to_a          # => [{ 'id' => 1, 'name' => 'Alice' }, ...]

Metadata access

result.count         # => 100
result.empty?        # => false
result.elapsed_time  # => 0.023 (seconds)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(columns:, types:, data:, statistics: {}, deserialize: true) ⇒ Result

Creates a new Result from parsed response data



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

def initialize(columns:, types:, data:, statistics: {}, deserialize: true)
  @columns = columns.freeze
  @types = types.freeze
  @elapsed_time = statistics["elapsed"]
  @rows_read = statistics["rows_read"]
  @bytes_read = statistics["bytes_read"]

  # Build type instances for deserialization
  @type_instances = (types.map { |t| Types.lookup(t) } if deserialize)

  # Convert raw data to row hashes
  @rows = build_rows(data).freeze
end

Instance Attribute Details

#bytes_readInteger? (readonly)



48
49
50
# File 'lib/clickhouse_ruby/result.rb', line 48

def bytes_read
  @bytes_read
end

#columnsArray<String> (readonly)



33
34
35
# File 'lib/clickhouse_ruby/result.rb', line 33

def columns
  @columns
end

#elapsed_timeFloat? (readonly)



42
43
44
# File 'lib/clickhouse_ruby/result.rb', line 42

def elapsed_time
  @elapsed_time
end

#rowsArray<Hash> (readonly) Also known as: data



39
40
41
# File 'lib/clickhouse_ruby/result.rb', line 39

def rows
  @rows
end

#rows_readInteger? (readonly)



45
46
47
# File 'lib/clickhouse_ruby/result.rb', line 45

def rows_read
  @rows_read
end

#typesArray<String> (readonly)



36
37
38
# File 'lib/clickhouse_ruby/result.rb', line 36

def types
  @types
end

Class Method Details

.emptyResult

Creates an empty result (for commands that don’t return data)



140
141
142
# File 'lib/clickhouse_ruby/result.rb', line 140

def self.empty
  new(columns: [], types: [], data: [])
end

.from_json_compact(response_data) ⇒ Result

Creates a result from JSONCompact format response



148
149
150
151
152
153
154
155
156
# File 'lib/clickhouse_ruby/result.rb', line 148

def self.from_json_compact(response_data)
  meta = response_data["meta"] || []
  columns = meta.map { |m| m["name"] }
  types = meta.map { |m| m["type"] }
  data = response_data["data"] || []
  statistics = response_data["statistics"] || {}

  new(columns: columns, types: types, data: data, statistics: statistics)
end

Instance Method Details

#[](index) ⇒ Hash?

Access a row by index



114
115
116
# File 'lib/clickhouse_ruby/result.rb', line 114

def [](index)
  @rows[index]
end

#column_typesHash<String, String>

Returns column names mapped to their types



133
134
135
# File 'lib/clickhouse_ruby/result.rb', line 133

def column_types
  @columns.zip(@types).to_h
end

#column_values(column_name) ⇒ Array

Returns a specific column’s values across all rows

Raises:

  • (ArgumentError)

    if column doesn’t exist



123
124
125
126
127
128
# File 'lib/clickhouse_ruby/result.rb', line 123

def column_values(column_name)
  index = @columns.index(column_name)
  raise ArgumentError, "Unknown column: #{column_name}" if index.nil?

  @rows.map { |row| row[column_name] }
end

#countInteger Also known as: size, length

Returns the number of rows



82
83
84
# File 'lib/clickhouse_ruby/result.rb', line 82

def count
  @rows.length
end

#each {|Hash| ... } ⇒ Enumerator

Iterates over each row

Yields:

  • (Hash)

    each row as a hash



75
76
77
# File 'lib/clickhouse_ruby/result.rb', line 75

def each(&block)
  @rows.each(&block)
end

#empty?Boolean

Returns whether there are no rows



92
93
94
# File 'lib/clickhouse_ruby/result.rb', line 92

def empty?
  @rows.empty?
end

#firstHash?

Returns the first row



99
100
101
# File 'lib/clickhouse_ruby/result.rb', line 99

def first
  @rows.first
end

#inspectString

Returns a human-readable string representation



161
162
163
# File 'lib/clickhouse_ruby/result.rb', line 161

def inspect
  "#<#{self.class.name} columns=#{@columns.inspect} rows=#{count}>"
end

#lastHash?

Returns the last row



106
107
108
# File 'lib/clickhouse_ruby/result.rb', line 106

def last
  @rows.last
end