Challenge - 5 Problems
Delta Merge Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this merge operation?
Given two Spark DataFrames
df_target and df_updates, what will be the content of df_target after the merge?Apache Spark
from pyspark.sql import SparkSession from delta.tables import DeltaTable spark = SparkSession.builder.appName('merge_test').getOrCreate() # Initial target data data_target = [(1, 'Alice', 100), (2, 'Bob', 200)] df_target = spark.createDataFrame(data_target, ['id', 'name', 'score']) df_target.write.format('delta').mode('overwrite').save('/tmp/delta/target') delta_target = DeltaTable.forPath(spark, '/tmp/delta/target') # Updates data data_updates = [(2, 'Bob', 250), (3, 'Charlie', 300)] df_updates = spark.createDataFrame(data_updates, ['id', 'name', 'score']) # Perform merge (upsert) delta_target.alias('t').merge( df_updates.alias('u'), 't.id = u.id' ).whenMatchedUpdate(set={ 'score': 'u.score' }).whenNotMatchedInsert(values={ 'id': 'u.id', 'name': 'u.name', 'score': 'u.score' }).execute() # Read final data result = spark.read.format('delta').load('/tmp/delta/target').orderBy('id').collect() output = [(row['id'], row['name'], row['score']) for row in result] print(output)
Attempts:
2 left
💡 Hint
Think about how matched rows are updated and unmatched rows are inserted.
✗ Incorrect
The merge updates the score for id=2 from 200 to 250 and inserts the new row with id=3. The row with id=1 remains unchanged.
❓ data_output
intermediateHow many rows will the target DataFrame have after this merge?
Consider the following merge operation between
df_target and df_updates. How many rows will df_target contain after the merge?Apache Spark
from pyspark.sql import SparkSession from delta.tables import DeltaTable spark = SparkSession.builder.appName('merge_count').getOrCreate() # Target data data_target = [(1, 'Anna', 50), (2, 'Ben', 60), (3, 'Cara', 70)] df_target = spark.createDataFrame(data_target, ['id', 'name', 'score']) df_target.write.format('delta').mode('overwrite').save('/tmp/delta/target_count') delta_target = DeltaTable.forPath(spark, '/tmp/delta/target_count') # Updates data data_updates = [(2, 'Ben', 65), (4, 'Derek', 80)] df_updates = spark.createDataFrame(data_updates, ['id', 'name', 'score']) # Merge operation delta_target.alias('t').merge( df_updates.alias('u'), 't.id = u.id' ).whenMatchedUpdate(set={'score': 'u.score'}).whenNotMatchedInsert(values={'id': 'u.id', 'name': 'u.name', 'score': 'u.score'}).execute() # Count rows after merge count = spark.read.format('delta').load('/tmp/delta/target_count').count() print(count)
Attempts:
2 left
💡 Hint
Rows with matching ids update, new ids insert.
✗ Incorrect
The original 3 rows remain, one row is updated, and one new row is inserted, totaling 4 rows.
🔧 Debug
advancedWhat error does this merge code raise?
This code attempts a merge operation but fails. What error will it raise?
Apache Spark
from pyspark.sql import SparkSession from delta.tables import DeltaTable spark = SparkSession.builder.appName('merge_error').getOrCreate() data_target = [(1, 'X', 10)] df_target = spark.createDataFrame(data_target, ['id', 'name', 'score']) df_target.write.format('delta').mode('overwrite').save('/tmp/delta/error') delta_target = DeltaTable.forPath(spark, '/tmp/delta/error') data_updates = [(1, 'X', 20)] df_updates = spark.createDataFrame(data_updates, ['id', 'name', 'score']) # Incorrect merge condition (missing alias) delta_target.merge( df_updates, 'id = id' ).whenMatchedUpdate(set={'score': 'df_updates.score'}).execute()
Attempts:
2 left
💡 Hint
Check how the merge condition references columns without aliases.
✗ Incorrect
The merge condition 'id = id' is ambiguous because both DataFrames have a column named 'id'. Spark cannot tell which 'id' is which without aliases.
🚀 Application
advancedWhich option correctly performs an upsert with multiple column updates?
You want to merge
df_updates into df_target updating both score and name columns when matched, and insert all columns when not matched. Which code snippet does this correctly?Attempts:
2 left
💡 Hint
Check for correct alias usage and full insert of all columns.
✗ Incorrect
Option D uses aliases correctly, updates both columns on match, and inserts all columns on no match using whenNotMatchedInsertAll().
🧠 Conceptual
expertWhat is the main advantage of using merge (upsert) operations in Delta Lake?
Select the best explanation for why merge (upsert) operations are important in Delta Lake workflows.
Attempts:
2 left
💡 Hint
Think about data consistency and atomicity in big data pipelines.
✗ Incorrect
Merge operations allow multiple data changes (insert, update, delete) in one atomic step, which keeps data consistent and avoids partial updates.
