{"record":{"id":"bd46fcee534b24aa","repo":"prestodb/presto","slug":"new-column-does-not-have-same-number-of-rows-as-ol","errorCode":null,"errorMessage":"New column does not have same number of rows as old column","messagePattern":"New column does not have same number of rows as old column","errorType":"exception","errorClass":"java.lang.IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"presto-common/src/main/java/com/facebook/presto/common/Page.java","lineNumber":469,"sourceCode":"                    retainedSizeInBytes.addAndGet(size);\n                }\n            });\n        }\n        this.retainedSizeInBytes = retainedSizeInBytes.longValue();\n        return retainedSizeInBytes.longValue();\n    }\n\n    /**\n     * Returns a new page with the same columns as the original page except for the one column replaced.\n     *\n     * @param channelIndex the column to replace\n     * @param column the replacement column\n     * @return a new page with the replacement column substituted for the old column\n     */\n    public Page replaceColumn(int channelIndex, Block column)\n    {\n        if (column.getPositionCount() != positionCount) {\n            throw new IllegalArgumentException(\"New column does not have same number of rows as old column\");\n        }\n\n        Block[] newBlocks = Arrays.copyOf(blocks, blocks.length);\n        newBlocks[channelIndex] = column;\n        return Page.wrapBlocksWithoutCopy(positionCount, newBlocks);\n    }\n\n    private static class DictionaryBlockIndexes\n    {\n        private final List<DictionaryBlock> blocks = new ArrayList<>();\n        private final List<Integer> indexes = new ArrayList<>();\n\n        public void addBlock(DictionaryBlock block, int index)\n        {\n            blocks.add(block);\n            indexes.add(index);\n        }\n","sourceCodeStart":451,"sourceCodeEnd":487,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-common/src/main/java/com/facebook/presto/common/Page.java#L451-L487","documentation":"Page.replaceColumn() builds a new Page by substituting one Block for another at a given channel index. The library throws this IllegalArgumentException when the replacement column's position count differs from the page's positionCount, because a Page requires all its channels (blocks) to have the same number of rows; otherwise the resulting page would be internally inconsistent.","triggerScenarios":"Calling page.replaceColumn(channelIndex, block) where block.getPositionCount() != page.getPositionCount() — e.g. appending or filtering rows in the new block without rebuilding the whole page.","commonSituations":"Operators/processors that transform a single column (e.g. adding a row-number column, coercing a column) and produce a Block with a different row count than the source page; off-by-one bugs when generating synthetic columns; reuse of a stale block from a different page.","solutions":["Ensure the replacement Block has exactly the same position count as the source page before calling replaceColumn (compare block.getPositionCount() with page.getPositionCount()).","If the transformation changes row counts, rebuild the entire page with PageBuilder instead of replacing a single column.","Verify the block was produced from the same page/input rows, not from a different or stale dataset.","Double-check channelIndex points at the intended channel (wrong index won't cause this error, but is a related replaceColumn pitfall)."],"exampleFix":"// before\nBlock rowNumbers = buildRowNumbers(allInputRows); // count != page.getPositionCount()\npage.replaceColumn(2, rowNumbers);\n// after\nif (rowNumbers.getPositionCount() != page.getPositionCount()) {\n    throw new IllegalStateException(\"column row count must match page row count\");\n}\npage.replaceColumn(2, rowNumbers);","handlingStrategy":"validation","validationCode":"if (block.getPositionCount() != page.getPositionCount()) {\n    throw new IllegalArgumentException(\"replacement column has \" + block.getPositionCount()\n        + \" rows but page has \" + page.getPositionCount());\n}\npage.replaceColumn(channelIndex, block);","typeGuard":"boolean isSameRowCount(Block block, Page page) {\n    return block.getPositionCount() == page.getPositionCount();\n}","tryCatchPattern":"try {\n    Page newPage = page.replaceColumn(channelIndex, column);\n} catch (IllegalArgumentException e) {\n    // fall back to rebuilding the page via PageBuilder\n    newPage = rebuildPage(page, channelIndex, column);\n}","preventionTips":["Always assert block.getPositionCount() == page.getPositionCount() before replaceColumn.","Derive replacement blocks from the same source page/input rather than unrelated data.","If row counts can change, use PageBuilder to construct a fresh page instead of column substitution.","Unit-test column transformations with row-count-changing inputs."],"tags":["presto","page","block","row-count-mismatch"],"backgroundTag":"row-count-mismatch","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"}