prestodb/presto · error · IllegalStateException

cannot do sequential write after getFieldBlockBuilder is cal

Error message

cannot do sequential write after getFieldBlockBuilder is called

What it means

checkFieldIndexToWrite enforces the reverse rule of error 410: after getFieldBlockBuilder hands out a field block builder, sequential writes are forbidden. Any sequential write method (writeByte, writeShort, writeInt, writeLong, writeBytes, appendStructure) throws IllegalStateException because mixing modes would corrupt field offsets.

Source

Thrown at presto-common/src/main/java/com/facebook/presto/common/block/SingleRowBlockWriter.java:263

    {
        throw new UnsupportedOperationException();
    }

    @Override
    public String toString()
    {
        if (!fieldBlockBuilderReturned) {
            return format("SingleRowBlockWriter(%d){numFields=%d, fieldBlockBuilderReturned=false, positionCount=%d}", hashCode(), fieldBlockBuilders.length, getPositionCount());
        }
        else {
            return format("SingleRowBlockWriter(%d){numFields=%d, fieldBlockBuilderReturned=true}", hashCode(), fieldBlockBuilders.length);
        }
    }

    private void checkFieldIndexToWrite()
    {
        if (fieldBlockBuilderReturned) {
            throw new IllegalStateException("cannot do sequential write after getFieldBlockBuilder is called");
        }
        if (currentFieldIndexToWrite >= fieldBlockBuilders.length) {
            throw new IllegalStateException("currentFieldIndexToWrite is not valid");
        }
    }
}

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Write every field through the obtained field block builders once getFieldBlockBuilder has been used.
  2. Choose exactly one write mode per row writer before serializing.
  3. Reset by completing the current row and creating a new SingleRowBlockWriter.

Example fix

// before
BlockBuilder f = writer.getFieldBlockBuilder(0);
writer.writeLong(1, value); // throws
// after
BlockBuilder f = writer.getFieldBlockBuilder(0);
BlockBuilder f1 = writer.getFieldBlockBuilder(1);
f1.writeLong(value);
Defensive patterns

Strategy: validation

Validate before calling

checkState(!fieldBuildersObtained, "use field builders for all writes once obtained");

Try / catch

try {
    writer.writeLong(idx, value);
} catch (IllegalStateException e) {
    // sequential write after getFieldBlockBuilder: route through field builder
}

Prevention

When it happens

Trigger: Calling writeByte/writeShort/writeInt/writeLong/writeBytes/appendStructure on a SingleRowBlockWriter after getFieldBlockBuilder() has been called on that writer instance.

Common situations: Type serializers that take the field-builder path for one field but fall back to sequential writes for another field within the same row; generic serialization code that checks types at runtime and mixes both APIs.

Related errors


AI-assisted analysis of prestodb/presto@55bb57d202 (2026-09-04). Data as JSON: /api/errors/cc8cb65c5ad3d732. Report an issue: GitHub.