Class: Blazer::Adapters::OpensearchAdapter

Inherits:
BaseAdapter
  • Object
show all
Defined in:
lib/blazer/adapters/opensearch_adapter.rb

Instance Attribute Summary

Attributes inherited from BaseAdapter

#data_source

Instance Method Summary collapse

Methods inherited from BaseAdapter

#cachable?, #cancel, #cohort_analysis_statement, #cost, #explain, #initialize, #parameter_binding, #reconnect, #schema, #supports_cohort_analysis?

Constructor Details

This class inherits a constructor from Blazer::Adapters::BaseAdapter

Instance Method Details

#preview_statementObject



37
38
39
# File 'lib/blazer/adapters/opensearch_adapter.rb', line 37

def preview_statement
  "SELECT * FROM `{table}` LIMIT 10"
end

#quotingObject



41
42
43
# File 'lib/blazer/adapters/opensearch_adapter.rb', line 41

def quoting
  # unknown
end

#run_statement(statement, comment) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/blazer/adapters/opensearch_adapter.rb', line 4

def run_statement(statement, comment)
  columns = []
  rows = []
  error = nil

  begin
    response = client.transport.perform_request("POST", "_plugins/_sql", {}, {query: "#{statement} /*#{comment}*/"}).body
    columns = response["schema"].map { |v| v["name"] }
    # TODO typecast more types
    # https://github.com/opensearch-project/sql/blob/main/docs/user/general/datatypes.rst
    date_indexes = response["schema"].each_index.select { |i| response["schema"][i]["type"] == "timestamp" }
    if columns.any?
      rows = response["datarows"]
      utc = ActiveSupport::TimeZone["Etc/UTC"]
      date_indexes.each do |i|
        rows.each do |row|
          row[i] &&= utc.parse(row[i])
        end
      end
    end
  rescue => e
    error = e.message
  end

  [columns, rows, error]
end

#tablesObject



31
32
33
34
35
# File 'lib/blazer/adapters/opensearch_adapter.rb', line 31

def tables
  indices = client.cat.indices(format: "json").map { |v| v["index"] }
  aliases = client.cat.aliases(format: "json").map { |v| v["alias"] }
  (indices + aliases).uniq.sort
end