apache/cassandra · error · InvalidRequestException

() cannot be used in the selection clause of a SELECT…

Error message

%s() cannot be used in the selection clause of a SELECT statement

What it means

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.

Solutions

  1. 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}')).
  2. For SELECT output, use toJson() on columns instead.
  3. Cast/wrap with an explicit type via a UDF or parse the JSON client-side.
  4. Instead of selecting fromJson(x) directly, select x and parse, or use toJson(col).

Example fix

// before
SELECT fromJson('{"a":1}') FROM t;
// after
INSERT INTO t (id, val) VALUES (1, fromJson('{"a":1}'));
Defensive patterns

Strategy: validation

Validate before calling

String q = "SELECT fromJson('{}') FROM t";
if (q.trim().toUpperCase().matches("SELECT\\s+FROMJSON.*")) throw new IllegalStateException("fromJson is only valid in INSERT/UPDATE value positions");

Try / catch

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; }

Prevention

When it happens

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

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

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at src/java/org/apache/cassandra/cql3/functions/FromJsonFct.java:105

    public static void addFunctionsTo(NativeFunctions functions)
    {
        functions.add(new Factory("from_json"));
        functions.add(new Factory("fromjson"));
    }
    
    private static class Factory extends FunctionFactory
    {
        private Factory(String name)
        {
            super(name, FunctionParameter.fixed(CQL3Type.Native.TEXT));
        }

        @Override
        protected NativeFunction doGetOrCreateFunction(List<AbstractType<?>> argTypes, AbstractType<?> receiverType)
        {
            if (receiverType == null)
                throw new InvalidRequestException(format("%s() cannot be used in the selection clause of a SELECT statement", name.name));

            return FromJsonFct.getInstance(name, receiverType);
        }
    }
}

View on GitHub (pinned to 88fd0f6a0e)