click_house-client 0.5.0 → 0.6.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: 4fb73c384094387fbb53365e18a5378aca87148e07e501b25e03f9371cead01e
4
- data.tar.gz: f5a179a229a5c9fe115ce03d84d22d14b01c56dfa99694cea5f75d9f9c034f37
3
+ metadata.gz: 97b70adb1e2d3d10d16d81ff3510446d9811680c2f19ecc7183fd97d337ef667
4
+ data.tar.gz: bc846f969732dc3db9b74d202d2616e4fe185857f5aa8180e6bb94888563b874
5
5
  SHA512:
6
- metadata.gz: 1b3055e3104e139834ac5e3538ac3d0e402c16b8f1456861ed0bca269c03870038c9aadc97ebb1c74e1486a273c6a44867f20a07f423075ab8e8b90f972c2acd
7
- data.tar.gz: 9f1e7160de8c0d4ead180e10dfc40b36db5e155d08a1d7d2c62c6225636dc72751c7b1ee68d583ff8653409b8fdddb8e9ce0cd2ff920bd38995e5eab8408e04e
6
+ metadata.gz: 1db29052a7ea692177553eeede929f566966af28a1d10873e98edea6d96176ce72a1004669f4e079c169a33df071ae93e20bb438f6a074c3ddad4b915b3c44ac
7
+ data.tar.gz: 3c89c6ea62bb20bdcf6ec74df0ffd57a677f38b85a72ecf9d430c8f7a1a049f5fc726cabac275bccfa362c83879c76de54065eaa7f3388e308a65286cc776333
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- click_house-client (0.5.0)
4
+ click_house-client (0.6.0)
5
5
  activerecord (>= 7.0, < 9.0)
6
6
  activesupport (>= 7.0, < 9.0)
7
7
  addressable (~> 2.8)
@@ -136,7 +136,7 @@ CHECKSUMS
136
136
  benchmark (0.4.1) sha256=d4ef40037bba27f03b28013e219b950b82bace296549ec15a78016552f8d2cce
137
137
  bigdecimal (3.2.2) sha256=39085f76b495eb39a79ce07af716f3a6829bc35eb44f2195e2753749f2fa5adc
138
138
  byebug (12.0.0) sha256=d4a150d291cca40b66ec9ca31f754e93fed8aa266a17335f71bb0afa7fca1a1e
139
- click_house-client (0.5.0)
139
+ click_house-client (0.6.0)
140
140
  concurrent-ruby (1.3.5) sha256=813b3e37aca6df2a21a3b9f1d497f8cbab24a2b94cab325bffe65ee0f6cbebc6
141
141
  connection_pool (2.5.3) sha256=cfd74a82b9b094d1ce30c4f1a346da23ee19dc8a062a16a85f58eab1ced4305b
142
142
  diff-lcs (1.5.0) sha256=49b934001c8c6aedb37ba19daec5c634da27b318a7a3c654ae979d6ba1929b67
data/README.md CHANGED
@@ -22,10 +22,13 @@ ClickHouse::Client.configure do |config|
22
22
 
23
23
  # Use any HTTP client to build the POST request, here we use Net::HTTP
24
24
  config.http_post_proc = ->(url, headers, body) do
25
- # Query placeholders go to the URI
26
- params = URI.encode_www_form(body.except("query"))
25
+ uri = URI.parse(url)
26
+
27
+ unless body.is_a?(IO)
28
+ # Append placeholders to URI's query
29
+ uri.query = [uri.query, URI.encode_www_form(body.except("query"))].compact.join('&')
30
+ end
27
31
 
28
- uri = URI.parse("#{url}&#{params}")
29
32
  request = Net::HTTP::Post.new(uri)
30
33
 
31
34
  headers.each do |header, value|
@@ -33,9 +36,14 @@ ClickHouse::Client.configure do |config|
33
36
  end
34
37
 
35
38
  request['Content-type'] = 'application/x-www-form-urlencoded'
36
- request.body = body["query"]
37
39
 
38
- response = Net::HTTP.start(uri.hostname, uri.port) do |http|
40
+ if body.is_a?(IO)
41
+ request.body_stream = body
42
+ else
43
+ request.body = body['query']
44
+ end
45
+
46
+ response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
39
47
  http.request(request)
40
48
  end
41
49
 
@@ -6,6 +6,9 @@ module ClickHouse
6
6
  DEFAULT = ->(value) { value }
7
7
 
8
8
  BASIC_TYPE_CASTERS = {
9
+ 'Int32' => ->(value) { Integer(value) },
10
+ 'UInt32' => ->(value) { Integer(value) },
11
+ 'Int64' => ->(value) { Integer(value) },
9
12
  'UInt64' => ->(value) { Integer(value) },
10
13
  "DateTime64(6, 'UTC')" => ->(value) { ActiveSupport::TimeZone['UTC'].parse(value) },
11
14
  "IntervalSecond" => ->(value) { ActiveSupport::Duration.build(value.to_i) },
@@ -54,7 +54,25 @@ module ClickHouse
54
54
  validate_constraint_type!(constraints)
55
55
 
56
56
  clone.tap do |new_instance|
57
- add_constraints_to(new_instance, constraints)
57
+ apply_constraints(new_instance, constraints, :where)
58
+ end
59
+ end
60
+
61
+ # The `having` method applies constraints to the HAVING clause, similar to how
62
+ # `where` applies constraints to the WHERE clause. It supports the same constraint types.
63
+ # Correct usage:
64
+ # query.group(:name).having(count: 5).to_sql
65
+ # "SELECT * FROM \"table\" GROUP BY \"table\".\"name\" HAVING \"table\".\"count\" = 5"
66
+ #
67
+ # query.group(:name).having(query.table[:count].gt(10)).to_sql
68
+ # "SELECT * FROM \"table\" GROUP BY \"table\".\"name\" HAVING \"table\".\"count\" > 10"
69
+ #
70
+ # @return [ClickHouse::QueryBuilder] New instance of query builder.
71
+ def having(constraints)
72
+ validate_constraint_type!(constraints)
73
+
74
+ clone.tap do |new_instance|
75
+ apply_constraints(new_instance, constraints, :having)
58
76
  end
59
77
  end
60
78
 
@@ -179,32 +197,45 @@ module ClickHouse
179
197
  raise ArgumentError, "Unsupported Arel node type for QueryBuilder: #{constraint.class.name}"
180
198
  end
181
199
 
182
- def add_constraints_to(instance, constraints)
200
+ def apply_constraints(instance, constraints, clause_type)
183
201
  if constraints.is_a?(Arel::Nodes::Node)
184
- instance.manager.where(constraints)
202
+ apply_constraint_node(instance, constraints, clause_type)
185
203
  else
186
204
  constraints.each do |key, value|
187
205
  if value.is_a?(Hash)
188
206
  # Handle nested hash for joined tables
189
207
  join_table = Arel::Table.new(key)
190
208
  value.each do |nested_key, nested_value|
191
- build_arel_constraint(instance, join_table, nested_key, nested_value)
209
+ constraint = build_constraint(join_table, nested_key, nested_value)
210
+ apply_constraint_node(instance, constraint, clause_type)
192
211
  end
193
212
  else
194
- build_arel_constraint(instance, instance.table, key, value)
213
+ constraint = build_constraint(instance.table, key, value)
214
+ apply_constraint_node(instance, constraint, clause_type)
195
215
  end
196
216
  end
197
217
  end
198
218
  end
199
219
 
200
- def build_arel_constraint(instance, table, key, value)
201
- constraint = if value.is_a?(Array)
202
- table[key].in(value)
203
- else
204
- table[key].eq(value)
205
- end
220
+ def apply_constraint_node(instance, constraint, clause_type)
221
+ case clause_type
222
+ when :where
223
+ instance.manager.where(constraint)
224
+ when :having
225
+ instance.manager.having(constraint)
226
+ else
227
+ raise ArgumentError, "Unsupported clause type: #{clause_type}"
228
+ end
229
+ end
206
230
 
207
- instance.manager.where(constraint)
231
+ def build_constraint(table, key, value)
232
+ if value.is_a?(Array)
233
+ table[key].in(value)
234
+ elsif value.is_a?(ClickHouse::Client::QueryBuilder)
235
+ table[key].in(value.to_arel)
236
+ else
237
+ table[key].eq(value)
238
+ end
208
239
  end
209
240
 
210
241
  def validate_order_direction!(direction)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ClickHouse
4
4
  module Client
5
- VERSION = "0.5.0"
5
+ VERSION = "0.6.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: click_house-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - group::optimize
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-08-22 00:00:00.000000000 Z
11
+ date: 2025-09-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord