Skip to content

A null time-series measurement is stored as a real 0.0, so AVG counts it and MIN returns it #7743

Closed

Description

@robfrank

A null field value - the natural way a client says "no measurement here" - is stored and read back as a real
0.0 rather than as the absent marker, so it is counted as a sample by AVG and beats every real value in MIN.

This is adjacent to #7694 but not the same question. #7694 decides what an aggregate answers when it holds no
real sample
; this is about a value that should have produced no real sample in the first place, and it is
pre-existing on both sides of that decision. Found while verifying #7694.

Repro

Two samples in one bucket, one real and one null:

t.getEngine().appendSamples(new long[] { 1_000L, 2_000L }, new Object[] { 10.0, null });
SELECT ts.timeBucket('1h', ts) AS b, sum(value) AS s, avg(value) AS a, min(value) AS mn, count(*) AS c
  FROM P GROUP BY b

measured:

sum=10.0  avg=5.0   min=0.0   count=2.0
  • avg=5.0 is wrong: one real sample of 10.0 was averaged over two, because the null entered the
    denominator. Expected 10.0.
  • min=0.0 is wrong: the null became a measurement that is smaller than every real one. Expected 10.0.
  • sum=10.0 is right only by accident - zero is the additive identity, so adding the phantom sample does not
    move the total.
  • count=2 is correct: COUNT counts rows, and there were two.

Both storage layers answer identically, so this is not a mutable-versus-sealed divergence:

PROBE-MUTABLE sum=10.0 avg=5.0 min=0.0 count=2.0
PROBE-SEALED  sum=10.0 avg=5.0 min=0.0 count=2.0   (after engine.compactAll())

An explicit Double.NaN behaves correctly - it is a Number, so it reaches the accumulator as the absent marker
and is skipped. It is only null that is coerced.

Where it comes from

ColumnDefinition.coerceValue passes null straight through:

public Object coerceValue(final Object value) {
  if (value == null)
    return null;

and the mutable-bucket reader in TimeSeriesEngine turns anything that is not a Number into 0.0 - at both
of its two accumulation sites, TimeSeriesEngine:654 and :700:

else if (columnIndices[r] < row.length && row[columnIndices[r]] instanceof Number n)
  rowValues[r] = n.doubleValue();
else
  rowValues[r] = 0.0;

TimeSeriesNaN.ABSENT is what that branch should produce, and the same branch also covers a row SHORTER than the
column index (columnIndices[r] >= row.length), which is what a row written before an ALTER ... ADD looks like -
so a column added to an existing type reads as a solid line of zeroes over its whole history rather than as a gap.
That second case is untested here and is worth confirming separately.

Suggested fix

Map "no value" to the absent marker at the two TimeSeriesEngine sites, and check the seal/compaction path
encodes the same for a null (the probe above shows the sealed layer agrees with the mutable one today, so
whatever writes the sealed column needs the same treatment). Needs regression tests for AVG and MIN, on both the
mutable and the sealed path, and for the short-row/ALTER ADD case.

Scope note

Worth deciding deliberately, because this changes query answers for data already stored: every AVG and MIN
over a column with nulls moves. That is why it is filed rather than folded into #7694's contract PR.

Activity

  1. added
    engine
    timeseries
    on Sep 16, 2026
  2. added this to the 26.10.1 milestone on Sep 16, 2026
  3. added
    in progress
    on Sep 16, 2026
  4. self-assigned this
    on Sep 16, 2026
  5. added 8 commits that reference this issue on Sep 16, 2026
  6. lvca commented on Sep 17, 2026

    @lvca
    Member

    Fixed in #7747, merged into main for 26.10.1.

    Writing null for a measurement - the natural way to say "no reading here" - stored a real 0.0. So AVG divided by that phantom sample and MIN returned the zero instead of your smallest real value.

    A null is now stored as "no measurement" on DOUBLE and FLOAT columns, on both the recent and the compacted data, and it reads back as SQL NULL. SUM, AVG, MIN and MAX skip it, exactly as they skip a NULL in SQL, while COUNT(*) still counts the row. An integer column has nowhere to record absence, so a null there is still stored as 0 - unchanged behaviour.

    Worth knowing before you upgrade: this changes the answers of AVG and MIN for series where you have been writing nulls. Data already stored as zero stays zero; only new writes are affected.


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

  7. removed
    in progress
    on Sep 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugenginesqltimeseries

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions