{"record":{"id":"fe9b885e506cf7c2","repo":"apache/druid","slug":"unknown-fe9b88","errorCode":"Unknown","errorMessage":"Incorrect type for column [%s]. Expected LONG but got type [%s]. Please ensure that the value is cast to LONG.","messagePattern":"Incorrect type for column \\[(.+?)\\]\\. Expected LONG but got type \\[(.+?)\\]\\. Please ensure that the value is cast to LONG\\.","errorType":"error_code","errorClass":"MSQException","httpStatus":null,"severity":"error","filePath":"multi-stage-query/src/main/java/org/apache/druid/msq/exec/MSQTasks.java","lineNumber":125,"sourceCode":"  public static long primaryTimestampFromObjectForInsert(final Object timestamp)\n  {\n    if (timestamp instanceof Long) {\n      return (long) timestamp;\n    } else if (timestamp == null) {\n      throw new MSQException(InsertTimeNullFault.INSTANCE);\n    } else {\n      // Normally we expect the SQL layer to validate that __time for INSERT is a TIMESTAMP type, which would\n      // be a long at execution time. So a nice user-friendly message isn't needed here: it would only happen\n      // if the SQL layer is bypassed. Nice, friendly users wouldn't do that :)\n      final UnknownFault fault =\n          UnknownFault.forMessage(\n              StringUtils.format(\n                  \"Incorrect type for column [%s]. Expected LONG but got type [%s]. Please ensure that the value is cast to LONG.\",\n                  ColumnHolder.TIME_COLUMN_NAME,\n                  timestamp.getClass().getSimpleName()\n              )\n          );\n      throw new MSQException(fault);\n    }\n  }\n\n  /**\n   * Returns the host:port from a {@link DruidNode}. Convenience method to make it easier to construct\n   * {@link MSQErrorReport} instances.\n   */\n  @Nullable\n  static String getHostFromSelfNode(@Nullable final DruidNode selfNode)\n  {\n    return selfNode != null ? selfNode.getHostAndPortToUse() : null;\n  }\n\n  static StorageConnector makeStorageConnector(final Injector injector)\n  {\n    try {\n      StorageConnector storageConnector = injector.getInstance(Key.get(StorageConnector.class, MultiStageQuery.class));\n      if (storageConnector instanceof NilStorageConnector) {","sourceCodeStart":107,"sourceCodeEnd":143,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/multi-stage-query/src/main/java/org/apache/druid/msq/exec/MSQTasks.java#L107-L143","documentation":"MSQTasks.primaryTimestampFromObjectForInsert requires the __time primary timestamp value for an INSERT to be a Long (epoch millis). If the supplied object is any other type, an MSQException is thrown instructing the user to cast the value to LONG.","triggerScenarios":"MSQ INSERT where the value provided for column __time evaluates to a non-LONG type (e.g. STRING timestamp, TIMESTAMP object, DOUBLE) instead of a Java Long.","commonSituations":"Writing a string date like '2023-01-01' directly to __time without CAST; a query returning __time as VARCHAR due to upstream casting; passing TIMESTAMP-typed expressions that MSQ doesn't auto-convert.","solutions":["CAST the expression to BIGINT before assigning it to __time, e.g. CAST(TIME_PARSE(ts) AS BIGINT) or UNIX_TIMESTAMP(ts)","Use functions that return epoch millis (MILLIS_TO_TIMESTAMP inverse: TIME_TO_MILLIS)","Inspect the query's projected type for __time and ensure the final expression is BIGINT"],"exampleFix":"// before\nINSERT INTO dst SELECT '2024-01-01T00:00:00' AS __time ... FROM src\n// after\nINSERT INTO dst SELECT TIME_TO_MILLIS(TIME_PARSE('2024-01-01T00:00:00')) AS __time ... FROM src","handlingStrategy":"type-guard","validationCode":"Object v = expr.eval(row);\nif (!(v instanceof Long)) { throw new IllegalArgumentException(\"__time must be BIGINT/LONG, got \" + (v == null ? \"null\" : v.getClass().getSimpleName())); }","typeGuard":"boolean isLongTimestamp(Object v) { return v instanceof Long; }","tryCatchPattern":"try { client.sqlQuery(insert); } catch (MSQException e) { if (e.getMessage().contains(\"Expected LONG\")) { rewriteQueryWithCast(); } else { throw e; } }","preventionTips":["Always CAST __time expressions to BIGINT in MSQ inserts","Use TIME_TO_MILLIS/UNIX_TIMESTAMP for string dates","EXPLAIN PLAN to verify projected column types"],"tags":["druid","msq","type-mismatch","sql"],"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"}