Module: UmbrellioUtils::ClickHouse

Extended by:
ClickHouse
Includes:
Memery
Included in:
ClickHouse
Defined in:
lib/umbrellio_utils/click_house.rb

Instance Method Summary collapse

Instance Method Details

#count(dataset) ⇒ Object



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

def count(dataset)
  query_value(dataset.select(SQL.ch_count))
end

#db_nameObject



75
76
77
# File 'lib/umbrellio_utils/click_house.rb', line 75

def db_name
  client.config.database.to_sym
end

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



67
68
69
70
71
72
73
# File 'lib/umbrellio_utils/click_house.rb', line 67

def describe_table(table_name, db_name: self.db_name)
  sql = "DESCRIBE TABLE #{full_table_name(table_name, db_name)} FORMAT JSON"

  log_errors(sql) do
    select_all(sql).map { |x| Misc::StrictHash[x.symbolize_keys] }
  end
end

#drop_table!(table_name, db_name: self.db_name) ⇒ Object



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

def drop_table!(table_name, db_name: self.db_name)
  execute("DROP TABLE #{db_name}.#{table_name} ON CLUSTER click_cluster SYNC")
end

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



29
30
31
32
33
# File 'lib/umbrellio_utils/click_house.rb', line 29

def execute(sql, host: nil, **opts)
  log_errors(sql) do
    client(host).execute(sql, params: opts)
  end
end

#from(source, db_name: self.db_name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/umbrellio_utils/click_house.rb', line 15

def from(source, db_name: self.db_name)
  ds =
    case source
    when Symbol
      DB.from(db_name == self.db_name ? SQL[source] : SQL[db_name][source])
    when nil
      DB.dataset
    else
      DB.from(source)
    end

  ds.clone(ch: true)
end

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



11
12
13
# File 'lib/umbrellio_utils/click_house.rb', line 11

def insert(table_name, db_name: self.db_name, rows: [])
  client.insert(full_table_name(table_name, db_name), rows, format: "JSONEachRow")
end

#optimize_table!(table_name, db_name: self.db_name) ⇒ Object



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

def optimize_table!(table_name, db_name: self.db_name)
  execute("OPTIMIZE TABLE #{db_name}.#{table_name} ON CLUSTER click_cluster FINAL")
end

#parse_value(value, type:) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/umbrellio_utils/click_house.rb', line 79

def parse_value(value, type:)
  case type
  when /String/
    value&.to_s
  when /DateTime/
    Time.zone.parse(value) if value
  else
    value
  end
end

#pg_table_connection(table) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/umbrellio_utils/click_house.rb', line 94

def pg_table_connection(table)
  host = ENV["PGHOST"] || DB.opts[:host].presence || "localhost"
  port = DB.opts[:port] || 5432
  database = DB.opts[:database]
  username = DB.opts[:user]
  password = DB.opts[:password]

  Sequel.function(:postgresql, "#{host}:#{port}", database, table, username, password)
end

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



35
36
37
38
39
40
41
# File 'lib/umbrellio_utils/click_house.rb', line 35

def query(dataset, host: nil, **opts)
  sql = sql_for(dataset)

  log_errors(sql) do
    select_all(sql, host:, **opts).map { |x| Misc::StrictHash[x.symbolize_keys] }
  end
end

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



43
44
45
46
47
48
49
# File 'lib/umbrellio_utils/click_house.rb', line 43

def query_value(dataset, host: nil, **opts)
  sql = sql_for(dataset)

  log_errors(sql) do
    select_value(sql, host:, **opts)
  end
end

#server_versionObject



90
91
92
# File 'lib/umbrellio_utils/click_house.rb', line 90

def server_version
  select_value("SELECT version()").to_f
end

#truncate_table!(table_name, db_name: self.db_name) ⇒ Object



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

def truncate_table!(table_name, db_name: self.db_name)
  execute("TRUNCATE TABLE #{db_name}.#{table_name} ON CLUSTER click_cluster SYNC")
end

#with_temp_table(dataset, temp_table_name:, primary_key: [:id], primary_key_types: [:integer], **opts) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/umbrellio_utils/click_house.rb', line 104

def with_temp_table(
  dataset, temp_table_name:, primary_key: [:id], primary_key_types: [:integer], **opts, &
)
  unless DB.table_exists?(temp_table_name)
    UmbrellioUtils::Database.create_temp_table(
      nil, primary_key:, primary_key_types:, temp_table_name:, &
    )
    populate_temp_table!(temp_table_name, dataset)
  end
  UmbrellioUtils::Database.with_temp_table(nil, primary_key:, temp_table_name:, **opts, &)
end