Class: Trino::Client::Query
- Inherits:
-
Object
- Object
- Trino::Client::Query
- Defined in:
- lib/trino/client/query.rb
Instance Attribute Summary collapse
-
#scalar_parser ⇒ Object
Returns the value of attribute scalar_parser.
Class Method Summary collapse
- .faraday_client(options) ⇒ Object
- .kill(query_id, options) ⇒ Object
- .resume(next_uri, options) ⇒ Object
- .start(query, options) ⇒ Object
- .transform_row(column_value_parsers, row) ⇒ Object
Instance Method Summary collapse
- #advance ⇒ Object
- #cancel ⇒ Object
- #close ⇒ Object
- #column_value_parsers ⇒ Object
- #columns ⇒ Object
- #current_results ⇒ Object
- #current_results_headers ⇒ Object
- #each_row(&block) ⇒ Object
- #each_row_chunk(&block) ⇒ Object
-
#initialize(api) ⇒ Query
constructor
A new instance of Query.
- #next_uri ⇒ Object
- #query_info ⇒ Object
- #raise_if_failed ⇒ Object
- #rows ⇒ Object
- #transform_row(row) ⇒ Object
- #transform_rows ⇒ Object
Constructor Details
#initialize(api) ⇒ Query
Returns a new instance of Query.
63 64 65 |
# File 'lib/trino/client/query.rb', line 63 def initialize(api) @api = api end |
Instance Attribute Details
#scalar_parser ⇒ Object
Returns the value of attribute scalar_parser.
61 62 63 |
# File 'lib/trino/client/query.rb', line 61 def scalar_parser @scalar_parser end |
Class Method Details
.faraday_client(options) ⇒ Object
44 45 46 |
# File 'lib/trino/client/query.rb', line 44 def self.faraday_client() Trino::Client.faraday_client() end |
.kill(query_id, options) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/trino/client/query.rb', line 36 def self.kill(query_id, ) faraday = faraday_client() response = faraday.delete do |req| req.url "/v1/query/#{query_id}" end return response.status / 100 == 2 end |
.resume(next_uri, options) ⇒ Object
32 33 34 |
# File 'lib/trino/client/query.rb', line 32 def self.resume(next_uri, ) new StatementClient.new(faraday_client(), nil, , next_uri) end |
.start(query, options) ⇒ Object
28 29 30 |
# File 'lib/trino/client/query.rb', line 28 def self.start(query, ) new StatementClient.new(faraday_client(), query, ) end |
.transform_row(column_value_parsers, row) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/trino/client/query.rb', line 48 def self.transform_row(column_value_parsers, row) row_object = {} row.each_with_index do |element, i| column = column_value_parsers[i] value = column.value(element) row_object[column.name] = value end row_object end |
Instance Method Details
#advance ⇒ Object
75 76 77 |
# File 'lib/trino/client/query.rb', line 75 def advance @api.advance end |
#cancel ⇒ Object
157 158 159 |
# File 'lib/trino/client/query.rb', line 157 def cancel @api.cancel_leaf_stage end |
#close ⇒ Object
161 162 163 164 |
# File 'lib/trino/client/query.rb', line 161 def close @api.close nil end |
#column_value_parsers ⇒ Object
105 106 107 108 109 110 |
# File 'lib/trino/client/query.rb', line 105 def column_value_parsers @column_value_parsers ||= {} @column_value_parsers[scalar_parser] ||= columns.map {|column| ColumnValueParser.new(column, scalar_parser) } end |
#columns ⇒ Object
99 100 101 102 103 |
# File 'lib/trino/client/query.rb', line 99 def columns wait_for_columns return @api.current_results.columns end |
#current_results ⇒ Object
67 68 69 |
# File 'lib/trino/client/query.rb', line 67 def current_results @api.current_results end |
#current_results_headers ⇒ Object
71 72 73 |
# File 'lib/trino/client/query.rb', line 71 def current_results_headers @api.current_results_headers end |
#each_row(&block) ⇒ Object
128 129 130 131 132 |
# File 'lib/trino/client/query.rb', line 128 def each_row(&block) each_row_chunk {|chunk| chunk.each(&block) } end |
#each_row_chunk(&block) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/trino/client/query.rb', line 134 def each_row_chunk(&block) wait_for_data if self.columns == nil raise TrinoError, "Query #{@api.current_results.id} has no columns" end loop do if data = @api.current_results.data block.call(data) end break unless advance_and_raise end end |
#next_uri ⇒ Object
153 154 155 |
# File 'lib/trino/client/query.rb', line 153 def next_uri @api.current_results.next_uri end |
#query_info ⇒ Object
149 150 151 |
# File 'lib/trino/client/query.rb', line 149 def query_info @api.query_info end |
#raise_if_failed ⇒ Object
166 167 168 169 170 171 172 173 174 |
# File 'lib/trino/client/query.rb', line 166 def raise_if_failed if @api.client_aborted? raise TrinoClientError, "Query aborted by user" elsif @api.query_failed? results = @api.current_results error = results.error raise TrinoQueryError.new("Query #{results.id} failed: #{error.}", results.id, error.error_code, error.error_name, error.error_type, error.error_location, error.failure_info) end end |
#rows ⇒ Object
120 121 122 123 124 125 126 |
# File 'lib/trino/client/query.rb', line 120 def rows rows = [] each_row_chunk {|chunk| rows.concat(chunk) } return rows end |
#transform_row(row) ⇒ Object
116 117 118 |
# File 'lib/trino/client/query.rb', line 116 def transform_row(row) self.class.transform_row(column_value_parsers, row) end |
#transform_rows ⇒ Object
112 113 114 |
# File 'lib/trino/client/query.rb', line 112 def transform_rows rows.map { |row| transform_row(row) } end |