clickhouse-native 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/clickhouse_native/client.rb +19 -1
- data/lib/clickhouse_native/pool.rb +33 -12
- data/lib/clickhouse_native/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b77997a63f9cfed092a0c26eccaeb63e34aad3072d52aff9f20f0bfaeaaeffa7
|
|
4
|
+
data.tar.gz: ebdf9129e7e6bea78bc12dfe0b04246a14d7a03024bf49feb67dc43f3bf6435b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: db47e0695db8a4e7d448372fafb0aaa3c6483909d787cf970d6cd5386199df779c68247eb638f6a83ea3e18e12f2a45c505121351459abe65ce9b08ce01f0653
|
|
7
|
+
data.tar.gz: f2c7c53106b186021a587e2b6a85991500cc322886b32392b6a32cc5da35e043914da6c43009804df8571496ce65f3e4e46c8497baf0b85ba2ecaaf4bde6ab9a
|
|
@@ -16,6 +16,10 @@ module ClickhouseNative
|
|
|
16
16
|
# columns in DDL order (for Array<Array>).
|
|
17
17
|
# types may be supplied to skip the DESCRIBE lookup.
|
|
18
18
|
#
|
|
19
|
+
# The schema lookup is memoized per `(table, db_name)` on the client, so
|
|
20
|
+
# repeated inserts into the same table don't re-run DESCRIBE. Call
|
|
21
|
+
# `clear_schema_cache` after `ALTER TABLE` to invalidate.
|
|
22
|
+
#
|
|
19
23
|
# Hash keys not present in the schema raise ArgumentError — if you need
|
|
20
24
|
# to insert a subset, pass `columns:` explicitly.
|
|
21
25
|
def insert(table, rows, columns: nil, db_name: nil, types: nil)
|
|
@@ -33,6 +37,14 @@ module ClickhouseNative
|
|
|
33
37
|
insert_block(fq, col_pairs, row_arrays)
|
|
34
38
|
end
|
|
35
39
|
|
|
40
|
+
# Drop the memoized schema for a table (or all tables). Needed after
|
|
41
|
+
# DDL that changes column set or types, since insert-time schema
|
|
42
|
+
# lookups are cached per (table, db_name).
|
|
43
|
+
def clear_schema_cache(table = nil, db_name: nil)
|
|
44
|
+
return unless defined?(@schema_cache) && @schema_cache
|
|
45
|
+
table.nil? ? @schema_cache.clear : @schema_cache.delete([db_name, table.to_s])
|
|
46
|
+
end
|
|
47
|
+
|
|
36
48
|
def inspect
|
|
37
49
|
"#<#{self.class} #{host}:#{port}/#{database}>"
|
|
38
50
|
end
|
|
@@ -47,7 +59,7 @@ module ClickhouseNative
|
|
|
47
59
|
end
|
|
48
60
|
|
|
49
61
|
def columns_from_schema(table, rows, columns, db_name, fqn)
|
|
50
|
-
schema =
|
|
62
|
+
schema = cached_schema(table, db_name: db_name)
|
|
51
63
|
type_by_name = schema.to_h { |c| [c[:name], c[:type]] }
|
|
52
64
|
columns ||= rows.first.is_a?(Hash) ? rows.first.keys.map(&:to_s) : schema.map { |c| c[:name] }
|
|
53
65
|
columns.map do |name|
|
|
@@ -58,6 +70,12 @@ module ClickhouseNative
|
|
|
58
70
|
end
|
|
59
71
|
end
|
|
60
72
|
|
|
73
|
+
def cached_schema(table, db_name:)
|
|
74
|
+
@schema_cache ||= {}
|
|
75
|
+
@schema_cache[[db_name, table.to_s]] ||=
|
|
76
|
+
describe_table(table, db_name: db_name).freeze
|
|
77
|
+
end
|
|
78
|
+
|
|
61
79
|
def hash_rows_to_arrays(rows, col_pairs)
|
|
62
80
|
lookup = col_pairs.map { |n, _| [n.to_sym, n] }
|
|
63
81
|
rows.map { |h| lookup.map { |sym, str| h.fetch(sym) { h[str] } } }
|
|
@@ -4,6 +4,8 @@ require "connection_pool"
|
|
|
4
4
|
|
|
5
5
|
module ClickhouseNative
|
|
6
6
|
class Pool
|
|
7
|
+
STALE_IVAR = :@clickhouse_native_settings_stale
|
|
8
|
+
|
|
7
9
|
attr_reader :host, :port, :database
|
|
8
10
|
|
|
9
11
|
def initialize(host:, port:, database: "default", user: "default", password: "",
|
|
@@ -13,52 +15,71 @@ module ClickhouseNative
|
|
|
13
15
|
@port = port
|
|
14
16
|
@database = database
|
|
15
17
|
client_kwargs = { host:, port:, database:, user:, password:, compression:, logger: }
|
|
16
|
-
set_sql = settings_sql(settings)
|
|
18
|
+
@set_sql = settings_sql(settings)
|
|
17
19
|
@pool = ConnectionPool.new(size: pool_size, timeout: pool_timeout) do
|
|
18
20
|
client = Client.new(**client_kwargs)
|
|
19
|
-
client.execute(set_sql) if set_sql
|
|
21
|
+
client.execute(@set_sql) if @set_sql
|
|
20
22
|
client
|
|
21
23
|
end
|
|
22
24
|
end
|
|
23
25
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
+
# Yields a client with the pool's session settings applied. If the
|
|
27
|
+
# previous checkout raised (which, in this gem's C++ bindings, always
|
|
28
|
+
# triggers a ResetConnection that wipes the session), re-apply the SET
|
|
29
|
+
# before yielding. Exceptions re-raise after marking the client stale.
|
|
30
|
+
def with
|
|
31
|
+
@pool.with do |client|
|
|
32
|
+
reapply_settings_if_stale(client)
|
|
33
|
+
begin
|
|
34
|
+
yield client
|
|
35
|
+
rescue
|
|
36
|
+
client.instance_variable_set(STALE_IVAR, true)
|
|
37
|
+
raise
|
|
38
|
+
end
|
|
39
|
+
end
|
|
26
40
|
end
|
|
27
41
|
|
|
28
42
|
def execute(sql)
|
|
29
|
-
|
|
43
|
+
with { |c| c.execute(sql) }
|
|
30
44
|
end
|
|
31
45
|
|
|
32
46
|
def query(sql)
|
|
33
|
-
|
|
47
|
+
with { |c| c.query(sql) }
|
|
34
48
|
end
|
|
35
49
|
|
|
36
50
|
def query_each(sql, &block)
|
|
37
|
-
|
|
51
|
+
with { |c| c.query_each(sql, &block) }
|
|
38
52
|
end
|
|
39
53
|
|
|
40
54
|
def query_value(sql)
|
|
41
|
-
|
|
55
|
+
with { |c| c.query_value(sql) }
|
|
42
56
|
end
|
|
43
57
|
|
|
44
58
|
def insert(table, rows, **opts)
|
|
45
|
-
|
|
59
|
+
with { |c| c.insert(table, rows, **opts) }
|
|
46
60
|
end
|
|
47
61
|
|
|
48
62
|
def ping
|
|
49
|
-
|
|
63
|
+
with(&:ping)
|
|
50
64
|
end
|
|
51
65
|
|
|
52
66
|
def server_version
|
|
53
|
-
|
|
67
|
+
with(&:server_version)
|
|
54
68
|
end
|
|
55
69
|
|
|
56
70
|
def describe_table(table, db_name: nil)
|
|
57
|
-
|
|
71
|
+
with { |c| c.describe_table(table, db_name:) }
|
|
58
72
|
end
|
|
59
73
|
|
|
60
74
|
private
|
|
61
75
|
|
|
76
|
+
def reapply_settings_if_stale(client)
|
|
77
|
+
return unless @set_sql
|
|
78
|
+
return unless client.instance_variable_get(STALE_IVAR)
|
|
79
|
+
client.execute(@set_sql)
|
|
80
|
+
client.instance_variable_set(STALE_IVAR, false)
|
|
81
|
+
end
|
|
82
|
+
|
|
62
83
|
# Render a `SET key1 = val1, key2 = val2` statement once at pool setup
|
|
63
84
|
# so every checked-out connection starts with the same session
|
|
64
85
|
# settings. Matches how the HTTP driver injected global_params per
|