AGG(DISTINCT x) beside an aggregate over an expression fails with Out of range [0x80000112] (3.4.2.5)
Version
taosd version: 3.4.2.5.community(TDengine TSDB-OSS tarball)git: c15925333c9fe385902b153879b812c26bc612f7,build: Linux-arm64 2026-08-16 16:59:49- Single dnode, stock
taos.cfg, Ubuntu 24.04 aarch64
What happens
A query that mixes AGG(DISTINCT ...) with another aggregate whose argument is
an expression rather than a bare column fails with
Out of range [0x80000112]. Either aggregate on its own is fine, and the same
pair with a bare column argument is fine. Three rows are enough to reproduce
it, so this is not an actual overflow.
Reproduction
CREATE DATABASE t;
CREATE TABLE t.t2 (ts TIMESTAMP, n BIGINT, s VARCHAR(64));
INSERT INTO t.t2 VALUES
(1700000000000, 1, 'aa') (1700000000001, 2, 'bbb') (1700000000002, 2, 'cccc');Fails:
SELECT count(DISTINCT n), sum(length(s)) FROM t2 -> Out of range [0x80000112]
SELECT count(DISTINCT n), max(length(s)) FROM t2 -> Out of range [0x80000112]
SELECT count(DISTINCT n), count(length(s)) FROM t2 -> Out of range [0x80000112]
SELECT count(DISTINCT n), sum(abs(n)) FROM t2 -> Out of range [0x80000112]
SELECT count(DISTINCT n), sum(n+1) FROM t2 -> Out of range [0x80000112]
SELECT count(DISTINCT n), sum(length('abc')) FROM t2 -> Out of range [0x80000112]
SELECT sum(DISTINCT n), sum(length(s)) FROM t2 -> Out of range [0x80000112]Succeeds:
SELECT count(DISTINCT n) FROM t2 -> 2
SELECT sum(length(s)) FROM t2 -> 9
SELECT count(DISTINCT n), sum(n) FROM t2 -> 2 | 5
SELECT count(DISTINCT n), avg(n), sum(n) FROM t2 -> 2 | 1.666.. | 5
SELECT count(DISTINCT abs(n)), sum(n) FROM t2 -> 2 | 5So the trigger is narrow and consistent: a DISTINCT aggregate co-located with
a non-DISTINCT aggregate over a function call or arithmetic expression.
An expression inside the DISTINCT aggregate itself is fine
(count(DISTINCT abs(n))), and a constant argument still trips it
(sum(length('abc'))), which points at the co-location handling rather than
at any value being out of range.
Wrapping the source in a subquery does not help:
SELECT count(DISTINCT n), sum(length(s)) FROM (SELECT n, s FROM t2)
-> Out of range [0x80000112]Expected
Both aggregates evaluated, as the documentation for DISTINCT aggregation allows: "A single query may mix DISTINCT and non-DISTINCT aggregates".
Why we ran into it
Found while adding TDengine to ClickBench.
None of the benchmark's final queries hit it, but it came up while translating
them and cost some time to pin down, because Out of range reads like a data
problem rather than a planner one.
Source: taosdata/TDengine