pypika icon indicating copy to clipboard operation
pypika copied to clipboard

How to set Snowflake dialect for all method calls

Open info-rchitect opened this issue 3 years ago • 2 comments

Hi,

I use the SnowflakeQuery dialect to make queries. However, if I use any other field-based methods, double-quotes will show up in the final query:

groupby = [
    'lot_id'
    ,'mfg_area_name'
    ,'mfg_step_name'
    ,'product_sub_family'
    ,'test_code'
    ,'test_temperature'
]

t = Table('mytable')

select = groupby + [fn.Sum(t.disp_flag).as_('qty_out'), fn.Count(t.disp_flag).as_('qty_in'), (Field('qty_out')/Field('qty_in')).as_('lot_yield')]

q = SnowflakeQuery.from_(t) \
    .select(*select) \
    .groupby(*groupby)

print(q.get_sql())

produces:

SELECT lot_id,mfg_area_name,mfg_step_name,product_sub_family,test_code,test_temperature,SUM(disp_flag) "qty_out",COUNT(disp_flag) "qty_in",qty_out/qty_in "lot_yield" FROM mytable GROUP BY lot_id,mfg_area_name,mfg_step_name,product_sub_family,test_code,test_temperature

The workaround is search and replace all double quotes after using pypika. Is there a way to set, within the current scope, a dialect search that the search and replace is not necessary?

thx

info-rchitect avatar Sep 13 '21 16:09 info-rchitect

this seems like valid sql for snowflake. is there an exception thrown from the DB?

for a workaround, I think you can override the double-quotes like this:

q.get_sql(alias_quote_char='')

ajustintrue avatar Sep 17 '21 20:09 ajustintrue

@ajustintrue Here is the error:

Error: invalid identifier 'QTY_OUT' (line 1)

Removing the double quotes fixes it. The workaround works, thx!

info-rchitect avatar Sep 18 '21 09:09 info-rchitect