{"record":{"id":"89b2d8cabc046842","repo":"apache/iceberg","slug":"unknown-type-for-long-field-type-name-89b2d8","errorCode":null,"errorMessage":"Unknown type for long field. Type name: ","messagePattern":"Unknown type for long field\\. Type name: ","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/source/StructInternalRow.java","lineNumber":156,"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":138,"sourceCodeEnd":174,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/source/StructInternalRow.java#L138-L174","documentation":"StructInternalRow.getLong converts the underlying value of a long/timestamp field to a Java long, accepting Long, OffsetDateTime (timestamptz), and LocalDate (date-as-days). Any other runtime type throws this IllegalStateException with the actual class name.","triggerScenarios":"Reading a long/Timestamp/Timestamptz field whose stored value is none of Long, OffsetDateTime, or LocalDate — e.g. LocalTime for a time field read through the wrong accessor or a String/BigDecimal in the row.","commonSituations":"Schema drift between the Iceberg schema and the Spark read schema, custom StructLike values with unexpected boxed types, or microsecond/nanosecond conversion code invoked on fields of the wrong declared type.","solutions":["Ensure the accessor matches the declared Iceberg type (getLong only for long/timestamptz/date semantics)","Reconcile schema drift: refresh the table schema and Spark conversion so field types match the data","Inspect the type name in the message and fix the producer writing that wrong type"],"exampleFix":"// before\nrow.getLong(timeFieldOrdinal); // throws: value is LocalTime\n// after\n// time fields must be routed to getTime or converted upstream\nlong nanos = ((LocalTime) value).toNanoOfDay();","handlingStrategy":"validation","validationCode":"// Java\nObject v = struct.getField(ordinal);\nif (!(v instanceof Long) && !(v instanceof OffsetDateTime) && !(v instanceof LocalDate)) {\n  throw new IllegalStateException(\"Expected Long/OffsetDateTime/LocalDate, got \" + v.getClass());\n}","typeGuard":"boolean isValidLongFieldValue(Object v) {\n  return v instanceof Long || v instanceof OffsetDateTime || v instanceof LocalDate;\n}","tryCatchPattern":"try {\n  long l = row.getLong(ordinal);\n} catch (IllegalStateException e) {\n  if (e.getMessage().startsWith(\"Unknown type for long field\")) {\n    // use the correct accessor for the field's declared type\n  }\n  throw e;\n}","preventionTips":["Use getLong only for long/timestamptz/date fields","Route time fields to the proper accessor (getTime) or convert upstream","Refresh cached schemas after table evolution"],"tags":["spark","type-mismatch","schema"],"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"}