Hive, a data warehouse software built on Hadoop, provides a variety of functions for aggregating and analyzing data. These functions allow users to perform complex data analysis and create meaningful reports. Hive supports basic aggregation using GROUP BY, advanced aggregation with GROUPING SETS, ROLLUP, and CUBE, and also offers powerful analytic functions and windowing clauses.
The most common form of aggregation in Hive uses the GROUP BY clause. This clause groups rows that have the same values in specified columns into summary rows. The GROUP BY clause is typically used with aggregation functions like COUNT, SUM, AVG, MIN, and MAX.
For example, to count the number of parts for each part type (p_type), you'd use the following query:
SELECT p_type, count(*) FROM part GROUP BY p_type;
This query groups all rows with the same p_type and then counts the number of rows in each group.
Advanced Aggregation: GROUPING SETS, ROLLUP, and CUBE
Hive provides more advanced aggregation options for generating multiple grouping combinations in a single query, which is more efficient than writing multiple GROUP BY queries.
GROUPING SETS: This clause allows you to specify multiple GROUP BY clauses in a single statement. It's useful when you want to see subtotals for different sets of columns. For example, you could get a count of parts by p_type and also a total count of all parts in one query.
ROLLUP: This is a shorthand for GROUPING SETS that generates all possible subtotals for a given hierarchy. It creates a hierarchy of groupings, from the most detailed to the grand total. For example, GROUP BY ROLLUP(col1, col2) would produce groupings for (col1, col2), (col1), and (), where () represents the grand total.
CUBE: This is another shorthand that generates all possible combinations of subtotals. It's similar to ROLLUP but it generates a combination of subtotals for all possible combinations of the specified columns. GROUP BY CUBE(col1, col2) would generate groupings for (col1, col2), (col1), (col2), and ().
Analytic Functions and Windowing Clauses
Analytic functions (also known as window functions) compute values over a "window" of rows. Unlike aggregation functions that collapse rows into a single summary row, analytic functions return a value for each row in the result set. They are often used to calculate a running total, a moving average, or a rank within a group.
The OVER clause defines the window or set of rows over which the function operates. Within the OVER clause, you can use:
PARTITION BY: This divides the rows into partitions (groups) on which the analytic function is applied independently. This is similar to GROUP BY, but it doesn't collapse the rows.
ORDER BY: This specifies the order of rows within each partition. This is crucial for functions like RANK or ROW_NUMBER.
Windowing clauses: These clauses, like ROWS BETWEEN or RANGE BETWEEN, further refine the set of rows within a partition. For instance, ROWS BETWEEN 2 PRECEDING AND CURRENT ROW would define a window of the current row and the two rows before it.
collect_set Function
The collect_set function is an aggregation function that returns an array of unique objects for a given group. This is useful for gathering all distinct values for a column into a single list for each group. .
The example SELECT p_type, collect_set(p_name), count(*) FROM part GROUP BY p_type; groups the data by p_type. For each group, it returns a unique set of p_name values and a count of the rows. The result for each p_type would be a single row containing the type, an array of unique part names, and the total count.