fix: span_union / + on span × span returns spanset, not garbage#45
Merged
nhungoc1508 merged 1 commit intomainfrom Apr 30, 2026
Merged
fix: span_union / + on span × span returns spanset, not garbage#45nhungoc1508 merged 1 commit intomainfrom
nhungoc1508 merged 1 commit intomainfrom
Conversation
Two bugs in the wrapper for span × span union:
1. Union_span_span called sizeof(*ret) where ret is SpanSet*. SpanSet
is a varlena type with a Span elems[1] flexible array — sizeof
only returns the header size, so memcpy stored an incomplete blob
that decoded as the canonical empty span (t, t).
Fixed by switching to spanset_mem_size(ret) which is the canonical
helper used elsewhere in spanset_functions.cpp.
2. The 10 ScalarFunction registrations for `+` and `span_union` over
span × span declared the return type as SpanType, but MEOS's
union_span_span returns SpanSet (since the union of two disjoint
spans cannot be expressed as a single span). Updated all 10
registrations across {int, bigint, float, date, tstz}span to
return the matching spanset type.
After the fix:
floatspan '[1, 3]' + floatspan '[1, 3]' = {[1, 3]}
floatspan '[1, 3]' + floatspan '(3, 5]' = {[1, 5]}
floatspan '[1, 1]' + floatspan '[3, 4]' = {[1, 1], [3, 4]}
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
Fixes two bugs in the `Union_span_span` wrapper that made `+` and `span_union` between two spans return the canonical empty span `(t, t)` regardless of input.
Bug 1 — wrong byte count
```cpp
SpanSet *ret = union_span_span(span1, span2);
size_t span_size = sizeof(*ret); // <-- returns sizeof header only
uint8_t span_buffer = (uint8_t) malloc(span_size);
memcpy(span_buffer, ret, span_size);
```
`SpanSet` is a varlena type with a flexible `Span elems[1]` member — `sizeof` returns only the fixed-header size, so the variable-length payload (the actual spans) was never copied. The decoded blob then read as the canonical empty span.
Fixed by switching to `spanset_mem_size(ret)` (the canonical size helper used elsewhere in `spanset_functions.cpp`).
Bug 2 — wrong return type
The 10 `ScalarFunction` registrations for `+` and `span_union` over span × span declared return type `SpanType`, but MEOS's `union_span_span` returns `SpanSet` — the union of two disjoint spans cannot be expressed as a single span. Updated all 10 registrations across `{int, bigint, float, date, tstz}span` to return the matching spanset type.
Verification
```sql
-- before fix:
SELECT floatspan '[1, 3]' + floatspan '[1, 3]'; -- (t, t)
SELECT floatspan '[1, 3]' + floatspan '(3, 5]'; -- (t, t)
SELECT floatspan '[1, 1]' + floatspan '[3, 4]'; -- (t, t)
-- after fix:
SELECT floatspan '[1, 3]' + floatspan '[1, 3]'; -- {[1, 3]}
SELECT floatspan '[1, 3]' + floatspan '(3, 5]'; -- {[1, 5]}
SELECT floatspan '[1, 1]' + floatspan '[3, 4]'; -- {[1, 1], [3, 4]}
```
Surfaced by parity work in PR #17 (005_span_ops.test). The skip block there can be removed in a follow-up once this lands.
Test plan