Skip to content

[dbolympics] sum of a decimal returns a wider type than the plan schema declares when the query has a DISTINCT aggregate #26193

Closed

Description

@adriangb

Describe the bug

For sum(d) next to a DISTINCT aggregate, the declared type of the result and the type of the returned batches differ. The plan schema says Decimal128(15, 2). The returned column is Decimal128(25, 2).

SingleDistinctToGroupBy rewrites the query to a sum of sums. The outer sum widens the precision a second time, and nothing casts the result back to the declared type.

To Reproduce

In datafusion-cli 54.0.0:

CREATE TABLE t AS
SELECT * FROM (VALUES (CAST(1.25 AS DECIMAL(5,2)), 1), (CAST(2.50 AS DECIMAL(5,2)), 2)) AS v(d, x);

-- Declared type: Decimal128(15, 2)
DESCRIBE SELECT sum(d) AS s, count(DISTINCT x) AS c FROM t;

-- Returned type: Decimal128(25, 2)
SELECT arrow_typeof(s) FROM (SELECT sum(d) AS s, count(DISTINCT x) AS c FROM t);

-- Without the DISTINCT aggregate: Decimal128(15, 2)
SELECT arrow_typeof(sum(d)) FROM t;

EXPLAIN shows the rewrite:

Projection: sum(alias2) AS s, count(alias1) AS c
  Aggregate: groupBy=[[]], aggr=[[sum(alias2), count(alias1)]]
    Aggregate: groupBy=[[t.x AS alias1]], aggr=[[sum(t.d) AS alias2]]
      TableScan: t projection=[d, x]

Expected behavior

The returned batches have the declared type, Decimal128(15, 2). The rewrite casts the outer sum to the type of the original aggregate.

Additional context

The optimizer accepts the rewritten plan because the schema check treats two Decimal128 types as equal when only their precision differs. A client that reads the declared schema and then the batches sees two different types.

Activity

  1. changed the title [-]`sum` of a decimal returns a wider type than the plan schema declares when the query has a `DISTINCT` aggregate[/-] [+][dbolympics] `sum` of a decimal returns a wider type than the plan schema declares when the query has a `DISTINCT` aggregate[/+] on Oct 24, 2026
  2. alamb commented on Oct 24, 2026

    @alamb
    Contributor

    Affects DB Olympics, see: #26661

  3. added a commit that references this issue on Oct 26, 2026
    833f499
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions