You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@hive.apache.org by elango vaidyanathan <el...@gmail.com> on 2021/11/26 03:09:18 UTC

Hive variable behaviour

Hi all,

I am trying to create a hive variable and store the current time stamp.
Then run some complex select queries and then create another variable to
store current timestamp and then find the runtime.
The problem I am facing is every time I use the variable it gives me the
current time instead of the assigned value. It just runs the current
timestamp function every time. Below is the code snippet.

Code

set hivevar:start_tm=current_timestamp();

select ${hivevar:start_tm};

select query;

select ${hivevar:start_tm};


Here the start_tm variable calculates every time the current time and
returns always the current time. How can I just store the value once and
use that value for this variable. Ease clarify.

Thanks,
Elango

Re: Hive variable behaviour

Posted by Pau Tallada <ta...@pic.es>.
Hi,

Note that CURRENT_TIMESTAMP is only evaluated once, when you assign to the
variable.

Why not using something like:

set hivevar:start_tm=current_timestamp;

select ${hivevar:start_tm};

select query;

select CURRENT_TIMESTAMP;


Cheers,

Pau.

Missatge de Zoltan Haindrich <ki...@rxd.hu> del dia dt., 30 de nov. 2021 a
les 9:17:

> Hey Elango!
>
> Variables are not evaluated at assignment time - instead they are
> evaluated at the time of usage.
>
> If you want to save the current time somehow I think you could:
> * put the current_timestamp into a temporary table:
>    create temporary table start_time as select current_timestamp() as t;
> * not sure about your use case; but if you need the start date of the
> session you could set it as an external variable when you start the session:
>    beeline -hivevar start_time=`date "+%Y-%m-%d %H:%M:%S"
>    select current_timestamp() - cast('${hivevar:start_time}' as timestamp);
> * consider using hplsql I think with that it might be possible to store it
> in a variable; however hplsql approaches things a bit differently...
>    I can't give you an example how to do this right the top of my head
>
>
> cheers,
> Zoltan
>
> On 11/30/21 7:03 AM, elango vaidyanathan wrote:
> >
> >
> > Hi All, any updates on this?
> >
> > Thanks,
> > Elango
> >
> > On Fri, Nov 26, 2021, 11:09 AM elango vaidyanathan <elango.rk@gmail.com
> <ma...@gmail.com>> wrote:
> >
> >
> >     Hi all,
> >
> >     I am trying to create a hive variable and store the current time
> stamp. Then run some complex select queries and then create another
> variable to store current timestamp
> >     and then find the runtime.
> >     The problem I am facing is every time I use the variable it gives me
> the current time instead of the assigned value. It just runs the current
> timestamp function every
> >     time. Below is the code snippet.
> >
> >     Code
> >
> >     set hivevar:start_tm=current_timestamp();____
> >
> >     select ${hivevar:start_tm};____
> >
> >     select query;__
> >
> >     select ${hivevar:start_tm};
> >
> >
> >     Here the start_tm variable calculates every time the current time
> and returns always the current time. How can I just store the value once
> and use that value for this
> >     variable. Please clarify.
> >
> >
> >     Thanks,
> >     Elango
> >
>


-- 
----------------------------------
Pau Tallada Crespí
Departament de Serveis
Port d'Informació Científica (PIC)
Tel: +34 93 170 2729
----------------------------------

Re: Hive variable behaviour

Posted by Zoltan Haindrich <ki...@rxd.hu>.
Hey Elango!

Variables are not evaluated at assignment time - instead they are evaluated at the time of usage.

If you want to save the current time somehow I think you could:
* put the current_timestamp into a temporary table:
   create temporary table start_time as select current_timestamp() as t;
* not sure about your use case; but if you need the start date of the session you could set it as an external variable when you start the session:
   beeline -hivevar start_time=`date "+%Y-%m-%d %H:%M:%S"
   select current_timestamp() - cast('${hivevar:start_time}' as timestamp);
* consider using hplsql I think with that it might be possible to store it in a variable; however hplsql approaches things a bit differently...
   I can't give you an example how to do this right the top of my head


cheers,
Zoltan

On 11/30/21 7:03 AM, elango vaidyanathan wrote:
> 
> 
> Hi All, any updates on this?
> 
> Thanks,
> Elango
> 
> On Fri, Nov 26, 2021, 11:09 AM elango vaidyanathan <elango.rk@gmail.com <ma...@gmail.com>> wrote:
> 
> 
>     Hi all,
> 
>     I am trying to create a hive variable and store the current time stamp. Then run some complex select queries and then create another variable to store current timestamp
>     and then find the runtime.
>     The problem I am facing is every time I use the variable it gives me the current time instead of the assigned value. It just runs the current timestamp function every
>     time. Below is the code snippet.
> 
>     Code
> 
>     set hivevar:start_tm=current_timestamp();____
> 
>     select ${hivevar:start_tm};____
> 
>     select query;__
> 
>     select ${hivevar:start_tm};
> 
> 
>     Here the start_tm variable calculates every time the current time and returns always the current time. How can I just store the value once and use that value for this
>     variable. Please clarify.
> 
> 
>     Thanks,
>     Elango
> 

Re: Hive variable behaviour

Posted by elango vaidyanathan <el...@gmail.com>.
Hi All, any updates on this?

Thanks,
Elango

On Fri, Nov 26, 2021, 11:09 AM elango vaidyanathan <el...@gmail.com>
wrote:

>
> Hi all,
>
> I am trying to create a hive variable and store the current time stamp.
> Then run some complex select queries and then create another variable to
> store current timestamp and then find the runtime.
> The problem I am facing is every time I use the variable it gives me the
> current time instead of the assigned value. It just runs the current
> timestamp function every time. Below is the code snippet.
>
> Code
>
> set hivevar:start_tm=current_timestamp();
>
> select ${hivevar:start_tm};
>
> select query;
>
> select ${hivevar:start_tm};
>
>
> Here the start_tm variable calculates every time the current time and
> returns always the current time. How can I just store the value once and
> use that value for this variable. Please clarify.
>
> Thanks,
> Elango
>