Clinical Quality Language Release 1 STU 3 (1.3)

This is Clinical Quality Language STU3 (1.3) in it's permanent home (it will always be available at this URL). It has been superseded by 1.4.
For a full list of available versions, see the Directory of published versions .

Clinical Decision Support Work GroupMaturity Level: 4Ballot Status: STU 3

Appendix B – CQL Reference

Table of Contents

This appendix provides a reference for all the system-defined types, operators, and functions that can be used within CQL. It is intended to provide complete semantics for each available type and operator as a companion to the Author’s and Developer’s Guides. The reference is organized by operator category.

For each type, the definition and semantics are provided. Note that because CQL does not define a type declaration syntax, the definitions are expressed in a pseudo-syntax.

For each operator or function, the signature, semantics, and usually an example are provided. Note that for built-in operators, the signature is expressed in a pseudo-syntax intended to clearly define the operator and its parameters. Although the symbolic operators may in general be prefix, infix, or postfix operators, the signatures for each operator are defined using function definition syntax for consistency and ease of representation. For example, the signature for the and operator is given as:

and(left Boolean, right Boolean) Boolean

Even though and is an infix operator and would be invoked as in the following expression:

InDemographic and NeedsScreening

In addition, formatting strings are used throughout the reference to describe possible string formats for operators such as ToDateTime that convert a string to a date/time value. The following symbols are used to describe format strings:

  • 0 - Any digit must appear at this location in the format string

  • # - Any digit may appear at this location in the format string

  • ? - The immediately preceding pattern is optional

  • ( ) - Used to group patterns

  • | - Used to combine choices of patterns (e.g. +|- means a + or - may appear at this location)

Any other character in a format string indicates that that character must appear at that location (or may appear if it is modified by the optional indicator, or part of a group that is modified by the optional indicator), with the exception of the following patterns used when describing date/time format strings:

  • YYYY - A full four digit year, padded with leading zeroes if necessary

  • MM - A full two digit month value, padded with leading zeroes if necessary

  • DD - A full two digit day value, padded with leading zeroes if necessary

  • HH - A full two digit hour value (00..24), padded with leading zeroes if necessary

  • mm - A full two digit minute value (00..59), padded with leading zeroes if necessary

  • ss - A full two digit second value (00..59), padded with leading zeroes if necessary

  • ffff - A fractional millisecond value (0..999)

1. Types

1.1. Any

Definition:

simple type Any

Description:

The Any type is the maximal supertype in the CQL type system, meaning that all types derive from Any, including list, interval, and structured types. In addition, the type of a null result is Any.

1.2. Boolean

Definition:

simple type Boolean

Description:

The Boolean type represents the logical boolean values true and false. The result of logical operations within CQL use the Boolean type, and constructs within the language that expect a conditional result, such as a where clause or conditional expression, expect results of the Boolean type.

For more information, see the Logical Operators section.

1.3. Code

Definition:

structured type Code
{
  code String,
  display String,
  system String,
  version String
}

Description:

The Code type represents single terminology codes within CQL.

1.4. Concept

Definition:

structured type Concept
{
  codes List<Code>,
  display String
}

Description:

The Concept type represents a single terminological concept within CQL.

1.5. Date

Definition:

simple type Date

Description:

The Date type represents date values with potential uncertainty within CQL.

CQL supports date values in the range @0001-01-01 to @9999-12-31 with a 1 day step size.

CQL also supports partial date values. For example, the date @2014 represents some day in the year 2014.

1.6. DateTime

Definition:

simple type DateTime

Description:

The DateTime type represents date and time values with potential uncertainty within CQL.

CQL supports date and time values in the range @0001-01-01T00:00:00.0 to @9999-12-31T23:59:59.999 with a 1 millisecond step size. Note that DateTime values may also optionally indicate an offset.

CQL also supports partial date/time values. For example, the datetime @2014-01-01T03 represents some instant during the hour of 3:00 on January 1st, 2014.

1.7. Decimal

Definition:

simple type Decimal

Description:

The Decimal type represents real values within CQL.

CQL supports positive and negative decimal values with a precision (meaning total number of possible digits) of 28 and a scale (meaning number of possible digits to the right of the decimal) of 8. In other words, decimal values in the range (-1028 + 1)/108 to (1028-1)/10-8 with a step size of 10-8.

1.8. Integer

Definition:

simple type Integer

Description:

The Integer type represents whole number values within CQL.

CQL supports integer values in the range -231 to 231-1 with a step size of 1.

1.9. Quantity

Definition:

structured type Quantity
{
  value Decimal
  unit String
}

Description:

The Quantity type represents quantities with a specified unit within CQL. The unit must be a valid UCUM unit or CQL temporal keyword. UCUM units in CQL use the case-sensitive (c/s) form. When a quantity value has no unit specified, operations are performed with the default UCUM unit ('1'). The value element of a Quantity must be present.

1.9.1. Ratio

Definition:

structured type Ratio
{
  numerator Quantity
  denominator Quantity
}

Description:

The Ratio type represents a relationship between two quantities, such as a titre (e.g. 1:128), or a concentration (e.g. 5 'mg':10’mL'). The numerator and denominator elements must be present (i.e. can not be null).

1.10. String

Definition:

simple type String

Description:

The String type represents string values within CQL.

CQL supports string values up to 231-1 characters in length.

For string literals, CQL uses standard escape sequences:

Escape Character

\'

Single-quote

\"

Double-quote

\r

Carriage Return

\n

Line Feed

\t

Tab

\f

Form Feed

\\

Backslash

\uXXXX

Unicode character, where XXXX is the hexadecimal representation of the character

1.11. Time

Definition:

simple type Time

Description:

The Time type represents time-of-day values within CQL.

CQL supports time values in the range @T00:00:00.0 to @T23:59:59.999 with a step size of 1 millisecond. Not that Time values may also optionally indicate an offset.

CQL also supports partial time values. For example, the time @T03 represents some instant during the hour of 3:00.

2. Logical Operators

2.1. And

Signature:

and (left Boolean, right Boolean) Boolean

Description:

The and operator returns true if both its arguments are true. If either argument is false, the result is false. Otherwise, the result is null.

The following table defines the truth table for this operator:

TRUE FALSE NULL

TRUE

TRUE

FALSE

NULL

FALSE

FALSE

FALSE

FALSE

NULL

NULL

FALSE

NULL

Table 9‑A

Example:

The following examples illustrate the behavior of the and operator:

define IsTrue: true and true
define IsFalse: true and false
define IsAlsoFalse: false and null
define IsNull: true and null

Note that CQL does not prescribe short-circuit evaluation of logical operators.

2.2. Implies

Signature:

implies (left Boolean, right Boolean) Boolean

Description:

The implies operator returns the logical implication of its arguments. This means that if the left operand evaluates to true, this operator returns the boolean evaluation of the right operand. If the left operand evaluates to false, this operator returns true. Otherwise, this operator returns true if the right operand evaluates to true, and null otherwise.

The following table defines the truth table for this operator:

TRUE FALSE NULL

TRUE

TRUE

FALSE

NULL

FALSE

TRUE

TRUE

TRUE

NULL

TRUE

NULL

NULL

Table 9‑A

Example:

The following examples illustrate the behavior of the implies operator:

define IsTrue: false implies false
define IsAlsoTrue: false implies null
define IsFalse: true implies false
define IsNull: true implies null

2.3. Not

Signature:

not (argument Boolean) Boolean

Description:

The not operator returns true if the argument is false and false if the argument is true. Otherwise, the result is null.

The following table defines the truth table for this operator:

NOT

TRUE

FALSE

FALSE

TRUE

NULL

NULL

Table 9‑B

The following examples illustrate the behavior of the not operator:

define IsTrue: not false
define IsFalse: not true
define IsNull: not null

2.4. Or

Signature:

or (left Boolean, right Boolean) Boolean

Description:

The or operator returns true if either of its arguments are true. If both arguments are false, the result is false. Otherwise, the result is null.

The following table defines the truth table for this operator:

TRUE FALSE NULL

TRUE

TRUE

TRUE

TRUE

FALSE

TRUE

FALSE

NULL

NULL

TRUE

NULL

NULL

Table 9‑C

Example:

The following examples illustrate the behavior of the or operator:

define IsTrue: true or false
define IsAlsoTrue: true or null
define IsFalse: false or false
define IsNull: false or null

Note that CQL does not prescribe short-circuit evaluation of logical operators.

2.5. Xor

Signature:

xor (left Boolean, right Boolean) Boolean

Description:

The xor (exclusive or) operator returns true if one argument is true and the other is false. If both arguments are true or both arguments are false, the result is false. Otherwise, the result is null.

The following table defines the truth table for this operator:

TRUE FALSE NULL

TRUE

FALSE

TRUE

NULL

FALSE

TRUE

FALSE

NULL

NULL

NULL

NULL

NULL

Table 9‑D

The following examples illustrate the behavior of the xor operator:

define IsTrue: true xor false
define IsAlsoTrue: false xor true
define IsFalse: true xor true
define IsNull: true xor null

3. Type Operators

3.1. As

Signature:

as<T>(argument Any) T
cast as<T>(argument Any) T

Description:

The as operator allows the result of an expression to be cast as a given target type. This allows expressions to be written that are statically typed against the expected run-time type of the argument.

If the argument is not of the specified type at run-time the result is null.

The cast prefix indicates that if the argument is not of the specified type at run-time then an exception is thrown.

Example:

The following examples illustrate the use of the as operator.

define AllProcedures: [Procedure]

define ImagingProcedures:
  AllProcedures P
    where P is ImagingProcedure
    return P as ImagingProcedure

define RuntimeError:
  ImagingProcedures P
    return cast P as Observation

3.2. Children

Signature:

Children(argument Any) List<Any>

Description:

For structured types, the Children operator returns a list of all the values of the elements of the type. List-valued elements are expanded and added to the result individually, rather than as a single list.

For list types, the result is the same as invoking Children on each element in the list and flattening the resulting lists into a single result.

If the source is null, the result is null.

3.3. Convert

Signature:

convert to<T>(argument Any) T

Description:

The convert operator converts a value to a specific type. The result of the operator is the value of the argument converted to the target type, if possible. If there is no valid conversion from the actual value to the target type, the result is null.

The following table lists the conversions supported in CQL:

From\To Boolean Integer Decimal Quantity Ratio String Date DateTime Time Code Concept List<Code>

Boolean

N/A

-

-

-

-

Explicit

-

-

-

-

-

-

Integer

-

N/A

Implicit

Implicit

-

Explicit

-

-

-

-

-

-

Decimal

-

-

N/A

Implicit

-

Explicit

-

-

-

-

-

-

Quantity

-

-

-

N/A

-

Explicit

-

-

-

-

-

-

Ratio

-

-

-

-

N/A

Explicit

-

-

-

-

-

-

String

Explicit

Explicit

Explicit

Explicit

Explicit

N/A

Explicit

Explicit

Explicit

-

-

-

Date

-

-

-

-

-

Explicit

N/A

Implicit

-

-

-

-

DateTime

-

-

-

-

-

Explicit

Explicit

N/A

-

-

-

-

Time

-

-

-

-

-

Explicit

-

-

N/A

-

-

-

Code

-

-

-

-

-

-

-

-

-

N/A

Implicit

-

Concept

-

-

-

-

-

-

-

-

-

-

N/A

Explicit

List<Code>

Explicit

N/A

Table 9‑E

For conversions between date/time and string values, ISO-8601 standard format is used:

yyyy-MM-ddThh:mm:ss.fff(Z | +/- hh:mm)

For example, the following are valid string representations for date/time values:

'2014-01-01T14:30:00.0Z' // January 1st, 2014, 2:30PM UTC
'2014-01-01T14:30:00.0-07:00' // January 1st, 2014, 2:30PM Mountain Standard (GMT-7:00)
'T14:30:00.0Z' // 2:30PM UTC
'T14:30:00.0-07:00' // 2:30PM Mountain Standard (GMT-7:00)

For specific semantics for each conversion, refer to the explicit conversion operator documentation.

3.4. Descendents

Signature:

Descendents(argument Any) List<Any>

Description:

For structured types, the Descendents operator returns a list of all the values of the elements of the type, recursively. List-valued elements are expanded and added to the result individually, rather than as a single list.

For list types, the result is the same as invoking Descendents on each element in the list and flattening the resulting lists into a single result.

If the source is null, the result is null.

3.5. Is

Signature:

is<T>(argument Any) Boolean

Description:

The is operator allows the type of a result to be tested. If the run-time type of the argument is of the type being tested, the result of the operator is true; otherwise, the result is false.

The following examples illustrate the behavior of the is operator:

define IsTrue: 5 is Integer
define IsFalse: '5' is Integer

3.6. ConvertsToBoolean

Signature:

ConvertsToBoolean(argument String) Boolean

Description:

The ConvertsToBoolean operator returns true if its argument can be converted to a Boolean value. See the ToBoolean operator for a description of the supported conversions.

If the input cannot be interpreted as a valid Boolean value, the result is false.

If the argument is null, the result is null.

3.7. ConvertsToDate

Signature:

ConvertsToDate(argument String) Boolean

Description:

The ConvertsToDate operator returns true if its argument can be converted to a Date value. See the ToDate operator for a description of the supported conversions.

If the input string is not formatted correctly, or does not represent a valid date value, the result is false.

As with date literals, date values may be specified to any precision.

If the argument is null, the result is null.

3.8. ConvertsToDateTime

Signature:

ConvertsToDateTime(argument Date) Boolean
ConvertsToDateTime(argument String) Boolean

Description:

The ConvertsToDateTime operator returns true if its argument can be converted to a DateTime value. See the ToDateTime operator for a description of the supported conversions.

If the input string is not formatted correctly, or does not represent a valid date/time value, the result is false.

As with date/time literals, date/time values may be specified to any precision. If no timezone offset is supplied, the timezone offset of the evaluation request timestamp is assumed.

If the argument is null, the result is null.

3.9. ConvertsToDecimal

Signature:

ConvertsToDecimal(argument String) Boolean

Description:

The ToDecimal operator returns true if its argument can be converted to a Decimal value. See the ToDecimal operator for a description of the supported conversions.

If the input string is not formatted correctly, or cannot be interpreted as a valid Decimal value, the result is false.

If the argument is null, the result is null.

3.10. ConvertsToInteger

Signature:

ConvertsToInteger(argument String) Boolean

Description:

The ConvertsToInteger operator returns true if its argument can be converted to an Integer value. See the ToInteger operator for a description of the supported conversions.

If the input string is not formatted correctly, or cannot be interpreted as a valid Integer value, the result is false.

3.11. ConvertsToQuantity

Signature:

ConvertsToQuantity(argument Decimal) Boolean
ConvertsToQuantity(argument Integer) Boolean
ConvertsToQuantity(argument String) Boolean

Description:

The ConvertsToQuantity operator returns true if its argument can be converted to a Quantity value. See the ToQuantity operator for a description of the supported conversions.

If the input string is not formatted correctly, or cannot be interpreted as a valid Quantity value, the result is false.

If the argument is null, the result is null.

3.11.1. ConvertsToRatio

Signature:

ConvertsToRatio(argument String) Boolean

Description:

The ConvertsToRatio operator returns true if its argument can be converted to a Ratio value. See the ToRatio operator for a description of the supported conversions.

If the input string is not formatted correctly, or cannot be interpreted as a valid Ratio value, the result is false.

If the argument is null, the result is null.

3.12. ConvertsToString

Signature:

ConvertsToString(argument Boolean) Boolean
ConvertsToString(argument Integer) Boolean
ConvertsToString(argument Decimal) Boolean
ConvertsToString(argument Quantity) Boolean
ConvertsToString(argument Ratio) Boolean
ConvertsToString(argument Date) Boolean
ConvertsToString(argument DateTime) Boolean
ConvertsToString(argument Time) Boolean

Description:

The ConvertsToString operator returns true if its argument can be converted to a String value. See the ToString operator for a description of the supported conversions.

If the argument is null, the result is null.

3.13. ConvertsToTime

Signature:

ConvertsToTime(argument String) Time

Description:

The ConvertsToTime operator returns true if its argument can be converted to a Time value. See the ToTime operator for a description of the supported conversions.

If the input string is not formatted correctly, or does not represent a valid time-of-day value, the result is false.

If the argument is null, the result is null.

3.14. ToBoolean

Signature:

ToBoolean(argument String) Boolean

Description:

The ToBoolean operator converts the value of its argument to a Boolean value. The operator accepts the following string representations:

String Representation Boolean Value

true t yes y 1

true

false f no n 0

false

Table 9‑F

Note that the operator will ignore case when interpreting the string as a Boolean value.

If the input cannot be interpreted as a valid Boolean value, the result is null.

If the argument is null, the result is null.

The following examples illustrate the behavior of the ToBoolean operator:

define IsTrue: ToBoolean('y')
define IsFalse: ToBoolean('0')
define IsNull: ToBoolean('falsetto')

3.15. ToConcept

Signature:

ToConcept(argument Code) Concept

Description:

The ToConcept operator converts a value of type Code to a Concept value with the given Code as its primary and only Code. If the Code has a display value, the resulting Concept will have the same display value.

If the argument is null, the result is null.

The following examples illustrate the behavior of the ToConcept operator:

define IsValid: ToConcept(Code { system: 'http://loinc.org', code: '8480-6' }) // Concept { codes: { Code { system: 'http://loinc.org', code: '8480-6' } } }
define IsNull: ToConcept(null as Code)

3.16. ToDate

Signature:

ToDate(argument String) DateTime

Description:

The ToDate operator converts the value of its argument to a Date value. The operator expects the string to be formatted using the ISO-8601 date representation:

YYYY-MM-DD

In addition, the string must be interpretable as a valid date value.

For example, the following are valid string representations for date values:

'2014-01' // January, 2014
'2014-01-01' // January 1st, 2014

If the input string is not formatted correctly, or does not represent a valid date value, the result is null.

As with date literals, date values may be specified to any precision.

If the argument is null, the result is null.

3.17. ToDateTime

Signature:

ToDateTime(argument Date) DateTime
ToDateTime(argument String) DateTime

Description:

The ToDateTime operator converts the value of its argument to a DateTime value.

For the string overload, the operator expects the string to be formatted using the ISO-8601 date/time representation:

YYYY-MM-DDThh:mm:ss.fff(+|-)hh:mm or YYYY-MM-DDThh:mm:ss.fffZ

In addition, the string must be interpretable as a valid date/time value.

For example, the following are valid string representations for date/time values:

'2014-01-01' // January 1st, 2014
'2014-01-01T14:30:00.0Z' // January 1st, 2014, 2:30PM UTC
'2014-01-01T14:30:00.0-07:00' // January 1st, 2014, 2:30PM Mountain Standard (GMT-7:00)

If the input string is not formatted correctly, or does not represent a valid date/time value, the result is null.

As with date/time literals, date/time values may be specified to any precision. If no timezone offset is supplied, the timezone offset of the evaluation request timestamp is assumed.

For the Date overload, the result will be a DateTime with the time components set to zero, except for the timezone offset, which will be set to the timezone offset of the evaluation request timestamp.

If the argument is null, the result is null.

3.18. ToDecimal

Signature:

ToDecimal(argument String) Decimal

Description:

The ToDecimal operator converts the value of its argument to a Decimal value. The operator accepts strings using the following format:

(+|-)?#0(.0#)?

Meaning an optional polarity indicator, followed by any number of digits (including none), followed by at least one digit, followed optionally by a decimal point, at least one digit, and any number of additional digits (including none).

Note that the decimal value returned by this operator must be limited in precision and scale to the maximum precision and scale representable for Decimal values within CQL.

If the input string is not formatted correctly, or cannot be interpreted as a valid Decimal value, the result is null.

If the argument is null, the result is null.

The following examples illustrate the behavior of the ToDecimal operator:

define IsValid: ToDecimal('-0.1')
define IsNull: ToDecimal('+-0.1')

3.19. ToInteger

Signature:

ToInteger(argument String) Integer

Description:

The ToInteger operator converts the value of its argument to an Integer value. The operator accepts strings using the following format:

(+|-)?#0

Meaning an optional polarity indicator, followed by any number of digits (including none), followed by at least one digit.

Note that the integer value returned by this operator must be a valid value in the range representable for Integer values in CQL.

If the input string is not formatted correctly, or cannot be interpreted as a valid Integer value, the result is null.

If the argument is null, the result is null.

The following examples illustrate the behavior of the ToInteger operator:

define IsValid: ToInteger('-1')
define IsNull: ToInteger('one')

3.20. ToQuantity

Signature:

ToQuantity(argument Decimal) Quantity
ToQuantity(argument Integer) Quantity
ToQuantity(argument String) Quantity

Description:

The ToQuantity operator converts the value of its argument to a Quantity value.

For the String overload, the operator accepts strings using the following format:

(+|-)?#0(.0#)?('<unit>')?

Meaning an optional polarity indicator, followed by any number of digits (including none) followed by at least one digit, optionally followed by a decimal point, at least one digit, and any number of additional digits, all optionally followed by a unit designator as a string literal specifying a valid, case-sensitive UCUM unit of measure. Spaces are allowed between the quantity value and the unit designator.

Note that the decimal value of the quantity returned by this operator must be a valid value in the range representable for Decimal values in CQL.

If the input string is not formatted correctly, or cannot be interpreted as a valid Quantity value, the result is null.

For the Integer and Decimal overloads, the operator returns a quantity with the value of the argument and a unit of '1' (the default unit).

If the argument is null, the result is null.

The following examples illustrate the behavior of the ToQuantity operator:

define DecimalOverload: ToQuantity(0.1) // 0.1 '1'
define IntegerOverload: ToQuantity(13) // 13 '1'
define StringOverload: ToQuantity('-0.1 \'mg\'') // -0.1 'mg'
define IsNull: ToQuantity('444 \'cm')

3.20.1. ToRatio

Signature:

ToRatio(argument String) Ratio

Description:

The ToRatio operator converts the value of its argument to a Ratio value. The operator accepts strings using the following format:

<quantity>:<quantity>

where <quantity> is the format used to by the ToQuantity operator.

If the input string is not formatted correctly, or cannot be interpreted as a valid Ratio value, the result is null.

If the argument is null, the result is null.

The following examples illustrate the behavior of the ToRatio operator:

define IsValid: ToRatio('1.0 \'mg\':2.0 \'mg\'')
define IsNull: ToRatio('1.0 \'mg\';2.0 \'mg\'')

3.21. ToString

Signature:

ToString(argument Boolean) String
ToString(argument Integer) String
ToString(argument Decimal) String
ToString(argument Quantity) String
ToString(argument Ratio) String
ToString(argument Date) String
ToString(argument DateTime) String
ToString(argument Time) String

Description:

The ToString operator converts the value of its argument to a String value. The operator uses the following string representations for each type:

Type String Representation

Boolean

true|false

Integer

(-)?#0

Decimal

(-)?#0.0#

Quantity

(-)?#0.0# '<unit>'

Ratio

<quantity>:<quantity>

Date

YYYY-MM-DD

DateTime

YYYY-MM-DDThh:mm:ss.fff(+|-)hh:mm

Time

Thh:mm:ss.fff(+|-)hh:mm

Table 9‑G

The result of any ToString must be round-trippable back to the source value.

If the argument is null, the result is null.

The following examples illustrate the behavior of the ToString operator:

define BooleanOverload: ToString(true)
define IntegerOverload: ToString(13)
define DecimalOverload: ToString(0.1)
define QuantityOverload: ToString(-0.1 'mg')
define RatioOverload: ToString(-0.1 'mg':0.1 'mg')
define DateOverload: ToString(@2012-01-01)
define DateTimeOverload: ToString(@2012-01-01T12:30:00.000-05:00)
define TimeOverload: ToString(@T12:30:00.000-05:00)
define IsNull: ToString(null as Integer)

3.22. ToTime

Signature:

ToTime(argument String) Time

Description:

The ToTime operator converts the value of its argument to a Time value. The operator expects the string to be formatted using ISO-8601 time representation:

Thh:mm:ss.fff(+|-)hh:mm or Thh:mm:ss.fffZ

In addition, the string must be interpretable as a valid time-of-day value.

For example, the following are valid string representations for time-of-day values:

'T14:30:00.0Z' // 2:30PM UTC
'T14:30:00.0-07:00' // 2:30PM Mountain Standard (GMT-7:00)

If the input string is not formatted correctly, or does not represent a valid time-of-day value, the result is null.

As with time-of-day literals, time-of-day values may be specified to any precision. If no timezone offset is supplied, the timezone offset of the evaluation request timestamp is assumed.

If the argument is null, the result is null.

4. Nullological Operators

4.1. Coalesce

Signature:

Coalesce<T>(argument1 T, argument2 T) T
Coalesce<T>(argument1 T, argument2 T, argument3 T) T
Coalesce<T>(argument1 T, argument2 T, argument3 T, argument4 T) T
Coalesce<T>(argument1 T, argument2 T, argument3 T, argument4 T, argument5 T) T
Coalesce<T>(arguments List<T>) T

Description:

The Coalesce operator returns the first non-null result in a list of arguments. If all arguments evaluate to null, the result is null.

The static type of the first argument determines the type of the result, and all subsequent arguments must be of that same type.

The following examples illustrate the behavior of the Coalesce operator:

define Coalesce15: Coalesce(null, 15, null)
define IsNull: Coalesce({ null, null, null })
define CoalesceError: Coalesce(null, 15, null, null, null, null) // more than 5 inputs must be represented as list

4.2. IsNull

Signature:

is null(argument Any) Boolean

Description:

The is null operator determines whether or not its argument evaluates to null. If the argument evaluates to null, the result is true; otherwise, the result is false.

The following examples illustrate the behavior of the is null operator:

define IsTrue: null is null
define IsFalse: true is null

4.3. IsFalse

Signature:

is false(argument Boolean) Boolean

Description:

The is false operator determines whether or not its argument evaluates to false. If the argument evaluates to false, the result is true; otherwise, the result is false.

The following examples illustrate the behavior of the is false operator:

define IsFalseIsTrue: false is false
define IsFalseIsFalse: null is false

4.4. IsTrue

Signature:

is true(argument Boolean) Boolean

Description:

The is true operator determines whether or not its argument evaluates to true. If the argument evaluates to true, the result is true; otherwise, the result is false.

The following examples illustrate the behavior of the is true operator:

define IsTrueIsTrue: true is true
define IsTrueIsFalse: false is true

5. Comparison Operators

5.1. Between

Signature:

between(argument Integer, low Integer, high Integer) Boolean
between(argument Decimal, low Decimal, high Decimal) Boolean
between(argument Quantity, low Quantity, high Quantity) Boolean
between(argument Date, low Date, high Date) Boolean
between(argument DateTime, low DateTime, high DateTime) Boolean
between(argument Time, low Time, high Time) Boolean
between(argument String, low String, high String) Boolean

Description:

The between operator determines whether the first argument is within a given range, inclusive. If the first argument is greater than or equal to the low argument, and less than or equal to the high argument, the result is true, otherwise, the result is false.

For comparisons involving quantities, the dimensions of each quantity must be the same, but not necessarily the unit. For example, units of 'cm' and 'm' are comparable, but units of 'cm2' and 'cm' are not. Attempting to operate on quantities with invalid units will result in a null.

For comparisons involving date/time values with imprecision, note that the result of the comparison may be null, depending on whether the values involved are specified to the level of precision used for the comparison. For example:

define DateTimeBetweenIsNull: @2012-01-01 between @2012-01-01T12 and @2012-01-02T12

String comparisons are strictly lexical based on the Unicode value of the individual characters in the string.

If any argument is null, the result is null.

The following examples illustrate the behavior of the between operator:

define IntegerBetweenIsTrue: 4 between 2 and 6
define DecimalBetweenIsFalse: 3.5 between 3.6 and 4.8
define QuantityBetweenIsNull: 3.5 'cm2' between 3.0 'cm' and 4.8 'cm'

5.2. Equal

Signature:

=<T>(left T, right T) Boolean

Description:

The equal (=) operator returns true if the arguments are equal; false if the arguments are known unequal, and null otherwise. Equality semantics are defined to be value-based.

For simple types, this means that equality returns true if and only if the result of each argument evaluates to the same value.

For string values, equality is strictly lexical based on the Unicode values for the individual characters in the strings.

For decimal values, trailing zeroes are ignored.

For quantities, this means that the dimensions of each quantity must be the same, but not necessarily the unit. For example, units of 'cm' and 'm' are comparable, but units of 'cm2' and 'cm' are not. Attempting to operate on quantities with invalid units will result in a null. When a quantity has no units specified, it is treated as a quantity with the default unit ('1').

For tuple types, this means that equality returns true if and only if the tuples are of the same type, and the values for all elements that have values, by name, are equal.

For list types, this means that equality returns true if and only if the lists contain elements of the same type, have the same number of elements, and for each element in the lists, in order, the elements are equal using equality semantics.

For interval types, equality returns true if and only if the intervals are over the same point type, and they have the same value for the starting and ending points of the interval as determined by the Start and End operators.

For date/time values, the comparison is performed by considering each precision in order, beginning with years (or hours for time values). If the values are the same, comparison proceeds to the next precision; if the values are different, the comparison stops and the result is false. If one input has a value for the precision and the other does not, the comparison stops and the result is null; if neither input has a value for the precision, or the last precision has been reached, the comparison stops and the result is true.

If either argument is null, the result is null.

The following examples illustrate the behavior of the equal operator:

define IntegerEqualIsTrue: 4 = (2 + 2)
define DecimalEqualIsFalse: 3.5 = (3.5 - 0.1)
define StringEqualIsFalse: 'CQL is awesome!' = 'cql is awesome!'
define QuantityEqualIsNull: 3.5 'cm2' = 3.5 'cm'
define ListEqualIsNull: { null, 1, 2, 3 } = { null, 1, 2, 3 }
define DateTimeEqualIsNull: @2012-01-01 = @2012-01-01T12
define NullEqualIsNull: null = null

5.3. Equivalent

Signature:

~<T>(left T, right T) Boolean

Description:

The equivalent (~) operator returns true if the arguments are equivalent in value, or if they are both null; and false otherwise.

For string values, equivalence is case- and locale-insensitive.

For tuple types, this means that two tuple values are equivalent if and only if the tuples are of the same type, and the values for all elements by name are equivalent.

For list types, this means that two list values are equivalent if and only if the lists contain elements of the same type, have the same number of elements, and for each element in the lists, in order, the elements are equivalent.

For interval types, this means that two intervals are equivalent if and only if the intervals are over the same point type, and the starting and ending points of the intervals as determined by the Start and End operators are equivalent.

For date/time values, the comparison is performed in the same way as it is for equality, except that if one input has a value for a given precision and the other does not, the comparison stops and the result is false, rather than null.

For Code values, equivalence is defined based on the code and system elements only. The version and display elements are ignored for the purposes of determining Code equivalence.

For Concept values, equivalence is defined as a non-empty intersection of the codes in each Concept.

Note that this operator will always return true or false, even if either or both of its arguments are null, or contain null components.

The following examples illustrate the behavior of the equivalent operator:

define IntegerEquivalentIsTrue: 4 ~ (2 + 2)
define DecimalEquivalentIsFalse: 3.5 ~ (3.5 - 0.1)
define StringEquivalentIsTrue: 'CQL is awesome!' ~ 'cql is awesome!'
define QuantityEquivalentIsFalse: 3.5 'cm2' ~ 3.5 'cm'
define ListEquivalentIsTrue: { null, 1, 2, 3 } ~ { null, 1, 2, 3 }
define DateTimeEquivalentIsFalse: @2012-01-01 ~ @2012-01-01T12
define NullEquivalentIsTrue: null ~ null

5.4. Greater

Signature:

>(left Integer, right Integer) Boolean
>(left Decimal, right Decimal) Boolean
>(left Quantity, right Quantity) Boolean
>(left Date, right Date) Boolean
>(left DateTime, right DateTime) Boolean
>(left Time, right Time) Boolean
>(left String, right String) Boolean

Description:

The greater (>) operator returns true if the first argument is greater than the second argument.

String comparisons are strictly lexical based on the Unicode value of the individual characters in the string.

For comparisons involving quantities, the dimensions of each quantity must be the same, but not necessarily the unit. For example, units of 'cm' and 'm' are comparable, but units of 'cm2' and 'cm' are not. Attempting to operate on quantities with invalid units will result in a null. When a quantity has no units specified, it is treated as a quantity with the default unit ('1').

For date/time values, the comparison is performed by considering each precision in order, beginning with years (or hours for time values). If the values are the same, comparison proceeds to the next precision; if the first value is greater than the second, the result is true; if the first value is less than the second, the result is false; if one input has a value for the precision and the other does not, the comparison stops and the result is null; if neither input has a value for the precision or the last precision has been reached, the comparison stops and the result is false. For example:

define DateTimeGreaterIsNull: @2012-01-01 > @2012-01-01T12

If either argument is null, the result is null.

The following examples illustrate the behavior of the greater operator:

define IntegerGreaterIsTrue: 4 > 3
define DecimalGreaterIsFalse: 3.5 > 3.5
define QuantityGreaterIsNull: 3.6 'cm2' > 3.5 'cm'
define NullGreaterIsNull: null > 5

5.5. Greater Or Equal

Signature:

>=(left Integer, right Integer) Boolean
>=(left Decimal, right Decimal) Boolean
>=(left Quantity, right Quantity) Boolean
>=(left Date, right Date) Boolean
>=(left DateTime, right DateTime) Boolean
>=(left Time, right Time) Boolean
>=(left String, right String) Boolean

Description:

The greater or equal (>=) operator returns true if the first argument is greater than or equal to the second argument.

String comparisons are strictly lexical based on the Unicode value of the individual characters in the string.

For comparisons involving quantities, the dimensions of each quantity must be the same, but not necessarily the unit. For example, units of 'cm' and 'm' are comparable, but units of 'cm2' and 'cm' are not. Attempting to operate on quantities with invalid units will result in a null. When a quantity has no units specified, it is treated as a quantity with the default unit ('1').

For date/time values, the comparison is performed by considering each precision in order, beginning with years (or hours for time values). If the values are the same, comparison proceeds to the next precision; if the first value is greater than the second, the result is true; if the first value is less than the second, the result is false; if one input has a value for the precision and the other does not, the comparison stops and the result is null; if neither input has a value for the precision or the last precision has been reached, the comparison stops and the result is true. For example:

define DateTimeGreaterOrEqualIsNull: @2012-01-01 >= @2012-01-01T12

If either argument is null, the result is null.

The following examples illustrate the behavior of the greater or equal operator:

define IntegerGreaterOrEqualIsTrue: 4 >= (2 + 2)
define DecimalGreaterOrEqualIsFalse: 3.5 >= (3.5 + 0.1)
define QuantityGreaterOrEqualIsNull: 3.6 'cm2' >= 3.5 'cm'
define NullGreaterOrEqualIsNull: null >= 5

5.6. Less

Signature:

<(left Integer, right Integer) Boolean
<(left Decimal, right Decimal) Boolean
<(left Quantity, right Quantity) Boolean
<(left Date, right Date) Boolean
<(left DateTime, right DateTime) Boolean
<(left Time, right Time) Boolean
<(left String, right String) Boolean

Description:

The less (<) operator returns true if the first argument is less than the second argument.

String comparisons are strictly lexical based on the Unicode value of the individual characters in the string.

For comparisons involving quantities, the dimensions of each quantity must be the same, but not necessarily the unit. For example, units of 'cm' and 'm' are comparable, but units of 'cm2' and 'cm' are not. Attempting to operate on quantities with invalid units will result in a null. When a quantity has no units specified, it is treated as a quantity with the default unit ('1').

For date/time values, the comparison is performed by considering each precision in order, beginning with years (or hours for time values). If the values are the same, comparison proceeds to the next precision; if the first value is less than the second, the result is true; if the first value is greater than the second, the result is false; if one input has a value for the precision and the other does not, the comparison stops and the result is null; if neither input has a value for the precision or the last precision has been reached, the comparison stops and the result is false. For example:

define DateTimeLessIsNull: @2012-01-01 < @2012-01-01T12

If either argument is null, the result is null.

The following examples illustrate the behavior of the less operator:

define IntegerLessIsTrue: 4 < (2 + 2 + 2)
define DecimalLessIsFalse: 3.5 < 3.5
define QuantityLessIsNull: 3.6 'cm2' < 3.5 'cm'
define DateTimeLessIsNull: @2012-01-01 < @2012-01-01T12
define NullLessIsNull: null < 5

5.7. Less Or Equal

Signature:

<=(left Integer, right Integer) Boolean
<=(left Decimal, right Decimal) Boolean
<=(left Quantity, right Quantity) Boolean
<=(left Date, right Date) Boolean
<=(left DateTime, right DateTime) Boolean
<=(left Time, right Time) Boolean
<=(left String, right String) Boolean

Description:

The less or equal (<=) operator returns true if the first argument is less than or equal to the second argument.

String comparisons are strictly lexical based on the Unicode value of the individual characters in the string.

For comparisons involving quantities, the dimensions of each quantity must be the same, but not necessarily the unit. For example, units of 'cm' and 'm' are comparable, but units of 'cm2' and 'cm' are not. Attempting to operate on quantities with invalid units will result in a null. When a quantity has no units specified, it is treated as a quantity with the default unit ('1').

For date/time values, the comparison is performed by considering each precision in order, beginning with years (or hours for time values). If the values are the same, comparison proceeds to the next precision; if the first value is less than the second, the result is true; if the first value is greater than the second, the result is false; if one input has a value for the precision and the other does not, the comparison stops and the result is null; if neither input has a value for the precision or the last precision has been reached, the comparison stops and the result is true. For example:

define DateTimeLessOrEqualIsNull: @2012-01-01 <= @2012-01-01T12

If either argument is null, the result is null.

The following examples illustrate the behavior of the less or equal operator:

define IntegerLessOrEqualIsTrue: 4 <= (2 + 2)
define DecimalLessOrEqualIsFalse: 3.5 <= (3.5 - 0.1)
define QuantityLessOrEqualIsNull: 3.6 'cm2' <= 3.6 'cm'
define NullLessOrEqualIsNull: null <= 5

5.8. Not Equal

Signature:

!=<T>(left T, right T) Boolean

Description:

The not equal (!=) operator returns true if its arguments are not the same value.

The not equal operator is a shorthand for invocation of logical negation (not) of the equal operator.

The following examples illustrate the behavior of the not equal operator:

define IntegerNotEqualIsFalse: 4 != (2 + 2)
define DecimalNotEqualIsTrue: 3.5 != (3.5 - 0.1)
define StringNotEqualIsTrue: 'CQL is awesome!' != 'cql is awesome!'
define QuantityNotEqualIsNull: 3.5 'cm2' != 3.5 'cm'
define ListNotEqualIsNull: { null, 1, 2, 3 } != { null, 1, 2, 3 }
define DateTimeNotEqualIsNull: @2012-01-01 != @2012-01-01T12
define NullNotEqualIsNull: null != null

5.9. Not Equivalent

Signature:

!~<T>(left T, right T) Boolean

Description:

The not equivalent (!~) operator returns true if its arguments are not equivalent.

The not equivalent operator is a shorthand for invocation of logical negation (not) of the equivalent operator.

The following examples illustrate the behavior of the not equivalent operator:

define IntegerNotEquivalentIsFalse: 4 !~ (2 + 2)
define DecimalNotEquivalentIsTrue: 3.5 !~ (3.5 - 0.1)
define StringNotEquivalentIsFalse: 'CQL is awesome!' !~ 'cql is awesome!'
define QuantityNotEquivalentIsTrue: 3.5 'cm2' !~ 3.5 'cm'
define ListNotEquivalentIsFalse: { null, 1, 2, 3 } !~ { null, 1, 2, 3 }
define DateTimeNotEquivalentIsTrue: @2012-01-01 !~ @2012-01-01T12
define NullNotEquivalentIsFalse: null !~ null

6. Arithmetic Operators

6.1. Abs

Signature:

Abs(argument Integer) Integer
Abs(argument Decimal) Decimal
Abs(argument Quantity) Quantity

Description:

The Abs operator returns the absolute value of its argument.

When taking the absolute value of a quantity, the unit is unchanged.

If the argument is null, the result is null.

The following examples illustrate the behavior of the Abs operator:

define IntegerAbs: Abs(-5) // 5
define IntegerAbsIsNull: Abs(null as Integer)
define DecimalAbs: Abs(-5.5) // 5.5
define QuantityAbs: Abs(-5.5 'mg') // 5.5 'mg'

6.2. Add

Signature:

+(left Integer, right Integer) Integer
+(left Decimal, right Decimal) Decimal
+(left Quantity, right Quantity) Quantity

Description:

The add (+) operator performs numeric addition of its arguments.

When invoked with mixed Integer and Decimal arguments, the Integer argument will be implicitly converted to Decimal.

When adding quantities, the dimensions of each quantity must be the same, but not necessarily the unit. For example, units of 'cm' and 'm' can be added, but units of 'cm2' and 'cm' cannot. The unit of the result will be the most granular unit of either input. Attempting to operate on quantities with invalid units will result in a null. When a quantity has no units specified, it is treated as a quantity with the default unit ('1').

If either argument is null, the result is null.

The following examples illustrate the behavior of the add operator:

define IntegerAdd: 2 + 2 // 4
define IntegerAddIsNull: 2 + null
define DecimalAdd: 2.5 + 5 // 7.5
define QuantityAdd: -5.5 'mg' + 2 'mg' // -3.5 'mg'
define QuantityAddIsNull: -5.5 'cm' + 2 'cm2'

6.3. Ceiling

Signature:

Ceiling(argument Decimal) Integer

Description:

The Ceiling operator returns the first integer greater than or equal to the argument.

When invoked with an Integer argument, the argument will be implicitly converted to Decimal.

If the argument is null, the result is null.

The following examples illustrate the behavior of the Ceiling operator:

define IntegerCeiling: Ceiling(1) // 1
define DecimalCeiling: Ceiling(1.1) // 2
define QuantityCeilingIsNull: Ceiling(null as Decimal)

6.4. Divide

Signature:

/(left Decimal, right Decimal) Decimal
/(left Quantity, right Quantity) Quantity

Description:

The divide (/) operator performs numeric division of its arguments. Note that this operator is Decimal division; for Integer division, use the truncated divide (div) operator.

When invoked with Integer arguments, the arguments will be implicitly converted to Decimal.

When invoked with a mixture of Decimal and Quantity arguments, the Decimal arguments will be implicitly converted to Quantity.

For division operations involving quantities, the resulting quantity will have the appropriate unit. For example:

12 'cm2' / 3 'cm'

In this example, the result will have a unit of 'cm'. Note that when performing division of quantities with the same units, the result will have the default UCUM unit ('1'). When a quantity has no units specified, it is treated as a quantity with the default unit ('1').

If either argument is null, the result is null.

The following examples illustrate the behavior of the divide operator:

define IntegerDivide: 4.0 / 2 // 2.0
define DecimalDivide: 9.9 / 3.0 // 3.3
define DecimalDivideIsNull: 2.2 / null
define DecimalDivideIsError: 2.2 / 0
define QuantityDivide: -5.5 'mg' / 2.0 'mg' // -2.75

6.5. Floor

Signature:

Floor(argument Decimal) Integer

Description:

The Floor operator returns the first integer less than or equal to the argument.

When invoked with an Integer argument, the argument will be implicitly converted to Decimal.

If the argument is null, the result is null.

The following examples illustrate the behavior of the Floor operator:

define IntegerFloor: Floor(1) // 1
define DecimalFloor: Floor(2.1) // 2
define QuantityFloorIsNull: Floor(null as Decimal)

6.6. Exp

Signature:

Exp(argument Decimal) Decimal

Description:

The Exp operator raises e to the power of its argument.

When invoked with an Integer argument, the argument will be implicitly converted to Decimal.

If the argument is null, the result is null.

The following examples illustrate the behavior of the Exp operator:

define IntegerExp: Exp(0) // 1.0
define DecimalExp: Exp(-0.0) // 1.0
define QuantityExpIsNull: Exp(null as Decimal)

6.7. Log

Signature:

Log(argument Decimal, base Decimal) Decimal

Description:

The Log operator computes the logarithm of its first argument, using the second argument as the base.

When invoked with Integer arguments, the arguments will be implicitly converted to Decimal.

If either argument is null, the result is null.

The following examples illustrate the behavior of the Log operator:

define IntegerLog: Log(16, 2) // 4.0
define DecimalLog: Log(100.0, 10.0) // 2.0
define QuantityLogIsNull: Log(null, 10.0)

6.8. Ln

Signature:

Ln(argument Decimal) Decimal

Description:

The Ln operator computes the natural logarithm of its argument.

When invoked with an Integer argument, the argument will be implicitly converted to Decimal.

If the argument is null, the result is null.

The following examples illustrate the behavior of the Ln operator:

define IntegerLn: Ln(1) // 0.0
define DecimalLn: Ln(1.0) // 0.0
define QuantityLnIsNull: Ln(null as Decimal)

6.9. Maximum

Signature:

maximum<T>() T

Description:

The maximum operator returns the maximum representable value for the given type.

The maximum operator is defined for the Integer, Decimal, Date, DateTime, and Time types.

For Integer, maximum returns the maximum signed 32-bit integer, 231 - 1.

For Decimal, maximum returns the maximum representable decimal value, (1028 – 1) / 108 (99999999999999999999.99999999).

For Date, maximum returns the maximum representable date value, Date(9999, 12, 31).

For DateTime, maximum returns the maximum representable date/time value, DateTime(9999, 12, 31, 23, 59, 59, 999).

For Time, maximum returns the maximum representable time value, Time(23, 59, 59, 999).

For any other type, attempting to invoke maximum results in an error.

The following examples illustrate the behavior of the maximum operator:

define IntegerMaximum: maximum Integer // 2147483647
define DateTimeMaximum: maximum DateTime // @9999-12-31T23:59:59.999
define ErrorMaximum: maximum Quantity

6.10. Minimum

Signature:

minimum<T>() T

Description:

The minimum operator returns the minimum representable value for the given type.

The minimum operator is defined for the Integer, Decimal, Date, DateTime, and Time types.

For Integer, minimum returns the minimum signed 32-bit integer, -231.

For Decimal, minimum returns the minimum representable decimal value, (-1028 + 1) / 108 (-99999999999999999999.99999999).

For Date, minimum returns the minimum representable date value, DateTime(1, 1, 1).

For DateTime, minimum returns the minimum representable date/time value, DateTime(1, 1, 1, 0, 0, 0, 0).

For Time, minimum returns the minimum representable time value, Time(0, 0, 0, 0).

For any other type, attempting to invoke minimum results in an error.

The following examples illustrate the behavior of the minimum operator:

define IntegerMinimum: minimum Integer // -2147483648
define DateTimeMinimum: minimum DateTime // @0001-01-01T00:00:00.000
define ErrorMinimum: minimum Quantity

6.11. Modulo

Signature:

mod(left Integer, right Integer) Integer
mod(left Decimal, right Decimal) Decimal

Description:

The mod operator computes the remainder of the division of its arguments.

When invoked with mixed Integer and Decimal arguments, the Integer argument will be implicitly converted to Decimal.

If either argument is null, the result is null.

The following examples illustrate the behavior of the mod operator:

define IntegerModulo: 3 mod 2 // 1
define DecimalModulo: 2.5 mod 2 // 0.5
define ModuloIsNull: 2.5 mod null

6.12. Multiply

Signature:

*(left Integer, right Integer) Integer
*(left Decimal, right Decimal) Decimal
*(left Quantity, right Quantity) Quantity

Description:

The multiply (*) operator performs numeric multiplication of its arguments.

When invoked with mixed Integer and Decimal arguments, the Integer argument will be implicitly converted to Decimal.

When invoked with mixed Decimal and Quantity arguments, the Decimal argument will be implicitly converted to Quantity.

For multiplication operations involving quantities, the resulting quantity will have the appropriate unit. For example:

12 'cm' * 3 'cm'
3 'cm' * 12 'cm2'

In this example, the first result will have a unit of 'cm2', and the second result will have a unit of 'cm3'. When a quantity has no units specified, it is treated as a quantity with the default unit ('1').

If either argument is null, the result is null.

6.13. Negate

Signature:

-(argument Integer) Integer
-(argument Decimal) Decimal
-(argument Quantity) Quantity

Description:

The negate (-) operator returns the negative of its argument.

When negating quantities, the unit is unchanged.

If the argument is null, the result is null.

The following examples illustrate the behavior of the negate operator:

define IntegerNegate: -3 // -3
define DecimalNegate: -(-3.3) // 3.3
define QuantityNegate: -3.3 'mg' // -3.3 'mg'
define NegateIsNull: -(null as Integer)

6.14. Predecessor

Signature:

predecessor of<T>(argument T) T

Description:

The predecessor operator returns the predecessor of the argument. For example, the predecessor of 2 is 1. If the argument is already the minimum value for the type, a run-time error is thrown.

The predecessor operator is defined for the Integer, Decimal, Quantity, Date, DateTime, and Time types.

For Integer, predecessor is equivalent to subtracting 1.

For Decimal, predecessor is equivalent to subtracting the minimum precision value for the Decimal type, or 10-08.

For Date, DateTime, and Time values, predecessor is equivalent to subtracting a time-unit quantity for the lowest specified precision of the value. For example, if the DateTime is fully specified, predecessor is equivalent to subtracting 1 millisecond; if the DateTime is specified to the second, predecessor is equivalent to subtracting one second, etc.

For Quantity values, the predecessor is equivalent to subtracting 1 if the quantity is an integer, and the minimum precision value for the Decimal type if the quantity is a decimal. The units are unchanged.

If the argument is null, the result is null.

The following examples illustrate the behavior of the predecessor operator:

define IntegerPredecessor: predecessor of 100 // 99
define DecimalPredecessor: predecessor of 1.0 // 0.99999999
define DatePredecessor: predecessor of @2014-01-01 // @2013-12-31
define PredecessorIsNull: predecessor of (null as Quantity)

6.15. Power

Signature:

^(argument Integer, exponent Integer) Integer
^(argument Decimal, exponent Decimal) Decimal

Description:

The power (^) operator raises the first argument to the power given by the second argument.

When invoked with mixed Integer and Decimal arguments, the Integer argument will be implicitly converted to Decimal.

If either argument is null, the result is null.

The following examples illustrate the behavior of the power operator:

define IntegerPower: 2^3 // 8
define IntegerPowerFun: Power(2, 3) // 8
define DecimalPower: 2.5^2.0 // 6.25
define NegateIsNull: 2.5^null

6.16. Round

Signature:

Round(argument Decimal) Decimal
Round(argument Decimal, precision Integer) Decimal

Description:

The Round operator returns the nearest whole number to its argument. The semantics of round are defined as a traditional round, meaning that a decimal value of 0.5 or higher will round to 1.

When invoked with an Integer argument, the argument will be implicitly converted to Decimal.

If the argument is null, the result is null.

Precision determines the decimal place at which the rounding will occur. If precision is not specified or null, 0 is assumed.

The following examples illustrate the behavior of the Round operator:

define IntegerRound: Round(1) // 1
define DecimalRound: Round(3.14159, 3) // 3.142
define RoundIsNull: Round(null)

6.17. Subtract

Signature:

-(left Integer, right Integer) Integer
-(left Decimal, right Decimal) Decimal
-(left Quantity, right Quantity) Quantity

Description:

The subtract (-) operator performs numeric subtraction of its arguments.

When invoked with mixed Integer and Decimal arguments, the Integer argument will be implicitly converted to Decimal.

When subtracting quantities, the dimensions of each quantity must be the same, but not necessarily the unit. For example, units of 'cm' and 'm' can be subtracted, but units of 'cm2' and 'cm' cannot. The unit of the result will be the most granular unit of either input. Attempting to operate on quantities with invalid units will result in a null. When a quantity has no units specified, it is treated as a quantity with the default unit ('1').

If either argument is null, the result is null.

The following examples illustrate the behavior of the subtract operator:

define IntegerSubtract: 2 - 1 // 1
define DecimalSubtract: 3.14 - 3.12 // 0.02
define QuantitySubtract: 3.14 'mg' - 3.12 'mg' // 0.02 'mg'
define QuantitySubtractError: 3.14 'cm' - 3.12 'cm2'
define SubtractIsNull: 3 - null

6.18. Successor

Signature:

successor of<T>(argument T) T

Description:

The successor operator returns the successor of the argument. For example, the successor of 1 is 2. If the argument is already the maximum value for the type, a run-time error is thrown.

The successor operator is defined for the Integer, Quantity, Decimal, Date, DateTime, and Time types.

For Integer, successor is equivalent to adding 1.

For Decimal, successor is equivalent to adding the minimum precision value for the Decimal type, or 10-08.

For Date, DateTime and Time values, successor is equivalent to adding a time-unit quantity for the lowest specified precision of the value. For example, if the DateTime is fully specified, successor is equivalent to adding 1 millisecond; if the DateTime is specified to the second, successor is equivalent to adding one second, etc.

For Quantity values, the successor is equivalent to adding 1 if the quantity is an integer, and the minimum precision value for the Decimal type if the quantity is a decimal. The units are unchanged.

If the argument is null, the result is null.

The following examples illustrate the behavior of the successor operator:

define IntegerSuccessor: successor of 100 // 101
define DecimalSuccessor: successor of 1.0 // 1.00000001
define DateSuccessor: successor of @2014-01-01 // @2014-01-02
define SuccessorIsNull: successor of (null as Quantity)

6.19. Truncate

Signature:

Truncate(argument Decimal) Integer

Description:

The Truncate operator returns the integer component of its argument.

When invoked with an Integer argument, the argument will be implicitly converted to Decimal.

If the argument is null, the result is null.

The following examples illustrate the behavior of the Truncate operator:

define IntegerTruncate: Truncate(101) // 101
define DecimalTruncate: Truncate(1.00000001) // 1
define TruncateIsNull: Truncate(null)

6.20. Truncated Divide

Signature:

div(left Integer, right Integer) Integer
div(left Decimal, right Decimal) Decimal

Description:

The div operator performs truncated division of its arguments.

When invoked with mixed Integer and Decimal arguments, the Integer argument will be implicitly converted to Decimal.

If either argument is null, the result is null.

The following examples illustrate the behavior of the div operator:

define IntegerTruncatedDivide: 4 div 2 // 2
define DecimalTruncatedDivide: 4.14 div 2.06 // 2
define TruncatedDivideIsNull: 3 div null

7. String Operators

7.1. Combine

Signature:

Combine(source List<String>) String
Combine(source List<String>, separator String) String

Description:

The Combine operator combines a list of strings, optionally separating each string with the given separator.

If either argument is null, or any element in the source list of strings is null, the result is null.

The following examples illustrate the behavior of the Combine operator:

define CombineList: Combine({ 'super', 'califragilistic', 'expialidocious' }) // 'supercalifragilisticexpialidocious'
define CombineWithSeparator: Combine({ 'CQL', 'is', 'awesome' }, ' ') // 'CQL is awesome'
define CombineIsNull: Combine({ 'the', 'next', 'arg', 'is', null })

7.2. Concatenate

Signature:

+(left String, right String) String
&(left String, right String) String

Description:

The concatenate (+ or &) operator performs string concatenation of its arguments.

When using +, if either argument is null, the result is null.

When using &, null arguments are treated as an empty string ('').

The following examples illustrate the behavior of the concatenate operator:

define ConcatenatePlus: 'CQL is' + ' awesome' // 'CQL is awesome'
define ConcatenateAnd: 'CQL is' & ' awesome' // 'CQL is awesome'
define ConcatenateFun: Concatenate('CQL is', ' awesome') // 'CQL is awesome'
define ConcatenateIsNull: 'CQL is' + null

7.3. EndsWith

Signature:

EndsWith(argument String, suffix String) Boolean

Description:

The EndsWith operator returns true if the given string ends with the given suffix.

If the suffix is the empty string, the result is true.

If either argument is null, the result is null.

The following examples illustrate the behavior of the EndsWith operator:

define EndsWithIsTrue: EndsWith('CQL is awesome', 'some')
define EndsWithIsFalse: EndsWith('CQL is awesome', 'Some')
define EndsWithIsNull: EndsWith('CQL is awesome', null)

7.4. Indexer

Signature:

[](argument String, index Integer) String

Description:

The indexer ([]) operator returns the character at the indexth position in a string.

Indexes in strings are defined to be 0-based.

If either argument is null, the result is null.

If the index is greater than the length of the string being indexed, the result is null.

The following examples illustrate the behavior of the indexer operator:

define Indexer2: 'CQL is awesome'[2] // 'L'
define IndexerIsNull: 'CQL is awesome'[14]
define IndexerIsAlsoNull: 'CQL is awesome'[null]

7.5. LastPositionOf

Signature:

LastPositionOf(pattern String, argument String) Integer

Description:

The LastPositionOf operator returns the 0-based index of the last appearance of the given pattern in the given string.

If the pattern is not found, the result is -1.

If either argument is null, the result is null.

The following examples illustrate the behavior of the LastPositionOf operator:

define LastPositionOfFound: LastPositionOf('awesome', 'CQL is awesome') // 7
define LastPositionOfNotFound: LastPositionOf('Some', 'CQL is awesome') // -1
define LastPositionOfIsNull: LastPositionOf(null, 'CQL is awesome')

7.6. Length

Signature:

Length(argument String) Integer

Description:

The Length operator returns the number of characters in a string.

If the argument is null, the result is null.

The following examples illustrate the behavior of the Length operator:

define Length14: Length('CQL is awesome') // 14
define LengthIsNull: Length(null as String)

7.7. Lower

Signature:

Lower(argument String) String

Description:

The Lower operator returns the given string with all characters converted to their lower case equivalents.

Note that the definition of lowercase for a given character is a locale-dependent determination, and is not specified by CQL. Implementations are expected to provide appropriate and consistent handling of locale for their environment.

If the argument is null, the result is null.

The following examples illustrate the behavior of the Lower operator:

define LowerCQL: Lower('CQL is awesome') // 'cql is awesome'
define LowerIsNull: Lower(null)

7.8. Matches

Signature:

Matches(argument String, pattern String) Boolean

Description:

The Matches operator returns true if the given string matches the given regular expression pattern. Regular expressions should function consistently, regardless of any culture- and locale-specific settings in the environment, should be case-sensitive, use single line mode, and allow Unicode characters.

If either argument is null, the result is null.

Platforms will typically use native regular expression implementations. These are typically fairly similar, but there will always be small differences. As such, CQL does not prescribe a particular dialect, but recommends the use of the dialect defined as part of XML Schema 1.1 as the dialect most likely to be broadly supported and understood.

The following examples illustrate the behavior of the Matches operator:

define MatchesTrue: Matches('1,2three', '\\d,\\d\\w+')
define MatchesFalse: Matches('1,2three', '\\w+')
define MatchesIsNull: Matches('12three', null)

7.9. PositionOf

Signature:

PositionOf(pattern String, argument String) Integer

Description:

The PositionOf operator returns the 0-based index of the given pattern in the given string.

If the pattern is not found, the result is -1.

If either argument is null, the result is null.

The following examples illustrate the behavior of the PositionOf operator:

define PositionOfFound: PositionOf('s', 'CQL is awesome') // 5
define PositionOfNotFound: PositionOf('\\d', 'CQL is awesome') // -1
define PositionOfIsNull: PositionOf(null, 'CQL is awesome')

7.10. ReplaceMatches

Signature:

Matches(argument String, pattern String, substitution String) String

Description:

The ReplaceMatches operator matches the given string using the given regular expression pattern, replacing each match with the given substitution. The substitution string may refer to identified match groups in the regular expression. Regular expressions should function consistently, regardless of any culture- and locale-specific settings in the environment, should be case-sensitive, use single line mode, and allow Unicode characters.

If any argument is null, the result is null.

Platforms will typically use native regular expression implementations. These are typically fairly similar, but there will always be small differences. As such, CQL does not prescribe a particular dialect, but recommends the use of the dialect defined as part of XML Schema 1.1 as the dialect most likely to be broadly supported and understood.

The following examples illustrate the behavior of the ReplaceMatches operator:

define ReplaceMatchesFound: ReplaceMatches('CQL is awesome', 'awesome', 'amazing') // 'CQL is amazing'
define ReplaceMatchesNotFound: ReplaceMatches('CQL is awesome', 'awesome!', 'amazing') // 'CQL is awesome'
define ReplaceMatchesIsNull: ReplaceMatches('CQL is awesome', 'awesome', null)

7.11. Split

Signature:

Split(stringToSplit String, separator String) List<String>

Description:

The Split operator splits a string into a list of strings using a separator.

If the stringToSplit argument is null, the result is null.

If the stringToSplit argument does not contain any appearances of the separator, the result is a list of strings containing one element that is the value of the stringToSplit argument.

The following examples illustrate the behavior of the Split operator:

define SplitFound: Split('CQL is awesome', ' ') // { 'CQL', 'is', 'awesome' }
define SplitNotFound: Split('CQL is awesome', ',') // { 'CQL is awesome' }
define SplitIsNull: Split(null, ' ')

7.12. SplitOnMatches

Signature:

Split(stringToSplit String, separatorPattern String) List<String>

Description:

The SplitOnMatches operator splits a string into a list of strings using a separator that is defined by a regular expression pattern.

If the stringToSplit argument is null, the result is null.

The separatorPattern argument is interpreted with the same regular expression semantics as defined for the Matches operator.

If the stringToSplit argument does not contain any matches for the separatorPattern, the result is a list of strings containing one element that is the value of the stringToSplit argument.

7.13. StartsWith

Signature:

StartsWith(argument String, prefix String) Boolean

Description:

The StartsWith operator returns true if the given string starts with the given prefix.

If the prefix is the empty string, the result is true.

If either argument is null, the result is null.

The following examples illustrate the behavior of the StartsWith operator:

define StartsWithIsTrue: StartsWith('CQL is awesome', 'CQL')
define StartsWithIsFalse: StartsWith('CQL is awesome', 'CQl')
define StartsWithIsNull: StartsWith('CQL is awesome', null)

7.14. Substring

Signature:

Substring(stringToSub String, startIndex Integer) String
Substring(stringToSub String, startIndex Integer, length Integer) String

Description:

The Substring operator returns the string within stringToSub, starting at the 0-based index startIndex, and consisting of length characters.

If length is omitted, the substring returned starts at startIndex and continues to the end of stringToSub.

If stringToSub or startIndex is null, or startIndex is out of range, the result is null.

The following examples illustrate the behavior of the Substring operator:

define SubstringWithoutLength: Substring('CQL is awesome', 7) // 'awesome'
define SubstringWithLength: Substring('CQL is awesome', 7, 3) // 'awe'
define SubstringIsNull: Substring('CQL is awesome', null)
define SubstringIsAlsoNull: Substring('CQL is awesome', 14)

7.15. Upper

Signature:

Upper(argument String) String

Description:

The Upper operator returns the given string with all characters converted to their upper case equivalents.

Note that the definition of uppercase for a given character is a locale-dependent determination, and is not specified by CQL. Implementations are expected to provide appropriate and consistent handling of locale for their environment.

If the argument is null, the result is null.

The following examples illustrate the behavior of the Upper operator:

define UpperCQL: Upper('cql IS AWESOME') // 'CQL IS AWESOME'
define UpperIsNull: Upper(null)

8. Date/Time Operators

8.1. Add

Signature:

+(left Date, right Quantity) Date
+(left DateTime, right Quantity) DateTime
+(left Time, right Quantity) Time

Description:

The add (+) operator returns the value of the given date/time, incremented by the time-valued quantity, respecting variable length periods for calendar years and months.

For Date values, the quantity unit must be one of: years, months, weeks, or days.

For DateTime values, the quantity unit must be one of: years, months, weeks, days, hours, minutes, seconds, or milliseconds.

For Time values, the quantity unit must be one of: hours, minutes, seconds, or milliseconds.

Note that the quantity units may be specified in singular, plural, or UCUM form.

The operation is performed by converting the time-based quantity to the most precise value specified in the date/time (truncating any resulting decimal portion) and then adding it to the date/time value. For example, the following addition:

DateTime(2014) + 24 months

This example results in the value DateTime(2016) even though the date/time value is not specified to the level of precision of the time-valued quantity.

Note also that this means that if decimals appear in the time-valued quantities, the fractional component will be ignored. For example, the following addition:

DateTime(2014) + 18 months

This example results in the value DateTime(2015)

If either argument is null, the result is null.

8.2. After

Signature:

after _precision_ of(left Date, right Date) Boolean
after _precision_ of(left DateTime, right DateTime) Boolean
after _precision_ of(left Time, right Time) Boolean

Description:

The after-precision-of operator compares two date/time values to the specified precision to determine whether the first argument is the after the second argument. The comparison is performed by considering each precision in order, beginning with years (or hours for time values). If the values are the same, comparison proceeds to the next precision; if the first value is greater than the second, the result is true; if the first value is less than the second, the result is false; if either input has no value for the precision, the comparison stops and the result is null; if the specified precision has been reached, the comparison stops and the result is false.

If no precision is specified, the comparison is performed beginning with years (or hours for time values) and proceeding to the finest precision specified in either input.

For Date values, precision must be one of: year, month, or day.

For DateTime values, precision must be one of: year, month, day, hour, minute, second, or millisecond.

For Time values, precision must be one of: hour, minute, second, or millisecond.

Note specifically that due to variability in the way week numbers are determined, comparisons involving weeks are not supported.

When this operator is called with both Date and DateTime inputs, the Date values will be implicitly converted to DateTime values as defined by the ToDateTime operator.

As with all date/time calculations, comparisons are performed respecting the timezone offset.

If either or both arguments are null, the result is null.

The following examples illustrate the behavior of the after-precision-of operator:

define AfterIsTrue: @2012-02-01 after month of @2012-01-01
define AfterIsFalse: @2012-01-01 after month of @2012-01-01
define AfterUncertainIsNull: @2012-01-01 after month of @2012
define AfterIsNull: @2012-01-01 after month of null

This operator is also defined for intervals, see the After (Intervals) operator for more information.

8.3. Before

Signature:

before _precision_ of(left Date, right Date) Boolean
before _precision_ of(left DateTime, right DateTime) Boolean
before _precision_ of(left Time, right Time) Boolean

Description:

The before-precision-of operator compares two date/time values to the specified precision to determine whether the first argument is the before the second argument. The comparison is performed by considering each precision in order, beginning with years (or hours for time values). If the values are the same, comparison proceeds to the next precision; if the first value is less than the second, the result is true; if the first value is greater than the second, the result is false; if either input has no value for the precision, the comparison stops and the result is null; if the specified precision has been reached, the comparison stops and the result is false.

If no precision is specified, the comparison is performed beginning with years (or hours for time values) and proceeding to the finest precision specified in either input.

For Date values, precision must be one of: year, month, or day.

For DateTime values, precision must be one of: year, month, day, hour, minute, second, or millisecond.

For Time values, precision must be one of: hour, minute, second, or millisecond.

Note specifically that due to variability in the way week numbers are determined, comparisons involving weeks are not supported.

When this operator is called with both Date and DateTime inputs, the Date values will be implicitly converted to DateTime values as defined by the ToDateTime operator.

As with all date/time calculations, comparisons are performed respecting the timezone offset.

If either or both arguments are null, the result is null.

The following examples illustrate the behavior of the before-precision-of operator:

define BeforeIsTrue: @2012-01-01 before month of @2012-02-01
define BeforeIsFalse: @2012-01-01 before month of @2012-01-01
define BeforeUncertainIsNull: @2012 before month of @2012-02-01
define BeforeIsNull: @2012-01-01 before month of null

This operator is also defined for intervals, see the Before (Intervals) operator for more information.

8.4. Date

Signature:

Date(year Integer) Date
Date(year Integer, month Integer) Date
Date(year Integer, month Integer, day Integer) Date

Description:

The Date operator constructs a date value from the given components.

At least one component must be specified, and no component may be specified at a precision below an unspecified precision. For example, month may be null, but if it is, day must be null as well.

The following examples illustrate the behavior of the Date operator:

define DateValid: Date(2012, 1, 1)
define DateInvalid: Date(2012, null, 1)

8.5. DateTime

Signature:

DateTime(year Integer) DateTime
DateTime(year Integer, month Integer) DateTime
DateTime(year Integer, month Integer, day Integer) DateTime
DateTime(year Integer, month Integer, day Integer,
  hour Integer) DateTime
DateTime(year Integer, month Integer, day Integer,
  hour Integer, minute Integer) DateTime
DateTime(year Integer, month Integer, day Integer,
  hour Integer, minute Integer, second Integer) DateTime
DateTime(year Integer, month Integer, day Integer,
  hour Integer, minute Integer, second Integer, millisecond Integer) DateTime
DateTime(year Integer, month Integer, day Integer,
  hour Integer, minute Integer, second Integer, millisecond Integer,
  timezoneOffset Decimal) DateTime

Description:

The DateTime operator constructs a date/time value from the given components.

At least one component other than timezoneOffset must be specified, and no component may be specified at a precision below an unspecified precision. For example, hour may be null, but if it is, minute, second, and millisecond must all be null as well.

If timezoneOffset is not specified, it is defaulted to the timezone offset of the evaluation request.

The following examples illustrate the behavior of the DateTime operator:

define DateValid: DateTime(2012, 1, 1, 12, 30, 0, 0, -7)
define DateInvalid: DateTime(2012, 1, 1, 12, null, 0, 0, -7)

8.6. Date/Time Component From

Signature:

_precision_ from(argument Date) Integer
_precision_ from(argument DateTime) Integer
_precision_ from(argument Time) Integer
timezone from(argument DateTime) Decimal
timezone from(argument Time) Decimal
date from(argument DateTime) Date
time from(argument DateTime) Time

Description:

The component-from operator returns the specified component of the argument.

For Date values, precision must be one of: year, month, or day.

For DateTime values, precision must be one of: year, month, day, hour, minute, second, or millisecond.

For Time values, precision must be one of: hour, minute, second, or millisecond.

Note specifically that due to variability in the way week numbers are determined, extraction of a week component is not supported.

If the argument is null, or is not specified to the level of precision being extracted, the result is null.

The following examples illustrate the behavior of the component-from operator:

define MonthFrom: month from DateTime(2012, 1, 1, 12, 30, 0, 0, -7) // 1
define TimeZoneFrom: timezone from DateTime(2012, 1, 1, 12, 30, 0, 0, -7) // -7.0
define DateFrom: date from DateTime(2012, 1, 1, 12, 30, 0, 0, -7) // @2012-01-01
define TimeFrom: time from DateTime(2012, 1, 1, 12, 30, 0, 0, -7) // @T12:30:00.000-07:00
define MonthFromIsNull: month from DateTime(2012)

8.7. Difference

Signature:

difference in _precision_ between(low Date, high Date) Integer
difference in _precision_ between(low DateTime, high DateTime) Integer
difference in _precision_ between(low Time, high Time) Integer

Description:

The difference-between operator returns the number of boundaries crossed for the specified precision between the first and second arguments. If the first argument is after the second argument, the result is negative. The result of this operation is always an integer; any fractional boundaries are dropped.

As with all date/time calculations, difference calculations are performed respecting the timezone offset depending on the precision.

For Date values, precision must be one of: years, months, weeks, or days.

For DateTime values, precision must be one of: years, months, weeks, days, hours, minutes, seconds, or milliseconds.

For Time values, precision must be one of: hours, minutes, seconds, or milliseconds.

For calculations involving weeks, Sunday is considered to be the first day of the week for the purposes of determining the number of boundaries crossed.

When this operator is called with both Date and DateTime inputs, the Date values will be implicitly converted to DateTime as defined by the ToDateTime operator.

If either argument is null, the result is null.

The following examples illustrate the behavior of the difference-between operator:

define DifferenceInMonths: difference in months between @2012-01-01 and @2012-02-01 // 1
define UncertainDifferenceInMonths: difference in months between @2012-01-02 and @2012 // [0, 11]
define DifferenceIsNull: difference in months between @2012-01-01 and null

8.8. Duration

Signature:

_duration_ between(low Date, high Date) Integer
_duration_ between(low DateTime, high DateTime) Integer
_duration_ between(low Time, high Time) Integer

Description:

The duration-between operator returns the number of whole calendar periods for the specified precision between the first and second arguments. If the first argument is after the second argument, the result is negative. The result of this operation is always an integer; any fractional periods are dropped.

For Date values, duration must be one of: years, months, weeks, or days.

For DateTime values, duration must be one of: years, months, weeks, days, hours, minutes, seconds, or milliseconds.

For Time values, duration must be one of: hours, minutes, seconds, or milliseconds.

When this operator is called with both Date and DateTime inputs, the Date values will be implicitly converted to DateTime as defined by the ToDateTime operator.

If either argument is null, the result is null.

The following examples illustrate the behavior of the duration-between operator:

define DurationInMonths: months between @2012-01-01 and @2012-02-01 // 1
define UncertainDurationInMonths: months between @2012-01-02 and @2012 // [0, 10]
define DurationIsNull: months between @2012-01-01 and null

8.9. Now

Signature:

Now() DateTime

Description:

The Now operator returns the date and time of the start timestamp associated with the evaluation request. Now is defined in this way for two reasons:

  1. The operation will always return the same value within any given evaluation, ensuring that the result of an expression containing Now will always return the same result.

  2. The operation will return the timestamp associated with the evaluation request, allowing the evaluation to be performed with the same timezone offset information as the data delivered with the evaluation request.

8.10. On Or After

Signature:

on or after _precision_ (left Date, right Date) Boolean
on or after _precision_ (left DateTime, right DateTime) Boolean
on or after _precision_ (left Time, right Time) Boolean

Description:

The on or after operator for date/time values is a synonym for the same or after operator and is supported to enable natural phrasing. See the description of the Same Or After (Date/Time) operator.

Note that this operator can be invoked using either the on or after or the after or on syntax.

In timing phrases, the keyword same is a synonym for on.

8.11. On Or Before

Signature:

on or before _precision_ (left Date, right Date) Boolean
on or before _precision_ (left DateTime, right DateTime) Boolean
on or before _precision_ (left Time, right Time) Boolean

Description:

The on or before operator for date/time values is a synonym for the same or before operator and is supported to enable natural phrasing. See the description of the Same Or Before (Date/Time) operator.

Note that this operator can be invoked using either the on or before or the before or on syntax.

In timing phrases, the keyword same is a synonym for on.

8.12. Same As

Signature:

same _precision_ as(left Date, right Date) Boolean
same _precision_ as(left DateTime, right DateTime) Boolean
same _precision_ as(left Time, right Time) Boolean

Description:

The same-precision-as operator compares two date/time values to the specified precision for equality. The comparison is performed by considering each precision in order, beginning with years (or hours for time values). If the values are the same, comparison proceeds to the next precision; if the values are different, the comparison stops and the result is false; if either input has no value for the precision, the comparison stops and the result is null; if the specified precision has been reached, the comparison stops and the result is true.

If no precision is specified, the comparison is performed beginning with years (or hours for time values) and proceeding to the finest precision specified in either input.

For Date values, precision must be one of: year, month, or day.

For DateTime values, precision must be one of: year, month, day, hour, minute, second, or millisecond.

For Time values, precision must be one of: hour, minute, second, or millisecond.

Note specifically that due to variability in the way week numbers are determined, comparisons involving weeks are not supported.

When this operator is called with both Date and DateTime inputs, the Date values will be implicitly converted to DateTime as defined by the ToDateTime operator.

As with all date/time calculations, comparisons are performed respecting the timezone offset.

If either or both arguments are null, the result is null.

The following examples illustrate the behavior of the same-precision-as operator:

define SameAsTrue: @2012-01-01 same day as @2012-01-01
define SameAsFalse: @2012-01-01 same day as @2012-01-02
define UncertainSameAsIsNull: @2012-01-01 same day as @2012-01
define SameAsIsNull: @2012-01-01 same day as null

This operator is also defined for intervals, see the Same As (Intervals) operator for more information.

8.13. Same Or After

Signature:

same _precision_ or after(left Date, right Date) Boolean
same _precision_ or after(left DateTime, right DateTime) Boolean
same _precision_ or after(left Time, right Time) Boolean

Description:

The same-precision-or after operator compares two date/time values to the specified precision to determine whether the first argument is the same or after the second argument. The comparison is performed by considering each precision in order, beginning with years (or hours for time values). If the values are the same, comparison proceeds to the next precision; if the first value is greater than the second, the result is true; if the first value is less than the second, the result is false; if either input has no value for the precision, the comparison stops and the result is null; if the specified precision has been reached, the comparison stops and the result is true.

If no precision is specified, the comparison is performed beginning with years (or hours for time values) and proceeding to the finest precision specified in either input.

For Date values, precision must be one of: year, month, or day.

For DateTime values, precision must be one of: year, month, day, hour, minute, second, or millisecond.

For Time values, precision must be one of: hour, minute, second, or millisecond.

Note specifically that due to variability in the way week numbers are determined, comparisons involving weeks are not supported.

When this operator is called with both Date and DateTime inputs, the Date values will be implicitly converted to DateTime as defined by the ToDateTime operator.

As with all date/time calculations, comparisons are performed respecting the timezone offset.

If either or both arguments are null, the result is null.

Note that in timing phrases, the keyword on may be used as a synonym for same for this operator.

The following examples illustrate the behavior of the same-precision-or after operator:

define SameOrAfterTrue: @2012-01-02 same day or after @2012-01-01
define SameOrAfterFalse: @2012-01-01 same day or after @2012-01-02
define UncertainSameOrAfterIsNull: @2012-01-02 same day or after @2012-01
define SameOrAfterIsNull: @2012-01-01 same day or after null

This operator is also defined for intervals, see the Same Or After (Intervals) operator for more information.

8.14. Same Or Before

Signature:

same _precision_ or before(left Date, right Date) Boolean
same _precision_ or before(left DateTime, right DateTime) Boolean
same _precision_ or before(left Time, right Time) Boolean

Description:

The same-precision-or before operator compares two date/time values to the specified precision to determine whether the first argument is the same or before the second argument. The comparison is performed by considering each precision in order, beginning with years (or hours for time values). If the values are the same, comparison proceeds to the next precision; if the first value is less than the second, the result is true; if the first value is greater than the second, the result is false; if either input has no value for the precision, the comparison stops and the result is null; if the specified precision has been reached, the comparison stops and the result is true.

If no precision is specified, the comparison is performed beginning with years (or hours for time values) and proceeding to the finest precision specified in either input.

For Date values, precision must be one of: year, month, or day.

For DateTime values, precision must be one of: year, month, day, hour, minute, second, or millisecond.

For Time values, precision must be one of: hour, minute, second, or millisecond.

Note specifically that due to variability in the way week numbers are determined, comparisons involving weeks are not supported.

When this operator is called with both Date and DateTime inputs, the Date values will be implicitly converted to DateTime as defined by the ToDateTime operator.

As with all date/time calculations, comparisons are performed respecting the timezone offset.

If either or both arguments are null, the result is null.

Note that in timing phrases, the keyword on may be used as a synonym for same for this operator.

The following examples illustrate the behavior of the same-precision-or before operator:

define SameOrBeforeTrue: @2012-01-01 same day or before @2012-01-02
define SameOrBeforeFalse: @2012-01-02 same day or before @2012-01-01
define UncertainSameOrBeforeIsNull: @2012-01-02 same day or before @2012-01
define SameOrBeforeIsNull: @2012-01-01 same day or before null

This operator is also defined for intervals, see the Same Or Before (Intervals) operator for more information.

8.15. Subtract

Signature:

-(left Date, right Quantity) Date
-(left DateTime, right Quantity) DateTime
-(left Time, right Quantity) Time

Description:

The subtract (-) operator returns the value of the given date/time, decremented by the time-valued quantity, respecting variable length periods for calendar years and months.

For Date values, the quantity unit must be one of: years, months, weeks, or days.

For DateTime values, the quantity unit must be one of: years, months, weeks, days, hours, minutes, seconds, or milliseconds.

For Time values, the quantity unit must be one of: hours, minutes, seconds, or milliseconds.

Note that the quantity units may be specified in singular, plural or UCUM form.

The operation is performed by converting the time-based quantity to the most precise value specified in the date/time (truncating any resulting decimal portion) and then subtracting it from the date/time value. For example, the following subtraction:

DateTime(2014) - 24 months

This example results in the value DateTime(2012) even though the date/time value is not specified to the level of precision of the time-valued quantity.

Note also that this means that if decimals appear in the time-valued quantities, the fractional component will be ignored. For example, the following subtraction:

DateTime(2014) - 18 months

This example results in the value DateTime(2013)

If either argument is null, the result is null.

8.16. Time

Signature:

Time(hour Integer) Time
Time(hour Integer, minute Integer) Time
Time(hour Integer, minute Integer, second Integer) Time
Time(hour Integer, minute Integer, second Integer, millisecond Integer) Time
Time(hour Integer, minute Integer, second Integer, millisecond Integer,
timezoneOffset Decimal) Time

Description:

The Time operator constructs a time value from the given components.

At least one component other than timezoneOffset must be specified, and no component may be specified at a precision below an unspecified precision. For example, minute may be null, but if it is, second, and millisecond must all be null as well.

If timezoneOffset is not specified, it is defaulted to the timezone offset of the evaluation request.

The following examples illustrate the behavior of the Time operator:

define TimeValid: Time(12, 30, 0, 0, -7)
define TimeInvalid: Time(12, null, 0, 0, -7)

8.17. TimeOfDay

Signature:

TimeOfDay() Time

Description:

The TimeOfDay operator returns the time of day of the start timestamp associated with the evaluation request. See the Now operator for more information on the rationale for defining the TimeOfDay operator in this way.

8.18. Today

Signature:

Today() Date

Description:

The Today operator returns the date of the start timestamp associated with the evaluation request. See the Now operator for more information on the rationale for defining the Today operator in this way.

9. Interval Operators

9.1. After

Signature:

after _precision_ (left Interval<T>, right Interval<T>) Boolean
after _precision_ (left T, right Interval<T>) Boolean
after _precision_ (left Interval<T>, right T) Boolean

Description:

The after operator for intervals returns true if the first interval starts after the second one ends. In other words, if the starting point of the first interval is greater than the ending point of the second interval.

For the point-interval overload, the operator returns true if the given point is greater than the end of the interval.

For the interval-point overload, the operator returns true if the given interval starts after the given point.

This operator uses the semantics described in the Start and End operators to determine interval boundaries.

If precision is specified and the point type is a date/time type, comparisons used in the operation are performed at the specified precision.

If either argument is null, the result is null.

The following examples illustrate the behavior of the after operator:

define AfterIsTrue: 5 after Interval[1, 4]
define AfterIsFalse: Interval[1, 4] after 5
define AfterIsNull: Interval[1, 4] after null

9.2. Before

Signature:

before _precision_ (left Interval<T>, right Interval<T>) Boolean
before _precision_ (left T, right Interval<T>) Boolean
before _precision_ (left interval<T>, right T) Boolean

Description:

The before operator for intervals returns true if the first interval ends before the second one starts. In other words, if the ending point of the first interval is less than the starting point of the second interval.

For the point-interval overload, the operator returns true if the given point is less than the start of the interval.

For the interval-point overload, the operator returns true if the given interval ends before the given point.

This operator uses the semantics described in the Start and End operators to determine interval boundaries.

If precision is specified and the point type is a date/time type, comparisons used in the operation are performed at the specified precision.

If either argument is null, the result is null.

The following examples illustrate the behavior of the before operator:

define BeforeIsTrue: 0 before Interval[1, 4]
define BeforeIsFalse: Interval[1, 4] before 0
define BeforeIsNull: Interval[1, 4] before null

9.3. Collapse

Signature:

collapse(argument List<Interval<T>>) List<Interval<T>>
collapse(argument List<Interval<T>>, per Quantity) List<Interval<T>>

Description:

The collapse operator returns the unique set of intervals that completely covers the ranges present in the given list of intervals. In other words, adjacent intervals within a sorted list are merged if they either overlap or meet.

Note that because the semantics for overlaps and meets are themselves defined in terms of the interval successor and predecessor operators, sets of date/time-based intervals that are only defined to a particular precision will calculate meets and overlaps at that precision. For example, a list of DateTime-based intervals where the boundaries are all specified to the hour will collapse at the hour precision, unless the collapse precision is overridden with the per argument.

The per argument determines the precision at which the collapse will be performed, and must be a quantity value that is compatible with the point type of the input intervals. For numeric intervals, this means a default unit ('1'). For date/time intervals, this means a temporal duration.

If the list of intervals is empty, the result is empty. If the list of intervals contains a single interval, the result is a list with that interval. If the list of intervals contains nulls, they will be excluded from the resulting list.

If the list argument is null, the result is null.

If the per argument is null, the default unit interval for the point type of the intervals involved will be used (i.e. the interval that has a width equal to the result of the successor function for the point type).

The following examples illustrate the behavior of the collapse operator:

define Collapse1To9: collapse { Interval[1, 4], Interval[4, 8], Interval[7, 9] } // { Interval[1, 9] }
define CollapseIsNull: collapse null

9.4. Contains

Signature:

contains _precision_ (argument Interval<T>, point T) Boolean

Description:

The contains operator for intervals returns true if the given point is greater than or equal to the starting point of the interval, and less than or equal to the ending point of the interval. For open interval boundaries, exclusive comparison operators are used. For closed interval boundaries, if the interval boundary is null, the result of the boundary comparison is considered true.

If precision is specified and the point type is a date/time type, comparisons used in the operation are performed at the specified precision.

If either argument is null, the result is null.

The following examples illustrate the behavior of the contains operator:

define ContainsIsTrue: Interval[1, 5] contains 4
define ContainsIsFalse: Interval[1, 5] contains 6
define ContainsIsNull: Interval[1, 5] contains null

9.5. End

Signature:

end of(argument Interval<T>) T

Description:

The End operator returns the ending point of an interval.

If the high boundary of the interval is open, this operator returns the predecessor of the high value of the interval. Note that if the high value of the interval is null, the result is null.

If the high boundary of the interval is closed and the high value of the interval is not null, this operator returns the high value of the interval. Otherwise, the result is the maximum value of the point type of the interval.

If the argument is null, the result is null.

The following examples illustrate the behavior of the End operator:

define EndOfInterval: end of Interval[1, 5] // 5
define EndIsNull: end of (null as Interval<Integer>)

9.6. Ends

Signature:

ends _precision_ (left Interval<T>, right Interval<T>) Boolean

Description:

The ends operator returns true if the first interval ends the second. More precisely, if the starting point of the first interval is greater than or equal to the starting point of the second, and the ending point of the first interval is equal to the ending point of the second.

This operator uses the semantics described in the Start and End operators to determine interval boundaries.

If precision is specified and the point type is a date/time type, comparisons used in the operation are performed at the specified precision.

If either argument is null, the result is null.

The following examples illustrate the behavior of the ends operator:

define EndsIsTrue: Interval[0, 5] ends Interval[-1, 5]
define EndsIsFalse: Interval[-1, 7] ends Interval[0, 7]
define EndsIsNull: Interval[1, 5] ends null

9.7. Equal

Signature:

=(left Interval<T>, right Interval<T>) Boolean

Description:

The equal (=) operator for intervals returns true if and only if the intervals are over the same point type, and they have the same value for the starting and ending points of the intervals as determined by the Start and End operators.

If either argument is null, the result is null.

The following examples illustrate the behavior of the equal operator:

define EqualIsTrue: Interval[0, 5] = Interval[0, 5]
define EqualIsFalse: Interval[-1, 7] = Interval[0, 7]
define EqualIsNull: Interval[1, 5] = null

9.8. Equivalent

Signature:

~(left Interval<T>, right Interval<T>) Boolean

Description:

The equivalent (~) operator for intervals returns true if and only if the intervals are over the same point type, and the starting and ending points of the intervals as determined by the Start and End operators are equivalent.

The following examples illustrate the behavior of the equivalent operator:

define EquivalentIsTrue: Interval[0, 5] ~ Interval[0, 5]
define EquivalentIsAlsoTrue: Interval[1, null] ~ Interval[1, null]
define EquivalentIsFalse: Interval[-1, 7] ~ Interval[0, 7]

9.9. Except

Signature:

except(left Interval<T>, right Interval<T>) Interval<T>

Description:

The except operator for intervals returns the set difference of two intervals. More precisely, this operator returns the portion of the first interval that does not overlap with the second. Note that to avoid returning an improper interval, if the second argument is properly contained within the first and does not start or end it, this operator returns null.

If either argument is null, the result is null.

The following examples illustrate the behavior of the except operator:

define Except: Interval[0, 5] except Interval[3, 7] // Interval[0, 2]
define ExceptIsNull: null except Interval[-1, 7]

9.10. Expand

Signature:

expand(argument List<Interval<T>>, per Quantity) List<Interval<T>>

Description:

The expand operator returns the set of intervals of width per for all the intervals in the input.

The per argument must be a quantity value that is compatible with the point type of the input intervals. For numeric intervals, this means a default unit ('1'). For date/time intervals, this means a temporal duration.

For example:

expand { Interval[@2018-01-01, @2018-01-04] } per day

The following is the result of the example:

{ Interval[@2018-01-01, @2018-01-01], Interval[@2018-01-02, @2018-01-02], Interval[@2018-01-03, @2018-01-03], Interval[@2018-01-04, @2018-01-04] }

Note that if the values in the intervals are more precise than the per quantity, the more precise values will be truncated to the precision specified by the per quantity.

If the list of intervals is empty, the result is empty. If the list of intervals contains nulls, they will be excluded from the resulting list.

If the list argument is null, the result is null.

If the per argument is null, the default unit interval for the point type of the intervals involved will be used (i.e. the interval that has a width equal to the result of the successor function for the point type).

9.11. In

Signature:

in _precision_ (point T, argument Interval<T>) Boolean

Description:

The in operator for intervals returns true if the given point is greater than or equal to the starting point of the interval, and less than or equal to the ending point of the interval. For open interval boundaries, exclusive comparison operators are used. For closed interval boundaries, if the interval boundary is null, the result of the boundary comparison is considered true.

If precision is specified and the point type is a date/time type, comparisons used in the operation are performed at the specified precision.

If either argument is null, the result is null.

The following examples illustrate the behavior of the in operator:

define InIsTrue: 3 in Interval[0, 5]
define InIsFalse: -1 in Interval[0, 7]
define InIsNull: 3 in (null as Interval<Integer>)

9.12. Includes

Signature:

includes _precision_ (left Interval<T>, right Interval<T>) Boolean
includes _precision_ (left Interval<T>, right T) Boolean

Description:

The includes operator for intervals returns true if the first interval completely includes the second. More precisely, if the starting point of the first interval is less than or equal to the starting point of the second interval, and the ending point of the first interval is greater than or equal to the ending point of the second interval.

For the point overload, this operator is a synonym for the [.kw]#contains operator.

This operator uses the semantics described in the Start and End operators to determine interval boundaries.

If precision is specified and the point type is a date/time type, comparisons used in the operation are performed at the specified precision.

If either argument is null, the result is null.

The following examples illustrate the behavior of the includes operator:

define IncludesIsTrue: Interval[-1, 5] includes Interval[0, 5]
define IncludesIsFalse: Interval[-1, 5] includes 6
define IncludesIsNull: Interval[-1, 5] includes null

9.13. Included In

Signature:

included in _precision_ (left Interval<T>, right Interval<T>) Boolean
included in _precision_ (left T, right Interval<T>) Boolean

Description:

The included in operator for intervals returns true if the first interval is completely included in the second. More precisely, if the starting point of the first interval is greater than or equal to the starting point of the second interval, and the ending point of the first interval is less than or equal to the ending point of the second interval.

For the point overload, this operator is a synonym for the in operator.

This operator uses the semantics described in the Start and End operators to determine interval boundaries.

If precision is specified and the point type is a date/time type, comparisons used in the operation are performed at the specified precision.

If either argument is null, the result is null.

Note that during is a synonym for included in and may be used to invoke the same operation wherever included in may appear.

The following examples illustrate the behavior of the included in operator:

define IncludedInIsTrue: Interval[1, 5] included in Interval[0, 5]
define IncludedInIsFalse: -1 during Interval[0, 7]
define IncludedInIsNull: 3 included in (null as Interval<Integer>)

9.14. Intersect

Signature:

intersect(left Interval<T>, right Interval<T>) Interval<T>

Description:

The intersect operator for intervals returns the intersection of two intervals. More precisely, the operator returns the interval that defines the overlapping portion of both arguments. If the arguments do not overlap, this operator returns null.

If either argument is null, the result is null.

The following examples illustrate the behavior of the intersect operator:

define Intersect: Interval[1, 5] intersect Interval[3, 7] // Interval[3, 5]
define IntersectIsNull: Interval[3, 5] intersect (null as Interval<Integer>)

9.15. Meets

Signature:

meets _precision_ (left Interval<T>, right Interval<T>) Boolean
meets before _precision_ (left Interval<T>, right Interval<T>) Boolean
meets after _precision_ (left Interval<T>, right Interval<T>) Boolean

Description:

The meets operator returns true if the first interval ends immediately before the second interval starts, or if the first interval starts immediately after the second interval ends. In other words, if the ending point of the first interval is equal to the predecessor of the starting point of the second, or if the starting point of the first interval is equal to the successor of the ending point of the second.

The meets before operator returns true if the first interval ends immediately before the second interval starts, while the meets after operator returns true if the first interval starts immediately after the second interval ends.

This operator uses the semantics described in the Start and End operators to determine interval boundaries.

If precision is specified and the point type is a date/time type, comparisons used in the operation are performed at the specified precision.

If either argument is null, the result is null.

Note that date/time-based intervals with partially specified boundaries will "meet" at the precision of the boundaries, not the finest precision for the point type. For example:

define MeetsAtHours: Interval[@T03, @T04] meets Interval[@T05, @T06]

The MeetsAtHours expression returns true because the meets operation is performed at the hour precision.

The following examples illustrate the behavior of the meets, meets before, and meets after operators:

define MeetsIsTrue: Interval[6, 10] meets Interval[0, 5]
define MeetsBeforeIsTrue: Interval[-5, -1] meets before Interval[0, 5]
define MeetsAfterIsFalse: Interval[6, 10] meets after Interval[0, 7]
define MeetsIsNull: Interval[6, 10] meets (null as Interval<Integer>)

9.16. Not Equal

Signature:

!=(left Interval<T>, right Interval<T>) : Boolean

Description:

The not equal (!=) operator for intervals returns true if its arguments are not the same value.

The not equal operator is a shorthand for invocation of logical negation (not) of the equal operator.

The following examples illustrate the behavior of the not equal operator:

define NotEqualIsFalse: Interval[0, 5] != Interval[0, 5]
define NotEqualIsTrue: Interval[-1, 7] != Interval[0, 7]
define NotEqualIsNull: Interval[1, 5] != null

9.17. Not Equivalent

Signature:

!~(left Interval<T>, right Interval<T>) : Boolean

Description:

The not equivalent (!~) operator for intervals returns true if its arguments are not equivalent.

The not equivalent operator is a shorthand for invocation of logical negation (not) of the equivalent operator.

The following examples illustrate the behavior of the not equivalent operator:

define NotEquivalentIsFalse: Interval[0, 5] !~ Interval[0, 5]
define NotEquivalentIsAlsoFalse: Interval[1, null] !~ Interval[1, null]
define NotEquivalentIsTrue: Interval[-1, 7] !~ Interval[0, 7]

9.18. On Or After

Signature:

on or after _precision_ (left Interval<T>, right Interval<T>) Boolean
on or after _precision_ (left T, right Interval<T>) Boolean
on or after _precision_ (left Interval<T>, right T) Boolean

Description:

The on or after operator for intervals is a synonym for the same or after operator and is supported to enable natural phrasing. See the description of the Same Or After (Intervals) operator.

Note that this operator can be invoked using either the on or after or the after or on syntax.

In timing phrases, the keyword same is a synonym for on.

9.19. On Or Before

Signature:

on or before _precision_ (left Interval<T>, right Interval<T>) Boolean
on or before _precision_ (left T, right Interval<T>) Boolean
on or before _precision_ (left Interval<T>, right T) Boolean

Description:

The on or before operator for date/time-based intervals is a synonym for the same or before operator and is supported to enable natural phrasing. See the description of the Same Or Before (Intervals) operator.

Note that this operator can be invoked using either the on or before or the before or on syntax.

In timing phrases, the keyword same is a synonym for on.

9.20. Overlaps

Signature:

overlaps _precision_ (left Interval<T>, right Interval<T>) Boolean
overlaps before _precision_ (left Interval<T>, right Interval<T>) Boolean
overlaps after _precision_ (left Interval<T>, right Interval<T>) Boolean

Description:

The overlaps operator returns true if the first interval overlaps the second. More precisely, if the ending point of the first interval is greater than or equal to the starting point of the second interval, and the starting point of the first interval is less than or equal to the ending point of the second interval.

The operator overlaps before returns true if the first interval overlaps the second and starts before it, while the overlaps after operator returns true if the first interval overlaps the second and ends after it.

This operator uses the semantics described in the Start and End operators to determine interval boundaries.

If precision is specified and the point type is a date/time type, comparisons used in the operation are performed at the specified precision.

If either argument is null, the result is null.

The following examples illustrate the behavior of the overlaps, overlaps before, and overlaps after operators:

define OverlapsIsTrue: Interval[0, 4] overlaps Interval[1, 4]
define OverlapsBeforeIsTrue: Interval[0, 4] overlaps before Interval[1, 4]
define OverlapsAfterIsFalse: Interval[0, 4] overlaps after Interval[1, 4]
define OverlapsIsNull: Interval[6, 10] overlaps (null as Interval<Integer>)

9.21. Point From

Signature:

point from(argument Interval<T>) : T

Description:

The point from operator extracts the single point from a unit interval. If the argument is not a unit interval, a run-time error is thrown.

If the argument is null, the result is null.

The following examples illustrate the behavior of the point from operator:

define PointFrom: point from Interval[4, 4] // 4
define PointFromError: point from Interval[1, 4]
define PointFromIsNull: point from (null as Interval<Integer>)

9.22. Properly Includes

Signature:

properly includes _precision_ (left Interval<T>, right Interval<T>) Boolean
properly includes _precision_ (left Interval<T>, right T) Boolean

Description:

The properly includes operator for intervals returns true if the first interval completely includes the second and the first interval is strictly larger than the second. More precisely, if the starting point of the first interval is less than or equal to the starting point of the second interval, and the ending point of the first interval is greater than or equal to the ending point of the second interval, and they are not the same interval.

For the point overload, this operator returns true if the interval contains (i.e. includes) the point, and the interval is not a unit interval containing only the point.

This operator uses the semantics described in the Start and End operators to determine interval boundaries.

If precision is specified and the point type is a date/time type, comparisons used in the operation are performed at the specified precision.

If either argument is null, the result is null.

The following examples illustrate the behavior of the properly includes operator:

define ProperlyIncludesIsTrue: Interval[-1, 5] properly includes Interval[0, 5]
define ProperlyIncludesIsFalse: Interval[-1, 5] properly includes Interval[-1, 5]
define ProperlyIncludesIsNull: Interval[-1, 5] properly includes null

9.23. Properly Included In

Signature:

properly included in _precision_ (left Interval<T>, right Interval<T>) Boolean
properly included in _precision_ (left T, right Interval<T>) Boolean

Description:

The properly included in operator for intervals returns true if the first interval is completely included in the second and the first interval is strictly smaller than the second. More precisely, if the starting point of the first interval is greater than or equal to the starting point of the second interval, and the ending point of the first interval is less than or equal to the ending point of the second interval, and they are not the same interval.

For the point overload, this operator returns true if the point is in (i.e. included in) the interval, and the interval is not a unit interval containing only the point.

This operator uses the semantics described in the Start and End operators to determine interval boundaries.

If precision is specified and the point type is a date/time type, comparisons used in the operation are performed at the specified precision.

If either argument is null, the result is null.

Note that during is a synonym for included in.

The following examples illustrate the behavior of the properly included in operator:

define ProperlyIncludedInIsTrue: Interval[1, 5] properly included in Interval[0, 5]
define ProperlyIncludedInIsFalse: Interval[0, 7] properly during Interval[0, 7]
define ProperlyIncludedInIsNull: Interval[1, 5] properly included in (null as Interval<Integer>)

9.24. Same As

Signature:

same _precision_ as(left Interval<T>, right Interval<T>) Boolean

Description:

The same-precision-as operator for intervals returns true if the two intervals start and end at the same value, using the semantics described in the Start and End operators to determine interval boundaries, and for date/time value, performing the comparisons at the specified precision, as described in the Same As operator for date/time values.

If no precision is specified, comparisons are performed beginning with years (or hours for time values) and proceeding to the finest precision specified in either input.

For Date-based intervals, precision must be one of: year, month, or day.

For DateTime-based intervals, precision must be one of: year, month, day, hour, minute, second, or millisecond.

For Time-based intervals, precision must be one of: hour, minute, second, or millisecond.

Note specifically that due to variability in the way week numbers are determined, comparisons involving weeks are not supported.

When this operator is called with a mixture of Date- and DateTime-based intervals, the Date values will be implicitly converted to DateTime values as defined by the ToDateTime operator.

For comparisons involving date/time or time values with imprecision, note that the result of the comparison may be null, depending on whether the values involved are specified to the level of precision used for the comparison.

As with all date/time calculations, comparisons are performed respecting the timezone offset.

If either or both arguments are null, the result is null.

9.25. Same Or After

Signature:

same _precision_ or after(left Interval<T>, right Interval<T>) Boolean
same _precision_ or after(left T, right Interval<T>) Boolean
same _precision_ or after(left Interval<T>, right T) Boolean

Description:

The same-precision-or after operator for intervals returns true if the first interval starts on or after the second one ends, using the semantics described in the Start and End operators to determine interval boundaries, and for date/time values, performing the comparisons at the specified precision, as described in the Same or After (Date/Time) operator for date/time values.

If no precision is specified, comparisons are performed beginning with years (or hours for time values) and proceeding to the finest precision specified in either input.

For Date-based intervals, precision must be one of: year, month, or day.

For DateTime-based intervals, precision must be one of: year, month, day, hour, minute, second, or millisecond.

For Time-based intervals, precision must be one of: hour, minute, second, or millisecond.

Note specifically that due to variability in the way week numbers are determined, comparisons involving weeks are not supported.

When this operator is called with a mixture of Date-based and DateTime-based intervals, the Date values will be implicitly converted to DateTime values as defined by the ToDateTime operator.

When this operator is called with a mixture of point values and intervals, the point values are implicitly converted to an interval starting and ending on the given point value.

For comparisons involving date/time or time intervals with imprecision, note that the result of the comparison may be null, depending on whether the values involved are specified to the level of precision used for the comparison.

As with all date/time calculations, comparisons are performed respecting the timezone offset.

If either or both arguments are null, the result is null.

Note that in timing phrases, the keyword on may be used as a synonym for same for this operator.

9.26. Same Or Before

Signature:

same _precision_ or before(left Interval<T>, right Interval<T>) Boolean
same _precision_ or before(left T, right Interval<T>) Boolean
same _precision_ or before(left Interval<T>, right T) Boolean

Description:

The same-precision-or before operator returns true if the first interval ends on or before the second one starts, using the semantics described in the Start and End operators to determine interval boundaries, and for date/time values, performing the comparisons at the specified precision, as described in the Same or Before (Date/Time) operator for date/time values.

If no precision is specified, comparisons are performed beginning with years (or hours for time values) and proceeding to the finest precision specified in either input.

For Date-based intervals, precision must be one of: year, month, or day.

For DateTime-based intervals, precision must be one of: year, month, day, hour, minute, second, or millisecond.

For Time-based intervals, precision must be one of: hour, minute, second, or millisecond.

Note specifically that due to variability in the way week numbers are determined, comparisons involving weeks are not supported.

When this operator is called with a mixture of Date- and DateTime-based intervals, the Date values will be implicitly converted to DateTime values as defined by the ToDateTime operator.

When this operator is called with a mixture of point values and intervals, the point values are implicitly converted to an interval starting and ending on the given point value.

For comparisons involving date/time or time values with imprecision, note that the result of the comparison may be null, depending on whether the values involved are specified to the level of precision used for the comparison.

As with all date/time calculations, comparisons are performed respecting the timezone offset.

If either or both arguments are null, the result is null.

Note that in timing phrases, the keyword on may be used as a synonym for same for this operator.

9.27. Start

Signature:

start of(argument Interval<T>) T

Description:

The Start operator returns the starting point of an interval.

If the low boundary of the interval is open, this operator returns the successor of the low value of the interval. Note that if the low value of the interval is null, the result is null.

If the low boundary of the interval is closed and the low value of the interval is not null, this operator returns the low value of the interval. Otherwise, the result is the minimum value of the point type of the interval.

If the argument is null, the result is null.

The following examples illustrate the behavior of the Start operator:

define StartOfInterval: start of Interval[1, 5] // 1
define StartIsNull: start of (null as Interval<Integer>)

9.28. Starts

Signature:

starts _precision_ (left Interval<T>, right Interval<T>) Boolean

Description:

The starts operator returns true if the first interval starts the second. More precisely, if the starting point of the first is equal to the starting point of the second interval and the ending point of the first interval is less than or equal to the ending point of the second interval.

This operator uses the semantics described in the Start and End operators to determine interval boundaries.

If precision is specified and the point type is a date/time type, comparisons used in the operation are performed at the specified precision.

If either argument is null, the result is null.

The following examples illustrate the behavior of the starts operator:

define StartsIsTrue: Interval[0, 5] starts Interval[0, 7]
define StartsIsFalse: Interval[0, 7] starts Interval[0, 6]
define StartsIsNull: Interval[1, 5] starts null

9.29. Union

Signature:

union(left Interval<T>, right Interval<T>) Interval<T>

Description:

The union operator for intervals returns the union of the intervals. More precisely, the operator returns the interval that starts at the earliest starting point in either argument, and ends at the latest starting point in either argument. If the arguments do not overlap or meet, this operator returns null.

If either argument is null, the result is null.

The following examples illustrate the behavior of the union operator:

define Union: Interval[1, 5] union Interval[3, 7] // Interval[1, 7]
define UnionIsNull: Interval[3, 5] union (null as Interval<Integer>)

9.30. Width

Signature:

width of(argument Interval<T>) T

Description:

The width operator returns the width of an interval. The result of this operator is equivalent to invoking: (start of argument end of argument) + point-size.

Note that because CQL defines duration and difference operations for date/time and time valued intervals, width is not defined for intervals of these types.

If the argument is null, the result is null.

The following examples illustrate the behavior of the width operator:

define Width: width of Interval[3, 7] // 4
define WidthIsNull: width of (null as Interval<Integer>)

10. List Operators

Note that the operators in this section may all be invoked with singleton arguments if the list promotion feature is enabled. See the Promotion and Demotion section for more information.

10.1. Contains

Signature:

contains(argument List<T>, element T) Boolean

Description:

The contains operator for lists returns true if the given element is in the list using equality semantics.

If either argument is null, the result is null.

The following examples illustrate the behavior of the contains operator:

define ContainsIsTrue: { 1, 3, 5, 7 } contains 5
define ContainsIsFalse: { 1, 3, 5, 7 } contains 4
define ContainsIsNull: null contains 4

10.2. Distinct

Signature:

distinct(argument List<T>) List<T>

Description:

The distinct operator returns the given list with duplicates eliminated using equality semantics.

If the argument is null, the result is null.

The following examples illustrate the behavior of the distinct operator:

define Distinct: distinct { 1, 3, 3, 5, 5 } // { 1, 3, 5, 7 }
define DistinctIsNull: distinct null

10.3. Equal

Signature:

=(left List<T>, right List<T>) Boolean

Description:

The equal (=) operator for lists returns true if and only if the lists have the same element type, and have the same elements by value, in the same order.

If either argument is null, or contains null elements, the result is null.

The following examples illustrate the behavior of the equal operator:

define EqualIsTrue: { 1, 3, 5, 7 } = { 1, 3, 5, 7 }
define EqualIsFalse: { 1, 3, 5, 7 } = { 1, 3, 5 }
define EqualIsNull: { 1, 3, 5, 7 } = null

10.4. Equivalent

Signature:

~(left List<T>, right List<T>) Boolean

Description:

The equivalent (~) operator for lists returns true if and only if the lists contain elements of the same type, have the same number of elements, and for each element in the lists, in order, the elements are equivalent.

The following examples illustrate the behavior of the equivalent operator:

define EquivalentIsTrue: { 1, 3, 5, 7 } ~ { 1, 3, 5, 7 }
define EquivalentIsAlsoTrue: { 1, 3, 5, null } ~ { 1, 3, 5, null }
define EquivalentIsFalse: { 1, 3, 5, 7 } ~ { 1, 3, 5 }

10.5. Except

Signature:

except(left List<T>, right List<T>) List<T>

Description:

The except operator returns the set difference of two lists. More precisely, the operator returns a list with the elements that appear in the first operand that do not appear in the second operand.

This operator uses equality semantics to determine whether two elements are the same for the purposes of computing the difference.

The operator is defined with set semantics, meaning that each element will appear in the result at most once, and that there is no expectation that the order of the inputs will be preserved in the results.

If either argument is null, the result is null.

The following examples illustrate the behavior of the except operator:

define Except: { 1, 3, 5, 7 } except { 1, 3 } // { 5, 7 }
define ExceptLeft: { 1, 3, 5, 7 } except null // { 1, 3, 5, 7 }
define ExceptIsNull: null except { 1, 3, 5 }

10.6. Exists

Signature:

exists(argument List<T>) Boolean

Description:

The exists operator returns true if the list contains any non-null elements.

If the argument is null, the result is false.

The following examples illustrate the behavior of the exists operator:

define ExistsIsTrue: exists { 1, 3, 5, 7 }
define ExistsIsFalse: exists { }
define ExistsIsAlsoFalse: exists { null }
define ExistsIsAlsoAlsoFalse: exists null

10.7. Flatten

Signature:

flatten(argument List<List<T>>) List<T>

Description:

The flatten operator flattens a list of lists into a single list.

If the argument is null, the result is null.

The following examples illustrate the behavior of the flatten operator:

define Flatten: flatten { { 1, 2 }, { 3, 4, 5 } } // { 1, 2, 3, 4, 5 }
define FlattenIsNull: flatten null

10.8. First

Signature:

First(argument List<T>) T

Description:

The First operator returns the first element in a list. The operator is equivalent to invoking the indexer with an index of 0.

If the argument is null, the result is null.

The following examples illustrate the behavior of the First operator:

define First1: First({ 1, 2, 5 }) // 1
define FirstIsNull: First(null)

10.9. In

Signature:

in(element T, argument List<T>) Boolean

Description:

The in operator for lists returns true if the given element is in the given list using equality semantics.

If either argument is null, the result is null.

The following examples illustrate the behavior of the in operator:

define InIsTrue: 5 in { 1, 3, 5, 7 }
define InIsFalse: 5 in { 1, 3 }
define InIsNull: 5 in null
define IsIsAlsoNull: null in { 1, 3, 5, null }

10.10. Includes

Signature:

includes(left List<T>, right List<T>) Boolean
includes(left List<T>, right T) Boolean

Description:

The includes operator for lists returns true if the first list contains every element of the second list using equality semantics.

For the singleton overload, this operator returns true if the list includes (i.e. contains) the singleton.

If either argument is null, the result is null.

Note that the order of elements does not matter for the purposes of determining inclusion.

The following examples illustrate the behavior of the includes operator:

define IncludesIsTrue: { 1, 3, 5, 7 } includes 5
define IncludesIsNull: { 1, 3, 5, null } includes null
define IncludesIsFalse: { 1, 3 } includes { 1, 3, 5 }
define IncludesIsAlsoNull: null includes { 1, 3, 5 }

10.11. Included In

Signature:

included in(left List<T>, right list<T>) Boolean
included in(left T, right list<T>) Boolean

Description:

The included in operator for lists returns true if every element of the first list is in the second list using equality semantics.

For the singleton overload, this operator returns true if the singleton is included in (i.e. in) the list.

If either argument is null, the result is null.

Note that the order of elements does not matter for the purposes of determining inclusion.

The following examples illustrate the behavior of the included in operator:

define IncludedInIsTrue: 5 included in { 1, 3, 5, 7 }
define IncludedInIsNull: null included in { 1, 3, 5 }
define IncludedInIsFalse: { 1, 3, 5 } included in { 1, 3 }
define IncludedInIsAlsoNull: { 1, 3, 5, null } included in null

10.12. Indexer

Signature:

[](argument List<T>, index Integer) T

Description:

The indexer ([]) operator returns the element at the indexth position in a list.

Indexes in lists are defined to be 0-based.

If the index is less than 0, or greater than the number of elements in the list, the result is null.

If either argument is null, the result is null.

The following examples illustrate the behavior of the indexer operator:

define Indexer: { 1, 3, 5, 7 }[1] // 3
define IndexerIsNull: { 1, 3, 5 }[-1]
define IndexerIsAlsoNull: (null as List<Integer>)[0]

10.13. IndexOf

Signature:

IndexOf(argument List<T>, element T) Integer

Description:

The IndexOf operator returns the 0-based index of the given element in the given source list using equality semantics.

If the list is empty, or no element is found, the result is -1.

If either argument is null, the result is null.

The following examples illustrate the behavior of the IndexOf operator:

define IndexOfFound: IndexOf({ 1, 3, 5, 7 }, 5) // 2
define IndexOfNotFound: IndexOf({ 1, 3, 5, 7 }, 4) // -1
define IndexOfIsNull: IndexOf(null, 4)

10.14. Intersect

Signature:

intersect(left List<T>, right List<T>) List<T>

Description:

The intersect operator for lists returns the intersection of two lists. More precisely, the operator returns a list containing only the elements that appear in both lists.

This operator uses equality semantics to determine whether or not two elements are the same.

The operator is defined with set semantics, meaning that each element will appear in the result at most once, and that there is no expectation that the order of the inputs will be preserved in the results.

If either argument is null, the result is null.

The following examples illustrate the behavior of the intersect operator:

define Intersect: { 1, 3, 5 } intersect { 3, 5, 7 } // { 3, 5 }
define IntersectIsNull: { 1, 3, 5 } intersect null

10.15. Last

Signature:

Last(argument List<T>) T

Description:

The Last operator returns the last element in a list. In a list of length N, the operator is equivalent to invoking the indexer with an index of N - 1.

If the argument is null, the result is null.

The following examples illustrate the behavior of the Last operator:

define Last5: Last({ 1, 3, 5 }) // 5
define LastIsNull: Last(null)

10.16. Length

Signature:

Length(argument List<T>) Integer

Description:

The Length operator returns the number of elements in a list.

If the argument is null, the result is 0.

The following examples illustrate the behavior of the Length operator:

define Length3: Length({ 1, 3, 5 }) // 3
define Length0: Length(null as List<Integer>)

10.17. Not Equal

Signature:

!=(left List<T>, right List<T>) Boolean

Description:

The not equal (!=) operator for lists returns true if its arguments are not the same value.

The not equal operator is a shorthand for invocation of logical negation (not) of the equal operator.

The following examples illustrate the behavior of the not equal operator:

define NotEqualIsFalse: { 1, 3, 5, 7 } != { 1, 3, 5, 7 }
define NotEqualIsTrue: { 1, 3, 5, 7 } != { 1, 3, 5 }
define NotEqualIsNull: { 1, 3, 5, 7 } != null

10.18. Not Equivalent

Signature:

!~(left List<T>, right List<T>) Boolean

Description:

The not equivalent (!~) operator for lists returns true if its arguments are not equivalent.

The not equivalent operator is a shorthand for invocation of logical negation (not) of the equivalent operator.

The following examples illustrate the behavior of the not equivalent operator:

define NotEquivalentIsFalse: { 1, 3, 5, 7 } !~ { 1, 3, 5, 7 }
define NotEquivalentIsAlsoFalse: { 1, 3, 5, null } !~ { 1, 3, 5, null }
define NotEquivalentIsTrue: { 1, 3, 5, 7 } !~ { 1, 3, 5 }

10.19. Properly Includes

Signature:

properly includes(left List<T>, right List<T>) Boolean
properly includes(left T, right List<T>) Boolean

Description:

The properly includes operator for lists returns true if the first list contains every element of the second list, and the first list is strictly larger than the second list.

For the element overload, this operator returns true if the list contains (i.e. includes) the element, and it is not the only element in the list.

This operator uses equality semantics to determine whether or not two elements are the same.

If either argument is null, the result is null.

Note that the order of elements does not matter for the purposes of determining inclusion.

The following examples illustrate the behavior of the properly includes operator:

define ProperlyIncludesIsTrue: { 1, 3, 5, 7 } properly includes { 1, 3, 5 }
define ProperlyIncludesIsAlsoTrue: { 1, 3, 5, null } properly includes null
define ProperlyIncludesIsFalse: { 1, 3, 5 } properly includes { 1, 3, 5 }
define ProperlyIncludesIsAlsoFalse: null properly includes { 1, 3, 5 }

10.20. Properly Included In

Signature:

properly included in(left List<T>, right list<T>) Boolean

Description:

The properly included in operator for lists returns true if every element of the first list is in the second list and the first list is strictly smaller than the second list.

For the element overload, this operator returns true if the element is in (i.e. included in) the list and it is not the only element in the list.

This operator uses equality semantics to determine whether or not two elements are the same.

If either argument is null, the result is null.

Note that the order of elements does not matter for the purposes of determining inclusion.

The following examples illustrate the behavior of the properly included in operator:

define ProperlyIncludedInIsTrue: { 1, 3, 5 } properly included in { 1, 3, 5, 7 }
define ProperlyIncludedInIsAlsoTrue: null properly included in { 1, 3, 5 }
define ProperlyIncludedInIsFalse: { 1, 3, 5 } properly included in { 1, 3, 5 }
define ProperlyIncludedInIsAlsoFalse: { 1, 3, 5, null } properly included in null

10.21. Singleton From

Signature:

singleton from(argument List<T>) T

Description:

The singleton from operator extracts a single element from the source list. If the source list is empty, the result is null. If the source list contains one element, that element is returned. If the list contains more than one element, a run-time error is thrown.

If the source list is null, the result is null.

The following examples illustrate the behavior of the singleton from operator:

define SingletonFrom: singleton from { 1 } // 1
define SingletonFromError: singleton from { 1, 3, 5 }
define SingletonFromIsNull: singleton from (null as List<Integer>)

10.22. Skip

Signature:

Skip(argument List<T>, number Integer) List<T>

Description:

The Skip operator returns the elements in the list, skipping the first number elements. If the list has less number elements, the result is empty.

If the source list is null, the result is null.

If the number of elements is null, the result is the entire list, no elements are skipped.

If the number of elements is less than zero, the result is an empty list.

The following examples illustrate the behavior of the Skip operator:

define Skip2: Skip({ 1, 2, 3, 4, 5 }, 2) // { 3, 4, 5 }
define SkipNull: Skip({ 1, 3, 5 }, null) // { 1, 3, 5 }
define SkipEmpty: Skip({ 1, 3, 5 }, -1) // { }
define SkipIsNull: Skip(null, 2)

10.23. Tail

Signature:

Tail(argument List<T>) List<T>

Description:

The Tail operator returns all but the first element from the given list. If the list is empty, the result is empty.

If the source list is null, the result is null.

The following examples illustrate the behavior of the Tail operator:

define Tail234: Tail({ 1, 2, 3, 4 }) // { 2, 3, 4 }
define TailEmpty: Tail({ }) // { }
define TailIsNull: Tail(null)

10.24. Take

Signature:

Take(argument List<T>, number Integer) List<T>

Description:

The Take operator returns the first number elements from the given list. If the list has less than number elements, the result only contains the elements in the list.

If the source list is null, the result is null.

If number is null, or 0 or less, the result is an empty list.

The following examples illustrate the behavior of the Take operator:

define Take2: Take({ 1, 2, 3, 4 }, 2) // { 1, 2 }
define TakeTooMany: Take({ 1, 2 }, 3) // { 1, 2 }
define TakeEmpty: Take({ 1, 2, 3, 4 }, null) // { }
define TakeIsNull: Take(null, 2)

10.25. Union

Signature:

union(left List<T>, right List<T>) List<T>

Description:

The union operator for lists returns a list with all elements from both arguments. Note that duplicates are eliminated during this process; if an element appears in both sources, that element will only appear once in the resulting list. In addition, there is no expectation that the order of elements in the inputs will be preserved in the results.

If either argument is null, the result is null.

Note that the union operator can also be invoked with the symbolic operator (|).

The following examples illustrate the behavior of the union operator:

define Union: { 1, 2, 3 } union { 4, 5 } // { 1, 2, 3, 4, 5 }
define UnionAlternateSyntax: { 1, 2, 3 } | { 4, 5 } // { 1, 2, 3, 4, 5 }
define UnionIsNull: null union { 4, 5 }

11. Aggregate Functions

11.1. AllTrue

Signature:

AllTrue(argument List<Boolean>) Boolean

Description:

The AllTrue operator returns true if all the non-null elements in the source are true.

If the source contains no non-null elements, true is returned.

If the source is null, the result is true.

The following examples illustrate the behavior of the AllTrue operator:

define AllTrueIsTrue: AllTrue({ true, null, true })
define AllTrueIsAlsoTrue: AllTrue({ null, null, null })
define AllTrueIsTrueWhenNull: AllTrue(null)
define AllTrueIsFalse: AllTrue({ true, false, null })

11.2. AnyTrue

Signature:

AnyTrue(argument List<Boolean>) Boolean

Description:

The AnyTrue operator returns true if any non-null element in the source is true.

If the source contains no non-null elements, false is returned.

If the source is null, the result is false.

The following examples illustrate the behavior of the AnyTrue operator:

define AnyTrueIsTrue: AnyTrue({ true, false, null })
define AnyTrueIsFalse: AnyTrue({ false, false, null })
define AnyTrueIsAlsoFalse: AnyTrue({ null, null, null })
define AnyTrueIsFalseWhenNull: AnyTrue(null)

11.3. Avg

Signature:

Avg(argument List<Decimal>) Decimal
Avg(argument List<Quantity>) Quantity

Description:

The Avg operator returns the average of the non-null elements in the source.

If the source contains no non-null elements, null is returned.

If the source is null, the result is null.

The following examples illustrate the behavior of the Avg operator:

define DecimalAvg: Avg({ 5.5, 4.7, 4.8 }) // 5.0
define QuantityAvg: Avg({ 5.5 'cm', 4.7 'cm', 4.8 'cm' }) // 5.0 'cm'
define AvgIsNull: Avg(null as List<Decimal>)

11.4. Count

Signature:

Count(argument List<T>) Integer

Description:

The Count operator returns the number of non-null elements in the source. If the list contains no non-null elements, the result is 0. If the list is null, the result is 0.

The following examples illustrate the behavior of the Count operator:

define Count5: Count({ 1, 2, 3, 4, 5 }) // 5
define Count0: Count({ null, null, null }) // 0
define CountNull0: Count(null as List<Decimal>) // 0

11.5. GeometricMean

Signature:

GeometricMean(argument List<Decimal>) Decimal

Description:

The GeometricMean operator returns the geometric mean of the non-null elements in the source. Geometric mean is defined as the Nth root of the geometric product of the elements. In other words:

GeometricMean(X) = Power(Product(X), 1 / Count(X))

If the source contains no non-null elements, the result is null.

If the source is null, the result is null.

The following examples illustrate the behavior of the GeometricMean operator:

define GeometricMean4: GeometricMean({ 2.0, 8.0 }) // 4.0
define GeometricMeanIsNull: GeometricMean({ null, null, null })
define GeometricMeanIsAlsoNull: GeometricMean(null as List<Decimal>)

11.6. Max

Signature:

Max(argument List<Integer>) Integer
Max(argument List<Decimal>) Decimal
Max(argument List<Quantity>) Quantity
Max(argument List<Date>) Date
Max(argument List<DateTime>) DateTime
Max(argument List<Time>) Time
Max(argument List<String>) String

Description:

The Max operator returns the maximum element in the source. Comparison semantics are defined by the Comparison Operators for the type of value being aggregated.

If the source contains no non-null elements, null is returned.

If the source is null, the result is null.

The following examples illustrate the behavior of the Max operator:

define IntegerMax: Max({ 2, 4, 8, 6 }) // 8
define DateMax: Max({ @2012-12-31, @2013-01-01, @2012-01-01 }) // @2012-12-31
define MaxIsNull: Max({ null as Quantity, null as Quantity, null as Quantity })
define MaxIsAlsoNull: Max(null as List<Decimal>)

11.7. Min

Signature:

Min(argument List<Integer>) Integer
Min(argument List<Decimal>) Decimal
Min(argument List<Quantity>) Quantity
Min(argument List<Date>) Date
Min(argument List<DateTime>) DateTime
Min(argument List<Time>) Time
Min(argument List<String>) String

Description:

The Min operator returns the minimum element in the source. Comparison semantics are defined by the Comparison Operators for the type of value being aggregated.

If the source contains no non-null elements, null is returned.

If the source is null, the result is null.

The following examples illustrate the behavior of the Min operator:

define IntegerMin: Min({ 2, 4, 8, 6 }) // 2
define DateMin: Min({ @2012-12-31, @2013-01-01, @2012-01-01 }) // @2012-01-01
define MinIsNull: Min({ null as Quantity, null as Quantity, null as Quantity })
define MinIsAlsoNull: Min(null as List<Decimal>)

11.8. Median

Signature:

Median(argument List<Decimal>) Decimal
Median(argument List<Quantity>) Quantity

Description:

The Median operator returns the median of the elements in source.

If the source contains no non-null elements, null is returned.

If the source is null, the result is null.

The following examples illustrate the behavior of the Median operator:

define DecimalMedian: Median({ 2.0, 4.0, 8.0, 6.0 }) // 5.0
define QuantityMedian: Median({ 1.0 'mg', 2.0 'mg', 3.0 'mg' }) // 2.0 'mg'
define MedianIsNull: Median({ null as Quantity, null as Quantity, null as Quantity })
define MedianIsAlsoNull: Median(null as List<Decimal>)

11.9. Mode

Signature:

Mode(argument List<T>) T

Description:

The Mode operator returns the statistical mode of the elements in source.

If the source contains no non-null elements, null is returned.

If the source is null, the result is null.

The following examples illustrate the behavior of the Mode operator:

define DecimalMode: Mode({ 2.0, 2.0, 8.0, 6.0, 8.0, 8.0 }) // 8.0
define QuantityMode: Mode({ 1.0 'mg', 2.0 'mg', 3.0 'mg', 2.0 'mg' }) // 2.0 'mg'
define ModeIsNull: Mode({ null as Quantity, null as Quantity, null as Quantity })
define ModeIsAlsoNull: Mode(null as List<Decimal>)

11.10. Population StdDev

Signature:

PopulationStdDev(argument List<Decimal>) Decimal
PopulationStdDev(argument List<Quantity>) Quantity

Description:

The PopulationStdDev operator returns the statistical standard deviation of the elements in source.

If the source contains no non-null elements, null is returned.

If the source is null, the result is null.

The following examples illustrate the behavior of the PopulationStdDev operator:

define DecimalPopulationStdDev: PopulationStdDev({ 1.0, 2.0, 3.0, 4.0, 5.0 }) // 1.41421356
define QuantityPopulationStdDev: PopulationStdDev({ 1.0 'mg', 2.0 'mg', 3.0 'mg', 4.0 'mg', 5.0 'mg' }) // 1.41421356 'mg'
define PopulationStdDevIsNull: PopulationStdDev({ null as Quantity, null as Quantity, null as Quantity })
define PopulationStdDevIsAlsoNull: PopulationStdDev(null as List<Decimal>)

11.11. Population Variance

Signature:

PopulationVariance(argument List<Decimal>) Decimal
PopulationVariance(argument List<Quantity>) Quantity

Description:

The PopulationVariance operator returns the statistical population variance of the elements in source.

If the source contains no non-null elements, null is returned.

If the source is null, the result is null.

The following examples illustrate the behavior of the PopulationVariance operator:

define DecimalPopulationVariance: PopulationVariance({ 1.0, 2.0, 3.0, 4.0, 5.0 }) // 2.0
define QuantityPopulationVariance: PopulationVariance({ 1.0 'mg', 2.0 'mg', 3.0 'mg', 4.0 'mg', 5.0 'mg' }) // 2.0 'mg'
define PopulationVarianceIsNull: PopulationVariance({ null as Quantity, null as Quantity, null as Quantity })
define PopulationVarianceIsAlsoNull: PopulationVariance(null as List<Decimal>)

11.12. Product

Signature:

Product(argument List<Integer>) Integer
Product(argument List<Decimal>) Decimal
Product(argument List<Quantity>) Quantity

Description:

The Product operator returns the geometric product of the elements in source.

If the source contains no non-null elements, null is returned.

If the source is null, the result is null.

The following examples illustrate the behavior of the Product operator:

define DecimalProduct: Product({ 1.0, 2.0, 3.0, 4.0 }) // 24.0
define QuantityProduct: Product({ 1.0 'mg', 2.0 'mg', 3.0 'mg', 4.0 'mg' }) // 24.0 'mg'
define ProductIsNull: Product({ null as Quantity, null as Quantity, null as Quantity })
define ProductIsAlsoNull: Product(null as List<Decimal>)

11.13. StdDev

Signature:

StdDev(argument List<Decimal>) Decimal
StdDev(argument List<Quantity>) Quantity

Description:

The StdDev operator returns the statistical standard deviation of the elements in source.

If the source contains no non-null elements, null is returned.

If the list is null, the result is null.

The following examples illustrate the behavior of the StdDev operator:

define DecimalStdDev: StdDev({ 1.0, 2.0, 3.0, 4.0, 5.0 }) // 1.58113883
define QuantityStdDev: StdDev({ 1.0 'mg', 2.0 'mg', 3.0 'mg', 4.0 'mg', 5.0 'mg' }) // 1.58113883 'mg'
define StdDevIsNull: StdDev({ null as Quantity, null as Quantity, null as Quantity })
define StdDevIsAlsoNull: StdDev(null as List<Decimal>)

11.14. Sum

Signature:

Sum(argument List<Integer>) Integer
Sum(argument List<Decimal>) Decimal
Sum(argument List<Quantity>) Quantity

Description:

The Sum operator returns the sum of non-null elements in the source.

If the source contains no non-null elements, null is returned.

If the list is null, the result is null.

The following examples illustrate the behavior of the Sum operator:

define DecimalSum: Sum({ 1.0, 2.0, 3.0, 4.0, 5.0 }) // 15.0
define QuantitySum: Sum({ 1.0 'mg', 2.0 'mg', 3.0 'mg', 4.0 'mg', 5.0 'mg' }) // 15.0 'mg'
define SumIsNull: Sum({ null as Quantity, null as Quantity, null as Quantity })
define SumIsAlsoNull: Sum(null as List<Decimal>)

11.15. Variance

Signature:

Variance(argument List<Decimal>) Decimal
Variance(argument List<Quantity>) Quantity

Description:

The Variance operator returns the statistical variance of the elements in source.

If the source contains no non-null elements, null is returned.

If the source is null, the result is null.

The following examples illustrate the behavior of the Variance operator:

define DecimalVariance: Variance({ 1.0, 2.0, 3.0, 4.0, 5.0 }) // 2.5
define QuantityVariance: Variance({ 1.0 'mg', 2.0 'mg', 3.0 'mg', 4.0 'mg', 5.0 'mg' }) // 2.5 'mg'
define VarianceIsNull: Variance({ null as Quantity, null as Quantity, null as Quantity })
define VarianceIsAlsoNull: Variance(null as List<Decimal>)

12. Clinical Operators

12.1. Age

Signature:

AgeInYears() Integer
AgeInMonths() Integer
AgeInWeeks() Integer
AgeInDays() Integer
AgeInHours() Integer
AgeInMinutes() Integer
AgeInSeconds() Integer

Description:

The Age operators calculate the age of the patient as of the current date/time in the precision named in the operator.

If the patient’s birthdate is null, the result is null.

The Age operators are defined in terms of a date/time duration calculation. This means that if the age of the patient is not specified to the level of precision corresponding to the operator being invoked, the result will be an uncertainty over the range of possible values, potentially causing some comparisons to return null.

Note that for AgeInYears and AgeInMonths, the birthDate is specified as a Date and Today() is used to obtain the current date; whereas with the other precisions, birthDate is specified as a DateTime, and Now() is used to obtain the current date/time.

12.2. AgeAt

Signature:

AgeInYearsAt(asOf Date) Integer
AgeInYearsAt(asOf DateTime) Integer
AgeInMonthsAt(asOf Date) Integer
AgeInMonthsAt(asOf DateTime) Integer
AgeInWeeksAt(asOf Date) Integer
AgeInWeeksAt(asOf DateTime) Integer
AgeInDaysAt(asOf Date) Integer
AgeInDaysAt(asOf DateTime) Integer
AgeInHoursAt(asOf DateTime) Integer
AgeInMinutesAt(asOf DateTime) Integer
AgeInSecondsAt(asOf DateTime) Integer

Description:

The AgeAt operators calculate the age of the patient as of the given date/time in the precision named in the operator.

If the patient’s birthdate is null, or the asOf argument is null, the result is null.

The AgeAt operators are defined in terms of a date/time duration calculation. This means that if the age of the patient or the given asOf value are not specified to the level of precision corresponding to the operator being invoked, the will be an uncertainty over the range of possible values, potentially causing some comparisons to return null.

12.3. CalculateAge

Signature:

CalculateAgeInYears(birthDate Date) Integer
CalculateAgeInYears(birthDate DateTime) Integer
CalculateAgeInMonths(birthDate Date) Integer
CalculateAgeInMonths(birthDate DateTime) Integer
CalculateAgeInWeeks(birthDate Date) Integer
CalculateAgeInWeeks(birthDate DateTime) Integer
CalculateAgeInDays(birthDate Date) Integer
CalculateAgeInDays(birthDate DateTime) Integer
CalculateAgeInHours(birthDate DateTime) Integer
CalculateAgeInMinutes(birthDate DateTime) Integer
CalculateAgeInSeconds(birthDate DateTime) Integer

Description:

The CalculateAge operators calculate the age of a person born on the given birth date/time as of today/now in the precision named in the operator.

If the birthdate is null, the result is null.

The CalculateAge operators are defined in terms of a date/time duration calculation. This means that if the given birthDate is not specified to the level of precision corresponding to the operator being invoked, the result will be an uncertainty over the range of possible values, potentially causing some comparisons to return null.

12.4. CalculateAgeAt

Signature:

CalculateAgeInYearsAt(birthDate Date, asOf Date) Integer
CalculateAgeInYearsAt(birthDate DateTime, asOf DateTime) Integer
CalculateAgeInMonthsAt(birthDate Date, asOf Date) Integer
CalculateAgeInMonthsAt(birthDate DateTime, asOf DateTime) Integer
CalculateAgeInWeeksAt(birthDate Date, asOf Date) Integer
CalculateAgeInWeeksAt(birthDate DateTime, asOf DateTime) Integer
CalculateAgeInDaysAt(birthDate Date, asOf Date) Integer
CalculateAgeInDaysAt(birthDate DateTime, asOf DateTime) Integer
CalculateAgeInHoursAt(birthDate DateTime, asOf DateTime) Integer
CalculateAgeInMinutesAt(birthDate DateTime, asOf DateTime) Integer
CalculateAgeInSecondsAt(birthDate DateTime, asOf DateTime) Integer

Description:

The CalculateAgeAt operators calculate the age of a person born on the given birth date/time as of the given date/time in the precision named in the operator.

If the birthDate is null or the asOf argument is null, the result is null.

The CalculateAgeAt operators are defined in terms of a date/time duration calculation. This means that if the given birthDate or asOf are not specified to the level of precision corresponding to the operator being invoked, the result will be an uncertainty over the range of possible values, potentially causing some comparisons to return null.

The following examples illustrate the behavior of the CalculateAgeAt operator:

define CalculateAgeAt: CalculateAgeInYearsAt(@2000-01-01, @2015-01-01) // 15
define CalculateAgeAtIsNull: CalculateAgeInYearsAt(@2000-01-01, null)

12.5. Equal

Signature:

=(left Code, right Code) Boolean
=(left Concept, right Concept) Boolean

Description:

The equal (=) operator for Codes and Concepts uses tuple equality semantics. This means that the operator will return true if and only if the values for each element by name are equal.

If either argument is null, or contains any null components, the result is null.

The following examples illustrate the behavior of the equal operator:

define Code1: Code { system: 'http://loinc.org', code: '8480-6', version: '1.0', display: 'Systolic blood pressure' }
define Concept1: Concept { codes: { Code1 }, display: 'Concepts' }
define Concept2: Concept { codes: { Code1 }, display: 'More Concepts' }
define EqualIsTrue: Code1 = Code1
define EqualIsFalse: Concept1 = Concept2
define EqualIsNull: Code1 = null

12.6. Equivalent

Signature:

~(left Code, right Code) Boolean
~(left Concept, right Concept) Boolean
~(left Code, right Concept) Boolean
~(left Concept, right Code) Boolean

Description:

The equivalent (~) operator for Code values returns true if the code and system elements are equivalent. The version and display elements are ignored for the purposes of determining Code equivalence.

For Concept values, equivalence is defined as a non-empty intersection of the codes in each Concept. The display element is ignored for the purposes of determining Concept equivalence.

Note that this operator will always return true or false, even if either or both of its arguments are null, or contain null components.

Note carefully that this notion of equivalence is not the same as the notion of equivalence used in terminology: “these codes represent the same concept.” CQL specifically avoids defining terminological equivalence. The notion of equivalence defined here is used to provide consistent and intuitive semantics when dealing with missing information in membership contexts.

The following examples illustrate the behavior of the equivalent operator:

define Code1: Code { system: 'http://loinc.org', code: '8480-6', display: 'Systolic blood pressure' }
define Concept1: Concept { codes: { Code1 }, display: 'Concepts' }
define Concept2: Concept { codes: { null }, display: 'More Concepts' }
define EquivalentIsTrue: Code1 ~ Code1
define EquivalentIsAlsoTrue: Concept2 ~ Concept2
define EquivalentIsFalse: Concept1 ~ Concept2

12.7. In (Codesystem)

Signature:

in(code String, codesystem CodeSystemRef) Boolean
in(code Code, codesystem CodeSystemRef) Boolean
in(concept Concept, codesystem CodeSystemRef) Boolean
in(codes List<Code>, codeSystem CodeSystemRef) Boolean

Description:

The in (Codesystem) operators determine whether or not a given code, or any of a list of codes, is in a particular codesystem. Note that these operators can only be invoked by referencing a defined codesystem.

For the String overload, if the given code system contains a code with an equivalent code element, the result is true.

For the Code overload, if the given code system contains an equivalent code, the result is true.

For the Concept overload, if the given code system contains a code equivalent to any code in the given concept, the result is true.

For the List<Code> overload, if the given code system contains a code equivalent to any code in the given list, the result is true.

If the code argument is null, the result is false.

If the code system reference cannot be resolved, a run-time error is thrown.

The following examples illustrate the behavior of the in (Codesystem) operator:

codesystem "SNOMED:2014": 'http://snomed.info/sct'
define StringInCodesystem: '12345678' in "SNOMED:2014"
define CodeInCodesystem: Code { system: "SNOMED:2014", code: '12345678' } in "SNOMED:2014"
define NullStringInCodesystem: null as String in "SNOMED:2014" // false

12.8. In (Valueset)

Signature:

in(code String, valueset ValueSetRef) Boolean
in(code Code, valueset ValueSetRef) Boolean
in(concept Concept, valueset ValueSetRef) Boolean
in(codes List<Code>, valueset ValueSetRef) Boolean

Description:

The in (Valueset) operators determine whether or not a given code, or any of a list of codes, is in a particular valueset. Note that these operators can only be invoked by referencing a defined valueset.

For the String overload, if the given valueset contains a code with an equivalent code element, the result is true. Note that for this overload, because the code being tested cannot specify code system information, if the resolved value set contains codes from multiple code systems, a run-time error is thrown because the operation is ambiguous.

For the Code overload, if the given valueset contains an equivalent code, the result is true.

For the Concept overload, if the given valueset contains a code equivalent to any code in the given concept, the result is true.

For the List<Code> overload, if the given valueset contains a code equivalent to any code in the given list, the result is true.

If the code argument is null, the result is false.

If the value set reference cannot be resolved, a run-time error is thrown.

The following examples illustrate the behavior of the in (Valueset) operator:

valueset "A Super Awesome ValueSet": 'http://super.awesome/valueset'
define StringInValueset: '12345678' in "A Super Awesome ValueSet"
define CodeInValueset: Code { system: 'http://snomed.info/sct', code: '12345678' } in "A Super Awesome ValueSet"
define NullStringInValueset: null as String in "A Super Awesome ValueSet" // false

13. Errors and Messaging

13.1. Message

Signature:

Message(source T, condition Boolean, code String, severity String, message String) T

Description:

The Message operator provides a run-time mechanism for returning messages, warnings, traces, and errors to the calling environment.

The source operator is any type and the result of the operation is the input source; the operation performs no modifications to input. This allows the message operation to appear at any point in any expression of CQL.

The condition is used to determine whether the message is generated and returned to the calling environment. If condition is true, the message is generated. Otherwise, the operation only returns the results and performs no processing at all.

The code provides a coded representation of the error. Note that this is a token (like a string or integer), not a terminology Code.

The severity determines what level of processing should occur for the message that is generated:

  • Trace – The operation produces an informational message that is expected to be made available to a tracing mechanism such as a debug log in the calling environment. In addition, some representation of the contents of the source parameter should be made available to the tracing mechanism.

  • Message – The operation produces an informational message that is expected to be made available in some way to the calling environment.

  • Warning – The operation produces a warning message that is expected to be made conspicuously available to the calling environment, potentially to the end-user of the logic.

  • Error – The operation produces a run-time error and return the message to the calling environment. This is the only severity that stops evaluation. All other severities continue evaluation of the expression.

If no severity is supplied, a default severity of Message is assumed.

The message is the content of the actual message that is sent to the calling environment.

Note that for Trace severity, the implementation should output the contents of the source parameter as part of the trace message. Because the logic may be operating on patient information, the utmost care should be taken to ensure that appropriate safeguards are in place to avoid logging sensitive information. At a minimum, all PHI should be redacted from these trace messages.

The following examples illustrate the behavior of the Message operator:

parameter Numerator Integer default 1
parameter Denominator Integer default 0
define ErrorMessage: Message('Divide by zero error!', Denominator = 0, 'Undefined', 'Error', 'The Denominator parameter is zero')