{"record":{"id":"c9506701f9db755f","repo":"prestodb/presto","slug":"decimal-does-not-fit-long-invalid-table-schema","errorCode":null,"errorMessage":"Decimal does not fit long (invalid table schema?)","messagePattern":"Decimal does not fit long \\(invalid table schema\\?\\)","errorType":"exception","errorClass":"OrcCorruptionException","httpStatus":null,"severity":"error","filePath":"presto-orc/src/main/java/com/facebook/presto/orc/stream/DecimalInputStream.java","lineNumber":112,"sourceCode":"        }\n\n        UnscaledDecimal128Arithmetic.pack(low, high, negative, result);\n    }\n\n    public long nextLong()\n            throws IOException\n    {\n        long result = 0;\n        int offset = 0;\n        long b;\n        do {\n            b = input.read();\n            if (b == -1) {\n                throw new OrcCorruptionException(input.getOrcDataSourceId(), \"Reading BigInteger past EOF\");\n            }\n            long work = 0x7f & b;\n            if (offset >= 63 && (offset != 63 || work > 1)) {\n                throw new OrcCorruptionException(input.getOrcDataSourceId(), \"Decimal does not fit long (invalid table schema?)\");\n            }\n            result |= work << offset;\n            offset += 7;\n        }\n        while (b >= 0x80);\n        boolean isNegative = (result & 0x01) != 0;\n        if (isNegative) {\n            result += 1;\n            result = -result;\n            result = result >> 1;\n            result |= 0x01L << 63;\n        }\n        else {\n            result = result >> 1;\n            result &= MAX_VALUE;\n        }\n        return result;\n    }","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-orc/src/main/java/com/facebook/presto/orc/stream/DecimalInputStream.java#L94-L130","documentation":"Thrown by DecimalInputStream.nextLong when a varint-encoded short decimal needs more than 63 bits of payload (offset > 63, or offset==63 with a payload byte > 1), meaning the value cannot fit in a signed 64-bit long. The parenthetical hints the usual cause: the actual data does not match the declared column type (schema says SHORT_DECIMAL/DECIMAL(p<=18) but the file holds a larger value).","triggerScenarios":"nextLong reads a varint whose continuation extends past byte offset 63 (or whose last allowed byte exceeds 1), i.e. the encoded magnitude exceeds Long range for a short-decimal column.","commonSituations":"Table schema declares DECIMAL(18,x) or similar but the writer stored bigger values; data imported from a system with unbounded precision (e.g. some ETL tools writing raw BigInteger); corrupt bytes extending a varint spuriously.","solutions":["Widen the table column to a precision that fits the data (e.g. DECIMAL(38,x), which reads via the 128-bit path) and rewrite the file, or fix the writer to emit only values within the declared precision.","Identify offending rows by scanning source data for values outside the declared precision and clamp/cast them before writing.","Verify the ORC footer type with orc-dump to confirm writer/schema mismatch, then recreate the table with the correct ORC type.","If the bytes are actually corrupt, regenerate the file from source."],"exampleFix":"-- before\nCREATE TABLE t (amount DECIMAL(18,2), ...);\n-- after: widen to hold values that exceed 18 digits\nCREATE TABLE t (amount DECIMAL(38,2), ...);","handlingStrategy":"validation","validationCode":"// caller-side guard before writing short decimals\nif (value.precision() > 18) {\n    value = value.setScale(scale, RoundingMode.HALF_UP);\n    if (value.precision() > 18) {\n        throw new IllegalArgumentException(\"does not fit DECIMAL(18,x): \" + value);\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    return readShortDecimal();\n} catch (OrcCorruptionException e) {\n    if (e.getMessage().contains(\"does not fit long\")) {\n        throw new SchemaMismatchException(\"column declared too narrow for data; widen to DECIMAL(38,x)\", e);\n    }\n    throw e;\n}","preventionTips":["Declare column precision no smaller than the widest value the source can produce.","Validate ranges in ETL against the table schema before writing.","When in doubt use 128-bit DECIMAL(38,x), which avoids the long path entirely.","Audit writer configs that drop or alter precision."],"tags":["orc","decimal","overflow","schema-mismatch"],"backgroundTag":"decimal-value-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"}