Class: ActiveRecord::Refined::AST::JsonAggregate

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

Overview

Rows gathered into one JSON document: json_arrayagg collects a value from each row into an array, json_objectagg a key and a value into an object. Every adapter has the pair under a name of its own; what PostgreSQL gets is the jsonb one, whose documents the other JSON operations here read.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Windowing

#over

Methods included from ComputedJson

#json_value?

Methods included from JsonComparable

#between?, #in?, #not_between?, #not_in?, #~

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(kind, operands, condition: nil) ⇒ JsonAggregate

Returns a new instance of JsonAggregate.



1673
1674
1675
1676
1677
# File 'lib/active_record/refined/ast.rb', line 1673

def initialize(kind, operands, condition: nil)
  @kind = kind
  @operands = operands
  @condition = condition
end

Instance Attribute Details

#conditionObject (readonly)

Returns the value of attribute condition.



1671
1672
1673
# File 'lib/active_record/refined/ast.rb', line 1671

def condition
  @condition
end

#kindObject (readonly)

Returns the value of attribute kind.



1671
1672
1673
# File 'lib/active_record/refined/ast.rb', line 1671

def kind
  @kind
end

#operandsObject (readonly)

Returns the value of attribute operands.



1671
1672
1673
# File 'lib/active_record/refined/ast.rb', line 1671

def operands
  @operands
end

Instance Method Details

#check_window(model) ⇒ Object

Over asks here before writing a window, since a family may take every other aggregate as one but not these two.



1690
1691
1692
# File 'lib/active_record/refined/ast.rb', line 1690

def check_window(model)
  Dialect.for(model).check_json_aggregate_window(json_source, model)
end

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

FILTER (WHERE condition), as Aggregate#filter; refused on the MySQL family, where the CASE that stands in would leave a JSON null for every row it drops.

Returns:



1683
1684
1685
1686
# File 'lib/active_record/refined/ast.rb', line 1683

def filter(condition = nil, &block)
  JsonAggregate.new(kind, operands,
                    condition: Case.argument(:filter, condition, block))
end

#to_arel(table, model) ⇒ Object



1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
# File 'lib/active_record/refined/ast.rb', line 1694

def to_arel(table, model)
  dialect = Dialect.for(model)
  call = Arel::Nodes::NamedFunction.new(
    dialect.json_aggregate_name(kind),
    operands.map { |operand| to_arel_argument(operand, table, model) })
  return call unless condition

  # The CASE that stands in for FILTER elsewhere hands the aggregate a
  # NULL for every row the condition misses, and these two keep a NULL
  # -- as JSON null -- rather than passing over it, so it is refused.
  unless dialect.json_aggregate_filter_supported?
    raise NotImplementedError,
      "#{json_source}.filter has no equivalent on " \
      "#{model.connection_db_config.adapter}; a CASE would leave a " \
      "null in the document for every row it drops"
  end
  call.filter(condition.to_arel(table, model))
end