apache/iceberg · error · UnsupportedOperationException

Cannot set fields in a TypeProjection

Error message

Cannot set fields in a TypeProjection

What it means

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.

Source

Thrown at api/src/main/java/org/apache/iceberg/util/StructProjection.java:224

    if (nestedProjections[pos] != null) {
      StructLike nestedStruct = struct.get(structPos, StructLike.class);
      if (nestedStruct == null) {
        return null;
      }

      return javaClass.cast(nestedProjections[pos].wrap(nestedStruct));
    }

    if (structPos != -1) {
      return struct.get(structPos, javaClass);
    } else {
      return null;
    }
  }

  @Override
  public <T> void set(int pos, T value) {
    throw new UnsupportedOperationException("Cannot set fields in a TypeProjection");
  }
}

View on GitHub (pinned to 86d9c8fc54)

Solutions

  1. Write to the underlying source StructLike instead of the projection
  2. Wrap the data in a mutable record type (e.g. Record or GenericDataUtil-backed) before setting values
  3. Restructure code so projections are only used on the read path

Example fix

// before
StructLike projected = StructProjection.create(type, ids);
projected.set(0, value);
// after
Record mutable = GenericRecord.create(type);
mutable.set(0, value);
StructLike projected = StructProjection.create(type, ids).wrap(mutable);
Defensive patterns

Strategy: type-guard

Validate before calling

if (structLike instanceof StructProjection) {
  throw new IllegalStateException("cannot mutate a read-only projection");
}

Type guard

boolean isMutable = !(s instanceof StructProjection);

Try / catch

try {
  structLike.set(pos, value);
} catch (UnsupportedOperationException e) {
  // mutate the underlying record instead of the projection
}

Prevention

When it happens

Trigger: Calling set(int, T) on any StructProjection instance, e.g. mutating a projected row returned by a scan or produced by project().

Common situations: 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.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of apache/iceberg@86d9c8fc54 (2026-09-12). Data as JSON: /api/errors/7bfcc1777f2a0509. Report an issue: GitHub.