prestodb/presto · error · java.lang.IllegalArgumentException

New column does not have same number of rows as old column

Error message

New column does not have same number of rows as old column

What it means

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.

Source

Thrown at presto-common/src/main/java/com/facebook/presto/common/Page.java:469

                    retainedSizeInBytes.addAndGet(size);
                }
            });
        }
        this.retainedSizeInBytes = retainedSizeInBytes.longValue();
        return retainedSizeInBytes.longValue();
    }

    /**
     * Returns a new page with the same columns as the original page except for the one column replaced.
     *
     * @param channelIndex the column to replace
     * @param column the replacement column
     * @return a new page with the replacement column substituted for the old column
     */
    public Page replaceColumn(int channelIndex, Block column)
    {
        if (column.getPositionCount() != positionCount) {
            throw new IllegalArgumentException("New column does not have same number of rows as old column");
        }

        Block[] newBlocks = Arrays.copyOf(blocks, blocks.length);
        newBlocks[channelIndex] = column;
        return Page.wrapBlocksWithoutCopy(positionCount, newBlocks);
    }

    private static class DictionaryBlockIndexes
    {
        private final List<DictionaryBlock> blocks = new ArrayList<>();
        private final List<Integer> indexes = new ArrayList<>();

        public void addBlock(DictionaryBlock block, int index)
        {
            blocks.add(block);
            indexes.add(index);
        }

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Ensure the replacement Block has exactly the same position count as the source page before calling replaceColumn (compare block.getPositionCount() with page.getPositionCount()).
  2. If the transformation changes row counts, rebuild the entire page with PageBuilder instead of replacing a single column.
  3. Verify the block was produced from the same page/input rows, not from a different or stale dataset.
  4. Double-check channelIndex points at the intended channel (wrong index won't cause this error, but is a related replaceColumn pitfall).

Example fix

// before
Block rowNumbers = buildRowNumbers(allInputRows); // count != page.getPositionCount()
page.replaceColumn(2, rowNumbers);
// after
if (rowNumbers.getPositionCount() != page.getPositionCount()) {
    throw new IllegalStateException("column row count must match page row count");
}
page.replaceColumn(2, rowNumbers);
Defensive patterns

Strategy: validation

Validate before calling

if (block.getPositionCount() != page.getPositionCount()) {
    throw new IllegalArgumentException("replacement column has " + block.getPositionCount()
        + " rows but page has " + page.getPositionCount());
}
page.replaceColumn(channelIndex, block);

Type guard

boolean isSameRowCount(Block block, Page page) {
    return block.getPositionCount() == page.getPositionCount();
}

Try / catch

try {
    Page newPage = page.replaceColumn(channelIndex, column);
} catch (IllegalArgumentException e) {
    // fall back to rebuilding the page via PageBuilder
    newPage = rebuildPage(page, channelIndex, column);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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