https://delta.io logo
r

Ram

06/13/2023, 7:36 AM
Hello Everyone, Im currently trying to use MERGE - WhenMatchedDelete clause like mentioned below. Just wanted to understand what happens if the condition that needs to be specify in the WhenMatchedDelete clause is NONE ? does it delete all the matched records Or It considers None as false in the WhenMatchedDelete condition and drops nothing ? could you please clarify on this ? https://docs.databricks.com/delta/merge.html#language-python
Copy code
deltaTable = ... # DeltaTable with schema (key, value)

changesDF = spark.table("changes")

latestChangeForEachKey = changesDF \
  .selectExpr("key", "struct(time, newValue, deleted) as otherCols") \
  .groupBy("key") \
  .agg(max("otherCols").alias("latest")) \
  .select("key", "latest.*") \

deltaTable.alias("t").merge(
    latestChangeForEachKey.alias("s"),
    "s.key = t.key") \
  .whenMatchedDelete(condition = "s.deleted = true").execute()
vs
Copy code
deltaTable.alias("t").merge(
    latestChangeForEachKey.alias("s"),
    "s.key = t.key") \
  .whenMatchedDelete(None).execute()