This is an automated email from the ASF dual-hosted git repository.

chenyz 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 022bd455f89 Fix fetch nested device with template will miss some 
devices (#11597)
022bd455f89 is described below

commit 022bd455f89b1f75609fa2a8ec488a8b567768fa
Author: Chen YZ <[email protected]>
AuthorDate: Thu Nov 23 20:38:23 2023 +0800

    Fix fetch nested device with template will miss some devices (#11597)
    
    Fix fetch nested device with template will miss some devices
---
 .../common/schematree/ClusterSchemaTree.java       | 27 ++++++++++++-------
 .../schemaRegion/SchemaRegionTemplateTest.java     | 30 ++++++++++++++++++++++
 2 files changed, 48 insertions(+), 9 deletions(-)

diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/common/schematree/ClusterSchemaTree.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/common/schematree/ClusterSchemaTree.java
index 317d0473a0b..df96e149b73 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/common/schematree/ClusterSchemaTree.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/common/schematree/ClusterSchemaTree.java
@@ -280,21 +280,30 @@ public class ClusterSchemaTree implements ISchemaTree {
     String[] nodes = devicePath.getNodes();
     SchemaNode cur = root;
     SchemaNode child;
-    for (int i = 1; i < nodes.length; i++) {
+    for (int i = 1; i < nodes.length - 1; i++) {
       child = cur.getChild(nodes[i]);
       if (child == null) {
-        if (i == nodes.length - 1) {
-          SchemaEntityNode entityNode = new SchemaEntityNode(nodes[i]);
-          entityNode.setAligned(isAligned);
-          entityNode.setTemplateId(templateId);
-          child = entityNode;
-        } else {
-          child = new SchemaInternalNode(nodes[i]);
-        }
+        child = new SchemaInternalNode(nodes[i]);
         cur.addChild(nodes[i], child);
       }
       cur = child;
     }
+    String deviceName = nodes[nodes.length - 1];
+    child = cur.getChild(deviceName);
+    if (child == null) {
+      SchemaEntityNode entityNode = new SchemaEntityNode(deviceName);
+      entityNode.setAligned(isAligned);
+      entityNode.setTemplateId(templateId);
+      cur.addChild(deviceName, entityNode);
+    } else if (child.isEntity()) {
+      child.getAsEntityNode().setTemplateId(templateId);
+      child.getAsEntityNode().setAligned(isAligned);
+    } else {
+      SchemaEntityNode entityNode = new SchemaEntityNode(deviceName);
+      entityNode.setAligned(isAligned);
+      entityNode.setTemplateId(templateId);
+      cur.replaceChild(deviceName, entityNode);
+    }
     templateMap.putIfAbsent(templateId, template);
   }
 
diff --git 
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/metadata/schemaRegion/SchemaRegionTemplateTest.java
 
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/metadata/schemaRegion/SchemaRegionTemplateTest.java
index 38c7037a8b4..f30c6d1a355 100644
--- 
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/metadata/schemaRegion/SchemaRegionTemplateTest.java
+++ 
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/metadata/schemaRegion/SchemaRegionTemplateTest.java
@@ -23,6 +23,7 @@ import org.apache.iotdb.commons.path.MeasurementPath;
 import org.apache.iotdb.commons.path.PartialPath;
 import org.apache.iotdb.commons.path.PathPatternTree;
 import org.apache.iotdb.db.queryengine.common.schematree.ClusterSchemaTree;
+import org.apache.iotdb.db.queryengine.common.schematree.DeviceSchemaInfo;
 import org.apache.iotdb.db.schemaengine.schemaregion.ISchemaRegion;
 import 
org.apache.iotdb.db.schemaengine.schemaregion.read.resp.info.ISchemaInfo;
 import 
org.apache.iotdb.db.schemaengine.schemaregion.read.resp.info.ITimeSeriesSchemaInfo;
@@ -55,6 +56,35 @@ public class SchemaRegionTemplateTest extends 
AbstractSchemaRegionTest {
     super(testParams);
   }
 
+  @Test
+  public void testFetchNestedTemplateDevice() throws Exception {
+    ISchemaRegion schemaRegion = getSchemaRegion("root.sg", 0);
+    int templateId = 1;
+    Template template =
+        new Template(
+            "t1",
+            Arrays.asList("s1", "s2"),
+            Arrays.asList(TSDataType.DOUBLE, TSDataType.INT32),
+            Arrays.asList(TSEncoding.RLE, TSEncoding.RLE),
+            Arrays.asList(CompressionType.SNAPPY, CompressionType.SNAPPY));
+    template.setId(templateId);
+    schemaRegion.activateSchemaTemplate(
+        SchemaRegionWritePlanFactory.getActivateTemplateInClusterPlan(
+            new PartialPath("root.sg.d1"), 2, templateId),
+        template);
+    schemaRegion.activateSchemaTemplate(
+        SchemaRegionWritePlanFactory.getActivateTemplateInClusterPlan(
+            new PartialPath("root.sg.d1.GPS"), 3, templateId),
+        template);
+    ClusterSchemaTree schemaTree =
+        schemaRegion.fetchSchema(
+            ALL_MATCH_SCOPE, Collections.singletonMap(templateId, template), 
true);
+    Assert.assertEquals(2, schemaTree.getAllDevices().size());
+    for (DeviceSchemaInfo deviceSchemaInfo : schemaTree.getAllDevices()) {
+      Assert.assertEquals(templateId, deviceSchemaInfo.getTemplateId());
+    }
+  }
+
   /** Test {@link ISchemaRegion#activateSchemaTemplate}. */
   @Test
   public void testActivateSchemaTemplate() throws Exception {

Reply via email to