Repository navigation
Description
Summary
ContinuousAggregateRefresher.incrementalRefresh is the whole refresh loop for a continuous
aggregate. Its correctness rests on one number, the watermark: rows whose bucket is at or after it
are deleted and recomputed, rows before it are left alone. The watermark is advanced by reading the
bucket column out of each row the defining query produced and converting it to epoch milliseconds
with a private toEpochMs.
That converter handles Date, Long and Number, and falls through to return 0 for anything
else. The bucket column is produced by ts.timeBucket(), and ts.timeBucket() returns a
LocalDateTime - on BOTH paths that can produce it: the SQL function itself (changed from
java.util.Date by the fix for #7610) and AggregateFromTimeSeriesStep's pushdown (changed by the
fix for #4385). A LocalDateTime is neither a Date nor a Number, so every row converts to 0,
maxBucketTs never exceeds the watermark, and setWatermarkTs is never called.
The watermark is therefore stuck at its initial 0 for the life of the aggregate, and 0 is also the
sentinel the rest of the method reads as "no watermark yet":
if (watermark > 0)is false, so theDELETE FROM <backing> WHERE <bucket> >= ?never runs;buildFilteredQuery(ca, 0)returns the defining query unfiltered, so the whole source range is
re-aggregated;- every row of that result is written with
database.newDocument(backingTypeName), a fresh record.
So each refresh appends a complete second copy of the aggregate. A refresh is scheduled from
SaveElementStep.scheduleContinuousAggregateRefresh as an after-commit callback on every
transaction that writes to the source TimeSeries type, so the backing type grows by the full
aggregate on every such commit, and SELECT FROM <aggregate> returns each bucket once per refresh
that has happened since the bucket first appeared. The status stays VALID and nothing is logged.
Where (HEAD 9a88be7)
engine/src/main/java/com/arcadedb/schema/ContinuousAggregateRefresher.java:245
private static long toEpochMs(final Object value) {
if (value instanceof Date d)
return d.getTime();
if (value instanceof Long l)
return l;
if (value instanceof Number n)
return n.longValue();
return 0;
}against what the bucket column actually holds,
engine/src/main/java/com/arcadedb/function/sql/time/SQLFunctionTimeBucket.java:82
// Issue #7610: expose the bucket as a LocalDateTime, not a java.util.Date. ...
return LocalDateTime.ofInstant(Instant.ofEpochMilli(bucketStart), ZoneOffset.UTC);and the same value on the pushdown path,
engine/src/main/java/com/arcadedb/query/sql/executor/AggregateFromTimeSeriesStep.java:108
// Issue #4385: expose the bucket timestamp as a LocalDateTime (the engine's standard
// DATETIME representation) rather than a java.util.Date, ...
row.setProperty(timeBucketAlias,
DateUtils.dateTime(context.getDatabase(), bucketTs, ChronoUnit.MILLIS, LocalDateTime.class, ChronoUnit.MILLIS));the read that silently becomes 0,
engine/src/main/java/com/arcadedb/schema/ContinuousAggregateRefresher.java:75
final Object bucketVal = result.getProperty(bucketColumn);
if (bucketVal != null) {
final long bucketMs = toEpochMs(bucketVal);
if (bucketMs > maxBucketTs)
maxBucketTs = bucketMs;
}and the two consumers of the watermark that then do nothing,
engine/src/main/java/com/arcadedb/schema/ContinuousAggregateRefresher.java:55
// Delete rows in the current (possibly incomplete) bucket and all newer buckets
if (watermark > 0)
database.command("sql", "DELETE FROM `" + backingTypeName + "` WHERE `" + bucketColumn + "` >= ?",
new Date(watermark));
// Build the filtered query: append WHERE clause with watermark filter on the source timestamp
final String filteredQuery = buildFilteredQuery(ca, watermark);Repro
Embedded database at HEAD. One TimeSeries type and one continuous aggregate, exactly the shape the
in-tree tests use; three further single-row inserts, each in its own transaction, each firing the
after-commit refresh:
CREATE TIMESERIES TYPE SensorReading TIMESTAMP ts TAGS (sensor_id STRING) FIELDS (temperature DOUBLE)
CA query: SELECT sensor_id, ts.timeBucket('1h', ts) AS hour, avg(temperature) AS avg_temp
FROM SensorReading GROUP BY sensor_id, hour
after create: watermark=0 rows=1
after insert 1: watermark=0 rows=3
after insert 2: watermark=0 rows=6
after insert 3: watermark=0 rows=10
distinct source buckets expected = 4 (hours 0,1,2,3)
row 0: hour=1970-01-01T00:00 (java.time.LocalDateTime) sensor=A avg=21.0
row 1: hour=1970-01-01T00:00 (java.time.LocalDateTime) sensor=A avg=21.0
row 2: hour=1970-01-01T00:00 (java.time.LocalDateTime) sensor=A avg=21.0
row 3: hour=1970-01-01T00:00 (java.time.LocalDateTime) sensor=A avg=21.0
row 4: hour=1970-01-01T01:00 (java.time.LocalDateTime) sensor=A avg=26.0
row 5: hour=1970-01-01T01:00 (java.time.LocalDateTime) sensor=A avg=26.0
row 6: hour=1970-01-01T01:00 (java.time.LocalDateTime) sensor=A avg=26.0
row 7: hour=1970-01-01T02:00 (java.time.LocalDateTime) sensor=A avg=27.0
row 8: hour=1970-01-01T02:00 (java.time.LocalDateTime) sensor=A avg=27.0
row 9: hour=1970-01-01T03:00 (java.time.LocalDateTime) sensor=A avg=28.0
Four buckets, ten rows: 1, 3, 6, 10 is the triangular growth of appending the whole aggregate each
time. The printed class of the bucket value is java.time.LocalDateTime, which is the converter's
fall-through case.
The causal link was verified by re-running the identical program with ONE line added to
toEpochMs - a LocalDateTime branch converting at ZoneOffset.UTC - and nothing else changed:
after create: watermark=0 rows=1
after insert 1: watermark=3600000 rows=3
after insert 2: watermark=7200000 rows=4
after insert 3: watermark=10800000 rows=5
The watermark now tracks the newest bucket and the row count stops growing per bucket. (The one
residual duplicate at hour 0 is the sentinel noted under "Suggested fix": a legitimate watermark of
exactly 0 - the epoch bucket - is indistinguishable from "not set yet", so the delete is skipped for
it once. Any timestamp after 1970 does not hit that.)
Why the existing tests pass: ContinuousAggregateTest.incrementalRefreshOnInsert asserts
ca.getWatermarkTs()).isGreaterThanOrEqualTo(initialWatermark) and watermarkAdvances asserts
wm2).isGreaterThanOrEqualTo(wm1) - both hold for a watermark that never moves - and the row
assertions are isNotEmpty(), which holds for any number of duplicates.
Suggested fix
Give toEpochMs the same coverage the producing side has: SQLFunctionTimeBucket.toEpochMs already
handles Long, Date, Instant, LocalDateTime, Number and String, and
SaveElementStep.toEpochMs handles four of those. Three near-copies of the same converter in three
packages is what let the return-type change in #7610 reach only some of them; the durable fix is one
shared converter (DateUtils is where the rest of this engine's date normalisation lives) that all
three call, and which throws on an unrecognised type rather than returning 0 - a silent 0 here is
indistinguishable from the epoch and is exactly what turned a type change into a data defect.
Separately, stop overloading 0 as "no watermark": hold it as a Long/OptionalLong or a separate
watermarkSet flag, so an aggregate legitimately anchored at the epoch still gets its delete.
A regression test belongs beside ContinuousAggregateTest: after N commits into the source type,
assert countType(<aggregate>) equals the number of distinct (tag, bucket) pairs - not
isNotEmpty() - and assert the watermark is strictly greater after a commit that adds a newer
bucket, not isGreaterThanOrEqualTo.
Scope / confidence
Reproduced end to end at HEAD on an embedded database through plain SQL and the public schema API,
and the fix verified by a one-line change to the converter with everything else held constant. Rated
high: a continuous aggregate is a materialised answer that callers read instead of the source, and
it silently returns duplicated rows - so count, sum and avg computed over it are wrong - while
the backing type grows without bound, one full copy of the aggregate per commit to the source type.
It affects every continuous aggregate, since ts.timeBucket() with an alias is mandatory at
creation time and both of its evaluation paths return LocalDateTime.
Activity
github-actions commentedon Sep 22, 2026
Merged into #8181, which now tracks this together with the other related issues in engine. Closing here as a duplicate - reopen this issue if the grouping is wrong.
fix(#8173): decide a JSON operand's kind from the operator, not from …
fix(#8152): read a numeric-string moment at its own epoch precision, …
fix(#8152): install the new watermark only after the refresh transact…
test(#8152): count the aggregate's rows with a scan, not with the cac…
fix(#8167,#8152,#8156): three defects found by CodeRabbit
fix(#8152): keep the clean-rebuild flag until it is persisted, and re…
Merge pull request #8198 from ArcadeData/fix/issues-8157-8181-8180
lvca commentedon Sep 22, 2026
Fixed in #8198, released in 26.10.1, tracked under #8181.
The refresher could not read the value
ts.timeBucket()returns, so a continuous aggregate's watermark never moved off zero - and zero also meant "never refreshed", so the cleanup before each recompute was skipped. Every refresh therefore appended a complete second copy of the aggregate.Existing aggregates repair themselves: the first refresh after upgrading rebuilds the backing type from the source once, clearing the accumulated duplicates.
❤️ Thanks for helping make ArcadeDB better! ArcadeDB is free and open source, sustained by its community. If it's useful to you or your company, please consider becoming a sponsor to keep development going.
fix(ArcadeData#8157,ArcadeData#8152,ArcadeData#8156,ArcadeData#8167,A…