dongjoon-hyun opened a new pull request, #60512:
URL: https://github.com/apache/spark/pull/60512
### What changes were proposed in this pull request?
This PR aims to backport #60509 to `branch-4.x`.
It makes the rewrites of DSv2 `DELETE`, `UPDATE` and `MERGE` build their
write projections in linear time in the number of table columns.
- `RewriteRowLevelCommand` computes the output of the rewritten plan once
per `buildReplaceDataProjections` or `buildWriteDeltaProjections` call, and
looks up the column names in a map from each name to its first ordinal. It
still scans the output with the resolver when no column has exactly the given
name. So it returns the same ordinals as before: the first column with exactly
the given name, or else the first column that the resolver matches
(SPARK-60978).
- `createProjectingInternalRow` and `generateExpandOutput` index
`IndexedSeq` copies of the outputs instead of `List`s.
`createProjectingInternalRow` also zips the attributes with their ordinals
instead of indexing the ordinals.
- `buildReplaceDataUpdateProjection` and `buildWriteDeltaUpdateProjection`
in `RewriteUpdateTable` index an `IndexedSeq` of the assigned values and use
its size instead of `assignments.size`.
```scala
// Before
private def findColOrdinal(plan: LogicalPlan, name: String): Int = {
val ordinal = plan.output.indexWhere(_.name == name)
if (ordinal != -1) ordinal else plan.output.indexWhere(attr =>
conf.resolver(attr.name, name))
}
```
```scala
// After
private def colOrdinalFinder(plan: LogicalPlan): String => Int = {
val output = plan.output.toIndexedSeq
val exactOrdinals = mutable.HashMap.empty[String, Int]
output.zipWithIndex.foreach { case (attr, ordinal) =>
// keep the first ordinal of each name like indexWhere
exactOrdinals.getOrElseUpdate(attr.name, ordinal)
}
name => exactOrdinals.getOrElse(name, output.indexWhere(a =>
conf.resolver(a.name, name)))
}
```
The rewritten plans and their projections stay the same.
### Why are the changes needed?
For a table with `W` columns, the row projection looks up `W` column names
in the output of the rewritten plan, and `findColOrdinal` evaluated
`plan.output` for each of them. The rewrites of `UPDATE` and group-based
`DELETE` end with a `Project`, whose `output` is
`projectList.map(_.toAttribute)`, and `Alias.toAttribute` creates a new
`AttributeReference`. So building the projections of an `UPDATE` created about
`W * W` attributes. In addition, several helpers indexed `List`s by position in
per-column loops. That takes another `O(W * W)` steps for every statement,
including `MERGE`, whose `MergeRows` keeps its output in a field.
For example, with a DSv2 table `t` that supports row-level operations and
has the columns `pk`, `c0`, ..., `c{W-1}` and `dep`:
```sql
UPDATE t SET c0 = -1 WHERE pk = 1
```
Local measurements on `master` with an Apple M3 Max, JDK 21 and
`InMemoryRowLevelOperationTableCatalog`, with `spark.sql.planChangeValidation`
turned off (the best of 3 to 5 runs). The time of the whole analysis of the
statement above on a group-based (copy-on-write) table, and of
`RewriteUpdateTable` in it:
| Columns (W) | Analysis before | Analysis after | `RewriteUpdateTable`
before | `RewriteUpdateTable` after |
|---:|---:|---:|---:|---:|
| 1,000 | 36 ms | 14 ms | 23 ms | 1.5 ms |
| 5,000 | 718 ms | 61 ms | 663 ms | 6.4 ms |
| 10,000 | 3.09 s | 126 ms | 2.97 s | 13 ms |
With 10,000 columns, the analysis allocated 12.2 GiB before and 261 MiB
after, measured with `ThreadMXBean.getCurrentThreadAllocatedBytes`. The other
statements, also with 10,000 columns:
| Statement | Rule | Before | After |
|---|---|---:|---:|
| Delta-based `UPDATE` | `RewriteUpdateTable` | 2.53 s | 11 ms |
| Delta-based `UPDATE` that splits updates into deletes and inserts |
`RewriteUpdateTable` | 425 ms | 14 ms |
| Group-based `UPDATE ... WHERE pk IN (SELECT ...)` | `RewriteUpdateTable` |
1.02 s | 16 ms |
| Group-based `DELETE FROM t WHERE pk = 1` | `RewriteDeleteFromTable` | 975
ms | 5.4 ms |
| Group-based `MERGE ... WHEN MATCHED THEN UPDATE SET c0 = s.v` |
`RewriteMergeIntoTable` | 410 ms | 15 ms |
| Delta-based `MERGE ... WHEN MATCHED THEN UPDATE SET c0 = s.v` |
`RewriteMergeIntoTable` | 415 ms | 15 ms |
A delta-based `DELETE` builds no row projection, so it took 4 ms before and
after. This PR changes only the analysis. The optimizer and the physical
planning get the same plans as before.
`RewriteRowLevelCommand` and `RewriteUpdateTable` are identical in `master`
and `branch-4.x`, so `branch-4.x` has the same cost.
This is not a regression. `findColOrdinal` and the positional lookups date
from [v3.5.0](https://github.com/apache/spark/releases/tag/v3.5.0)
(2023-09-09), when SPARK-43324, SPARK-43885 and SPARK-43975 added the
delta-based `UPDATE` and `MERGE` and the group-based `UPDATE`. SPARK-50820
([v4.0.0](https://github.com/apache/spark/releases/tag/v4.0.0), 2025-05-19)
started to build the projections of group-based operations with them.
### Does this PR introduce _any_ user-facing change?
No. The rewritten plans are the same. The analyzer only spends less time and
memory on DSv2 `DELETE`, `UPDATE` and `MERGE` statements over wide tables.
### How was this patch tested?
Pass the CIs with the existing tests, such as the SPARK-60978 test cases
that check the exact name lookup and the fallback to the resolver. The
following suites also pass locally on `branch-4.x` with Java 17.
```
build/sbt 'catalyst/testOnly *ResolveEncodersInUDFSuite *AnalysisSuite
*PullupCorrelatedPredicatesSuite *ReplaceNullWithFalseInPredicateSuite
*RewriteWithExpressionSuite *V2CommandTreePatternSuite *ConvertToCatalystSuite'
build/sbt 'sql/testOnly *BasedUpdateTableSuite
*BasedNoMetadataUpdateTableSuite *DeltaBasedUpdateAsDeleteAndInsertTableSuite
*BasedDeleteFromTableSuite *BasedNoMetadataDeleteFromTableSuite
*BasedMergeIntoTableSuite *BasedNoMetadataMergeIntoTableSuite
*DeltaBasedMergeIntoTableUpdateAsDeleteAndInsertSuite *MergeIntoDataFrameSuite
*BasedRowLevelOperationCatalystRuntimeFilterSuite'
build/sbt 'sql/testOnly *BasedMergeIntoSchemaEvolution*Suite
*AlignUpdateAssignmentsSuite *AlignMergeAssignmentsSuite'
build/sbt 'sql/testOnly *TimestampNanosDml*Suite *GeneratedColumnWriteSuite
*SchemaAlignmentConfigSuite *DataSourceV2EnhancedDeleteFilterSuite
*DataSourceV2OptionSuite *CreateTableWriteOrderSuite *PlanResolutionSuite
*CheckConstraintSuite *ParseSqlAnalyzeSuite'
```
Unlike `master`, `branch-4.x` uses Java 17 as the base version of the
benchmark results files because it does not have SPARK-60831 (#60046). So the
results files are not cherry-picked from `master`. Instead,
`RowLevelOperationBenchmark-results.txt` (JDK 17),
`RowLevelOperationBenchmark-jdk21-results.txt` (JDK 21) and
`RowLevelOperationBenchmark-jdk25-results.txt` (JDK 25) are regenerated with
the GitHub Actions `benchmark.yml` workflow on `AMD EPYC 7763` for `branch-4.x`.
The local numbers above come from a harness on `master`, which is not part
of this PR. The same harness compared the rewritten plans before and after
#60509 for 75 statements. They cover `UPDATE` (including an assignment to the
row ID and a subquery condition), `DELETE` and `MERGE` (with all clause types)
on group-based, delta-based, split-update and no-metadata tables. The tables
include columns whose names differ only by case, and a row ID column whose name
differs in case from the reference of the connector. The row, row ID and
metadata projections (ordinals, nullability and metadata), the outputs of
`Expand` and `MergeRows`, and the plan trees were all the same.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 5.5
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]