apache/iceberg · error · UncheckedIOException

Failed to serialize sort key

Error message

Failed to serialize sort key

What it means

SortKeySketchSerializer.serializeToByteArray(SortKey) writes a single SortKey to a DataOutputSerializer to feed the Apache DataSketches reservoir. If the underlying write throws IOException (serialization backend failure, output buffer problem), it is wrapped in UncheckedIOException("Failed to serialize sort key"). It converts checked I/O exceptions from the low-level serializer into runtime exceptions for the sketch API.

Source

Thrown at flink/v2.2/flink/src/main/java/org/apache/iceberg/flink/sink/shuffle/SortKeySketchSerializer.java:69

  SortKeySketchSerializer(TypeSerializer<SortKey> itemSerializer) {
    this.itemSerializer = itemSerializer;
    this.listSerializer = new ListSerializer<>(itemSerializer);
    this.input = new DataInputDeserializer();
  }

  @Override
  public byte[] serializeToByteArray(SortKey item) {
    try {
      DataOutputSerializer output = new DataOutputSerializer(DEFAULT_SORT_KEY_SIZE);
      itemSerializer.serialize(item, output);
      byte[] itemBytes = output.getSharedBuffer();
      int numBytes = output.length();
      byte[] out = new byte[numBytes + Integer.BYTES];
      ByteArrayUtil.copyBytes(itemBytes, 0, out, 4, numBytes);
      ByteArrayUtil.putIntLE(out, 0, numBytes);
      return out;
    } catch (IOException e) {
      throw new UncheckedIOException("Failed to serialize sort key", e);
    }
  }

  @Override
  public byte[] serializeToByteArray(SortKey[] items) {
    try {
      DataOutputSerializer output = new DataOutputSerializer(DEFAULT_SORT_KEY_SIZE * items.length);
      listSerializer.serialize(Arrays.asList(items), output);
      byte[] itemsBytes = output.getSharedBuffer();
      int numBytes = output.length();
      byte[] out = new byte[Integer.BYTES + numBytes];
      ByteArrayUtil.putIntLE(out, 0, numBytes);
      System.arraycopy(itemsBytes, 0, out, Integer.BYTES, numBytes);
      return out;
    } catch (IOException e) {
      throw new UncheckedIOException("Failed to serialize sort key", e);
    }
  }

View on GitHub (pinned to 86d9c8fc54)

Solutions

  1. Inspect the IOException cause (UncheckedIOException.getCause()) to identify the underlying buffer/IO failure.
  2. Reduce sort key size: avoid sorting on very large string/binary columns; prefix or hash long values before sorting.
  3. Increase JVM heap/taskmanager memory if buffer growth failures stem from memory pressure.
  4. Verify the table schema types of sort columns match expected primitives (no broken conversion logic).

Example fix

// before: sorting on huge binary column
ALTER TABLE t WRITE ORDERED BY payload;  -- binary, tens of MB
// after
ALTER TABLE t WRITE ORDERED BY payload_hash;
Defensive patterns

Strategy: try-catch

Validate before calling

// pre-check key size before handing to sketch
if (estimatedSortKeySize(key) > MAX_KEY_BYTES) {
  key = hashKey(key);
}

Try / catch

try {
  byte[] bytes = serializer.serializeToByteArray(key);
} catch (UncheckedIOException e) {
  LOG.error("Sort key serialization failed", e.getCause());
  throw e;
}

Prevention

When it happens

Trigger: The sketch calls serializeToByteArray during toByteArray/sizeOf while serializing a SortKey and DataOutputSerializer.write throws IOException — typically due to buffer growth failures (e.g. negative or overflowed sizes) or memory issues for very large field values.

Common situations: Extremely large string/binary sort key values causing huge intermediate buffers; JVM memory pressure / OOM conditions inside the output stream; custom types with faulty conversion producing oversized output.

Understand the failure class

Background: "JSON serialization failed", "not JSON serializable", "Failed to serialize": why JSON marshaling errors happen and how to fix them — this error's family across 46 libraries.

Related errors


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