FHIR Chat · Aggregate clause · implementers

Stream: implementers

Topic: Aggregate clause


view this post on Zulip Joel Montavon (Oct 04 2021 at 16:21):

I am trying to test the aggregate clause in the Atom extension. I'm using the following code from the developer's guide:

define "FactorialOfFive":
({ 1, 2, 3, 4, 5 }) Num
aggregate Result starting 1: Result * Num

This returns the original list ({ 1, 2, 3, 4, 5 }) instead of 120.

It looks like the aggregate clause is a new feature of CQL 1.5. And, the atom extension lists CQL 1.5 in its dependencies. How do I know if this is a bug or hasn't truly been implemented yet?

Thanks in advance.

view this post on Zulip Lloyd McKenzie (Oct 04 2021 at 16:32):

@Bryn Rhodes

view this post on Zulip JP (Oct 05 2021 at 02:39):

Hi Joel!

The cql-engine used by Atom doesn’t (yet) implement all of the 1.5 features. Aggregate is a missing one. There’s an issue tracking that here:

https://github.com/DBCG/cql_engine/issues/373

view this post on Zulip Rebecca Green (Oct 29 2021 at 15:55):

Asked this in a different channel but feel like it might be appropriate to ask on this thread. I'm trying to write a function similar to this:

define function AggregateFunction():
  (expand Interval[1, 4]) C
  aggregate S starting ({
    A: null,
    B: null,
    C: null,
    D: null
  } as Tuple {
    A Boolean,
    B Boolean,
    C Boolean,
    D Boolean
  }):
  case
    when C > 1 then S.A is true
    when C = 1 then S.B is true
    when C < 1 then S.C is true
    when C = 0 then S.D is true
    else S
  end

But I'm getting this syntax error: Expected an expression of type 'System.Boolean', but found an expression of type 'tuple of A: System.Boolean, B: System.Boolean, C: System.Boolean, D: System.Boolean'.

I thought that by specifying the starting value as the tuple the cql would expect the tuple as a response. Any help? Thoughts on what I'm getting wrong here?

view this post on Zulip Lloyd McKenzie (Oct 29 2021 at 17:41):

@Bryn Rhodes

view this post on Zulip Rebecca Green (Oct 29 2021 at 18:36):

I think i understand the error. because I am returning a boolean and when with S.A is true. It seems that cql doesn't allow you do multiple thing after then ideally i would write assign S.A to true then return S but not sure that's possible.

view this post on Zulip Bryn Rhodes (Oct 29 2021 at 19:28):

Yes, the aggregate clause is letting you specify the accumulating expression, and optionally the starting expression. In this case you've specified the starting expression as a tuple { A: Any, B: Any, C: Any, D: Any }, so that determines the shape of the accumulator, and it's expecting the accumulating expression to return that same shape.

view this post on Zulip Bryn Rhodes (Oct 29 2021 at 19:30):

In theory, a case expression could return a choice of all the types of all the then expressions and the else expression, but the translator takes the more conservative approach of using the type of the first case to determine the expression, and then seeing if there are implicit conversions to/from the type of all the conditions. So the case expression will do some promotion, but not a completely general choice like that, and it falls back to a type error on the else condition.

view this post on Zulip Bryn Rhodes (Oct 29 2021 at 19:30):

@Rebecca Green

view this post on Zulip Rebecca Green (Oct 29 2021 at 19:39):

Thanks! That makes sense. Do you know if there's a way to update the value in the Tuple and return the Tuple itself in one line? So for example that same code would look something like this:

define function AggregateFunction():
  (expand Interval[1, 4]) C
  aggregate S starting ({
    A: null,
    B: null,
    C: null,
    D: null
  } as Tuple {
    A Boolean,
    B Boolean,
    C Boolean,
    D Boolean
  }):
  case
    when C > 1 then S.A is true return S
    when C = 1 then S.B is true return S
    when C < 1 then S.C is true return S
    when C = 0 then S.D is true return S
    else S
  end

That way I wouldn't get a Type error and could keep track of what conditions of have been updated.


Last updated: Apr 12 2022 at 19:14 UTC