apache/druid · error · Types.InvalidCastException
Cannot cast [%s] to [%s] (Types.InvalidCastException from in
Error message
Cannot cast [%s] to [%s] (Types.InvalidCastException from invalidCast)
What it means
A typed ExprEval subclass (ExprEval.java:888, castTo) throws invalidCast when asked to cast its value to an ExpressionType combination it does not support. The message 'Cannot cast [from] to [to]' surfaces Druid's Types.InvalidCastException; it indicates an unsupported expression-level type coercion in a query.
Source
Thrown at processing/src/main/java/org/apache/druid/math/expr/ExprEval.java:888
return ExprEval.ofString(asString());
case ARRAY:
switch (castTo.getElementType().getType()) {
case DOUBLE:
return ExprEval.ofDoubleArray(asArray());
case LONG:
return ExprEval.ofLongArray(value == null ? null : new Object[]{value.longValue()});
case STRING:
return ExprEval.ofStringArray(value == null ? null : new Object[]{value.toString()});
default:
ExpressionType elementType = (ExpressionType) castTo.getElementType();
return new ArrayExprEval(castTo, new Object[]{castTo(elementType).value()});
}
case COMPLEX:
if (ExpressionType.NESTED_DATA.equals(castTo)) {
return new NestedDataExprEval(value);
}
}
throw invalidCast(type(), castTo);
}
@Override
public Expr toExpr()
{
if (value == null) {
return new NullDoubleExpr();
}
return new DoubleExpr(value.doubleValue());
}
}
private static class LongExprEval extends NumericExprEval
{
private static final LongExprEval TRUE = new LongExprEval(Evals.asLong(true));
private static final LongExprEval FALSE = new LongExprEval(Evals.asLong(false));
private static final LongExprEval OF_NULL = new LongExprEval(null);
View on GitHub (pinned to 9b90983fd2)
Solutions
- Remove the cast or cast to a supported target type for that source type.
- Extract a scalar member from complex/nested data before casting (e.g. json_value / nested field access).
- Use TRY_CAST to get NULL instead of the exception where supported.
- Check EXPLAIN output for implicit casts the planner inserted.
Example fix
// before SELECT CAST(complex_col AS BIGINT) FROM t // after SELECT CAST(json_value(complex_col, '$.x') AS BIGINT) FROM t
Defensive patterns
Strategy: try-catch
When it happens
Trigger: Invoking cast() / CAST() on an expression whose runtime ExprEval type has no conversion to the requested target type (e.g. casting an unsupported complex value type to a numeric type, or incompatible nested-data casts).
Common situations: CAST(... AS BIGINT) on complex/nested columns without extracting a scalar first; queries mixing complex types across engine versions; nested GROUP BY or functions that implicitly coerce complex values.
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
- Cannot implicitly cast [%s] to [%s]
- second argument must be a base64 encoded STRING value but go
- Invalid type [%s]
- second argument should be STRING but got %s instead
- second argument must be a a scalar type but got %s instead
AI-assisted analysis of apache/druid@9b90983fd2 (2026-09-07).
Data as JSON: /api/errors/b68b6a95e091d1b0.
Report an issue: GitHub.