{"record":{"id":"27ca2b0ec6c5735d","repo":"apache/cassandra","slug":"s-only-accepts-one-argument-got-d","errorCode":null,"errorMessage":"%s() only accepts one argument (got %d)","messagePattern":"(.+?)\\(\\) only accepts one argument \\(got (.+?)\\)","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/functions/ToJsonFct.java","lineNumber":40,"sourceCode":"import java.util.Map;\nimport java.util.concurrent.ConcurrentHashMap;\n\nimport org.apache.cassandra.cql3.FunctionContext;\nimport org.apache.cassandra.db.marshal.AbstractType;\nimport org.apache.cassandra.db.marshal.UTF8Type;\nimport org.apache.cassandra.exceptions.InvalidRequestException;\nimport org.apache.cassandra.utils.ByteBufferUtil;\n\nimport static java.lang.String.format;\n\npublic class ToJsonFct extends NativeScalarFunction\n{\n    private static final Map<AbstractType<?>, ToJsonFct> instances = new ConcurrentHashMap<>();\n\n    public static ToJsonFct getInstance(String name, List<AbstractType<?>> argTypes) throws InvalidRequestException\n    {\n        if (argTypes.size() != 1)\n            throw new InvalidRequestException(format(\"%s() only accepts one argument (got %d)\", name, argTypes.size()));\n\n        AbstractType<?> fromType = argTypes.get(0);\n        ToJsonFct func = instances.get(fromType);\n        if (func == null)\n        {\n            func = new ToJsonFct(name, fromType);\n            instances.put(fromType, func);\n        }\n        return func;\n    }\n\n    private ToJsonFct(String name, AbstractType<?> argType)\n    {\n        super(name, UTF8Type.instance, argType);\n    }\n\n    @Override\n    public Arguments newArguments(FunctionContext context)","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/functions/ToJsonFct.java#L22-L58","documentation":"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.","triggerScenarios":"SELECT toJson(a, b) FROM ... or calling toJson() with zero arguments; any multi-argument or zero-argument usage of the toJson function.","commonSituations":"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.","solutions":["Pass exactly one argument: toJson(column_or_value)","Wrap each column separately: toJson(col1), toJson(col2), ...","Use SELECT JSON * or the Json class if you want the whole row as JSON instead of individual values"],"exampleFix":"// before\nSELECT toJson(a, b) FROM t;\n// after\nSELECT toJson(a), toJson(b) FROM t;","handlingStrategy":"validation","validationCode":"// Ensure exactly one argument before building the query:\nif (columns.size() != 1) throw new IllegalArgumentException(\"toJson() takes exactly one argument\");\nString cql = \"SELECT toJson(\" + columns.get(0) + \") FROM \" + table;","typeGuard":null,"tryCatchPattern":"try { session.execute(query); }\ncatch (InvalidRequestException e) {\n  if (e.getMessage().contains(\"only accepts one argument\")) { /* rebuild query with a single argument */ }\n  else throw e;\n}","preventionTips":["Wrap each value individually with toJson()","Use SELECT JSON ... for whole-row JSON instead of toJson()"],"tags":["cql","function","argument-count","json"],"backgroundTag":"missing-required-argument","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"}