Documentation

InfluxQL mathematical operators

This page documents an earlier version of InfluxDB OSS. InfluxDB OSS v2 is the latest stable version. See the InfluxDB v2 documentation.

Mathematical operators follow the standard order of operations. That is, parentheses take precedence to division and multiplication, which takes precedence to addition and subtraction. For example 5 / 2 + 3 * 2 = (5 / 2) + (3 * 2) and 5 + 2 * 3 - 2 = 5 + (2 * 3) - 2.

Content

Mathematical Operators

Addition

Perform addition with a constant.

SELECT "A" + 5 FROM "add"
  • Copy
  • Fill window
SELECT * FROM "add" WHERE "A" + 5 > 10
  • Copy
  • Fill window

Perform addition on two fields.

SELECT "A" + "B" FROM "add"
  • Copy
  • Fill window
SELECT * FROM "add" WHERE "A" + "B" >= 10
  • Copy
  • Fill window

Subtraction

Perform subtraction with a constant.

SELECT 1 - "A" FROM "sub"
  • Copy
  • Fill window
SELECT * FROM "sub" WHERE 1 - "A" <= 3
  • Copy
  • Fill window

Perform subtraction with two fields.

SELECT "A" - "B" FROM "sub"
  • Copy
  • Fill window
SELECT * FROM "sub" WHERE "A" - "B" <= 1
  • Copy
  • Fill window

Multiplication

Perform multiplication with a constant.

SELECT 10 * "A" FROM "mult"
  • Copy
  • Fill window
SELECT * FROM "mult" WHERE "A" * 10 >= 20
  • Copy
  • Fill window

Perform multiplication with two fields.

SELECT "A" * "B" * "C" FROM "mult"
  • Copy
  • Fill window
SELECT * FROM "mult" WHERE "A" * "B" <= 80
  • Copy
  • Fill window

Multiplication distributes across other operators.

SELECT 10 * ("A" + "B" + "C") FROM "mult"
  • Copy
  • Fill window
SELECT 10 * ("A" - "B" - "C") FROM "mult"
  • Copy
  • Fill window
SELECT 10 * ("A" + "B" - "C") FROM "mult"
  • Copy
  • Fill window

Division

Perform division with a constant.

SELECT 10 / "A" FROM "div"
  • Copy
  • Fill window
SELECT * FROM "div" WHERE "A" / 10 <= 2
  • Copy
  • Fill window

Perform division with two fields.

SELECT "A" / "B" FROM "div"
  • Copy
  • Fill window
SELECT * FROM "div" WHERE "A" / "B" >= 10
  • Copy
  • Fill window

Division distributes across other operators.

SELECT 10 / ("A" + "B" + "C") FROM "mult"
  • Copy
  • Fill window

Modulo

Perform modulo arithmetic with a constant.

SELECT "B" % 2 FROM "modulo"
  • Copy
  • Fill window
SELECT "B" FROM "modulo" WHERE "B" % 2 = 0
  • Copy
  • Fill window

Perform modulo arithmetic on two fields.

SELECT "A" % "B" FROM "modulo"
  • Copy
  • Fill window
SELECT "A" FROM "modulo" WHERE "A" % "B" = 0
  • Copy
  • Fill window

Bitwise AND

You can use this operator with any integers or Booleans, whether they are fields or constants. It does not work with float or string datatypes, and you cannot mix integers and Booleans.

SELECT "A" & 255 FROM "bitfields"
  • Copy
  • Fill window
SELECT "A" & "B" FROM "bitfields"
  • Copy
  • Fill window
SELECT * FROM "data" WHERE "bitfield" & 15 > 0
  • Copy
  • Fill window
SELECT "A" & "B" FROM "booleans"
  • Copy
  • Fill window
SELECT ("A" ^ true) & "B" FROM "booleans"
  • Copy
  • Fill window

Bitwise OR

You can use this operator with any integers or Booleans, whether they are fields or constants. It does not work with float or string datatypes, and you cannot mix integers and Booleans.

SELECT "A" | 5 FROM "bitfields"
  • Copy
  • Fill window
SELECT "A" | "B" FROM "bitfields"
  • Copy
  • Fill window
SELECT * FROM "data" WHERE "bitfield" | 12 = 12
  • Copy
  • Fill window

Bitwise Exclusive-OR

You can use this operator with any integers or Booleans, whether they are fields or constants. It does not work with float or string datatypes, and you cannot mix integers and Booleans.

SELECT "A" ^ 255 FROM "bitfields"
  • Copy
  • Fill window
SELECT "A" ^ "B" FROM "bitfields"
  • Copy
  • Fill window
SELECT * FROM "data" WHERE "bitfield" ^ 6 > 0
  • Copy
  • Fill window

Common Issues with Mathematical Operators

Issue 1: Mathematical operators with wildcards and regular expressions

InfluxDB does not support combining mathematical operations with a wildcard (*) or regular expression in the SELECT clause. The following queries are invalid and the system returns an error:

Perform a mathematical operation on a wildcard.

> SELECT * + 2 FROM "nope"
ERR: unsupported expression with wildcard: * + 2
  • Copy
  • Fill window

Perform a mathematical operation on a wildcard within a function.

> SELECT COUNT(*) / 2 FROM "nope"
ERR: unsupported expression with wildcard: count(*) / 2
  • Copy
  • Fill window

Perform a mathematical operation on a regular expression.

> SELECT /A/ + 2 FROM "nope"
ERR: error parsing query: found +, expected FROM at line 1, char 12
  • Copy
  • Fill window

Perform a mathematical operation on a regular expression within a function.

> SELECT COUNT(/A/) + 2 FROM "nope"
ERR: unsupported expression with regex field: count(/A/) + 2
  • Copy
  • Fill window

Issue 2: Mathematical operators with functions

The use of mathematical operators inside of function calls is currently unsupported. Note that InfluxDB only allows functions in the SELECT clause.

For example

SELECT 10 * mean("value") FROM "cpu"
  • Copy
  • Fill window

will work, however

SELECT mean(10 * "value") FROM "cpu"
  • Copy
  • Fill window

will yield a parse error.

InfluxQL supports subqueries which offer similar functionality to using mathematical operators inside a function call. See Data Exploration for more information.

Unsupported Operators

Inequalities

Using any of =,!=,<,>,<=,>=,<> in the SELECT clause yields empty results for all types. See GitHub issue 3525.

Logical Operators

Using any of !|,NAND,XOR,NOR yield a parser error.

Additionally using AND, OR in the SELECT clause of a query will not behave as mathematical operators and simply yield empty results, as they are tokens in InfluxQL. However, you can apply the bitwise operators &, | and ^ to Boolean data.

Bitwise Not

There is no bitwise-not operator, because the results you expect depend on the width of your bitfield. InfluxQL does not know how wide your bitfield is, so cannot implement a suitable bitwise-not operator.

For example, if your bitfield is 8 bits wide, then to you the integer 1 represents the bits 0000 0001. The bitwise-not of this should return the bits 1111 1110, i.e. the integer 254.

However, if your bitfield is 16 bits wide, then the integer 1 represents the bits 0000 0000 0000 0001. The bitwise-not of this should return the bits 1111 1111 1111 1110, i.e. the integer 65534.

Solution

You can implement a bitwise-not operation by using the ^ (bitwise xor) operator together with the number representing all-ones for your word-width:

For 8-bit data:

SELECT "A" ^ 255 FROM "data"
  • Copy
  • Fill window

For 16-bit data:

SELECT "A" ^ 65535 FROM "data"
  • Copy
  • Fill window

For 32-bit data:

SELECT "A" ^ 4294967295 FROM "data"
  • Copy
  • Fill window

In each case the constant you need can be calculated as (2 ** width) - 1.


Was this page helpful?

Thank you for your feedback!


The future of Flux

Flux is going into maintenance mode. You can continue using it as you currently are without any changes to your code.

Read more

InfluxDB 3 Open Source Now in Public Alpha

InfluxDB 3 Open Source is now available for alpha testing, licensed under MIT or Apache 2 licensing.

We are releasing two products as part of the alpha.

InfluxDB 3 Core, is our new open source product. It is a recent-data engine for time series and event data. InfluxDB 3 Enterprise is a commercial version that builds on Core’s foundation, adding historical query capability, read replicas, high availability, scalability, and fine-grained security.

For more information on how to get started, check out: