{"record":{"id":"17e1f230b4ca269b","repo":"apache/cassandra","slug":"value-must-be-non-negative","errorCode":null,"errorMessage":"value must be non-negative","messagePattern":"value must be non-negative","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/functions/FormatFcts.java","lineNumber":86,"sourceCode":"    {\n        return decimalFormat.format(value);\n    }\n\n    public static void addFunctionsTo(NativeFunctions functions)\n    {\n        functions.add(FormatBytesFct.factory());\n        functions.add(FormatTimeFct.factory());\n    }\n\n    private static long validateAndGetValue(Arguments arguments)\n    {\n        if (arguments.containsNulls())\n            throw new InvalidRequestException(\"none of the arguments may be null\");\n\n        long value = getValue(arguments);\n\n        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            }","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/functions/FormatFcts.java#L68-L104","documentation":"Thrown by FormatFcts.validateAndGetValue when the numeric argument to a time-formatting function is negative. These functions expect a non-negative epoch/timestamp-like value, and negative inputs are rejected with InvalidRequestException.","triggerScenarios":"Calling a format function with a negative long, typically an epoch-millis/epoch-second value computed from bad data, clock skew before epoch, or arithmetic underflow.","commonSituations":"Pre-1970 dates stored as raw epoch numbers; unsigned values misparsed as signed; delta computations where minuend < subtrahend; garbage data in numeric columns.","solutions":["Validate the value is >= 0 before calling the function; clamp or correct negative values.","Fix the upstream computation producing the negative timestamp (ordering of subtraction, unit conversion).","If pre-epoch dates are legitimate, use a function/type that supports them rather than these format functions."],"exampleFix":"// before: SELECT formatDate(-1000); -- rejected\n// after\nSELECT formatDate(max(value, 0)); -- clamp, or fix the source value","handlingStrategy":"validation","validationCode":"// Java/CQL: reject or clamp negatives before formatting\nif (epochMillis < 0) throw new IllegalArgumentException(\"timestamp must be non-negative\");\n// CQL: SELECT formatDate(CASE WHEN v < 0 THEN 0 ELSE v END) ...","typeGuard":null,"tryCatchPattern":"catch (InvalidRequestException e) { if (e.getMessage().contains(\"value must be non-negative\")) { log.warn(\"negative value passed to format function\"); } }","preventionTips":["Validate epoch values at ingestion time.","Check subtraction order when computing durations/deltas.","Handle pre-1970 dates with an explicit type, not epoch math."],"tags":["cassandra","cql","function","value-out-of-range"],"backgroundTag":"value-out-of-range","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"}