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

RETRYABLE_ERRORS =
[
  /No nodes available to run query/
]
VERSION =
'0.0.6'.freeze

Class Method Summary collapse

Class Method Details

.ensure_stopObject

Make sure to stop the cluster.

TinyPresto.ensure_stop


110
111
112
113
# File 'lib/tiny-presto.rb', line 110

def self.ensure_stop
  presto = TinyPresto.instance
  presto.stop
end

.prepare(table_name, table_data) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/tiny-presto.rb', line 46

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


34
35
36
37
38
39
40
41
42
43
44
# File 'lib/tiny-presto.rb', line 34

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")


71
72
73
74
75
# File 'lib/tiny-presto.rb', line 71

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")


84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/tiny-presto.rb', line 84

def self.run_with_retry(sql, max_retry = 3)
  max_retry.times do
    return run(sql)
  rescue Presto::Client::PrestoQueryError => e
    # Cluster may be in the initialization phase.
    if RETRYABLE_ERRORS.any? { |err| e.message =~ err }
      sleep(1)
      next
    end
    raise
  end
end

.verify(sql, expected_result) ⇒ Object

Run the given SQL and verify the result.

TinyPresto.verify("show schemas", [["default"], ["information_schema"]])
# => return true


101
102
103
104
# File 'lib/tiny-presto.rb', line 101

def self.verify(sql, expected_result)
  rows = run_with_retry(sql)
  rows == expected_result
end