Skip to content

DDIR: fragment heads, set tests and evaluated relations (spike) - #1115

Merged
frankmcsherry merged 9 commits into
TimelyDataflow:master-nextfrom
frankmcsherry:ddir-fragment-tests-heads
Nov 24, 2026
Merged

frankmcsherry merged 9 commits into
TimelyDataflow:master-nextfrom
frankmcsherry:ddir-fragment-tests-heads

Conversation

@frankmcsherry

@frankmcsherry frankmcsherry commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

A spike, as a draft: four additions to DDIR's join fragments (#1085), one commit each, then a fix to the antijoin expansion, a commit that can be dropped, one rewrite in the structural pass, and two fixes from a review.
Like #1085, the additions are surface forms that the parser expands into plain DDP as it reads it.
No runtime operators or corgi code change.

Commit Source lines
Fragment heads, |- (key, value) +60 −20
Fragment antijoins, !b(...) +116 −21
Fragment semijoins, ?b(...) +38 −23
Evaluated relations, @f(...) and !@f(...) +273 −74
Bind an antijoin's rows to a name instead of copying them +23 −4
Require a head on every fragment +13 −19
Drop a fragment test's key set over a unit distinct +40 −6
Give an antijoin's bound rows a name the source does not write +12 −4
A fragment test's _ matches any row, not a sum of rows +13 −7

Source lines count interactive/src; the rest is tests and LANGUAGE.md.

The forms

let large = orders(o, (c, amount)) * customers((c), (region))
          * !shipped(o, _) * ?flagged(_, (c))
          * @gt(amount, 100)
          |- ((region), (o, amount));
  • Heads, |- (key, value). A fragment ends with a pair of corgi expressions over its variables. The head goes into the last join's projection, so there is no separate map after the last join. |- is the turnstile: premises, then conclusion.
  • !b(...): no row of b matches. The atom binds nothing new: each variable in it is bound to its left, and the other positions are _ or literals. It may test value fields, and _ may appear in its key.
  • ?b(...): some row of b matches. It follows the same rules. A plain atom stays a join whose weights multiply.
  • @f(...): relations by evaluation. These are the comparisons eq, ne, lt, le, gt and ge, and add, sub and mul, which compute their third argument from the first two. Which arguments are bound to the left decides the mode at parse time. !@f(...) negates a test. @ keeps these names apart from collection names.
  • Requiring the head (sixth commit). This removes the old ((vars), ()) output. It is separate so that it can be dropped. The seventh commit applies without it; the eighth and ninth touch lines it changed, so dropping it now needs small conflict fixes.

Meaning and cost

! and ? look for a row of b whose count is not zero and that agrees with the pattern's variables, literals and variants; _ agrees with any value.
So the size and sign of b's counts do not matter, only whether they are zero.
Both build one set D per pattern:

  • When the matched fields are b's whole key and its value is untested, D is b | reduce(limit 1, … ((), 1) enlist), which reads b's own arrangement.
  • Otherwise, D is b filtered by the pattern's literals and repeated variables, keyed by the matched fields with what the wildcards match as the value, and reduced the same way.
    Rows that differ only under _ stay apart, so their counts are never added together (the ninth commit; before it, they were projected away and the counts summed, so opposite counts cancelled).

The matched positions are numbered in pattern order, so equal patterns spell D identically even under different variable names, and structural deduplication shares it.
A semijoin joins the rows so far against D, and an antijoin subtracts that join from the rows.

Each test costs one reduce for its set, plus an arrangement of the rows so far by the matched fields.
When the right side is already a set by key, that reduce is redundant.

The seventh commit removes it in one case.
When b is a distinct of () values, it already holds each key once with diff 1, so D and b hold the same rows.
The structural pass then points the test's join at b and drops D, as it drops an arrange of an arrangement.
This needs no condition on D's readers.
The fragment builds D's lambda from one function, and distinct lowers to one fixed lambda, so the pass recognises both by equality.

Other right sides that are sets by key still get the reduce.
Examples are a min, which has values, or a distinct behind a filter or a map that keeps the key.

x(k, v) * !y(k, _) |- (k, v) expands to exactly x - (x | join(D, …)), reusing x's arrangement.

An antijoin reads the rows so far twice.
As first written, the expansion copied that expression, so k antijoins in a row built 2^k copies before structural deduplication: 16 in a row parsed to 9.3 MB and took 3.3 s to plan.
The fifth commit binds those rows to a fresh let (fragment_rows0, …) just before the statement, so both reads name it: 16 in a row now parse to 3.6 KB, and 32 to 7.3 KB, each in under 0.01 s.
The eighth commit makes those names skip every name the source writes, so they cannot collide with the program's own bindings or capture a reference to one.

@ tests are filters, placed at the first point their arguments are bound, as literal tests are.
Computed fields are added where written.
No @ form needs an arrangement or a distinct, since these relations are sets by definition.

Evidence

  • cargo test --release -p interactive passes at each of the four feature commits, at the sixth commit with 365 tests passing and 9 ignored, at the seventh with 366, at the eighth with 367, and at the ninth with 368, each with 9 ignored. The fifth commit was tested only together with the sixth.

  • Seven existing programs (kcore twice) were rewritten with the new forms, with answers matching the originals through the fixture paths (one to four workers, serializing channels, existing sessions). The answers were checked again at the seventh and ninth commits. Counts are at the ninth commit; before the seventh, upsert had 3 reduces.

    Program Change Joins Arrangements Reduces Maps
    access Absence in the sharing loop 3 → 3 7 → 7 2 → 3 7 → 7
    upsert Absence on touched keys 1 → 1 3 → 3 2 → 2 3 → 3
    left_join Head projection and absence 2 → 2 2 → 2 0 → 1 2 → 2
    kcore Presence against a unit-valued set, and a degree test 1 → 1 5 → 5 2 → 3 5 → 5
    kcore Plain join against the same set, and a degree test 1 → 1 5 → 5 2 → 2 5 → 5
    scc Equality filters next to joins 6 → 6 11 → 11 2 → 2 7 → 7
    pair_keys Arithmetic result in the join head 1 → 1 3 → 3 0 → 0 2 → 2
    lambda_slots Comparison on a single atom 0 → 0 1 → 1 1 → 1 7 → 7

    The extra reduces are the !/? sets described above.
    upsert's test reads a distinct, so the seventh commit removes its set.

  • A randomized check compared fragments with a naive evaluator. It used 12,500 generated fragments with plain joins, both tests, comparisons, arithmetic tests, computed fields and heads, over inputs with duplicate rows, followed by retractions. Each ran at the root and in a loop over a feedback binding, with all passes and with none. That made 50,000 runs and 100,000 snapshots, with no mismatches. At the seventh commit it ran again with 2,500 fragments, and at the ninth with all 12,500, with no mismatches. Its generator writes no distinct, so the seventh commit's rewrite never fires there. Its reference evaluator already reads a test as "some matching row exists", but its data never has cancelling counts, so it cannot tell the ninth commit's meaning from the old one. The checker is not committed.

  • A sceptical review of the first seven commits wrote probe programs for signed and duplicate rows, sharing, heads, @ modes, loops and the seventh commit's rewrite. It found the two wrong answers that the eighth and ninth commits fix. The fixes were checked with its probes, with all passes and with none.

Open questions

  • Other redundant sets. The seventh commit handles a distinct of () values. A right side with values, such as a min, needs every reader of D to ignore the value, since the value's shape changes. Sets reached through filters or maps that keep the key, or through antijoins, need reasoning about what those steps preserve. Inputs that are sets only by how they are written would need declared facts.
  • A set reading of a binding atom. A distinct between a scan and a join still breaks a chain. ? cannot express this, because it binds nothing new.
  • The direct-antijoin shortcut recognises the head by its text ((k, v) naming the first atom's variables). Any other head takes the general path.
  • Further @ relations: reverse modes (such as add with its result bound), relations that produce several rows (member, range), and user-defined relations.
  • Other review findings, not yet reproduced here. @eq(v, "") is rejected because the argument check loses the empty string's element type. An empty string pattern is accepted against a list of floats. The least integer and fractional literals cannot be written in a pattern. An @ test on the first atom runs after the first join, as the first atom's literal tests do since DDIR: join fragments, a(x, y) * b(y, z), an experimental surface form (spike) #1085, which LANGUAGE.md's "at the first point their arguments are bound" does not say. A chain of computed fields carries every earlier variable, so the parsed size grows quadratically. The string pattern comes from DDIR: join fragments, a(x, y) * b(y, z), an experimental surface form (spike) #1085's pattern reader, and the two literals from the DDP lexer.

Not intended to land as is.

Implemented by GPT-6.2 Sol (Codex), except the fifth and the seventh to ninth commits, by Claude Code. Reviewed by GPT-6.2 Astra (Codex) and Claude Code; tested and opened by Claude Code.

🤖 Generated with Claude Code

frankmcsherry and others added 9 commits November 23, 2026 15:03
Capture the head as corgi source and bind its variables from the fragment.
Emit the requested row in the final join projection when possible.

Co-Authored-By: GPT-6.2 Sol (Codex) <noreply@openai.com>
Build a presence set from the tested fields and subtract the matched rows.
Read the tested collection's key arrangement when the whole key is matched.
Reuse the first atom's arrangement when its key is already the lookup key.

Co-Authored-By: GPT-6.2 Sol (Codex) <noreply@openai.com>
Keep rows that join against the same presence set used by absence tests.
Keep the original bag weights of the left input.
Use the same right-side expression for both tests so the plan can share it.

Co-Authored-By: GPT-6.2 Sol (Codex) <noreply@openai.com>
Choose a relation's mode from the variables bound to its left.
Run tests at the first checkpoint that can read their arguments.
Compute arithmetic results where written and carry them to later atoms.

Co-Authored-By: GPT-6.2 Sol (Codex) <noreply@openai.com>
An antijoin reads the rows so far twice, once to keep and once to join.
The expansion copied that expression, so a chain of k antijoins built 2^k copies before deduplication.
With 16 in a row, the parsed program was 9.3 MB and took 3.3 s to plan.
The rows now go to a fresh `let` just before the statement that uses them, and both reads name it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Ask for a head when a fragment ends without one.
Give the existing fragment cases an explicit output row.

Co-Authored-By: GPT-6.2 Sol (Codex) <noreply@openai.com>
A `!b(...)` or `?b(...)` test of `b`'s whole key builds `b | reduce(limit 1, ...)`, one `()` per key.
When `b` is a `distinct`, it already holds each key once with diff 1, so that reduce is the identity.
The structural pass now redirects its readers to `b`, as it does for an arrange of an arrangement.
The fragment builds its key set from one function, so the pass recognises it by equality.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The rows a fragment's antijoin reads twice are bound to a fresh `let fragment_rowsN`.
The counter kept those names apart from each other, but not from the program's own names.
A program that bound `fragment_rows0` was rejected, a reference to an outer `fragment_rows0` read the fragment's rows instead, and an unbound `fragment_rows0` resolved.
The parser now skips every number whose name the source writes anywhere.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A test of some of `b`'s fields projected `b` to those fields and took a `distinct`, so rows that differed only under `_` summed their counts.
Opposite counts cancelled, and large ones wrapped, so `?b((x, ()), _)` and `!b((x, _), _)` could both hold for the same rows.
The test now keys the matching rows by the matched fields, keeps what the wildcards match as the value, and takes the same `limit 1` set as a whole-key test.
A key is present when any matching row has a count that is not zero.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@frankmcsherry
frankmcsherry marked this pull request as ready for review November 24, 2026 03:35
@frankmcsherry
frankmcsherry merged commit 0c451e5 into TimelyDataflow:master-next Nov 24, 2026
7 checks passed
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.

1 participant