apache/cassandra · error · InvalidRequestException

() only accepts one argument (got )

Error message

%s() only accepts one argument (got %d)

What it means

InvalidRequestException from ToJsonFct.getInstance/factory: the toJson() function was invoked with an argument count other than one. The native function is unary regardless of argument types, so the arity check is the only acceptance guard in the factory.

Solutions

  1. Pass exactly one argument: toJson(column_or_value)
  2. Wrap each column separately: toJson(col1), toJson(col2), ...
  3. Use SELECT JSON * or the Json class if you want the whole row as JSON instead of individual values

Example fix

// before
SELECT toJson(a, b) FROM t;
// after
SELECT toJson(a), toJson(b) FROM t;
Defensive patterns

Strategy: validation

Validate before calling

// Ensure exactly one argument before building the query:
if (columns.size() != 1) throw new IllegalArgumentException("toJson() takes exactly one argument");
String cql = "SELECT toJson(" + columns.get(0) + ") FROM " + table;

Try / catch

try { session.execute(query); }
catch (InvalidRequestException e) {
  if (e.getMessage().contains("only accepts one argument")) { /* rebuild query with a single argument */ }
  else throw e;
}

Prevention

When it happens

Trigger: SELECT toJson(a, b) FROM ... or calling toJson() with zero arguments; any multi-argument or zero-argument usage of the toJson function.

Common situations: Attempting to convert an entire row or multiple columns in one call; confusion with JSON-encoding whole rows (which SELECT JSON already does); copy-paste of a multi-column expression.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at src/java/org/apache/cassandra/cql3/functions/ToJsonFct.java:40

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.cassandra.cql3.FunctionContext;
import org.apache.cassandra.db.marshal.AbstractType;
import org.apache.cassandra.db.marshal.UTF8Type;
import org.apache.cassandra.exceptions.InvalidRequestException;
import org.apache.cassandra.utils.ByteBufferUtil;

import static java.lang.String.format;

public class ToJsonFct extends NativeScalarFunction
{
    private static final Map<AbstractType<?>, ToJsonFct> instances = new ConcurrentHashMap<>();

    public static ToJsonFct getInstance(String name, List<AbstractType<?>> argTypes) throws InvalidRequestException
    {
        if (argTypes.size() != 1)
            throw new InvalidRequestException(format("%s() only accepts one argument (got %d)", name, argTypes.size()));

        AbstractType<?> fromType = argTypes.get(0);
        ToJsonFct func = instances.get(fromType);
        if (func == null)
        {
            func = new ToJsonFct(name, fromType);
            instances.put(fromType, func);
        }
        return func;
    }

    private ToJsonFct(String name, AbstractType<?> argType)
    {
        super(name, UTF8Type.instance, argType);
    }

    @Override
    public Arguments newArguments(FunctionContext context)

View on GitHub (pinned to 88fd0f6a0e)