Class: ClickhouseRuby::Result
- Inherits:
-
Object
- Object
- ClickhouseRuby::Result
- 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.
Instance Attribute Summary collapse
-
#bytes_read ⇒ Integer?
readonly
Bytes read by ClickHouse.
-
#columns ⇒ Array<String>
readonly
Column names in order.
-
#elapsed_time ⇒ Float?
readonly
Query execution time in seconds (from ClickHouse).
-
#rows ⇒ Array<Hash>
(also: #data)
readonly
Rows as hashes with column names as keys.
-
#rows_read ⇒ Integer?
readonly
Number of rows read by ClickHouse.
-
#types ⇒ Array<String>
readonly
ClickHouse type strings for each column.
Class Method Summary collapse
-
.empty ⇒ Result
Creates an empty result (for commands that don’t return data).
-
.from_json_compact(response_data) ⇒ Result
Creates a result from JSONCompact format response.
Instance Method Summary collapse
-
#[](index) ⇒ Hash?
Access a row by index.
-
#column_types ⇒ Hash<String, String>
Returns column names mapped to their types.
-
#column_values(column_name) ⇒ Array
Returns a specific column’s values across all rows.
-
#count ⇒ Integer
(also: #size, #length)
Returns the number of rows.
-
#each {|Hash| ... } ⇒ Enumerator
Iterates over each row.
-
#empty? ⇒ Boolean
Returns whether there are no rows.
-
#first ⇒ Hash?
Returns the first row.
-
#initialize(columns:, types:, data:, statistics: {}, deserialize: true) ⇒ Result
constructor
Creates a new Result from parsed response data.
-
#inspect ⇒ String
Returns a human-readable string representation.
-
#last ⇒ Hash?
Returns the last row.
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_read ⇒ Integer? (readonly)
48 49 50 |
# File 'lib/clickhouse_ruby/result.rb', line 48 def bytes_read @bytes_read end |
#columns ⇒ Array<String> (readonly)
33 34 35 |
# File 'lib/clickhouse_ruby/result.rb', line 33 def columns @columns end |
#elapsed_time ⇒ Float? (readonly)
42 43 44 |
# File 'lib/clickhouse_ruby/result.rb', line 42 def elapsed_time @elapsed_time end |
#rows ⇒ Array<Hash> (readonly) Also known as: data
39 40 41 |
# File 'lib/clickhouse_ruby/result.rb', line 39 def rows @rows end |
#rows_read ⇒ Integer? (readonly)
45 46 47 |
# File 'lib/clickhouse_ruby/result.rb', line 45 def rows_read @rows_read end |
#types ⇒ Array<String> (readonly)
36 37 38 |
# File 'lib/clickhouse_ruby/result.rb', line 36 def types @types end |
Class Method Details
.empty ⇒ Result
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) = response_data["meta"] || [] columns = .map { |m| m["name"] } types = .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_types ⇒ Hash<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
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 |
#count ⇒ Integer 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
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 |
#first ⇒ Hash?
Returns the first row
99 100 101 |
# File 'lib/clickhouse_ruby/result.rb', line 99 def first @rows.first end |
#inspect ⇒ String
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 |
#last ⇒ Hash?
Returns the last row
106 107 108 |
# File 'lib/clickhouse_ruby/result.rb', line 106 def last @rows.last end |