Class: ConceptQL::Operators::TrimDateEnd

Inherits:
TemporalOperator show all
Defined in:
lib/conceptql/operators/trim_date_end.rb

Overview

Trims the end_date of the LHS set of results by the RHS’s earliest start_date (per person) If a the RHS contains a start_date that comes before the LHS’s start_date that LHS result is completely discarded.

If there is no RHS result for an LHS result, the LHS result is passed thru unaffected.

If the RHS result’s start_date is later than the LHS end_date, the LHS result is passed thru unaffected.

Constant Summary

Constants inherited from Operator

Operator::COLUMNS

Instance Attribute Summary

Attributes inherited from Operator

#arguments, #options, #upstreams, #values

Instance Method Summary collapse

Methods inherited from TemporalOperator

#inclusive?, #left_stream, #right_stream

Methods inherited from BinaryOperatorOperator

#display_name, #graph_it, #upstreams

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

#query(db) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/conceptql/operators/trim_date_end.rb', line 31

def query(db)
  grouped_right = db.from(right_stream(db)).select_group(:person_id).select_append(Sequel.as(Sequel.function(:min, :start_date), :start_date))

  where_criteria = Sequel.expr { l__start_date <= r__start_date }
  where_criteria = where_criteria.|(r__start_date: nil)

  # If the RHS's min start date is less than the LHS start date,
  # the entire LHS date range is truncated, which implies the row itself
  # is ineligible to pass thru
  db.from(db.from(left_stream(db))
            .left_join(Sequel.as(grouped_right, :r), l__person_id: :r__person_id)
            .where(where_criteria)
            .select(*new_columns)
            .select_append(Sequel.as(Sequel.function(:least, :l__end_date, :r__start_date), :end_date))
         )
end