Passing values from a CTE (Common Table Expression) to user-defined functions (UDF) in Spark SQL
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 hours ago
I think the issue is in your subquery. You shouldn't have the entire cte query in parentheses. Only the column from your CTE. Your FROM clause is inside your udf arguments. See if you can use the example below to fix the issue.
CREATE OR REPLACE FUNCTION my_udf(input_str1 STRING, input_str2 STRING, input_int INT)
RETURNS STRING
RETURN CONCAT('Processed: ', input_str1, ', ', input_str2, ', ', CAST(input_int AS STRING));
with t1 as (
select 1 as one, 'sample' as data1, 'example1' as data2
union all
select 2, 'example', 'example2'
union all
select 3, 'test', 'example3'
)
select
my_udf(data1, data2, one) AS processed_data
from t1;

