Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Return Type

Name(Signature)

Description

OSS

string

from_unixtime(bigint unixtime[,string pattern])

Converts a number of seconds since epoch (1970-01-01 00:00:00 UTC) to a string representing the timestamp of that moment in the current time zone(using config "hive.local.time.zone") using the specified pattern. If the pattern is missing the default is used ('uuuu-MM-dd HH:mm:ss' or yyyy-MM-dd HH:mm:ss'). Example: from_unixtime(0)=1970-01-01 00:00:00 (hive.local.time.zone=Etc/GMT)

Status
colourGreen
titleNew

As of Hive 4.0.0 the "hive.datetime.formatter" property can be used to control the underlying formatter implementation and as a consequence the accepted patterns and their behavior.  Earlier versions used https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html as the underlying formatter.

bigint

unix_timestamp()

Gets the current Unix timestamp in seconds. This function is not deterministic and its value is not fixed for the scope of a query execution, therefore prevents proper optimization of queries - this has been deprecated since 2.0 in favour of CURRENT_TIMESTAMP constant.



bigint

unix_timestamp(string date)

Converts a DateTimestring to unix time (seconds since epoch) using the default pattern(s). The default accepted patterns depend on the underlying formatter implementation. The DateTime string does not contain a timezone so the conversion uses the local time zone as specified by "hive.local.time.zone" property. Returns null when the conversion fails. Example: unix_timestamp('2009-03-20 11:30:01') = 1237573801

Status
colourGreen
titleNew

As of Hive 4.0.0 the "hive.datetime.formatter" property can be used to control the underlying formatter implementation and as a consequence the accepted patterns and their behavior. Earlier versions used https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html as the underlying formatter.

bigint

unix_timestamp(string date,string pattern)

Converts a date-time string to unix time (seconds since epoch) using the specified pattern. The accepted patterns and their behavior depend on the underlying formatter implementation. Returns null when the conversion fails. Example: unix_timestamp('2009-03-20', 'uuuu-MM-dd') = 1237532400

Status
colourGreen
titleNew

As of Hive 4.0.0 the "hive.datetime.formatter" property can be used to control the underlying formatter implementation, and as a consequence the accepted patterns and their behavior.  Earlier versions used https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html as the underlying formatter.

date

to_date(string timestamp)

Returns the date part of a timestamp date object. Example: to_date("1970-01-01 00:00:00") 

GenericUDFDate

int

year(string date)

Returns the year part of a date or a timestamp string: year("1970-01-01 00:00:00") = 1970, year("1970-01-01") = 1970.

UDFYear

int

quarter(date/timestamp/string)
Returns the quarter of the year for a date, timestamp, or string in the range 1 to 4. Example: quarter('2015-04-08') = 2.GenericUDFQuarter

int

month(string date)

Returns the month part of a date or a timestamp string. Example: month("1970-11-01 00:00:00") = 11, month("1970-11-01") = 11.

UDFMonth

int

day(string date) dayofmonth(date)

Returns the day part of a date or a timestamp string. Example: day("1970-11-01 00:00:00") = 1, day("1970-11-01") = 1.

UDFDayOfMonth

int

hour(string date)

Returns the hour of the timestamp: Example: hour('2009-07-30 12:58:59') = 12, hour('12:58:59') = 12.

UDFHour

int

minute(string date)

Returns the minute of the timestamp.

UDFMinute

int

second(string date)

Returns the second of the timestamp.

UDFSecond

int

weekofyear(string date)

Returns the week number of a timestamp string. Example: weekofyear("1970-11-01 00:00:00") = 44 or weekofyear("1970-11-01") = 44.

UDFWeekOfYear

int
extract(field FROM source)

Retrieve fields such as days or hours from the source. The source must be a date, timestamp, interval, or string that can be converted into either a date or timestamp. Supported fields include day, dayofweek, hour, minute, month, quarter, second, week and year.

Examples:

  1. select extract(month from "2016-10-20") results in 10.

  2. select extract(hour from "2016-10-20 05:06:07") results in 5.

  3. select extract(dayofweek from "2016-10-20 05:06:07") results in 5.

  4. select extract(month from interval '1-3' year to month) results in 3.

  5. select extract(minute from interval '3 12:20:30' day to second) results in 20.


int

datediff(string enddate,string startdate)

Returns the number of days from startdate to end date. Example: datediff('2009-03-01', '2009-02-27') = 2.

GenericUDFDateDiff

date

date_add(date/timestamp/string startdate, tinyint/smallint/int days)

Adds a number of days to startdate. Example: date_add('2008-12-31', 1) = '2009-01-01'.

GenericUDFDateAdd

date

date_sub(date/timestamp/string startdate, tinyint/smallint/int days)

Subtracts a number of days to startdate: date_sub('2008-12-31', 1) = '2008-12-30'.

GenericUDFDateSub

timestamp

from_utc_timestamp({any primitive type} ts,string timezone)

Converts a timestamp* in UTC to a given timezone.

* timestamp is a primitive type, including timestamp/date, tinyint/smallint/int/bigint, float/double and decimal.

Fractional values are considered as seconds.integer values are considered as milliseconds. For example, from_utc_timestamp(2592000.0,'PST'), from_utc_timestamp(2592000000,'PST') and from_utc_timestamp(timestamp '1970-01-30 16:00:00','PST') all return the timestamp 1970-01-30 08:00:00.

GenericUDFFromUtcTimestamp

timestamp

to_utc_timestamp({any primitive type} ts,string timezone)

Converts a timestamp* in a given timezone to UTC.

* timestamp is a primitive type, including timestamp/date, tinyint/smallint/int/bigint, float/double and decimal.

Fractional values are considered as seconds.integer values are considered as milliseconds. For example, to_utc_timestamp(2592000.0,'PST'), to_utc_timestamp(2592000000,'PST') and to_utc_timestamp(timestamp '1970-01-30 16:00:00','PST') all return the timestamp 1970-01-31 00:00:00.

GenericUDFToUtcTimestamp

date
current_date

Returns the current date at the start of query evaluation. All calls of current_date within the same query return the same value.

GenericUDFCurrentDate

timestamp
current_timestamp

Returns the current timestamp at the start of query evaluation. All calls of current_timestamp within the same query return the same value.

GenericUDFCurrentTimestamp

string
add_months(string start_date,int num_months, output_date_format)

Returns the date that is num_months after start_date. start_date is a string, date or timestamp. num_months is an integer. If start_date is the last day of the month or if the resulting month has fewer days than the day component of start_date, then the result is the last day of the resulting month. Otherwise, the result has the same day component as start_date. The default output format is 'yyyy-MM-dd'.

Status
colourGreen
titleNew

Before Hive 4.0.0, the time part of the date is ignored. As of Hive 4.0.0, add_months supports an optional argument output_date_format, which accepts a string that represents a valid date format for the output. This allows to retain the time format in the output.

For example :

add_months('2009-08-31', 1) returns '2009-09-30'.
add_months('2017-12-31 14:15:16', 2, 'YYYY-MM-dd HH:mm:ss') returns '2018-02-28 14:15:16'.

string
last_day(string date)
Returns the last day of the month to which the date belongs. date is a string in the format 'yyyy-MM-dd HH:mm:ss' or 'yyyy-MM-dd'. The time part of the date is ignored!GenericUDFLastDay
string
next_day(string start_date,string day_of_week)
Returns the first date which is later than start_date and named as day_of_week.  start_date  is a string/date/timestamp. day_of_week is 2 letters, 3 letters or full name of the day of the week (e.g. Mo, tue, FRIDAY). The time part of start_date is ignored. Example: next_day('2015-01-14', 'TU') = 2015-01-20.GenericUDFNextDay
string
trunc(string date,string format)
Returns date truncated to the unit specified by the format. Supported formats: MONTH/MON/MM, YEAR/YYYY/YY. Example: trunc('2015-03-17', 'MM') = 2015-03-01.GenericUDFTrunc
double
months_between(date1, date2)
Returns the number of months between dates date1 and date2. If date1 is later than date2, then the result is positive. If date1 is earlier than date , then the result is negative. If date1 and date2 are either the same days of the month or both last days of months, then the result is always aninteger. Otherwise, the UDF calculates the fractional portion of the result based on a 31-day month and considers the difference in time components date1 and date2. date1 and date2 type can be date, timestamp or string in the format 'yyyy-MM-dd' or 'yyyy-MM-dd HH:mm:ss'. The result is rounded to 8 decimal places. Example: months_between('1997-02-28 10:30:00', '1996-10-30') = 3.94959677GenericUDFMonthsBetween
string
date_format(date/timestamp/string ts,string pattern)

Converts a date/timestamp/string to a value of string using the specified pattern. The accepted patterns and their behavior depend on the underlying formatter implementation. The pattern argument should be constant. Example: date_format('2015-04-08', 'y') = '2015'.

date_format can be used to implement other UDFs, e.g.:

  • dayname(date) is date_format(date, 'EEEE')
  • dayofyear(date) is date_format(date, 'D')

Status
colourGreen
titleNew

As of Hive 4.0.0 the "hive.datetime.formatter" property can be used to control the underlying formatter implementation and as a consequence the accepted patterns and their behavior.  Earlier versions used https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html as the underlying formatter.

...

Return Type

Name (Signature)

Description

OSS

double

round(double a)

Returns the rounded bigint value of a.

GenericUDFRound

double

round(double a,int d)

Returns a rounded to d decimal places.

double
bround(double a)
Returns the rounded bigint value of a using HALF_EVEN rounding mode. Also known as Gaussian rounding or bankers' rounding. Example: bround(2.5) = 2, bround(3.5) = 4.GenericUDFBRound
double
bround(double a,int d)
Returns a rounded to d decimal places using HALF_EVEN rounding mode.  Example: bround(8.25, 1) = 8.2, bround(8.35, 1) = 8.4.

bigint

floor(double a)

Returns the maximum bigint value that is equal to or less than a.

GenericUDFFloor

bigint

ceil(double a), ceiling(double a)

Returns the minimum bigint value that is equal to or greater than a.

GenericUDFCeil

double

rand(), rand(INT seed)

Returns a random number (that changes from row to row) that is distributed uniformly from 0 to 1. Specifying the seed will make sure the generated random number sequence is deterministic.

UDFRand

double

exp(double a), exp(decimal a)

Returns ea where e is the base of the natural logarithm.

UDFExp

double

ln(double a), ln(decimal a)

Returns the natural logarithm of the argument a.

UDFLn

double

log10(double a), log10(decimal a)

Returns the base-10 logarithm of the argument a.

UDFLog10

double

log2(double a), log2(decimal a)

Returns the base-2 logarithm of the argument a

UDFLog2

double

log(double base, double a)
log(decimal base, decimal a)

Returns the base-base logarithm of the argument a.

UDFLog

double

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

Returns ap.

GenericUDFPower

double

sqrt(double a), sqrt(decimal a)

Returns the square root of a.

UDFSqrt

string

bin(bigint a)

Returns the number in binary format.

UDFBin

string

hex(bigint a) hex(string a) hex(binary a)

If the argument is an int or binary, hex returns the number as a string in hexadecimal format. Otherwise, if the number is a string, it converts each character into its hexadecimal representation and returns the resulting string. 

UDFHex

binary

unhex(STRING a)

Inverse of hex.interprets each pair of characters as a hexadecimal number and converts to the byte representation of the number.

UDFUnhex

string

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

Converts a number from a given base to another.

UDFConv

double

abs(double a)

Returns the absolute value.

GenericUDFAbs

int or double

pmod(INT a,int b), pmod(double a, double b)

Returns the positive value of a mod b.

GenericUDFOPMod

double

sin(double a), sin(decimal a)

Returns the sine of a (a is in radians).

UDFSin

double

asin(double a), asin(decimal a)

Returns the arc sin of a if -1<=a<=1 or NULL otherwise

UDFAsin

double

cos(double a), cos(decimal a)

Returns the cosine of a (a is in radians)

UDFCos

double

acos(double a), acos(decimal a)

Returns the arccosine of a if -1<=a<=1 or NULL otherwise.

UDFAcos

double

tan(double a), tan(decimal a)

Returns the tangent of a (a is in radians).

UDFTan

double

atan(double a), atan(decimal a)

Returns the arctangent of a.

UDFAtan

double

degrees(double a), degrees(decimal a)

Converts value of a from radians to degrees

UDFDegrees

double

radians(double a), radians(double a)

Converts value of a from degrees to radians

UDFRadians

int or double

positive(INT a), positive(double a)

Returns a.

GenericUDFOPPositive

int or double

negative(INT a), negative(double a)

Returns -a.

GenericUDFOPNegative

double or int

sign(double a), sign(decimal a)

Returns the sign of a as '1.0' (if a is positive) or '-1.0' (if a is negative), '0.0' otherwise. The decimal version returns int instead of double. 

UDFSign

double

e()

Returns the value of e.

UDFE

double

pi()

Returns the value of pi.

UDFPI

bigint
factorial(INT a)
Returns the factorial of a Valid a is [0..20].GenericUDFFactorial
double
cbrt(double a)
Returns the cube root of a double value.GenericUDFCbrt

int

bigint

shiftleft(TINYINT|SMALLINT|INT a,int b)
shiftleft(bigint a,int b)

Bitwise left shift. Shifts a b positions to the left.

Returns int for tinyint, smallint andint a. Returns bigint for bigint a.

UDFOPBitShiftLeft

int

bigint

shiftright(TINYINT|SMALLINT|INT a,int b)
shiftright(bigint a,int b)

Bitwise right shift. Shifts a b positions to the right.

Returns int for tinyint, smallint andint a. Returns bigint for bigint a.

UDFOPBitShiftRight

int

bigint

shiftrightunsigned(TINYINT|SMALLINT|INT a,int b),
shiftrightunsigned(bigint a,int b)

Bitwise unsigned right shift. Shifts a b positions to the right.

Returns int for tinyint, smallint andint a. Returns bigint for bigint a.

UDFOPBitShiftRightUnsigned

T
greatest(T v1, T v2, ...)
Returns the greatest value of the list of values. Fixed to return NULL when one or more arguments are NULL, and strict type restriction relaxed, consistent with ">" operator.GenericUDFGreatest
T
least(T v1, T v2, ...)
Returns the least value of the list of values. Fixed to return NULL when one or more arguments are NULL, and strict type restriction relaxed, consistent with "<" operator.GenericUDFLeast
int
width_bucket(NUMERIC expr, NUMERIC min_value, NUMERIC max_value,int num_buckets)

Returns an integer between 0 and num_buckets+1 by mapping expr into the ith equally sized bucket. Buckets are made by dividing [min_value, max_value]into equally sized regions. If expr < min_value, return 1, if expr > max_value return num_buckets+1. See https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions214.htm

GenericUDFWidthBucket

...

Return Type

Name(Signature)

Description

int

size(Map<K.V>)

Returns the number of elements in the map type.

int

size(Array<T>)

Returns the number of elements in the array type.

array<K>

map_keys(Map<K.V>)

Returns an unordered array containing the keys of the input map.

array<V>

map_values(Map<K.V>)

Returns an unordered array containing the values of the input map.

boolean

array_contains(Array<T>, value)

Returns TRUE if the array contains the provided parameter value.

array<t>

sort_array(Array<T>)

Sorts the input array in ascending order according to the natural ordering of the array elements and returns it.

...

Return Type

Name(Signature)

Description

OSS

binary

binary(string|binary)

Casts the parameter into a binary.

GenericUDFBaseBinary

Expected "=" to follow "type"

cast(expr as <type>)

Converts the results of the expression expr to <type>. For example, cast('1' as bigint) will convert the string '1' to its integral representation. A null is returned if the conversion does not succeed. If cast(expr as boolean) Hive returns true for a non-empty string.


...

Return Type

Name(Signature)

Description

T

if(boolean testCondition, T valueTrue, T valueFalseOrNull)

Returns valueTrue when testCondition is true, returns valueFalseOrNull otherwise.

boolean
isnull( a )
Returns true if a is NULL and false otherwise.
boolean
isnotnull ( a )
Returns true if a is not NULL and false otherwise.
T
nvl(T value, T default_value)
Returns default value if value is null else returns value.

T

COALESCE(T v1, T v2, ...)

Returns the first v that is not NULL, or NULL if all v's are NULL.

T

CASE a WHEN b THEN c [WHEN d THEN e]* [ELSE f] END

When a = b, returns c; when a = d, returns e; else returns f.

T

CASE WHEN a THEN b [WHEN c THEN d]* [ELSE e] END

When a = true, returns b; when c = true, returns d; else returns e.

T
nullif( a, b )

Returns NULL if a=b; otherwise returns a.

Tip
titleShorthand for

CASE WHEN a = b then NULL else a


void
assert_true(boolean condition)
Throw an exception if 'condition' is not true, otherwise, return null. For example, select assert_true (2<1).

...

Return Type

Name(Signature)

Description

OSS

string

mask(string str[,string upper[,string lower[,string number]]])

Returns a masked version of str. By default, upper case letters are converted to "X", lower case letters are converted to "x" and numbers are converted to "n". Example: mask("abcd-EFGH-8765-4321") results in xxxx-XXXX-nnnn-nnnn. You can override the characters used in the mask by supplying additional arguments: the second argument controls the mask character for upper case letters, the third argument for lower case letters, and the fourth argument for numbers. For example, mask("abcd-EFGH-8765-4321", "U", "l", "#") results in llll-UUUU-####-####.

GenericUDFMask

string
mask_first_n(string str[,int n])
Returns a masked version of str with the first n values masked. Upper case letters are converted to "X", lower case letters are converted to "x" and numbers are converted to "n". For example, mask_first_n("1234-5678-8765-4321", 4) results in nnnn-5678-8765-4321.GenericUDFMaskFirstN
string
mask_last_n(string str[,int n])
Returns a masked version of str with the last n values masked. Upper case letters are converted to "X", lower case letters are converted to "x" and numbers are converted to "n". For example, mask_last_n("1234-5678-8765-4321", 4) results in 1234-5678-8765-nnnn.GenericUDFMaskLastN
string
mask_show_first_n(string str[,int n])
Returns a masked version of str, showing the first n characters unmasked. Upper case letters are converted to "X", lower case letters are converted to "x" and numbers are converted to "n". For example, mask_show_first_n("1234-5678-8765-4321", 4) results in 1234-nnnn-nnnn-nnnn.GenericUDFMaskShowFirstN
string
mask_show_last_n(string str[,int n])
Returns a masked version of str, showing the last n characters unmasked. Upper case letters are converted to "X", lower case letters are converted to "x" and numbers are converted to "n". For example, mask_show_last_n("1234-5678-8765-4321", 4) results in nnnn-nnnn-nnnn-4321.GenericUDFMaskShowLastN
string
mask_hash(string|char|varchar str)
Returns a hashed value based on str. The hash is consistent and can be used to join masked values together across tables. This function returns null for non-string types.GenericUDFMaskHash

...

Return Type

Name(Signature)

Description


varies

java_method(class, method[, arg1[, arg2..]])

Synonym for reflect.


varies

reflect(class, method[, arg1[, arg2..]])

Calls a Java method by matching the argument signature, using reflection. See Reflect (Generic) UDF for examples.

GenericUDFReflect

int

hash(a1[, a2...])

Returns a hash value of the arguments. 

GenericUDFHash

string
current_user()
Returns current user name from the configured authenticator manager. Could be the same as the user provided when connecting, but with some authentication managers (for example HadoopDefaultAuthenticator) it could be different.GenericUDFCurrentUser
string
logged_in_user()
Returns the current user name from the session state. This is the username provided when connecting to Hive.GenericUDFLoggedInUser
string
current_database()
Returns current database name.GenericUDFCurrentDatabase
string
md5(string/binary)
Calculates an MD5 128-bit checksum for the string or binary. The value is returned as a string of 32 hex digits, or NULL if the argument was NULL. Example: md5('ABC') = '902fbdd2b1df0c4f70b4a5d23525e932'.UDFMd5
string
sha1(string/binary)
sha(string/binary)
Calculates the SHA-1 digest for string or binary and returns the value as a hex string.
Example: sha1('ABC') = '3c01bdbb26f358bab27f267924aa2c9a03fcfdb8'.
UDFSha1
bigint
crc32(string/binary)
Computes a cyclic redundancy check value for string or binary argument and returns bigint value. Example: crc32('ABC') = 2743272264.UDFCrc32
string
sha2(string/binary,int)
Calculates the SHA-2 family of hash functions (SHA-224, SHA-256, SHA-384, and SHA-512). The first argument is the string or binary to be hashed. The second argument indicates the desired bit length of the result, which must have a value of 224, 256, 384, 512, or 0 (which is equivalent to 256). SHA-224 is supported starting from Java 8. If either argument is NULL or the hash length is not one of the permitted values, the return value is NULL. Example: sha2('ABC', 256) = 'b5d4045c3f466fa91fe2cc6abe79232a1a57cdf104f7a26e716e0a1e2789df78'.GenericUDFSha2
binary
aes_encrypt(inputstring/binary, keystring/binary)
Encrypt input using AES. Key lengths of 128, 192 or 256 bits can be used. 192 and 256 bits keys can be used if Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files are installed. If either argument is NULL or the key length is not one of the permitted values, the return value is NULL.
Example: base64(aes_encrypt('ABC', '1234567890123456')) = 'y6Ss+zCYObpCbgfWfyNWTw=='.
GenericUDFAesEncrypt
binary
aes_decrypt(input binary, keystring/binary)
Decrypt input using AES. Key lengths of 128, 192 or 256 bits can be used. 192 and 256 bits keys can be used if Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files are installed. If either argument is NULL or the key length is not one of the permitted values, the return value is NULL.
Example: aes_decrypt(unbase64('y6Ss+zCYObpCbgfWfyNWTw=='), '1234567890123456') = 'ABC'.
GenericUDFAesDecrypt
string
version()
Returns the Hive version. The string contains 2 fields, the first being a build number and the second being a build hash. Example: "select version();" might return "2.1.0.2.5.0.0-1245 r027527b9c5ce1a3d7d0b6d2e6de2378fb0c39232". Actual results will depend on your build.UDFVersion
bigint
surrogate_key([write_id_bits, task_id_bits])
Automatically generate numerical Ids for rows as you enter data into a table. Can only be used as the default value for acid or insert-only tables.GenericUDFSurrogateKey

...