Class: ConceptQL::Operators::Occurrence

Inherits:
Operator
  • Object
show all
Defined in:
lib/conceptql/operators/occurrence.rb

Overview

Represents a operator that will grab the Nth occurrence of something

Specify occurrences as integers, excluding O 1 => first 2 => second … -1 => last -2 => second-to-last

The operator treats all streams as a single, large stream. It partitions that larget stream by person_id, then sorts within those groupings by start_date and then select at most one row per person, regardless of how many different types of streams enter the operator

If two rows have the same start_date, the order of their ranking is arbitrary

If we ask for the second occurrence of something and a person has only one occurrence, this operator returns nothing for that person

Direct Known Subclasses

First, Last

Constant Summary

Constants inherited from Operator

ConceptQL::Operators::Operator::COLUMNS

Instance Attribute Summary

Attributes inherited from Operator

#arguments, #options, #upstreams, #values

Instance Method Summary collapse

Methods inherited from Operator

#columns, #evaluate, #initialize, #label, #select_it, #set_values, #sql, #stream, #types

Methods included from Metadatable

#allows_many_upstreams, #allows_one_upstream, #argument, #category, #desc, #humanized_class_name, #inherited, #just_class_name, #option, #predominant_types, #preferred_name, #reset_categories, #to_metadata, #types

Constructor Details

This class inherits a constructor from ConceptQL::Operators::Operator

Instance Method Details

#occurrenceObject



54
55
56
# File 'lib/conceptql/operators/occurrence.rb', line 54

def occurrence
  @occurrence ||= arguments.first
end

#query(db) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/conceptql/operators/occurrence.rb', line 45

def query(db)
  db[:occurrences]
    .with(:occurrences,
      stream.evaluate(db)
        .from_self
        .select_append { |o| o.row_number(:over, partition: :person_id, order: ordered_columns){}.as(:rn) })
    .where(rn: occurrence.abs)
end