{"record":{"id":"f859ea1c544a6159","repo":"prestodb/presto","slug":"numeric-value-out-of-range-f859ea","errorCode":"NUMERIC_VALUE_OUT_OF_RANGE","errorMessage":"decimal overflow: ","messagePattern":"decimal overflow: ","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/operator/scalar/MathFunctions.java","lineNumber":1346,"sourceCode":"        @SqlType(\"decimal(rp, s)\")\n        @Constraint(variable = \"rp\", expression = \"min(38, p + 1)\")\n        public static Slice roundNLong(\n                @LiteralParameter(\"s\") long numScale,\n                @LiteralParameter(\"rp\") long resultPrecision,\n                @SqlType(\"decimal(p, s)\") Slice num,\n                @SqlType(StandardTypes.INTEGER) long decimals)\n        {\n            if (decimals >= numScale) {\n                return num;\n            }\n            int rescaleFactor = ((int) numScale) - (int) decimals;\n            try {\n                Slice result = rescale(rescale(num, -rescaleFactor), rescaleFactor);\n                throwIfOverflows(result, ((int) resultPrecision));\n                return result;\n            }\n            catch (ArithmeticException e) {\n                throw new PrestoException(NUMERIC_VALUE_OUT_OF_RANGE, \"decimal overflow: \" + num, e);\n            }\n        }\n\n        @LiteralParameters({\"p\", \"s\", \"rp\"})\n        @SqlType(\"decimal(rp, s)\")\n        @Constraint(variable = \"rp\", expression = \"min(38, p + 1)\")\n        public static Slice roundNShortLong(\n                @LiteralParameter(\"s\") long numScale,\n                @LiteralParameter(\"rp\") long resultPrecision,\n                @SqlType(\"decimal(p, s)\") long num,\n                @SqlType(StandardTypes.INTEGER) long decimals)\n        {\n            return roundNLong(numScale, resultPrecision, unscaledDecimal(num), decimals);\n        }\n    }\n\n    @ScalarFunction(\"truncate\")\n    @Description(\"round to integer by dropping digits after decimal point\")","sourceCodeStart":1328,"sourceCodeEnd":1364,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/operator/scalar/MathFunctions.java#L1328-L1364","documentation":"A decimal rescale round-trip in MathFunctions (e.g. used by decimal arithmetic/sqrt paths) produced a value that no longer fits the declared result precision, so NUMERIC_VALUE_OUT_OF_RANGE 'decimal overflow: <num>' is thrown. Presto decimals are fixed-precision; the result would exceed decimal(rp,s) bounds.","triggerScenarios":"Invoking decimal-returning math functions (e.g. sqrt/abs/inverse on DECIMAL) with values whose result needs more precision digits than the type allows (max 38), or arithmetic that overflows after rescale.","commonSituations":"Very large DECIMAL(p,s) inputs pushed through functions whose result precision rule (like min(38, p+1)) is exceeded; overly tight column/cast precision on computed expressions; large monetary computations.","solutions":["CAST the operand to a wider decimal before the operation, e.g. CAST(x AS DECIMAL(38,s)).","Reduce scale (fewer fractional digits) so precision fits within 38.","Catch the error and handle in TRY: SELECT TRY(sqrt(x)) to get NULL instead of failing.","Re-examine the query's expected magnitudes; scale down inputs if overflow is expected."],"exampleFix":"-- before\nSELECT sqrt(dec_col) FROM t; -- dec_col DECIMAL(38,10) overflows\n-- after\nSELECT CAST(sqrt(CAST(dec_col AS DECIMAL(38,4))) AS DECIMAL(38,10)) FROM t;","handlingStrategy":"try-catch","validationCode":"-- ensure the operand fits the result precision before computing\nSELECT * FROM t WHERE abs(CAST(x AS DECIMAL(38,s))) > <max_value_for_precision>;","typeGuard":null,"tryCatchPattern":"SELECT TRY(sqrt(dec_col)) AS sqrt_x FROM t; -- NULL instead of NUMERIC_VALUE_OUT_OF_RANGE","preventionTips":["Declare decimal columns with headroom (precision < 38) when results feed further math.","CAST operands to DECIMAL(38,s) with reduced scale before heavy math.","Use TRY() around decimal math on unbounded user data."],"tags":["presto","sql","decimal","overflow"],"backgroundTag":"decimal-overflow","analyzedSha":"55bb57d202de3b926896fa966c2c4a44c779634e","analyzedAt":"2026-09-04T12:50:26.162Z","contentChangedAt":"2026-09-04T12:50:26.162Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}