Class: UmbrellioUtils::ClickHouse::Backends::Native

Inherits:
Base
  • Object
show all
Defined in:
lib/umbrellio_utils/click_house/backends/native.rb

Overview

Adapter for the clickhouse-native gem (TCP driver).

Intentional differences from the HTTP-era module:

- Values returned by query / query_value are real Ruby types
  (Time, Integer, etc.), not JSON-stringified.
- The `host:` kwarg on execute / query / query_value is accepted
  for source compatibility but ignored — hostname is bound at
  Pool construction, not per query.

Constant Summary collapse

SERVER_ERROR =
::ClickhouseNative::ServerError
UNKNOWN_TABLE =

Server-side error codes that mean “object doesn’t exist”. Used by describe_table callers that want to tolerate eager-load against a database that hasn’t been created yet (e.g. rake ch:create).

60
UNKNOWN_DATABASE =
81

Instance Method Summary collapse

Methods inherited from Base

#count, #create_database, #drop_database, #drop_table!, #from, #on_cluster, #optimize_table!, #parse_value, #pg_table_connection, #populate_temp_table!, #truncate_table!, #with_temp_table

Instance Method Details

#admin_execute(sql) ⇒ Object

DDL that creates/drops the configured database can’t run through the main pool (which is bound to that database). Open a one-shot client connected to the always-present “default” db instead.



90
91
92
93
94
95
# File 'lib/umbrellio_utils/click_house/backends/native.rb', line 90

def admin_execute(sql)
  admin = ::ClickhouseNative::Client.new(**client_options(database: "default"))
  admin.execute(sql)
ensure
  admin&.close
end

#configObject



63
64
65
# File 'lib/umbrellio_utils/click_house/backends/native.rb', line 63

def config
  ::ClickHouse.config
end

#db_nameObject

Read through pool so test mocks of ‘pool` also redirect `db_name`.



68
69
70
# File 'lib/umbrellio_utils/click_house/backends/native.rb', line 68

def db_name
  pool.database.to_sym
end

#describe_table(table_name, db_name: self.db_name) ⇒ Object



51
52
53
# File 'lib/umbrellio_utils/click_house/backends/native.rb', line 51

def describe_table(table_name, db_name: self.db_name)
  pool.describe_table(normalize_identifier(table_name), db_name: db_name.to_s)
end

#execute(sql, host: nil, **_opts) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



26
27
28
29
# File 'lib/umbrellio_utils/click_house/backends/native.rb', line 26

def execute(sql, host: nil, **_opts) # rubocop:disable Lint/UnusedMethodArgument
  sql_string = sql.is_a?(String) ? sql : sql.sql
  log_errors(sql_string) { pool.execute(sql_string) }
end

#insert(table_name, db_name: self.db_name, rows: []) ⇒ Object



46
47
48
49
# File 'lib/umbrellio_utils/click_house/backends/native.rb', line 46

def insert(table_name, db_name: self.db_name, rows: [])
  return if rows.empty?
  pool.insert(normalize_identifier(table_name), rows, db_name: db_name.to_s)
end

#loggerObject



72
73
74
75
76
# File 'lib/umbrellio_utils/click_house/backends/native.rb', line 72

def logger
  @logger ||= UmbrellioUtils.config.clickhouse_native_logger ||
              (defined?(Rails) && Rails.logger) ||
              Logger.new($stdout)
end

#poolObject



78
79
80
81
82
83
84
85
# File 'lib/umbrellio_utils/click_house/backends/native.rb', line 78

def pool
  @pool ||= ::ClickhouseNative::Pool.new(
    **client_options(database: (config[:database] || "default").to_s),
    pool_size: Integer(config[:pool_size] || 5),
    pool_timeout: Integer(config[:pool_timeout] || 10),
    settings: UmbrellioUtils.config.clickhouse_native_settings || {},
  )
end

#query(dataset, host: nil, **_opts) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



31
32
33
34
# File 'lib/umbrellio_utils/click_house/backends/native.rb', line 31

def query(dataset, host: nil, **_opts) # rubocop:disable Lint/UnusedMethodArgument
  sql = sql_for(dataset)
  log_errors(sql) { pool.query(sql) }
end

#query_each(dataset, host: nil, **_opts) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



41
42
43
44
# File 'lib/umbrellio_utils/click_house/backends/native.rb', line 41

def query_each(dataset, host: nil, **_opts, &) # rubocop:disable Lint/UnusedMethodArgument
  sql = sql_for(dataset)
  log_errors(sql) { pool.query_each(sql, &) }
end

#query_value(dataset, host: nil, **_opts) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



36
37
38
39
# File 'lib/umbrellio_utils/click_house/backends/native.rb', line 36

def query_value(dataset, host: nil, **_opts) # rubocop:disable Lint/UnusedMethodArgument
  sql = sql_for(dataset)
  log_errors(sql) { pool.query_value(sql) }
end

#server_versionObject



55
56
57
# File 'lib/umbrellio_utils/click_house/backends/native.rb', line 55

def server_version
  pool.with(&:server_version).to_f
end

#tablesObject



59
60
61
# File 'lib/umbrellio_utils/click_house/backends/native.rb', line 59

def tables
  pool.query("SHOW TABLES").pluck(:name)
end