What is Hive Query Language? HiveQL Operators

โšก Smart Summary

Hive Query Language supplies the operators that drive every HiveQL expression, grouping them into relational, arithmetic, logical, complex-type and constructor families that together cover comparison, calculation and access to nested data.

  • ๐Ÿงฎ Five operator families: Relational, arithmetic, logical, complex-type and constructor operators cover every expression a HiveQL query needs.
  • ๐Ÿ” Comparison takes any primitive: Relational operators accept all primitive types, while LIKE, RLIKE and REGEXP work on strings only.
  • โž• Arithmetic includes bitwise work: Beyond the four basic operations, HiveQL exposes modulo and bitwise AND, OR, XOR and NOT on number types.
  • ๐Ÿงฉ Nested data has its own syntax: A[n] reaches an array element and M[key] reaches a map value, both usable anywhere an expression is allowed.
  • ๐Ÿ—๏ธ Constructors build complex values: array(), map(), struct(), named_struct() and create_union() assemble complex columns inside a query.
  • ๐Ÿ“š Functions extend the operators: Mathematical, string, date, conditional, collection and aggregate functions handle work no operator covers.

What is Hive Query Language (HiveQL) operators

What is Hive Query Language (HiveQL)?

Hive Query Language (HiveQL) is a query language in Apache Hive for processing and analyzing structured data. It separates users from the complexity of MapReduce programming. It reuses common concepts from relational databases, such as tables, rows, columns, and schema, to ease learning. Hive provides a CLI for Hive query writing using Hive Query Language (HiveQL).

Most interactions tend to take place over a command line interface (CLI). Generally, HiveQL syntax is similar to the SQL syntax that most data analysts are familiar with. The four file formats named in the original Hive documentation are TEXTFILE, SEQUENCEFILE, ORC and RCFILE (Record Columnar File); current releases also read and write Parquet and Avro, and ORC is the format most tuning guidance assumes.

Hive uses an embedded Derby database for single-user metadata storage. For multi-user or shared metastore deployments, Hive uses MySQL or another external relational database instead, because Derby allows only one session at a time.

HiveQL Built-in Operators

Hive provides built-in operators for the data operations that run against the tables held in the Hive warehouse.

These operators act on operands inside an expression and return a value according to the logic applied, whether that logic is a comparison, a calculation or a lookup into a nested column.

Below are the main types of built-in operators in HiveQL:

  • Relational operators
  • Arithmetic operators
  • Logical operators
  • Operators on complex types
  • Complex type constructors

Each family is covered in its own section below, with the full operator table for that family.

Relational Operators in HiveQL

We use relational operators for relationship comparisons between two operands.

  • Operators such as equals, not equals, less than and greater than.
  • The operand types are all primitive types in these operators, and the pattern-matching operators accept strings only.

Every relational operator returns a BOOLEAN, which is why these operators are what a WHERE clause, a JOIN condition and a CASE WHEN branch are built from. The following table gives details about relational operators and their usage in HiveQL:

Built-in Operator Description Operand
X = Y TRUE if expression X is equivalent to expression Y. Otherwise FALSE. It takes all primitive types
X != Y TRUE if expression X is not equivalent to expression Y. Otherwise FALSE. It takes all primitive types
X < Y TRUE if expression X is less than expression Y. Otherwise FALSE. It takes all primitive types
X <= Y TRUE if expression X is less than or equal to expression Y. Otherwise FALSE. It takes all primitive types
X > Y TRUE if expression X is greater than expression Y. Otherwise FALSE. It takes all primitive types
X >= Y TRUE if expression X is greater than or equal to expression Y. Otherwise FALSE. It takes all primitive types
X IS NULL TRUE if expression X evaluates to NULL, otherwise FALSE. It takes all types
X IS NOT NULL FALSE if expression X evaluates to NULL, otherwise TRUE. It takes all types
X LIKE Y TRUE if string pattern X matches to Y, otherwise FALSE. Takes only Strings
X RLIKE Y NULL if X or Y is NULL, TRUE if any substring of X matches the Java regular expression Y, otherwise FALSE. Takes only Strings
X REGEXP Y Same as RLIKE. Takes only Strings

Note that a comparison against NULL never returns TRUE or FALSE. That is the reason the table lists IS NULL and IS NOT NULL as separate operators rather than expecting X = NULL to work.

HiveQL Arithmetic Operators

We use arithmetic operators for performing arithmetic operations on operands.

  • Arithmetic operations such as addition, subtraction, multiplication and division between operands use these operators.
  • The operand types are all number types in these operators.

Sample example:

2 + 3 gives the result 5.

In this example, ‘+’ is the operator, 2 and 3 are the operands, and the return value is 5.

The following table gives details about arithmetic operators in Hive Query Language:

Built-in Operator Description Operand
X + Y It will return the output of adding X and Y value. It takes all number types
X – Y It will return the output of subtracting Y from X value. It takes all number types
X * Y It will return the output of multiplying X and Y values. It takes all number types
X / Y It will return the output of dividing Y from X. It takes all number types
X % Y It will return the remainder resulting from dividing X by Y. It takes all number types
X & Y It will return the output of bitwise AND of X and Y. It takes all number types
X | Y It will return the output of bitwise OR of X and Y. It takes all number types
X ^ Y It will return the output of bitwise XOR of X and Y. It takes all number types
~X It will return the output of bitwise NOT of X. It takes all number types

The last four rows are bitwise rather than ordinary arithmetic: they operate on the binary representation of integer operands, so &, | and ^ are not the same as the logical AND, OR and NOT covered in the next section.

HiveQL Logical Operators

We use logical operators for performing logical operations on operands.

  • Logical operations such as AND, OR and NOT between operands use these operators.
  • The operand types are all BOOLEAN type in these operators.

The following table gives details about logical operators in HiveQL:

Operators Description Operands
X AND Y TRUE if both X and Y are TRUE, otherwise FALSE. Boolean types only
X && Y Same as X AND Y, but here we use the && symbol. Boolean types only
X OR Y TRUE if either X or Y or both are TRUE, otherwise FALSE. Boolean types only
X || Y Same as X OR Y, but here we use the || symbol. Boolean types only
NOT X TRUE if X is FALSE, otherwise FALSE. Boolean types only
!X Same as NOT X, but here we use the ! symbol. Boolean types only

Because relational operators return BOOLEAN values, logical operators are what combine them: a predicate such as salary > 50000 AND dept = ‘Sales’ chains one family into the other.

Operators on Complex Types

Relational, arithmetic and logical operators all work on single scalar values. Hive also stores array, map and struct columns, and those need a different mechanism to reach the elements inside them. The following table gives details about complex type operators, which provide that mechanism:

Operators Operands Description
A[n] A is an Array and n is an integer type It will return the nth element in the array A. The first element has index 0.
M[key] M is a Map<K, V> and key has type K It will return the value belonging to the key in the map.

Both forms are ordinary expressions, so they can appear in a SELECT list, a WHERE clause or a GROUP BY, and a struct field is reached with dot notation such as S.field instead of a bracket.

Complex Type Constructors

Where the operators above read an existing complex column, constructors build one. The following table gives details about complex type constructors, which create instances of the complex data types โ€” Array, Map and Struct โ€” in Hive.

In this section, we are going to see the operations performed on complex type constructors.

Operators Operands Description
array (val1, val2, …) It will create an array with the given elements as mentioned, like val1, val2.
create_union (tag, val1, val2, …) It will create a union type with the value that is referenced by the tag parameter.
map (key1, value1, key2, value2, …) It will create a map with the given key/value pairs mentioned in the operands.
named_struct (name1, val1, name2, val2, …) It will create a Struct with the given field names and values mentioned in the operands.
struct (val1, val2, val3, …) Creates a Struct with the given field values. Struct field names will be col1, col2, and so on.

Constructors are most often used in an INSERT that populates a complex column, or in a SELECT that packs several scalar columns into one struct before writing the result to another table.

HiveQL Built-in Functions

Operators cover comparison, calculation and element access, but they do not round a number, trim a string or subtract two dates. That work belongs to Hive’s built-in functions, which are called by name inside the same expressions the operators appear in. The table below groups the families a beginner meets first.

Function family Typical members What it is used for
Mathematical round(), floor(), ceil(), abs(), pow(), sqrt(), rand() Rounding, powers and other numeric calculations on a single column value.
String concat(), substr(), upper(), lower(), trim(), length(), regexp_replace() Cleaning, trimming and reshaping text before it is compared or grouped.
Date current_date, year(), month(), datediff(), date_add(), unix_timestamp() Extracting date parts and measuring intervals between two dates.
Conditional if(), CASE WHEN, coalesce(), nvl(), isnull() Choosing a value at row level and replacing NULL with a fallback.
Collection size(), map_keys(), map_values(), array_contains(), sort_array() Inspecting the array, map and struct columns reached by the complex-type operators.
Type conversion cast(), binary() Forcing a column into the type an operator expects.
Aggregate (UDAF) count(), sum(), avg(), min(), max(), collect_set() Collapsing many rows into one value, normally alongside GROUP BY.

The function list depends on the Hive release, so it is worth reading it from the session itself rather than from a static page. Two statements do that:

SHOW FUNCTIONS;

DESCRIBE FUNCTION round;

DESCRIBE FUNCTION EXTENDED round;

SHOW FUNCTIONS lists every registered name, DESCRIBE FUNCTION prints the one-line signature, and the EXTENDED form adds usage examples where the implementing class supplies them. When no built-in function fits, Hive accepts a user defined function written in Java.

HiveQL vs SQL: Key Differences

HiveQL borrows SQL syntax deliberately, and the operator tables above will look familiar to anyone who has written SQL. The execution model underneath is not the same, and the differences show up as soon as a query moves beyond a simple SELECT.

Behaviour HiveQL Traditional SQL (RDBMS)
Schema handling Schema on read โ€” the schema is applied when the file is queried Schema on write โ€” the schema is enforced when the row is inserted
Row-level UPDATE and DELETE Supported only on ACID tables; managed tables are ACID by default from Hive 3.0 Supported on every table by default
Indexes Index support was removed in Hive 3.0 (HIVE-18448); ORC or Parquet file-level indexes and materialized views replace it B-tree and other secondary indexes are a core feature
Execution and latency Compiles to batch jobs on Tez, MapReduce or Spark, so even small queries carry job start-up cost Interactive execution, usually in milliseconds
Design goal Throughput over very large files on HDFS or an object store Low-latency reads and writes on a single server or cluster

The practical consequence for expression writing is small but real. Operators behave as expected, yet a predicate that would be answered from an index in a relational database is answered in Hive by scanning files, so filtering on a partition column is usually worth far more than tuning the operator itself.

FAQs

Keywords, operator words such as AND or LIKE, and function names resolve case-insensitively, so SELECT and select behave the same. String data does not: comparing ‘Sales’ with ‘sales’ returns FALSE unless upper() or lower() is applied first.

X / Y always returns a DOUBLE, even when both operands are integers, so 7 / 2 gives 3.5 rather than 3. Use the DIV keyword for integer division and the % operator for the remainder.

Arithmetic and comparison operators propagate NULL: any expression involving NULL evaluates to NULL, not FALSE. Test with IS NULL or IS NOT NULL, and substitute a fallback with coalesce() or nvl() before the value reaches an operator.

LIKE uses SQL wildcards, where % matches any run of characters and _ matches one. RLIKE matches a Java regular expression against any substring of the value. REGEXP is a synonym for RLIKE and behaves identically.

Arithmetic operators bind first, then comparison operators, then NOT, then AND, then OR. Because the ordering is easy to misread in long predicates, parentheses are recommended whenever AND and OR appear in the same clause.

Yes. Relational and logical operators form join conditions, arithmetic operators can produce grouping keys, and complex-type access such as M[key] is valid wherever an expression is allowed, including SELECT lists, WHERE clauses and ORDER BY.

Machine learning assistants translate a plain-language filter into a candidate predicate and flag type mismatches, such as comparing a string column with a number. Always confirm the generated operator against the tables above, because dialects differ between Hive, Spark SQL and Presto.

It handles common predicates well and suggests coalesce() or CASE WHEN patterns from a short comment. It also mixes in syntax from other engines, so verify bitwise operators, complex-type access and any function name with DESCRIBE FUNCTION before running the query.

Summarize this post with: