cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Failed to merge fields 'LIFNR' and 'LIFNR'. Failed to merge incompatible data types IntegerType and StringType

vijaykumarbotla
New Contributor III

I am have imported a csv file using spark.read method, i have used custom schema and declared the type of the column as string.

i have delta table and the type of the column in the table is also string.

I am getting failed to merge fields errors in spark.write.format('delta') command,

can you please suggest.

1 ACCEPTED SOLUTION

Accepted Solutions

vijaykumarbotla
New Contributor III

Hi All,

the issue is resolved, i have executed column conversion and from next run the code is working fine.

df = spark.read.format("delta").load("/mnt/dev/deltav2/X")

df= df.withColumn("LIFNR", df.LIFNR.cast("string"))

df.write.format('delta').option("overwriteSchema", "true").mode('overwrite').save("/mnt/dev/deltav2/X")

View solution in original post

4 REPLIES 4

-werners-
Esteemed Contributor III

apparently the schema was not applied to your df.

You will have to check why (assuming the delta table col is indeed of type string). Without sharing code it is impossible to know.

pvignesh92
Honored Contributor

@Vijay Kumarโ€‹ 

  1. Check your schema of the table using DESCRIBE TABLE and the dataframe you loaded using df.printSchema() and compare which column has a mismatch. You need to see if your custom schema is matching the table schema.
  2. MergeSchema will support only the schemas that it can typecast.
  3. If you want to completely recreate the table schema with the ones of dataframes, you can use overwriteSchema option.

Please share your code so that we can look and help you.

Anonymous
Not applicable

Hi @Vijay Kumarโ€‹ 

Thank you for posting your question in our community! We are happy to assist you.

To help us provide you with the most accurate information, could you please take a moment to review the responses and select the one that best answers your question?

This will also help other community members who may have similar questions in the future. Thank you for your participation and let us know if you need any further assistance! 

vijaykumarbotla
New Contributor III

Hi All,

the issue is resolved, i have executed column conversion and from next run the code is working fine.

df = spark.read.format("delta").load("/mnt/dev/deltav2/X")

df= df.withColumn("LIFNR", df.LIFNR.cast("string"))

df.write.format('delta').option("overwriteSchema", "true").mode('overwrite').save("/mnt/dev/deltav2/X")

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group