clickhouse-native 0.6.0 → 0.8.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c2ae7d2e92132e0d22bb51146348282c7009d4db404faf7c980a22bde81c6e34
4
- data.tar.gz: e3075a98f28385d5a7b47fa909f3d3290124ec7299c0fafa9126aabb1e50ea91
3
+ metadata.gz: d1bf3fdd8bd132c0d19937f50055f56b5ab55f4bf383fafc63a74131ff435545
4
+ data.tar.gz: 4e6f233a2e250ee5c2c6c1e46ebf9a961416b69553aee7c2018afea619c1abab
5
5
  SHA512:
6
- metadata.gz: 3297b9411533624dc51a944f3f7b1af442c57887da09d8eccd9659e13c946493746d1b4116dff9636b4a19e480754d1b73a932e2b65960a49ea92f646e2c0d9f
7
- data.tar.gz: ac467f35eb0fc86e92b1630ed671305d410926513025ab44b0e66eae8050283df0a28c768ed91700027a7c42418c3063bdde9fbdfa8b70d62e2f58e02a7c45e4
6
+ metadata.gz: a79cba4fd6ad33b75ec2eecadf87ec0e9fc163ff46874b2ad687f6d767405f7357fffe04a1af584a6716d0c1dcc3ba7171b201a46b130c327d87e3b5e04c52ad
7
+ data.tar.gz: 7ed472036327f17efceda43eac9f41cf7e6675e267711de5f974b2b75de88fcc5e557e7290ef1b0f00b084573fc4a8a5a88e49875d0b4fb83237302262b418f4
@@ -296,6 +296,20 @@ static VALUE value_at(const ColumnRef& col, size_t idx, const std::string& decla
296
296
 
297
297
  static void append_value(const ColumnRef& col, VALUE value);
298
298
 
299
+ // rb_hash_foreach callback: appends each key/value pair into the columns
300
+ // passed via the context pointer. Used by the Map encoder.
301
+ struct MapInsertCtx {
302
+ ColumnRef key_col;
303
+ ColumnRef val_col;
304
+ };
305
+
306
+ static int append_map_pair(VALUE key, VALUE val, VALUE arg) {
307
+ auto* ctx = reinterpret_cast<MapInsertCtx*>(arg);
308
+ append_value(ctx->key_col, key);
309
+ append_value(ctx->val_col, val);
310
+ return ST_CONTINUE;
311
+ }
312
+
299
313
  // Append a zero/default value; used for the nested column of Nullable when
300
314
  // the flag is set to null. We never expose these bytes to the caller.
301
315
  static void append_default(const ColumnRef& col) {
@@ -539,8 +553,8 @@ static void append_value(const ColumnRef& col, VALUE value) {
539
553
 
540
554
  case Type::LowCardinality: {
541
555
  // Only LowCardinality(String) and LowCardinality(Nullable(String))
542
- // are supported for insert in v1 this covers the profile-service
543
- // use case. Numeric LC dictionaries are rare and can wait.
556
+ // are supported for insert. Numeric LC dictionaries are rare and
557
+ // can wait.
544
558
  auto nested_type = type->As<LowCardinalityType>()->GetNestedType();
545
559
  bool nullable = nested_type->GetCode() == Type::Nullable;
546
560
  auto inner_type = nullable
@@ -576,6 +590,43 @@ static void append_value(const ColumnRef& col, VALUE value) {
576
590
  return;
577
591
  }
578
592
 
593
+ case Type::Map: {
594
+ // CH's wire format for Map(K, V) is Array(Tuple(K, V)). We build
595
+ // a one-row Map column holding all of this row's pairs and
596
+ // Append it to the target — ColumnMap exposes only Append(map),
597
+ // its underlying ColumnArray is private. Nil is treated as an
598
+ // empty map.
599
+ if (NIL_P(value)) value = rb_hash_new();
600
+ Check_Type(value, T_HASH);
601
+ auto map_type = type->As<MapType>();
602
+ auto key_type = map_type->GetKeyType();
603
+ auto val_type = map_type->GetValueType();
604
+
605
+ auto key_col = CreateColumnByType(key_type->GetName());
606
+ auto val_col = CreateColumnByType(val_type->GetName());
607
+ if (!key_col || !val_col) {
608
+ throw chn::EncoderFailure(
609
+ "cannot create columns for Map(" + key_type->GetName() +
610
+ ", " + val_type->GetName() + ")");
611
+ }
612
+
613
+ MapInsertCtx ctx{key_col, val_col};
614
+ rb_hash_foreach(value, append_map_pair, reinterpret_cast<VALUE>(&ctx));
615
+
616
+ auto src_tuple = std::make_shared<ColumnTuple>(
617
+ std::vector<ColumnRef>{key_col, val_col});
618
+
619
+ auto seed_tuple = std::make_shared<ColumnTuple>(std::vector<ColumnRef>{
620
+ CreateColumnByType(key_type->GetName()),
621
+ CreateColumnByType(val_type->GetName()),
622
+ });
623
+ auto seed_array = std::make_shared<ColumnArray>(seed_tuple);
624
+ seed_array->AppendAsColumn(src_tuple);
625
+
626
+ col->As<ColumnMap>()->Append(std::make_shared<ColumnMap>(seed_array));
627
+ return;
628
+ }
629
+
579
630
  default:
580
631
  throw chn::EncoderFailure(
581
632
  "cannot insert into column of type " + type->GetName());
@@ -29,11 +29,26 @@ module ClickhouseNative
29
29
  # session settings), producing misleading log lines and re-raises in
30
30
  # unrelated code. A fresh socket + handshake is cheap relative to
31
31
  # debugging that.
32
+ #
33
+ # ConnectionError gets one automatic retry: pooled connections that
34
+ # have been idle long enough for the server / an LB to FIN them
35
+ # surface as "closed" on the very next recv (errno 0, message
36
+ # "closed: Success"). Discarding and re-checking out lands a fresh
37
+ # socket and the operation succeeds. The retry only triggers when
38
+ # the dead-connection error fired before any data was sent, so
39
+ # write operations don't risk double-execution from this path.
32
40
  def with
33
- @pool.with do |client|
34
- yield client
35
- rescue
36
- @pool.discard_current_connection(&:close)
41
+ attempts = 0
42
+ begin
43
+ @pool.with do |client|
44
+ yield client
45
+ rescue
46
+ @pool.discard_current_connection(&:close)
47
+ raise
48
+ end
49
+ rescue ConnectionError
50
+ attempts += 1
51
+ retry if attempts == 1
37
52
  raise
38
53
  end
39
54
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ClickhouseNative
4
- VERSION = "0.6.0"
4
+ VERSION = "0.8.0"
5
5
  end
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.6.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuri Smirnov