{"record":{"id":"a0aee6116657d395","repo":"apache/iceberg","slug":"not-a-primitive-type-this","errorCode":null,"errorMessage":"Not a primitive type: + this","messagePattern":"Not a primitive type: \\+ this","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"api/src/main/java/org/apache/iceberg/types/Type.java","lineNumber":73,"sourceCode":"    private final Class<?> javaClass;\n\n    TypeID(Class<?> javaClass) {\n      this.javaClass = javaClass;\n    }\n\n    public Class<?> javaClass() {\n      return javaClass;\n    }\n  }\n\n  TypeID typeId();\n\n  default boolean isPrimitiveType() {\n    return false;\n  }\n\n  default PrimitiveType asPrimitiveType() {\n    throw new IllegalArgumentException(\"Not a primitive type: \" + this);\n  }\n\n  default Types.StructType asStructType() {\n    throw new IllegalArgumentException(\"Not a struct type: \" + this);\n  }\n\n  default Types.ListType asListType() {\n    throw new IllegalArgumentException(\"Not a list type: \" + this);\n  }\n\n  default Types.MapType asMapType() {\n    throw new IllegalArgumentException(\"Not a map type: \" + this);\n  }\n\n  default Types.VariantType asVariantType() {\n    throw new IllegalArgumentException(\"Not a variant type: \" + this);\n  }\n","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/api/src/main/java/org/apache/iceberg/types/Type.java#L55-L91","documentation":"Type.asPrimitiveType() is a narrowing convenience on Iceberg's Type interface. The default implementation throws IllegalArgumentException because most Type instances (struct, list, map, variant) are not primitives. Only PrimitiveType subtypes override it to return this.","triggerScenarios":"Calling asPrimitiveType() on any Type that is not a PrimitiveType, e.g. type.asPrimitiveType() after finding a field whose result type is a StructType or ListType, or on TypeUtil.visit results typed as Type.","commonSituations":"Generic schema-walking code that assumes all columns are primitives (Avro/Parquet-style flat schemas); partition or metrics code that blindly narrows field types; migrating code from engines where all column types were primitive.","solutions":["Check type.isPrimitiveType() (or instanceof Type.PrimitiveType) before calling asPrimitiveType().","Dispatch via a TypeVisitor (TypeUtil.visit) instead of manual narrowing.","Fix logic that selected the wrong field/type — e.g. iterate only primitive fields when narrowing."],"exampleFix":"// before\nTypes.NestedField f = schema.findField(\"location\");\nType.PrimitiveType p = f.type().asPrimitiveType();\n// after\nTypes.NestedField f = schema.findField(\"location\");\nif (f.type().isPrimitiveType()) {\n  Type.PrimitiveType p = f.type().asPrimitiveType();\n}","handlingStrategy":"type-guard","validationCode":"Preconditions.checkArgument(type.isPrimitiveType(), \"Expected primitive, got: %s\", type);","typeGuard":"if (type.isPrimitiveType()) {\n  Type.PrimitiveType p = type.asPrimitiveType();\n}","tryCatchPattern":"try {\n  Type.PrimitiveType p = type.asPrimitiveType();\n} catch (IllegalArgumentException e) {\n  // handle non-primitive branch\n}","preventionTips":["Check isPrimitiveType() before every asPrimitiveType() call.","Prefer TypeVisitor dispatch over manual type narrowing.","Don't assume flat schemas — Iceberg types are arbitrarily nested."],"tags":["types","iceberg","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"86d9c8fc543e7c56c9f624eb725f76c9baff9570","analyzedAt":"2026-09-12T00:46:39.097Z","contentChangedAt":"2026-09-12T00:46:39.097Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}