{"record":{"id":"41760d4b2317e1cb","repo":"prestodb/presto","slug":"decimal-scale-must-be-in-range-0-precision","errorCode":null,"errorMessage":"DECIMAL scale must be in range [0, precision]","messagePattern":"DECIMAL scale must be in range \\[0, precision\\]","errorType":"validation","errorClass":"InvalidFunctionArgumentException","httpStatus":null,"severity":"error","filePath":"presto-common/src/main/java/com/facebook/presto/common/type/DecimalType.java","lineNumber":96,"sourceCode":"\n    public int getScale()\n    {\n        return scale;\n    }\n\n    public boolean isShort()\n    {\n        return precision <= MAX_SHORT_PRECISION;\n    }\n\n    void validatePrecisionScale(int precision, int scale, int maxPrecision)\n    {\n        if (precision <= 0 || precision > maxPrecision) {\n            throw new InvalidFunctionArgumentException(\"DECIMAL precision must be in range [1, \" + MAX_PRECISION + \"]\");\n        }\n\n        if (scale < 0 || scale > precision) {\n            throw new InvalidFunctionArgumentException(\"DECIMAL scale must be in range [0, precision]\");\n        }\n    }\n\n    private static List<TypeSignatureParameter> buildTypeParameters(int precision, int scale)\n    {\n        List<TypeSignatureParameter> typeParameters = new ArrayList<>();\n        typeParameters.add(TypeSignatureParameter.of(precision));\n        typeParameters.add(TypeSignatureParameter.of(scale));\n        return unmodifiableList(typeParameters);\n    }\n}\n","sourceCodeStart":78,"sourceCodeEnd":108,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-common/src/main/java/com/facebook/presto/common/type/DecimalType.java#L78-L108","documentation":"DecimalType.validatePrecisionScale requires the scale to satisfy 0 <= scale <= precision. A negative scale or a scale larger than the precision is not a representable DECIMAL(p,s) in Presto, so InvalidFunctionArgumentException is thrown. This fires only when precision itself was in range, so the caller got precision right but the scale wrong.","triggerScenarios":"Calling DecimalType.createType(precision, scale) with scale < 0 or scale > precision (e.g. DECIMAL(5,7) or DECIMAL(10,-1)); connector metadata emitting scale greater than precision; arithmetic deriving scale from two operands incorrectly.","commonSituations":"Hand-translated schemas from systems with different scale semantics; formula computing scale = a+b while precision = a (scale exceeds precision); typos in DDL mapping code.","solutions":["Clamp scale to [0, precision] before createDecimalType","Fix the connector/schema mapping so scale <= precision is guaranteed","If the source genuinely has scale > precision, widen precision (up to 38) or fall back to DOUBLE","Validate DDL/derived type parameters before registering the type"],"exampleFix":"// before\nType type = DecimalType.createDecimalType(precision, scale); // scale may exceed precision\n// after\nint safeScale = Math.max(0, Math.min(scale, precision));\nType type = DecimalType.createDecimalType(precision, safeScale);","handlingStrategy":"validation","validationCode":"public static boolean isValidScale(int precision, int scale) {\n    return scale >= 0 && scale <= precision;\n}\n// clamp: int s = Math.max(0, Math.min(scale, precision));","typeGuard":"public static boolean hasValidScale(Integer p, Integer s) {\n    return p != null && s != null && s >= 0 && s <= p;\n}","tryCatchPattern":"try {\n    Type t = DecimalType.createDecimalType(precision, scale);\n} catch (InvalidFunctionArgumentException e) {\n    // clamp scale and retry, or widen precision\n}","preventionTips":["Always clamp scale to [0, precision] before type creation","Verify schema-mapping logic preserves scale <= precision","Test DDL translation with extreme scale/precision combos"],"tags":["presto","type-system","decimal","argument-validation"],"backgroundTag":"decimal-scale-out-of-range","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"}