alpass163 commented on code in PR #17102:
URL: https://github.com/apache/iotdb/pull/17102#discussion_r2740373150


##########
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/schema/table/TsTable.java:
##########
@@ -217,42 +212,60 @@ public void addColumnSchema(final TsTableColumnSchema 
columnSchema) {
         });
   }
 
+  /**
+   * Renames a column in the table schema while strictly preserving its 
original ordinal position.
+   */
   public void renameColumnSchema(final String oldName, final String newName) {
     executeWrite(
         () -> {
-          // Ensures idempotency
-          if (columnSchemaMap.containsKey(oldName)) {
-            final TsTableColumnSchema schema = columnSchemaMap.remove(oldName);
-            final Map<String, String> oldProps = schema.getProps();
-            oldProps.computeIfAbsent(TreeViewSchema.ORIGINAL_NAME, k -> 
schema.getColumnName());
-            switch (schema.getColumnCategory()) {
-              case TAG:
-                columnSchemaMap.put(
-                    newName, new TagColumnSchema(newName, 
schema.getDataType(), oldProps));
-                break;
-              case FIELD:
-                columnSchemaMap.put(
-                    newName,
-                    new FieldColumnSchema(
-                        newName,
-                        schema.getDataType(),
-                        ((FieldColumnSchema) schema).getEncoding(),
-                        ((FieldColumnSchema) schema).getCompressor(),
-                        oldProps));
-                break;
-              case ATTRIBUTE:
-                columnSchemaMap.put(
-                    newName, new AttributeColumnSchema(newName, 
schema.getDataType(), oldProps));
-                break;
-              case TIME:
-              default:
-                // Do nothing
-                columnSchemaMap.put(oldName, schema);
+          if (!columnSchemaMap.containsKey(oldName)) {
+            return;
+          }
+
+          // Capture the current strict order of current columns
+          List<Map.Entry<String, TsTableColumnSchema>> snapshotOfColumns =
+              new ArrayList<>(columnSchemaMap.entrySet());
+          columnSchemaMap.clear();
+
+          // Re-insert all entries in their original sequence, substituting 
the renamed column at
+          // its exact original index
+          for (Map.Entry<String, TsTableColumnSchema> entry : 
snapshotOfColumns) {
+            String currentKey = entry.getKey();
+            TsTableColumnSchema currentColumnSchema = entry.getValue();
+
+            if (currentKey.equals(oldName)) {
+              TsTableColumnSchema newSchema = 
createRenamedSchema(currentColumnSchema, newName);
+              columnSchemaMap.put(newName, newSchema);
+            } else {
+              columnSchemaMap.put(currentKey, currentColumnSchema);
             }
           }
         });

Review Comment:
   fixed it



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