click_house-client 0.13.0 → 0.14.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: 839c38cadbcf3638fc513c949c9f7d1b40453c92c0274e32db1d1e9da86df156
4
- data.tar.gz: f23d52098f04a9cdb460bf0d8479b19947641546b37ccd85519eeeaba3e59eb6
3
+ metadata.gz: adb2fc38340de425ff11e42dd7bee76851eac77cad898bef8b0d99c232cb701d
4
+ data.tar.gz: 5734a9ccf153c2eaf48028f3c8114efe379a8292e6562ffe24d08adba84c57dd
5
5
  SHA512:
6
- metadata.gz: 3c6f54deb213de89f6a5e2e5ad3cccf3e4b3412eb2b22624dc4fda38a41e8a21555736fa7b453fc2e7a07c1cf9fe6f07ef5f5b7bef0aee58019f0e7faa77604c
7
- data.tar.gz: 2bbbcf502cba7cb5ed185674afd8eae30a28173ab599b2b00f582a21ed65a140b2d92b0ed523ad1dad192c35fbc0a5eb19ec98a3c815f7212f4f98d739795a53
6
+ metadata.gz: 76f02a69bf0eb2a1225bc6668789a1fccf71438bf17eef3423058b6922ae740c2db53aedbcc06cfdf3a9a21418a84a77a0d6507e7fc6982989d73c336435e0b0
7
+ data.tar.gz: 330f9065578dc973eca88f21fe47a59087ccc3b1beb76060017d62a683bc27203274f9557466e78398417a9c02640750800e23af00061a791a30724efe611f12
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- click_house-client (0.13.0)
4
+ click_house-client (0.14.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.13.0)
139
+ click_house-client (0.14.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
@@ -202,7 +202,7 @@ query
202
202
  The `reverse_order` method inverts the direction of all `ORDER BY` clauses (`ASC` becomes `DESC` and vice versa).
203
203
 
204
204
  ```ruby
205
- query.order(:name, :desc).order(:id).reverse_order.to_sql
205
+ query.order(name: :desc).order(:id).reverse_order.to_sql
206
206
  # => "SELECT * FROM `users` ORDER BY `users`.`name` ASC, `users`.`id` DESC"
207
207
  ```
208
208
 
@@ -253,6 +253,15 @@ query.where(id: subquery).to_sql
253
253
  # => "SELECT * FROM `users` WHERE `users`.`id` IN (SELECT `orders`.`user_id` FROM `orders` SETTINGS max_threads = 2)"
254
254
  ```
255
255
 
256
+ ### Rendering Arel nodes
257
+
258
+ To render an Arel node as ClickHouse SQL, pass `ClickHouse::Client::ArelEngine` to `#to_sql`:
259
+
260
+ ```ruby
261
+ node = Arel::Table.new(:users)[:name].eq('a\b') # Arel::Nodes::Equality
262
+ node.to_sql(ClickHouse::Client::ArelEngine.new) # "`users`.`name` = 'a\\b'"
263
+ ```
264
+
256
265
  ### Working with JOINs
257
266
 
258
267
  `#joins` supports `INNER JOIN` (default) and `LEFT OUTER JOIN` via
@@ -362,7 +371,7 @@ result = ClickHouse::Client::QueryBuilder.new('users')
362
371
  .where(query.table[:email].matches('%@company.com'))
363
372
  .group(:department)
364
373
  .having(count_func.gt(5))
365
- .order(Arel.sql('user_count'), :desc)
374
+ .order(Arel.sql('user_count') => :desc)
366
375
  .limit(10)
367
376
 
368
377
  puts result.to_sql
@@ -3,6 +3,19 @@
3
3
  module ClickHouse
4
4
  module Client
5
5
  class ArelEngine
6
+ def with_connection
7
+ yield self
8
+ end
9
+
10
+ # Rails < 7.2 calls `engine.connection` in Arel::Nodes::Node#to_sql
11
+ def connection
12
+ self
13
+ end
14
+
15
+ def visitor
16
+ @visitor ||= ClickHouse::Client::ArelVisitor.new(self)
17
+ end
18
+
6
19
  def quote_table_name(name)
7
20
  "`#{name}`" # Safest approach for ClickHouse
8
21
  end
@@ -10,6 +10,8 @@ module ClickHouse
10
10
  attr_reader :table
11
11
  attr_accessor :manager
12
12
 
13
+ DEPRECATOR = ActiveSupport::Deprecation.new('1.0', 'ClickHouse::Client')
14
+
13
15
  VALID_NODES = [
14
16
  Arel::Nodes::In,
15
17
  Arel::Nodes::Equality,
@@ -134,19 +136,53 @@ module ClickHouse
134
136
  end
135
137
  end
136
138
 
137
- def order(field, direction = :asc)
138
- validate_order_direction!(direction)
139
+ # Appends ORDER BY clauses. Accepts either a single column, or a Hash of
140
+ # columns to directions. A single column is a shorthand for `column => :asc`.
141
+ #
142
+ # Passing a direction as the second argument is deprecated, use a Hash
143
+ # instead, so that `order(:a, :b)` can later mean `order(a: :asc, b: :asc)`.
144
+ #
145
+ # @example
146
+ # query.order(:id).to_sql
147
+ # # => "SELECT * FROM `test_table` ORDER BY `test_table`.`id` ASC"
148
+ # query.order(id: :desc).to_sql
149
+ # # => "SELECT * FROM `test_table` ORDER BY `test_table`.`id` DESC"
150
+ # query.order(name: :desc, id: :asc).to_sql
151
+ # # => "SELECT * FROM `test_table` ORDER BY `test_table`.`name` DESC, `test_table`.`id` ASC"
152
+ #
153
+ # @param field_or_hash [Symbol, String, Arel::Nodes::Node, Arel::Attribute, Hash] column, Arel node,
154
+ # or a Hash of column (or Arel node) => direction pairs
155
+ # @param direction [Symbol, String, nil] :asc (default) or :desc; deprecated, and not allowed with a Hash
156
+ # @raise [ArgumentError] if a direction is invalid, or is passed together with a Hash
157
+ # @return [ClickHouse::Client::QueryBuilder] New instance of query builder.
158
+ def order(field_or_hash, direction = nil)
159
+ unless field_or_hash.is_a?(Hash)
160
+ if direction
161
+ DEPRECATOR.warn(
162
+ 'Passing a direction as the second argument of QueryBuilder#order is deprecated. ' \
163
+ "Use `order(#{field_or_hash.inspect} => #{direction.inspect})` instead."
164
+ )
165
+ end
166
+
167
+ return order({ field_or_hash => direction || :asc })
168
+ end
169
+
170
+ raise ArgumentError, 'Direction cannot be passed together with a Hash of orderings' if direction
171
+
172
+ field_or_hash.each_value { |column_direction| validate_order_direction!(column_direction) }
139
173
 
140
174
  clone.tap do |new_instance|
141
- order_node = case field
142
- when Arel::Nodes::SqlLiteral, Arel::Nodes::Node, Arel::Attribute
143
- field
144
- else
145
- new_instance.table[field]
146
- end
147
-
148
- new_order = direction.to_s.casecmp('desc').zero? ? order_node.desc : order_node.asc
149
- new_instance.manager.order(new_order)
175
+ field_or_hash.each do |column, column_direction|
176
+ order_node = case column
177
+ when Arel::Nodes::SqlLiteral, Arel::Nodes::Node, Arel::Attribute
178
+ column
179
+ else
180
+ new_instance.table[column]
181
+ end
182
+
183
+ new_order = column_direction.to_s.casecmp('desc').zero? ? order_node.desc : order_node.asc
184
+ new_instance.manager.order(new_order)
185
+ end
150
186
  end
151
187
  end
152
188
 
@@ -518,8 +554,7 @@ module ClickHouse
518
554
  end
519
555
 
520
556
  def to_sql
521
- visitor = ClickHouse::Client::ArelVisitor.new(ClickHouse::Client::ArelEngine.new)
522
- visitor.accept(manager.ast, Arel::Collectors::SQLString.new).value
557
+ manager.ast.to_sql(ClickHouse::Client::ArelEngine.new)
523
558
  end
524
559
 
525
560
  def to_redacted_sql
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ClickHouse
4
4
  module Client
5
- VERSION = "0.13.0"
5
+ VERSION = "0.14.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.13.0
4
+ version: 0.14.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: 2026-10-07 00:00:00.000000000 Z
11
+ date: 2026-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord