<?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 Custom ENUM input as parameter for SQL UDF? in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/custom-enum-input-as-parameter-for-sql-udf/m-p/66831#M33211</link>
    <description>&lt;P&gt;Hello &amp;nbsp;-&amp;nbsp;&lt;/P&gt;&lt;P&gt;We're migrating from T-SQL to Spark SQL.&amp;nbsp;We're migrating a significant number of queries.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;"datediff(unit, start,end)" is different between these two implementations (in a good way). &amp;nbsp;For the purpose of migration, we'd like to stay as consistent with possible, so I was evaluating writing a custom migration-specific UDF that emulated the t-sql behavior - so people could transition over time with minimal disruption.&lt;BR /&gt;I'm bumping into something I have no idea how to achieve - allowing ENUMs to be exposed to SQL from a Python function.&lt;BR /&gt;For example -&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;SELECT&lt;BR /&gt;DATEDIFF(&lt;BR /&gt;&amp;nbsp; &amp;nbsp; DAY, &amp;nbsp;-- &amp;lt;---- the enum unit&lt;BR /&gt;&amp;nbsp; &amp;nbsp; TIMESTAMP'1999-12-31 23:59:59',&lt;BR /&gt;&amp;nbsp; &amp;nbsp; TIMESTAMP'2000-01-01 23:59:58'&lt;BR /&gt;) diff_day&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Note the "DAY" there is NOT a string - it's some for of ENUM exposed to Spark SQL. I'd like to achieve that similar behavior if at all possible, but I'm not sure how to go about doing it. I'm writing the function in Python and then registering the UDF ala:&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;def&lt;/SPAN&gt; &lt;SPAN&gt;my_date_diff&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;unit&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;start_date&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;end_date&lt;/SPAN&gt;&lt;SPAN&gt;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; ...&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;spark.udf.&lt;/SPAN&gt;&lt;SPAN&gt;register&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;"udf_my_date_diff"&lt;/SPAN&gt;&lt;SPAN&gt;, my_date_diff, &lt;/SPAN&gt;&lt;SPAN&gt;IntegerType&lt;/SPAN&gt;&lt;SPAN&gt;())&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;When I call this in SQL, though, I get an error (which I kind of expected):&lt;/P&gt;&lt;P&gt;%sql&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;select&lt;/SPAN&gt;&lt;SPAN&gt; udf_my_date_diff(&lt;/SPAN&gt;&lt;SPAN&gt;DAY&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;TIMESTAMP&lt;/SPAN&gt;&lt;SPAN&gt;'1999-12-31 23:59:59'&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;TIMESTAMP&lt;/SPAN&gt;&lt;SPAN&gt;'2000-01-01 23:59:58'&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;[UNRESOLVED_COLUMN.WITHOUT_SUGGESTION]&amp;nbsp;A column, variable, or function parameter with name `DAY` cannot be resolved. SQLSTATE: 42703&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Is there a way to expose my custom ENUM as a type when calling the UDF from SQL?&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&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>Sat, 20 Apr 2024 17:12:43 GMT</pubDate>
    <dc:creator>EWhitley</dc:creator>
    <dc:date>2024-04-20T17:12:43Z</dc:date>
    <item>
      <title>Custom ENUM input as parameter for SQL UDF?</title>
      <link>https://community.databricks.com/t5/data-engineering/custom-enum-input-as-parameter-for-sql-udf/m-p/66831#M33211</link>
      <description>&lt;P&gt;Hello &amp;nbsp;-&amp;nbsp;&lt;/P&gt;&lt;P&gt;We're migrating from T-SQL to Spark SQL.&amp;nbsp;We're migrating a significant number of queries.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;"datediff(unit, start,end)" is different between these two implementations (in a good way). &amp;nbsp;For the purpose of migration, we'd like to stay as consistent with possible, so I was evaluating writing a custom migration-specific UDF that emulated the t-sql behavior - so people could transition over time with minimal disruption.&lt;BR /&gt;I'm bumping into something I have no idea how to achieve - allowing ENUMs to be exposed to SQL from a Python function.&lt;BR /&gt;For example -&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;SELECT&lt;BR /&gt;DATEDIFF(&lt;BR /&gt;&amp;nbsp; &amp;nbsp; DAY, &amp;nbsp;-- &amp;lt;---- the enum unit&lt;BR /&gt;&amp;nbsp; &amp;nbsp; TIMESTAMP'1999-12-31 23:59:59',&lt;BR /&gt;&amp;nbsp; &amp;nbsp; TIMESTAMP'2000-01-01 23:59:58'&lt;BR /&gt;) diff_day&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Note the "DAY" there is NOT a string - it's some for of ENUM exposed to Spark SQL. I'd like to achieve that similar behavior if at all possible, but I'm not sure how to go about doing it. I'm writing the function in Python and then registering the UDF ala:&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;def&lt;/SPAN&gt; &lt;SPAN&gt;my_date_diff&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;unit&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;start_date&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;end_date&lt;/SPAN&gt;&lt;SPAN&gt;&lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; ...&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;spark.udf.&lt;/SPAN&gt;&lt;SPAN&gt;register&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;"udf_my_date_diff"&lt;/SPAN&gt;&lt;SPAN&gt;, my_date_diff, &lt;/SPAN&gt;&lt;SPAN&gt;IntegerType&lt;/SPAN&gt;&lt;SPAN&gt;())&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;When I call this in SQL, though, I get an error (which I kind of expected):&lt;/P&gt;&lt;P&gt;%sql&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;select&lt;/SPAN&gt;&lt;SPAN&gt; udf_my_date_diff(&lt;/SPAN&gt;&lt;SPAN&gt;DAY&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;TIMESTAMP&lt;/SPAN&gt;&lt;SPAN&gt;'1999-12-31 23:59:59'&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;TIMESTAMP&lt;/SPAN&gt;&lt;SPAN&gt;'2000-01-01 23:59:58'&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;[UNRESOLVED_COLUMN.WITHOUT_SUGGESTION]&amp;nbsp;A column, variable, or function parameter with name `DAY` cannot be resolved. SQLSTATE: 42703&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Is there a way to expose my custom ENUM as a type when calling the UDF from SQL?&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&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>Sat, 20 Apr 2024 17:12:43 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/custom-enum-input-as-parameter-for-sql-udf/m-p/66831#M33211</guid>
      <dc:creator>EWhitley</dc:creator>
      <dc:date>2024-04-20T17:12:43Z</dc:date>
    </item>
  </channel>
</rss>

