feat(bindings): tCount(temporal) aggregate — first skiplist-backed aggregate#50
Closed
estebanzimanyi wants to merge 1 commit intofeat/aggregate-fn-extent-tnumberfrom
Closed
feat(bindings): tCount(temporal) aggregate — first skiplist-backed aggregate#50estebanzimanyi wants to merge 1 commit intofeat/aggregate-fn-extent-tnumberfrom
estebanzimanyi wants to merge 1 commit intofeat/aggregate-fn-extent-tnumberfrom
Conversation
…gregate
Adds tCount() temporal-count aggregate over all 4 temporal base types:
tint, tbool, tfloat, ttext. Returns a tint where each instant carries
the count of input temporal values valid at that instant.
Implementation pattern (new in MobilityDuck — establishes the scaffolding
for tand/tor/tmin/tmax/tsum):
- TCountState holds a heap-allocated MEOS SkipList* (the skiplist
itself lives outside the inline aggregate state buffer).
- Lifetime is managed by registering the aggregate with
AggregateFunction::UnaryAggregateDestructor, which installs a
destructor that calls our TCountFunction::Destroy → skiplist_free.
- Operation copies the input Temporal blob (varlena) into a heap
allocation and calls temporal_tcount_transfn, which allocates a
fresh SkipList on the first call and extends it in place after.
- Combine merges two skiplist states via temporal_skiplist_splice
with a sum merge function. MEOS's datum_sum_int32 isn't exported
so we provide a local equivalent (mduck_datum_sum_int32).
- Finalize calls temporal_tagg_finalfn which produces a Temporal*
AND frees the skiplist; we null state.skiplist after the call so
the destructor doesn't double-free.
Verified for:
* single-row, multi-distinct, dup-instant (sums to 2), overlapping
sequences (count rises in overlap region)
* tint, tfloat, tbool, ttext
* NULL handling: ignored mid-run, all-NULL → returns NULL
This was referenced Apr 29, 2026
Member
Author
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.
Summary
Adds `tCount()` aggregate over all 4 temporal base types (`tint`, `tbool`, `tfloat`, `ttext`). Returns a `tint` where each instant carries the count of input temporal values valid at that instant.
```sql
-- distinct timestamps, count = 1 at each instant
SELECT tCount(t) FROM (VALUES (tint '1@2000-01-01'), (tint '2@2000-01-02')) tt(t);
-- {1@2000-01-01, 1@2000-01-02}
-- duplicate timestamp, count sums to 2
SELECT tCount(t) FROM (VALUES (tint '1@2000-01-01'), (tint '5@2000-01-01')) tt(t);
-- {2@2000-01-01}
-- overlapping sequences, count = 2 in overlap region
SELECT tCount(t) FROM (VALUES (tint '[1@2000-01-01, 1@2000-01-05]'),
(tint '[1@2000-01-03, 1@2000-01-07]')) tt(t);
-- {[1@2000-01-01, 2@2000-01-03, 2@2000-01-05], (1@2000-01-05, 1@2000-01-07]}
```
Implementation pattern (new for MobilityDuck)
This is the first skiplist-backed aggregate in MobilityDuck. The pattern established here generalises to `tand`, `tor`, `tmin`, `tmax`, `tsum`, `tavg`, `twAvg`, etc. — only the underlying `*_transfn` and merge function differ.
Stacked on
Test plan