{"record":{"id":"dcab927504c5d82e","repo":"apache/iceberg","slug":"unknown-type-for-long-field-type-name-longva-dcab92","errorCode":null,"errorMessage":"Unknown type for long field. Type name: \" + longVal.getClass().getName()","messagePattern":"Unknown type for long field\\. Type name: \" \\+ longVal\\.getClass\\(\\)\\.getName\\(\\)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/source/StructInternalRow.java","lineNumber":153,"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":135,"sourceCodeEnd":171,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/source/StructInternalRow.java#L135-L171","documentation":"StructInternalRow.getLong converts the underlying Iceberg struct field to a Spark BIGINT/TIMESTAMP long. It supports Long, OffsetDateTime (timestamp), Duration-equivalent nanos, and LocalDate (date-to-epoch-day); any other runtime type triggers this IllegalStateException naming the actual class.","triggerScenarios":"Expected schema says LongType/TimestampType at the ordinal but the struct holds a different Java object (e.g. Integer, BigDecimal, LocalTime) — a mismatch between the declared read schema and the actual stored Java classes.","commonSituations":"Timestamp vs timestamp_ntz vs time mapping confusion; wrong schema passed to Spark scan; Iceberg version differences in the Java class chosen for timestamp types (LocalDateTime vs OffsetDateTime vs Long).","solutions":["Align the expected Spark schema with the table's actual field types (e.g. read a Timestamp column with getLong only if the producer yields OffsetDateTime)","Fix the producer/row wrapper to store the type Iceberg's conversion expects (Long for long, OffsetDateTime for timestamp, LocalDate for date)","Print/inspect the class name from the message to identify what is actually stored and adapt accordingly","Upgrade Iceberg if your version's timestamp Java mapping differs from the reader's expectations"],"exampleFix":"// before\nrow.getLong(tsOrdinal); // stored LocalTime -> IllegalStateException\n// after\nObject v = row.get(tsOrdinal, sparkType); // confirm stored class first\nlong nanos = v instanceof OffsetDateTime\n    ? Duration.between(Instant.EPOCH, (OffsetDateTime) v).toNanos() / 1000\n    : (Long) v;","handlingStrategy":"type-guard","validationCode":"Object v = struct.get(pos, javaClass);\nif (!(v instanceof Long) && !(v instanceof OffsetDateTime) && !(v instanceof LocalDate)) {\n  throw new IllegalStateException(\"expected Long/OffsetDateTime/LocalDate for long field, got \" + v.getClass());\n}","typeGuard":"boolean isLongBacked(Object v) {\n  return v instanceof Long || v instanceof OffsetDateTime || v instanceof LocalDate;\n}","tryCatchPattern":"try { long l = row.getLong(ordinal); } catch (IllegalStateException e) { /* read the class name from the message and correct schema/producer */ }","preventionTips":["Match timestamp field mappings (TimestampType vs LongType) exactly between producer and reader","Avoid writing Timestamp columns and reading them with a LongType expected schema (or vice versa)","Pin a single Iceberg version across the pipeline so Java-class mappings agree"],"tags":["spark","internalrow","type-mismatch","long","timestamp"],"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"}