Skip to content

feat(bindings): spanUnion(span | spanset) → spanset aggregate#53

Closed
estebanzimanyi wants to merge 1 commit intofeat/aggregate-fn-tand-tor-tmin-tmax-tsumfrom
feat/aggregate-fn-spanunion
Closed

feat(bindings): spanUnion(span | spanset) → spanset aggregate#53
estebanzimanyi wants to merge 1 commit intofeat/aggregate-fn-tand-tor-tmin-tmax-tsumfrom
feat/aggregate-fn-spanunion

Conversation

@estebanzimanyi
Copy link
Copy Markdown
Member

Summary

Adds `spanUnion` aggregate — merges a column of spans (or spansets) into a single canonical spanset. 10 overloads:

Input Output
`spanUnion()` `` for `{int, bigint, float, date, tstz}span` (5)
`spanUnion()` `` same 5 types

Closes the architectural gap documented in the parity manifest at `test/sql/parity/015_span_aggfuncs.test` (PR #21).

```sql
SELECT spanUnion(s) FROM (VALUES (intspan '[1, 5)'), (intspan '[3, 8)')) tt(s);
-- {[1, 8)} -- overlap merged

SELECT spanUnion(s) FROM (VALUES (intspan '[1, 3)'), (intspan '[3, 5)')) tt(s);
-- {[1, 5)} -- adjacent merged

SELECT spanUnion(s) FROM (VALUES (intspan '[1, 5)'), (intspan '[10, 12)')) tt(s);
-- {[1, 5), [10, 12)} -- disjoint preserved
```

Implementation

New `SpansetUnionState` holds a heap-allocated `SpanSet *`. Same destructor pattern as the skiplist-backed aggregates (PR #50, PR #52).

Step What it does
`Operation` Copies input blob, calls MEOS `span_union_transfn` (or `spanset_union_transfn` when `INPUT_IS_SPANSET=true`), reassigns `state.spanset` to whatever the transfn returns — the transfn may return a new larger spanset and free the old state when it grows.
`Combine` Calls `spanset_union_transfn(target, source)` to merge two states.
`Finalize` Calls `spanset_union_finalfn` which compacts and frees the state. We null `state.spanset` after to keep the destructor from double-freeing.

The transfn dispatch is templated on `bool INPUT_IS_SPANSET` so the same `SpanUnionFunction` handles both Span and SpanSet inputs.

Stacked on

Test plan

  • Distinct spans → preserved as multiple elements
  • Overlapping spans → merged
  • Adjacent spans → merged
  • Spanset inputs (multiple element merges)
  • All 5 base types (int / bigint / float / date / tstz)
  • NULL handling: mixed NULL + value → returns the spanset; all-NULL → returns NULL
  • No segfaults (destructor + finalfn double-free guard)

Adds spanUnion aggregate that merges a column of spans (or spansets)
into a single canonical spanset. 10 overloads:

  spanUnion(<span>)    -> <spanset>   for {int, bigint, float, date, tstz}span
  spanUnion(<spanset>) -> <spanset>   same 5 types

Closes the architectural gap documented in the parity manifest at
test/sql/parity/015_span_aggfuncs.test (PR #21).

Implementation: new SpansetUnionState holds a heap-allocated
SpanSet*. Same destructor pattern as the skiplist-backed aggregates.
Operation copies the input blob, calls span_union_transfn or
spanset_union_transfn, and reassigns state.spanset to the returned
pointer (the transfn may return a new spanset and free the old
state when it grows). Combine merges via spanset_union_transfn
on (target, source). Finalize calls spanset_union_finalfn which
compacts and frees the state — null state.spanset after to avoid
double-free.

The transfn dispatch is templated on a bool INPUT_IS_SPANSET so
the same SpanUnionFunction handles both Span and SpanSet inputs.

Verified for distinct / overlapping / adjacent spans, nested
spansets, floatspan / tstzspan, and NULL handling (mid-run + all-
NULL → returns NULL).
@estebanzimanyi
Copy link
Copy Markdown
Member Author

Consolidated into #60 (aggregate cluster squash). The full chain of aggregate work is now reviewable as a single PR; this branch's commits are preserved in #60's history. Closing to minimize the review queue.

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