diff --git a/Gemfile.lock b/Gemfile.lock index a2c44fe..6f0399d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -11,7 +11,7 @@ GIT PATH remote: . specs: - umbrellio-utils (1.16.0) + umbrellio-utils (1.16.1) memery (~> 1) GEM diff --git a/lib/umbrellio_utils/click_house/backends/base.rb b/lib/umbrellio_utils/click_house/backends/base.rb index d15bdca..6864d4d 100644 --- a/lib/umbrellio_utils/click_house/backends/base.rb +++ b/lib/umbrellio_utils/click_house/backends/base.rb @@ -117,16 +117,15 @@ def deduplication_source # / config / logger) and define SERVER_ERROR. 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).with_extend(ClickHouseDatasetMethods) + ds = base_dataset + case source + when Symbol + ds.from(db_name == self.db_name ? SQL[source] : SQL[db_name][source]) + when nil + ds + else + ds.from(source) + end end def count(dataset, **) @@ -246,6 +245,12 @@ def with_temp_table( protected + def base_dataset + return @base_dataset if @base_dataset&.db.equal?(DB) + + @base_dataset = DB.dataset.clone(ch: true).with_extend(ClickHouseDatasetMethods) + end + # Every read path needs both halves, and pairing them here keeps a new # one from silently reinstating session-wide FINAL inside a dedup # subquery — the exact cost `deduplicate` exists to remove. diff --git a/lib/umbrellio_utils/version.rb b/lib/umbrellio_utils/version.rb index bf0eec0..1d39808 100644 --- a/lib/umbrellio_utils/version.rb +++ b/lib/umbrellio_utils/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module UmbrellioUtils - VERSION = "1.16.0" + VERSION = "1.16.1" end diff --git a/spec/umbrellio_utils/clickhouse_spec.rb b/spec/umbrellio_utils/clickhouse_spec.rb index 51e35c4..a48528b 100644 --- a/spec/umbrellio_utils/clickhouse_spec.rb +++ b/spec/umbrellio_utils/clickhouse_spec.rb @@ -10,6 +10,26 @@ end describe "#from" do + it "reuses the same dataset class for every source kind" do + datasets = [ch.from(:a), ch.from(:b), ch.from(nil), ch.from(ch.from(:a))] + + expect(datasets.map(&:class)).to all(equal(datasets.first.class)) + end + + it "marks every source kind as a ClickHouse dataset" do + datasets = [ch.from(:a), ch.from(nil), ch.from(ch.from(:a)), ch.from(Sequel[:a].as(:b))] + + expect(datasets.map { |dataset| dataset.opts[:ch] }).to all(be(true)) + end + + it "uses the current DB after the constant is replaced" do + ch.from(:a) + replacement_db = Sequel.mock + stub_const("DB", replacement_db) + + expect(ch.from(:b).db).to equal(replacement_db) + end + context "with another db" do specify do expect(ch.from(:test, db_name: :test).sql).to eq(