beansandroasters opened a new pull request, #4639:
URL: https://github.com/apache/flink-cdc/pull/4639

   
   ## What is the purpose of this pull request?
   
   JIRA: https://issues.apache.org/jira/browse/FLINK-41773
   
   When a column is changed from `NOT NULL` to nullable upstream, the Iceberg 
sink keeps the Iceberg field `required`, and the first row that has a null 
value in that column stops the pipeline with a `NullPointerException`.
   
   ```sql
   CREATE TABLE users (id BIGINT PRIMARY KEY, name VARCHAR(255) NOT NULL);
   -- pipeline: MySQL source -> Iceberg sink, schema.change.behavior: evolve
   INSERT INTO users VALUES (1, 'alice');
   ALTER TABLE users MODIFY name VARCHAR(255) NULL;
   INSERT INTO users VALUES (2, NULL);
   ```
   
   The MySQL source emits `AlterColumnTypeEvent{name -> VARCHAR(255)}` 
(nullable). `IcebergMetadataApplier.applyAlterColumnType` only calls 
`UpdateSchema.updateColumn(name, type)`, which changes the primitive type but 
never the nullability, so `name` stays `required` in Iceberg. The writer's 
schema is updated to the nullable type, and writing row 2 fails:
   
   ```
   java.lang.NullPointerException
        at 
org.apache.iceberg.flink.data.FlinkParquetWriters$StringDataWriter.write(FlinkParquetWriters.java:345)
        at 
org.apache.iceberg.parquet.ParquetValueWriters$StructWriter.write(ParquetValueWriters.java:665)
        at org.apache.iceberg.parquet.ParquetWriter.add(ParquetWriter.java:138)
        at org.apache.iceberg.io.DataWriter.write(DataWriter.java:71)
   ```
   
   Expected: `name` becomes optional in Iceberg and the table contains `1, 
alice` and `2, null`.
   
   The same situation also occurs with `schema.change.behavior: lenient`, which 
turns a `DropColumnEvent` into an `AlterColumnTypeEvent` that makes the column 
nullable. This was reported in #4372, which was closed by its author without 
review; this PR takes the same direction and adds tests and the identifier 
field handling.
   
   Affected versions: 3.7.0 and master.
   
   ## Brief change log
   
   - `IcebergMetadataApplier.applyAlterColumnType`: when the new type is 
nullable and the current Iceberg field is required, call 
`UpdateSchema.makeColumnOptional`. Making a column optional is always a 
compatible change in Iceberg.
   - Identifier fields are not changed, because Iceberg requires identifier 
fields to be required.
   - Tightening a column from nullable to `NOT NULL` is not applied (it would 
require `allowIncompatibleChanges()` and existing data may contain nulls); 
behavior is unchanged in that direction.
   
   ---
   
   ## Verifying this change
   
   This change added tests and can be verified as follows:
   
   - Added unit test 
`IcebergWriterTest#testWriteNullAfterColumnBecomesNullable`: creates a table 
with a `NOT NULL` column, applies an `AlterColumnTypeEvent` that makes it 
nullable, asserts that the Iceberg field is optional and writes a row with a 
null value. It fails without the fix.
   - `mvn verify -pl 
flink-cdc-connect/flink-cdc-pipeline-connectors/flink-cdc-pipeline-connector-iceberg`
 (spotless, checkstyle, unit tests and `IcebergSinkITCase`) passes on JDK 11, 
both with the default Flink 1.20 profile and with `-Pflink2`.
   - Manually tested with a MySQL 8.0 container → Flink → Iceberg Hadoop 
catalog pipeline (Flink 1.20.3 and 2.2.0, parallelism 1 and 4) running the SQL 
above: without the fix the null rows never arrive (with Flink 1.20.3 the job 
log shows the `NullPointerException`), with the fix both rows are written. This 
end-to-end test is not part of this PR.
   
   ## Documentation
   
   - Does this pull request introduce a new feature? (no)
   - If yes, how is the feature documented? (not applicable)
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   - [X] Yes (please specify the tool below)
   
   Generated-by: Claude Code 2.1.300 (Claude Opus 5.5)
   


-- 
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