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

Instance Attribute Summary

Attributes inherited from Operator

#arguments, #errors, #nodifier, #options, #upstreams, #values

Instance Method Summary collapse

Methods inherited from Operator

#annotate, #cast_column, #code_list, codes_should_match, #columns, #create_upstreams, #data_model, #database_type, default_query_columns, #domains, #dup_values, #dynamic_columns, #evaluate, inherited, #initialize, #inspect, #label, new, #operator_name, #optimized, query_columns, register, require_column, #required_columns, #scope, #select_it, #setup_select, #sql, #stream, #to_op, #unionable?, #upstreams_valid?, #valid?

Methods included from Metadatable

#allows_many_upstreams, #allows_one_upstream, #argument, #auto_label, #basic_type, #category, #derive_metadata_from_validations, #desc, #domains, #get_desc, #humanized_class_name, #inherited, #just_class_name, #no_desc, #option, #predominant_domains, #pref_name, #preferred_name, #reset_categories, #standard_description, #to_metadata, #validate_at_least_one_upstream_to_metadata, #validate_at_most_one_upstream_to_metadata, #validate_no_arguments_to_metadata, #validate_no_upstreams_to_metadata, #validate_one_upstream_to_metadata, #validate_required_options_to_metadata, #warn_about_missing_metadata

Constructor Details

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

Instance Method Details

#occurrenceObject



61
62
63
# File 'lib/conceptql/operators/occurrence.rb', line 61

def occurrence
  @occurrence ||= arguments.first
end

#occurrences(db) ⇒ Object



65
66
67
68
69
# 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) ⇒ Object



55
56
57
58
59
# File 'lib/conceptql/operators/occurrence.rb', line 55

def query(db)
  db[:occurrences]
    .with(:occurrences, occurrences(db))
    .where(rn: occurrence.abs)
end

#query_colsObject



51
52
53
# File 'lib/conceptql/operators/occurrence.rb', line 51

def query_cols
  dynamic_columns + [:rn]
end