{"record":{"id":"fe15aeb47dd5304d","repo":"apache/cassandra","slug":"s-cannot-be-used-in-the-selection-clause-of-a-s","errorCode":null,"errorMessage":"%s() cannot be used in the selection clause of a SELECT statement","messagePattern":"(.+?)\\(\\) cannot be used in the selection clause of a SELECT statement","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/functions/FromJsonFct.java","lineNumber":105,"sourceCode":"\n    public static void addFunctionsTo(NativeFunctions functions)\n    {\n        functions.add(new Factory(\"from_json\"));\n        functions.add(new Factory(\"fromjson\"));\n    }\n    \n    private static class Factory extends FunctionFactory\n    {\n        private Factory(String name)\n        {\n            super(name, FunctionParameter.fixed(CQL3Type.Native.TEXT));\n        }\n\n        @Override\n        protected NativeFunction doGetOrCreateFunction(List<AbstractType<?>> argTypes, AbstractType<?> receiverType)\n        {\n            if (receiverType == null)\n                throw new InvalidRequestException(format(\"%s() cannot be used in the selection clause of a SELECT statement\", name.name));\n\n            return FromJsonFct.getInstance(name, receiverType);\n        }\n    }\n}\n","sourceCodeStart":87,"sourceCodeEnd":111,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/functions/FromJsonFct.java#L87-L111","documentation":"The fromJson() native function's type is only known from the type it is assigned to. When used as a top-level item in a SELECT selection clause there is no receiver type, so doGetOrCreateFunction() cannot resolve the target type and throws this InvalidRequestException. It means fromJson() was invoked in a context where its return type cannot be inferred.","triggerScenarios":"Writing 'SELECT fromJson(...)' directly in a selection clause; calling fromJson() without assigning it to a typed target (e.g. insert value position, set/list element of a known type).","commonSituations":"Developers assuming fromJson works like a scalar conversion in SELECT; migration from client-side JSON parsing; confusion between fromJson (INSERT/UPDATE values) and toJson (SELECT).","solutions":["Use fromJson() only where a receiver type exists: in INSERT/UPDATE values bound to a typed column, e.g. INSERT INTO t (id, tags) VALUES (1, fromJson('{\"a\":1}')).","For SELECT output, use toJson() on columns instead.","Cast/wrap with an explicit type via a UDF or parse the JSON client-side.","Instead of selecting fromJson(x) directly, select x and parse, or use toJson(col)."],"exampleFix":"// before\nSELECT fromJson('{\"a\":1}') FROM t;\n// after\nINSERT INTO t (id, val) VALUES (1, fromJson('{\"a\":1}'));","handlingStrategy":"validation","validationCode":"String q = \"SELECT fromJson('{}') FROM t\";\nif (q.trim().toUpperCase().matches(\"SELECT\\\\s+FROMJSON.*\")) throw new IllegalStateException(\"fromJson is only valid in INSERT/UPDATE value positions\");","typeGuard":null,"tryCatchPattern":"try { session.execute(q); } catch (InvalidRequestException e) { if (e.getMessage().contains(\"cannot be used in the selection clause\")) { /* rewrite query using toJson or INSERT position */ } else throw e; }","preventionTips":["Use fromJson only for INSERT/UPDATE values bound to typed targets","Use toJson() when you need JSON output in SELECT","Remember fromJson requires a receiver type to resolve"],"tags":["cql","json","invalid-context"],"backgroundTag":"unsupported-operation","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"}