Repository navigation
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
Conversation
| + "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 " |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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") { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Done. The test now runs with the setting true and false.
Codecov Report❌ Patch coverage is 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
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
…-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
d1fa7f6 to
c52856a
Compare
…and MERGE INTO on merge-on-read tables without a record key in the update-as-delete-insert mode at c52856a
… 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.
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:
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.insertsandhoodie.spark.sql.optimized.writes.enableare updated.Contributor's checklist