简体   繁体   中英

running sql queries inside a Snowpark UDF (cannot pickle '_thread.lock')

I want to create a Snowflake Snowpark UDF which returns a single value. However, to converge to this value, I want to run a SQL query inside the function and iterate over the resultant rows.

My code looks something like this:

@udf(.....)
def myFunction(a:str) -> int:
    lookup_df = session.sql('select * from MyTable').collect()
  
    value = 0
    for row in lookup_df:
        value += 1

    return value

However, upon registring this, Snowflake thrws an TypeError :

TypeError: cannot pickle '_thread.lock' object: you might have to save the unpicklable object in the local environment first, add it to the UDF with session.add_import(), and read it from the UDF.

What would be the correct way of going about my problem?

Thank you in advance!

UDFs are not usually able to issue other queries - use a stored procedure instead:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM