{"record":{"id":"abcd7259d17d8abe","repo":"apache/cassandra","slug":"cannot-infer-type-for-term-s-in-selection-clause","errorCode":null,"errorMessage":"Cannot infer type for term %s in selection clause (try using a cast to force a type)","messagePattern":"Cannot infer type for term (.+?) in selection clause \\(try using a cast to force a type\\)","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/selection/Selectable.java","lineNumber":659,"sourceCode":"        {\n            if (selectables.size() == 1 && !receiver.type.isTuple())\n                return selectables.get(0).testAssignment(keyspace, receiver);\n\n            return Tuples.testTupleAssignment(receiver, selectables);\n        }\n\n        @Override\n        public Factory newSelectorFactory(TableMetadata cfm,\n                                          AbstractType<?> expectedType,\n                                          List<ColumnMetadata> defs,\n                                          VariableSpecifications boundNames)\n        {\n            AbstractType<?> type = getExactTypeIfKnown(cfm.keyspace);\n            if (type == null)\n            {\n                type = expectedType;\n                if (type == null)\n                    throw invalidRequest(\"Cannot infer type for term %s in selection clause (try using a cast to force a type)\",\n                                         this);\n            }\n\n            if (selectables.size() == 1 && !type.isTuple())\n                return newBetweenParenthesesSelectorFactory(cfm, expectedType, defs, boundNames);\n\n            return newTupleSelectorFactory(cfm, (TupleType) type, defs, boundNames);\n        }\n\n        private Factory newBetweenParenthesesSelectorFactory(TableMetadata cfm,\n                                                             AbstractType<?> expectedType,\n                                                             List<ColumnMetadata> defs,\n                                                             VariableSpecifications boundNames)\n        {\n            Selectable selectable = selectables.get(0);\n            final Factory factory = selectable.newSelectorFactory(cfm, expectedType, defs, boundNames);\n\n            return new ForwardingFactory()","sourceCodeStart":641,"sourceCodeEnd":677,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/selection/Selectable.java#L641-L677","documentation":"When building a BETWEEN-parentheses selector factory in a selection clause, Cassandra must know the term's type. It first tries the selectable's exact type, then falls back to the expected type; if both are null the type cannot be inferred and Selectable throws this error, suggesting an explicit cast.","triggerScenarios":"SELECT (a BETWEEN 1 AND 2)-style expressions in the selection list where the term's type is not derivable from the schema (e.g. literals or untyped placeholders) and no cast supplies it.","commonSituations":"Untyped bind markers / literals in SELECT expressions; wrapping arbitrary selectables in parentheses-between expressions without type context; typed-NULL or empty-literal terms whose type Cassandra cannot deduce.","solutions":["Add an explicit cast to the term: CAST(x AS int) or a typed literal","Bind the value with a typed bind marker so the driver supplies the type","Rewrite the expression to avoid BETWEEN on an untyped term, or compute it client-side","Use a type-specific literal form (e.g. [1,2] list literal, {..} set/map literal) that carries its type"],"exampleFix":"// before\nSELECT (1 BETWEEN ? AND ?) FROM t;\n// after\nSELECT (CAST(1 AS int) BETWEEN ? AND ?) FROM t;","handlingStrategy":"validation","validationCode":"// Ensure every term in the selection clause is typed: use typed literals or CAST(x AS type),\n// and provide driver type hints for bind markers before execution.","typeGuard":null,"tryCatchPattern":"catch (InvalidRequestException e) {\n  if (e.getMessage().contains(\"Cannot infer type for term\")) {\n    // retry with explicit CAST on the offending term\n  }\n}","preventionTips":["Always cast literals and bind markers used inside SELECT expressions","Avoid untyped placeholders in selection clauses","Prefer computing complex expressions client-side"],"tags":["cql","cassandra","type-inference","selection"],"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"}