isEmpty
Description
Returns true if the array contains no elements, or false otherwise.
- Supported element types include
string,bool,number,interval,timestamp,regexp, andenum.
Syntax
Like many functions in DataPrime, isEmpty supports two notations, function and method. These interchangeable forms allow flexibility in how you structure expressions.
- Function notation
- Method notation
isEmpty(array: array<T>): bool
(array: array<T>).isEmpty(): bool
Arguments
| Name | Type | Required | Description |
|---|---|---|---|
array | array<T> | true | The array to check for emptiness |
Example
Use case: Check if an array is empty
You can directly test whether an array has any elements:
create is_empty from isEmpty(my_array)
This produces a new boolean field indicating whether the array is empty.
Use case 2: Optimize execution by skipping work when arrays are empty
Suppose you have documents like the following:
{
"names": ["Chip", "Ruby", "Glitch"]
},
{
"names": []
}
If you want to join names into a string but avoid unnecessary computation for empty arrays, you can combine isEmpty with an if statement:
Example query
- Function notation
- Method notation
create msg from if(!names.isEmpty(), names.arrayJoin(','), '')
create msg from if(!isEmpty(names), names.arrayJoin(','), '')
Example output
For the above input, the results will look like:
{
"names": ["Chip", "Ruby", "Glitch"],
"msg": "Chip,Ruby,Glitch"
},
{
"names": [],
"msg": ""
}