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: 

merge breaking persistance of dataframe

FabriceDeseyn
Contributor

Hi all

In the minimal example below you can see that executing a merge statement trigger recomputation of a persisted dataframe. How does this happen? 

 

 

from delta.tables import DeltaTable

table_name = "hive_metastore.default.test_table"

# initialize table
data = [{"column1": 1}]
df = spark.createDataFrame(data)
df.write.saveAsTable(table_name, mode="overwrite")
dt_dummy = DeltaTable.forName(spark, table_name)

# Create new data
new_data = [{"column1": 2}]
df_new = spark.createDataFrame(new_data)

# Identify differences
df_diff = dt_dummy.toDF().join(df_new, "column1", "outer")
df_diff = df_diff.persist()
print(f"we now have {df_diff.count()} records in our data")

# Execute merge
print("executing merge")
merge_results = (
    dt_dummy.alias("deltatable")
    .merge(df_diff.alias("updates"), "updates.column1 != deltatable.column1")
    .whenMatchedUpdateAll()
).execute()
# Identify differences after merge
print(f"we now have {df_diff.count()} records in our persisted data")

# --> recompute of persisted dataframe happened

 

 

FabriceDeseyn_1-1694011507567.png

 

This is run on DBR 13.2 and 13.3 LTS.

0 REPLIES 0

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