{"record":{"id":"a01ceddfa5355cc5","repo":"prestodb/presto","slug":"current-entry-must-be-closed-before-a-null-can-be","errorCode":null,"errorMessage":"Current entry must be closed before a null can be written","messagePattern":"Current entry must be closed before a null can be written","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"presto-common/src/main/java/com/facebook/presto/common/block/Int128ArrayBlockBuilder.java","lineNumber":107,"sourceCode":"            throw new IllegalStateException(\"Expected entry size to be exactly \" + INT128_BYTES + \" bytes but was \" + (entryPositionCount * SIZE_OF_LONG));\n        }\n\n        positionCount++;\n        entryPositionCount = 0;\n        if (blockBuilderStatus != null) {\n            blockBuilderStatus.addBytes(Byte.BYTES + INT128_BYTES);\n        }\n        return this;\n    }\n\n    @Override\n    public BlockBuilder appendNull()\n    {\n        if (valueIsNull.length <= positionCount) {\n            growCapacity();\n        }\n        if (entryPositionCount != 0) {\n            throw new IllegalStateException(\"Current entry must be closed before a null can be written\");\n        }\n\n        valueIsNull[positionCount] = true;\n\n        hasNullValue = true;\n        positionCount++;\n        if (blockBuilderStatus != null) {\n            blockBuilderStatus.addBytes(Byte.BYTES + INT128_BYTES);\n        }\n        return this;\n    }\n\n    @Override\n    public Block build()\n    {\n        if (!hasNonNullValue) {\n            return new RunLengthEncodedBlock(NULL_VALUE_BLOCK, positionCount);\n        }","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-common/src/main/java/com/facebook/presto/common/block/Int128ArrayBlockBuilder.java#L89-L125","documentation":"Int128ArrayBlockBuilder builds variable-width 128-bit integer entries via beginEntry/appendNull/closeEntry. A null (appendNull) is itself a complete entry, so calling appendNull while an entry is still open (entryPositionCount != 0) would corrupt the block layout; the builder throws IllegalStateException to force the caller to close the current entry first.","triggerScenarios":"Calling appendNull() after beginEntry() and writing partial value bytes without calling closeEntry(), or calling appendNull() directly inside an open entry instead of via writeByte/append structure. Typical call path: readPositionFrom or a block-building loop that begins an entry then switches to appendNull.","commonSituations":"Hand-rolled block copy/conversion code that mixes beginEntry/appendNull incorrectly; serializers that call appendNull unconditionally without tracking entry state; copying positions between blocks where nulls and values share one code path.","solutions":["Call closeEntry() (or ensure the prior entry was closed) before invoking appendNull().","Restructure copying logic so null positions use appendNull() only when no entry is open, and value positions use beginEntry/writeBytes/closeEntry.","If wrapping this builder, expose appendNull only when the builder is not inside an entry (check entryPositionCount == 0)."],"exampleFix":"// before\nblockBuilder.beginEntry();\nblockBuilder.appendNull(); // IllegalStateException\n// after\nblockBuilder.beginEntry();\nblockBuilder.closeEntry();\nblockBuilder.appendNull();","handlingStrategy":"validation","validationCode":"if (blockBuilder instanceof Int128ArrayBlockBuilder) {\n    // ensure no entry is open before appending a null\n    blockBuilder.closeEntry(); // or track entry state in your wrapper\n}\nblockBuilder.appendNull();","typeGuard":"boolean canAppendNull(BlockBuilder b) { return b.getPositionCount() == 0 || b instanceof Int128ArrayBlockBuilder; }","tryCatchPattern":"try {\n    builder.appendNull();\n} catch (IllegalStateException e) {\n    builder.closeEntry();\n    builder.appendNull();\n}","preventionTips":["Never interleave beginEntry/write with appendNull; treat nulls as standalone entries","Centralize block copying in one utility that tracks entry state","Prefer writeNull-style APIs on higher-level writers when available"],"tags":["presto","block-builder","illegal-state","int128"],"backgroundTag":"block-builder-entry-not-closed","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"}