cqframework / cql-engine

Clinical Quality Language Evaluation Engine
Apache License 2.0
62 stars 45 forks source link

Error with a function that returns an Interval #33

Closed brynrhodes closed 6 years ago

brynrhodes commented 6 years ago

The following script:

define TestDateTime1: @2017-12-20T11:00:00
define function ToDate(Value DateTime):
  DateTime(year from Value, month from Value, day from Value, 0, 0, 0, 0, timezone from Value)

define function CalendarDayOf(Value DateTime):
  Interval[Value, ToDate(Value + 1 day))

define TestCalendarDayOf: CalendarDayOf(TestDateTime1)

Gives the error "Invalid interval, the ending boundary must be greater than or equal to the starting boundary", but when I create an interval with what I expect the result of the above to be:

define TestDateTime1: @2017-12-20T11:00:00
define TestDateTime3: @2017-12-21T00:00:00.0
define TestInterval: Interval[TestDateTime1, TestDateTime3)

I get the correct result.

brynrhodes commented 6 years ago

Actually, this is due to the scoping issue with function evaluation. The problem is that the default iterator for a stack is backwards, so it's walking the context from the bottom up, not the top down.

https://github.com/DBCG/cql_engine/blob/master/cql-engine/src/main/java/org/opencds/cqf/cql/execution/Context.java#L215

https://github.com/DBCG/cql_engine/blob/master/cql-engine/src/main/java/org/opencds/cqf/cql/execution/Context.java#L499

It's okay for variable resolution within a window, it's only the stack windows that need to be reversed.

duncand commented 6 years ago

See also issue #39 which the fix for issue #33 indirectly exposed. Two DateTime that display equal don't compare as equal.