Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS ATHENA: user-defined variables

I need to create a row number by group. In mysql I use "user-defined variables" for that. But in Athena is gives me an error: no viable alternative at input '@'

Here is my query:

SELECT  @row_num := IF(@prev_value=concat_ws('',t.user_id,t.campaign_id,t.placement_id, t.creative_id),@row_num+1,1) AS RowNumber
     ,t.user_id 
     ,t.campaign_id
     ,t.placement_id
     ,t.creative_id
     , t.imp_clk_event_dt
     ,@prev_value := concat_ws('',t.user_id,t.campaign_id,t.placement_id, t.creative_id)
FROM e2c_conv t,
     (SELECT @row_num := 1) x,
     (SELECT @prev_value := '') y ORDER BY t.user_id,t.campaign_id,t.placement_id, t.creative_id, t.imp_clk_event_dt

Any suggestions on how to resolve this problem will be highly appreciated.

like image 926
user3825422 Avatar asked Aug 21 '17 13:08

user3825422


3 Answers

TLDR: AWS Athena does not support variables.

As per their documentation, AWS Athena is based on Presto 0.172 with some limitations. Presto currently has an open issue where the addition of variables is being discussed. PR-5918

like image 113
harwood Avatar answered Oct 31 '22 00:10

harwood


with Mandeep as 
              ( 
               select 123 as m_1
                    , 234 as m_2
                ) -- Variable_table (Define Variables here)
select * 
from (
select 123 as a
      , 'abc' as v) as b 
left join Mandeep as m on 1 = 1 - mapping with rows
where a = m.m_1 -- using variables
like image 21
Mandeep Singh HYDERABAD Avatar answered Oct 30 '22 23:10

Mandeep Singh HYDERABAD


Athena now supports Prepared Statements for parameterized queries:

You can use the Athena parameterized query feature to prepare statements for repeated execution of the same query with different query parameters. A prepared statement contains parameter placeholders whose values are supplied at execution time. Prepared statements enable Athena queries to take parameters directly and help to prevent SQL injection attacks.

like image 2
Matthew Avatar answered Oct 30 '22 23:10

Matthew