{"record":{"id":"36df64fdd4cdb9c8","repo":"apache/cassandra","slug":"s-cannot-be-cast-to-s","errorCode":null,"errorMessage":"%s cannot be cast to %s","messagePattern":"(.+?) cannot be cast to (.+?)","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/selection/Selectable.java","lineNumber":496,"sourceCode":"        }\n\n        public Selector.Factory newSelectorFactory(TableMetadata table, AbstractType<?> expectedType, List<ColumnMetadata> defs, VariableSpecifications boundNames)\n        {\n            List<Selectable> args = Collections.singletonList(arg);\n            SelectorFactories factories = SelectorFactories.createFactoriesAndCollectColumnDefinitions(args, null, table, defs, boundNames);\n\n            Selector.Factory factory = factories.get(0);\n\n            // If the user is trying to cast a type on its own type we simply ignore it.\n            if (type.getType().equals(factory.getReturnType()))\n                return factory;\n\n            FunctionName name = FunctionName.nativeFunction(CastFcts.getFunctionName(type));\n            Function fun = FunctionResolver.get(table.keyspace, name, args, table.keyspace, table.name, null, UserFunctions.getCurrentUserFunctions(name, table.keyspace));\n\n            if (fun == null)\n            {\n                    throw new InvalidRequestException(String.format(\"%s cannot be cast to %s\",\n                                                                    defs.get(0).name,\n                                                                    type));\n            }\n            return AbstractFunctionSelector.newFactory(fun, factories);\n        }\n\n        public AbstractType<?> getExactTypeIfKnown(String keyspace)\n        {\n            return type.getType();\n        }\n\n        @Override\n        public boolean selectColumns(Predicate<ColumnMetadata> predicate)\n        {\n            return arg.selectColumns(predicate);\n        }\n\n        public static class Raw implements Selectable.Raw","sourceCodeStart":478,"sourceCodeEnd":514,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/selection/Selectable.java#L478-L514","documentation":"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.","triggerScenarios":"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).","commonSituations":"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).","solutions":["Choose a supported cast path (e.g. via blob: textAsBlob then blobAsInt)","Remove the cast and convert client-side","Change the column type or use a UDF that performs the conversion"],"exampleFix":"// before\nSELECT (map<int,text>)val FROM t;\n// after\nSELECT val FROM t; // convert client-side","handlingStrategy":"validation","validationCode":"// verify a cast exists before emitting one\nif (!CastFcts.isSupportedCast(sourceType, targetType)) throw new IllegalArgumentException(\"no cast \" + sourceType + \"->\" + targetType);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Limit casts to known-supported conversions (numeric family, via-blob)","Prefer client-side conversion for exotic type changes","Centralize cast usage in a query-builder whitelist"],"tags":["cql","cast","type-mismatch"],"backgroundTag":"incompatible-source-type","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"}