2121import org .apache .fluss .metadata .Schema ;
2222import org .apache .fluss .metadata .TableChange ;
2323import org .apache .fluss .metadata .TableInfo ;
24- import org .apache .fluss .types .DataType ;
25- import org .apache .fluss .types .ReassignFieldId ;
2624
27- import java .util .ArrayList ;
28- import java .util .HashMap ;
2925import java .util .List ;
30- import java .util .Map ;
3126import java .util .Objects ;
32- import java .util .concurrent .atomic .AtomicInteger ;
3327
3428/** Schema update. */
3529public class SchemaUpdate {
3630
37- /** Apply schema changes to the given table info and return the updated schema. */
3831 public static Schema applySchemaChanges (TableInfo tableInfo , List <TableChange > changes ) {
3932 SchemaUpdate schemaUpdate = new SchemaUpdate (tableInfo );
4033 for (TableChange change : changes ) {
@@ -43,36 +36,16 @@ public static Schema applySchemaChanges(TableInfo tableInfo, List<TableChange> c
4336 return schemaUpdate .getSchema ();
4437 }
4538
46- private final List <Schema .Column > columns ;
47- private final AtomicInteger highestFieldId ;
48- private final List <String > primaryKeys ;
49- private final Map <String , Schema .Column > existedColumns ;
50- private final List <String > autoIncrementColumns ;
39+ // Now we only maintain the Builder
40+ private final Schema .Builder builder ;
5141
5242 public SchemaUpdate (TableInfo tableInfo ) {
53- this .columns = new ArrayList <>();
54- this .existedColumns = new HashMap <>();
55- this .highestFieldId = new AtomicInteger (tableInfo .getSchema ().getHighestFieldId ());
56- this .primaryKeys = tableInfo .getPrimaryKeys ();
57- this .autoIncrementColumns = tableInfo .getSchema ().getAutoIncrementColumnNames ();
58- this .columns .addAll (tableInfo .getSchema ().getColumns ());
59- for (Schema .Column column : columns ) {
60- existedColumns .put (column .getName (), column );
61- }
43+ // Initialize builder from the current table schema
44+ this .builder = Schema .newBuilder ().fromSchema (tableInfo .getSchema ());
6245 }
6346
6447 public Schema getSchema () {
65- Schema .Builder builder =
66- Schema .newBuilder ()
67- .fromColumns (columns )
68- .highestFieldId ((short ) highestFieldId .get ());
69- if (!primaryKeys .isEmpty ()) {
70- builder .primaryKey (primaryKeys );
71- }
72- for (String autoIncrementColumn : autoIncrementColumns ) {
73- builder .enableAutoIncrement (autoIncrementColumn );
74- }
75-
48+ // Validation and building are now delegated
7649 return builder .build ();
7750 }
7851
@@ -91,9 +64,10 @@ public SchemaUpdate applySchemaChange(TableChange columnChange) {
9164 }
9265
9366 private SchemaUpdate addColumn (TableChange .AddColumn addColumn ) {
94- Schema .Column existingColumn = existedColumns .get (addColumn .getName ());
67+ // Use the builder to check if column exists
68+ Schema .Column existingColumn = builder .getColumn (addColumn .getName ()).orElse (null );
69+
9570 if (existingColumn != null ) {
96- // Allow idempotent retries: if column name/type/comment match existing, treat as no-op
9771 if (!existingColumn .getDataType ().equals (addColumn .getDataType ())
9872 || !Objects .equals (
9973 existingColumn .getComment ().orElse (null ), addColumn .getComment ())) {
@@ -103,8 +77,7 @@ private SchemaUpdate addColumn(TableChange.AddColumn addColumn) {
10377 return this ;
10478 }
10579
106- TableChange .ColumnPosition position = addColumn .getPosition ();
107- if (position != TableChange .ColumnPosition .last ()) {
80+ if (addColumn .getPosition () != TableChange .ColumnPosition .last ()) {
10881 throw new IllegalArgumentException ("Only support addColumn column at last now." );
10982 }
11083
@@ -113,13 +86,15 @@ private SchemaUpdate addColumn(TableChange.AddColumn addColumn) {
11386 "Column " + addColumn .getName () + " must be nullable." );
11487 }
11588
116- int columnId = highestFieldId .incrementAndGet ();
117- DataType dataType = ReassignFieldId .reassign (addColumn .getDataType (), highestFieldId );
89+ // Delegate the actual addition to the builder
90+ builder .column (addColumn .getName (), addColumn .getDataType ());
91+
92+ // Fixed: Use null check for the String comment
93+ String comment = addColumn .getComment ();
94+ if (comment != null ) {
95+ builder .withComment (comment );
96+ }
11897
119- Schema .Column newColumn =
120- new Schema .Column (addColumn .getName (), dataType , addColumn .getComment (), columnId );
121- columns .add (newColumn );
122- existedColumns .put (newColumn .getName (), newColumn );
12398 return this ;
12499 }
125100
0 commit comments