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:

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, exists returns true.
  • If the key exists in the static record, but has a null value, exists returns false.
  • If the key does not exist in the static record, because the record type is statically known, exists returns an error.
  1. import "internal/debug"
  2. p = {
  3. firstName: "John",
  4. lastName: "Doe",
  5. age: 42,
  6. height: debug.null(type: "int"),
  7. }
  8. exists p.firstName
  9. // Returns true
  10. exists p.height
  11. // Returns false
  12. exists p.hairColor
  13. // Returns "error: record is missing label hairColor"

exists