{"record":{"id":"b68b6a95e091d1b0","repo":"apache/druid","slug":"cannot-cast-s-to-s-types-invalidcastexcepti","errorCode":null,"errorMessage":"Cannot cast [%s] to [%s] (Types.InvalidCastException from invalidCast)","messagePattern":"Cannot cast \\[(.+?)\\] to \\[(.+?)\\] \\(Types\\.InvalidCastException from invalidCast\\)","errorType":"validation","errorClass":"Types.InvalidCastException","httpStatus":400,"severity":"error","filePath":"processing/src/main/java/org/apache/druid/math/expr/ExprEval.java","lineNumber":888,"sourceCode":"          return ExprEval.ofString(asString());\n        case ARRAY:\n          switch (castTo.getElementType().getType()) {\n            case DOUBLE:\n              return ExprEval.ofDoubleArray(asArray());\n            case LONG:\n              return ExprEval.ofLongArray(value == null ? null : new Object[]{value.longValue()});\n            case STRING:\n              return ExprEval.ofStringArray(value == null ? null : new Object[]{value.toString()});\n            default:\n              ExpressionType elementType = (ExpressionType) castTo.getElementType();\n              return new ArrayExprEval(castTo, new Object[]{castTo(elementType).value()});\n          }\n        case COMPLEX:\n          if (ExpressionType.NESTED_DATA.equals(castTo)) {\n            return new NestedDataExprEval(value);\n          }\n      }\n      throw invalidCast(type(), castTo);\n    }\n\n    @Override\n    public Expr toExpr()\n    {\n      if (value == null) {\n        return new NullDoubleExpr();\n      }\n      return new DoubleExpr(value.doubleValue());\n    }\n  }\n\n  private static class LongExprEval extends NumericExprEval\n  {\n    private static final LongExprEval TRUE = new LongExprEval(Evals.asLong(true));\n    private static final LongExprEval FALSE = new LongExprEval(Evals.asLong(false));\n    private static final LongExprEval OF_NULL = new LongExprEval(null);\n","sourceCodeStart":870,"sourceCodeEnd":906,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/processing/src/main/java/org/apache/druid/math/expr/ExprEval.java#L870-L906","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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."],"exampleFix":"// before\nSELECT CAST(complex_col AS BIGINT) FROM t\n// after\nSELECT CAST(json_value(complex_col, '$.x') AS BIGINT) FROM t","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":["druid","expressions","cast","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"9b90983fd291f26935af934383ce360473179e4d","analyzedAt":"2026-09-07T13:32:30.957Z","contentChangedAt":"2026-09-07T13:32:30.957Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}