<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic unable to perform modifications  on Table  while Using Python UDF in query in Warehousing &amp; Analytics</title>
    <link>https://community.databricks.com/t5/warehousing-analytics/unable-to-perform-modifications-on-table-while-using-python-udf/m-p/62884#M1222</link>
    <description>&lt;P&gt;Here, we're trying to use the&amp;nbsp;Python UDF inside the query.&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;taking the table as function input&amp;nbsp;&lt;/LI&gt;&lt;LI&gt;converting the table into dataframe&amp;nbsp;&lt;/LI&gt;&lt;LI&gt;performing modification&amp;nbsp;&lt;/LI&gt;&lt;LI&gt;converting the dataframe into table&amp;nbsp;&lt;/LI&gt;&lt;LI&gt;returning the table&amp;nbsp;&amp;nbsp;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;How can we create spark context inside UDF in the&amp;nbsp; query&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="php"&gt;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)
$$;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 07 Mar 2024 11:36:08 GMT</pubDate>
    <dc:creator>doremon11</dc:creator>
    <dc:date>2024-03-07T11:36:08Z</dc:date>
    <item>
      <title>unable to perform modifications  on Table  while Using Python UDF in query</title>
      <link>https://community.databricks.com/t5/warehousing-analytics/unable-to-perform-modifications-on-table-while-using-python-udf/m-p/62884#M1222</link>
      <description>&lt;P&gt;Here, we're trying to use the&amp;nbsp;Python UDF inside the query.&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;taking the table as function input&amp;nbsp;&lt;/LI&gt;&lt;LI&gt;converting the table into dataframe&amp;nbsp;&lt;/LI&gt;&lt;LI&gt;performing modification&amp;nbsp;&lt;/LI&gt;&lt;LI&gt;converting the dataframe into table&amp;nbsp;&lt;/LI&gt;&lt;LI&gt;returning the table&amp;nbsp;&amp;nbsp;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;How can we create spark context inside UDF in the&amp;nbsp; query&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="php"&gt;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)
$$;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Mar 2024 11:36:08 GMT</pubDate>
      <guid>https://community.databricks.com/t5/warehousing-analytics/unable-to-perform-modifications-on-table-while-using-python-udf/m-p/62884#M1222</guid>
      <dc:creator>doremon11</dc:creator>
      <dc:date>2024-03-07T11:36:08Z</dc:date>
    </item>
    <item>
      <title>Re: unable to perform modifications  on Table  while Using Python UDF in query</title>
      <link>https://community.databricks.com/t5/warehousing-analytics/unable-to-perform-modifications-on-table-while-using-python-udf/m-p/120871#M2101</link>
      <description>&lt;P&gt;Hi team,&lt;BR /&gt;I believe you cannot create or access a SparkSession or run Spark operations like spark.sql() directly inside a Python UDF.&amp;nbsp;&lt;CODE data-start="573" data-end="586"&gt;input_table&lt;/CODE&gt; is a table argument, not a string with a table name. You receive it as a pandas DataFrame when using &lt;CODE data-start="696" data-end="711"&gt;RETURNS TABLE&lt;/CODE&gt;&lt;/P&gt;
&lt;P data-start="1479" data-end="1589"&gt;You need to define your logic outside SQL in a notebook and&amp;nbsp;use regular Spark APIs:&lt;/P&gt;
&lt;DIV class="contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary"&gt;
&lt;DIV class="flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none rounded-t-[5px]"&gt;&lt;CODE class="whitespace-pre! language-python"&gt;&lt;SPAN class="hljs-keyword"&gt;def&lt;/SPAN&gt; &lt;SPAN class="hljs-title function_"&gt;process_table&lt;/SPAN&gt;(&lt;SPAN class="hljs-params"&gt;table_name&lt;/SPAN&gt;):&lt;/CODE&gt;&lt;/DIV&gt;
&lt;DIV class="overflow-y-auto p-4" dir="ltr"&gt;&lt;CODE class="whitespace-pre! language-python"&gt;&lt;SPAN&gt;    df = spark.table(table_name)
    &lt;SPAN class="hljs-comment"&gt;# Transform using Spark DataFrame APIs&lt;/SPAN&gt;
    df = df.withColumn(&lt;SPAN class="hljs-string"&gt;"new_col"&lt;/SPAN&gt;, df[&lt;SPAN class="hljs-string"&gt;"existing_col"&lt;/SPAN&gt;]+2)
    df.write.mode(&lt;SPAN class="hljs-string"&gt;"overwrite"&lt;/SPAN&gt;).saveAsTable(&lt;SPAN class="hljs-string"&gt;"processed_table"&lt;/SPAN&gt;)&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P data-start="1833" data-end="1895"&gt;Then call &lt;CODE data-start="1843" data-end="1870"&gt;process_table("my_table")&lt;/CODE&gt; in your notebook or job. Hope this helps!&lt;/P&gt;</description>
      <pubDate>Wed, 04 Jun 2025 04:20:00 GMT</pubDate>
      <guid>https://community.databricks.com/t5/warehousing-analytics/unable-to-perform-modifications-on-table-while-using-python-udf/m-p/120871#M2101</guid>
      <dc:creator>Vidhi_Khaitan</dc:creator>
      <dc:date>2025-06-04T04:20:00Z</dc:date>
    </item>
  </channel>
</rss>

