Merge Operation in delta table in databricks update records shows wrong information.

Vaibhav B 31 Reputation points
2022-07-06T12:38:33.967+00:00

Hi all,

I am performing merge operation on my delta table in spark databricks

my existing delta table is dlt_salary, it already has some records

Now i created another dataframe followed by new source using tempView which is going to use as updated source as df_source in merge statement.
Now my updated source table is df_source -->Please check the snips

here if you see i have one record is updated and One newly inserted

After merge Operation The count should show updated 1 and inserted 1

but its not showing the desired thing ,I am confused in the log table created after merge statement.
Please help me to understand what's going on behind the scenes.218145-updated-source-tables-for-merge.png218108-initial-delta-table.png218118-merger-statment.png

Azure Databricks
Azure Databricks
An Apache Spark-based analytics platform optimized for Azure.
1,947 questions
{count} votes

1 answer

Sort by: Most helpful
  1. HimanshuSinha-msft 19,381 Reputation points Microsoft Employee
    2022-07-06T21:27:38.877+00:00

    Hello @Vaibhav B ,
    Thanks for the question and using MS Q&A platform.
    As we understand the ask here is how the MERGE operatios works, please do let us know if its not accurate.

    You said

    After merge Operation The count should show updated 1 and inserted 1

    But the snashot says Updated -3
    218372-image.png

    Let me share what i think is going on .

    We are clear about the INSERT part , as we have 1 ID ( ID 4 , Viraj ) which is not in the dlt_salary table and so the INSERT happens for this record .
    For the update , the dlt_salary and df_source have 3 records which are common ( matched i should say ID , 1,2,3 ) and so three records are updated .
    Behind the scene MERGE command is combination of INSERT and UPDATE statement on match records .

    Please do let me if you have any queries.
    Thanks
    Himanshu


    • Please don't forget to click on 130616-image.png or upvote 130671-image.png button whenever the information provided helps you. Original posters help the community find answers faster by identifying the correct answer. Here is how
    • Want a reminder to come back and check responses? Here is how to subscribe to a notification
      • If you are interested in joining the VM program and help shape the future of Q&A: Here is how you can be part of Q&A Volunteer Moderators
    0 comments No comments