Chapter 22 · Bounded join reference
Count the join output
One event can create more than one answer.
Prerequisite Join changing streams.
Explain how widening the time bound or adding a receipt changes the number of matches for the same orders.
The question
Order x has two payment receipts with matching IDs. A join produces two rows. If the dashboard treats both as one paid order each, its count doubles even though the join followed its specified rule.
A stream-stream join matches records from two histories. Here, each payment’s order ID must match the order’s ID. The payment timestamp must be at or after the order time and no more than horizon seconds later. This authored model checks every order–receipt pair in two fixed lists. An online join also needs rules for tracking progress and removing old records.
- Matching receipt p1Joined row x / p1One output match.
- Matching receipt p2Joined row x / p2Another output for the same input.
Conceptual diagram. This is the labelled interval-join reference. Retained history and matching rules determine output multiplicity.
Predict the result
The model supplies order x at 0 seconds and order y at 4 seconds. The editable payments list supplies the receipt ID, associated order ID and timestamp of each payment.
How many output rows should order x produce? Does y’s receipt at 20 match an order at 4 with horizon 10?
Run the experiment
The editor contains the published example. Select Run with the code unchanged.
Python prepares in the background while you read. First use can download about 84 MiB. Your code runs only when you select Run; each run starts with clean experiment state.
A saved draft is open. Select Restore code to see the published example. Recover draft brings back your draft.
Ctrl+Enter or ⌘+Enter runs the code.
Edits are saved in this browser when storage is available.
Technical error details
Your result
Console output
Troubleshooting
If Python stops responding, restart the environment and try again. Your code and last completed result are kept.
View the published result
The course build ran the published code to generate this output.
{
"matches": [
{
"order": "x",
"receipt": "p1"
},
{
"order": "x",
"receipt": "p2"
}
],
"comparisons": 6,
"retained_records": 5,
"note": "Bounded nested-loop reference; no online eviction or watermark is implemented."
}Read the result
In the console, inspect matches to see which receipts match each order. comparisons counts every pair the model checks, including pairs it rejects.
Change the experiment
Replace horizon=10 with horizon=20 in the editor. Select Run again. Compare matches and comparisons. Which receipt is new? Keep horizon=20 and add {"id": "x", "time": 10, "receipt": "p4"}, on its own line before the closing bracket of payments. Select Run again. How many receipts now match order x, and how many pairs are checked?
Runtime, source code and downloads
Runtime and reset details explain what stays ready and what is isolated on each run. Download Python packages the published helpers with the code in your editor.
Browse the sources for Bytewax windows and authored models, Kafka log and group exercises, live Kafka input, checkpoint recovery and processing patterns.
Bound the search and the retained history
An interval bound constrains which event-time pairs can match. To safely discard an old order online, progress on the payment input must establish that no further accepted match can occur under the late-data policy. Processing-time age alone answers a different question.
Choose the business cardinality
If each order should have one final payment, decide whether repeated receipts are duplicates, installments or conflicting updates. Deduplication by receipt ID differs from choosing one payment per order. Validate that rule before aggregating the join’s output.
Work, state and recovery costs
Where used, n counts input events, k counts retained distinct keys, r counts overlapping windows per event and z counts output matches. Bounds assume the stated algorithm. Collecting, sorting and displaying results adds work.
| Question | Mechanism and boundary |
|---|---|
| Work | For n orders and m receipts, the helper makes n×m comparisons and writes z matches. A suitable hash algorithm has a different expected bound. |
| State | O(n+m+z) input and output records here. An unbounded online join also needs an eviction policy. |
| Movement | Both lists are in local memory. A keyed distributed join would additionally move records to shared key owners. |
| Progress | The nested loops compare every order with every payment in the input lists. |
| Recovery | Keep both input histories and the matching rule to reproduce a bounded result. An ongoing join needs a way to restore both source positions and unmatched state. |
Think it through
Can a hash join always make a many-to-many join O(n+m) total work?
Show hints
Hint 1
Hashing helps locate matching groups.
Hint 2
Every matching pair may still need to be emitted.
Hint 3
Include output cardinality z in the cost expression.
Show explanation
No. A useful expected bound is O(n+m+z) for suitable hash-based matching, with additional qualifications for collisions, time indexing and state maintenance. If every left row matches every right row, z is n×m. No lookup structure makes emitting that result linear in input size.
ReferencesFunction documentation and helper source
References open in a new tab; course helpers are documented separately from library APIs.
Interval join contract for comparison
interval_join is an explicit nested-loop course implementation. Use the framework guide to compare time bounds and retention requirements.
Course helper implementation
These are functions written for this course, not additional library APIs. Inspect their parameters, operator calls and authored policies. The source shown is generated from the same Python files packaged with Run and Download Python.
Experiment scope
This is an executed nested-loop reference, not a distributed hash join. It does not evict unmatched state or emit outer-join revisions.
Primary reference →