Class: ActiveRecord::Refined::AST::Aggregate

Inherits:
Node
  • Object
show all
Includes:
Arithmetics, Predications, Windowing
Defined in:
lib/active_record/refined/ast.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Windowing

#over

Methods included from Arithmetics

#&, #*, #+, #-, #/, #<<, #>>, #^, #|, #~

Methods included from Predications

#!=, #!~, #<, #<=, #==, #=~, #>, #>=, #between?, #bury, #casecmp?, #contains?, #dig, #dig_text, #distinct_from?, #end_with?, #except, #false?, #ilike?, #in?, #include?, #intersect?, #key?, #keys, #like?, #member?, #not_between?, #not_distinct_from?, #not_false?, #not_ilike?, #not_in?, #not_like?, #not_null?, #not_true?, #null?, #start_with?, #subset?, #superset?, #true?, #when

Methods inherited from Node

#as, #asc, #collate, #desc

Constructor Details

#initialize(operand, function, distinct: false, condition: nil) ⇒ Aggregate

Returns a new instance of Aggregate.



1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
# File 'lib/active_record/refined/ast.rb', line 1611

def initialize(operand, function, distinct: false, condition: nil)
  if distinct && !DISTINCT_FUNCTIONS.include?(function)
    raise ArgumentError, "#{function} does not take distinct; it would give the same"
  end
  if distinct && operand == :*
    raise ArgumentError, "count(:*) does not take distinct; name a column"
  end
  @operand = operand
  @function = function
  @distinct = distinct
  @condition = condition
end

Instance Attribute Details

#conditionObject (readonly)

Returns the value of attribute condition.



1609
1610
1611
# File 'lib/active_record/refined/ast.rb', line 1609

def condition
  @condition
end

#distinctObject (readonly)

Returns the value of attribute distinct.



1609
1610
1611
# File 'lib/active_record/refined/ast.rb', line 1609

def distinct
  @distinct
end

#functionObject (readonly)

Returns the value of attribute function.



1609
1610
1611
# File 'lib/active_record/refined/ast.rb', line 1609

def function
  @function
end

#operandObject (readonly)

Returns the value of attribute operand.



1609
1610
1611
# File 'lib/active_record/refined/ast.rb', line 1609

def operand
  @operand
end

Instance Method Details

#filter(condition = nil, &block) ⇒ AST::Aggregate

FILTER (WHERE condition): the aggregate taken over the rows the condition holds for, as a value or a block. Where there is no FILTER clause -- MySQL, SQL Server -- the CASE that means the same.

Examples:

Author.select { [count(:*).as(:all), count(:*).filter { :age < 50 }.as(:young)] }

Returns:



1630
1631
1632
1633
# File 'lib/active_record/refined/ast.rb', line 1630

def filter(condition = nil, &block)
  Aggregate.new(operand, function, distinct: distinct,
                condition: Case.argument(:filter, condition, block))
end

#to_arel(table, model) ⇒ Object



1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
# File 'lib/active_record/refined/ast.rb', line 1635

def to_arel(table, model)
  return aggregate(operand, table, model) unless condition

  # A family without a FILTER clause gets the CASE that means the same.
  # An aggregate passes over a NULL, so the case that yields nothing for
  # the rows the condition misses is the same aggregate over the same
  # rows -- count(*) has no operand to keep, and counts a 1 instead.
  unless Dialect.for(model).filter_supported?
    kept = Case.new.when(condition).then(operand == :* ? 1 : operand)
    return aggregate(kept, table, model)
  end

  aggregate(operand, table, model).filter(condition.to_arel(table, model))
end