I have spark sql query which requires using like operator. for example:
spark.sql("select * from tbl where name like '%apple%' ")
Now I have a long list of values
name_list = ['apple', 'orange', 'banana', .......]
My question is how I can build my query from the long python list. What I need is a query like below:
spark.sql("select * from tbl where name like '%apple%' or name like '%orange%' or .... ")
The python list is long and can change. I certainly do not want to hard code everything. Wondering if there is any concise way to achieve that? Thanks!
CodePudding user response:
You can build the or condition using the list of names like this:
import functools
condition = functools.reduce(
lambda acc, x: f"{acc} or name like '%{x}%'",
name_list[1:],
f"name like '%{name_list[0]}%'"
)
spark.sql(f"select * from tbl where {condition}")