02-29-2016 11:44 PM
I want to generate sequence number for each new record in Impala.
1. First load - am loading 100 records with sequence number starting from 1 to 100.
2. Second load - i need to load new 10 records which are not present in first load . So my sequence number has to start from 101 to 110 for these new to records.
I think there is no sequence function here and Next value cannot be used to hold the max value.
Is this possible with any other function or UDFs has to be written ?
03-01-2016 07:12 AM
You could possibly use the ROW_NUMBER() analytic function as part of the solution.
select t2.last_id + 1 + row_number() over (order by table1.col1), table1.col1
inner join (select max(id) last_id from table2) t2
This wouldn't be safe if you are running multiple insert concurrently.
03-02-2016 05:15 AM - edited 03-02-2016 05:16 AM
Thanks Tim for answering it. I tried using UDFrowsequence java code and it always start from 1.
Is there a way to start from max value by passing it ?
03-03-2016 09:46 AM
The Hive UDF won't help you. If you look at the Hive issue tracker https://issues.apache.org/jira/browse/HIVE-1304, row_sequence() was added as a workaround because they didn't support the row_number() analytic function at that point in time.
We support the row_number() analytic function in Impala, so there's no reason to try to use that UDF.
If you want to start froma particular number, can't you just add it like I suggested in my previous answer?
03-03-2016 11:57 PM - edited 03-03-2016 11:57 PM
Yes Tim. That works. Here we need to join target table (table2).
If CREATE SEQUENCE function is there in impala, it would be of more useful instead of doing these tricky things.
Thanks for your reply.
04-24-2017 01:02 AM
Thanks for the solution but i'm not able to understand it prorperly. Can you provide a query with the below issue description. I tried using your solution query but wasn't able to map the col's. So i have a table buyers and its has one col 'id' for which every time an insert happens i want that col to get incremental value's for each insert.
Col: id,name,created on
Can you provide a sample query for this table. It will be really helpful to me as i'm stcuk on this from long time.
06-14-2017 06:32 PM