{"record":{"id":"a4f82a8497dca601","repo":"apache/iceberg","slug":"unknown-type-for-int-field-type-name-a4f82a","errorCode":null,"errorMessage":"Unknown type for int field. Type name: ","messagePattern":"Unknown type for int 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":140,"sourceCode":"  public byte getByte(int ordinal) {\n    return (byte) (int) struct.get(ordinal, Integer.class);\n  }\n\n  @Override\n  public short getShort(int ordinal) {\n    return (short) (int) struct.get(ordinal, Integer.class);\n  }\n\n  @Override\n  public int getInt(int ordinal) {\n    Object integer = struct.get(ordinal, Object.class);\n\n    if (integer instanceof Integer) {\n      return (int) integer;\n    } else if (integer instanceof LocalDate) {\n      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    }","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/spark/v4.2/spark/src/main/java/org/apache/iceberg/spark/source/StructInternalRow.java#L122-L158","documentation":"StructInternalRow.getInt validates that the underlying value for an int-typed field is an Integer or LocalDate before converting; any other runtime type triggers this IllegalStateException naming the actual Java class. It guards the unsafe cast from the StructLike value to Spark's int representation.","triggerScenarios":"Reading an int/Date field whose stored value is neither Integer nor LocalDate — e.g. schema drift where the Iceberg schema declares int but the row's value object is a different type (Long, String, etc.).","commonSituations":"Schema mismatches between the declared Spark/Iceberg schema and actual data, corrupt or wrongly converted rows, or custom data sources producing unexpected boxed types.","solutions":["Align the requested Spark schema with the actual Iceberg schema so int fields hold Integer values","Check for schema evolution drift (e.g. field promoted to long) and refresh the table schema/cache","Log the offending field and type name from the message, then fix the producer that writes the wrong type"],"exampleFix":"// before\nrow.getInt(dateFieldOrdinal); // throws: value is Long\n// after\nSchema updated = SparkSchemaUtil.convert(sparkSchema); // ensure types align\nif (value instanceof Long) { /* reload with corrected schema */ }","handlingStrategy":"validation","validationCode":"// Java\nObject v = struct.getField(ordinal);\nif (!(v instanceof Integer) && !(v instanceof LocalDate)) {\n  throw new IllegalStateException(\"Expected Integer/LocalDate, got \" + v.getClass());\n}","typeGuard":"boolean isValidIntFieldValue(Object v) {\n  return v instanceof Integer || v instanceof LocalDate;\n}","tryCatchPattern":"try {\n  int i = row.getInt(ordinal);\n} catch (IllegalStateException e) {\n  if (e.getMessage().startsWith(\"Unknown type for int field\")) {\n    // reload with a corrected schema matching the data\n  }\n  throw e;\n}","preventionTips":["Keep Spark schema in sync with the Iceberg table schema","Watch for type promotions (int->long) during schema evolution","Test schema conversions with SparkSchemaUtil.convert"],"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"}