<?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: error during delta table merge with CHAR data type in Data Engineering</title>
    <link>https://community.fabric.microsoft.com/t5/Data-Engineering/error-during-delta-table-merge-with-CHAR-data-type/m-p/4080419#M3443</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/714599"&gt;@Winnie2024&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks to&amp;nbsp;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/777262"&gt;@SachinNandanwar&lt;/a&gt;&amp;nbsp;'s suggestion. Based on my test, it is very helpful!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PySpark doesn’t have a direct CHAR or &lt;SPAN&gt;VARCHAR&amp;nbsp;&lt;/SPAN&gt;type, you can use &lt;STRONG&gt;StringType()&lt;/STRONG&gt; or &lt;STRONG&gt;"&lt;SPAN&gt;STRING&lt;/SPAN&gt;"&lt;/STRONG&gt; to&amp;nbsp;&lt;SPAN&gt;represent the&amp;nbsp;VARCHAR(15) and&amp;nbsp;CHAR(3) data types. &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Try&lt;/SPAN&gt;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;from delta.tables import DeltaTable
from pyspark.sql.types import StringType

# Create a Delta table with two columns
DeltaTable.createIfNotExists(spark) \
    .tableName("test") \
    .addColumn("id", StringType()) \
    .addColumn("code", StringType()) \
    .execute()&lt;/LI-CODE&gt;
&lt;P&gt;or&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;from delta.tables import *
from pyspark.sql.types import *

DeltaTable.createIfNotExists(spark) \
    .tableName("test") \
    .addColumn("id", "STRING") \
    .addColumn("code", "STRING") \
    .execute()&lt;/LI-CODE&gt;
&lt;P&gt;Based on my test, both of the above work for your later merge test code without any error.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best Regards,&lt;BR /&gt;Jing&lt;BR /&gt;&lt;EM&gt;If this post helps, please Accept it as Solution to help other members find it. Appreciate your Kudos! &lt;/EM&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 05 Aug 2024 08:20:28 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2024-08-05T08:20:28Z</dc:date>
    <item>
      <title>error during delta table merge with CHAR data type</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/error-during-delta-table-merge-with-CHAR-data-type/m-p/4077178#M3431</link>
      <description>&lt;P&gt;I need to create one delte table. And it will be upserted many times as the time on. I have the code like below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;from delta.tables import *&lt;/P&gt;&lt;P&gt;DeltaTable.createIfNotExists(spark) \&lt;BR /&gt;.tableName("test") \&lt;BR /&gt;.addColumn("id", "VARCHAR(15)")\&lt;BR /&gt;.addColumn("code", "CHAR(3)")\&lt;BR /&gt;.execute()&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;dt_test = DeltaTable.forName(spark, "test")&lt;/P&gt;&lt;P&gt;dt_test_update = spark.createDataFrame([&lt;BR /&gt;("1","001"),&lt;BR /&gt;("2","002"),&lt;BR /&gt;],&lt;BR /&gt;schema=["id","code"]&lt;BR /&gt;)&lt;/P&gt;&lt;P&gt;dt_test.alias('test') \&lt;BR /&gt;.merge(&lt;BR /&gt;dt_test_update.alias('updates'),&lt;BR /&gt;'test.id = updates.id'&lt;BR /&gt;) \&lt;BR /&gt;.whenNotMatchedInsertAll()\&lt;BR /&gt;.whenMatchedUpdateAll()\&lt;BR /&gt;.execute()&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;df = spark.sql("select * from test")&lt;BR /&gt;display(df)&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;got the error below :&lt;/P&gt;&lt;P&gt;"Resolved attribute(s) id#33730,code#33731 missing from id#33631,code#33633,_metadata#33735 in operator !Project [id#33730, staticinvoke(class org.apache.spark.sql.catalyst.util.CharVarcharCodegenUtils, StringType, readSidePadding, code#33731, 3, true, false, true) AS code#33732]. Attribute(s) with the same name appear in the operation: id,code. Please check if the right attribute(s) are used.;"&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;But if I change '.addColumn("code", "CHAR(3)")\' to '.addColumn("code", "VARCHAR(3)")\' when i create the delta table.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;It will be OK.&lt;/P&gt;&lt;P&gt;Any idea about this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 03 Aug 2024 09:14:06 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/error-during-delta-table-merge-with-CHAR-data-type/m-p/4077178#M3431</guid>
      <dc:creator>Winnie2024</dc:creator>
      <dc:date>2024-08-03T09:14:06Z</dc:date>
    </item>
    <item>
      <title>Re: error during delta table merge with CHAR data type</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/error-during-delta-table-merge-with-CHAR-data-type/m-p/4077196#M3432</link>
      <description>&lt;P&gt;I think it would be better to use STRING data types instead of CHAR&lt;/P&gt;</description>
      <pubDate>Sat, 03 Aug 2024 09:57:25 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/error-during-delta-table-merge-with-CHAR-data-type/m-p/4077196#M3432</guid>
      <dc:creator>SachinNandanwar</dc:creator>
      <dc:date>2024-08-03T09:57:25Z</dc:date>
    </item>
    <item>
      <title>Re: error during delta table merge with CHAR data type</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/error-during-delta-table-merge-with-CHAR-data-type/m-p/4080419#M3443</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/714599"&gt;@Winnie2024&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks to&amp;nbsp;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/777262"&gt;@SachinNandanwar&lt;/a&gt;&amp;nbsp;'s suggestion. Based on my test, it is very helpful!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PySpark doesn’t have a direct CHAR or &lt;SPAN&gt;VARCHAR&amp;nbsp;&lt;/SPAN&gt;type, you can use &lt;STRONG&gt;StringType()&lt;/STRONG&gt; or &lt;STRONG&gt;"&lt;SPAN&gt;STRING&lt;/SPAN&gt;"&lt;/STRONG&gt; to&amp;nbsp;&lt;SPAN&gt;represent the&amp;nbsp;VARCHAR(15) and&amp;nbsp;CHAR(3) data types. &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Try&lt;/SPAN&gt;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;from delta.tables import DeltaTable
from pyspark.sql.types import StringType

# Create a Delta table with two columns
DeltaTable.createIfNotExists(spark) \
    .tableName("test") \
    .addColumn("id", StringType()) \
    .addColumn("code", StringType()) \
    .execute()&lt;/LI-CODE&gt;
&lt;P&gt;or&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;from delta.tables import *
from pyspark.sql.types import *

DeltaTable.createIfNotExists(spark) \
    .tableName("test") \
    .addColumn("id", "STRING") \
    .addColumn("code", "STRING") \
    .execute()&lt;/LI-CODE&gt;
&lt;P&gt;Based on my test, both of the above work for your later merge test code without any error.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best Regards,&lt;BR /&gt;Jing&lt;BR /&gt;&lt;EM&gt;If this post helps, please Accept it as Solution to help other members find it. Appreciate your Kudos! &lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 05 Aug 2024 08:20:28 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/error-during-delta-table-merge-with-CHAR-data-type/m-p/4080419#M3443</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2024-08-05T08:20:28Z</dc:date>
    </item>
  </channel>
</rss>

