# AQL: Support for functions **Category:** [AQL](https://discourse.openehr.org/c/aql/43) **Created:** 2020-11-27 16:11 UTC **Views:** 585 **Replies:** 8 **URL:** https://discourse.openehr.org/t/aql-support-for-functions/1122 --- ## Post #1 by @sebastian.iancu in relation with adding support for more function, we created this https://openehr.atlassian.net/browse/SPECQUERY-28. 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. Comments? --- ## Post #2 by @thomas.beale I'm adding these kinds of things to the [Base Types spec](https://specifications.openehr.org/releases/BASE/latest/base_types.html#_builtins_package), since we need them in many places - AQL is just one. In the end, we need just one [Expression Language (EL)](https://specifications.openehr.org/releases/LANG/latest/expression_language.html) 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. --- ## Post #3 by @sebastian.iancu 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. --- ## Post #4 by @thomas.beale [quote="sebastian.iancu, post:3, topic:1122"] `primary_language` [/quote] 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. --- ## Post #5 by @sebastian.iancu Any new feedback on this topic, suggestions for core-functions? --- ## Post #6 by @bna 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 () Returns max () Returns the highest / largest value based on the input parameter specified. min() 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. sum() Returns the sum of the input parameter specified. tag () Returns the composition tag with the input parameter specified. --- ## Post #7 by @bna 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 ``` =============================================================== RuntimeProperties =============================================================== 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: ``` select 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' ``` --- ## Post #8 by @sebastian.iancu 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). --- ## Post #9 by @bna 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. --- **Canonical:** https://discourse.openehr.org/t/aql-support-for-functions/1122 **Original content:** https://discourse.openehr.org/t/aql-support-for-functions/1122