AQL: Support for functions

in relation with adding support for more function, we created this [SPECQUERY-28] Add support for built-in functions - openEHR JIRA.

One of the first thing I wanted to clarify is the situation of current-date() and now() function still mentioned, not described in the specs. Does any of the AQL implementation supports anything like this?

Somewhere (forgot where) I saw a comment about function being supported as current_date(), as the dash - sign might be tricky to be allowed, easily confused with the minus - operator.


I’m adding these kinds of things to the Base Types spec, since we need them in many places - AQL is just one.

In the end, we need just one Expression Language (EL) for everything. There can be more than one syntax flavour, but the point of the EL spec is to standardise one language semantics for the whole of openEHR. AQL only needs a very small subset of this. Those class definitions in Base just add specific pre-defined types and functions that may be used in EL expressions.

Thanks, the base functions are good ones, I think it makes sense and they are useful to have in AQL

  • current_date()
  • current_time()
  • current_date_time()
  • time_zone() => is it an idea to name this as current_timezone() as usually timezone() is used to convert a given time according to a timezone
  • primary_language() => usually this is named using ‘locale’ or ‘lc’, but explicit ‘primary’ is oalso ok IMO
  • sum, min, ‘max’ we already have; mean is avg(); mode we don’t have it yet on our AQL aggregation functions; count is missing on that interface, but I guess it is conceptually the one on the container itself

I’m still hoping for more feedback from AQL implementers.

Maybe this one needs discussion - I’m not sure what the best name is, or if we need more functions. I would think we need a function to return the default language of the locale, but we might also need a way to set and retrieve a language that acts as the primary for the current environment. E.g. in Catalonia, Spain, a system could have Catalan as its default language, or (at least in principle) Spanish as the default, even thought Catalan is probably the default for the region in a generic geographical sense.

The other suggestions I like - I’ll do them.

Any new feedback on this topic, suggestions for core-functions?

We support the following function:

average ()

Returns the average of the input parameter specified.

count ()

Counts the number of elements specified as input parameters to the function.

current-date ()

Used to compare the current date with the previous or future date.

current-date-time ()

Used to compare current date and time with past or future date and time.

ehr-uri ()

Returns an ehr-uri with EHR-id and composition-id based on the element that the function takes in.

locatable-ref ()


max ()

Returns the highest / largest value based on the input parameter specified.


Returns the lowest / smallest value based on the input parameter specified.

relative-ehr-uri ()

Returns an ehr-uri with composition ID based on the element that the function takes in.


Returns the sum of the input parameter specified.

tag ()

Returns the composition tag with the input parameter specified.

And then we created some functions to handle INSTRUCTION/ACTIVITY/INDEX:

current-action ()

Returns current action from an activity

current-state ()

Returns the current state of an activity

instruction-aggregate-state ()

Returns aggregate state from an instruction.

These we are not that happy with. We are now working on some new patterns for this: runtime_properties


    VERSIONED_OBJECT [StorageId, Server, CacheTime]
           --- VERSION [Tags, StorageId, ETag]
                    --- FOLDER/EHR_STATUS
                    --- COMPOSITION
                            --- INSTRUCTION [AGGREGATED_STATE]
                                    --- ACTIVITY (1) [STATE]
                                    --- ACTIVITY (2) [STATE]

Which makes it possible to query like this:

current-state(ac) as OldFunction, 
ac/runtime_properties/state/value as NewRuntimeProperty
from composition c contains instruction i contains activity ac where ac/runtime_properties/state/value = 'active'

Nice list @bna, thanks.

Will it be a (big) problem for your implementation if we change the use of dash with underscore in function names?
So instead of current-date() will use current_date(), instead of xx-yy() will use xx_yy(). This would be needed to align with other QL languages, but also to avoid matching the - operator (diff).

We will add a synonym for the snake cased functions and keep the old ones for backward compability. Not to much work. It’s OK.