Skip to content

The continuous-aggregate watermark filter is spliced into an existing WHERE without parentheses, so a defining query whose WHERE contains an OR re-aggregates rows older than the watermark #8156

Closed as duplicate

Description

@ruispereira

Summary

buildFilteredQuery injects the incremental filter by string surgery: it finds the top-level
WHERE and inserts \` >= AND straight after it, leaving the caller's own predicate unbracketed to the right.ANDbinds tighter thanOR`, so a defining query whose WHERE
is a disjunction is rewritten into something with a different meaning:

  written:  WHERE temperature > 100 OR sensor_id = 'A'
  intended: WHERE ts >= W AND (temperature > 100 OR sensor_id = 'A')
  produced: WHERE ts >= W AND temperature > 100 OR sensor_id = 'A'
          = WHERE (ts >= W AND temperature > 100) OR sensor_id = 'A'

The second disjunct escapes the watermark entirely, so rows from buckets strictly older than the
watermark come back and are aggregated again. Those older buckets are NOT covered by the
DELETE ... WHERE <bucket> >= <watermark> that opens the refresh, so their existing aggregate rows
survive and the recomputed ones are inserted next to them. The aggregate accumulates a duplicate row
for each such bucket on every refresh.

Where (HEAD 9a88be7)

engine/src/main/java/com/arcadedb/schema/ContinuousAggregateRefresher.java:139

    if (whereIdx >= 0) {
      // Insert the watermark filter right after WHERE
      final String before = query.substring(0, whereIdx + 5); // "WHERE" is 5 chars
      final String after = query.substring(whereIdx + 5);
      return before + " `" + tsColumn + "` >= " + watermark + " AND " + after.stripLeading();
    } else {

against the delete it has to stay in step with,
engine/src/main/java/com/arcadedb/schema/ContinuousAggregateRefresher.java:55

        if (watermark > 0)
          database.command("sql", "DELETE FROM `" + backingTypeName + "` WHERE `" + bucketColumn + "` >= ?",
              new Date(watermark));

Repro

What the rewriter emits, called directly with a watermark of 7200000:

IN : SELECT sensor_id, ts.timeBucket('1h', ts) AS hour, avg(temperature) AS avg_temp FROM SensorReading WHERE temperature > 100 OR sensor_id = 'A' GROUP BY sensor_id, hour
OUT: SELECT sensor_id, ts.timeBucket('1h', ts) AS hour, avg(temperature) AS avg_temp FROM SensorReading WHERE `ts` >= 7200000 AND temperature > 100 OR sensor_id = 'A' GROUP BY sensor_id, hour

IN : SELECT ts.timeBucket('1h', ts) AS hour, count(*) AS c FROM SensorReading WHERE sensor_id = 'A' GROUP BY hour
OUT: SELECT ts.timeBucket('1h', ts) AS hour, count(*) AS c FROM SensorReading WHERE `ts` >= 7200000 AND sensor_id = 'A' GROUP BY hour

The second pair is the control: a conjunctive WHERE is rewritten correctly.

End to end, on an embedded database, with the watermark defect above repaired so that the filter
path actually runs. Source type gets two rows (sensor A at 20 degrees, sensor B at 200) in each of
four consecutive hours, each hour inserted in its own transaction; the aggregate is
... WHERE temperature > 100 OR sensor_id = 'A' GROUP BY sensor_id, hour, and every (sensor, hour)
pair should appear exactly once:

after create         watermark=h1 rows=2  -> A@h1 B@h1
after hour-2 insert  watermark=h2 rows=4  -> A@h1 B@h1 A@h2 B@h2
after hour-3 insert  watermark=h3 rows=7  -> A@h1 A@h1 B@h1 A@h2 B@h2 A@h3 B@h3
after hour-4 insert  watermark=h4 rows=11 -> A@h1 A@h1 A@h1 B@h1 A@h2 A@h2 B@h2 A@h3 B@h3 A@h4 B@h4

A@h1 gains a copy on every refresh - it is the disjunct that escaped the watermark - while B@h1,
whose only qualifying predicate is temperature > 100 and is therefore inside the AND, stays at
one.

Suggested fix

Bracket the caller's predicate: before + " \" + tsColumn + "` >= " + watermark + " AND (" +
after.stripLeading() + ")"is wrong as written becauseafter` also carries the GROUP BY / ORDER BY
tail, so the closing bracket has to go at the end of the WHERE clause, not at the end of the string

  • which is the real lesson here. The rewriter already has findKeywordIndex for GROUP BY /
    ORDER BY / LIMIT and uses it in the other branch; use it in this branch too to find where the
    WHERE clause ends and close the bracket there. Better still, stop doing this with substrings: the
    statement is already parsed (StatementCache.get) for extractSourceType and for the classifier, so
    the filter can be added to the parsed WhereClause as a conjunct, which is bracket-safe by
    construction. A regression test should assert that a defining query with an OR in its WHERE produces
    exactly one row per (tag, bucket) after several refreshes.

Scope / confidence

The rewriting itself is shown directly from the method's own output; the duplication is reproduced
end to end with the watermark defect repaired, since the two interact. Rated medium rather than high
only because it is currently masked - today the watermark never leaves 0, so this branch is never
taken - and it needs an OR in the defining query's WHERE. It is reported as medium because the fix
for the first finding unmasks it, and because the failure mode is the same silent duplication.
ContinuousAggregateBuilder.validateQueryStructure rejects CTEs, subqueries and UNION but says
nothing about OR, so such a query is accepted at creation time.


Activity

  1. added
    audit
    engine
    severity:major
    timeseries
    on Sep 22, 2026
  2. added this to the 26.10.1 milestone on Sep 22, 2026
  3. github-actions commented on Sep 22, 2026

    @github-actions
    Contributor

    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.

  4. added 4 commits that reference this issue on Sep 22, 2026
  5. lvca commented on Sep 22, 2026

    @lvca
    Member

    Fixed in #8198, released in 26.10.1, tracked under #8181.

    When a continuous aggregate's query had its own WHERE containing an OR, the incremental filter was spliced in without brackets. AND binds tighter than OR, so part of the condition escaped the filter and re-aggregated buckets that were already present, adding a duplicate row for each on every refresh. The condition is now bracketed, and the clause scan that finds where it ends handles quoted identifiers, string literals and irregular whitespace.

    ❤️ 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    auditbugengineseverity:majorsqltimeseries

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions