click_house-client 0.12.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: 5513d15d8bf17295cf98d021b30650f46d0101a6d828b2620499f87a949f2e3d
4
- data.tar.gz: 27fd28cc07d8b67d8de72031de4e3c7e343cef51a3230afd96ba0adf3d24c252
3
+ metadata.gz: adb2fc38340de425ff11e42dd7bee76851eac77cad898bef8b0d99c232cb701d
4
+ data.tar.gz: 5734a9ccf153c2eaf48028f3c8114efe379a8292e6562ffe24d08adba84c57dd
5
5
  SHA512:
6
- metadata.gz: b3d7bf7328dd7ab42b004e3e35e58c35b4869267e4a2e701f7d29f963c2c445c3d63f0bbd5e45db679cc4d2fda6880ef367aff18e0a04b1a030f54ede2317ead
7
- data.tar.gz: 40125fc9d75f13be2f1230ea872b72c6d01e797384674064d3fb42c613d1792c827e58d6ccfcd894b783ff4d89b838c04e9f50b2e297f4b085bba5c33b95585e
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.11.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.11.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
 
@@ -231,6 +231,37 @@ query.final.where(active: true).to_sql
231
231
 
232
232
  `FINAL` is currently applied only to the main `FROM` table. When joining, joined tables are not marked `FINAL`. Calling `.final` multiple times is idempotent.
233
233
 
234
+ ### Query Settings
235
+
236
+ Use `.settings` to append a ClickHouse
237
+ [`SETTINGS`](https://clickhouse.com/docs/en/sql-reference/statements/select#settings-in-select-query)
238
+ clause to the query. Chained calls merge, with later values overriding earlier ones.
239
+ Booleans are rendered as `1` / `0` and strings are quoted.
240
+
241
+ ```ruby
242
+ query.where(active: true).settings(max_execution_time: 60, use_query_cache: true).limit(10).to_sql
243
+ # => "SELECT * FROM `users` WHERE `users`.`active` = 'true' LIMIT 10 SETTINGS max_execution_time = 60, use_query_cache = 1"
244
+ ```
245
+
246
+ Settings belong to the query they are set on. When that builder is embedded as a
247
+ subquery, CTE, `UNION` member or `{var:Subquery}` placeholder, its `SETTINGS` clause is
248
+ rendered inside that part of the query, and ClickHouse applies it there:
249
+
250
+ ```ruby
251
+ subquery = ClickHouse::Client::QueryBuilder.new('orders').select(:user_id).settings(max_threads: 2)
252
+ query.where(id: subquery).to_sql
253
+ # => "SELECT * FROM `users` WHERE `users`.`id` IN (SELECT `orders`.`user_id` FROM `orders` SETTINGS max_threads = 2)"
254
+ ```
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
+
234
265
  ### Working with JOINs
235
266
 
236
267
  `#joins` supports `INNER JOIN` (default) and `LEFT OUTER JOIN` via
@@ -340,7 +371,7 @@ result = ClickHouse::Client::QueryBuilder.new('users')
340
371
  .where(query.table[:email].matches('%@company.com'))
341
372
  .group(:department)
342
373
  .having(count_func.gt(5))
343
- .order(Arel.sql('user_count'), :desc)
374
+ .order(Arel.sql('user_count') => :desc)
344
375
  .limit(10)
345
376
 
346
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
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'arel'
4
+
5
+ module ClickHouse
6
+ module Client
7
+ module ArelExtensions
8
+ module Nodes
9
+ # Holds the settings hash rendered as a trailing ClickHouse `SETTINGS` clause.
10
+ class Settings < ::Arel::Nodes::Unary
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -40,7 +40,20 @@ module ClickHouse
40
40
  collector << " FINAL"
41
41
  end
42
42
 
43
+ def visit_ClickHouse_Client_ArelExtensions_Nodes_Settings(object, collector)
44
+ rendered = object.expr.map { |name, value| "#{name} = #{quote_setting_value(value)}" }
45
+ collector << "SETTINGS #{rendered.join(', ')}"
46
+ end
47
+
43
48
  # rubocop:enable Naming/MethodName
49
+
50
+ def quote_setting_value(value)
51
+ case value
52
+ when true then '1'
53
+ when false then '0'
54
+ else ClickHouse::Client::Quoting.quote(value)
55
+ end
56
+ end
44
57
  end
45
58
  end
46
59
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'active_record'
4
4
  require_relative 'arel_extensions/nodes/final'
5
+ require_relative 'arel_extensions/nodes/settings'
5
6
 
6
7
  module ClickHouse
7
8
  module Client
@@ -9,6 +10,8 @@ module ClickHouse
9
10
  attr_reader :table
10
11
  attr_accessor :manager
11
12
 
13
+ DEPRECATOR = ActiveSupport::Deprecation.new('1.0', 'ClickHouse::Client')
14
+
12
15
  VALID_NODES = [
13
16
  Arel::Nodes::In,
14
17
  Arel::Nodes::Equality,
@@ -30,6 +33,8 @@ module ClickHouse
30
33
  Arel::Nodes::As
31
34
  ].freeze
32
35
 
36
+ SETTING_NAME_REGEX = /\A[a-zA-Z_][a-zA-Z0-9_]*\z/
37
+
33
38
  delegate :[], to: :table
34
39
 
35
40
  def initialize(table, alias_name = nil)
@@ -131,19 +136,53 @@ module ClickHouse
131
136
  end
132
137
  end
133
138
 
134
- def order(field, direction = :asc)
135
- 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) }
136
173
 
137
174
  clone.tap do |new_instance|
138
- order_node = case field
139
- when Arel::Nodes::SqlLiteral, Arel::Nodes::Node, Arel::Attribute
140
- field
141
- else
142
- new_instance.table[field]
143
- end
144
-
145
- new_order = direction.to_s.casecmp('desc').zero? ? order_node.desc : order_node.asc
146
- 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
147
186
  end
148
187
  end
149
188
 
@@ -224,6 +263,35 @@ module ClickHouse
224
263
  end
225
264
  end
226
265
 
266
+ # Appends a ClickHouse `SETTINGS` clause to the end of the query.
267
+ # See https://clickhouse.com/docs/en/sql-reference/statements/select#settings-in-select-query
268
+ #
269
+ # Chained calls merge settings; a later value overrides an earlier one.
270
+ # Boolean values are rendered as 1 / 0 and strings are quoted.
271
+ #
272
+ # Settings are part of the query AST, so they are also rendered when the
273
+ # builder is embedded as a subquery, CTE or UNION member, and ClickHouse
274
+ # applies them to that part of the query.
275
+ #
276
+ # @example
277
+ # query.settings(max_execution_time: 60, use_query_cache: true).to_sql
278
+ # # => "SELECT * FROM `test_table` SETTINGS max_execution_time = 60, use_query_cache = 1"
279
+ #
280
+ # @param new_settings [Hash{Symbol, String => Integer, Float, String, Symbol, Boolean}]
281
+ # @raise [ArgumentError] if a setting name or value is invalid
282
+ # @return [ClickHouse::Client::QueryBuilder] New instance of query builder.
283
+ def settings(new_settings)
284
+ new_settings.each { |name, value| validate_setting!(name, value) }
285
+
286
+ clone.tap do |new_instance|
287
+ ast = new_instance.manager.ast
288
+ merged = (ast.lock&.expr || {}).merge(new_settings.transform_keys(&:to_s)).freeze
289
+ # ClickHouse has no row locking, so the `lock` slot (rendered after LIMIT / OFFSET)
290
+ # is free to carry the SETTINGS clause.
291
+ ast.lock = ArelExtensions::Nodes::Settings.new(merged)
292
+ end
293
+ end
294
+
227
295
  def from(subquery, alias_name)
228
296
  clone.tap do |new_instance|
229
297
  new_from = if subquery.is_a?(self.class)
@@ -486,8 +554,7 @@ module ClickHouse
486
554
  end
487
555
 
488
556
  def to_sql
489
- visitor = ClickHouse::Client::ArelVisitor.new(ClickHouse::Client::ArelEngine.new)
490
- visitor.accept(manager.ast, Arel::Collectors::SQLString.new).value
557
+ manager.ast.to_sql(ClickHouse::Client::ArelEngine.new)
491
558
  end
492
559
 
493
560
  def to_redacted_sql
@@ -501,6 +568,16 @@ module ClickHouse
501
568
 
502
569
  private
503
570
 
571
+ def validate_setting!(name, value)
572
+ unless (name.is_a?(String) || name.is_a?(Symbol)) && name.to_s.match?(SETTING_NAME_REGEX)
573
+ raise ArgumentError, "Invalid setting name '#{name}'"
574
+ end
575
+
576
+ return if [Integer, Float, String, Symbol, TrueClass, FalseClass].any? { |klass| value.is_a?(klass) }
577
+
578
+ raise ArgumentError, "Invalid value for setting '#{name}': #{value.class.name}"
579
+ end
580
+
504
581
  def normalize_operand(operand)
505
582
  case operand
506
583
  when Arel::Expressions
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ClickHouse
4
4
  module Client
5
- VERSION = "0.12.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.12.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-08-04 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
@@ -186,6 +186,7 @@ files:
186
186
  - lib/click_house/client.rb
187
187
  - lib/click_house/client/arel_engine.rb
188
188
  - lib/click_house/client/arel_extensions/nodes/final.rb
189
+ - lib/click_house/client/arel_extensions/nodes/settings.rb
189
190
  - lib/click_house/client/arel_visitor.rb
190
191
  - lib/click_house/client/configuration.rb
191
192
  - lib/click_house/client/database.rb