apache/cassandra · error · InvalidRequestException

unable to convert string

Error message

unable to convert string '<string>' to a value of type long

What it means

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.

Solutions

  1. Pass the argument as a plain integer string (e.g. '12345' or '-42') or as a native bigint literal instead of a string.
  2. Validate/normalize the string (strip separators, confirm digits only) before calling the function.
  3. If the value may be fractional, use the appropriate format function for double/decimal instead of the long one.

Example fix

// before
SELECT formatLong(uptime, '%d ms') ... -- with uptime = '1,000'
// after
SELECT formatLong(uptime, '%d ms') ... -- with uptime = '1000'
Defensive patterns

Strategy: validation

Validate before calling

String s = maybeValue.trim();
if (!s.matches("-?\\d+")) throw new IllegalArgumentException("not a long: " + s);
long v = Long.parseLong(s); // range check happens here too

Type guard

boolean isLongString(String s) { try { Long.parseLong(s.trim()); return true; } catch (NumberFormatException e) { return false; } }

Try / catch

try { stmt.execute(query); } catch (InvalidRequestException e) { if (e.getMessage().contains("unable to convert string")) { /* fix argument to integer literal */ } else throw e; }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10). Data as JSON: /api/errors/c574356156e743d8. Report an issue: GitHub.

Appendix: source

Thrown at src/java/org/apache/cassandra/cql3/functions/FormatFcts.java:103

        if (value < 0)
            throw new InvalidRequestException("value must be non-negative");

        return value;
    }

    private static long getValue(Arguments arguments)
    {
        Optional<String> maybeString = getAsString(arguments, 0);

        if (maybeString.isPresent())
        {
            try
            {
                return Long.parseLong(maybeString.get());
            }
            catch (Exception ex)
            {
                throw new InvalidRequestException("unable to convert string '" + maybeString.get() + "' to a value of type long");
            }
        }
        else
        {
            return arguments.getAsLong(0);
        }
    }

    private static Optional<String> getAsString(Arguments arguments, int i)
    {
        try
        {
            return Optional.ofNullable(arguments.get(i));
        }
        catch (Exception ex)
        {
            return Optional.empty();
        }

View on GitHub (pinned to 88fd0f6a0e)