====================== Mathematical Functions ====================== .. spark:function:: abs(x) -> [same as x] (ANSI compliant) Returns the absolute value of ``x``. When ``x`` is negative minimum value of integral type returns the same value as ``x`` following the behavior when Spark ANSI mode is disabled and throws exception when Spark ANSI mode is enabled. :: SELECT abs(-42); -- 42 SELECT abs(3.14); -- 3.14 SELECT abs(-128); -- 128 (with ANSI mode disabled) SELECT abs(-128); -- Overflow exception (with ANSI mode enabled for TINYINT) .. spark:function:: acos(x) -> double Returns the inverse cosine (a.k.a. arc cosine) of ``x``. .. spark:function:: acosh(x) -> double Returns inverse hyperbolic cosine of ``x``. .. spark:function:: asin(x) -> double Returns the arc sine of ``x``. .. spark:function:: asinh(x) -> double Returns inverse hyperbolic sine of ``x``. .. spark:function:: atan(x) -> double Returns the arc tangent of ``x``. .. spark:function:: atan2(y, x) -> double Returns the arc tangent of ``y / x``. For compatibility with Spark, returns 0 for the following corner cases: * atan2(0.0, 0.0) * atan2(-0.0, -0.0) * atan2(-0.0, 0.0) * atan2(0.0, -0.0) .. spark:function:: atanh(x) -> double Returns inverse hyperbolic tangent of ``x``. .. spark:function:: add(x, y) -> [same as x] Returns the result of adding x to y. The types of x and y must be the same. For interval inputs (``interval day to second`` and ``interval year to month``), overflow always throws an exception. Corresponds to Spark's operator ``+``. .. spark:function:: add(x, y) -> decimal Returns the result of adding ``x`` to ``y``. The argument types should be DECIMAL, and can have different precisions and scales. Fast path is implemented for cases that should not overflow. For the others, the whole parts and fractional parts of input decimals are added separately and combined finally. The result type is calculated with the max precision of input precisions, the max scale of input scales, and one extra digit for possible carrier. Overflow results in null output. Corresponds to Spark's operator ``+``. :: SELECT CAST(1.1232100 as DECIMAL(38, 7)) + CAST(1 as DECIMAL(10, 0)); -- DECIMAL(38, 6) 2.123210 SELECT CAST(-999999999999999999999999999.999 as DECIMAL(30, 3)) + CAST(-999999999999999999999999999.999 as DECIMAL(30, 3)); -- DECIMAL(31, 3) -1999999999999999999999999999.998 SELECT CAST(99999999999999999999999999999999.99998 as DECIMAL(38, 6)) + CAST(-99999999999999999999999999999999.99999 as DECIMAL(38, 5)); -- DECIMAL(38, 6) -0.000010 SELECT CAST(-99999999999999999999999999999999990.0 as DECIMAL(38, 3)) + CAST(-0.00001 as DECIMAL(38, 7)); -- DECIMAL(38, 6) NULL .. spark:function:: bin(x) -> varchar Returns the string representation of the long value ``x`` represented in binary. .. spark:function:: cbrt(x) -> double Returns the cube root of ``x``. .. spark:function:: ceil(x) -> [same as x] (ANSI compliant) Returns ``x`` rounded up to the nearest integer. Supported types are: BIGINT, DOUBLE and DECIMAL. For DOUBLE input the result is BIGINT: NaN returns 0, and a result outside the BIGINT range saturates to the BIGINT limits, or is an error when Spark ANSI mode is enabled (``spark.ansi_enabled``). The error matches Spark 4.3 and later; Spark 3.5 to 4.2 saturate in both modes. .. spark:function:: checked_add(x, y) -> [same as x] :noindex: Returns the result of adding x to y. The types of x and y must be the same. For integral and interval types, overflow results in an error. Corresponds to Spark's operator ``+`` with ``failOnError`` as true. .. spark:function:: checked_div(x, y) -> bigint :noindex: Returns the result of integer division of ``x`` by ``y``, truncating toward zero. Supported types are integral types, ``x`` and ``y`` must have the same type. Division by zero or overflow results in an error. This function operates in ANSI mode (error on invalid input). Corresponds to Spark's operator ``div`` with ``spark.sql.ansi.enabled`` set to true. .. spark:function:: checked_divide(x, y) -> [same as x] Returns the results of dividing x by y. The types of x and y must be the same. Division by zero results in an error. Corresponds to Spark's operator ``/`` with ``failOnError`` as true. .. spark:function:: checked_multiply(x, y) -> [same as x] :noindex: Returns the result of multiplying x by y. The types of x and y must be the same. For integral types, overflow results in an error. Corresponds to Spark's operator ``*`` with ``failOnError`` as true. .. spark:function:: checked_subtract(x, y) -> [same as x] :noindex: Returns the result of subtracting y from x. The types of x and y must be the same. For integral and interval types, overflow results in an error. Corresponds to Spark's operator ``-`` with ``failOnError`` as true. .. spark:function:: cos(x) -> double Returns the cosine of ``x``. .. spark:function:: cosh(x) -> double Returns the hyperbolic cosine of ``x``. .. spark:function:: cot(x) -> double Returns the cotangent of ``x``(measured in radians). Supported type is DOUBLE. .. spark:function:: csc(x) -> double Returns the cosecant of ``x``. .. spark:function:: decimal_ceil(x, scale) -> decimal Rounds the decimal value ``x`` up (toward positive infinity) to the specified target ``scale``. The first argument must be a DECIMAL and the second must be a constant INTEGER representing the desired number of digits after the decimal point. When ``scale`` is negative, rounds up to a power of 10 (e.g., scale = -2 rounds up to the nearest hundred). When ``scale`` >= the input scale, returns the value unchanged. Values that overflow the result precision raise a user error regardless of ANSI mode; ``try`` catches errors per row. Shares scale handling and result typing with :spark:func:`decimal_round`. All constant INTEGER scales are accepted. For example, rounding DECIMAL(38,38) 0.1 to scale -1 returns 10, not zero. When a negative scale requests a quantum larger than the result precision, positive nonzero inputs raise an error; negative inputs and zero return zero. A typed NULL scale returns NULL without evaluating the runtime decimal child; its result type uses scale zero. **Result type rules:** The result type is determined by the input type ``DECIMAL(p, s)`` and the target ``scale`` (n): * When n >= 0: result is ``DECIMAL(min(p - s + 1 + min(s, n), 38), min(s, n))``. The ``+1`` accounts for carry (e.g., ``ceil(9.9, 0)`` = 10 needs an extra digit). The result scale is ``min(s, n)`` — it never pads with trailing zeros beyond the input scale. * When n < 0: result is ``DECIMAL(min(max(p - s + 1, |n| + 1), 38), 0)``. The result scale is always 0. ``|n| + 1`` digits are needed because rounding to the nearest 10^|n| can produce a value one digit wider. See https://github.com/apache/spark/blob/v4.1.1/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/mathExpressions.scala. Examples (type annotations for clarity, not executable SQL):: -- scale 0: round up to integer SELECT decimal_ceil(DECIMAL(4,3) 1.234, 0); -- 2 (DECIMAL(2,0)) SELECT decimal_ceil(DECIMAL(4,3) -1.234, 0); -- -1 (DECIMAL(2,0)) -- scale 1: one fractional digit SELECT decimal_ceil(DECIMAL(4,3) 1.234, 1); -- 1.3 (DECIMAL(3,1)) SELECT decimal_ceil(DECIMAL(4,3) -1.234, 1); -- -1.2 (DECIMAL(3,1)) -- scale 2: two fractional digits SELECT decimal_ceil(DECIMAL(4,3) 1.234, 2); -- 1.24 (DECIMAL(4,2)) -- scale >= input scale: identity (no rounding needed) SELECT decimal_ceil(DECIMAL(3,1) 10.1, 5); -- 10.1 (DECIMAL(4,1)) -- negative scale: round up to power of 10 SELECT decimal_ceil(DECIMAL(3,1) 99.0, -1); -- 100 (DECIMAL(3,0)) .. spark:function:: decimal_floor(x, scale) -> decimal Rounds the decimal value ``x`` down (toward negative infinity) to the specified target ``scale``. The first argument must be a DECIMAL and the second must be a constant INTEGER representing the desired number of digits after the decimal point. When ``scale`` is negative, rounds down to a power of 10 (e.g., scale = -2 rounds down to the nearest hundred). When ``scale`` >= the input scale, returns the value unchanged. Shares the scale validation, NULL-scale handling and row-local overflow errors of :spark:func:`decimal_ceil`. When the quantum is outside the result precision, negative nonzero inputs raise an error; positive inputs and zero return zero. **Result type rules:** Same as ``decimal_ceil`` — see above. Examples (type annotations for clarity, not executable SQL):: -- scale 0: round down to integer SELECT decimal_floor(DECIMAL(4,3) 1.234, 0); -- 1 (DECIMAL(2,0)) SELECT decimal_floor(DECIMAL(4,3) -1.234, 0); -- -2 (DECIMAL(2,0)) -- scale 1: one fractional digit SELECT decimal_floor(DECIMAL(4,3) 1.234, 1); -- 1.2 (DECIMAL(3,1)) SELECT decimal_floor(DECIMAL(4,3) -1.234, 1); -- -1.3 (DECIMAL(3,1)) -- scale 2: two fractional digits SELECT decimal_floor(DECIMAL(4,3) 1.234, 2); -- 1.23 (DECIMAL(4,2)) -- scale >= input scale: identity (no rounding needed) SELECT decimal_floor(DECIMAL(3,1) 10.1, 5); -- 10.1 (DECIMAL(4,1)) -- negative scale: round down to power of 10 SELECT decimal_floor(DECIMAL(3,1) 99.0, -1); -- 90 (DECIMAL(3,0)) .. spark:function:: degrees(x) -> double Converts angle x in radians to degrees. .. spark:function:: div(x, y) -> bigint Returns the results of dividing x by y. Performs the integer division truncates toward zero. Supported types are integral types, x and y must have the same type. Division by zero or overflow results in null. :: SELECT 3 div 2; -- 1 SELECT 1L div 2L; -- 0 SELECT 3 div 0; -- NULL .. spark:function:: divide(x, y) -> double Returns the results of dividing x by y. Performs floating point division. Supported type is DOUBLE. Corresponds to Spark's operator ``/``. :: SELECT 3 / 2; -- 1.5 SELECT 2L / 2L; -- 1.0 SELECT 3 / 0; -- NULL .. spark:function:: divide(x, y) -> decimal Returns the results of dividing x by y. Supported type is DECIMAL which can be different precision and scale. Performs floating point division. The result type depends on the precision and scale of x and y. Overflow results return null. Corresponds to Spark's operator ``/``. :: SELECT CAST(1 as DECIMAL(17, 3)) / CAST(2 as DECIMAL(17, 3)); -- decimal 0.500000000000000000000 SELECT CAST(1 as DECIMAL(20, 3)) / CAST(20 as DECIMAL(20, 2)); -- decimal 0.0500000000000000000 SELECT CAST(1 as DECIMAL(20, 3)) / CAST(0 as DECIMAL(20, 3)); -- NULL .. spark:function:: exp(x) -> double Returns Euler's number raised to the power of ``x``. .. spark:function:: expm1(x) -> double Returns Euler's number raised to the power of ``x``, minus 1, which is ``exp(x) - 1`` in math. This function expm1(x) is more accurate than ``exp(x) - 1``, when ``x`` is close to zero. If the argument is NaN, the result is NaN. If the argument is positive infinity, then the result is positive infinity. If the argument is negative infinity, then the result is -1.0. If the argument is zero, then the result is a zero with the same sign as the argument. .. spark:function:: factorial(x) -> bigint Returns the factorial of integer ``x``, defined as ``x! = x * (x-1) * ... * 1``. The input must be between 0 and 20 inclusive. Returns NULL for values outside this range. :: SELECT factorial(0); -- 1 SELECT factorial(5); -- 120 SELECT factorial(20); -- 2432902008176640000 SELECT factorial(21); -- NULL SELECT factorial(-1); -- NULL .. spark:function:: floor(x) -> [same as x] (ANSI compliant) Returns ``x`` rounded down to the nearest integer. Supported types are: BIGINT, DOUBLE and DECIMAL. For DOUBLE input the result is BIGINT: NaN returns 0, and a result outside the BIGINT range saturates to the BIGINT limits, or is an error when Spark ANSI mode is enabled (``spark.ansi_enabled``). The error matches Spark 4.3 and later; Spark 3.5 to 4.2 saturate in both modes. .. spark:function:: hex(x) -> varchar Converts ``x`` to hexadecimal. Supported types are: BIGINT, VARBINARY and VARCHAR. If the argument is a VARCHAR or VARBINARY, the result is string where each input byte is represented using 2 hex characters. If the argument is a positive BIGINT, the result is a hex representation of the number (up to 16 characters), if the argument is a negative BIGINT, the result is a hex representation of the number which will be treated as two's complement. :: SELECT hex("Spark SQL"); -- 537061726B2053514C SELECT hex(17); -- 11 SELECT hex(-1); -- FFFFFFFFFFFFFFFF .. spark:function:: hypot(a, b) -> double Returns the square root of `a` squared plus `b` squared. .. spark:function:: isnan(x) -> boolean Returns true if x is Nan, or false otherwise. Returns false is x is NULL. Supported types are: REAL, DOUBLE. .. spark:function:: ln(x) -> double Returns the natural logarithm of ``x``. Return null for zero and non-positive input. .. spark:function:: log(base, expr) -> double Returns the logarithm of ``expr`` with ``base``. Returns NULL if either ``expr`` or ``base`` is less than or equal to 0. .. spark:function:: log1p(x) -> double Returns the natural logarithm of the “given value ``x`` plus one”. Returns NULL if x is less than or equal to -1. .. spark:function:: log2(x) -> double Returns the logarithm of ``x`` with base 2. Return null for zero and non-positive input. .. spark:function:: log10(x) -> double Returns the logarithm of ``x`` with base 10. Return null for zero and non-positive input. .. spark:function:: sqrt(x) -> double Returns the square root of ``x``. .. spark:function:: multiply(x, y) -> [same as x] Returns the result of multiplying x by y. The types of x and y must be the same. Corresponds to Spark's operator ``*``. .. spark:function:: multiply(x, y) -> [decimal] Returns the result of multiplying x by y. The types of x and y must be decimal which can be different precision and scale. The result type depends on the precision and scale of x and y. Overflow results return null. Corresponds to Spark's operator ``*``. :: SELECT CAST(1 as DECIMAL(17, 3)) * CAST(2 as DECIMAL(17, 3)); -- decimal 2.000000 SELECT CAST(1 as DECIMAL(20, 3)) * CAST(20 as DECIMAL(20, 2)); -- decimal 20.00000 SELECT CAST(1 as DECIMAL(20, 3)) * CAST(0 as DECIMAL(20, 3)); -- decimal 0.000000 SELECT CAST(201e-38 as DECIMAL(38, 38)) * CAST(301e-38 as DECIMAL(38, 38)); -- decimal 0.0000000000000000000000000000000000000 .. spark:function:: normalize_nan_and_zero(x) -> [same as x] Returns 0.0 if ``x`` is -0.0, the canonical NaN if ``x`` is any NaN, and ``x`` otherwise. The canonical NaN is Java's ``Double.NaN`` (``0x7ff8000000000000``) for DOUBLE and ``Float.NaN`` (``0x7fc00000``) for REAL. Returns NULL if ``x`` is NULL. Supported types are: REAL, DOUBLE. Note: This function is not directly callable in Spark SQL, but is used internally by `Spark's NormalizeFloatingNumbers `_. :: -- Function call examples (this function is not directly callable in Spark SQL). normalize_nan_and_zero(-0.0) -- 0.0 normalize_nan_and_zero(0.0) -- 0.0 normalize_nan_and_zero(1.5) -- 1.5 normalize_nan_and_zero(NaN) -- NaN normalize_nan_and_zero(-NaN) -- NaN normalize_nan_and_zero(NULL) -- NULL .. spark:function:: not(x) -> boolean Logical not. :: SELECT not true; -- false SELECT not false; -- true SELECT not NULL; -- NULL .. spark:function:: pmod(n, m) -> [same as n] Returns the positive remainder of n divided by m. Supported types are: TINYINT, SMALLINT, INTEGER, BIGINT, REAL and DOUBLE. .. spark:function:: power(x, p) -> double Returns ``x`` raised to the power of ``p``. .. spark:function:: radians(x) -> double Converts angle x in degrees to radians. .. spark:function:: rand() -> double Returns a random value with uniformly distributed values in [0, 1). :: SELECT rand(); -- 0.9629742951434543 .. spark:function:: rand(seed) -> double Returns a random value with uniformly distributed values in [0, 1) using a seed formed by combining user-specified ``seed`` and the configuration `spark.partition_id`. The framework is responsible for deterministic partitioning of the data and assigning unique `spark.partition_id` to each thread (in a deterministic way) . ``seed`` must be constant. NULL ``seed`` is identical to zero ``seed``. :: SELECT rand(0); -- 0.7604953758285915 SELECT rand(NULL); -- 0.7604953758285915 .. spark:function:: randn() -> double Returns a random value from the standard normal distribution (mean 0.0 and standard deviation 1.0). :: SELECT randn(); -- -1.1750280342265669 .. spark:function:: randn(seed) -> double Returns a random value from the standard normal distribution (mean 0.0 and standard deviation 1.0) using a seed formed by combining user-specified ``seed`` and the configuration `spark.partition_id`. The framework is responsible for deterministic partitioning of the data and assigning unique `spark.partition_id` to each thread (in a deterministic way). ``seed`` must be constant. NULL ``seed`` is identical to zero ``seed``. :: SELECT randn(0); -- 1.6034991609278433 SELECT randn(NULL); -- 1.6034991609278433 .. spark:function:: random() -> double An alias for ``rand()``. .. spark:function:: random(seed) -> double An alias for ``rand(seed)``. .. spark:function:: remainder(n, m) -> [same as n] (ANSI compliant) Returns the modulus (remainder) of ``n`` divided by ``m``. Corresponds to Spark's operator ``%``. Supported types are: TINYINT, SMALLINT, INTEGER, BIGINT, REAL and DOUBLE. When ``m`` is zero, returns NULL following the behavior when Spark ANSI mode is disabled, and throws an exception when Spark ANSI mode is enabled. .. spark:function:: rint(x) -> double Returns the double value that is closest in value to the argument and is equal to a mathematical integer. Returns ``x`` if ``x`` is a positive or negative infinity or a NaN. :: SELECT rint(12.3456); -- 12.0 .. spark:function:: round(x, d) -> [same as x] Returns ``x`` rounded to ``d`` decimal places using HALF_UP rounding mode. In HALF_UP rounding, the digit 5 is rounded up. Supported types for ``x`` are integral and floating point types. Omitting ``d`` is equivalent to ``d = 0``. For integral inputs, nonnegative ``d`` returns ``x`` unchanged. Negative ``d`` rounds to a multiple of ``10^(-d)``, with halfway values rounded away from zero. Integral overflow raises an error when ``spark.ansi_enabled`` is true; otherwise the result wraps to the input type's two's-complement representation. Integral inputs accept every INTEGER scale, including a scale column. Column scales are a native extension: Spark 4.1 requires a foldable scale expression, which need not be a literal. Extreme-scale behavior is not fully compatible with Spark 4.1. For BIGINT input ``25`` and scale ``-2147483648``, Velox returns zero whereas Spark's interpreted and generated evaluators throw. At scale ``2147483647``, Velox and Spark's generated evaluator return ``25``, but Spark's interpreted evaluator throws. Velox does not reproduce JVM BigDecimal/BigInteger scale and resource limits. .. spark:function:: sec(x) -> double Returns the secant of ``x``. .. spark:function:: sign(x) -> double Returns the signum of ``x``. Supported type for ``x`` is DOUBLE. It returns: * 0.0 if the argument is 0.0, * 1.0 if the argument is greater than 0.0, * -1.0 if the argument is less than 0.0, * NaN if the argument is NaN, * 1.0 if the argument is +Infinity, * -1.0 if the argument is -Infinity. .. spark:function:: sin(x) -> double Returns the sine of ``x``. .. spark:function:: sinh(x) -> double Returns hyperbolic sine of ``x``. .. spark:function:: subtract(x, y) -> [same as x] Returns the result of subtracting y from x. The types of x and y must be the same. For interval inputs (``interval day to second`` and ``interval year to month``), overflow always throws an exception. Corresponds to Spark's operator ``-``. .. spark:function:: subtract(x, y) -> decimal Returns the result of subtracting ``y`` from ``x``. Reuses the logic of add function for decimal type. Corresponds to Spark's operator ``-``. :: SELECT CAST(1.1232100 as DECIMAL(38, 7)) - CAST(1 as DECIMAL(10, 0)); -- DECIMAL(38, 6) 0.123210 SELECT CAST(-999999999999999999999999999.999 as DECIMAL(30, 3)) - CAST(-999999999999999999999999999.999 as DECIMAL(30, 3)); -- DECIMAL(31, 3) 0.000 SELECT CAST(99999999999999999999999999999999.99998 as DECIMAL(38, 6)) - CAST(-0.00001 as DECIMAL(38, 5)); -- DECIMAL(38, 6) 99999999999999999999999999999999.999990 SELECT CAST(-99999999999999999999999999999999990.0 as DECIMAL(38, 3)) - CAST(0.00001 as DECIMAL(38, 7)); -- DECIMAL(38, 6) NULL .. spark:function:: tan(x) -> double Returns the tangent of ``x``. .. spark:function:: tanh(x) -> double Returns the hyperbolic tangent of ``x``. .. spark:function:: try_add(x, y) -> [same as x] Returns the result of adding ``x`` to ``y``. The types of ``x`` and ``y`` must be the same. For integral types, overflow returns NULL. Corresponds to Spark's ``try_add``. Unlike ``try(checked_add(x, y))``, errors from ``x`` and ``y`` are not turned into NULL. :: SELECT try_add(2147483647, 1); -- NULL SELECT try_add(1, 2); -- 3 .. spark:function:: try_multiply(x, y) -> [same as x] Returns the result of multiplying ``x`` by ``y``. The types of ``x`` and ``y`` must be the same. For integral types, overflow returns NULL. Corresponds to Spark's ``try_multiply``. Unlike ``try(checked_multiply(x, y))``, errors from ``x`` and ``y`` are not turned into NULL. :: SELECT try_multiply(2147483647, 2); -- NULL SELECT try_multiply(3, -4); -- -12 .. spark:function:: try_subtract(x, y) -> [same as x] Returns the result of subtracting ``y`` from ``x``. The types of ``x`` and ``y`` must be the same. For integral types, overflow returns NULL. Corresponds to Spark's ``try_subtract``. Unlike ``try(checked_subtract(x, y))``, errors from ``x`` and ``y`` are not turned into NULL. :: SELECT try_subtract(-2147483648, 1); -- NULL SELECT try_subtract(5, 2); -- 3 .. spark:function:: unaryminus(x) -> [same as x] (ANSI compliant) Returns the negative of ``x``. Corresponds to Spark's operator ``-``. For integral inputs, negating the minimum value returns the same value when Spark ANSI mode is disabled and throws an overflow exception when Spark ANSI mode is enabled. For interval inputs (``interval day to second`` and ``interval year to month``), overflow always throws an exception. .. spark:function:: unhex(x) -> varbinary Converts hexadecimal varchar ``x`` to varbinary. ``x`` is considered case insensitive and expected to contain only hexadecimal characters 0-9 and A-F. If ``x`` contains non-hexadecimal character, the function returns NULL. When ``x`` contains an even number of characters, each pair is converted to a single byte. The number of bytes in the result is half the number of bytes in the input. When ``x`` contains an odd number of characters, the first character is decoded into the first byte of the result and the remaining pairs of characters are decoded into subsequent bytes. This behavior matches Spark 3.3.2 and newer. :: SELECT unhex("23"); -- # SELECT unhex("f"); -- \x0F SELECT unhex("b2323"); -- \x0B## SELECT unhex("G"); -- NULL SELECT unhex("G23"); -- NULL .. spark:function:: width_bucket(x, bound1, bound2, n) -> bigint Returns the zero-based bucket number to which ``x`` would be assigned in an equiwidth histogram with ``n`` buckets, in the range ``bound1`` to ``bound2``. `bound1` can be greater than `bound2`. If `bound1` less than `bound2`, if `x` less than `bound1` return 0, if `x` greater than or equal to `bound2` return n + 1. If `bound1` greater than `bound2`, if `x` greater than `bound1` return 0, if `x` less than or equal to `bound2` return n + 1. `n` must be a positive integral value. `x`, `bound1`, and `bound2` cannot be NaN. `bound1`, and `bound2` must be finite. `bound1` cannot equal `bound2`; Otherwise, the function will return NULL. :: SELECT width_bucket(-1.0, 0.0, 10.0, 5); -- 0 SELECT width_bucket(0.1, 0.0, 10.0, 5); -- 1 SELECT width_bucket(10.1, 0.0, 10.0, 5); -- 6 SELECT width_bucket(-1.0, 10.0, 0.0, 5); -- 6 SELECT width_bucket(0.1, 10.0, 0.0, 5); -- 5 SELECT width_bucket(10.1, 10.0, 0.0, 5); -- 0 SELECT width_bucket(10.1, 10.0, 10.0, 5); -- NULL