Check if a value exists
Use the exists operator to check if a row record contains a column or if a column’s value is null.
1. (r) => exists r.column
If you’re just getting started with Flux queries, check out the following:
- Get started with Flux for a conceptual overview of Flux and parts of a Flux query.
- Execute queries to discover a variety of ways to run your queries.
Use exists with row functions ( filter(), map(), reduce()) to check if a row includes a column or if the value for that column is null.
Filter null values
1. from(bucket: "example-bucket")
2. |> range(start: -5m)
3. |> filter(fn: (r) => exists r._value)
Map values based on existence
1. from(bucket: "default")
2. |> range(start: -30s)
3. |> map(
4. fn: (r) => ({r with
5. human_readable: if exists r._value then
6. "${r._field} is ${string(v: r._value)}."
7. else
8. "${r._field} has no value.",
9. }),
10. )
Ignore null values in a custom aggregate function
1. customSumProduct = (tables=<-) => tables
2. |> reduce(
3. identity: {sum: 0.0, product: 1.0},
4. fn: (r, accumulator) => ({r with
5. sum: if exists r._value then
6. r._value + accumulator.sum
7. else
8. accumulator.sum,
9. product: if exists r._value then
10. r.value * accumulator.product
11. else
12. accumulator.product,
13. }),
14. )
Check if a statically defined record contains a key
When you use the record literal syntax to statically define a record, Flux knows the record type and what keys to expect.
- If the key exists in the static record,
existsreturnstrue. - If the key exists in the static record, but has a null value,
existsreturnsfalse. - If the key does not exist in the static record, because the record type is statically known,
existsreturns an error.
1. import "internal/debug"
3. p = {
4. firstName: "John",
5. lastName: "Doe",
6. age: 42,
7. height: debug.null(type: "int"),
8. }
10. exists p.firstName
11. // Returns true
13. exists p.height
14. // Returns false
16. exists p.hairColor
17. // Returns "error: record is missing label hairColor"
