Skip to content

docs(query-algebra): join key follows determination; full operator table; lineage and 2.3.6 notes - #320

Open
dimitri-yatsenko wants to merge 6 commits into
mainfrom
docs/query-algebra-key-rules
Open

dimitri-yatsenko wants to merge 6 commits into
mainfrom
docs/query-algebra-key-rules

Conversation

@dimitri-yatsenko

@dimitri-yatsenko dimitri-yatsenko commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

This PR corrects the query-algebra pages where they contradicted the library or each other. It touches the explanation, reference, how-to, and spec pages, and the queries tutorial.

Primary key of a join

  • Said: the primary key of A * B is the union of both primary keys, and Session * Trial is keyed by both.

  • Says: A determines B when every primary-key attribute of B is an attribute of A.

    • If A → B, the join is keyed by A.
    • If only B → A, it is keyed by B.
    • If neither determines the other, it is keyed by the union, and each row is a pair.

    Session * Trial is keyed by Trial, because Trial determines Session. A table of the three cases replaces the old statement on the explanation, operator-reference, and primary-key spec pages.

  • Why: the pages and the tutorial gave the direction backward.

  • Two specs gave Session.join(Trial, left=True) as a valid left join, which the library rejects. Those examples are corrected.

  • Premise of the abstract examples (primary-keys.md): a shared attribute is a copy of one declared primary-key attribute. Either one operand declares it and the other inherits it, or both inherit it from a common parent. The previous premise excluded the page's own Session * Trial example.

The operator set

  • Restriction and anti-restriction are named separately. A & c keeps the entities of A that match c, and A - c keeps those that do not. The headings "Include" and "Exclude" become "Restriction" and "Anti-restriction".
  • Extension (.extend()) is stated as a left join that keeps every entity of the left operand, with NULL where nothing matches, keyed by the left operand. It equals the join when every entity has a match.
  • Aggregation examples count a primary-key attribute of the right operand (COUNT(trial_num)). COUNT(*) returns 1 for an entity with no match, because it counts the row the underlying left join pads with NULLs.
  • Union requires the same primary key and no secondary attribute names in common. An entity in both operands carries the attributes of both. Projected operands combine keys.
  • Universal sets (dj.U) get their own section. It covers both forms (restricted by a query expression, and the left operand of an aggregation), and when to aggregate over a table instead. The queries tutorial now counts sessions per experimenter with Experimenter.aggr(Session, ...), and the pages note that grouping by dj.U over a nullable reference also returns one NULL group.
  • The operator tables now list anti-restriction, extension, union, and both forms of the universal set, each with its primary key and entity type.

Lineage and semantic matching

  • A secondary attribute declared in a table has no lineage and is never matched.
  • The lineage-error section quotes the messages the library raises.
  • Restriction by a query expression matches on every shared attribute.

Version notes

Two behaviors change in 2.3.7. Each gets a version-changed note, "Changed in 2.3.7", on the pages that describe it:

  • Extension: through 2.3.6, a restriction on the right operand is applied after the left join, so it drops the unmatched entities of the left operand. The same holds for aggregation, and for union of operands that have secondary attributes.
  • Semantic matching: through 2.3.6, a namesake without lineage raises the "lineage missing on one side" message.

Also

  • The footnote citing arXiv:1807.11104 gives the paper's title, "DataJoint: A Simpler Relational Data Model".
  • Dependency arrows on the primary-key spec page that meant something other than "determines" are reworded.

Overlap

#305 also edits semantic-matching.md. #307 also edits query-algebra.md (explanation and spec), operators.md, query-data.md, and the semantic-matching spec. Whichever of these merges later needs a rebase.

Checks

mkdocs build --strict, scripts/check_links.py on the built site, scripts/check_markdown_lists.py, and codespell pass.

…neage

Session * Trial is keyed by Trial, because Trial determines Session; the
pages and the queries tutorial stated the direction backward, and two specs
gave Session.join(Trial, left=True) as a valid left join, which the library
rejects. The operator tables now list anti-restriction, extension, union,
and both forms of the universal set with their keys and entity types.

A secondary attribute declared in a table has no lineage and is never
matched; the lineage-error section quotes the messages the library raises.
Restriction by a query expression matches on every shared attribute.
Aggregation examples count a key attribute of the right operand, since
count(*) counts the padded row of an unmatched entity. Union combines
operands with the same primary key and disjoint secondary attributes, and
an entity in both carries the attributes of both.
- Extension: through 2.3.6 a restriction on the right operand drops the
  unmatched entities of the left operand (also aggregation and union of
  operands with secondary attributes); version note on the explanation
  page and in the queries tutorial.
- Semantic matching: through 2.3.6 a namesake without lineage raises the
  "lineage missing on one side" message; version notes on the
  explanation and spec pages.
- Primary keys: the abstract examples state that shared attributes are
  inherited through a foreign key; the dependency arrows that meant
  something other than "determines" are reworded.
- Explanation page: the restriction section heading names both
  operators.
A shared attribute in the examples is a copy of one declared primary key
attribute: declared in one operand and inherited by the other, or
inherited by both from a common parent. Session declares session_id and
only Trial inherits it, so the premise as written excluded the page's own
Session * Trial example.
…enter in the tutorial

Relabel the three version notes "Changed in 2.3.7" and date the extension
change in the queries tutorial. Replace the tutorial's dj.U('experimenter_id')
aggregation with Experimenter.aggr, which the guidance in this PR recommends;
the stored output is unchanged apart from the key column's tooltip. State that
dj.U grouping over a nullable reference also returns one NULL group.

@MilagrosMarin MilagrosMarin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks @dimitri-yatsenko, this is a careful cleanup, and the determination framing is much easier to follow than "functional dependencies."

I checked the claims against datajoint-python master and v2.3.6 and ran them on MySQL 8.0:

  • Session * Trial and Trial * Session are both keyed by Trial's primary key; Session.join(Trial, left=True) raises with exactly the message quoted in primary-keys.md; Trial.join(Session, left=True) is keyed by Trial.
  • COUNT(*) returns 1 for an unmatched entity, and counting an attribute shared with the left operand also returns 1, as spec 5.7 now says.
  • Union, dj.U (both forms, including the NULL group on a nullable reference), and Experimenter.aggr(Session, ...) behave as described.
  • On master, extend, aggr and union with a restricted right operand keep the unmatched rows; on 2.3.6 they drop them, as the "Changed in 2.3.7" notes say. Both namesake-without-lineage messages match condition.py exactly, and the 2.3.6 message in the note is verbatim.
  • mkdocs build --strict passes with the same warnings as main.

A few points on timing and overlap:

  • Both changes (datajoint/datajoint-python#1655 and datajoint/datajoint-python#1642) are on master but not in a release yet. Is the plan to merge this after 2.3.7 is tagged, or now, with the version notes covering the gap?
  • Two open library PRs in the v2.4 milestone would change statements added here: datajoint/datajoint-python#1774 makes COUNT(*) return 0 for unmatched rows (the "returns 1" sentence is on about six pages, including the tutorial), and datajoint/datajoint-python#1710 makes dj.U refuse nullable attributes (the NULL-group sentence is on three pages). Worth tracking so those passages get a 2.4 note.
  • #305 documents the same lineage-message change in the same two files, labels it "Changed in 2.4", and conflicts with this PR. It looks superseded; should it be closed or rebased onto this?
  • Tutorial nit: the new comment says "with 0 for one who has no sessions", but the tutorial data has no such experimenter (the output is alice 3, bob 2), so the reader never sees the 0. Adding an experimenter without sessions, or dropping that clause, would fix it. Also, "a valid primary key determined by which operand determines the other" reads a little circular.
  • Small one: the PR description says "behaviours"; "behaviors" would match the rest of the docs.

Once the 2.3.7 timing is settled, the corrected Session * Trial direction and the full operator table will make these pages much easier to trust.

@dimitri-yatsenko

Copy link
Copy Markdown
Member Author

Thanks for the careful check against master and v2.3.6. Answers in order:

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.

2 participants