<?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 Re: Can't mergeSchema handle int and bigint? in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/can-t-mergeschema-handle-int-and-bigint/m-p/135518#M50374</link>
    <description>&lt;P&gt;Hi Dhruv,&lt;/P&gt;&lt;P&gt;Delta won't automatically upcast unless you explicitly handle it. Cast the column Lob_Pk to LongType (which maps to BIGINT in SQL/Delta). Try below snippet&lt;/P&gt;&lt;P&gt;from pyspark.sql.functions import col&lt;BR /&gt;from pyspark.sql.types import LongType&lt;BR /&gt;&lt;BR /&gt;crm_retail_df = crm_retail_df.withColumn("Lob_Pk", col("Lob_Pk").cast(LongType()))&lt;BR /&gt;crm_retail_df.write \&lt;BR /&gt;.mode("overwrite") \&lt;BR /&gt;.format("delta") \&lt;BR /&gt;.option("mergeSchema", "true") \&lt;BR /&gt;.saveAsTable(silver_table)&lt;/P&gt;</description>
    <pubDate>Tue, 21 Oct 2025 14:02:18 GMT</pubDate>
    <dc:creator>Chiran-Gajula</dc:creator>
    <dc:date>2025-10-21T14:02:18Z</dc:date>
    <item>
      <title>Can't mergeSchema handle int and bigint?</title>
      <link>https://community.databricks.com/t5/data-engineering/can-t-mergeschema-handle-int-and-bigint/m-p/135504#M50369</link>
      <description>&lt;P&gt;I have a table which has a column of data type 'bigint'. While overwriting it with new data, given that I do full loads, I used 'mergeSchema' to handle schema changes. The new data's datatype was int. I thought mergeSchema can easily handle that, but it throws an error.&lt;/P&gt;&lt;DIV&gt;&lt;PRE&gt;&lt;SPAN&gt;crm_retail_df.write.&lt;/SPAN&gt;&lt;SPAN&gt;mode&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;"overwrite"&lt;/SPAN&gt;&lt;SPAN&gt;).&lt;/SPAN&gt;&lt;SPAN&gt;format&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;"delta"&lt;/SPAN&gt;&lt;SPAN&gt;).&lt;/SPAN&gt;&lt;SPAN&gt;option&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;'mergeSchema'&lt;/SPAN&gt;&lt;SPAN&gt;, &lt;/SPAN&gt;&lt;SPAN&gt;True&lt;/SPAN&gt;&lt;SPAN&gt;).&lt;/SPAN&gt;&lt;SPAN&gt;saveAsTable&lt;/SPAN&gt;&lt;SPAN&gt;(silver_table)&lt;BR /&gt;&lt;BR /&gt;-- Output&lt;BR /&gt;[&lt;A class="" href="https://learn.microsoft.com/azure/databricks/error-messages/error-classes#delta_failed_to_merge_fields" target="_blank" rel="noopener noreferrer"&gt;DELTA_FAILED_TO_MERGE_FIELDS&lt;/A&gt;] Failed to merge fields 'Lob_Pk' and 'Lob_Pk' SQLSTATE: 22005&lt;BR /&gt;&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Shouldn't 'mergeSchema' automatically widen the incoming from int to bigint? If not, then does mergeSchema only handle cases when datatypes match between new and old data?&lt;/P&gt;&lt;/DIV&gt;</description>
      <pubDate>Tue, 21 Oct 2025 11:25:23 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/can-t-mergeschema-handle-int-and-bigint/m-p/135504#M50369</guid>
      <dc:creator>Dhruv-22</dc:creator>
      <dc:date>2025-10-21T11:25:23Z</dc:date>
    </item>
    <item>
      <title>Re: Can't mergeSchema handle int and bigint?</title>
      <link>https://community.databricks.com/t5/data-engineering/can-t-mergeschema-handle-int-and-bigint/m-p/135517#M50373</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/99515"&gt;@Dhruv-22&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;No—mergeSchema doesn’t auto-widen an incoming INT column to a table’s BIGINT (nor does it auto-cast). mergeSchema mainly helps add new columns (and historically only a tiny set of numeric upcasts), but it won’t change an existing column’s type or cast your DataFrame to match the table. That’s why you see &lt;STRONG&gt;[DELTA_FAILED_TO_MERGE_FIELDS]&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Doc Link:&amp;nbsp;&lt;/STRONG&gt;&lt;A href="https://www.databricks.com/blog/2019/09/24/diving-into-delta-lake-schema-enforcement-evolution.html" target="_blank" rel="noopener"&gt;https://www.databricks.com/blog/2019/09/24/diving-into-delta-lake-schema-enforcement-evolution.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;However, you can rewrite the schema and data using&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;df_with_new_types
  .write.format("delta")
  .mode("overwrite")
  .option("overwriteSchema", "true")
  .saveAsTable(table_name))&lt;/LI-CODE&gt;
&lt;P&gt;If you are using DBR 15.4 and above, you can try enabling&amp;nbsp;&lt;STRONG&gt;type_widening&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Doc Link :&amp;nbsp;&lt;/STRONG&gt;&lt;A href="https://docs.databricks.com/aws/en/delta/type-widening#enable-type-widening" target="_blank"&gt;https://docs.databricks.com/aws/en/delta/type-widening#enable-type-widening&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Oct 2025 14:06:55 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/can-t-mergeschema-handle-int-and-bigint/m-p/135517#M50373</guid>
      <dc:creator>K_Anudeep</dc:creator>
      <dc:date>2025-10-21T14:06:55Z</dc:date>
    </item>
    <item>
      <title>Re: Can't mergeSchema handle int and bigint?</title>
      <link>https://community.databricks.com/t5/data-engineering/can-t-mergeschema-handle-int-and-bigint/m-p/135518#M50374</link>
      <description>&lt;P&gt;Hi Dhruv,&lt;/P&gt;&lt;P&gt;Delta won't automatically upcast unless you explicitly handle it. Cast the column Lob_Pk to LongType (which maps to BIGINT in SQL/Delta). Try below snippet&lt;/P&gt;&lt;P&gt;from pyspark.sql.functions import col&lt;BR /&gt;from pyspark.sql.types import LongType&lt;BR /&gt;&lt;BR /&gt;crm_retail_df = crm_retail_df.withColumn("Lob_Pk", col("Lob_Pk").cast(LongType()))&lt;BR /&gt;crm_retail_df.write \&lt;BR /&gt;.mode("overwrite") \&lt;BR /&gt;.format("delta") \&lt;BR /&gt;.option("mergeSchema", "true") \&lt;BR /&gt;.saveAsTable(silver_table)&lt;/P&gt;</description>
      <pubDate>Tue, 21 Oct 2025 14:02:18 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/can-t-mergeschema-handle-int-and-bigint/m-p/135518#M50374</guid>
      <dc:creator>Chiran-Gajula</dc:creator>
      <dc:date>2025-10-21T14:02:18Z</dc:date>
    </item>
    <item>
      <title>Re: Can't mergeSchema handle int and bigint?</title>
      <link>https://community.databricks.com/t5/data-engineering/can-t-mergeschema-handle-int-and-bigint/m-p/148834#M52977</link>
      <description>&lt;P&gt;Just out of curiosity: In my impression type_widening would only solve the opposite problem (writing bigint into int) and not the original one (writing int into bigint)?&lt;/P&gt;</description>
      <pubDate>Thu, 19 Feb 2026 17:20:46 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/can-t-mergeschema-handle-int-and-bigint/m-p/148834#M52977</guid>
      <dc:creator>fehrin1</dc:creator>
      <dc:date>2026-02-19T17:20:46Z</dc:date>
    </item>
  </channel>
</rss>

