This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/dev/pr-13267-50e8a0d30db53b6be77509fee8873a2439716171 in repository https://gitbox.apache.org/repos/asf/seatunnel.git
commit c812d79ce156e98b3dc6f77861211b117025cedb Author: corgy-w <[email protected]> AuthorDate: Tue Nov 17 07:57:29 2026 +0000 [Fix][API] Reject an ADD COLUMN whose AFTER column is missing from the schema (#13267) --- .../handler/AlterTableSchemaEventHandler.java | 16 +++++ .../handler/AlterTableSchemaEventHandlerTest.java | 76 ++++++++++++++++++++++ 2 files changed, 92 insertions(+) diff --git a/seatunnel-api/src/main/java/org/apache/seatunnel/api/table/schema/handler/AlterTableSchemaEventHandler.java b/seatunnel-api/src/main/java/org/apache/seatunnel/api/table/schema/handler/AlterTableSchemaEventHandler.java index 627d2c8157..a00a595dc6 100644 --- a/seatunnel-api/src/main/java/org/apache/seatunnel/api/table/schema/handler/AlterTableSchemaEventHandler.java +++ b/seatunnel-api/src/main/java/org/apache/seatunnel/api/table/schema/handler/AlterTableSchemaEventHandler.java @@ -33,6 +33,8 @@ import org.apache.seatunnel.api.table.schema.event.AlterTableModifyColumnEvent; import org.apache.seatunnel.api.table.schema.event.AlterTableNameEvent; import org.apache.seatunnel.api.table.schema.event.RestoreTableSchemaEvent; import org.apache.seatunnel.api.table.schema.event.SchemaChangeEvent; +import org.apache.seatunnel.api.table.schema.exception.SchemaEvolutionErrorCode; +import org.apache.seatunnel.api.table.schema.exception.SchemaEvolutionException; import org.apache.seatunnel.api.table.type.SeaTunnelDataType; import java.util.Arrays; @@ -118,6 +120,20 @@ public class AlterTableSchemaEventHandler implements TableSchemaChangeEventHandl newColumns.addFirst(column); } else if (addColumnEvent.getAfterColumn() != null) { int index = originFields.indexOf(addColumnEvent.getAfterColumn()); + if (index < 0) { + // -1 + 1 == 0 would silently insert the column first, misaligning every positional + // mapping downstream. The event names a column this schema does not have, so it + // does not describe this table. + throw new SchemaEvolutionException( + SchemaEvolutionErrorCode.SCHEMA_EVENT_PROCESSING_FAILED, + String.format( + "Cannot add column '%s' after '%s': table %s has no such column," + + " existing columns: %s", + column.getName(), + addColumnEvent.getAfterColumn(), + addColumnEvent.tableIdentifier(), + originFields)); + } newColumns.add(index + 1, column); } else { newColumns.addLast(column); diff --git a/seatunnel-api/src/test/java/org/apache/seatunnel/api/table/schema/handler/AlterTableSchemaEventHandlerTest.java b/seatunnel-api/src/test/java/org/apache/seatunnel/api/table/schema/handler/AlterTableSchemaEventHandlerTest.java new file mode 100644 index 0000000000..891a3d3bed --- /dev/null +++ b/seatunnel-api/src/test/java/org/apache/seatunnel/api/table/schema/handler/AlterTableSchemaEventHandlerTest.java @@ -0,0 +1,76 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.seatunnel.api.table.schema.handler; + +import org.apache.seatunnel.api.table.catalog.PhysicalColumn; +import org.apache.seatunnel.api.table.catalog.TableIdentifier; +import org.apache.seatunnel.api.table.catalog.TableSchema; +import org.apache.seatunnel.api.table.schema.event.AlterTableAddColumnEvent; +import org.apache.seatunnel.api.table.schema.exception.SchemaEvolutionException; +import org.apache.seatunnel.api.table.type.BasicType; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +class AlterTableSchemaEventHandlerTest { + + private static final TableIdentifier TABLE = TableIdentifier.of("catalog", "db", "t"); + + @Test + void addColumnAfterExistingColumnKeepsItsPosition() { + TableSchema result = + new AlterTableSchemaEventHandler() + .reset(schema("a", "b")) + .apply(AlterTableAddColumnEvent.addAfter(TABLE, column("c"), "a")); + + Assertions.assertArrayEquals(new String[] {"a", "c", "b"}, result.getFieldNames()); + } + + /** + * The event names the column it must follow. When the schema has no such column the event does + * not describe this table, and inserting the column anywhere would silently misalign every + * positional row-to-schema mapping downstream, so it is rejected. + */ + @Test + void addColumnAfterUnknownColumnIsRejected() { + TableSchemaChangeEventHandler handler = + new AlterTableSchemaEventHandler().reset(schema("a", "b")); + + SchemaEvolutionException failure = + Assertions.assertThrows( + SchemaEvolutionException.class, + () -> + handler.apply( + AlterTableAddColumnEvent.addAfter( + TABLE, column("c"), "missing"))); + + Assertions.assertTrue(failure.getMessage().contains("missing"), failure.getMessage()); + } + + private static PhysicalColumn column(String name) { + return PhysicalColumn.of(name, BasicType.INT_TYPE, 0L, true, null, null); + } + + private static TableSchema schema(String... names) { + TableSchema.Builder builder = TableSchema.builder(); + for (String name : names) { + builder.column(column(name)); + } + return builder.build(); + } +}
