{"record":{"id":"7bfcc1777f2a0509","repo":"apache/iceberg","slug":"cannot-set-fields-in-a-typeprojection","errorCode":null,"errorMessage":"Cannot set fields in a TypeProjection","messagePattern":"Cannot set fields in a TypeProjection","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"api/src/main/java/org/apache/iceberg/util/StructProjection.java","lineNumber":224,"sourceCode":"    if (nestedProjections[pos] != null) {\n      StructLike nestedStruct = struct.get(structPos, StructLike.class);\n      if (nestedStruct == null) {\n        return null;\n      }\n\n      return javaClass.cast(nestedProjections[pos].wrap(nestedStruct));\n    }\n\n    if (structPos != -1) {\n      return struct.get(structPos, javaClass);\n    } else {\n      return null;\n    }\n  }\n\n  @Override\n  public <T> void set(int pos, T value) {\n    throw new UnsupportedOperationException(\"Cannot set fields in a TypeProjection\");\n  }\n}\n","sourceCodeStart":206,"sourceCodeEnd":227,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/api/src/main/java/org/apache/iceberg/util/StructProjection.java#L206-L227","documentation":"StructProjection is a read-only StructLike view over projected positions of another struct. set(pos, value) is intentionally unsupported because a projection never owns mutable data — it just re-maps positions onto an underlying struct.","triggerScenarios":"Calling set(int, T) on any StructProjection instance, e.g. mutating a projected row returned by a scan or produced by project().","commonSituations":"Code that treats a StructLike generically and tries to write into it; reusing a writer that expects a mutable record but was handed a projected read-only row.","solutions":["Write to the underlying source StructLike instead of the projection","Wrap the data in a mutable record type (e.g. Record or GenericDataUtil-backed) before setting values","Restructure code so projections are only used on the read path"],"exampleFix":"// before\nStructLike projected = StructProjection.create(type, ids);\nprojected.set(0, value);\n// after\nRecord mutable = GenericRecord.create(type);\nmutable.set(0, value);\nStructLike projected = StructProjection.create(type, ids).wrap(mutable);","handlingStrategy":"type-guard","validationCode":"if (structLike instanceof StructProjection) {\n  throw new IllegalStateException(\"cannot mutate a read-only projection\");\n}","typeGuard":"boolean isMutable = !(s instanceof StructProjection);","tryCatchPattern":"try {\n  structLike.set(pos, value);\n} catch (UnsupportedOperationException e) {\n  // mutate the underlying record instead of the projection\n}","preventionTips":["Treat StructProjection as strictly read-only","Mutate the source StructLike/Record, then wrap it with the projection","Avoid passing projections into APIs that write to StructLike"],"tags":["read-only","unsupported-operation","structlike"],"backgroundTag":"unsupported-operation","analyzedSha":"86d9c8fc543e7c56c9f624eb725f76c9baff9570","analyzedAt":"2026-09-12T00:46:39.097Z","contentChangedAt":"2026-09-12T00:46:39.097Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}