This is an automated email from the ASF dual-hosted git repository.
jt2594838 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git
The following commit(s) were added to refs/heads/master by this push:
new eefcdd21ecb Fixed the renaming bug of table view (#17577)
eefcdd21ecb is described below
commit eefcdd21ecb8ca02d3acb04e273bb5a1d0c51311
Author: Caideyipi <[email protected]>
AuthorDate: Thu May 7 11:42:08 2026 +0800
Fixed the renaming bug of table view (#17577)
---
.../table/TsTableRenameColumnSchemaTest.java | 60 ++++++++++++++++++++++
.../apache/iotdb/commons/schema/table/TsTable.java | 35 +++++++++----
2 files changed, 85 insertions(+), 10 deletions(-)
diff --git
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/schemaengine/table/TsTableRenameColumnSchemaTest.java
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/schemaengine/table/TsTableRenameColumnSchemaTest.java
new file mode 100644
index 00000000000..799e10511b8
--- /dev/null
+++
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/schemaengine/table/TsTableRenameColumnSchemaTest.java
@@ -0,0 +1,60 @@
+/*
+ * 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.iotdb.db.schemaengine.table;
+
+import org.apache.iotdb.commons.schema.table.TreeViewSchema;
+import org.apache.iotdb.commons.schema.table.TsTable;
+import org.apache.iotdb.commons.schema.table.column.FieldColumnSchema;
+import org.apache.iotdb.commons.schema.table.column.TagColumnSchema;
+
+import org.apache.tsfile.enums.TSDataType;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TsTableRenameColumnSchemaTest {
+
+ @Test
+ public void testRenameColumnSchemaMovesMapKeyAndKeepsIdempotency() {
+ final TsTable table = new TsTable("test_table");
+ table.addColumnSchema(new FieldColumnSchema("old_field",
TSDataType.DOUBLE));
+
+ table.renameColumnSchema("old_field", "new_field");
+ table.renameColumnSchema("old_field", "new_field");
+
+ Assert.assertNull(table.getColumnSchema("old_field"));
+ Assert.assertNotNull(table.getColumnSchema("new_field"));
+ Assert.assertEquals("new_field",
table.getColumnSchema("new_field").getColumnName());
+ Assert.assertEquals(
+ "old_field",
TreeViewSchema.getOriginalName(table.getColumnSchema("new_field")));
+ }
+
+ @Test
+ public void testRenameTagColumnKeepsTagOrdinal() {
+ final TsTable table = new TsTable("test_table");
+ table.addColumnSchema(new TagColumnSchema("tag_1", TSDataType.STRING));
+ table.addColumnSchema(new TagColumnSchema("tag_2", TSDataType.STRING));
+
+ table.renameColumnSchema("tag_1", "renamed_tag");
+
+ Assert.assertEquals(-1, table.getTagColumnOrdinal("tag_1"));
+ Assert.assertEquals(0, table.getTagColumnOrdinal("renamed_tag"));
+ Assert.assertEquals(1, table.getTagColumnOrdinal("tag_2"));
+ }
+}
diff --git
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/schema/table/TsTable.java
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/schema/table/TsTable.java
index 4a82bdd70a0..19cf03d764c 100644
---
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/schema/table/TsTable.java
+++
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/schema/table/TsTable.java
@@ -230,16 +230,31 @@ public class TsTable {
}
public void renameColumnSchema(final String oldName, final String newName) {
- executeWrite(
- () -> {
- // Ensures idempotency
- if (columnSchemaMap.containsKey(oldName)) {
- final TsTableColumnSchema schema = columnSchemaMap.get(oldName);
- final Map<String, String> oldProps = schema.getProps();
- oldProps.computeIfAbsent(TreeViewSchema.ORIGINAL_NAME, k ->
schema.getColumnName());
- schema.setColumnName(newName);
- }
- });
+ executeWrite(() -> renameColumnSchemaInternal(oldName, newName));
+ }
+
+ protected void renameColumnSchemaInternal(final String oldName, final String
newName) {
+ if (Objects.equals(oldName, newName)
+ || !columnSchemaMap.containsKey(oldName) &&
columnSchemaMap.containsKey(newName)) {
+ return;
+ }
+
+ final TsTableColumnSchema schema = columnSchemaMap.remove(oldName);
+ if (Objects.isNull(schema)) {
+ return;
+ }
+
+ final Map<String, String> oldProps = schema.getProps();
+ oldProps.computeIfAbsent(TreeViewSchema.ORIGINAL_NAME, k ->
schema.getColumnName());
+ schema.setColumnName(newName);
+ columnSchemaMap.put(newName, schema);
+
+ if (TsTableColumnCategory.TAG.equals(schema.getColumnCategory())) {
+ final Integer ordinal = tagColumnIndexMap.remove(oldName);
+ if (Objects.nonNull(ordinal)) {
+ tagColumnIndexMap.put(newName, ordinal);
+ }
+ }
}
public void removeColumnSchema(final String columnName) {