class ConceptQL::Operators::Occurrence

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

Public Instance Methods

occurrence() click to toggle source
# File lib/conceptql/operators/occurrence.rb, line 61
def occurrence
  @occurrence ||= arguments.first
end
occurrences(db) click to toggle source
# File lib/conceptql/operators/occurrence.rb, line 65
def occurrences(db)
  all_or_uniquified_results(db)
    .from_self
    .select_append { |o| o.row_number(:over, partition: :person_id, order: ordered_columns){}.as(:rn) }
end
query(db) click to toggle source
# File lib/conceptql/operators/occurrence.rb, line 55
def query(db)
  db[:occurrences]
    .with(:occurrences, occurrences(db))
    .where(rn: occurrence.abs)
end
query_cols() click to toggle source
# File lib/conceptql/operators/occurrence.rb, line 51
def query_cols
  dynamic_columns + [:rn]
end

Private Instance Methods

all_or_uniquified_results(db) click to toggle source
# File lib/conceptql/operators/occurrence.rb, line 95
def all_or_uniquified_results(db)
  return stream.evaluate(db) unless options[:unique]
  stream.evaluate(db)
    .from_self
    .select_append { |o| o.row_number(:over, partition: uniquify_partition_columns, order: ordered_columns){}.as(:unique_rn) }
    .from_self
    .where(unique_rn: 1)
end
asc_or_desc() click to toggle source
# File lib/conceptql/operators/occurrence.rb, line 82
def asc_or_desc
  occurrence < 0 ? :desc : :asc
end
ordered_columns() click to toggle source
# File lib/conceptql/operators/occurrence.rb, line 86
def ordered_columns
  ordered_columns = [Sequel.send(asc_or_desc, :start_date)]
  ordered_columns += [:criterion_id]
end
uniquify_partition_columns() click to toggle source
# File lib/conceptql/operators/occurrence.rb, line 91
def uniquify_partition_columns
  dynamic_columns - [:criterion_id, :start_date, :end_date]
end
validate(db, opts = {}) click to toggle source
Calls superclass method
# File lib/conceptql/operators/occurrence.rb, line 73
def validate(db, opts = {})
  super
  if self.class == Occurrence
    validate_one_argument
  else
    validate_no_arguments
  end
end