Apache Hive : Hive Operators

Last updated: December 12, 2024

Apache Hive : Hive Operators

Operators Precedences

ExampleOperatorsDescription
A[B] , A.identifierbracket_op([]), dot(.)element selector, dot
-Aunary(+), unary(-), unary(~)unary prefix operators
A IS [NOT] (NULLTRUEFALSE)
A ^ Bbitwise xor(^)bitwise xor
A * Bstar(*), divide(/), mod(%), div(DIV)multiplicative operators
A + Bplus(+), minus(-)additive operators
AB
A & Bbitwise and(&)bitwise and
ABbitwise or(

Relational Operators

The following operators compare the passed operands and generate a TRUE or FALSE value depending on whether the comparison between the operands holds.

OperatorOperand typesDescription
A = BAll primitive typesTRUE if expression A is equal to expression B otherwise FALSE.
A == BAll primitive typesSynonym for the = operator.
A <=> BAll primitive typesReturns same result with EQUAL(=) operator for non-null operands, but returns TRUE if both are NULL, FALSE if one of them is NULL. 
A <> BAll primitive typesNULL if A or B is NULL, TRUE if expression A is NOT equal to expression B, otherwise FALSE.
A != BAll primitive typesSynonym for the <> operator.
A < BAll primitive typesNULL if A or B is NULL, TRUE if expression A is less than expression B, otherwise FALSE.
A <= BAll primitive typesNULL if A or B is NULL, TRUE if expression A is less than or equal to expression B, otherwise FALSE.
A > BAll primitive typesNULL if A or B is NULL, TRUE if expression A is greater than expression B, otherwise FALSE.
A >= BAll primitive typesNULL if A or B is NULL, TRUE if expression A is greater than or equal to expression B, otherwise FALSE.
A [NOT] BETWEEN B AND CAll primitive typesNULL if A, B, or C is NULL, TRUE if A is greater than or equal to B AND A less than or equal to C, otherwise FALSE. This can be inverted by using the NOT keyword. 
A IS NULLAll typesTRUE if expression A evaluates to NULL, otherwise FALSE.
A IS NOT NULLAll typesFALSE if expression A evaluates to NULL, otherwise TRUE.
A IS [NOT] (TRUEFALSE)Boolean types
A [NOT] LIKE BstringsNULL if A or B is NULL, TRUE if string A matches the SQL simple regular expression B, otherwise FALSE. The comparison is done character by character. The _ character in B matches any character in A (similar to . in POSIX regular expressions) while the % character in B matches an arbitrary number of characters in A (similar to .* in posix regular expressions). For example, ‘foobar’ like ‘foo’ evaluates to FALSE whereas ‘foobar’ like ‘foo_ _ _’ evaluates to TRUE and so does ‘foobar’ like ‘foo%’.
A RLIKE BstringsNULL if A or B is NULL, TRUE if any (possibly empty) substring of A matches the Java regular expression B, otherwise FALSE. For example, ‘foobar’ RLIKE ‘foo’ evaluates to TRUE and so does ‘foobar’ RLIKE ‘^f.*r$’.
A REGEXP BstringsSame as RLIKE.

Arithmetic Operators

The following operators support various common arithmetic operations on the operands. All return number types; if any of the operands are NULL, then the result is also NULL.

OperatorOperand typesDescription
A + BAll number typesGives the result of adding A and B. The type of the result is the same as the common parent(in the type hierarchy) of the types of the operands. For example, since every integer is a float, therefore float is a containing type of integer so the + operator on a float and an int will result in a float.
A - BAll number typesGives the result of subtracting B from A. The type of the result is the same as the common parent(in the type hierarchy) of the types of the operands.
A * BAll number typesGives the result of multiplying A and B. The type of the result is the same as the common parent(in the type hierarchy) of the types of the operands. Note that if the multiplication causes overflow, you will have to cast one of the operators to a type higher in the type hierarchy.
A / BAll number typesGives the result of dividing A by B. The result is a double type in most cases. When A and B are both integers, the result is a double type except when the hive.compat configuration parameter is set to “0.13” or “latest” in which case the result is a decimal type.
A DIV BInteger typesGives the integer part resulting from dividing A by B. E.g 17 div 3 results in 5.
A % BAll number typesGives the remainder resulting from dividing A by B. The type of the result is the same as the common parent(in the type hierarchy) of the types of the operands.
A & BAll number typesGives the result of bitwise AND of A and B. The type of the result is the same as the common parent(in the type hierarchy) of the types of the operands.
ABAll number types
A ^ BAll number typesGives the result of bitwise XOR of A and B. The type of the result is the same as the common parent(in the type hierarchy) of the types of the operands.
~AAll number typesGives the result of bitwise NOT of A. The type of the result is the same as the type of A.

Logical Operators

The following operators provide support for creating logical expressions. All of them return a boolean TRUE, FALSE, or NULL depending upon the boolean values of the operands. NULL behaves as an “unknown” flag, so if the result depends on the state of an unknown, the result itself is unknown.

OperatorOperand typesDescription
A AND BbooleanTRUE if both A and B are TRUE, otherwise FALSE. NULL if A or B is NULL.
A OR BbooleanTRUE if either A or B or both are TRUE, FALSE OR NULL is NULL, otherwise FALSE.
NOT AbooleanTRUE if A is FALSE or NULL if A is NULL. Otherwise FALSE.
! AbooleanSame as NOT A.
A IN (val1, val2, …)booleanTRUE if A is equal to any of the values.
A NOT IN (val1, val2, …)booleanTRUE if A is not equal to any of the values. 
[NOT] EXISTS (subquery)booleanTRUE if the subquery returns at least one row.

String Operator

OperatorOperand typesDescription
AB

Complex Type Constructors

The following functions construct instances of complex types.

Constructor FunctionOperandsDescription
map(key1, value1, key2, value2, …)Creates a map with the given key/value pairs.
struct(val1, val2, val3, …)Creates a struct with the given field values. Struct field names will be col1, col2, etc
named_struct(name1, val1, name2, val2, …)Creates a struct with the given field names and values.
array(val1, val2, …)Creates an array with the given elements.
create_union(tag, val1, val2, …)Creates a union type with the value that is being pointed to by the tag parameter.

Operators on Complex Types

The following operators provide mechanisms to access elements in Complex Types.

OperatorOperand typesDescription
A[n]A is an Array and n is an intReturns the nth element in the array A. The first element has index 0. For example, if A is an array comprising of [‘foo’, ‘bar’] then A[0] returns ‘foo’ and A[1] returns ‘bar’.
M[key]M is a Map<K, V> and key has type KReturns the value corresponding to the key in the map. For example, if M is a map comprising of {‘f’ -> ‘foo’, ‘b’ -> ‘bar’, ‘all’ -> ‘foobar’} then M[‘all’] returns ‘foobar’.
S.xS is a structReturns the x field of S. For example for the struct foobar {int foo, int bar}, foobar.foo returns the integer stored in the foo field of the struct.