pypika
pypika copied to clipboard
How to set Snowflake dialect for all method calls
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
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 Here is the error:
Error: invalid identifier 'QTY_OUT' (line 1)
Removing the double quotes fixes it. The workaround works, thx!