assert()

Checks for a condition. If the condition is false, outputs error messages and fails the query.

Note

The assert function gets evaluated during the query analysis phase, before optimizations such as constant-folding and predicate short-circuiting get applied.

Note

The parameters given to assert must be evaluated to constants during the query analysis phase. In other words, it can be constructed from other expressions referencing constants only, and can't be bound to row-context.

Syntax

assert(condition,message)

Learn more about syntax conventions.

Parameters

Name Type Required Description
condition bool ✔️ The conditional expression to evaluate. The condition must be evaluated to constant during the query analysis phase.
message string ✔️ The message used if assertion is evaluated to false.

Returns

Returns true if the condition is true. Raises a semantic error if the condition is evaluated to false.

Examples

The following query defines a function checkLength() that checks input string length, and uses assert to validate input length parameter (checks that it's greater than zero).

let checkLength = (len:long, s:string)
{
    assert(len > 0, "Length must be greater than zero") and
    strlen(s) > len
};
datatable(input:string)
[
    '123',
    '4567'
]
| where checkLength(len=long(-1), input)

Running this query yields an error: assert() has failed with message: 'Length must be greater than zero'

Example of running with valid len input:

let checkLength = (len:long, s:string)
{
    assert(len > 0, "Length must be greater than zero") and strlen(s) > len
};
datatable(input:string)
[
    '123',
    '4567'
]
| where checkLength(len=3, input)

Output

input
4567

The following query will always fail, demonstrating that the assert function gets evaluated even though the where b operator returns no data when b is false:

let b=false;
print x="Hello"
| where b
| where assert(b, "Assertion failed")