A DuckDB SUM that cannot fit UINT8: validate aggregate output capacity
Small integer inputs can require a wider aggregate output type.
Aggregation changes the required range
The output type must accommodate the possible aggregate range, not merely the input values. For Truthound, the useful contract is the relationship between the output type's capacity and the maximum required by the workload, rather than the type name alone. [1, 2, 3]
A narrowing cast after a predicate change
The public DuckDB reproduction adds an equivalent CASE predicate to a DISTINCT SUM query. A narrowing conversion then appears after aggregation and cannot store a valid sum. Each input fitting the narrow type does not justify using that same type for the aggregate result. [1]
Record the export boundary
Declare the output type and its representable maximum at the aggregation boundary. Verify that predicate refactoring preserves the approved wider output contract. Derive a sum bound from input counts and value limits, or from an approved business restriction. Do not shrink a type solely because an observed small batch happened to fit. [1, 3]
A worked two-input fixture
An illustrative input containing 200 and 200 requires capacity for 400. An output-type maximum of 65535 satisfies that requirement; a maximum of 255 fails. Checking an execution that computes 400 is insufficient unless the export stage can retain that result too. [1, 3]
The Truthound relation check
Truthound Core's ExpressionValidator can check that the output-type maximum is at least the required aggregate bound in a prepared contract table. Supplying accurate type limits and workload bounds remains an external responsibility. The check does not repair optimizer materialization or casts. An explicit wider output type must still be validated against the destination schema. [1, 2, 3]
Application conditions
For each aggregate_name, place output_maximum and independently derived required_maximum in the same export-contract table.
Alternatives
Use an explicit wider result path without the narrowing conversion after aggregation.
Limitations
Accurate output-type maxima and workload aggregate bounds must be supplied externally. Truthound does not repair compressed materialization plans.
Evidence
- Event checked
- Subject checked
- Modified
