{"record":{"id":"c50b0e1526aec5f9","repo":"apache/iceberg","slug":"unknown-type-for-long-field-type-name-longval-c50b0e","errorCode":null,"errorMessage":"Unknown type for long field. Type name: ${longVal.getClass().getName()}","messagePattern":"Unknown type for long field\\. Type name: (.+?)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"spark/v3.5/spark/src/main/java/org/apache/iceberg/spark/source/StructInternalRow.java","lineNumber":147,"sourceCode":"      return (int) ((LocalDate) integer).toEpochDay();\n    } else {\n      throw new IllegalStateException(\n          \"Unknown type for int field. Type name: \" + integer.getClass().getName());\n    }\n  }\n\n  @Override\n  public long getLong(int ordinal) {\n    Object longVal = struct.get(ordinal, Object.class);\n\n    if (longVal instanceof Long) {\n      return (long) longVal;\n    } else if (longVal instanceof OffsetDateTime) {\n      return Duration.between(Instant.EPOCH, (OffsetDateTime) longVal).toNanos() / 1000;\n    } else if (longVal instanceof LocalDate) {\n      return ((LocalDate) longVal).toEpochDay();\n    } else {\n      throw new IllegalStateException(\n          \"Unknown type for long field. Type name: \" + longVal.getClass().getName());\n    }\n  }\n\n  @Override\n  public float getFloat(int ordinal) {\n    return struct.get(ordinal, Float.class);\n  }\n\n  @Override\n  public double getDouble(int ordinal) {\n    return struct.get(ordinal, Double.class);\n  }\n\n  @Override\n  public Decimal getDecimal(int ordinal, int precision, int scale) {\n    return isNullAt(ordinal) ? null : getDecimalInternal(ordinal);\n  }","sourceCodeStart":129,"sourceCodeEnd":165,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/spark/v3.5/spark/src/main/java/org/apache/iceberg/spark/source/StructInternalRow.java#L129-L165","documentation":"StructInternalRow.getLong converts a StructLike value to a Spark long, accepting Long, OffsetDateTime (timestamptz -> microseconds since epoch), LocalDate (date -> epoch day), Timestamp, and LocalDateTime. Any other object type throws IllegalStateException with the actual Java class name, meaning the physical value doesn't match the declared long-backed Iceberg type.","triggerScenarios":"A column declared as long/timestamp/timestamptz/date whose stored StructLike value is none of the expected types (e.g. Integer, BigDecimal, or a wrongly-typed object from a custom producer).","commonSituations":"Custom record producers putting wrong types in StructLike; schema evolution mismatch; mis-declared partition/type mapping between Iceberg and Spark; bugs in timestamp conversion paths.","solutions":["Verify the Iceberg column type (long vs timestamp vs timestamptz) matches what the reader actually stores","Use the class name in the message to find the code path writing the unexpected object","Fix the producer to store Long, OffsetDateTime, Timestamp, LocalDateTime, or LocalDate","Convert the value manually via get(ordinal, null) if you control the call site"],"exampleFix":"// before\nlong v = row.getLong(ordinal); // value is Integer\n// after\nObject raw = row.get(ordinal, null);\nlong v = raw instanceof Integer ? ((Integer) raw).longValue() : row.getLong(ordinal);","handlingStrategy":"type-guard","validationCode":"Object v = row.get(ordinal, null);\nif (!(v instanceof Long || v instanceof OffsetDateTime || v instanceof LocalDate\n    || v instanceof Timestamp || v instanceof LocalDateTime)) {\n  throw new IllegalStateException(\"Unexpected long-field value: \" + v.getClass());\n}","typeGuard":"boolean isLongCompatible(Object v) { return v instanceof Long || v instanceof OffsetDateTime\n    || v instanceof LocalDate || v instanceof Timestamp || v instanceof LocalDateTime; }","tryCatchPattern":"try {\n  return row.getLong(ordinal);\n} catch (IllegalStateException e) {\n  throw new IllegalStateException(\"Long/timestamp field held unexpected Java type\", e);\n}","preventionTips":["Store timestamps as OffsetDateTime/Timestamp and dates as LocalDate in StructLike","Match producer conversion to Iceberg column type","Add unit tests over timestamp/date columns"],"tags":["spark","type-mismatch","timestamp","internalrow"],"backgroundTag":"type-mismatch","analyzedSha":"86d9c8fc543e7c56c9f624eb725f76c9baff9570","analyzedAt":"2026-09-12T00:46:39.097Z","contentChangedAt":"2026-09-12T00:46:39.097Z","schemaVersion":2},"datasetVersion":"2026-09-14T21:17:11.552Z"}