Impala Mathematical Functions

Mathematical functions, or arithmetic functions, perform numeric calculations that are typically more complex than basic addition, subtraction, multiplication, and division. For example, these functions include trigonometric, logarithmic, and base conversion operations.

Note: In Impala, exponentiation uses the pow() function rather than an exponentiation operator such as **.

Related information:

The mathematical functions operate mainly on these data types: INT Data Type, BIGINT Data Type, SMALLINT Data Type, TINYINT Data Type, DOUBLE Data Type, FLOAT Data Type, and DECIMAL Data Type (Impala 3.0 or higher only). For the operators that perform the standard operations such as addition, subtraction, multiplication, and division, see Arithmetic Operators.

Functions that perform bitwise operations are explained in Impala Bit Functions.

Function reference:

Impala supports the following mathematical functions:

abs(numeric_type a)

Purpose: Returns the absolute value of the argument.

Return type: Same as the input value

Usage notes: Use this function to ensure all return values are positive. This is different than the positive() function, which returns its argument unchanged (even if the argument was negative).

acos(double a)

Purpose: Returns the arccosine of the argument.

Return type: double

asin(double a)

Purpose: Returns the arcsine of the argument.

Return type: double

atan(double a)

Purpose: Returns the arctangent of the argument.

Return type: double

atan2(double a, double b)

Purpose: Returns the arctangent of the two arguments, with the signs of the arguments used to determine the quadrant of the result.

Return type: double

bin(bigint a)

Purpose: Returns the binary representation of an integer value, that is, a string of 0 and 1 digits.

Return type: string

ceil(double a), ceil(decimal(p,s) a), ceiling(double a), ceiling(decimal(p,s) a), dceil(double a), dceil(decimal(p,s) a)

Purpose: Returns the smallest integer that is greater than or equal to the argument.

Return type: Same as the input value

conv(bigint num, int from_base, int to_base), conv(string num, int from_base, int to_base)

Purpose: Returns a string representation of an integer value in a particular base. The input value can be a string, for example to convert a hexadecimal number such as fce2 to decimal. To use the return value as a number (for example, when converting to base 10), use CAST() to convert to the appropriate type.

Return type: string

cos(double a)

Purpose: Returns the cosine of the argument.

Return type: double

cosh(double a)

Purpose: Returns the hyperbolic cosine of the argument.

Return type: double

cot(double a)

Purpose: Returns the cotangent of the argument.

Return type: double

Added in: Impala 2.3.0

degrees(double a)

Purpose: Converts argument value from radians to degrees.

Return type: double

e()

Purpose: Returns the mathematical constant e.

Return type: double

exp(double a), dexp(double a)

Purpose: Returns the mathematical constant e raised to the power of the argument.

Return type: double

factorial(integer_type a)

Purpose: Computes the factorial of an integer value. It works with any integer type.

Added in: Impala 2.3.0

Usage notes: You can use either the factorial() function or the ! operator. The factorial of 0 is 1. Likewise, the factorial() function returns 1 for any negative value. The maximum positive value for the input argument is 20; a value of 21 or greater overflows the range for a BIGINT and causes an error.

Return type: bigint

Added in: Impala 2.3.0

  1. select factorial(5);
  2. +--------------+
  3. | factorial(5) |
  4. +--------------+
  5. | 120 |
  6. +--------------+
  7. select 5!;
  8. +-----+
  9. | 5! |
  10. +-----+
  11. | 120 |
  12. +-----+
  13. select factorial(0);
  14. +--------------+
  15. | factorial(0) |
  16. +--------------+
  17. | 1 |
  18. +--------------+
  19. select factorial(-100);
  20. +-----------------+
  21. | factorial(-100) |
  22. +-----------------+
  23. | 1 |
  24. +-----------------+

floor(double a), floor(decimal(p,s) a), dfloor(double a), dfloor(decimal(p,s) a)

Purpose: Returns the largest integer that is less than or equal to the argument.

Return type: Same as the input type

fmod(double a, double b), fmod(float a, float b)

Purpose: Returns the modulus of a floating-point number. Equivalent to the % arithmetic operator.

Return type: float or double, depending on type of arguments

Added in: Impala 1.1.1

Usage notes:

Because this function operates on DOUBLE or FLOAT values, it is subject to potential rounding errors for values that cannot be represented precisely. Prefer to use whole numbers, or values that you know can be represented precisely by the DOUBLE or FLOAT types.

Examples:

The following examples show equivalent operations with the fmod() function and the % arithmetic operator, for values not subject to any rounding error.

  1. select fmod(10,3);
  2. +-------------+
  3. | fmod(10, 3) |
  4. +-------------+
  5. | 1 |
  6. +-------------+
  7. select fmod(5.5,2);
  8. +--------------+
  9. | fmod(5.5, 2) |
  10. +--------------+
  11. | 1.5 |
  12. +--------------+
  13. select 10 % 3;
  14. +--------+
  15. | 10 % 3 |
  16. +--------+
  17. | 1 |
  18. +--------+
  19. select 5.5 % 2;
  20. +---------+
  21. | 5.5 % 2 |
  22. +---------+
  23. | 1.5 |
  24. +---------+

The following examples show operations with the fmod() function for values that cannot be represented precisely by the DOUBLE or FLOAT types, and thus are subject to rounding error. fmod(9.9,3.0) returns a value slightly different than the expected 0.9 because of rounding. fmod(9.9,3.3) returns a value quite different from the expected value of 0 because of rounding error during intermediate calculations.

  1. select fmod(9.9,3.0);
  2. +--------------------+
  3. | fmod(9.9, 3.0) |
  4. +--------------------+
  5. | 0.8999996185302734 |
  6. +--------------------+
  7. select fmod(9.9,3.3);
  8. +-------------------+
  9. | fmod(9.9, 3.3) |
  10. +-------------------+
  11. | 3.299999713897705 |
  12. +-------------------+

fnv_hash(type v),

Purpose: Returns a consistent 64-bit value derived from the input argument, for convenience of implementing hashing logic in an application.

Return type: BIGINT

Usage notes:

You might use the return value in an application where you perform load balancing, bucketing, or some other technique to divide processing or storage.

Because the result can be any 64-bit value, to restrict the value to a particular range, you can use an expression that includes the ABS() function and the % (modulo) operator. For example, to produce a hash value in the range 0-9, you could use the expression ABS(FNV_HASH(x)) % 10.

This function implements the same algorithm that Impala uses internally for hashing, on systems where the CRC32 instructions are not available.

This function implements the Fowler–Noll–Vo hash function, in particular the FNV-1a variation. This is not a perfect hash function: some combinations of values could produce the same result value. It is not suitable for cryptographic use.

Similar input values of different types could produce different hash values, for example the same numeric value represented as SMALLINT or BIGINT, FLOAT or DOUBLE, or DECIMAL(5,2) or DECIMAL(20,5).

Examples:

  1. [localhost:21000] > create table h (x int, s string);
  2. [localhost:21000] > insert into h values (0, 'hello'), (1,'world'), (1234567890,'antidisestablishmentarianism');
  3. [localhost:21000] > select x, fnv_hash(x) from h;
  4. +------------+----------------------+
  5. | x | fnv_hash(x) |
  6. +------------+----------------------+
  7. | 0 | -2611523532599129963 |
  8. | 1 | 4307505193096137732 |
  9. | 1234567890 | 3614724209955230832 |
  10. +------------+----------------------+
  11. [localhost:21000] > select s, fnv_hash(s) from h;
  12. +------------------------------+---------------------+
  13. | s | fnv_hash(s) |
  14. +------------------------------+---------------------+
  15. | hello | 6414202926103426347 |
  16. | world | 6535280128821139475 |
  17. | antidisestablishmentarianism | -209330013948433970 |
  18. +------------------------------+---------------------+
  19. [localhost:21000] > select s, abs(fnv_hash(s)) % 10 from h;
  20. +------------------------------+-------------------------+
  21. | s | abs(fnv_hash(s)) % 10.0 |
  22. +------------------------------+-------------------------+
  23. | hello | 8 |
  24. | world | 6 |
  25. | antidisestablishmentarianism | 4 |
  26. +------------------------------+-------------------------+

For short argument values, the high-order bits of the result have relatively low entropy:

  1. [localhost:21000] > create table b (x boolean);
  2. [localhost:21000] > insert into b values (true), (true), (false), (false);
  3. [localhost:21000] > select x, fnv_hash(x) from b;
  4. +-------+---------------------+
  5. | x | fnv_hash(x) |
  6. +-------+---------------------+
  7. | true | 2062020650953872396 |
  8. | true | 2062020650953872396 |
  9. | false | 2062021750465500607 |
  10. | false | 2062021750465500607 |
  11. +-------+---------------------+

Added in: Impala 1.2.2

greatest(bigint a[, bigint b ...]), greatest(double a[, double b ...]), greatest(decimal(p,s) a[, decimal(p,s) b ...]), greatest(string a[, string b ...]), greatest(timestamp a[, timestamp b ...])

Purpose: Returns the largest value from a list of expressions.

Return type: same as the initial argument value, except that integer values are promoted to BIGINT and floating-point values are promoted to DOUBLE; use CAST() when inserting into a smaller numeric column

hex(bigint a), hex(string a)

Purpose: Returns the hexadecimal representation of an integer value, or of the characters in a string.

Return type: string

is_inf(double a),

Purpose: Tests whether a value is equal to the special value “inf”, signifying infinity.

Return type: boolean

Usage notes:

Infinity and NaN can be specified in text data files as inf and nan respectively, and Impala interprets them as these special values. They can also be produced by certain arithmetic expressions; for example, 1/0 returns Infinity and pow(-1, 0.5) returns NaN. Or you can cast the literal values, such as CAST('nan' AS DOUBLE) or CAST('inf' AS DOUBLE).

is_nan(double a),

Purpose: Tests whether a value is equal to the special value “NaN”, signifying “not a number”.

Return type: boolean

Usage notes:

Infinity and NaN can be specified in text data files as inf and nan respectively, and Impala interprets them as these special values. They can also be produced by certain arithmetic expressions; for example, 1/0 returns Infinity and pow(-1, 0.5) returns NaN. Or you can cast the literal values, such as CAST('nan' AS DOUBLE) or CAST('inf' AS DOUBLE).

least(bigint a[, bigint b ...]), least(double a[, double b ...]), least(decimal(p,s) a[, decimal(p,s) b ...]), least(string a[, string b ...]), least(timestamp a[, timestamp b ...])

Purpose: Returns the smallest value from a list of expressions.

Return type: same as the initial argument value, except that integer values are promoted to BIGINT and floating-point values are promoted to DOUBLE; use CAST() when inserting into a smaller numeric column

ln(double a), dlog1(double a)

Purpose: Returns the natural logarithm of the argument.

Return type: double

log(double base, double a)

Purpose: Returns the logarithm of the second argument to the specified base.

Return type: double

log10(double a), dlog10(double a)

Purpose: Returns the logarithm of the argument to the base 10.

Return type: double

log2(double a)

Purpose: Returns the logarithm of the argument to the base 2.

Return type: double

max_int(), max_tinyint(), max_smallint(), max_bigint()

Purpose: Returns the largest value of the associated integral type.

Return type: The same as the integral type being checked.

Usage notes: Use the corresponding min_ and max_ functions to check if all values in a column are within the allowed range, before copying data or altering column definitions. If not, switch to the next higher integral type or to a DECIMAL with sufficient precision.

min_int(), min_tinyint(), min_smallint(), min_bigint()

Purpose: Returns the smallest value of the associated integral type (a negative number).

Return type: The same as the integral type being checked.

Usage notes: Use the corresponding min_ and max_ functions to check if all values in a column are within the allowed range, before copying data or altering column definitions. If not, switch to the next higher integral type or to a DECIMAL with sufficient precision.

mod(numeric_type a, same_type b)

Purpose: Returns the modulus of a number. Equivalent to the % arithmetic operator. Works with any size integer type, any size floating-point type, and DECIMAL with any precision and scale.

Return type: Same as the input value

Added in: Impala 2.2.0

Usage notes:

Because this function works with DECIMAL values, prefer it over fmod() when working with fractional values. It is not subject to the rounding errors that make fmod() problematic with floating-point numbers. The % arithmetic operator now uses the mod() function in cases where its arguments can be interpreted as DECIMAL values, increasing the accuracy of that operator.

Examples:

The following examples show how the mod() function works for whole numbers and fractional values, and how the % operator works the same way. In the case of mod(9.9,3), the type conversion for the second argument results in the first argument being interpreted as DOUBLE, so to produce an accurate DECIMAL result requires casting the second argument or writing it as a DECIMAL literal, 3.0.

  1. select mod(10,3);
  2. +-------------+
  3. | fmod(10, 3) |
  4. +-------------+
  5. | 1 |
  6. +-------------+
  7. select mod(5.5,2);
  8. +--------------+
  9. | fmod(5.5, 2) |
  10. +--------------+
  11. | 1.5 |
  12. +--------------+
  13. select 10 % 3;
  14. +--------+
  15. | 10 % 3 |
  16. +--------+
  17. | 1 |
  18. +--------+
  19. select 5.5 % 2;
  20. +---------+
  21. | 5.5 % 2 |
  22. +---------+
  23. | 1.5 |
  24. +---------+
  25. select mod(9.9,3.3);
  26. +---------------+
  27. | mod(9.9, 3.3) |
  28. +---------------+
  29. | 0.0 |
  30. +---------------+
  31. select mod(9.9,3);
  32. +--------------------+
  33. | mod(9.9, 3) |
  34. +--------------------+
  35. | 0.8999996185302734 |
  36. +--------------------+
  37. select mod(9.9, cast(3 as decimal(2,1)));
  38. +-----------------------------------+
  39. | mod(9.9, cast(3 as decimal(2,1))) |
  40. +-----------------------------------+
  41. | 0.9 |
  42. +-----------------------------------+
  43. select mod(9.9,3.0);
  44. +---------------+
  45. | mod(9.9, 3.0) |
  46. +---------------+
  47. | 0.9 |
  48. +---------------+

murmur_hash(type v)

Purpose: Returns a consistent 64-bit value derived from the input argument, for convenience of implementing MurmurHash2 non-cryptographic hash function.

Return type: BIGINT

Usage notes:

You might use the return value in an application where you perform load balancing, bucketing, or some other technique to divide processing or storage. This function provides a good performance for all kinds of keys such as number, ascii string and UTF-8. It can be recommended as general-purpose hashing function.

Regarding comparison of murmur_hash with fnv_hash, murmur_hash is based on Murmur2 hash algorithm and fnv_hash function is based on FNV-1a hash algorithm. Murmur2 and FNV-1a can show very good randomness and performance compared with well known other hash algorithms, but Murmur2 slightly show better randomness and performance than FNV-1a. See [1][2][3] for details.

Similar input values of different types could produce different hash values, for example the same numeric value represented as SMALLINT or BIGINT, FLOAT or DOUBLE, or DECIMAL(5,2) or DECIMAL(20,5).

Examples:

  1. [localhost:21000] > create table h (x int, s string);
  2. [localhost:21000] > insert into h values (0, 'hello'), (1,'world'), (1234567890,'antidisestablishmentarianism');
  3. [localhost:21000] > select x, murmur_hash(x) from h;
  4. +------------+----------------------+
  5. | x | murmur_hash(x) |
  6. +------------+----------------------+
  7. | 0 | 6960269033020761575 |
  8. | 1 | -780611581681153783 |
  9. | 1234567890 | -5754914572385924334 |
  10. +------------+----------------------+
  11. [localhost:21000] > select s, murmur_hash(s) from h;
  12. +------------------------------+----------------------+
  13. | s | murmur_hash(s) |
  14. +------------------------------+----------------------+
  15. | hello | 2191231550387646743 |
  16. | world | 5568329560871645431 |
  17. | antidisestablishmentarianism | -2261804666958489663 |
  18. +------------------------------+----------------------+

For short argument values, the high-order bits of the result have relatively higher entropy than fnv_hash:

  1. [localhost:21000] > create table b (x boolean);
  2. [localhost:21000] > insert into b values (true), (true), (false), (false);
  3. [localhost:21000] > select x, murmur_hash(x) from b;
  4. +-------+----------------------+
  5. | x | murmur_hash(x) |
  6. +-------+---------------------++
  7. | true | -5720937396023583481 |
  8. | true | -5720937396023583481 |
  9. | false | 6351753276682545529 |
  10. | false | 6351753276682545529 |
  11. +-------+--------------------+-+

Added in: Impala 2.12.0

negative(numeric_type a)

Purpose: Returns the argument with the sign reversed; returns a positive value if the argument was already negative.

Return type: Same as the input value

Usage notes: Use -abs(a) instead if you need to ensure all return values are negative.

pi()

Purpose: Returns the constant pi.

Return type: double

pmod(bigint a, bigint b), pmod(double a, double b)

Purpose: Returns the positive modulus of a number. Primarily for HiveQL compatibility.

Return type: int or double, depending on type of arguments

Examples:

The following examples show how the fmod() function sometimes returns a negative value depending on the sign of its arguments, and the pmod() function returns the same value as fmod(), but sometimes with the sign flipped.

  1. select fmod(-5,2);
  2. +-------------+
  3. | fmod(-5, 2) |
  4. +-------------+
  5. | -1 |
  6. +-------------+
  7. select pmod(-5,2);
  8. +-------------+
  9. | pmod(-5, 2) |
  10. +-------------+
  11. | 1 |
  12. +-------------+
  13. select fmod(-5,-2);
  14. +--------------+
  15. | fmod(-5, -2) |
  16. +--------------+
  17. | -1 |
  18. +--------------+
  19. select pmod(-5,-2);
  20. +--------------+
  21. | pmod(-5, -2) |
  22. +--------------+
  23. | -1 |
  24. +--------------+
  25. select fmod(5,-2);
  26. +-------------+
  27. | fmod(5, -2) |
  28. +-------------+
  29. | 1 |
  30. +-------------+
  31. select pmod(5,-2);
  32. +-------------+
  33. | pmod(5, -2) |
  34. +-------------+
  35. | -1 |
  36. +-------------+

positive(numeric_type a)

Purpose: Returns the original argument unchanged (even if the argument is negative).

Return type: Same as the input value

Usage notes: Use abs() instead if you need to ensure all return values are positive.

pow(double a, double p), power(double a, double p), dpow(double a, double p), fpow(double a, double p)

Purpose: Returns the first argument raised to the power of the second argument.

Return type: double

precision(numeric_expression)

Purpose: Computes the precision (number of decimal digits) needed to represent the type of the argument expression as a DECIMAL value.

Usage notes:

Typically used in combination with the scale() function, to determine the appropriate DECIMAL(precision,scale) type to declare in a CREATE TABLE statement or CAST() function.

Return type: int

Examples:

The following examples demonstrate how to check the precision and scale of numeric literals or other numeric expressions. Impala represents numeric literals in the smallest appropriate type. 5 is a TINYINT value, which ranges from -128 to 127, therefore 3 decimal digits are needed to represent the entire range, and because it is an integer value there are no fractional digits. 1.333 is interpreted as a DECIMAL value, with 4 digits total and 3 digits after the decimal point.

  1. [localhost:21000] > select precision(5), scale(5);
  2. +--------------+----------+
  3. | precision(5) | scale(5) |
  4. +--------------+----------+
  5. | 3 | 0 |
  6. +--------------+----------+
  7. [localhost:21000] > select precision(1.333), scale(1.333);
  8. +------------------+--------------+
  9. | precision(1.333) | scale(1.333) |
  10. +------------------+--------------+
  11. | 4 | 3 |
  12. +------------------+--------------+
  13. [localhost:21000] > with t1 as
  14. ( select cast(12.34 as decimal(20,2)) x union select cast(1 as decimal(8,6)) x )
  15. select precision(x), scale(x) from t1 limit 1;
  16. +--------------+----------+
  17. | precision(x) | scale(x) |
  18. +--------------+----------+
  19. | 24 | 6 |
  20. +--------------+----------+

quotient(bigint numerator, bigint denominator), quotient(double numerator, double denominator)

Purpose: Returns the first argument divided by the second argument, discarding any fractional part. Avoids promoting integer arguments to DOUBLE as happens with the / SQL operator. Also includes an overload that accepts DOUBLE arguments, discards the fractional part of each argument value before dividing, and again returns BIGINT. With integer arguments, this function works the same as the DIV operator.

Return type: bigint

radians(double a)

Purpose: Converts argument value from degrees to radians.

Return type: double

rand(), rand(int seed), random(), random(int seed)

Purpose: Returns a random value between 0 and 1. After rand() is called with a seed argument, it produces a consistent random sequence based on the seed value.

Return type: double

Usage notes: Currently, the random sequence is reset after each query, and multiple calls to rand() within the same query return the same value each time. For different number sequences that are different for each query, pass a unique seed value to each call to rand(). For example, select rand(unix_timestamp()) from ...

Examples:

The following examples show how rand() can produce sequences of varying predictability, so that you can reproduce query results involving random values or generate unique sequences of random values for each query. When rand() is called with no argument, it generates the same sequence of values each time, regardless of the ordering of the result set. When rand() is called with a constant integer, it generates a different sequence of values, but still always the same sequence for the same seed value. If you pass in a seed value that changes, such as the return value of the expression unix_timestamp(now()), each query will use a different sequence of random values, potentially more useful in probability calculations although more difficult to reproduce at a later time. Therefore, the final two examples with an unpredictable seed value also include the seed in the result set, to make it possible to reproduce the same random sequence later.

  1. select x, rand() from three_rows;
  2. +---+-----------------------+
  3. | x | rand() |
  4. +---+-----------------------+
  5. | 1 | 0.0004714746030380365 |
  6. | 2 | 0.5895895192351144 |
  7. | 3 | 0.4431900859080209 |
  8. +---+-----------------------+
  9. select x, rand() from three_rows order by x desc;
  10. +---+-----------------------+
  11. | x | rand() |
  12. +---+-----------------------+
  13. | 3 | 0.0004714746030380365 |
  14. | 2 | 0.5895895192351144 |
  15. | 1 | 0.4431900859080209 |
  16. +---+-----------------------+
  17. select x, rand(1234) from three_rows order by x;
  18. +---+----------------------+
  19. | x | rand(1234) |
  20. +---+----------------------+
  21. | 1 | 0.7377511392057646 |
  22. | 2 | 0.009428468537250751 |
  23. | 3 | 0.208117277924026 |
  24. +---+----------------------+
  25. select x, rand(1234) from three_rows order by x desc;
  26. +---+----------------------+
  27. | x | rand(1234) |
  28. +---+----------------------+
  29. | 3 | 0.7377511392057646 |
  30. | 2 | 0.009428468537250751 |
  31. | 1 | 0.208117277924026 |
  32. +---+----------------------+
  33. select x, unix_timestamp(now()), rand(unix_timestamp(now()))
  34. from three_rows order by x;
  35. +---+-----------------------+-----------------------------+
  36. | x | unix_timestamp(now()) | rand(unix_timestamp(now())) |
  37. +---+-----------------------+-----------------------------+
  38. | 1 | 1440777752 | 0.002051228658320023 |
  39. | 2 | 1440777752 | 0.5098743483004506 |
  40. | 3 | 1440777752 | 0.9517714925817081 |
  41. +---+-----------------------+-----------------------------+
  42. select x, unix_timestamp(now()), rand(unix_timestamp(now()))
  43. from three_rows order by x desc;
  44. +---+-----------------------+-----------------------------+
  45. | x | unix_timestamp(now()) | rand(unix_timestamp(now())) |
  46. +---+-----------------------+-----------------------------+
  47. | 3 | 1440777761 | 0.9985985015512437 |
  48. | 2 | 1440777761 | 0.3251255333074953 |
  49. | 1 | 1440777761 | 0.02422675025846192 |
  50. +---+-----------------------+-----------------------------+

round(double a), round(double a, int d), round(decimal a, int_type d), dround(double a), dround(double a, int d), dround(decimal(p,s) a, int_type d)

Purpose: Rounds a floating-point value. By default (with a single argument), rounds to the nearest integer. Values ending in .5 are rounded up for positive numbers, down for negative numbers (that is, away from zero). The optional second argument specifies how many digits to leave after the decimal point; values greater than zero produce a floating-point return value rounded to the requested number of digits to the right of the decimal point.

Return type: Same as the input type

scale(numeric_expression)

Purpose: Computes the scale (number of decimal digits to the right of the decimal point) needed to represent the type of the argument expression as a DECIMAL value.

Usage notes:

Typically used in combination with the precision() function, to determine the appropriate DECIMAL(precision,scale) type to declare in a CREATE TABLE statement or CAST() function.

Return type: int

Examples:

The following examples demonstrate how to check the precision and scale of numeric literals or other numeric expressions. Impala represents numeric literals in the smallest appropriate type. 5 is a TINYINT value, which ranges from -128 to 127, therefore 3 decimal digits are needed to represent the entire range, and because it is an integer value there are no fractional digits. 1.333 is interpreted as a DECIMAL value, with 4 digits total and 3 digits after the decimal point.

  1. [localhost:21000] > select precision(5), scale(5);
  2. +--------------+----------+
  3. | precision(5) | scale(5) |
  4. +--------------+----------+
  5. | 3 | 0 |
  6. +--------------+----------+
  7. [localhost:21000] > select precision(1.333), scale(1.333);
  8. +------------------+--------------+
  9. | precision(1.333) | scale(1.333) |
  10. +------------------+--------------+
  11. | 4 | 3 |
  12. +------------------+--------------+
  13. [localhost:21000] > with t1 as
  14. ( select cast(12.34 as decimal(20,2)) x union select cast(1 as decimal(8,6)) x )
  15. select precision(x), scale(x) from t1 limit 1;
  16. +--------------+----------+
  17. | precision(x) | scale(x) |
  18. +--------------+----------+
  19. | 24 | 6 |
  20. +--------------+----------+

sign(double a)

Purpose: Returns -1, 0, or 1 to indicate the signedness of the argument value.

Return type: int

sin(double a)

Purpose: Returns the sine of the argument.

Return type: double

sinh(double a)

Purpose: Returns the hyperbolic sine of the argument.

Return type: double

sqrt(double a), dsqrt(double a)

Purpose: Returns the square root of the argument.

Return type: double

tan(double a)

Purpose: Returns the tangent of the argument.

Return type: double

tanh(double a)

Purpose: Returns the hyperbolic tangent of the argument.

Return type: double

truncate(double_or_decimal a[, digits_to_leave]), dtrunc(double_or_decimal a[, digits_to_leave]), trunc(double_or_decimal a[, digits_to_leave])

Purpose: Removes some or all fractional digits from a numeric value.

Arguments: With a single floating-point argument, removes all fractional digits, leaving an integer value. The optional second argument specifies the number of fractional digits to include in the return value, and only applies when the argument type is DECIMAL. A second argument of 0 truncates to a whole integer value. A second argument of negative N sets N digits to 0 on the left side of the decimal

Scale argument: The scale argument applies only when truncating DECIMAL values. It is an integer specifying how many significant digits to leave to the right of the decimal point. A scale argument of 0 truncates to a whole integer value. A scale argument of negative N sets N digits to 0 on the left side of the decimal point.

truncate(), dtrunc(), and trunc() are aliases for the same function.

Return type: Same as the input type

Added in: The trunc() alias was added in Impala 2.10.

Usage notes:

You can also pass a DOUBLE argument, or DECIMAL argument with optional scale, to the dtrunc() or truncate functions. Using the trunc() function for numeric values is common with other industry-standard database systems, so you might find such trunc() calls in code that you are porting to Impala.

The trunc() function also has a signature that applies to TIMESTAMP values. See Impala Date and Time Functions for details.

Examples:

The following examples demonstrate the truncate() and dtrunc() signatures for this function:

  1. select truncate(3.45);
  2. +----------------+
  3. | truncate(3.45) |
  4. +----------------+
  5. | 3 |
  6. +----------------+
  7. select truncate(-3.45);
  8. +-----------------+
  9. | truncate(-3.45) |
  10. +-----------------+
  11. | -3 |
  12. +-----------------+
  13. select truncate(3.456,1);
  14. +--------------------+
  15. | truncate(3.456, 1) |
  16. +--------------------+
  17. | 3.4 |
  18. +--------------------+
  19. select dtrunc(3.456,1);
  20. +------------------+
  21. | dtrunc(3.456, 1) |
  22. +------------------+
  23. | 3.4 |
  24. +------------------+
  25. select truncate(3.456,2);
  26. +--------------------+
  27. | truncate(3.456, 2) |
  28. +--------------------+
  29. | 3.45 |
  30. +--------------------+
  31. select truncate(3.456,7);
  32. +--------------------+
  33. | truncate(3.456, 7) |
  34. +--------------------+
  35. | 3.4560000 |
  36. +--------------------+

The following examples demonstrate using trunc() with DECIMAL or DOUBLE values, and with an optional scale argument for DECIMAL values. (The behavior is the same for the truncate() and dtrunc() aliases also.)

  1. create table t1 (d decimal(20,7));
  2. -- By default, no digits to the right of the decimal point.
  3. insert into t1 values (1.1), (2.22), (3.333), (4.4444), (5.55555);
  4. select trunc(d) from t1 order by d;
  5. +----------+
  6. | trunc(d) |
  7. +----------+
  8. | 1 |
  9. | 2 |
  10. | 3 |
  11. | 4 |
  12. | 5 |
  13. +----------+
  14. -- 1 digit to the right of the decimal point.
  15. select trunc(d,1) from t1 order by d;
  16. +-------------+
  17. | trunc(d, 1) |
  18. +-------------+
  19. | 1.1 |
  20. | 2.2 |
  21. | 3.3 |
  22. | 4.4 |
  23. | 5.5 |
  24. +-------------+
  25. -- 2 digits to the right of the decimal point,
  26. -- including trailing zeroes if needed.
  27. select trunc(d,2) from t1 order by d;
  28. +-------------+
  29. | trunc(d, 2) |
  30. +-------------+
  31. | 1.10 |
  32. | 2.22 |
  33. | 3.33 |
  34. | 4.44 |
  35. | 5.55 |
  36. +-------------+
  37. insert into t1 values (9999.9999), (8888.8888);
  38. -- Negative scale truncates digits to the left
  39. -- of the decimal point.
  40. select trunc(d,-2) from t1 where d > 100 order by d;
  41. +--------------+
  42. | trunc(d, -2) |
  43. +--------------+
  44. | 8800 |
  45. | 9900 |
  46. +--------------+
  47. -- The scale of the result is adjusted to match the
  48. -- scale argument.
  49. select trunc(d,2),
  50. precision(trunc(d,2)) as p,
  51. scale(trunc(d,2)) as s
  52. from t1 order by d;
  53. +-------------+----+---+
  54. | trunc(d, 2) | p | s |
  55. +-------------+----+---+
  56. | 1.10 | 15 | 2 |
  57. | 2.22 | 15 | 2 |
  58. | 3.33 | 15 | 2 |
  59. | 4.44 | 15 | 2 |
  60. | 5.55 | 15 | 2 |
  61. | 8888.88 | 15 | 2 |
  62. | 9999.99 | 15 | 2 |
  63. +-------------+----+---+
  1. create table dbl (d double);
  2. insert into dbl values
  3. (1.1), (2.22), (3.333), (4.4444), (5.55555),
  4. (8888.8888), (9999.9999);
  5. -- With double values, there is no optional scale argument.
  6. select trunc(d) from dbl order by d;
  7. +----------+
  8. | trunc(d) |
  9. +----------+
  10. | 1 |
  11. | 2 |
  12. | 3 |
  13. | 4 |
  14. | 5 |
  15. | 8888 |
  16. | 9999 |
  17. +----------+

unhex(string a)

Purpose: Returns a string of characters with ASCII values corresponding to pairs of hexadecimal digits in the argument.

Return type: string

Parent topic: Impala Built-In Functions