View a markdown version of this page

Numeric functions and operators in DuckDB - Amazon Athena

Numeric functions and operators in DuckDB

Use numeric functions and operators to do arithmetic, raise numbers to powers, round and truncate numbers, compute logarithms and roots, compute trigonometric values, and work with the bits of integers. Several operators also have a function name: add for +, subtract for -, multiply for *, divide for //, and mod for %. In the same way, pow and power are function names for **, abs for @, and factorial for !.

DuckDB has the signed integer types TINYINT, SMALLINT, INTEGER, BIGINT, and HUGEINT (128 bits). It also has the unsigned integer types UTINYINT, USMALLINT, UINTEGER, UBIGINT, and UHUGEINT. The exact type DECIMAL(precision, scale) holds up to 38 digits, and the approximate types are FLOAT and DOUBLE. A whole-number literal, such as 42, is an INTEGER, or a BIGINT or HUGEINT when it is too large for INTEGER. A literal with a decimal point, such as 1.5, is a DECIMAL, and a literal with an exponent, such as 1.5e0, is a DOUBLE.

When the operands of an arithmetic operator have different numeric types, DuckDB converts them to a common type, and the result has that type. For example, an INTEGER plus a BIGINT is a BIGINT, an INTEGER times a DECIMAL is a DECIMAL, and arithmetic with a DOUBLE operand returns a DOUBLE. An integer result is not widened when it is too large for its type: the query fails instead. Because 2147483647 and 1 are both INTEGER literals, 2147483647 + 1 fails. To avoid the error, cast an operand to a wider type, as in CAST(2147483647 AS BIGINT) + 1.

The / operator always returns a fractional result, so 7 / 2 returns 3.5. To divide integers and drop the fractional part, use //, and to get the remainder, use %. If an operand of an arithmetic or bitwise operator on numbers is NULL, the result is NULL.

When an expression has more than one operator, DuckDB applies them in the following order. The factorial operator ! comes first. Next comes a sign, - or +, in front of a value, and then ** and ^. After those come *, /, //, and %, and then + and - between two values. Then come &, |, <<, >>, ~, and @, which all have the same precedence. Comparison operators such as = come after all of these. DuckDB applies operators at the same level from left to right. This order differs from many programming languages. For example, -2 ** 2 returns 4.0, and 2 ** 3 ** 2 returns 64.0. Also, 4 | 1 & 2 returns 0, and ~12 + 1 returns -14. Use parentheses to make the order explicit.

The following table lists the numeric functions and operators, with a link to the description of each one.

Function or operator Description

! (factorial)

Returns the factorial of an integer.

%

Returns the remainder of dividing one number by another.

&

Returns the bitwise AND of two integers or two bitstrings.

*

Multiplies two numbers, or multiplies an interval by a number.

**

Raises a number to a power.

+

Adds two numbers, or adds an interval or a number of days to a date or time value.

-

Subtracts one value from another, or negates a number.

/

Divides one number by another and returns a fractional result.

//

Divides one integer by another and drops the fractional part.

<<

Shifts the bits of an integer or a bitstring to the left.

>>

Shifts the bits of an integer or a bitstring to the right.

@

Returns the absolute value of a number.

^

Alias of **.

|

Returns the bitwise OR of two integers or two bitstrings.

~

Inverts every bit of an integer or a bitstring, or tests whether a string matches a regular expression.

abs

Alias of @.

acos

Returns the arccosine of a number, in radians.

acosh

Returns the inverse hyperbolic cosine of a number.

add

Alias of +.

asin

Returns the arcsine of a number, in radians.

asinh

Returns the inverse hyperbolic sine of a number.

atan

Returns the arctangent of a number, in radians.

atan2

Returns the angle, in radians, of the point with a given y-coordinate and x-coordinate.

atanh

Returns the inverse hyperbolic tangent of a number.

binom

Returns the binomial coefficient, the number of ways to choose a number of items from a set.

cbrt

Returns the cube root of a number.

ceil

Rounds a number up to the nearest integer.

ceiling

Alias of ceil.

cos

Returns the cosine of an angle in radians.

cosh

Returns the hyperbolic cosine of a number.

cot

Returns the cotangent of an angle in radians.

decimal_division

Returns the quotient of two DECIMAL values as a DECIMAL, without converting them to DOUBLE.

degrees

Converts an angle from radians to degrees.

divide

Alias of //.

even

Rounds the absolute value of a number up to an even integer and keeps the number's sign.

exp

Returns e raised to the power of a number.

factorial

Alias of the ! (factorial) operator.

fdiv

Divides two numbers and rounds the quotient down to the nearest integer.

floor

Rounds a number down to the nearest integer.

fmod

Returns the floating-point remainder of a division, with the sign of the divisor.

gamma

Returns the gamma function of a number, which extends the factorial to real numbers.

gcd

Alias of greatest_common_divisor.

greatest_common_divisor

Returns the greatest common divisor of two integers.

isfinite

Returns whether a number, date, or timestamp is finite.

isinf

Returns whether a number, date, or timestamp is positive or negative infinity.

isnan

Returns whether a floating-point value is NaN (not a number).

lcm

Alias of least_common_multiple.

least_common_multiple

Returns the least common multiple of two integers.

lgamma

Returns the natural logarithm of the absolute value of the gamma function of a number.

ln

Returns the natural logarithm of a number.

log

Returns the logarithm of a number to base 10, or to a base that you choose.

log10

Returns the base-10 logarithm of a number.

log2

Returns the base-2 logarithm of a number.

mod

Alias of %.

multiply

Alias of *.

nextafter

Moves a floating-point number by the smallest possible step toward a second number.

pi

Returns the constant pi.

pow

Alias of **.

power

Alias of **.

radians

Converts an angle from degrees to radians.

random

Returns a random number that is at least 0 and less than 1.

round

Rounds a number to a given number of decimal places.

round_even

Rounds a number in the same way as round, except that it rounds halfway values so that the last digit it keeps is even.

roundbankers

Alias of round_even.

sign

Returns -1, 0, or 1 to show whether a number is negative, zero, or positive.

signbit

Returns whether the sign bit of a floating-point number is set.

sin

Returns the sine of an angle given in radians.

sinh

Returns the hyperbolic sine of a number.

sqrt

Returns the square root of a number.

subtract

Alias of -.

tan

Returns the tangent of an angle given in radians.

tanh

Returns the hyperbolic tangent of a number.

trunc

Removes the digits of a number after a given decimal place, without rounding.

xor

Returns the bitwise exclusive OR (XOR) of two integers or two bitstrings.