{"record":{"id":"14c2cb07a2fbeda9","repo":"apache/flink","slug":"there-is-leading-or-trailing-whitespace-in-the-num","errorCode":null,"errorMessage":"There is leading or trailing whitespace in the numeric field.","messagePattern":"There is leading or trailing whitespace in the numeric field\\.","errorType":"exception","errorClass":"NumberFormatException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/types/parser/BigDecParser.java","lineNumber":121,"sourceCode":"     * @throws IllegalArgumentException Thrown when the value cannot be parsed because the text\n     *     represents not a correct number.\n     */\n    public static final BigDecimal parseField(\n            byte[] bytes, int startPos, int length, char delimiter) {\n        if (length <= 0) {\n            throw new NumberFormatException(\"Invalid input: Empty string\");\n        }\n        int i = 0;\n        final byte delByte = (byte) delimiter;\n\n        while (i < length && bytes[startPos + i] != delByte) {\n            i++;\n        }\n\n        if (i > 0\n                && (Character.isWhitespace(bytes[startPos])\n                        || Character.isWhitespace(bytes[startPos + i - 1]))) {\n            throw new NumberFormatException(\n                    \"There is leading or trailing whitespace in the numeric field.\");\n        }\n\n        final char[] chars = new char[i];\n        for (int j = 0; j < i; j++) {\n            final byte b = bytes[startPos + j];\n            if ((b < '0' || b > '9') && b != '-' && b != '+' && b != '.' && b != 'E' && b != 'e') {\n                throw new NumberFormatException();\n            }\n            chars[j] = (char) bytes[startPos + j];\n        }\n        return new BigDecimal(chars);\n    }\n}\n","sourceCodeStart":103,"sourceCodeEnd":136,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/types/parser/BigDecParser.java#L103-L136","documentation":"Thrown by BigDecParser.parseField when the decimal field's text has leading or trailing whitespace. After scanning to the delimiter, the parser checks the first and last byte of the field with Character.isWhitespace and rejects the value before constructing the BigDecimal, because BigDecimal's constructor preserves whitespace-free digits strictly.","triggerScenarios":"Calling BigDecParser.parseField with a field like \" 1.23\" or \"45.6 \" — i.e. delimited text where numeric columns contain padding, spaces around delimiters, or CR characters from Windows line endings (\\r trailing before a \\n).","commonSituations":"CSV files edited in spreadsheets or exported with padded columns; Windows CRLF line endings leaving a trailing \\r on the last field; fixed-width sources sliced with whitespace included.","solutions":["Trim the field bytes before parsing, or pre-clean the input so numeric columns contain no padding.","For CRLF files, normalize line endings (\\r\\n -> \\n) before splitting records, since a trailing \\r counts as whitespace.","Adjust delimiter/quote configuration so whitespace is not captured inside numeric fields."],"exampleFix":"// before\nBigDecimal v = BigDecParser.parseField(bytes, start, len, '|');\n\n// after\nString field = new String(bytes, start, len, StandardCharsets.UTF_8).trim();\nBigDecimal v = new BigDecimal(field);","handlingStrategy":"validation","validationCode":"int end = start + len;\nif (len > 0 && (Character.isWhitespace(bytes[start]) || Character.isWhitespace(bytes[end - 1]))) {\n    throw new IllegalArgumentException(\"Untrimmed BigDecimal field\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    BigDecimal v = BigDecParser.parseField(bytes, start, len, delim);\n} catch (NumberFormatException e) {\n    String raw = new String(bytes, start, len, StandardCharsets.UTF_8);\n    BigDecimal v = new BigDecimal(raw.trim()); // fallback after explicit trim\n}","preventionTips":["Trim numeric fields once at ingestion before format parsing.","Normalize CRLF line endings when reading Windows-sourced files.","Include real exported files in test fixtures to catch padding drift."],"tags":["parser","bigdecimal","whitespace","csv","flink-core"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}