Scalar functions operate on one or more values and then return a single value. They can be used wherever a value expression is valid.

Scalar functions are divided into serval kinds.

Trigonometric Functions

All trigonometric functions perform a calculation, operate on a single numeric value and then return a single numeric value.

For values outside the domain, NaN is returned.

  1. SIN(num_expr)
  2. SINH(num_expr)
  3. ASIN(num_expr)
  4. ASINH(num_expr)
  5. COS(num_expr)
  6. COSH(num_expr)
  7. ACOS(num_expr)
  8. ACOSH(num_expr)
  9. TAN(num_expr)
  10. TANH(num_expr)
  11. ATAN(num_expr)
  12. ATANH(num_expr)

Arithmetic Functions

The following functions perform a calculation, operate on a single numeric value and then return a single numeric value.

  1. ABS(num_expr)

Absolute value.

  1. CEIL(num_expr)

The function application CEIL(n) returns the least integer not less than n.

  1. FLOOR(num_expr)

The function application FLOOR(n) returns the greatest integer not greater than n.

  1. ROUND(num_expr)

The function application ROUND(n) returns the nearest integer to n the even integer if n is equidistant between two integers.

  1. SQRT(num_expr)

The square root of a numeric value.

  1. LOG(num_expr)
  2. LOG2(num_expr)
  3. LOG10(num_expr)
  4. EXP(num_expr)
  1. SIGN(num_expr)

The function application SIGN(n) returns the sign of a numeric value as an Integer.

  • returns -1 if n is negative
  • returns 0 if n is exact zero
  • returns 1 if n is positive
  • returns null if n is exact null

Predicate Functions

Function applications of the form IS_A(x) where A is the name of a type returns TRUE if the argument x is of type A, otherwise FALSE.

  1. IS_INT(val_expr)
  2. IS_FLOAT(val_expr)
  3. IS_NUM(val_expr)
  4. IS_BOOL(val_expr)
  5. IS_STR(val_expr)
  6. IS_MAP(val_expr)
  7. IS_ARRAY(val_expr)
  8. IS_DATE(val_expr)
  9. IS_TIME(val_expr)

String Functions

  1. TO_STR(val_expr)

Convert a value expression to a readable string.

  1. TO_LOWER(str)

Convert a string to lower case, using simple case conversion.

  1. TO_UPPER(str)

Convert a string to upper case, using simple case conversion.

  1. TRIM(str)

Remove leading and trailing white space from a string.

  1. LEFT_TRIM(str)

Remove leading white space from a string.

  1. RIGHT_TRIM(str)

Remove trailing white space from a string.

  1. REVERSE(str)

Reverse the characters of a string.

  1. STRLEN(str)

Returns the number of characters in a string.

  1. TAKE(num_expr, str)

The function application TAKE(n, s) returns the prefix of the string of length n.

  1. TAKEEND(num_expr, str)

The function application TAKEEND(n, s) returns the suffix remaining after taking n characters from the end of the string.

  1. DROP(num_expr, str)

The function application DROP(n, s) returns the suffix of the string after the first n characters, or the empty string if n is greater than the length of the string.

  1. DROPEND(num_expr, str)

The function application DROPEND(n, s) returns the prefix remaining after dropping n characters from the end of the string.

Null Functions

  1. IFNULL(val_expr, val_expr)

The function application IFNULL(x, y) returns y if x is NULL, otherwise x.

When the argument type is a complex type, for example, ARRAY or MAP, the contents of the complex type are not inspected.

  1. NULLIF(val_expr, val_expr)

The function application NULLIF(x, y) returns NULL if x is equal to y, otherwise x.

When the argument type is a complex type, for example, ARRAY or MAP, the contents of the complex type are not inspected.

Time and Date Functions

Time Format

Formats are analogous to strftimeScalar Functions - 图1 (opens new window).

Format NameRaw Format String
simpleDateFormat“%Y-%m-%d %H:%M:%S”
iso8061DateFormat“%Y-%m-%dT%H:%M:%S%z”
webDateFormat“%a, %d %b %Y %H:%M:%S GMT”
mailDateFormat“%a, %d %b %Y %H:%M:%S %z”
  1. DATETOSTRING(val_expr, str)

Formatting seconds since 1970-01-01 00:00:00 UTC to string in GMT with the second string argument as the given format name.

  1. STRINGTODATE(str, str)

Formatting string to seconds since 1970-01-01 00:00:00 UTC in GMT with the second string argument as the given format name.

Array Functions

  1. ARRAY_CONTAINS(arr_expr, val_expr)

Given an array, checks if the search value is contained in the array (of the same type).

  1. ARRAY_DISTINCT(arr_expr)

Returns an array of all the distinct values, including NULL if present, from the input array. The output array elements are in order of their first occurrence in the input.

Returns NULL if the argument is NULL.

  1. ARRAY_EXCEPT(arr_expr, arr_expr)

Returns an array of all the distinct elements from an array, except for those also present in a second array. The order of entries in the first array is preserved but duplicates are removed.

Returns NULL if either input is NULL.

  1. ARRAY_INTERSECT(arr_expr, arr_expr)

Returns an array of all the distinct elements from the intersection of both input arrays. If the first list contains duplicates, so will the result. If the element is found in both the first and the second list, the element from the first list will be used.

Returns NULL if either input is NULL.

  1. ARRAY_UNION(arr_expr, arr_expr)

Returns the array union of the two arrays. Duplicates, and elements of the first list, are removed from the second list, but if the first list contains duplicates, so will the result.

Returns NULL if either input is NULL.

  1. ARRAY_JOIN(arr_expr)
  2. ARRAY_JOIN(arr_expr, str)

Creates a flat string representation of all the primitive elements contained in the given array. The elements in the resulting string are separated by the chosen delimiter, which is an optional parameter that falls back to a comma ,.

  1. ARRAY_LENGTH(arr_expr)

Returns the length of a finite list.

Returns NULL if the argument is NULL.

  1. ARRAY_MAX(arr_expr)

Returns the maximum value from within a given array of elements.

Returns NULL if the argument is NULL.

  1. ARRAY_MIN(arr_expr)

Returns the minimum value from within a given array of elements.

Returns NULL if the argument is NULL.

  1. ARRAY_REMOVE(arr_expr, val_expr)

Removes all elements from the input array equal to the second argument.

Returns NULL if the first argument is NULL.

  1. ARRAY_SORT(arr_expr)

Sort an array. Elements are arranged from lowest to highest, keeping duplicates in the order they appeared in the input.

Returns NULL if the first argument is NULL.