{"record":{"id":"686bbc2a3fcb9ee8","repo":"apache/cassandra","slug":"cannot-parse-decimal-value-from-s","errorCode":null,"errorMessage":"Cannot parse decimal value from \"%s\"","messagePattern":"Cannot parse decimal value from \"(.+?)\"","errorType":"validation","errorClass":"InvalidTypeException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java","lineNumber":1310,"sourceCode":"        private static final DecimalCodec instance = new DecimalCodec();\n\n        private DecimalCodec()\n        {\n            super(DataType.decimal(), BigDecimal.class);\n        }\n\n        @Override\n        public BigDecimal parse(String value)\n        {\n            try\n            {\n                return value == null || value.isEmpty() || value.equalsIgnoreCase(\"NULL\")\n                       ? null\n                       : new BigDecimal(value);\n            }\n            catch (NumberFormatException e)\n            {\n                throw new InvalidTypeException(\n                String.format(\"Cannot parse decimal value from \\\"%s\\\"\", value));\n            }\n        }\n\n        @Override\n        public String format(BigDecimal value)\n        {\n            if (value == null) return \"NULL\";\n            return value.toString();\n        }\n\n        @Override\n        public ByteBuffer serialize(BigDecimal value, ProtocolVersion protocolVersion)\n        {\n            if (value == null) return null;\n            BigInteger bi = value.unscaledValue();\n            int scale = value.scale();\n            byte[] bibytes = bi.toByteArray();","sourceCodeStart":1292,"sourceCodeEnd":1328,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java#L1292-L1328","documentation":"The decimal codec's parse() delegates to new BigDecimal(value); strings that are not valid decimal representations raise NumberFormatException, rethrown as InvalidTypeException. Valid forms are optional sign, digits with optional decimal point and exponent — like CQL decimal literals.","triggerScenarios":"Calling decimalCodec.parse(value) with non-numeric or malformed strings such as \"abc\", \"1,234.5\", \"1.2.3\", empty-after-NULL-check tokens, or locale-formatted numbers.","commonSituations":"CSV/JSON imports with thousands separators or comma decimal points (European locales); currency symbols in the string; scientific notation from one system pasted where a different syntax is expected; whitespace or invisible characters.","solutions":["Normalize the string: strip currency symbols and grouping separators, convert the decimal comma to a dot, trim whitespace.","Use a NumberFormat with the correct Locale and parse to BigDecimal, then pass its toPlainString().","Use codec.format(BigDecimal) when starting from a real BigDecimal instead of a string."],"exampleFix":"// before\ndecimalCodec.parse(\"1.234,56\"); // throws (NumberFormatException)\n// after\nString normalized = \"1.234,56\".replace(\".\", \"\").replace(',', '.'); // \"1234.56\"\ndecimalCodec.parse(normalized);","handlingStrategy":"validation","validationCode":"static boolean isCqlDecimalLiteral(String v) {\n    return v != null && v.trim().matches(\"^[+-]?(\\\\d+(\\\\.\\\\d*)?|\\\\.\\\\d+)([eE][+-]?\\\\d+)?$\" );\n}","typeGuard":"boolean isDecimalLiteral(String v) {\n    if (v == null) return false;\n    try { new BigDecimal(v.trim()); return true; } catch (NumberFormatException e) { return false; }\n}","tryCatchPattern":"try {\n    return decimalCodec.parse(raw);\n} catch (InvalidTypeException e) {\n    String norm = raw.replace(\".\", \"\").replace(',', '.').trim();\n    return decimalCodec.parse(norm);\n}","preventionTips":["Normalize locale-specific formats (decimal comma, grouping separators, currency symbols) before parsing.","Prefer building BigDecimal objects in code and using codec.format().","Pre-validate with new BigDecimal(s) in a try/catch helper."],"tags":["cql","decimal","codec","number-parsing","locale"],"backgroundTag":"invalid-argument-format","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}