corgy-w opened a new pull request, #13267:
URL: https://github.com/apache/seatunnel/pull/13267

   ### What
   
   `AlterTableSchemaEventHandler` now rejects an `ADD COLUMN ... AFTER 
<column>` event whose anchor column is not in the schema, instead of silently 
inserting the new column at position 0.
   
   ### Why
   
   `applyAddColumn` looks the anchor up and adds right after it:
   
   ```java
   } else if (addColumnEvent.getAfterColumn() != null) {
       int index = originFields.indexOf(addColumnEvent.getAfterColumn());
       newColumns.add(index + 1, column);          // index == -1  ->  add(0, 
column)
   }
   ```
   
   When the schema has no column with that name, `indexOf` returns `-1`, `-1 + 
1` is `0`, and `LinkedList.add(0, column)` **prepends** the column. Nothing 
throws and nothing is logged, so the schema change looks applied while the 
column order now disagrees with the event and with the physical table. Every 
positional row-to-schema mapping downstream (`TableSchemaChangeEventDispatcher` 
is used by the JDBC, Doris, Paimon, StarRocks, Databend and Elasticsearch sink 
writers) is then off by one for that table.
   
   The anchor name comes from the source and is not validated: the MySQL CDC 
alter-table parser passes the raw `AFTER` identifier, and the SQL Server and 
Postgres resolvers do the same, so a renamed, projected-away or 
differently-cased anchor reaches this code. The sibling branch 
`applyModifyColumn` already treats an unknown `AFTER` column as a hard failure 
(`Optional.get()`), so the two paths contradicted each other.
   
   ### Fix
   
   Treat an unknown anchor as an invalid event for this schema and fail loudly:
   
   ```java
   int index = originFields.indexOf(addColumnEvent.getAfterColumn());
   if (index < 0) {
       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);
   ```
   
   The message names the table, the anchor and the existing columns, so the 
mismatch is diagnosable from the job log.
   
   ### How verified
   
   New `AlterTableSchemaEventHandlerTest` (no external services):
   
   - `addColumnAfterExistingColumnKeepsItsPosition` — `[a, b]` + `ADD c AFTER 
a` → `[a, c, b]` (guards the normal path);
   - `addColumnAfterUnknownColumnIsRejected` — `[a, b]` + `ADD c AFTER missing` 
must throw `SchemaEvolutionException`.
   
   Results:
   
   - before the fix: `AssertionFailedError: Expected SchemaEvolutionException 
to be thrown, but nothing was thrown` (the schema came back as `[c, a, b]`);
   - after the fix: `Tests run: 5, Failures: 0, Errors: 0` (`mvn -o -pl 
seatunnel-api test 
-Dtest='AlterTableSchemaEventHandlerTest,RestoreTableSchemaEventTest'`), 
including the existing restore tests.
   
   ### Compatibility
   
   No public API or option change. The behaviour change is that an `ADD COLUMN 
... AFTER` event naming an unknown column now fails the schema change instead 
of corrupting the row layout; `ADD COLUMN` (append), `ADD COLUMN FIRST` and 
`ADD COLUMN AFTER <existing>` are unchanged.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to