apache/cassandra · error · InvalidRequestException

Cannot infer type for term

Error message

Cannot infer type for term %s in selection clause (try using a cast to force a type)

What it means

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.

Solutions

  1. Add an explicit cast to the term: CAST(x AS int) or a typed literal
  2. Bind the value with a typed bind marker so the driver supplies the type
  3. Rewrite the expression to avoid BETWEEN on an untyped term, or compute it client-side
  4. Use a type-specific literal form (e.g. [1,2] list literal, {..} set/map literal) that carries its type

Example fix

// before
SELECT (1 BETWEEN ? AND ?) FROM t;
// after
SELECT (CAST(1 AS int) BETWEEN ? AND ?) FROM t;
Defensive patterns

Strategy: validation

Validate before calling

// Ensure every term in the selection clause is typed: use typed literals or CAST(x AS type),
// and provide driver type hints for bind markers before execution.

Try / catch

catch (InvalidRequestException e) {
  if (e.getMessage().contains("Cannot infer type for term")) {
    // retry with explicit CAST on the offending term
  }
}

Prevention

When it happens

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

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

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

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

        {
            if (selectables.size() == 1 && !receiver.type.isTuple())
                return selectables.get(0).testAssignment(keyspace, receiver);

            return Tuples.testTupleAssignment(receiver, selectables);
        }

        @Override
        public Factory newSelectorFactory(TableMetadata cfm,
                                          AbstractType<?> expectedType,
                                          List<ColumnMetadata> defs,
                                          VariableSpecifications boundNames)
        {
            AbstractType<?> type = getExactTypeIfKnown(cfm.keyspace);
            if (type == null)
            {
                type = expectedType;
                if (type == null)
                    throw invalidRequest("Cannot infer type for term %s in selection clause (try using a cast to force a type)",
                                         this);
            }

            if (selectables.size() == 1 && !type.isTuple())
                return newBetweenParenthesesSelectorFactory(cfm, expectedType, defs, boundNames);

            return newTupleSelectorFactory(cfm, (TupleType) type, defs, boundNames);
        }

        private Factory newBetweenParenthesesSelectorFactory(TableMetadata cfm,
                                                             AbstractType<?> expectedType,
                                                             List<ColumnMetadata> defs,
                                                             VariableSpecifications boundNames)
        {
            Selectable selectable = selectables.get(0);
            final Factory factory = selectable.newSelectorFactory(cfm, expectedType, defs, boundNames);

            return new ForwardingFactory()

View on GitHub (pinned to 88fd0f6a0e)