Projection Pushdown with Newly Added Columns Fails for Old Batches
Search before asking
- [x] I searched in the issues and found nothing similar.
Fluss version
0.9.0 (latest release)
Please describe the bug 🐞
Our system supports schema evolution: when a new column is added to the table schema, existing data files are not rewritten—instead, the query engine handles missing columns by injecting NULL or default values at read time (client-side compatibility). This works correctly in most scenarios.
However, when projection pushdown is applied and the query explicitly selects the newly added column, the scan operator may attempt to read the column directly from storage—despite its absence in old data files—leading to a failure because the column vector is missing in the batch.
Solution
No response
Are you willing to submit a PR?
- [ ] I'm willing to submit a PR!
@loserwang1024 Hi, I would like to work on this issue. Could you please assign it to me?
@Kaixuan-Duan Would you like to share your thoughts before that? The most important thing prior to it is how to fix it and whether a change in the RPC proto is needed.
@loserwang1024 Thanks for the feedback! Here's my proposed fix:
- No RPC proto change needed: The client already sends the expected projection indices.
- Server-side alignment: In
createProjectionInfo(), we splitselectedFieldPositionsinto existing fields and missing fields. Then inprojectSingleBatch(), we reconstruct the Arrow batch by copying existing vectors and generating all-NULL Arrow vectors for the missing columns at their correct target positions.
- Server-side alignment: In
createProjectionInfo(), we splitselectedFieldPositionsinto existing fields and missing fields. Then inprojectSingleBatch(), we reconstruct the Arrow batch by copying existing vectors and generating all-NULL Arrow vectors for the missing columns at their correct target positions.
@wuchong , I have been talked this with you , WDYT? (You have adviced that server only return existing fields (and with filed id), then append null in client side.)
We ran into this on Fluss 1.0.0 in a Flink batch read of a primary-key table (FlussSource with setBatch() and OffsetsInitializer.full(), no projection specified) shortly after ALTER TABLE ... ADD COLUMN:
InvalidColumnProjectionException: Projected fields [0, 1, ..., 118] is out of bound for schema with 118 fields.
It also reproduces on main (8a92b4f) with the test below:
org.apache.fluss.exception.FetchException: Unexpected error code INVALID_COLUMN_PROJECTION while fetching at offset 4 from bucket TableBucket{tableId=0, bucket=0}: org.apache.fluss.exception.InvalidColumnProjectionException: Projected fields [0, 1, 2] is out of bound for schema with 2
What happens
KvSnapshotAndLogBatchScannerbuilds aProjectionPlan. WhenprojectedFieldsis null,ProjectionPlan.createreturns all columns of the current schema ([0..N-1]), and the log scanner for the bounded log range is created with that projection. The fetch request is therefore sent withprojection_pushdown_enabled=true, although the caller asked for no projection.- On the server,
FileLogProjection#createProjectionInfovalidates the projection against the schema of each batch (toBitSet(arrowSchema.getFields().size(), ...)). Batches written before the ADD COLUMN have one column less, so the fetch fails. - It goes away once the next KV snapshot moves the log range past the old batches, so it only shows up within one
kv.snapshot.intervalafter a schema change.
The streaming path is not affected when no projection is requested: the log scanner sends no projection, and LogRecordReadContext#getOutputProjectedRow maps old-schema rows on the client.
Reproduction (fluss-client, added to BatchScannerITCase; fails on main with the error above)
@Test
void testSnapshotAndLogBatchScanAfterAddColumn() throws Exception {
TablePath tablePath = TablePath.of(DEFAULT_DB, "test-table-snapshot-log-add-column");
long tableId = createTable(tablePath, DEFAULT_TABLE_DESCRIPTOR, true);
// rows in the kv snapshot, written with the old schema
Map<TableBucket, List<InternalRow>> snapshotRows = putRows(tableId, tablePath, 10);
FLUSS_CLUSTER_EXTENSION.triggerAndWaitSnapshots(snapshotRows.keySet());
// rows in the changelog after the snapshot, also written with the old schema
List<InternalRow> logRows = new ArrayList<>();
for (int i = 10; i < 20; i++) {
logRows.add(compactedRow(DATA1_ROW_TYPE, new Object[] {i, "v" + i}));
}
putRows(tableId, tablePath, logRows);
admin.alterTable(
tablePath,
Collections.singletonList(
TableChange.addColumn(
"new_column",
DataTypes.BIGINT().copy(true),
null,
TableChange.ColumnPosition.last())),
false)
.get();
// No snapshot is taken after the column was added, so the bounded log range of each
// bucket still contains batches written with the old schema. Read it the way the Flink
// batch source does, without a projection.
KvSnapshots kvSnapshots = admin.getLatestKvSnapshots(tablePath).get();
Map<Integer, Long> logEndOffsets =
admin.listOffsets(
tablePath, kvSnapshots.getBucketIds(), new OffsetSpec.LatestSpec())
.all()
.get();
List<String> actualRows = new ArrayList<>();
try (Table table = conn.getTable(tablePath)) {
assertThat(table.getTableInfo().getSchema().getColumnNames()).contains("new_column");
for (int bucketId : kvSnapshots.getBucketIds()) {
OptionalLong snapshotId = kvSnapshots.getSnapshotId(bucketId);
BatchScanner scanner =
new KvSnapshotAndLogBatchScanner(
table,
new TableBucket(tableId, bucketId),
snapshotId.isPresent() ? snapshotId.getAsLong() : -1L,
snapshotId.isPresent()
? kvSnapshots.getLogOffset(bucketId).getAsLong()
: LogScanner.EARLIEST_OFFSET,
logEndOffsets.get(bucketId),
null);
for (InternalRow row : collectRows(scanner)) {
actualRows.add(
row.getInt(0)
+ ","
+ row.getString(1)
+ ","
+ (row.isNullAt(2) ? "null" : row.getLong(2)));
}
}
}
List<String> expectedRows = new ArrayList<>();
for (int i = 0; i < 20; i++) {
expectedRows.add(i + ",v" + i + ",null");
}
assertThat(actualRows).containsExactlyInAnyOrderElementsOf(expectedRows);
}
Possible fix for the no-projection case
Pushing down a projection of every column saves nothing, so the log range can be read without a projection when none is requested. The client then maps old-schema rows the same way the streaming path does:
int[] logProjectedFields = projectedFields == null ? null : projectionPlan.scanProjectedFields;
this.logScanner = table.newScan().project(logProjectedFields).createLogScanner();
With this change the test above passes, together with the rest of BatchScannerITCase and the batch scanner unit tests. An explicit projection that includes a new column still fails the same way; that case needs server-side support such as #2396. LakeSnapshotAndLogSplitScanner creates its log scanner from the same ProjectionPlan.