Hi Team
I am trying to create a python UDF which I want to use for column masking. This function will take 2 input parameters(column name and groups name) and return the column value if user is part of group otherwise return masked value. I wrote following line of code but it seems we can't uses spark.sql in python UDF to execute sql query.
Can anyone help me to execute SQL query in python function?
CREATE OR REPLACE FUNCTION ryanlakehouse.default.column_masking(column_value STRING, groups_str String)
RETURNS STRING
LANGUAGE PYTHON
AS $$
def mask_column(column, groups_str):
groups = groups_str.split(',')
for group in groups:
if spark.sql(f"SELECT is_account_group_member('{group}')").collect()[0][0]:
return column
return '******'
column = "{column_value}"
groups_str = groups_str
return mask_column(column, groups_str)
$$;