Repository navigation
fix(delete): part_integrity="cascade" includes the master of every part row it reaches - #1788
Open
dimitri-yatsenko wants to merge 3 commits into
Open
dimitri-yatsenko wants to merge 3 commits into
dimitri-yatsenko wants to merge 3 commits into
Conversation
With part_integrity="cascade", the part-to-master step ran once per (part, master) pair, at the first foreign key that reached the part. Part rows reached later through another parent were deleted while their masters stayed. The cascade is now planned to a fixed point. Each round builds the downward cascade in one topological pass from the seed plus the master rows added so far, then lifts the reached rows of every part to its master rows through the foreign key's attribute map and adds the master rows the plan does not yet reach as literal key conditions. Planning stops when no master gains rows. If a master row added in an earlier round is still not reached (its key values select no row when sent back, as with a float primary key), the planner raises DataJointError before anything is deleted. The row-level part-integrity check now also runs in cascade mode as a final assertion, and matches part rows to master rows through the part's reference to its master rather than by shared attribute names. Closes #1785
Follow-ups to the re-planning cascade for part_integrity="cascade": - Master keys stay key dicts in the plan, compiled once by restrict(), so a string key containing % (or a quote or underscore) selects its row. - New master keys are checked once, in batches, to select their rows when sent back. Keys that do not (floating-point values, or on PostgreSQL strings containing backslashes) are refused before anything is deleted, with a message naming the condition. - A part is lifted only from the rows it receives through foreign keys other than its own reference to its master, and the lifted master keys are compared with the seeded ones in Python. Planning no longer sends the master literal inside every lift and final check: Diagram.cascade over 6000 masters plans in about 0.2 s instead of 5 s on PostgreSQL. - A part that does not reference its master is not lifted and is skipped by the final assertion, so its reached rows are deleted as before. - In cascade mode the final assertion reports a failure as an internal planning error. The enforce error counts master rows through the part's reference to its master, which may rename attributes. - Table.delete documents what "cascade" deletes. Tests cover awkward string keys, the refused backslash key, bulk planning queries, a part without a master reference, the enforce count, the internal-error message, and chains of masters linked through parts. The time limit uses a worker thread, so it also works on Windows.
dimitri-yatsenko
requested review from
MilagrosMarin,
lum-agilyti and
ttngu207
December 2, 2026 22:25
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #1785. Addresses item 2 of #1767.
For v2.4. Merges after the v2.3.7 tag.
What was wrong
With
part_integrity="cascade",Diagram.cascadelifted a part's restriction to its master once per (part, master) pair: at the first edge that reached the part. Part rows reached later, through another parent or another reference to the same table, were deleted while their masters stayed. A master that grew after its own edges had been propagated also missed its other parts and dependents.What changes
Diagram.cascadewithpart_integrity="cascade"now plans in rounds until nothing changes:delete()removes leaves first, so a subquery on the part would find nothing by the time the master is deleted.part_integrity="cascade"deletes masters that have no matching part rows #1591). Only rows that reached the part through an edge other than its own reference to the master are lifted, so no lift query carries the master keys.Termination and refusal:
DataJointErrornaming the master and part before anything is deleted.Code removed: the multi-pass loop,
visited_part_master_pairs,_propagate_part_to_master, and the save/restore of intermediate restrictions._apply_propagation_rule_upwardstays fortrace().Other behavior:
"ignore"and"enforce"give the same results as before, with two exceptions, both for parts whose reference to the master renames attributes (-> master.proj(a2="a")):-> master) is not lifted in cascade mode; its reached rows are deleted, as before.%, quotes,_, dates and UUIDs are selected correctly. Before, string keys containing%or quotes, and composite keys with date or UUID parts, could fail to select their masters, which were then kept.The
Table.deletedocstring's"cascade"bullet now says it deletes the master row of every deleted part row, with all of that master's parts and dependents.Tests
tests/integration/test_cascade_part_second_parent.py, 15 tests, each on MySQL and PostgreSQL:%, quotes and backslashes;-> master;Every case that applies checks that
counts()equals the rowsdelete()removes. Most of these tests fail on master. Unit tests and the full integration suite pass on both backends (3261 passed).Planning cost
Diagram.cascadeplanning time at 6,000 masters:The extra time is the batched key check. A separate problem that this PR does not change: with about 15,000 masters,
counts()on PostgreSQL does not finish, on master and here. Tracked in #1787.Not in this PR
From #1767: walking only the owning reference (item 5, with #1620), and planning inside the delete's transaction (item 6). The cascade specification in datajoint-docs ("Cascade flow" and the "Single FK path" limitation) needs a matching update, in a separate docs PR.