Class: Blazer::Adapters::ClickhouseAdapter

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

Instance Attribute Summary

Attributes inherited from BaseAdapter

#data_source

Instance Method Summary collapse

Methods inherited from BaseAdapter

#cachable?, #cohort_analysis_statement, #cost, #explain, #initialize, #reconnect, #supports_cohort_analysis?

Constructor Details

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

Instance Method Details

#cancel(run_id) ⇒ Object



85
86
87
# File 'lib/blazer/adapters/clickhouse_adapter.rb', line 85

def cancel(run_id)
  execute("KILL QUERY WHERE query LIKE {query: String} AND query NOT LIKE '%system.processes%' SYNC", {"query" => "%,run_id:#{run_id}%"})
end

#parameter_bindingObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/blazer/adapters/clickhouse_adapter.rb', line 60

def parameter_binding
  proc do |statement, variables|
    variables.each do |k, v|
      if v.nil?
        statement = statement.gsub("{#{k}}") { "NULL" }
      else
        type =
          case v
          when Integer
            # TODO improve
            "Int64"
          when Float
            "Float64"
          when ActiveSupport::TimeWithZone
            "DateTime64(9, 'UTC')"
          else
            "String"
          end
        statement = statement.gsub("{#{k}}") { "{#{k}: #{type}}" }
      end
    end
    [statement, variables]
  end
end

#preview_statementObject



51
52
53
# File 'lib/blazer/adapters/clickhouse_adapter.rb', line 51

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

#quotingObject



56
57
58
# File 'lib/blazer/adapters/clickhouse_adapter.rb', line 56

def quoting
  :backslash_escape
end

#run_statement(statement, comment, bind_params) ⇒ 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
30
31
32
33
34
35
36
37
38
39
# File 'lib/blazer/adapters/clickhouse_adapter.rb', line 4

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

  begin
    res = execute("#{statement} /*#{comment}*/", bind_params)

    if res.is_a?(Net::HTTPSuccess)
      data = JSON.parse(res.body)
      columns = data["meta"].map { |v| v["name"] }
      rows = data["data"]
      data["meta"].each_with_index do |c, i|
        type = c["type"]
        type = type[9..-2] if type.start_with?("Nullable(")
        if type.start_with?("DateTime")
          utc = ActiveSupport::TimeZone["Etc/UTC"]
          rows.each do |row|
            row[i] &&= utc.parse(row[i])
          end
        elsif ["Date", "Date32"].include?(type)
          rows.each do |row|
            row[i] &&= Date.parse(row[i])
          end
        end
      end
    else
      error = res.body
      error = Blazer::TIMEOUT_MESSAGE if error.include?("TIMEOUT_EXCEEDED")
    end
  rescue Errno::ECONNREFUSED => e
    error = e.message
  end

  [columns, rows, error]
end

#schemaObject



46
47
48
49
# File 'lib/blazer/adapters/clickhouse_adapter.rb', line 46

def schema
  result = data_source.run_statement("SELECT table_schema, table_name, column_name, data_type, ordinal_position FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = currentDatabase() ORDER BY 1, 2")
  result.rows.group_by { |r| [r[0], r[1]] }.map { |k, vs| {schema: k[0], table: k[1], columns: vs.sort_by { |v| v[2] }.map { |v| {name: v[2], data_type: v[3]} }} }
end

#tablesObject



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

def tables
  result = data_source.run_statement("SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = currentDatabase() ORDER BY table_name")
  result.rows.map(&:first)
end