'using Trino to access his table and passing list/tuple to dynamically filter query in the where clause
I have encountered an issue using dynamic filtering and passing a list/tuple into the where clause, and I am not very experienced in python so please help me.
I am using conn_hms = trino.dbapi.connect to HMS table, and I define the read function below:
def read_hms(conn_hms, q, start_day, end_day, sf, query):
cur = conn_hms.cursor()
cur.execute(q, params=[start_day, end_day, sf, query])
rows = cur.fetchall()
column_names = [desc[0] for desc in cur.description]
data = pd.DataFrame(rows, columns=column_names)
return data
so i will pass in start/end day, sf, and query into the sql later (query parameter is the list/tuple i want to filter in sql)
sf_choice = str(market.value)
start = start_date.value.strftime("%Y-%m-%d")
end = end_date.value.strftime("%Y-%m-%d")
genre_converted = """
SELECT query
,kind_type
,COUNT(*) AS frequencies
FROM
table abc
WHERE
DATE_TRUNC('day', date)>= from_iso8601_date(?) and DATE_TRUNC('day', date)<= from_iso8601_date(?)
AND market = ?
AND query in ?
GROUP BY 1,2
order by query, COUNT(*) desc
"""
top_genre = read_hms(conn_hms, genre_converted, start, end, sf_choice, queries)
Until the point i make query a list/ tuple, this works, but now they are returning syntex error like "TrinoUserError: TrinoUserError(type=USER_ERROR, name=SYNTAX_ERROR, message="line 14:27: mismatched input 'in'. Expecting: 'AND', 'EXCEPT', 'FETCH', 'GROUP', 'HAVING', 'INTERSECT', 'LIMIT', 'OFFSET', 'OR', 'ORDER', 'UNION', 'WINDOW', ", query_id=20220228_004211_00817_et3jr)".
Could you please help me on this?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
