ClickHouse function reference
e
Returns the mathematical constant e (Euler’s number). Syntax:Float64
]
Example:
The
e()
function always returns the same value and does not take any arguments.pi
Returns the mathematical constant π (pi). Syntax:Float64
value representing π (approximately 3.14159265358979323846).
Example:
pi()
to obtain the value of π and rounds it to 5 decimal places for easier reading.
The
pi()
function is useful in various mathematical and geometric calculations, such as computing the area or circumference of a circle.exp
Returns e (Euler’s constant) raised to the power of the given argument. Syntax:x
(numeric): The exponent. Can be any numeric type.
- Type:
Float32
orFloat64
depending on the input type. - The value of e^x.
The
exp
function is particularly useful in various mathematical and statistical computations, such as calculating compound interest, modeling population growth, or computing probabilities in logistic regression.exp
function to model the growth in popularity of different taco varieties. The growth_rate
represents the natural log of the popularity multiplier, so exp(growth_rate)
gives us the actual multiplier. We then use this to calculate the projected popularity based on the initial popularity and the growth rate.
log
Returns the natural logarithm of the argument. Syntax:- ln(x)
x
(numeric): A number. Type:UInt*
,Int*
,Float*
, orDecimal*
.
- The natural logarithm of
x
. Type:Float*
.
- If
x
is negative or zero, the function will returnnan
. - For very small values of
x
close to 1, consider usinglog1p(x)
for better numerical accuracy.
exp2
Returns 2 raised to the power of the given argument. Syntax:x
(numeric): The exponent. Can be any numeric type:UInt8
,UInt16
,UInt32
,UInt64
,Int8
,Int16
,Int32
,Int64
,Float32
,Float64
, orDecimal
.
- 2^x
- Type:
Float32
orFloat64
depending on the input type
For integer results, consider using the
intExp2
function instead.intExp2
Calculates 2 raised to the power of the given argument. Syntax:x
(numeric): The exponent. Can be any numeric type.
UInt64
.
Example:
This function is similar to
exp2
, but returns an integer result. It’s useful when you need an exact integer power of 2, such as for bit manipulations or when dealing with data sizes.log2
Calculates the binary logarithm (base-2 logarithm) of the argument. Syntax:x
(numeric): A number to calculate the binary logarithm for. Can be any of these types:UInt8
,UInt16
,UInt32
,UInt64
,Int8
,Int16
,Int32
,Int64
,Float32
,Float64
, orDecimal
.
- The binary logarithm of
x
. - Type:
Float64
log2(8)
returns 3 because 2³ = 8log2(0.5)
returns -1 because 2⁻¹ = 0.5
- If
x
is negative or zero, the function will returnnan
(Not a Number). - This function is particularly useful in computer science and information theory contexts, such as calculating the number of bits needed to represent a number or measuring information content.
exp10
Returns 10 raised to the power of the given argument. Syntax:x
(numeric): The exponent. Can be any numeric type:UInt8
,UInt16
,UInt32
,UInt64
,Int8
,Int16
,Int32
,Int64
,Float32
,Float64
, orDecimal
.
- 10 raised to the power of
x
.
Float32
or Float64
, depending on the input type.
Example:
For integer results, consider using
intExp10
instead, which returns a UInt64
.intExp10
Returns 10 raised to the power of the given argument. Syntax:x
(numeric): The exponent. Can be any numeric type.
UInt64
.
Example:
This function is similar to
exp10
, but returns an integer result. It’s useful when you need an exact integer value for powers of 10, which is common in certain calculations or when dealing with large numbers.log10
Calculates the base-10 logarithm of a number. Syntax:x
(numeric): The input value. Can be any numeric type:UInt8
,UInt16
,UInt32
,UInt64
,Int8
,Int16
,Int32
,Int64
,Float32
,Float64
, orDecimal
.
- The base-10 logarithm of
x
. Type:Float64
.
log10(100)
returns 2, as 10^2 = 100.log10(taco_price)
calculates the logarithm of the taco price, assuming the ‘Spicy Carnitas’ taco costs $6.00 (as 10^0.778151 ≈ 6).
The
log10
function is particularly useful for dealing with data that spans multiple orders of magnitude, such as comparing taco sales across different restaurant sizes or market populations.sqrt
Calculates the square root of a number. Syntax:x
(numeric): The input value. Can be any numeric type:UInt*
,Int*
,Float*
, orDecimal*
.
- The square root of
x
. Type:Float32
orFloat64
, depending on the input type.
- For negative input values, the function returns
NaN
(Not a Number). - For
NULL
input, the function returnsNULL
.
cbrt
Returns the cubic root of the argument. Syntax:x
(numeric): A numeric value. Supported types:Int
,UInt
,Float
, orDecimal
.
- The cubic root of
x
. - Type:
Float32
orFloat64
, depending on the input type.
The This query would return the side length of a cubic box for each taco order, assuming the volume is given in cubic centimeters.
cbrt
function is particularly useful when dealing with volumes or three-dimensional calculations. For example, you could use it to calculate the side length of a cubic taco box given its volume:erf
Returns the error function of the argument. Syntax:x
(numeric): A numeric value. SupportsUInt*
,Int*
,Float*
, andDecimal*
types.
x
. Type: Float64
.
Description:
The error function erf(x)
is a special mathematical function related to the probability that a random variable with normal distribution takes a value between 0 and x√2.
For non-negative x, erf(x√2)
is the probability that a random variable with a normal distribution of mean 0 and variance 1/2 falls in the range [−x, x].
Example:
The
erf
function is closely related to the normal distribution and is often used in statistical analysis and machine learning applications.erfc
Returns the complementary error function of the argument. Syntax:x
(numeric): The input value. Type:Float32
,Float64
,Int8
,Int16
,Int32
,Int64
,UInt8
,UInt16
,UInt32
,UInt64
orDecimal
.
Float64
.
Example:
The
erfc
function is particularly useful in statistics and probability theory, especially when dealing with normally distributed data. It’s often used in scenarios where high precision is required for large input values, as it avoids the loss of significance that can occur when subtracting erf(x) from 1 for large xlgamma
Returns the logarithm of the gamma function. Syntax:x
(numeric): A numeric value. Can be any of:Int8
,UInt8
,Int16
,UInt16
,Int32
,UInt32
,Int64
,UInt64
Float32
,Float64
Decimal32
,Decimal64
,Decimal128
,Decimal256
- The logarithm of the gamma function of
x
. - Type:
Float64
The
lgamma
function is particularly useful when dealing with large factorials or in statistical distributions where the gamma function appears, such as in the chi-squared distribution or in Bayesian statistics.x
(numeric): A numeric value. Type:(U)Int*
,Float*
, orDecimal*
.
- The gamma function of
x
. Type:Float64
.
The gamma function is an extension of the factorial function to real and complex numbers. For positive integers,
tgamma(n)
is equal to (n-1)!
.sin
Returns the sine of the argument. Syntax:x
(numeric): The angle in radians. AcceptsUInt*
,Int*
,Float*
, orDecimal*
types.
- The sine of
x
. Type:Float64
.
The
sin
function operates on radians, not degrees. To convert degrees to radians, you can use the radians
function or multiply by π/180.cos
Returns the cosine of the argument. Syntax:x
(numeric): The angle in radians. Accepts numeric types:Int
,UInt
,Float
, orDecimal
.
- The cosine of
x
. - Type:
Float64
taco_angle
is in radians, where:
- 0 radians (0°) gives a cosine of 1
- π/2 radians (90°) gives a cosine very close to 0
- π radians (180°) gives a cosine of -1
The slight deviation from 0 for π/2 is due to floating-point precision limitations.
tan
Returns the tangent of the argument. Syntax:x
(numeric): The angle in radians. Accepts numeric types:Int
,UInt
,Float
, orDecimal
.
- The tangent of
x
. - Type:
Float64
The
tan
function can produce very large values for angles close to π/2 + πn, where n is an integer. Be cautious when working with angles near these values.asin
Returns the arc sine of the argument. Syntax:x
(numeric): The value to calculate the arc sine for. Acceptable types:UInt*
,Int*
,Float*
, orDecimal*
.
- The arc sine of
x
in radians. Type:Float32
orFloat64
.
- The function expects input values in the range [-1, 1].
- For values outside this range, the function returns
NaN
(Not a Number). - The returned value is in radians, not degrees.
asin
to calculate a “filling angle” based on the weight of taco fillings. The angle could represent how high the filling might stack in the taco shell, with heavier fillings resulting in a higher angle.
acos
Returns the arc cosine of the argument. Syntax:x
(numeric): A number between -1 and 1. Can be any numeric type:Int
,Float
, orDecimal
.
- The arc cosine of
x
in radians, ranging from 0 to π. - Type:
Float64
If the input value is outside the range [-1, 1], the function returns
nan
(Not a Number).atan
Returns the arc tangent of the argument. Syntax:x
(numeric): The input value. Can be any numeric type:UInt*
,Int*
,Float*
, orDecimal*
.
- The arc tangent of
x
in radians. Type:Float64
.
The returned value is in the range -π/2 to π/2 radians.
pow
Calculates x raised to the power of y. Syntax:- power(x, y)
x
(UInt8
,UInt16
,UInt32
,UInt64
,Int8
,Int16
,Int32
,Int64
,Float32
,Float64
, orDecimal
): Base.y
(UInt8
,UInt16
,UInt32
,UInt64
,Int8
,Int16
,Int32
,Int64
,Float32
,Float64
, orDecimal
): Exponent.
- The result of raising x to the power of y.
Float64
Example:
The
pow
function is particularly useful when you need to perform exponential calculations or create non-linear scales based on input values.x
(Float64
): The angle, in radians. Values from the interval: −∞ < x < +∞.
- The hyperbolic cosine of
x
. Values from the interval: 1 ≤ cosh(x) < +∞. - Type:
Float64
.
The
cosh
function is commonly used in various mathematical and physical calculations, particularly in problems involving hyperbolic geometry or signal processing.acosh
Calculates the inverse hyperbolic cosine of a number. Syntaxx
(Float64
): The input value. Must be greater than or equal to 1.
- The inverse hyperbolic cosine of
x
. Type:Float64
.
acosh
function returns the inverse hyperbolic cosine of x
. This is the value whose hyperbolic cosine is x
.
For input values less than 1, the function returns NaN
(Not a Number).
Example
acosh
function as a metaphor for stack complexity.
sinh
Returns the hyperbolic sine of the argument. Syntax:x
(Float64
): The angle, in radians. Values from the interval: −∞ < x < +∞.
- The hyperbolic sine of
x
. Type:Float64
. - Values from the interval: −∞ < sinh(x) < +∞.
The
sinh
function is particularly useful in various mathematical and physical calculations, especially those involving exponential growth or decay, such as in signal processing or solving differential equations.asinh
Calculates the inverse hyperbolic sine of a number. Syntaxx
(numeric): The value to calculate the inverse hyperbolic sine for. Can be any numeric type:Int
,Float
, orDecimal
.
- The inverse hyperbolic sine of
x
. - Type:
Float64
asinh
function computes the inverse hyperbolic sine of x
. It is defined for all real numbers and is the inverse function of hyperbolic sine (sinh
).
For small values of x
, asinh(x)
is approximately equal to x
.
Example
asinh(0)
returns exactly 0.asinh(1)
is approximately 0.8813736, rounded to 7 decimal places.asinh(-2)
is approximately -1.4436355, rounded to 7 decimal places.
The
asinh
function is particularly useful in statistical and scientific computations, especially when dealing with hyperbolic geometries or certain types of distributions.tanh
Returns the hyperbolic tangent of the argument. Syntax:x
(numeric): The angle, in radians. Values from the interval: −∞ < x < +∞. SupportsUInt*
,Int*
,Float*
, orDecimal*
types.
- Values from the interval: −1 < tanh(x) < 1.
- Type:
Float64
The
tanh
function is commonly used in machine learning, particularly in neural networks as an activation function. It maps input values to output values between -1 and 1, with a steeper slope near 0.tanh
to normalize taco spiciness ratings. The original ratings range from -2 (very mild) to 2 (extremely spicy), and tanh
maps these to values between -1 and 1, providing a normalized measure of spiciness that can be useful for comparisons or further calculations.
atanh
Returns the inverse hyperbolic tangent of the argument. Syntax:x
(numeric): Hyperbolic tangent of angle. Values from the interval: -1 < x < 1. SupportsUInt*
,Int*
,Float*
, orDecimal*
types.
- The angle, in radians. Values from the interval: -∞ < atanh(x) < +∞.
- Type:
Float64
The
atanh
function is particularly useful in various mathematical and scientific computations, especially in fields like physics and engineering where hyperbolic functions are commonly used.atan2
Returns the angle in radians between the positive x-axis and the ray from the origin to the point (y, x) in the Euclidean plane. Syntax:y
(Float64
): y-coordinate of the point.x
(Float64
): x-coordinate of the point.
- The angle θ in radians, such that -π < θ ≤ π.
- Type:
Float64
The
atan2
function is particularly useful in scenarios where you need to calculate angles or directions, such as in navigation systems or when working with polar coordinates.hypot
Calculates the length of the hypotenuse of a right-angle triangle. Syntax:x
(Float64
): The length of the first cathetus.y
(Float64
): The length of the second cathetus.
Float64
]
Description:
This function computes the length of the hypotenuse of a right-angle triangle using the formula: sqrt(x^2 + y^2)
. It’s designed to avoid problems that can occur when squaring very large or very small numbers.
Example:
hypot
to calculate the straight-line distance between taco stands and the center of Los Angeles (latitude 34.0522, longitude -118.2437). The results are ordered to show the closest stands first.
This example assumes latitude and longitude are stored as decimal degrees. In real-world applications, more sophisticated geospatial functions would typically be used for accurate distance calculations on a spherical surface.
log1p
Calculateslog(1+x)
. This calculation is more accurate than log(1+x)
for small values of x.
Syntax:
x
(numeric): A value from the interval: -1 < x < +∞. Can be any numeric type:Int
,Float
, orDecimal
.
- The natural logarithm of 1 plus the argument. Type:
Float64
. - Values range from -∞ < log1p(x) < +∞.
log1p(0)
, which is equivalent to log(1+0)
, resulting in 0.
Example:
log1p
of taco prices. This transformation can be useful for various statistical analyses or machine learning models where you want to compress the range of prices while maintaining the ability to differentiate between small price differences.
The
log1p
function is particularly useful when dealing with values close to zero, as it provides better numerical stability compared to calculating log(1+x)
directly.sign
Returns the sign of a numeric value. Syntax:x
(numeric): A numeric value. Supports all numeric types in ClickHouse.
-1
for x < 00
for x = 01
for x > 0
Int8
Example:
- The
sign
function compares each taco price to $5. -1
indicates the taco is cheaper than $5.0
indicates the taco costs exactly $5.1
indicates the taco is more expensive than $5.
sigmoid
Returns the sigmoid function value for the given input. Syntax:x
(numeric): The input value. Can be any numeric type:UInt*
,Int*
,Float*
, orDecimal*
.
- The sigmoid function value for
x
, which is always between 0 and 1. Type:Float64
.
The sigmoid function is commonly used in machine learning, particularly in logistic regression and neural networks, as an activation function. It’s useful for transforming any input into a probability-like output between 0 and 1.
degrees
Converts radians to degrees. Syntax:x
(numeric): Input value in radians. Can be any numeric type:Int*
,UInt*
,Float*
, orDecimal*
.
- The angle in degrees. Type:
Float64
.
radians
Converts degrees to radians. Syntax:x
(numeric): Input in degrees. Can be any numeric type:UInt8
,UInt16
,UInt32
,UInt64
,Int8
,Int16
,Int32
,Int64
,Float32
,Float64
, orDecimal
.
Float64
.
Example:
The
radians
function is useful when you need to convert angle measurements from degrees to radians for trigonometric calculations or when working with circular data in a taco analysis, such as calculating the optimal angle for folding a taco shell.factorial
Computes the factorial of an integer value. Syntax:n
(UInt8
,UInt16
,UInt32
,UInt64
,Int8
,Int16
,Int32
,Int64
): An integer value.
UInt64
]
Details:
- The factorial of 0 is 1.
- For any negative input value, the function returns 1.
- The maximum allowed input value is 20. Values of 21 or greater will cause an exception.
five_factorial
calculates 5! which is 5 * 4 * 3 * 2 * 1 = 120.zero_factorial
demonstrates that 0! is defined as 1.negative_factorial
shows that the function returns 1 for negative inputs.max_factorial
calculates 20!, which is the largest factorial the function can compute.
Attempting to calculate the factorial of a number greater than 20 will result in an exception. For example,
SELECT factorial(21)
would throw an error.widthBucket
Returns the bucket number into which theoperand
value falls in a histogram with count
equal-width buckets spanning the range from low
to high
.
Syntax:
- WIDTH_BUCKET
operand
(numeric): The value to be bucketed. Can be any native number type.low
(numeric): The lower bound of the range. Can be any native number type.high
(numeric): The upper bound of the range. Can be any native number type.count
(UInt64
): The number of buckets. Must be an unsigned native integer and cannot be zero.
- The bucket number (1-based) as a
UInt64
. - Returns 0 if
operand < low
. - Returns
count + 1
ifoperand >= high
.
- Bucket 1 represents tacos priced from 3.99
- Bucket 2 represents tacos priced from 6.99
- Bucket 3 represents tacos priced from 9.99
- Bucket 4 represents tacos priced $10.00 and above
proportionsZTest
Performs a two-proportion Z-test, which is a statistical test for comparing proportions from two populations. Syntax:successes_x
(UInt64
): Number of successes in population x.successes_y
(UInt64
): Number of successes in population y.trials_x
(UInt64
): Number of trials in population x.trials_y
(UInt64
): Number of trials in population y.conf_level
(Float64
): Confidence level for the test (e.g., 0.95 for 95% confidence).pool_type
(String
): Method for estimating standard error. Can be either ‘unpooled’ or ‘pooled’.
Float64
values:
z_stat
- The Z statistic.p_val
- The p-value.ci_low
- The lower bound of the confidence interval.ci_high
- The upper bound of the confidence interval.
- In the pooled version, the two proportions are averaged, and only one proportion is used to estimate the standard error.
- In the unpooled version, the two proportions are used separately.