Skip to content

MOD-8187 - fix non twa aggregation - #2053

Merged
TalBarYakar merged 6 commits into
masterfrom
tal.ba/bug/fix_non_twa_aggregation
Jun 10, 2026
Merged

TalBarYakar merged 6 commits into
masterfrom
tal.ba/bug/fix_non_twa_aggregation

Conversation

@TalBarYakar

@TalBarYakar TalBarYakar commented Jun 8, 2026 •

Copy link
Copy Markdown
Contributor

Note

Medium Risk
Touches core aggregation iterator empty-bucket logic used by TS.RANGE/REVRANGE; behavior changes for non-TWA EMPTY queries but is heavily covered by new flow tests.

Overview
Fixes MOD-8187: with EMPTY, prefix/suffix/whole-range gap filling now runs for every aggregator, not only TWA. The old TWA_EMPTY_RANGE gate is replaced by self->empty everywhere; iterator flags are renamed to handled_empty_prefix / handled_empty_suffix.

LAST (LOCF) empty gaps use shared seed_locf_for_empty_gap() (forward + reverse): reverse always seeds from the older neighbor; forward seeds only when LastValueIsUnseeded() (new helper). Seeding runs once in fillEmptyBuckets() before TWA vs default fill so TWA+LAST multi-agg gets correct LAST values.

fillEmptyBuckets() gains check_edge_gaps: prefix/suffix/whole-range callers still drop buckets with no neighbor on one side (all aggs); interior gap callers skip the two series scans. Adds assert(self->empty) on entry.

Flow tests cover prefix/suffix/whole-range, aligned buckets, TWA+LAST multi-agg, and interior vs edge gap behavior.

Reviewed by Cursor Bugbot for commit 47ce097. Bugbot is set up for automated code reviews on this repo. Configure here.

@TalBarYakar TalBarYakar changed the title Tal.ba/bug/fix non twa aggregation MOD-8187 - fix non twa aggregation Jun 10, 2026
@TalBarYakar TalBarYakar self-assigned this Jun 10, 2026

@gabsow gabsow left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the MOD-8187 follow-up. The core change is correct and well-scoped: degating the prefix/suffix/whole-range emission from TWA_EMPTY_RANGE to plain self->empty, moving seed_locf_for_empty_gap() ahead of the TWA-vs-default split, and adding check_edge_gaps. I verified the LastValueIsUnseeded NaN sentinel against aggLast (createContext inits value=NAN, isValueValid=nonNaNValueValid, LastValueReset preserves value), that all 7 fillEmptyBuckets call sites pass the right check_edge_gaps value, that the forward optimization preserves prior hot-path behavior, and that no stale TWA_EMPTY_RANGE/handled_twa_empty_* references remain. No correctness issues found; CI is green. A few non-blocking nits and one additive test suggestion inline.

Comment thread src/filter_iterator.c
* without seeding, so we must seed here first. */
// NOTE: seed AFTER the in-place realloc/memmove above. That block moves samples buffers but
// not the aggregation contexts; keep seeding here so it stays decoupled from the compaction.
seed_locf_for_empty_gap(self, first_bucket_ts, reversed);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optimization (low priority): on the edge paths, seed_locf_for_empty_gap() runs another twa_get_samples_from_left() even though the check_edge_gaps block above (~L720) already fetched the left neighbor (sample_before). This only fires on the one-shot prefix/suffix/whole-range fills (not the interior loop), so it isn't hot — but the already-fetched older sample could be threaded in to save one full series scan. The comment here already acknowledges the duplication; just flagging it as a possible cleanup.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch — agreed it's redundant on the edge paths. Since it's only the one-shot fills and not the interior loop, I'll leave it for now and open a follow-up to thread the already-fetched sample_before into the seed. Tracked.

Comment thread src/filter_iterator.c
* All callers must gate on this condition. Without this assertion, a future caller that
* violates this precondition would silently produce wrong results (dropped edge buckets)
* rather than failing loudly. The assertion catches such bugs immediately. */
assert(self->empty);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: assert() compiles out under NDEBUG, so the "fail loudly rather than silently drop edge buckets" guarantee only holds in debug builds — in a release build a future mis-gated caller would silently produce wrong results. This is consistent with other plain asserts in this file (e.g. in agg_iter_max_emit_opening_sample), so fine to keep; if you want the invariant enforced in production you'd need RedisModule_Assert.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right — it's debug-only by design, matching the existing assert style in this file. I'll keep it as-is for consistency. If we decide the invariant must hold in release builds, we can switch these over to RedisModule_Assert in a separate cleanup.

Comment thread src/filter_iterator.c Outdated
last_bucket,
self,
is_reversed,
true, // edge gap (whole-range / suffix)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tiny label nit: this is the !handled_empty_prefix (whole-range) branch, but the inline label is identical to the genuine suffix branch below (L872). // edge gap (whole-range / prefix) would read more accurately. No functional impact.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment thread src/filter_iterator.c
last_bucket,
self,
is_reversed,
true, // edge gap (suffix)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pre-existing, but worth a glance now that this path is degated from TWA-only to every aggregator: the reverse suffix at L1445 computes first_bucket - aggregationTimeDelta without the max(0, …) clamp that the equivalent agg_iter_on_empty_chunk suffix uses (L862). It's guarded by the first_bucket <= last_bucket check so an underflow is likely unreachable, but extending to all agg types widens the input space — could you confirm the guard fully prevents wrap-around near ts 0?

# follows) -> LOCF 100. Buckets 8,9 (before the first-ever sample) are dropped.
case4 = [[10, '100'], [11, '100'], [12, '100']]
assert rng(8, 12, 'last') == case4
assert revrng(8, 12, 'last') == list(reversed(case4))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additive coverage suggestion: every prefix case here is dropped because the first in-range sample is also the first-ever sample, so the forward agg_iter_apply_empty_prefix path that emits LOCF prefix buckets (a prefix gap that has an older out-of-range neighbor) isn't directly exercised. The whole-range case 3 covers the same seed_locf_for_empty_gap code via a different entry point, so risk is low — but e.g. samples at 10, 25, 35 with TS.RANGE a 20 40 ALIGN 0 AGGREGATION last 1 EMPTY (bucket 20 should carry sample-10's value via LOCF rather than be dropped) would close the gap.

@sonarqubecloud

Copy link
Copy Markdown

@gabsow gabsow left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving — all five points from my earlier review are now resolved or acknowledged.

Fixed (commits fix remarks + formatting):

  • Comment-label nit corrected to edge gap (whole-range / prefix); the genuine suffix branch label left intact.
  • Reverse suffix underflow: max(0, …) clamp added in agg_iter_finalize, matching the agg_iter_on_empty_chunk suffix path.
  • Added the forward prefix-emit test (prefix gap whose older neighbor is out of range → LOCF), exercising agg_iter_apply_empty_prefix directly.

Acknowledged (no change needed):

  • Redundant left-neighbor scan on the one-shot prefix/suffix/whole-range fills — deferred to a follow-up; not on the interior hot path.
  • assert(self->empty) being debug-only — kept for consistency with the existing plain asserts in this file.

Core fix verified during review: the LastValueIsUnseeded NaN sentinel is sound against aggLast (createContext inits NAN, isValueValid=nonNaNValueValid, LastValueReset preserves value); all 7 fillEmptyBuckets call sites pass the correct check_edge_gaps; the forward seeding optimization preserves prior hot-path behavior; no stale TWA_EMPTY_RANGE/handled_twa_empty_* references remain. Lint is green — only the in-progress build/test matrix needs to finish before merge.

@TalBarYakar
TalBarYakar merged commit 1cb4f67 into master Jun 10, 2026
54 of 55 checks passed
@TalBarYakar
TalBarYakar deleted the tal.ba/bug/fix_non_twa_aggregation branch June 10, 2026 19:48
YaacovHazan pushed a commit to redis/redis that referenced this pull request Jun 14, 2026
## Summary

Bumps the pinned `MODULE_VERSION` for **RedisTimeSeries** from `v8.8.0`
to `v8.9.80`. RedisBloom and RedisJSON are unchanged.

| Module          | From    | To       |
|-----------------|---------|----------|
| RedisTimeSeries | v8.8.0  | v8.9.80  |

> **Note:** `v8.9.80` is the **8.10 LTS branch cut** (targets Redis
8.10) and has diverged from `v8.8.0`. The `v8.8.0...v8.9.80` ancestry
diff contains 37 commits, but **7 are backports already shipped in
`v8.8.0`** (verified with `git cherry`, see below); the **30 genuinely
new** changes are listed here.

## Module changes ([v8.8.0 →
v8.9.80](RedisTimeSeries/RedisTimeSeries@v8.8.0...v8.9.80))

- MOD-14124 — m commands acl
([#1913](RedisTimeSeries/RedisTimeSeries#1913))
- MOD-14250 — missing artifacts logs fix
([#1932](RedisTimeSeries/RedisTimeSeries#1932))
- MOD-14289 — Increase LibMR execution timeout for Valgrind builds
([#1933](RedisTimeSeries/RedisTimeSeries#1933))
- MOD-12726 — unique snapshot name + new output params for nighly event
([#1937](RedisTimeSeries/RedisTimeSeries#1937))
- MOD-14674 — oss refresh list
([#1935](RedisTimeSeries/RedisTimeSeries#1935))
- nightly build, upload snapshot artifact
([#1954](RedisTimeSeries/RedisTimeSeries#1954))
- MOD-14902 — existence check to prevent Timeseries crash when Redis is
not aligned with new redis-core changes
([#1940](RedisTimeSeries/RedisTimeSeries#1940))
- MOD-14850 — Bump libmr
([#1962](RedisTimeSeries/RedisTimeSeries#1962))
- MOD-14715 — add apt retries/timeouts to Debian/Ubuntu Dockerfiles
([#1965](RedisTimeSeries/RedisTimeSeries#1965))
- MOD-15262 — Align TimeSeries with the RedisModule_GetUserUserName API
changes
([#1975](RedisTimeSeries/RedisTimeSeries#1975))
- RED-180951 RED-180027 — fixing bugs and improving the code
([#2004](RedisTimeSeries/RedisTimeSeries#2004))
- MOD-14239 — Ignore known init-rdbchannel migration timeout under
sanitizer
([#1979](RedisTimeSeries/RedisTimeSeries#1979))
- MOD-15105 — make one generic getUser function
([#1981](RedisTimeSeries/RedisTimeSeries#1981))
- MOD-15741 MOD-14091 — add dont-cache command tip to non-cacheable read
commands
([#2031](RedisTimeSeries/RedisTimeSeries#2031))
- MOD-15731 — modules modernization alignment
([#2027](RedisTimeSeries/RedisTimeSeries#2027))
- MOD-15728 — Tal.ba/feat/blocking get
([#2028](RedisTimeSeries/RedisTimeSeries#2028))
- MOD-8187 — fix the aggregation bugs (fix reverse iterator)
([#2036](RedisTimeSeries/RedisTimeSeries#2036))
- MOD-15749 — Link to new version of LibMR with short-form CLUSTERSET
and add tests
([#2034](RedisTimeSeries/RedisTimeSeries#2034))
- Fix REDISMODULE_MAIN typo + drop common.h forward-decl workarounds
([#2044](RedisTimeSeries/RedisTimeSeries#2044))
- CI: switch flow-linux arm64 runner to standard ubuntu-24.04-arm
([#2048](RedisTimeSeries/RedisTimeSeries#2048))
- MOD-15999 — Centralize Redis ref into a single source-of-truth file
for CI
([#2045](RedisTimeSeries/RedisTimeSeries#2045))
- CI: trigger OSS cluster (multi-shard) benchmarks on demand
([#2050](RedisTimeSeries/RedisTimeSeries#2050))
- MOD-15899 — oom try calloc
([#2051](RedisTimeSeries/RedisTimeSeries#2051))
- MOD-8187 — fix non twa aggregation
([#2053](RedisTimeSeries/RedisTimeSeries#2053))
- MOD-16224 — Fix filter-blind neighbor lookups in EMPTY aggregation
([#2056](RedisTimeSeries/RedisTimeSeries#2056))
- MOD-15893 — Nrange new command
([#2052](RedisTimeSeries/RedisTimeSeries#2052))
- MOD-16160 — update blocking get syntax
([#2054](RedisTimeSeries/RedisTimeSeries#2054))
- MOD-16160 — Fix client-side cache tips for TS.BGET and
TS.NRANGE/TS.NREVRANGE
([#2057](RedisTimeSeries/RedisTimeSeries#2057))
- Cut 8.10 LTS branch: bump version to 8.9.80, target Redis 8.10
([27484f7](RedisTimeSeries/RedisTimeSeries@27484f7))
- Fix event-nightly CI on 8.10: read Redis ref from dedicated redis_ref
field
([#2058](RedisTimeSeries/RedisTimeSeries#2058))

<details>
<summary>7 commits excluded — already shipped in v8.8.0 as
backports</summary>

These show up in the `v8.8.0...v8.9.80` ancestry diff with different
SHAs, but `git cherry` confirms their patch content is already in
`v8.8.0`: they were backported to the 8.8 release branch (the "in 8.8
via" PR below), so they are **not** new in this bump.

- MOD-9320 — Multiple aggregators single command
([#1916](RedisTimeSeries/RedisTimeSeries#1916))
→ in 8.8 via
[#1921](RedisTimeSeries/RedisTimeSeries#1921)
- MOD-9320 — rename AGGREGATION token in m commands
([#1926](RedisTimeSeries/RedisTimeSeries#1926))
→ in 8.8 via
[#1927](RedisTimeSeries/RedisTimeSeries#1927)
- MOD-13142 — Allow changing number of threads while pool hasn't started
([#1928](RedisTimeSeries/RedisTimeSeries#1928))
→ in 8.8 via
[#1934](RedisTimeSeries/RedisTimeSeries#1934)
- MOD-14439 — Detect cluster topology changes during a multi-shard
command
([#1930](RedisTimeSeries/RedisTimeSeries#1930))
→ in 8.8 under the same PR
- ci: skip Event CI when PR only changes src/version.h
([#1984](RedisTimeSeries/RedisTimeSeries#1984))
→ in 8.8 via
[#1986](RedisTimeSeries/RedisTimeSeries#1986)
- MOD-15020 — remove bionic os support
([#1983](RedisTimeSeries/RedisTimeSeries#1983))
→ in 8.8 via
[#1996](RedisTimeSeries/RedisTimeSeries#1996)
- MOD-14420 — fix count reducers return wrong NaN
([#2013](RedisTimeSeries/RedisTimeSeries#2013))
→ in 8.8 via
[#2016](RedisTimeSeries/RedisTimeSeries#2016)

</details>

## Test plan
- [ ] CI passes for RedisTimeSeries at the new pinned version
- [ ] `make all` builds RedisTimeSeries cleanly against `unstable`
- [ ] Module tests run under `make test`

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
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.

2 participants