prestodb/presto · error · java.lang.IllegalArgumentException

Column does not have same position count (%s) as page (%s)

Error message

Column does not have same position count (%s) as page (%s)

What it means

Page.prependColumn adds a Block as the first channel of the page; all channels in a Page must have the same number of positions. If the new column's position count differs from the page's positionCount, the resulting Page would be malformed, so the library throws IllegalArgumentException.

Source

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

    {
        return wrapBlocksWithoutCopy(positionCount, new Block[] {this.blocks[channel]});
    }

    public Page extractChannels(int[] channels)
    {
        requireNonNull(channels, "channels is null");

        Block[] blocks = new Block[channels.length];
        for (int i = 0; i < channels.length; i++) {
            blocks[i] = this.blocks[channels[i]];
        }
        return wrapBlocksWithoutCopy(positionCount, blocks);
    }

    public Page prependColumn(Block column)
    {
        if (column.getPositionCount() != positionCount) {
            throw new IllegalArgumentException(String.format("Column does not have same position count (%s) as page (%s)", column.getPositionCount(), positionCount));
        }

        Block[] result = new Block[blocks.length + 1];
        result[0] = column;
        System.arraycopy(blocks, 0, result, 1, blocks.length);

        return wrapBlocksWithoutCopy(positionCount, result);
    }

    public Page dropColumn(int channelIndex)
    {
        if (channelIndex < 0 || channelIndex >= getChannelCount()) {
            throw new IndexOutOfBoundsException(format("Invalid channel %d in page with %s channels", channelIndex, getChannelCount()));
        }

        Block[] result = new Block[getChannelCount() - 1];
        System.arraycopy(blocks, 0, result, 0, channelIndex);
        System.arraycopy(blocks, channelIndex + 1, result, channelIndex, getChannelCount() - channelIndex - 1);

View on GitHub (pinned to 55bb57d202)

Solutions

  1. Recompute the column so it has exactly page.getPositionCount() positions before prepending
  2. If the page was filtered, filter/rebuild the column with the same retainedPositions before prepending
  3. Sanity-check the order of operations: prepend before filtering, or apply the same mask to both

Example fix

// before
Page filtered = page.getPositions(retained, 0, retained.length);
filtered.prependColumn(rowIds); // rowIds sized for original page -> throws
// after
Block sizedRowIds = rowIds.getPositions(retained, 0, retained.length);
filtered.prependColumn(sizedRowIds);
Defensive patterns

Strategy: validation

Validate before calling

if (column.getPositionCount() != page.getPositionCount()) {
    throw new IllegalArgumentException("column rows " + column.getPositionCount()
        + " != page rows " + page.getPositionCount());
}
Page result = page.prependColumn(column);

Type guard

boolean canPrepend(Page page, Block column) {
    return column.getPositionCount() == page.getPositionCount();
}

Try / catch

try {
    page = page.prependColumn(column);
} catch (IllegalArgumentException e) {
    if (!e.getMessage().startsWith("Column does not have same position count")) throw e;
    column = column.getPositions(retainedPositions, 0, retainedPositions.length);
    page = page.prependColumn(column);
}

Prevention

When it happens

Trigger: Calling page.prependColumn(block) where block.getPositionCount() != page.getPositionCount() — e.g. prepending row-number or group-id columns computed over a different number of rows.

Common situations: Operators like MarkDistinct/Window that build group id or row id columns; test code (testPrependColumnWrongNumberOfRows) constructing mismatched blocks; bugs where a filter earlier reduced page rows but not the new column.

Related errors


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