% / mod
Modulo: the remainder left after dividing one number by another.% is the operator form and mod is the equivalent function.
Syntax
a— the dividend.b— the divisor.
- The remainder of
adivided byb. The result takes the sign of the dividenda(truncated division). Dividing by zero raises an error.
Bucket users into 10 groups by a numeric id
abs
Returns the absolute (non-negative) value of a number. Syntaxx— a numeric value.
- The absolute value of
x, in the same numeric domain as the input.
Distance of lifetime value from a target
floor / ceil / round / trunc
Rounding functions.floor rounds down, ceil rounds up, round rounds to the nearest value (ties away from zero), and trunc truncates toward zero. round and trunc optionally take a number of decimal places.
Syntax
x— a numeric value.n— (optional, forroundandtrunc) the number of decimal places to keep. Defaults to0.
- The rounded value of
x.floorreturns the largest integer not greater thanx;ceilthe smallest integer not less thanx;roundthe nearest value atndecimals;truncthe value with digits beyondndecimals dropped.
Rounding a value four ways
greatest / least
Return the largest or smallest value among their arguments. Syntaxx1, x2, ...— two or more comparable values.
greatest: the maximum of the arguments.least: the minimum of the arguments.
Clamp lifetime value to at least 0
power
Raises a base to an exponent. Syntaxa— the base.b— the exponent.
araised to the powerb, as a floating-point number.
Two raised to the tenth power
sqrt / cbrt
sqrt returns the square root and cbrt the cube root of a number.
Syntax
x— a numeric value. Forsqrt,xmust be non-negative.
- The square root (
sqrt) or cube root (cbrt) ofx, as a floating-point number.
Square root and cube root
sign
Returns the sign of a number. Syntaxx— a numeric value.
-1for negative,0for zero, and1for positivex.
Sign of a value
negate
Returns the argument with its sign flipped, equivalent to the unary minus. Syntaxx— a numeric value.
- The value
-x, always signed.
Negate a number
max2 / min2
Return the larger or smaller of exactly two numbers. Syntaxa,b— numeric values.
max2: the greater ofaandb.min2: the lesser ofaandb. Returned as a floating-point number.
Larger and smaller of two numbers
moduloOrZero
Likemod, but returns 0 instead of raising an error when the divisor is zero.
Syntax
a— the dividend.b— the divisor.
- The remainder of
adivided byb, or0whenbis0.
Safe modulo
positiveModulo
Returns the remainder of a division, always as a non-negative number. Syntaxa— the dividend.b— the divisor.
- The non-negative remainder of
adivided byb. For example,positiveModulo(-7, 3)is2rather than-1.
Always-positive remainder
intDiv / intDivOrZero
Integer division.intDiv returns the whole-number quotient; intDivOrZero behaves the same but yields 0 on division by zero (or when dividing the minimum negative value by -1) instead of raising an error.
Syntax
a— the dividend.b— the divisor.
- The integer part of
adivided byb.intDivraises an error whenbis0;intDivOrZeroreturns0in that case.
Integer division
hypot
Returns the length of the hypotenuse of a right triangle given the two other sides, that is, the Euclidean distance from the origin. Syntaxa,b— numeric values representing the two legs.
- The square root of
a² + b², as a floating-point number.
Hypotenuse of a 3-4-5 triangle
exp / exp2 / exp10
Exponential functions.exp uses base e, exp2 uses base 2, and exp10 uses base 10.
Syntax
x— the exponent, a numeric value.
exp: e raised tox.exp2: 2 raised tox.exp10: 10 raised tox. All returned as floating-point numbers.
Exponentials of a value
ln / log10 / log / log2 / log1p
Logarithms.ln is the natural logarithm (base e); log10 and log are both base-10; log2 is base-2; log1p computes ln(1 + x) with extra precision near zero.
Syntax
x— a positive numeric value (forlog1p,xmust be greater than-1).
- The logarithm of
xin the corresponding base, as a floating-point number. Note thatlog(x)is base-10 (matching PostgreSQL), not the natural logarithm.
Logarithms of a value
sin / cos / tan
Trigonometric functions taking an angle in radians. Syntaxx— an angle in radians.
- The sine, cosine, or tangent of
x, as a floating-point number.
Trig functions
asin / acos / atan
Inverse trigonometric functions, returning an angle in radians. Syntaxx— a numeric value. Forasinandacos,xmust be in the range −1 to 1.
- The arcsine, arccosine, or arctangent of
x, in radians, as a floating-point number.
Inverse trig functions
atan2
Returns the angle, in radians, between the positive x-axis and the point(x, y), using the signs of both arguments to determine the correct quadrant.
Syntax
y— the ordinate (y-coordinate).x— the abscissa (x-coordinate).
- The angle of the point
(x, y)in radians, as a floating-point number.
Angle of a point
sinh / cosh / tanh
Hyperbolic functions. Syntaxx— a numeric value.
- The hyperbolic sine, cosine, or tangent of
x, as a floating-point number.
Hyperbolic functions
asinh / acosh / atanh
Inverse hyperbolic functions. Syntaxx— a numeric value.acoshrequiresx≥ 1;atanhrequiresxin the open interval between −1 and 1.
- The inverse hyperbolic sine, cosine, or tangent of
x, as a floating-point number.
Inverse hyperbolic functions
degrees / radians
Convert between radians and degrees.degrees converts radians to degrees; radians converts degrees to radians.
Syntax
x— an angle: in radians fordegrees, in degrees forradians.
- The converted angle, as a floating-point number.
Convert between radians and degrees
pi
Returns the mathematical constant π. Syntax- The value of π (approximately 3.14159), as a floating-point number.
Value of pi
e
Returns Euler’s number, the base of the natural logarithm. Syntax- The value of e (approximately 2.71828), as a floating-point number.
Value of e
factorial
Returns the factorial of a non-negative integer. Syntaxn— a non-negative integer.
n!, the product of all positive integers up ton(withfactorial(0)equal to1).
Factorial of 5
gcd / lcm
gcd returns the greatest common divisor and lcm the least common multiple of two integers.
Syntax
a,b— integer values.
gcd: the largest integer that divides bothaandb.lcm: the smallest positive integer that is a multiple of bothaandb.
GCD and LCM of two integers
intExp2 / intExp10
Integer exponentials.intExp2 returns 2 raised to x and intExp10 returns 10 raised to x, both as integers.
Syntax
x— the exponent, a non-negative integer.
intExp2: 2 raised toxas an integer.intExp10: 10 raised toxas an integer.
Integer powers of 2 and 10
roundBankers
Rounds a number to the nearest value, breaking ties by rounding to the nearest even digit (banker’s rounding). This avoids the upward bias of always rounding halves away from zero. Syntaxx— a numeric value.n— (optional) the number of decimal places to keep. Defaults to0.
xrounded tondecimals, with halfway cases rounded to the nearest even digit. For example,roundBankers(2.5)is2androundBankers(3.5)is4.
Banker's rounding of halves
width_bucket
Returns the index of the histogram bucket that a value falls into, given an equal-width partitioning of a range into a number of buckets. Syntaxoperand— the value to bucket.low— the lower bound of the range.high— the upper bound of the range.count— the number of equal-width buckets to divide the range into.
- An integer bucket index from
1tocountfor values within the range;0for values belowlow, andcount + 1for values at or abovehigh.
Bucket lifetime value into 4 bands from 0 to 1000
random
Returns a pseudo-random floating-point number. Syntax- A pseudo-random floating-point value in the range from
0.0(inclusive) to1.0(exclusive).
A random 10% sample of users
isFinite / isInfinite / isNaN
Test the classification of a floating-point number. Syntaxx— a floating-point value.
1if the condition holds, otherwise0.isFiniteis true for ordinary finite numbers;isInfiniteis true for positive or negative infinity;isNaNis true for “not a number”.
Classify a computed value
ifNotFinite
Returns a value if it is finite, otherwise returns a fallback. Useful for replacing infinities and NaN results from a computation. Syntaxx— the value to check.alt— the fallback returned whenxis not finite (infinity or NaN).
xwhenxis finite, otherwisealt.
Guard against a non-finite ratio
erf / erfc
The Gauss error function and its complement.erf(x) gives the probability that a normally distributed variable falls within x standard deviations of the mean (scaled); erfc(x) is 1 - erf(x), computed with better precision for large x.
Syntax
x— a numeric value.
erf: the error function ofx.erfc: the complementary error function ofx. Both are floating-point numbers.
Error function and its complement
lgamma / tgamma
Gamma functions, available on ClickHouse only.tgamma(x) is the gamma function Γ(x), which generalizes the factorial (Γ(n) equals (n−1)! for positive integers). lgamma(x) is the natural logarithm of the absolute value of Γ(x), useful when Γ(x) would overflow.
Syntax
x— a numeric value.
tgamma: the gamma function Γ(x).lgamma: the natural log of the absolute value of Γ(x). Both are floating-point numbers.
Gamma function and log-gamma