Module: TinyPresto
- Defined in:
- lib/tiny-presto.rb,
lib/tiny-presto/cluster.rb,
lib/tiny-presto/version.rb
Defined Under Namespace
Classes: Cluster, TinyPresto
Constant Summary collapse
- VERSION =
'0.0.10'.freeze
Class Method Summary collapse
-
.ensure_stop ⇒ Object
Make sure to stop the cluster.
- .prepare(table_name, table_data) ⇒ Object
- .print_record(record) ⇒ Object
-
.run(sql) ⇒ Object
Run the given SQL.
-
.run_with_retry(sql, max_retry = 3) ⇒ Object
Run the given query with retrying in case of undeterministic error.
-
.verify(sql, expected_result) ⇒ Object
Run the given SQL and verify the result.
Class Method Details
.ensure_stop ⇒ Object
Make sure to stop the cluster.
TinyPresto.ensure_stop
116 117 118 119 |
# File 'lib/tiny-presto.rb', line 116 def self.ensure_stop presto = TinyPresto.instance presto.stop end |
.prepare(table_name, table_data) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/tiny-presto.rb', line 56 def self.prepare(table_name, table_data) # {'c1': [1, 2, 3], 'c2': ['a', 'b', 'c']} columns = table_data.keys records = [] table_data.each do |_, rows| rows.each_with_index do |r, idx| if records[idx].nil? records << [r] else records[idx] << r end end end values_clause = [] records.each do |record| values_clause << print_record(record) end query = "CREATE TABLE #{table_name} AS SELECT * FROM (values #{values_clause.join(',')}) t(#{columns.join(',')})" run_with_retry(query) end |
.print_record(record) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/tiny-presto.rb', line 44 def self.print_record(record) ret = record.map do |v| if v.is_a? Numeric v.to_s else # Non numeric value is interpreted as string "'#{v}'" end end "(#{ret.join(',')})" end |
.run(sql) ⇒ Object
Run the given SQL.
TinyPresto.run("show schemas")
81 82 83 84 85 |
# File 'lib/tiny-presto.rb', line 81 def self.run(sql) presto = TinyPresto.instance _, rows = presto.client.run(sql) rows end |
.run_with_retry(sql, max_retry = 3) ⇒ Object
Run the given query with retrying in case of undeterministic error.
TinyPresto.run_with_retry("show schemas")
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/tiny-presto.rb', line 91 def self.run_with_retry(sql, max_retry = 3) max_retry.times do return run(sql) rescue Trino::Client::TrinoQueryError => e # Cluster may be in the initialization phase. raise unless e..match?(/^No nodes available to run query/) sleep(1000) next end end |
.verify(sql, expected_result) ⇒ Object
Run the given SQL and verify the result.
TinyPresto.verify("show schemas", [["default"], ["information_schema"]])
# => return true
107 108 109 110 |
# File 'lib/tiny-presto.rb', line 107 def self.verify(sql, expected_result) rows = run_with_retry(sql) rows == expected_result end |