assert()
Checks for a condition. If the condition is false, outputs error messages and fails the query.
Syntax
assert(condition, message)
Arguments
- condition: The conditional expression to evaluate. If the condition is
false, the specified message is used to report an error. If the condition istrue, it returnstrueas an evaluation result. Condition must be evaluated to constant during the query analysis phase. - message: The message used if assertion is evaluated to
false. The message must be a string literal.
Note
condition must be evaluated to constant during the query analysis phase. In other words, it can be constructed from other expressions referencing constants, and can't be bound to row-context.
Returns
true- if the condition istrue- Raises 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)
| input |
|---|
| 4567 |
Feedback
Submit and view feedback for