- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-28-2024 10:47 AM
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?
- Labels:
-
Spark
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-28-2024 06:06 PM
Hi @Dnirmania, You could achieve something similar using this UDF:
%sql
CREATE OR REPLACE FUNCTION ryanlakehouse.default.column_masking(column_value STRING, groups_str String)
RETURNS STRING
LANGUAGE SQL
COMMENT 'Return the column value if user is part of group otherwise return masked value'
RETURN CASE
WHEN array_contains(
transform(
split(groups_str, ','),
x -> is_account_group_member(trim(x))
),
True
) THEN column_value
ELSE repeat("*", len(column_value))
END
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-28-2024 06:06 PM
Hi @Dnirmania, You could achieve something similar using this UDF:
%sql
CREATE OR REPLACE FUNCTION ryanlakehouse.default.column_masking(column_value STRING, groups_str String)
RETURNS STRING
LANGUAGE SQL
COMMENT 'Return the column value if user is part of group otherwise return masked value'
RETURN CASE
WHEN array_contains(
transform(
split(groups_str, ','),
x -> is_account_group_member(trim(x))
),
True
) THEN column_value
ELSE repeat("*", len(column_value))
END
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2024 01:34 AM
Thank you so much @menotron. Your suggestion worked perfectly.

