prestodb/presto · error · IllegalStateException

currentFieldIndexToWrite is not valid

Error message

currentFieldIndexToWrite is not valid

What it means

checkFieldIndexToWrite also validates that the sequential write cursor (currentFieldIndexToWrite) has not advanced past the number of field block builders in the row, i.e. the row has more sequential entries written than it has fields. This is an IllegalArgumentException-free IllegalStateException thrown when sequential writes overflow the row's field count.

Source

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

    @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. Verify the number of sequential writes matches the field count of the row type.
  2. Regenerate or update the encoder after any schema change to the row type.
  3. Add an assertion in the caller comparing planned field count with the writer's fieldBlockBuilders length before writing.

Example fix

// before
for (Object v : values) { writer.appendStructure(...); } // values larger than row arity
// after
checkState(values.size() == fieldCount, "row arity mismatch");
for (Object v : values) { writer.appendStructure(...); }
Defensive patterns

Strategy: validation

Validate before calling

checkState(plannedFieldCount == expectedRowArity, "sequential writes exceed row field count");

Try / catch

try {
    writer.appendStructure(value);
} catch (IllegalStateException e) {
    // too many writes: schema/encoder mismatch, fail fast with schema info
}

Prevention

When it happens

Trigger: Performing more sequential writes (writeByte/writeShort/writeInt/writeLong/writeBytes/appendStructure) than the row has fields; e.g. appending an extra entry to a row whose type declares fewer fields.

Common situations: Serializer/schema mismatch: the RowType changed (column added/removed) but the encoder still writes the old number of values; miscounted fields in dynamic row construction.

Related errors


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