Skip to content

feat(spark): support Spark SQL UPDATE, DELETE and MERGE INTO on merge-on-read tables without a record key in the update-as-delete-insert mode - #21020

Merged
yihua merged 8 commits into
apache:masterfrom
yihua:keyless-mor-sql-dml-delete-insert
Dec 3, 2026

Conversation

@yihua

@yihua yihua commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Describe the issue this Pull Request addresses

issue: #20175

In the update-as-delete-insert write mode (hoodie.write.updates.as.deletes.and.inserts), Spark SQL UPDATE, DELETE and MERGE INTO were refused on merge-on-read tables without a record key, since a write that looks rows up by record key cannot locate rows whose keys the writer generates.

Summary and Changelog

These statements do not need the lookup: they read the target rows with their meta columns, so every record already carries the record key, partition path and file group of the row it replaces. On such a table they now write prepped records in the mode. The Spark write client accepts a prepped upsert and delete in the mode, the commit action executor reads the base file position of each prepped record, and the prepped upsert executor splits each located update into a positional delete plus an insert through the same split the write helper applies to the records the index locates, including its MERGE INTO evaluation and format meta column values. When an upsert combines the records of a key before tagging, the combined record now keeps the location of the record it is built from; outside the mode this fixes a keyless MERGE INTO on a table with ordering fields that wrote the combined record as an insert next to the row it replaced.

Still refused on such a table: an upsert or delete by record key, an INSERT INTO that upserts, a datasource write of a frame that carries the meta columns, prepped inserts, and every prepped write of the Java client.

Tests: the Spark SQL suite runs UPDATE, DELETE and MERGE INTO on partitioned and non-partitioned tables with the simple and the record level index, across a compaction and across partition moves, checking the rows and that every log block holds deletes at the positions of the deleted rows. The Spark client test writes a prepped upsert and delete in the mode.

Two follow-up commits come from a self-review. The first locates a record at the commit time of the base file its position comes from, so after a compaction whose earlier base file is not cleaned yet, the format meta column values of a prepped record are read from the right row (a row lineage test reproduces the wrong row without it). The second reads base file positions only for the keys a write locates: a delete of a key with several rows in a file group writes a positional delete for each row, and a prepped upsert of such a key fails with the key, file group and positions, since it would otherwise insert the record once per row. The config docs note the limits below.

The last commit carries the fix of #21047 (a join of list-based data could drop pairs from a parallel stream), which the position lookup here relies on on the Java engine. It drops out on a rebase once #21047 merges.

Limitations a user will hit:

  • Two writers that update the same key of such a table concurrently can leave a duplicate live copy (never supported).
  • On tables with a record key, a direct Spark write client prepped upsert or delete in the mode uses the caller's location as is, so a stale location leaves a duplicate. Spark SQL on such tables does not write prepped records in the mode.
  • A prepped upsert of a key that has more than one row in a base file fails instead of updating it.

Impact

Spark SQL UPDATE, DELETE and MERGE INTO now work on merge-on-read tables without a record key in the update-as-delete-insert mode. The combined-record location change also applies outside the mode, as described above.

Risk Level

medium: it opens prepped writes to the split in this mode and changes which location a combined record keeps. Verified with the Spark SQL and Spark client suites of the mode; the change is gated by the mode and merge-on-read except for the combined-record location.

Documentation Update

The descriptions of hoodie.write.updates.as.deletes.and.inserts and hoodie.spark.sql.optimized.writes.enable are updated.

Contributor's checklist

  • Read through contributor's guide
  • Enough context is provided in the sections above
  • Adequate tests were added if applicable

@yihua
yihua requested a review from wombatu-kun December 1, 2026 10:21
@github-actions github-actions Bot added the size:L PR with lines of changes in (300, 1000] label Dec 1, 2026
@wombatu-kun
wombatu-kun self-requested a review December 1, 2026 11:43
+ "supported on merge-on-read tables. Compaction is not scheduled while an earlier delta commit is pending, "
+ " disabled. Prepped inserts and partial updates are not supported on merge-on-read tables, and prepped "
+ "upserts and deletes only with the Spark write client, which splits each located update of a prepped upsert "
+ "into a positional delete and an insert. On a table without a primary key only Spark SQL UPDATE, DELETE and "

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Spark write client also accepts direct prepped upserts and deletes on keyless tables when the caller supplies the locations. Should the docs include that path, or should keyless prepped writes require the Spark SQL write flag?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. The config doc now says the prepped upserts and deletes of the Spark write client also update tables without a primary key, and that the caveat about stale locations applies to them too.

@Override
public HoodieData<HoodieRecord<T>> preppedUpdatesAsDeletesAndInserts(HoodieData<HoodieRecord<T>> positionedRecords,
HoodieTable<T, HoodieData<HoodieRecord<T>>, HoodieData<HoodieKey>, HoodieData<WriteStatus>> table) {
return splitLocatedRecords(positionedRecords, table);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A keyless UPDATE that moves a row between partitions loses its original row ID on formats that preserve row identity. Carry the source row's format metadata onto the insert in the destination partition.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed for UPDATE: the new version of a moved row now takes the format meta column values read at the source row, with a regression test that checks the row ID survives the move. A keyless MERGE INTO move writes the new version with a new generated record key, so it cannot be paired the same way; I filed #21044 for it.

TypedProperties updateProps = TypedProperties.copy(props);
updateProps.setProperty(HoodiePayloadProps.PAYLOAD_IS_UPDATE_RECORD_FOR_MOR, "true");
return ReplacedFormatMetaColumns.set(keepLatestLocations(taggedRecords, parallelism), table)
return ReplacedFormatMetaColumns.set(locatedRecords, table)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A matched keyless MERGE INTO reaches ReplacedFormatMetaColumns.set without a row position and fails on tables with format metadata columns. Resolve the matched records' positions before splitting them and add a regression test using a format that declares metadata columns.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. On formats with meta columns the matched records get their base file positions before they are split, and a regression test with the test table format covers a matched keyless MERGE INTO.

withTempDir { tmp =>
val tableName = generateTableName
val tablePath = s"${tmp.getCanonicalPath}/$tableName"
withSQLConf(HoodieWriteConfig.WRITE_UPDATES_AS_DELETES_AND_INSERTS.key() -> "true") {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The duplicate-source MERGE test only runs with update-as-delete-insert enabled, although the location fix also changes ordinary merge-on-read writes. Run this case with the setting both true and false and keep the assertion that exactly one updated target row remains.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. The test now runs with the setting true and false.

@codecov-commenter

codecov-commenter commented Dec 1, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.00000% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.05%. Comparing base (48090a5) to head (38dc87c).

Files with missing lines Patch % Lines
...ache/hudi/table/action/commit/BaseWriteHelper.java 78.57% 2 Missing and 1 partial ⚠️
...n/java/org/apache/hudi/index/HoodieIndexUtils.java 93.93% 1 Missing and 1 partial ⚠️
...n/scala/org/apache/hudi/HoodieSparkSqlWriter.scala 33.33% 0 Missing and 2 partials ⚠️
.../org/apache/hudi/client/BaseHoodieWriteClient.java 75.00% 0 Missing and 1 partial ⚠️
...table/action/commit/ReplacedFormatMetaColumns.java 95.83% 0 Missing and 1 partial ⚠️
...t/SparkUpsertPreppedDeltaCommitActionExecutor.java 66.66% 0 Missing and 1 partial ⚠️
...g/apache/spark/sql/hudi/ProvidesHoodieConfig.scala 66.66% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master   #21020      +/-   ##
============================================
- Coverage     81.07%   81.05%   -0.02%     
- Complexity    37323    37361      +38     
============================================
  Files          2609     2609              
  Lines        151031   151120      +89     
  Branches      18974    18988      +14     
============================================
+ Hits         122447   122491      +44     
- Misses        20065    20097      +32     
- Partials       8519     8532      +13     
Components Coverage Δ
hudi-common 84.39% <100.00%> (-0.03%) ⬇️
hudi-client 84.34% <91.11%> (+<0.01%) ⬆️
hudi-flink 86.33% <ø> (-0.02%) ⬇️
hudi-spark-datasource 74.61% <57.14%> (-0.10%) ⬇️
hudi-utilities 79.29% <ø> (-0.01%) ⬇️
hudi-cli 70.92% <ø> (ø)
hudi-hadoop 71.27% <ø> (+0.01%) ⬆️
hudi-sync 77.37% <ø> (+0.06%) ⬆️
hudi-io 81.66% <ø> (ø)
hudi-timeline-service 82.60% <ø> (+0.07%) ⬆️
hudi-cloud 81.01% <ø> (ø)
hudi-kafka-connect 58.40% <ø> (ø)
Flag Coverage Δ
common-and-other-modules 44.56% <6.00%> (-0.03%) ⬇️
flink-integration-tests 40.71% <3.48%> (-0.03%) ⬇️
hadoop-mr-java-client 45.31% <45.34%> (+0.02%) ⬆️
integration-tests 14.45% <6.00%> (-0.01%) ⬇️
spark-client-hadoop-common 38.22% <74.00%> (+0.03%) ⬆️
spark-java-tests 52.82% <69.00%> (-0.01%) ⬇️
spark-scala-tests 53.98% <75.00%> (+0.16%) ⬆️
utilities 42.19% <9.00%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...java/org/apache/hudi/config/HoodieWriteConfig.java 92.59% <ø> (ø)
...he/hudi/table/action/commit/HoodieWriteHelper.java 91.42% <100.00%> (+0.42%) ⬆️
...va/org/apache/hudi/client/SparkRDDWriteClient.java 91.47% <100.00%> (+0.04%) ⬆️
...e/action/commit/BaseSparkCommitActionExecutor.java 88.77% <100.00%> (+0.11%) ⬆️
...rg/apache/hudi/common/data/HoodieListPairData.java 90.58% <100.00%> (+1.95%) ⬆️
...main/scala/org/apache/hudi/DataSourceOptions.scala 95.56% <100.00%> (ø)
.../org/apache/hudi/client/BaseHoodieWriteClient.java 84.16% <75.00%> (-0.14%) ⬇️
...table/action/commit/ReplacedFormatMetaColumns.java 86.39% <95.83%> (+1.84%) ⬆️
...t/SparkUpsertPreppedDeltaCommitActionExecutor.java 85.71% <66.66%> (-14.29%) ⬇️
...g/apache/spark/sql/hudi/ProvidesHoodieConfig.scala 85.18% <66.66%> (-0.27%) ⬇️
... and 3 more

... and 26 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

yihua added 7 commits December 2, 2026 11:50
…-on-read tables without a record key in the update-as-delete-insert mode

The mode refused these statements on a table without hoodie.table.recordkey.fields, since a write that looks
rows up by record key cannot locate rows whose keys the writer generates. The statements do not need the lookup:
UPDATE and DELETE read the target rows with their meta columns, and MERGE INTO joins the source with them, so
every record already carries the record key, the partition path and the file group of the row it replaces.

UPDATE and DELETE on such a table now always write prepped records in the mode, whatever
hoodie.spark.sql.optimized.writes.enable says, and a table with a primary key keeps the index lookup. The Spark
write client accepts a prepped upsert and a prepped delete in the mode. The commit action executor reads the
base file position of each prepped record, as it does for the records a global index or the record index
locates, and the prepped upsert executor then splits each located update into a positional delete plus an
insert that keeps its record key, through the split the write helper applies to the records the index locates,
including its format meta column values. MERGE INTO already wrote through the upsert that splits its matched
records, so it is no longer refused. Prepped inserts, and every prepped write of the Java client, stay refused.

When an upsert combines the records of a key before tagging, as a MERGE INTO on a table with ordering fields
does, the combined record now keeps the location of the record it is built from. The index of a MERGE INTO on a
table without a record key tags nothing, so the combined record was written as an insert next to the row it
should have replaced.

Still refused on such a table: an upsert or delete by record key, an INSERT INTO that upserts, and a datasource
upsert or delete of a frame that carries the meta columns, which can hold a row twice or a row another write has
replaced since it was read.

The SQL test runs UPDATE, DELETE and MERGE INTO (matched update and delete, not-matched insert, extra conjuncts
in the merge condition, insert only, more than one source row for a target row) on partitioned and
non-partitioned tables with the simple and the record index, across a compaction, moves records across
partitions with MERGE INTO and UPDATE, and checks the rows and that every log block is a delete block whose
positions hold the deleted rows. The Spark client test writes a prepped upsert and delete in the mode.
…e-as-delete-insert write mode

The write reads the base file position of each located record that has none, as it does for the records of a
prepped upsert or delete, of a global index and of the record index, from the latest base file of the file group.
The location kept the instant the record was located with, which for a prepped record is the commit time its meta
fields hold. After a compaction that instant names the older base file of the file group until the cleaner removes
it, and the format meta columns of the replaced record were read from the position in that older file, where
another row is, so the new version of the record took the row lineage of that row. The location now takes the
instant of the base file the position comes from, as the simple index gives it.

A key with more than one row in the base file of a file group made the join with the positions write the record
once per row. The write now fails with the key and the file group.

The documentation of hoodie.write.updates.as.deletes.and.inserts says that a concurrent writer that moves a row
between the read and the write of a Spark SQL statement on a table without a primary key can leave two live copies
of the row, and that a prepped upsert or delete of the Spark write client takes the location the caller gives as
is.

The row lineage test compacts a merge-on-read table whose first rows were deleted, keeps the older base file, and
updates a moved record with a prepped upsert located at its insert instant, as a Spark SQL UPDATE of a table
without a primary key locates it, checking that its new version keeps its own row ID. The Spark client test writes
a prepped delete of a key with two rows in one base file and checks that it fails.
…ase file positions

The write read the positions of every row of each base file it locates records in and failed when any key had
more than one row in a file group, so a key that an insert left twice in a base file failed every later write of
the file group in the update-as-delete-insert write mode, including one that deletes the key. The rows of the base
files are now joined with the keys the write locates before they are grouped by key, so only a write of such a key
sees its rows. A delete of the key gets a positional delete for each of its rows, which is what a delete by key
means, and a prepped upsert of the key fails, since its record is split into a delete and an insert after its
positions are set and would insert the new version once per row.

The documentation of hoodie.write.updates.as.deletes.and.inserts says that Spark SQL UPDATE, DELETE and MERGE INTO
on a table with a primary key look the records up through the index in the mode, so the caveat on stale locations
of prepped writes does not apply to them.

The Spark client test inserts a key twice into one base file, upserts another key of the file group, checks that a
prepped upsert of the key fails, and deletes the key with a prepped delete, reading back the table without both
of its rows.
… a table without a primary key in the update-as-delete-insert write mode
…of a table without a primary key with the update-as-delete-insert write mode enabled and disabled
…out a primary key matches before it splits their updates on a table format with format meta columns
…able without a primary key moves to another partition
@yihua
yihua force-pushed the keyless-mor-sql-dml-delete-insert branch from d1fa7f6 to c52856a Compare December 2, 2026 19:50
@yihua
yihua enabled auto-merge December 2, 2026 19:50
@yihua
yihua added this pull request to the merge queue Dec 2, 2026
yihua added a commit to yihua/hudi that referenced this pull request Dec 2, 2026
…and MERGE INTO on merge-on-read tables without a record key in the update-as-delete-insert mode at c52856a
@yihua
yihua removed this pull request from the merge queue due to a manual request Dec 2, 2026
… to a shared list from a parallel stream

HoodieListPairData#join added the joined pairs to an ArrayList from forEach on the stream of the data, which is parallel
for eager data, so concurrent adds lost pairs or failed with an ArrayIndexOutOfBoundsException. The join now collects the
pairs from the stream, which also keeps them in the order of the data.
@hudi-bot

hudi-bot commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

CI report:

Bot commands @hudi-bot supports the following commands:
  • @hudi-bot run azure re-run the last Azure build

@yihua
yihua added this pull request to the merge queue Dec 2, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Dec 3, 2026
@yihua
yihua added this pull request to the merge queue Dec 3, 2026
Merged via the queue into apache:master with commit 3d0f8be Dec 3, 2026
23 of 26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L PR with lines of changes in (300, 1000]

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants