unable to perform modifications on Table while Using Python UDF in query

doremon11
New Contributor

Here, we're trying to use the Python UDF inside the query.

  • taking the table as function input 
  • converting the table into dataframe 
  • performing modification 
  • converting the dataframe into table 
  • returning the table  

How can we create spark context inside UDF in the  query 

 

 

CREATE FUNCTION fun1(input_table TABLE) RETURNS TABLE
LANGUAGE PYTHON
AS $$
  import pandas as pd
  
  df = spark.sql(f"SELECT * FROM {input_table}")
  def fun(df):
      # Convert table to DataFrame
      df.write.saveAsTable("my_table")
      return my_table
  return fun(input_table)
$$;