[Spark] Prune MERGE target files by the join keys of a small source #8035
All Vouch review confidence checks passed
Residual risk 0.28 = Change Risk (0.52) × (1 − Review Confidence (0.47)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #8035"
general
weight: 0.35
sezruby
Which Delta project/connector is this regarding?
Description
Resolves #8034.
Motivation
MERGE selects the target files it scans with the target-only conjuncts of its ON condition. An upsert with
ON t.key = s.keyhas none, so it scans every target file, even for a one-row source on a table clustered bykey.Adding the keys to the ON condition by hand only partly works. One key (
AND t.key = 42) skips files. A list (AND t.key IN (42, 7000, 90000)) does not: data skipping only tests each file against the range from the list's smallest to its largest value. Keys spread over the key space overlap almost every file.Change
1. Data skipping for IN lists (
DataFiltersBuilder.constructLiteralInListDataFilters)a = v.SortedValuesInRange. It binary-searches the sorted values for the smallest value>= minand checks that it is<= max.min <= v AND v <= maxterm per value went over the 64 KB limit for a generated method at 150 to 200 BIGINT values (Code grows beyond 64 KB).skipping.inList.exactMaxValuesvalues (default 10000). It is not used for strings with a non-binary collation or for non-column builders; these keep the current range test.2. MERGE with a small source (
MergeIntoCommandBase.getSourceKeyFilePruningPredicates, used byfindTouchedFilesand the insert-onlywriteOnlyInserts)target_column = source_expression.- The target side must be a column that data skipping can use.
- The source side must be a deterministic expression of the source, of the same type.
- FLOAT/DOUBLE keys (
-0.0 = 0.0, NaN), strings with a non-binary collation, and null-safe equality are not used.maxSourceRows + 1rows of these source expressions from the materialized source, withtake.- If the source has more rows, nothing changes.
- Otherwise, MERGE adds
target_column IN (distinct non-null values)per key to the predicates passed tofilterFiles. Data skipping and partition pruning then drop the files that cannot hold any of the keys.- If a key has no non-null value, no target row can match, and the predicate is
false.source_expressionfor some source row.merge.materializeSource). So they are exactly the keys the join sees, and collecting them reads the cached rows, not the source data again. A key missing from them could prune the file of a matching row, and that row would be inserted a second time. Sources that MERGE does not materialize are not pruned: most often a direct read of a Delta table (only deterministic filters and projections), and also insert-only MERGE inautomode andmaterializeSource=none.filterFilesrecords the predicate and the selected files as read, as when the keys are written into the ON condition by hand.- A concurrent transaction that only removes files this MERGE skipped no longer conflicts with it.
- For a partition column key, the partition read predicate is narrower.
delta.dml.merge.sourceKeyFilePruningrecords whether the pruning was applied, whether the source was materialized and why, the number of key columns, and the source rows read.| Config (
spark.databricks.delta.prefix, internal) | Default ||---|---|
|
merge.sourceKeyFilePruning.enabled|true||
merge.sourceKeyFilePruning.maxSourceRows(0 disables) |1000||
skipping.inList.exactMaxValues(1 or less: range test only) |10000|How was this patch tested?
New
MergeIntoSourceKeyFilePruningSuite(19 tests). Each test runs the MERGE on two copies of a 10-file target, with and without the pruning. It checks that both copies end with the same rows, the number of target files read (numTargetFilesAfterSkipping), and the usage event. Cases:autoandallmode, from a view and from a Delta table, with and without deletion vectors.maxSourceRows; with NOT MATCHED BY SOURCE; for sources that are not materialized (materializeSource=none, a Delta table source inautomode, insert-only MERGE inautomode); and without a usable equality (<=>, DOUBLE keys,abs(t.key), no equality between target and source).New tests in
DataSkippingDeltaTests. These run in every suite that mixes it in; locallyDataSkippingDeltaV1SuiteandDataSkippingDeltaV1ColMapNameModeSuite.skipping.inList.exactMaxValues = 1keeps the range test.Negative control. With the IN-list change reverted, the main upsert test reads 9 files instead of 2.
Existing suites run locally with this change (pruning enabled by default):
MergeIntoBasic(path-based, DVs, CDC, column mapping, Scala),MergeIntoSuiteBaseMisc(path-based, DVs),MergeIntoExtendedSyntaxSQLPathBasedSuite,MergeIntoNestedDataSQLPathBasedSuite,MergeIntoNotMatchedBySourceSQLPathBasedSuite,MergeIntoMaterializeSourceMergePersistentDVOffSuite,MergeIntoDVsSQLPathBasedDVsPredPushOnSuite,MergeCDCSQLPathBasedCDCOnSuite,MergeIntoAccumulatorSuite,MergeIntoTimestampConsistencySuite,MergeIntoEmptySchemaTargetSuite,ImplicitMergeCastingSuite,DescribeDeltaHistorySuite,RowTrackingMergeCommonNameBasedCDCOnSuite,DataSkippingDeltaV1Suite,DataSkippingDeltaV1ColMapNameModeSuite,PartitionLikeDataSkippingSuite,StatsCollectionSuite,DeleteMetricsSuite: 1408 tests. An earlier revision also pruned sources that MERGE does not materialize, by reading them again. It failedinsert only merge - target data skipping(the source of an insert-only MERGE was scanned twice) andmaterialize source preserves dataframe hints(an extra query over the source). This revision reads only the materialized source, andgetHintsinMergeIntoMaterializeSourceSuiteskips plans without a join (the plan that reads the keys); these suites pass.Local benchmark (not part of the PR). A 1,000-file, 5M-row (194 MB) table, range-partitioned on
key, on a laptop, local mode. Each run is a fresh shallow clone. The MERGE is an upsert ont.key = s.key; times are best of 3:| Source | Target files read (off → on) | Scan time | MERGE time |
|---|---|---|---|
| 1 key | 1000 → 1 | 520 → 171 ms | 1312 → 855 ms |
| 100 keys, spread | 1000 → 100 | 447 → 188 ms | 1358 → 1208 ms |
| 1000 keys in ~10 files | 1000 → 10 | 447 → 185 ms | 1146 → 944 ms |
| 1000 keys, one per file | 1000 → 965 | 596 → 489 ms | 3819 → 3401 ms |
| 1001 keys (above
maxSourceRows) | 1000 → 1000 | 465 → 478 ms | 3321 → 3374 ms |The scan of the target falls with the number of files it can skip. When the keys hit almost every file there is little to skip. Above
maxSourceRows, the only extra cost is readingmaxSourceRows + 1rows of the materialized source.Does this PR introduce _any_ user-facing changes?
No change in query or MERGE results.
numTargetFilesAfterSkipping.🤖 Generated with Claude Code