Module: CoreExtensions::ActiveRecord::Relation
- Defined in:
- lib/core_extensions/active_record/relation.rb
Instance Method Summary collapse
-
#final ⇒ Object
When FINAL is specified, ClickHouse fully merges the data before returning the result and thus performs all data transformations that happen during merges for the given table engine.
- #final! ⇒ Object
- #reverse_order! ⇒ Object
-
#settings(**opts) ⇒ Object
Define settings in the SETTINGS clause of the SELECT query.
- #settings!(**opts) ⇒ Object
-
#using(*opts) ⇒ Object
The USING clause specifies one or more columns to join, which establishes the equality of these columns.
- #using!(*opts) ⇒ Object
-
#window(name, **opts) ⇒ Object
Windows functions let you perform calculations across a set of rows that are related to the current row.
- #window!(name, **opts) ⇒ Object
Instance Method Details
#final ⇒ Object
When FINAL is specified, ClickHouse fully merges the data before returning the result and thus performs all data transformations that happen during merges for the given table engine. For example:
users = User.final.all
# SELECT users.* FROM users FINAL
An ActiveRecord::ActiveRecordError
will be raised if database not ClickHouse.
40 41 42 |
# File 'lib/core_extensions/active_record/relation.rb', line 40 def final spawn.final! end |
#final! ⇒ Object
44 45 46 47 48 |
# File 'lib/core_extensions/active_record/relation.rb', line 44 def final! check_command('FINAL') @values[:final] = true self end |
#reverse_order! ⇒ Object
4 5 6 7 8 9 10 11 12 |
# File 'lib/core_extensions/active_record/relation.rb', line 4 def reverse_order! return super unless connection.is_a?(::ActiveRecord::ConnectionAdapters::ClickhouseAdapter) orders = order_values.uniq.reject(&:blank?) return super unless orders.empty? && !primary_key self.order_values = (column_names & %w[date created_at]).map { |c| arel_table[c].desc } self end |
#settings(**opts) ⇒ Object
Define settings in the SETTINGS clause of the SELECT query. The setting value is applied only to that query and is reset to the default or previous value after the query is executed. For example:
users = User.settings(optimize_read_in_order: 1, cast_keep_nullable: 1).where(name: 'John')
# SELECT users.* FROM users WHERE users.name = 'John' SETTINGS optimize_read_in_order = 1, cast_keep_nullable = 1
An ActiveRecord::ActiveRecordError
will be raised if database not ClickHouse.
22 23 24 |
# File 'lib/core_extensions/active_record/relation.rb', line 22 def settings(**opts) spawn.settings!(**opts) end |
#settings!(**opts) ⇒ Object
27 28 29 30 31 |
# File 'lib/core_extensions/active_record/relation.rb', line 27 def settings!(**opts) check_command('SETTINGS') @values[:settings] = (@values[:settings] || {}).merge opts self end |
#using(*opts) ⇒ Object
The USING clause specifies one or more columns to join, which establishes the equality of these columns. For example:
users = User.joins(:joins).using(:event_name, :date)
# SELECT users.* FROM users INNER JOIN joins USING event_name,date
An ActiveRecord::ActiveRecordError
will be raised if database not ClickHouse.
57 58 59 |
# File 'lib/core_extensions/active_record/relation.rb', line 57 def using(*opts) spawn.using!(*opts) end |
#using!(*opts) ⇒ Object
62 63 64 65 |
# File 'lib/core_extensions/active_record/relation.rb', line 62 def using!(*opts) @values[:using] = opts self end |
#window(name, **opts) ⇒ Object
Windows functions let you perform calculations across a set of rows that are related to the current row. For example:
users = User.window('x', order: 'date', partition: 'name', rows: 'UNBOUNDED PRECEDING').select('sum(value) OVER x')
# SELECT sum(value) OVER x FROM users WINDOW x AS (PARTITION BY name ORDER BY date ROWS UNBOUNDED PRECEDING)
74 75 76 |
# File 'lib/core_extensions/active_record/relation.rb', line 74 def window(name, **opts) spawn.window!(name, **opts) end |
#window!(name, **opts) ⇒ Object
78 79 80 81 82 |
# File 'lib/core_extensions/active_record/relation.rb', line 78 def window!(name, **opts) @values[:windows] = [] unless @values[:windows] @values[:windows] << [name, opts] self end |