cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
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")

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.