UInt8
, UInt16
, UInt32
, UInt64
, Int8
, Int16
, Int32
, Int64
, Float32
, or Float64
.
Type casting and result types
Before performing an operation, both operands are cast to the result type. The result type is determined as follows:- For operands up to 32 bits wide, the result type is the next larger type after the larger of the two operands (integer size promotion).
- For operands of 64 bits or more, the result type matches the size of the larger operand.
- If one operand is signed, the result type will be signed; otherwise, it will be unsigned.
ClickHouse function reference
plus
Calculates the sum of two values. It can be used with numeric types, dates, and timestamps. Syntax:a
(numeric, date, or timestamp): The first value to be added.b
(numeric, date, or timestamp): The second value to be added.
+
operator:
tacos_with_extras
adds 2 extra tacos to each order.delivery_date
calculates the delivery date by adding 1 day to the order date.latest_pickup_time
adds 1800 seconds (30 minutes) to the original pickup time.
minus
Calculates the difference between two values. It can be used with numeric types, dates, and timestamps. Syntax:a
(numeric, date, or timestamp): The value to subtract from.b
(numeric, date, or timestamp): The value to subtract.
-
operator:
tacos_without_extras
subtracts 2 tacos from each order.prep_date
calculates the preparation date by subtracting 1 day from the order date.earliest_pickup_time
subtracts 900 seconds (15 minutes) from the original pickup time.
multiply
Calculates the product of two numeric values. Syntax:*
operator:
a
(numeric): The first value to be multiplied.b
(numeric): The second value to be multiplied.
a
and b
.
Example:
multiply
calculates the total price for each taco order by multiplying the number of tacos by the price per taco.
divide
Calculates the quotient of two numeric values. The result is always of typeFloat64
.
Syntax:
/
operator:
a
(numeric): The dividend.b
(numeric): The divisor.
Float64
value representing the quotient.
- Division by 0 returns
inf
,-inf
, ornan
. - For integer division, use the
intDiv
function instead.
divide
calculates the price per taco by dividing the total price by the number of tacos for each order.
intDiv
Performs integer division of two numeric values, rounding down to the nearest integer. Syntax:a
(numeric): The dividend.b
(numeric): The divisor.
intDiv
calculates the price per taco by dividing the total price by the number of tacos, rounding down to the nearest integer.
Note that for order 2, the actual price per taco would be 6.67, but
intDiv
rounds it down to 6.intDivOrZero
Performs integer division of two values, returning zero when dividing by zero or when dividing the minimum negative number by -1. Syntax:a
(Integer): The dividend.b
(Integer): The divisor.
a
divided by b
, or zero if b
is zero or if dividing the minimum negative number by -1. [Integer]
Example:
intDivOrZero
calculates how many tacos each person can have based on the total number of tacos and available people. For order 3, where there are no available people (division by zero), the result is 0 instead of throwing an error.
isFinite
Checks whether a floating-point value is finite (not infinite and not NaN). Syntax:x
(Float32
orFloat64
): The value to check.
1
if the value is finite, 0
otherwise. [ UInt8
]
Example:
isFinite
checks if each taco_price
is a valid finite number. The result 1
indicates a finite price, while 0
indicates an infinite or NaN price.
isInfinite
Checks if a floating-point value is infinite. Syntax:x
(Float32
orFloat64
): The value to check.
1
if x
is infinite, 0
otherwise. Note that 0
is returned for NaN values. [ UInt8
]
Example:
isInfinite
checks if each taco_price
is infinite. The result 1
indicates an infinite price, while 0
indicates a finite price or NaN.
ifNotFinite
Checks whether a floating-point value is finite and returns either the original value or a fallback value. Syntax:x
(Float32
orFloat64
): Value to check for finiteness.y
(Float32
orFloat64
): Fallback value to return ifx
is not finite.
x
ifx
is finitey
ifx
is not finite (infinity or NaN)
ifNotFinite
is used to handle potential division by zero when calculating the taco price. If the division results in infinity (due to division by zero), the function returns the fallback price of 9.99.
isNaN
Checks if a floating-point value is NaN (Not a Number). Syntax:x
(Float32
orFloat64
): The value to check.
1
if the argument is NaN, 0
otherwise. [ UInt8
]
Example:
isNaN
checks if each taco_price
is NaN. The result 1
indicates that the price is NaN, while 0
indicates a valid numeric price.
modulo
Calculates the remainder of the division of two values. Syntax:a
(numeric): The dividend.b
(numeric): The divisor.
a
divided by b
. The result type is:
- Integer if both inputs are integers
- Float64 if one of the inputs is a floating-point number
Uses truncated division for negative numbers (like C++). Throws an exception when dividing by zero or when dividing the minimum negative number by -1.
modulo
calculates how many tacos are left over when dividing the order quantity by 3 (e.g. for packaging in boxes of 3).
moduloOrZero
Calculates the remainder of the division of two values, returning zero if the divisor is zero. Syntax:a
(numeric): The dividend.b
(numeric): The divisor.
a
divided by b
, or zero if b
is zero. [Numeric]
Example:
moduloOrZero
calculates how many tacos are left over when dividing the number of tacos by 3 (e.g., for grouping into sets of 3). If num_tacos
were 0, the result would be 0 instead of throwing an error.
positiveModulo
Calculates the positive modulo of two values. It always returns a non-negative number. Syntax:a
(numeric): The dividend.b
(numeric): The divisor.
a
divided by b
. [Numeric]
Aliases:
positive_modulo(a, b)
pmod(a, b)
positiveModulo
calculates how many tacos are left over when dividing the order into groups of 3. The result is always non-negative, even for negative inputs.
This function is 4-5 times slower than the regular
modulo
function.negate
Negates a numeric value. The result is always signed. Syntax:a
(numeric): The numeric value to negate.
a
. [Numeric]
Example:
negate
and the unary minus operator are used to calculate the negative value of each taco price.
abs
Calculates the absolute value of a numeric input. Syntax:a
(numeric): The numeric value to calculate the absolute value of.
abs
calculates the absolute value of taco_price
, removing the negative sign for orders 1 and 3 while leaving the positive value unchanged for order 2.
gcd
Calculates the greatest common divisor (GCD) of two numeric values. Syntax:a
(numeric): First numeric value.b
(numeric): Second numeric value.
a
and b
. [Numeric]
Example:
gcd
calculates the greatest common divisor between the number of tacos and sides for each order. A common divisor of 1 indicates the numbers are coprime.
An exception is thrown when dividing by zero or when dividing a minimal negative number by minus one.
lcm
Calculates the least common multiple of two numeric values. Syntax:a
(numeric): First numeric value.b
(numeric): Second numeric value.
a
and b
. [Numeric]
Example:
lcm
calculates the least common multiple of the number of tacos and sides for each order. This could be useful for determining how many complete “sets” of the order can be made without leftover items.
An exception is thrown when dividing by zero or when dividing a minimal negative number by minus one.
max2
Returns the larger of two numeric values. Syntax:a
(numeric): First numeric value.b
(numeric): Second numeric value.
a
and b
. [Float64
]
Example:
max2
compares num_tacos
and extra_tacos
for each order, returning the larger value as max_tacos
.
min2
Returns the smaller of two numeric values. Syntax:a
(numeric): First numeric value.b
(numeric): Second numeric value.
Float64
]
Example:
min2
returns the smaller value between num_tacos
and extra_tacos
for each order. The result is stored in the min_tacos
column.
multiplyDecimal
Multiplies two decimal values with precise control over the result scale. Syntax:a
(Decimal): First decimal value.b
(Decimal): Second decimal value.result_scale
(Int/UInt): (Optional) Scale of the result.
a
and b
as a Decimal256
value with the specified scale.
Example:
multiplyDecimal
calculates the total price by multiplying the taco price by the quantity, with the result rounded to 2 decimal places.
This function is slower than regular multiplication but provides precise control over the result’s scale. Use regular multiplication when speed is more important than exact decimal precision.
divideDecimal
Divides two decimal values with precise control over the result scale. Syntax:a
(Decimal): The dividend (numerator).b
(Decimal): The divisor (denominator).result_scale
(Int/UInt): (Optional) The scale of the result.
Decimal256
]
Example:
divideDecimal
calculates the price per taco by dividing the total price by the number of tacos, with the result rounded to 2 decimal places.
This function is significantly slower than regular division. Use the standard
divide
function if precise control over the result scale is not required or if faster computation is needed.byteSwap
Reverses the bytes of an integer, changing its endianness. Syntax:a
(Integer): The integer value to reverse.
byteSwap
reverses the bytes of the taco_order_id
. The process can be understood as:
- Convert 3351772109 to hexadecimal: C7 C7 FB CD
- Reverse the bytes: CD FB C7 C7
- Convert back to decimal: 3455829959