apache/cassandra · error · InvalidRequestException

cannot be cast to

Error message

%s cannot be cast to %s

What it means

A cast in the selection clause (e.g. (int)x) is resolved as the native cast function for the target type; if FunctionResolver cannot find that cast function for the receiver column, the cast is impossible and the error reports that the source column cannot be cast to the target type.

Solutions

  1. Choose a supported cast path (e.g. via blob: textAsBlob then blobAsInt)
  2. Remove the cast and convert client-side
  3. Change the column type or use a UDF that performs the conversion

Example fix

// before
SELECT (map<int,text>)val FROM t;
// after
SELECT val FROM t; // convert client-side
Defensive patterns

Strategy: validation

Validate before calling

// verify a cast exists before emitting one
if (!CastFcts.isSupportedCast(sourceType, targetType)) throw new IllegalArgumentException("no cast " + sourceType + "->" + targetType);

Prevention

When it happens

Trigger: SELECT (someType)col FROM t where no native cast exists from col's type to someType (e.g. casting a non-blob to an arbitrary type, or incompatible numeric/collection casts).

Common situations: Attempting casts between types without a registered CastFcts conversion (e.g. text->int requires textAsInt-style casts only via blobAs* family on frozen blobs, or incompatible collection casts).

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


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

Appendix: source

Thrown at src/java/org/apache/cassandra/cql3/selection/Selectable.java:496

        }

        public Selector.Factory newSelectorFactory(TableMetadata table, AbstractType<?> expectedType, List<ColumnMetadata> defs, VariableSpecifications boundNames)
        {
            List<Selectable> args = Collections.singletonList(arg);
            SelectorFactories factories = SelectorFactories.createFactoriesAndCollectColumnDefinitions(args, null, table, defs, boundNames);

            Selector.Factory factory = factories.get(0);

            // If the user is trying to cast a type on its own type we simply ignore it.
            if (type.getType().equals(factory.getReturnType()))
                return factory;

            FunctionName name = FunctionName.nativeFunction(CastFcts.getFunctionName(type));
            Function fun = FunctionResolver.get(table.keyspace, name, args, table.keyspace, table.name, null, UserFunctions.getCurrentUserFunctions(name, table.keyspace));

            if (fun == null)
            {
                    throw new InvalidRequestException(String.format("%s cannot be cast to %s",
                                                                    defs.get(0).name,
                                                                    type));
            }
            return AbstractFunctionSelector.newFactory(fun, factories);
        }

        public AbstractType<?> getExactTypeIfKnown(String keyspace)
        {
            return type.getType();
        }

        @Override
        public boolean selectColumns(Predicate<ColumnMetadata> predicate)
        {
            return arg.selectColumns(predicate);
        }

        public static class Raw implements Selectable.Raw

View on GitHub (pinned to 88fd0f6a0e)