[Spark][2.3] Fix a data loss bug in MergeIntoCommand #2158
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a cherry-pick of #2128 to delta-2.3 branch.
Which Delta project/connector is this regarding?
Description
Fix a data loss bug in MergeIntoCommand.
It's caused by using different spark session config object for PreprocessTableMerge and MergeIntoCommand. In PreprocessTableMerge, the config value is from the spark session which is given for DeltaTable. In MergeIntoSchema it refers SQLConf.get which is from the active session of the current thread. It can be different when user set another new spark session or active session just doesn't set properly before the execution.
If source dataframe has more columns than target table, auto schema merge feature adds additional nullable column to
target table schema. The updated output projection built in PreprocessTableMerge, so
matchedClauses
andnotMatchedClauses
contains the addtional columns, but target table schema in MergeIntoCommand doesn't have it.As a result, the following index doesn't indicate the delete flag column index, which is numFields - 2.
row.getBoolean returns
getByte() != 0
, which causes dropping rows randomly.matched rows in target table loss
Also as autoMerge doesn't work
newly added column data in source df loss.
The fix makes sure setting active session as the given spark session for target table. The PR applies the fix for other command to avoid any inconsistent config behavior.
Fixes #2104
How was this patch tested?
I confirmed that #2104 is fixed with the change.
I confirmed the following by debug log message without the change:
Does this PR introduce any user-facing changes?
Yes, fixes the data loss issue