taosdata / taosdata/TDengine

AGG(DISTINCT x) beside an aggregate over an expression fails with Out of range [0x80000112] (3.4.2.5)

Open
#35,451 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
25.1k
Forks
5k
Avg merge
4d 59m
Merged PRs (30d)
7

Description

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 | 5

So 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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided CREATE TABLE, INSERT, and mixed DISTINCT/non-DISTINCT aggregate queries against the stated TDengine version, then inspect the aggregate planning and expression-handling path. Done means these queries execute without the out-of-range error, return the expected aggregate results, and have regression coverage for function-call and arithmetic expressions alongside DISTINCT aggregates.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, sql
Domain
database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
62/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.