Module: ActiveRecord::ConnectionAdapters::ClickHouse::RelationCalculations
- Defined in:
- lib/active_record/connection_adapters/clickhouse/querying.rb
Overview
Terminal calculations over ClickHouse's aggregate-function library; separate
from RelationMethods because these execute immediately rather than build state.
All accept merge: true (-Merge, finishing AggregateFunction state columns) and
if: condition (-If, per-row conditional aggregation in a single scan); if is
a Ruby keyword, so it travels in **options.
Instance Method Summary collapse
- #arg_max(column, criterion, **options) ⇒ Object
- #arg_min(column, criterion, **options) ⇒ Object
-
#estimated_count ⇒ Object
Table-level row estimate from metadata — O(1), ignores relation scopes.
-
#quantile(fraction, column, merge: false, **options) ⇒ Object
Parametric aggregates render as name(param)(args); Float/Integer coercion is the injection guard for the parameter.
- #top_k(count, column, merge: false, **options) ⇒ Object
- #uniq_count(column, exact: false, merge: false, **options) ⇒ Object
Instance Method Details
#arg_max(column, criterion, **options) ⇒ Object
339 340 341 |
# File 'lib/active_record/connection_adapters/clickhouse/querying.rb', line 339 def arg_max(column, criterion, **) aggregate("argMax", [klass.arel_table[column], klass.arel_table[criterion]], condition: [:if]) end |
#arg_min(column, criterion, **options) ⇒ Object
343 344 345 |
# File 'lib/active_record/connection_adapters/clickhouse/querying.rb', line 343 def arg_min(column, criterion, **) aggregate("argMin", [klass.arel_table[column], klass.arel_table[criterion]], condition: [:if]) end |
#estimated_count ⇒ Object
Table-level row estimate from metadata — O(1), ignores relation scopes.
348 349 350 351 352 353 354 355 |
# File 'lib/active_record/connection_adapters/clickhouse/querying.rb', line 348 def estimated_count klass.with_connection do |connection| connection.select_value(<<~SQL.squish, "#{klass.name} Estimated Count").to_i SELECT total_rows FROM system.tables WHERE database = currentDatabase() AND name = #{connection.quote(klass.table_name)} SQL end end |
#quantile(fraction, column, merge: false, **options) ⇒ Object
Parametric aggregates render as name(param)(args); Float/Integer coercion is the injection guard for the parameter.
329 330 331 332 |
# File 'lib/active_record/connection_adapters/clickhouse/querying.rb', line 329 def quantile(fraction, column, merge: false, **) aggregate("quantile", [klass.arel_table[column]], parameter: Float(fraction), merge: merge, condition: [:if]) end |
#top_k(count, column, merge: false, **options) ⇒ Object
334 335 336 337 |
# File 'lib/active_record/connection_adapters/clickhouse/querying.rb', line 334 def top_k(count, column, merge: false, **) aggregate("topK", [klass.arel_table[column]], parameter: Integer(count), merge: merge, condition: [:if]) end |
#uniq_count(column, exact: false, merge: false, **options) ⇒ Object
322 323 324 325 |
# File 'lib/active_record/connection_adapters/clickhouse/querying.rb', line 322 def uniq_count(column, exact: false, merge: false, **) aggregate(exact ? "uniqExact" : "uniq", [klass.arel_table[column]], merge: merge, condition: [:if]) end |