{"record":{"id":"8c27001963578628","repo":"prestodb/presto","slug":"current-entry-must-be-closed-before-the-block-can","errorCode":null,"errorMessage":"Current entry must be closed before the block can be built","messagePattern":"Current entry must be closed before the block can be built","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"presto-common/src/main/java/com/facebook/presto/common/block/RowBlockBuilder.java","lineNumber":251,"sourceCode":"        hasNullRow |= isNull;\n        positionCount++;\n\n        for (int i = 0; i < numFields; i++) {\n            if (fieldBlockBuilders[i].getPositionCount() != fieldBlockOffsets[positionCount]) {\n                throw new IllegalStateException(format(\"field %s has unexpected position count. Expected: %s, actual: %s\", i, fieldBlockOffsets[positionCount], fieldBlockBuilders[i].getPositionCount()));\n            }\n        }\n\n        if (blockBuilderStatus != null) {\n            blockBuilderStatus.addBytes(Integer.BYTES + Byte.BYTES);\n        }\n    }\n\n    @Override\n    public Block build()\n    {\n        if (currentEntryOpened) {\n            throw new IllegalStateException(\"Current entry must be closed before the block can be built\");\n        }\n        Block[] fieldBlocks = new Block[numFields];\n        for (int i = 0; i < numFields; i++) {\n            fieldBlocks[i] = fieldBlockBuilders[i].build();\n        }\n        return createRowBlockInternal(0, positionCount, hasNullRow ? rowIsNull : null, fieldBlockOffsets, fieldBlocks);\n    }\n\n    @Override\n    public String toString()\n    {\n        return format(\"RowBlockBuilder(%d){numFields=%d, positionCount=%d\", hashCode(), numFields, getPositionCount());\n    }\n\n    @Override\n    public BlockBuilder appendStructure(Block block)\n    {\n        if (!(block instanceof AbstractSingleRowBlock)) {","sourceCodeStart":233,"sourceCodeEnd":269,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-common/src/main/java/com/facebook/presto/common/block/RowBlockBuilder.java#L233-L269","documentation":"RowBlockBuilder.build() materializes the final RowBlock from the accumulated field blocks and null flags. A still-open entry means the last row is incomplete: its field values have not been committed and offsets are not finalized. The library throws IllegalStateException rather than emit a partially-written row.","triggerScenarios":"Calling build() after beginBlockEntry() without calling closeEntry(); early exception/return between beginBlockEntry and closeEntry leaving the entry open, then a later build() attempt.","commonSituations":"Page processors or operators that build row-typed columns and abort mid-row on exception; code that appends structure (appendStructure opens an entry) and then builds before closing.","solutions":["Call closeEntry() before build()","Wrap entry building in try/finally to guarantee closeEntry()","After appendStructure, remember it leaves the entry open and close it before building","On abort, discard the builder instead of reusing it"],"exampleFix":"// before\nrowBuilder.beginBlockEntry();\n// ... write fields\nBlock block = rowBuilder.build(); // IllegalStateException: entry open\n// after\nrowBuilder.beginBlockEntry();\n// ... write fields\nrowBuilder.closeEntry();\nBlock block = rowBuilder.build();","handlingStrategy":"try-catch","validationCode":"// track entry state yourself when wrapping the builder\nclass GuardedRowBuilder {\n    private boolean entryOpen;\n    void beforeBuild() { checkState(!entryOpen, \"close the current row entry before build()\"); }\n}\n","typeGuard":null,"tryCatchPattern":"try {\n    return rowBuilder.build();\n} catch (IllegalStateException e) {\n    if (e.getMessage().contains(\"Current entry must be closed before the block can be built\")) {\n        // close the dangling entry and retry once\n        rowBuilder.closeEntry();\n        return rowBuilder.build();\n    }\n    throw e;\n}","preventionTips":["Always close entries before any terminal operation (build, getRegion, appendNull)","Use try/finally so exceptions mid-row cannot leave entries open","Discard builders after failed entry writes instead of reusing them"],"tags":["presto","row-block","illegal-state","block-builder"],"backgroundTag":"block-entry-still-open","analyzedSha":"55bb57d202de3b926896fa966c2c4a44c779634e","analyzedAt":"2026-09-04T12:50:26.162Z","contentChangedAt":"2026-09-04T12:50:26.162Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}