clickhouse-native 0.3.1 → 0.4.1
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
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7bd4c34d5b234b7e69e31760baf997bf26b11ed5409564ff7c8aeb900a9799b7
|
|
4
|
+
data.tar.gz: 96cc5dd2feea27635c14f90e05818ceddcfa2cf76dfdab1cb77c7b9332b9ef59
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 39abed1faa63eadd6a1f56f2be45f943cf3037fc01c4e9f235863e64e2dc4eea643731be9428ae872b2be72412262ca151d9e73bde1104ae1d3c21aa80212f5c
|
|
7
|
+
data.tar.gz: 7090e04f9b60c7b07b46c57e9117df800f5e94c21ab7b630de4ba4e58fb11822fe4a40a722c2133796a29bbd1107e7d1ab80ab9df0a5f5ae552d2258babc7f0f
|
|
@@ -101,7 +101,22 @@ static int64_t pow10_i64(size_t n) {
|
|
|
101
101
|
return r;
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
-
static VALUE value_at(const ColumnRef& col, size_t idx) {
|
|
104
|
+
static VALUE value_at(const ColumnRef& col, size_t idx, const std::string& declared_type = {}) {
|
|
105
|
+
// clickhouse-cpp normalises Bool -> UInt8 at column-construction time,
|
|
106
|
+
// so at the codec layer we see UInt8 and can't tell Bool apart without
|
|
107
|
+
// the declared type string that came over the wire. The patched vendor
|
|
108
|
+
// tree preserves it on Block; we consume it here for top-level columns.
|
|
109
|
+
// Nested occurrences (Array(Bool), Map(_, Bool)) are not handled — the
|
|
110
|
+
// declared type for nested children is not threaded through recursion.
|
|
111
|
+
if (declared_type == "Bool") {
|
|
112
|
+
return col->As<ColumnUInt8>()->At(idx) ? Qtrue : Qfalse;
|
|
113
|
+
}
|
|
114
|
+
if (declared_type == "Nullable(Bool)") {
|
|
115
|
+
auto n = col->As<ColumnNullable>();
|
|
116
|
+
if (n->IsNull(idx)) return Qnil;
|
|
117
|
+
return n->Nested()->As<ColumnUInt8>()->At(idx) ? Qtrue : Qfalse;
|
|
118
|
+
}
|
|
119
|
+
|
|
105
120
|
auto type = col->Type();
|
|
106
121
|
switch (type->GetCode()) {
|
|
107
122
|
case Type::Int8: return INT2NUM(col->As<ColumnInt8>()->At(idx));
|
|
@@ -736,7 +751,8 @@ static VALUE ch_client_query(VALUE self, VALUE rb_sql) {
|
|
|
736
751
|
for (size_t r = 0; r < nrows; r++) {
|
|
737
752
|
VALUE h = rb_hash_new();
|
|
738
753
|
for (size_t cc = 0; cc < ncols; cc++) {
|
|
739
|
-
rb_hash_aset(h, ID2SYM(col_ids[cc]),
|
|
754
|
+
rb_hash_aset(h, ID2SYM(col_ids[cc]),
|
|
755
|
+
value_at(block[cc], r, block.GetColumnType(cc)));
|
|
740
756
|
}
|
|
741
757
|
rb_ary_push(rows, h);
|
|
742
758
|
}
|
|
@@ -765,7 +781,7 @@ static VALUE ch_client_query_value(VALUE self, VALUE rb_sql) {
|
|
|
765
781
|
c->client->Select(sql, [&](const Block& block) {
|
|
766
782
|
if (seen) return;
|
|
767
783
|
if (block.GetRowCount() == 0 || block.GetColumnCount() == 0) return;
|
|
768
|
-
out = value_at(block[0], 0);
|
|
784
|
+
out = value_at(block[0], 0, block.GetColumnType(0));
|
|
769
785
|
seen = true;
|
|
770
786
|
});
|
|
771
787
|
return out;
|
|
@@ -914,7 +930,8 @@ static VALUE yield_rows_body(VALUE arg) {
|
|
|
914
930
|
for (size_t r = 0; r < nrows; r++) {
|
|
915
931
|
VALUE h = rb_hash_new();
|
|
916
932
|
for (size_t cc = 0; cc < ncols; cc++) {
|
|
917
|
-
rb_hash_aset(h, ID2SYM(state->col_ids[cc]),
|
|
933
|
+
rb_hash_aset(h, ID2SYM(state->col_ids[cc]),
|
|
934
|
+
value_at(block[cc], r, block.GetColumnType(cc)));
|
|
918
935
|
}
|
|
919
936
|
rb_funcall(state->user_proc, rb_intern("call"), 1, h);
|
|
920
937
|
}
|
|
@@ -35,6 +35,24 @@ unless File.exist?(File.join(VENDOR, "CMakeLists.txt"))
|
|
|
35
35
|
MSG
|
|
36
36
|
end
|
|
37
37
|
|
|
38
|
+
# Apply in-tree patches to the vendored clickhouse-cpp before CMake
|
|
39
|
+
# configures/builds it. Patches are idempotent: skip any that already
|
|
40
|
+
# apply in reverse (meaning they've been applied previously).
|
|
41
|
+
PATCHES_DIR = File.expand_path("patches", EXT_DIR)
|
|
42
|
+
if Dir.exist?(PATCHES_DIR)
|
|
43
|
+
Dir[File.join(PATCHES_DIR, "*.patch")].each do |patch|
|
|
44
|
+
# -R --check: does the patch apply in reverse? If yes, already applied.
|
|
45
|
+
already_applied = system(
|
|
46
|
+
"patch", "-R", "--dry-run", "-p1", "-s", "-f",
|
|
47
|
+
"-d", VENDOR, "-i", patch,
|
|
48
|
+
out: File::NULL, err: File::NULL
|
|
49
|
+
)
|
|
50
|
+
next if already_applied
|
|
51
|
+
system("patch", "-p1", "-s", "-f", "-d", VENDOR, "-i", patch) or
|
|
52
|
+
fatal("failed to apply patch: #{patch}")
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
38
56
|
unless find_executable("cmake")
|
|
39
57
|
fatal <<~MSG
|
|
40
58
|
CMake is required to build the vendored clickhouse-cpp library
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
diff --git a/clickhouse/block.cpp b/clickhouse/block.cpp
|
|
2
|
+
index 519cfa4..15334c5 100644
|
|
3
|
+
--- a/clickhouse/block.cpp
|
|
4
|
+
+++ b/clickhouse/block.cpp
|
|
5
|
+
@@ -53,13 +53,17 @@ Block::Block(size_t cols, size_t rows)
|
|
6
|
+
Block::~Block() = default;
|
|
7
|
+
|
|
8
|
+
void Block::AppendColumn(const std::string& name, const ColumnRef& col) {
|
|
9
|
+
+ AppendColumn(name, col, std::string());
|
|
10
|
+
+}
|
|
11
|
+
+
|
|
12
|
+
+void Block::AppendColumn(const std::string& name, const ColumnRef& col, const std::string& type) {
|
|
13
|
+
if (columns_.empty()) {
|
|
14
|
+
rows_ = col->Size();
|
|
15
|
+
} else if (col->Size() != rows_) {
|
|
16
|
+
throw ValidationError("all columns in block must have same count of rows. Name: ["+name+"], rows: ["+std::to_string(rows_)+"], columns: [" + std::to_string(col->Size())+"]");
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
- columns_.push_back(ColumnItem{name, col});
|
|
20
|
+
+ columns_.push_back(ColumnItem{name, col, type});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/// Count of columns in the block.
|
|
24
|
+
diff --git a/clickhouse/block.h b/clickhouse/block.h
|
|
25
|
+
index a32cd02..d2263cb 100644
|
|
26
|
+
--- a/clickhouse/block.h
|
|
27
|
+
+++ b/clickhouse/block.h
|
|
28
|
+
@@ -67,6 +67,11 @@ public:
|
|
29
|
+
|
|
30
|
+
/// Append named column to the block.
|
|
31
|
+
void AppendColumn(const std::string& name, const ColumnRef& col);
|
|
32
|
+
+ /// Same, but preserves the declared wire type string (e.g. "Bool",
|
|
33
|
+
+ /// "Nullable(Bool)") alongside the column. clickhouse-cpp normalises
|
|
34
|
+
+ /// some types at column construction (Bool -> UInt8, etc.), so keep the
|
|
35
|
+
+ /// original for callers who need to recover the true declared type.
|
|
36
|
+
+ void AppendColumn(const std::string& name, const ColumnRef& col, const std::string& type);
|
|
37
|
+
|
|
38
|
+
/// Count of columns in the block.
|
|
39
|
+
size_t GetColumnCount() const;
|
|
40
|
+
@@ -85,6 +90,13 @@ public:
|
|
41
|
+
return columns_.at(idx).name;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
+ /// Declared wire type string for the column at `idx` (e.g. "Bool",
|
|
45
|
+
+ /// "Nullable(Bool)"). Empty for columns appended via the two-arg
|
|
46
|
+
+ /// AppendColumn overload.
|
|
47
|
+
+ const std::string& GetColumnType(size_t idx) const {
|
|
48
|
+
+ return columns_.at(idx).type;
|
|
49
|
+
+ }
|
|
50
|
+
+
|
|
51
|
+
/// Convenience method to wipe out all rows from all columns
|
|
52
|
+
void Clear();
|
|
53
|
+
|
|
54
|
+
@@ -103,6 +115,7 @@ private:
|
|
55
|
+
struct ColumnItem {
|
|
56
|
+
std::string name;
|
|
57
|
+
ColumnRef column;
|
|
58
|
+
+ std::string type;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
BlockInfo info_;
|
|
62
|
+
diff --git a/clickhouse/client.cpp b/clickhouse/client.cpp
|
|
63
|
+
index a8c8ea6..d81f636 100644
|
|
64
|
+
--- a/clickhouse/client.cpp
|
|
65
|
+
+++ b/clickhouse/client.cpp
|
|
66
|
+
@@ -784,7 +784,7 @@ bool Client::Impl::ReadBlock(InputStream& input, Block* block) {
|
|
67
|
+
throw ProtocolError("can't load column '" + name + "' of type " + type);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
- block->AppendColumn(name, col);
|
|
71
|
+
+ block->AppendColumn(name, col, type);
|
|
72
|
+
} else {
|
|
73
|
+
throw UnimplementedError(std::string("unsupported column type: ") + type);
|
|
74
|
+
}
|
|
@@ -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
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: clickhouse-native
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.4.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Yuri Smirnov
|
|
@@ -34,6 +34,7 @@ extra_rdoc_files: []
|
|
|
34
34
|
files:
|
|
35
35
|
- ext/clickhouse_native/client.cpp
|
|
36
36
|
- ext/clickhouse_native/extconf.rb
|
|
37
|
+
- ext/clickhouse_native/patches/0001-preserve-declared-column-type.patch
|
|
37
38
|
- ext/clickhouse_native/vendor/clickhouse-cpp/.clang-format
|
|
38
39
|
- ext/clickhouse_native/vendor/clickhouse-cpp/.git
|
|
39
40
|
- ext/clickhouse_native/vendor/clickhouse-cpp/.gitattributes
|