{"record":{"id":"e27ec426c9443b84","repo":"apache/cassandra","slug":"cannot-infer-type-for-term","errorCode":null,"errorMessage":"Cannot infer type for term ","messagePattern":"Cannot infer type for term ","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/selection/Selectable.java","lineNumber":217,"sourceCode":"             * But in the 2nd case, we're fine and can use the expectedType to \"prepare\" the bind marker/collect the bound type.\n             *\n             * Further, the term might not be a bind marker, in which case we sometimes can default to some most-general type. For instance, in\n             *   SELECT 3 FROM foo\n             * we'll just default the type to 'varint' as that's the most generic type for the literal '3' (this is mostly for convenience, the query\n             * is not terribly useful in practice and use can force the type as for the bind marker case through \"SELECT (int)3 FROM foo\").\n             * But note that not all literals can have such default type. For instance, there is no way to infer the type of a UDT literal in a vacuum,\n             * and so we simply error out if we have something like:\n             *   SELECT { foo: 'bar' } FROM foo\n             *\n             * Lastly, note that if the term is a terminal literal, we don't have to check it's compatibility with 'expectedType' as any incompatibility\n             * would have been found at preparation time.\n             */\n            AbstractType<?> type = getExactTypeIfKnown(table.keyspace);\n            if (type == null)\n            {\n                type = expectedType;\n                if (type == null)\n                    throw new InvalidRequestException(\"Cannot infer type for term \" + this + \" in selection clause (try using a cast to force a type)\");\n            }\n\n            // The fact we default the name to \"[selection]\" inconditionally means that any bind marker in a\n            // selection will have this name. Which isn't terribly helpful, but it's unclear how to provide\n            // something a lot more helpful and in practice user can bind those markers by position or, even better,\n            // use bind markers.\n            Term term = rawTerm.prepare(table.keyspace, new ColumnSpecification(table.keyspace, table.name, bindMarkerNameInSelection, type));\n            term.collectMarkerSpecification(boundNames, table);\n            return TermSelector.newFactory(rawTerm.getText(), term, type);\n        }\n\n        @Override\n        public AbstractType<?> getExactTypeIfKnown(String keyspace)\n        {\n            return rawTerm.getExactTypeIfKnown(keyspace);\n        }\n\n        @Override","sourceCodeStart":199,"sourceCodeEnd":235,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/selection/Selectable.java#L199-L235","documentation":"A bare term/bind marker in the SELECT clause (e.g. SELECT ?) must have a known CQL type to create a receiver. If the selectable's exact type cannot be resolved from the table and no expectedType was passed down, type inference fails and the query is rejected with a hint to use a cast.","triggerScenarios":"SELECT ? FROM t where the term is a bind marker (or literal needing inference) with no surrounding function/cast that fixes its type.","commonSituations":"Users write SELECT ? or pass constants wrapped in functions whose overload is ambiguous; Cassandra cannot determine which type the marker should bind to.","solutions":["Add an explicit cast: SELECT (int)? FROM t","Use a literal of the intended type instead of a bare bind marker","Wrap the term in a typed function such as blobAsInt(...) or toTimestamp(...) to fix the type"],"exampleFix":"// before\nSELECT ? FROM t;\n// after\nSELECT (int)? FROM t;","handlingStrategy":"validation","validationCode":"// ensure the term type is inferable before preparing\nif (termType == null) addCast(\"SELECT (int)? ...\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never leave bare bind markers in the SELECT clause","Cast inferred-type terms explicitly","Use literals of the exact intended type"],"tags":["cql","type-inference","bind-marker"],"backgroundTag":"type-mismatch","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"}