Sunday, 19 November 2017

Analytic Functions in Hive - IV

In the following post, we will take a look at a new set of analytic functions continuing in the series. We will follow the post that we wrote before. For all our work, we will use the Mapr sandbox, MapR-Sandbox-For-Hadoop-5.2.0.

We will look at LAG and LEAD functions. We will use the results of the below query to understand these two functions:

select * from employees where employee_id <= 109 order by salary desc;

The results returned are shown below:  











The two functions are dependent on the order of the rows. If the order of the rows are changed, then, the values returned by these functions also will change as we will see in the queries shortly. LAG and LEAD functions return values from previous of following rows. The offset determines the row that is selected from which the value will be fetched. If no offset is specified, then, value from the immediate previous or immediate following row depending on whether LAG or LEAD function is used, respectively. If no value is available because previous row or next row is non-existent, then NULL, is returned. To avoid NULL values being returned, we can specify default value so that a meaningful result is returned.

As an example, let us run below query:

select employee_id, last_name, hire_date, department_id, salary, lag(salary) over (order by salary desc) as PREVIOUS_SALARY from employees where employee_id <= 109;

The results returned are shown below:  



The second row and further rows correctly carry the salary from the previous row. Two things are to be noted. The first row has NULL for previous salary as there there is no previous row from which salary can be fetched. NULL value is returned as we have not set a default value to LAG function.  Below query corrects both the issues where 0 is returned instead of NULL:

select employee_id, last_name, hire_date, department_id, salary, lag(salary,1,0) over (order by salary desc) as PREVIOUS_SALARY from employees where employee_id <= 109;

The results returned are shown below:











If, however, instead of 1 as offset, we use 0 with no default for NULL value as shown below, the PREVIOUS SALARY column shows the current salary. This is only to be expected.

select employee_id, last_name, hire_date, department_id, salary, lag(salary,0) over (order by salary desc) as PREVIOUS_SALARY from employees where employee_id <= 109;

The results returned are shown below: 












 The next query shows a offset value of 2 using LEAD function but with a different sorting on order.

select employee_id, last_name, hire_date, department_id, salary, lead(salary,2,0) over (order by salary) as FOLLOWING_SALARY from employees where employee_id <= 109;


The results returned are shown below:












 Finally, we introduce the PARTITION keyword in the clause following OVER as shown below:

select employee_id, last_name, hire_date, department_id, salary, lag(salary) over (partition by department_id order by salary desc) as PREVIOUS_SALARY from employees where employee_id <= 109;

The results returned are shown below:










The results are neatly partitioned by department_id.

Thursday, 16 November 2017

Analytic Functions in Hive - III

We will now take a look at more analytic functions continuing in the series. We will follow the post that we wrote before. For all our work, we will use the Mapr sandbox, MapR-Sandbox-For-Hadoop-5.2.0.

RANK as the name suggests ranks any measure in a query using sequential numbers starting from 1. If in the ranking process, a tie is encountered, then, the same rank is repeated for all values that tie and the next rank in sequence is used after skipping as many rank values as was repeated in the tie . Let us see that by means of an example. For starters, let us use the same hr.employees table used in the previous posts. We are interested only in the results of the below query:

select employee_id, last_name, department_id, salary from employees where employee_id <= 109 order by salary desc;

The results returned are shown below:  













The query returns ten records. Let us now run the first query using RANK shown below:

select employee_id, last_name, department_id, salary, rank() over (order by salary desc) as SALARY_RANK from employees where employee_id <= 109;

The results returned are shown below:  













The results are as written above: a tie is seen at 17000, so there is a repeat of ranks for 17000 and at the next value of 12008, rank value 3 is skipped for one repeat of 17000 and a rank of 4 is returned. Same is true for the salary value 9000 where rank of 6 is skipped.

DENSE_RANK is similar to RANK except that in case a of tie, the rank values are not skipped. Let us replace RANK with DENSE_RANK in above query to see this contrast.

select employee_id, last_name, department_id, salary, dense_rank() over (order by salary desc) as SALARY_DENSE_RANK from employees where employee_id <= 109;

The results returned are shown below: 













Results show that DENSE_RANK does not skip any values at all and ranks are repeated at ties. 

Let us add the partition keyword and see the results. The query using partition is show below:

select department_id, last_name, salary, rank() over (partition by department_id order by salary desc) as SALARY_RANK from employees where employee_id <= 109;

The results returned are shown below:













The results show that the RANK function is applied department wise. The same can be tried using DENSE_RANK as shown below:

select department_id, last_name, salary, dense_rank() over (partition by department_id order by salary desc)  as SALARY_DENSE_RANK from employees where employee_id <= 109;

The results returned are shown below:












The results are clearly in line with our understanding of DENSE_RANK as is seen in the ranking of Lorentz.

We will conclude this post with a query for 5 salaries as shown below:

select employee_id, last_name, department_id, salary from employees where employee_id <= 109 order by salary desc limit 5; 

The results returned are shown below:










Tuesday, 14 November 2017

Analytic Functions in Hive - II

In second part of the series on Analytic Functions in Hive, we will follow the blog that was written earlier for Oracle. For all our work, we will use the Mapr sandbox, MapR-Sandbox-For-Hadoop-5.2.0.

For starters, let us run the following query:

select count(*) from employees where department_id < 40;

The results returned are shown below:  













 The result is the same that we have got earlier.

Let us add the OVER keyword to this as shown below:

select count(*) over() from employees where department_id < 40;

The results returned are shown below:  























 
The results returned are the same as before.

The same result is now returned nine times. Since nothing is specified in the parenthesis following OVER, we see that OVER returns the results of COUNT nine times as there are nine rows that satisfy the filter clause without the COUNT and OVER clauses.

The same behaviour can be seen in the below query where we introduce a partition clause as shown below:

select department_id, count(*) over(partition by department_id) from employees where department_id < 40;

The results returned are shown below:  














The results returned are the same as before.

 A more meaningful query is shown below:

select distinct department_id, count(*) over(partition by department_id) from employees where department_id < 40;

The results returned are shown below: 













The results returned are not in line with the results got in the earlier blog for Oracle. While for the all previous queries, the results were the same, when we introduce the distinct keyword, the query in Hive gives incorrect results. After logging into the Mapr Virtual Machine as mapr/mapr, we can run the commands shown below to get the version of Hive as shown below:















Fortunately, we have a work around in the query below to get the correct result:

select department_id, count(*) from employees where department_id < 40 group by department_id; 

The results returned are shown below:









Now the results look good.


Now that we have seen how OVER() and OVER() with a PARTITION clause works, let us see next variant that can be used to Moving calculations. For all the variants shown hereafter, the calculations are dependent on the order of the rows of the result. The first query calculates the moving average
of the salary in the previous record and salary in current record:

select employee_id, first_name, last_name, salary, avg(salary) over (order by employee_id rows between 1 preceding and current row) as moving_avg_of_sal from employees where employee_id <= 105;

The results returned are shown below:












The results returned look the same that we have got before.

The moving average salary of the first row is the same as the salary of that row as there is no previous row. Others salaries are calculated according to the logic correctly.

The second query is similar to the first one with the only difference that the average is calculated based on three values: salary in previous row, salary in current row and salary in next row.

select employee_id, first_name, last_name, salary, avg(salary) over (order by employee_id rows between 1 preceding and 1 following) as moving_avg_of_sal from employees where employee_id <= 105; 

The results returned are shown below:  











The results returned are the same as before.

It is instructive to note that for the first and the last row in the result set, since there only two records, the averages of only two salaries are shown. 

The third query calculates the  moving average of salaries in all previous records.

select employee_id, first_name, last_name, salary, avg(salary) over (order by employee_id rows between unbounded preceding and current row) as moving_avg_of_sal from employees where employee_id <= 105;

The results returned are shown below:  










The results returned are the same as before.

We will next look at FIRST_VALUE. In the first query, we will fetch the lowest salary in departments with department_id 20 and 30. The query is shown below:

select employee_id, department_id, salary, first_value(salary) over (partition by department_id order by salary) as first_sal_in_dept from employees where department_id in (20,30);

The results returned are shown below:  












 As expected, we see that first_sal_in_dept gives the lowest salary in respective departments having department_ids 20 and 30.

We can fetch the highest salary by changing reversing the order of salary as shown below:

select employee_id, department_id, salary, first_value(salary) over (partition by department_id order by salary desc) as first_sal_in_dept from employees where department_id in (20,30);

The results returned are shown below:  













The results are on expected lines. In the case of LAST_VALUE, we get the same funny results we got in Oracle as shown below:

select employee_id, department_id, salary, last_value(salary) over (partition by department_id order by salary) as last_sal_in_dept from employees where department_id in (20,30);











For some reason, value in each row is considered the last value. To correct this, we have to add an additional clause indicating the extent of the application of LAST_VALUE like "rows between unbounded preceding and unbounded following". This will fetch the last value correctly as shown below where the highest salary is obtained:

select employee_id, department_id, salary, last_value(salary) over (partition by department_id order by salary rows between unbounded preceding and unbounded following) as last_sal_in_dept from employees where department_id in (20,30); 












In the same way, we can add desc keyword to the query as shown below to get the minimum salary in the two departments:

select employee_id, department_id, salary, last_value(salary) over (partition by department_id order by salary desc rows between unbounded preceding
and unbounded following) as last_sal_in_dept from employees where department_id in (20,30);












With this treatment of FIRST_VALUE and LAST_VALUE, we come to the close of the second part in this series.