#28553·presto

Casting to DECIMAL(38, 38) fails for representable values (e.g. 0)

Author: lingbinCreated Sep 25, 2026Updated Sep 25, 2026
Labelsbug

Environment

  • Presto version: 0.299 (commit 7d50721, official prestodb/presto:latest docker image); the relevant code paths are unchanged on master
  • Not connector-specific — reproducible with literals only

Current behavior

Any integral value — including 0 — fails to cast to DECIMAL(38, 38):

presto> SELECT CAST(0 AS DECIMAL(38, 38));
Query ... failed: Cannot cast INTEGER '0' to DECIMAL(38, 38)

The same happens for TINYINT/SMALLINT/BIGINT (any value, not just 0), and TRY_CAST returns NULL.

The decimal-to-decimal path fails the same way when the target scale is 38 and the source scale is 0:

presto> SELECT CAST(CAST(0 AS DECIMAL(10,0)) AS DECIMAL(38, 38));
Query ... failed: Cannot cast DECIMAL '0' to DECIMAL(38, 38)

presto> SELECT CAST(0.1012 AS DECIMAL(38,38)) > CAST(0 AS DECIMAL(10,0));
Query ... failed: Cannot cast DECIMAL '0' to DECIMAL(38, 38)

Expected behavior

0 has no integer digits and is representable in DECIMAL(38, 38), so the first query should return 0.00000000000000000000000000000000000000. Casts of genuinely unrepresentable values (e.g. CAST(1 AS DECIMAL(38, 38)), which needs 39 digits) should keep failing.

Everything at scale ≤ 37 already behaves correctly (CAST(0 AS DECIMAL(38, 37)) and CAST(0.0 AS DECIMAL(38, 38)) both work), so this is specific to scale-38 targets.

The short-decimal maximum behaves correctly

The exact same casts work at DECIMAL(18, 18) — the maximum scale for short decimals:

presto> SELECT CAST(0 AS DECIMAL(18, 18));
          _col0
-------------------------
 0.000000000000000000

CAST(CAST(0 AS DECIMAL(10,0)) AS DECIMAL(18, 18)), TRY_CAST(0 AS DECIMAL(18, 18)) and the comparison case also work there, and CAST(1 AS DECIMAL(18, 18)) still fails as expected. The short-decimal path uses plain long arithmetic (multiplyExact(value, tenToScale)), so the multiplier never runs through a value-range check — which suggests the long-decimal failure is a boundary artifact of how the 10^38 multiplier is represented, not an intentional overflow policy.

Root cause

Two failure points, one underlying issue: 10^38 is a perfectly valid int128 constant, but it gets rejected by checks that are meant for data values:

  1. Integer-to-long-decimal casts pack the scale multiplier as if it were a value: DecimalCasts.integerToLongDecimal computes multiply(unscaledDecimal(value), unscaledDecimal(tenToScale)), and unscaledDecimal(10^38) → pack() → throwIfOverflows rejects anything ≥ 10^38 before the multiplication happens — so the failure is independent of the value being cast, 0 included.
  2. Decimal-to-decimal casts call UnscaledDecimal128Arithmetic.rescale(value, 38), which throws unconditionally when rescaleFactor >= POWERS_OF_TEN.length — the table only holds 10^0..10^37.

The post-multiply overflow check (overflows(decimal, precision), which has a dedicated precision == 38 path) is already correct, so once the multiplier is representable, CAST(1 AS DECIMAL(38, 38)) would still fail as expected while CAST(0 AS DECIMAL(38, 38)) would succeed.

Suggested direction

Extending UnscaledDecimal128Arithmetic.POWERS_OF_TEN to include 10^38 (and building the integer-cast multiplier from it) should fix both paths: the post-multiply overflows(decimal, precision) check already handles precision == 38 correctly, so genuinely unrepresentable values would still fail.