Skip to content

fix(delete): part_integrity="cascade" includes the master of every part row it reaches - #1788

Open
dimitri-yatsenko wants to merge 3 commits into
masterfrom
fix/1785-part-second-parent
Open

dimitri-yatsenko wants to merge 3 commits into
masterfrom
fix/1785-part-second-parent

Conversation

@dimitri-yatsenko

Copy link
Copy Markdown
Member

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.cascade lifted 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.cascade with part_integrity="cascade" now plans in rounds until nothing changes:

  1. Plan: one forward topological pass from the seed plus any master rows collected so far. Those master rows are kept as literal keys, because delete() removes leaves first, so a subquery on the part would find nothing by the time the master is deleted.
  2. Lift: each reached part's rows are mapped to master rows along the foreign-key path, through each reference's attribute map. Rows are never matched on names the two tables happen to share, and the part's condition is never copied upward (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.
  3. Master rows not yet in the plan are added, and the next round re-plans. The rounds stop when no master gains rows.

Termination and refusal:

  • Every round that continues adds a master table or a master row not seen before, so planning ends.
  • Newly added master keys are checked against the database in batches. If keys read from the database select no row when sent back (floating-point keys; on PostgreSQL, strings containing backslashes), planning raises DataJointError naming the master and part before anything is deleted.
  • After planning, the per-row master–part check also runs in cascade mode, as a final assertion. If it ever fires, the message says it is an internal planning failure and nothing was 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_upward stays for trace().

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")):
    • enforce now matches part rows to master rows through that reference;
    • the error's count of master rows is now correct.
  • A part with no path to its master (no -> master) is not lifted in cascade mode; its reached rows are deleted, as before.
  • Master keys with %, 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.delete docstring'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:

Every case that applies checks that counts() equals the rows delete() removes. Most of these tests fail on master. Unit tests and the full integration suite pass on both backends (3261 passed).

Planning cost

Diagram.cascade planning time at 6,000 masters:

Version MySQL PostgreSQL
master 0.11 s 0.12 s
this PR 0.22 s 0.23 s

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.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: part_integrity="cascade" does not include the master of a part reached through a second parent

1 participant