{"record":{"id":"c574356156e743d8","repo":"apache/cassandra","slug":"unable-to-convert-string-string-to-a-value-of","errorCode":null,"errorMessage":"unable to convert string '<string>' to a value of type long","messagePattern":"unable to convert string '<string>' to a value of type long","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/functions/FormatFcts.java","lineNumber":103,"sourceCode":"        if (value < 0)\n            throw new InvalidRequestException(\"value must be non-negative\");\n\n        return value;\n    }\n\n    private static long getValue(Arguments arguments)\n    {\n        Optional<String> maybeString = getAsString(arguments, 0);\n\n        if (maybeString.isPresent())\n        {\n            try\n            {\n                return Long.parseLong(maybeString.get());\n            }\n            catch (Exception ex)\n            {\n                throw new InvalidRequestException(\"unable to convert string '\" + maybeString.get() + \"' to a value of type long\");\n            }\n        }\n        else\n        {\n            return arguments.getAsLong(0);\n        }\n    }\n\n    private static Optional<String> getAsString(Arguments arguments, int i)\n    {\n        try\n        {\n            return Optional.ofNullable(arguments.get(i));\n        }\n        catch (Exception ex)\n        {\n            return Optional.empty();\n        }","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/functions/FormatFcts.java#L85-L121","documentation":"FormatFcts' long-typed dynamic format functions accept either a native long argument or a string argument that must parse as a long. When a string is supplied, getValue() attempts Long.parseLong and throws this InvalidRequestException if the text is not a valid signed 64-bit integer. The error means the literal bound for the format argument is not convertible to a long.","triggerScenarios":"Calling a format function such as formatLong(...) with a string literal like 'abc', '12.5', or an out-of-long-range number, or binding a column/variable whose string representation is not a plain integer.","commonSituations":"Passing formatted numbers with thousand separators or currency symbols from application code; accidentally passing a float string; users typing non-numeric input in interactive cqlsh queries.","solutions":["Pass the argument as a plain integer string (e.g. '12345' or '-42') or as a native bigint literal instead of a string.","Validate/normalize the string (strip separators, confirm digits only) before calling the function.","If the value may be fractional, use the appropriate format function for double/decimal instead of the long one."],"exampleFix":"// before\nSELECT formatLong(uptime, '%d ms') ... -- with uptime = '1,000'\n// after\nSELECT formatLong(uptime, '%d ms') ... -- with uptime = '1000'","handlingStrategy":"validation","validationCode":"String s = maybeValue.trim();\nif (!s.matches(\"-?\\\\d+\")) throw new IllegalArgumentException(\"not a long: \" + s);\nlong v = Long.parseLong(s); // range check happens here too","typeGuard":"boolean isLongString(String s) { try { Long.parseLong(s.trim()); return true; } catch (NumberFormatException e) { return false; } }","tryCatchPattern":"try { stmt.execute(query); } catch (InvalidRequestException e) { if (e.getMessage().contains(\"unable to convert string\")) { /* fix argument to integer literal */ } else throw e; }","preventionTips":["Bind numbers as numeric literals/types, not strings","Strip locale-specific separators before passing number strings","Use the double/decimal format functions for fractional values"],"tags":["cql","invalid-argument-format","type-mismatch"],"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-17T15:17:12.973Z"}