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

jiangtian pushed a commit to branch dev/1.3
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/dev/1.3 by this push:
     new 78da8c7ee58 [To dev/1.3] Fixed the bug that the template may create 
measurements under timeSeries && Optimized the logger for meta user exceptions 
#17158 (#17160)
78da8c7ee58 is described below

commit 78da8c7ee5866bbccaeda974b57125c0bbccbae6
Author: Caideyipi <[email protected]>
AuthorDate: Wed Feb 4 17:26:30 2026 +0800

    [To dev/1.3] Fixed the bug that the template may create measurements under 
timeSeries && Optimized the logger for meta user exceptions #17158 (#17160)
    
    * Fixed the bug that the template may create measurements under timeSeries 
&& Optimized the logger for meta user exceptions (#17158)
    
    * fix
    
    * fix
    
    * shop
    
    * mm
    
    * while-fix
    
    * fix
    
    * sptls
---
 .../iotdb/db/it/schema/IoTDBSchemaTemplateIT.java  | 17 +++++
 .../plan/planner/LogicalPlanBuilder.java           |  8 +--
 .../plan/planner/LogicalPlanVisitor.java           |  6 +-
 .../metedata/read/SeriesSchemaFetchScanNode.java   |  4 +-
 .../mtree/impl/mem/MTreeBelowSGMemoryImpl.java     | 74 +++++++++++++---------
 .../mtree/impl/pbtree/MTreeBelowSGCachedImpl.java  | 60 +++++++++++-------
 .../apache/iotdb/db/utils/ErrorHandlingUtils.java  |  2 +
 7 files changed, 108 insertions(+), 63 deletions(-)

diff --git 
a/integration-test/src/test/java/org/apache/iotdb/db/it/schema/IoTDBSchemaTemplateIT.java
 
b/integration-test/src/test/java/org/apache/iotdb/db/it/schema/IoTDBSchemaTemplateIT.java
index ecc16af137c..ee1018db950 100644
--- 
a/integration-test/src/test/java/org/apache/iotdb/db/it/schema/IoTDBSchemaTemplateIT.java
+++ 
b/integration-test/src/test/java/org/apache/iotdb/db/it/schema/IoTDBSchemaTemplateIT.java
@@ -853,4 +853,21 @@ public class IoTDBSchemaTemplateIT extends 
AbstractSchemaIT {
       }
     }
   }
+
+  @Test
+  public void testTemplatePathConflict() throws Exception {
+    try (final Connection connection = EnvFactory.getEnv().getConnection();
+        final Statement statement = connection.createStatement()) {
+      // set device template
+      statement.execute("SET DEVICE TEMPLATE t1 TO root.sg1");
+      // activate device template
+      statement.execute("create timeSeries using device template on 
root.sg1.d1");
+      try {
+        statement.execute("create timeSeries using device template on 
root.sg1.d1.s1");
+        fail();
+      } catch (final Exception e) {
+        Assert.assertEquals("506: Path [root.sg1.d1.s1] already exist", 
e.getMessage());
+      }
+    }
+  }
 }
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/LogicalPlanBuilder.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/LogicalPlanBuilder.java
index 81afda469c3..401e5393aac 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/LogicalPlanBuilder.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/LogicalPlanBuilder.java
@@ -1087,21 +1087,21 @@ public class LogicalPlanBuilder {
       boolean withAttributes,
       boolean withTemplate,
       boolean withAliasForce) {
-    PartialPath storageGroupPath;
+    PartialPath databasePath;
     for (String storageGroup : storageGroupList) {
       try {
-        storageGroupPath = new PartialPath(storageGroup);
+        databasePath = new PartialPath(storageGroup);
         PathPatternTree overlappedPatternTree = new PathPatternTree();
         for (PartialPath pathPattern :
             patternTree.getOverlappedPathPatterns(
-                storageGroupPath.concatNode(MULTI_LEVEL_PATH_WILDCARD))) {
+                databasePath.concatNode(MULTI_LEVEL_PATH_WILDCARD))) {
           // pathPattern has been deduplicated, no need to deduplicate again
           overlappedPatternTree.appendFullPath(pathPattern);
         }
         this.root.addChild(
             new SeriesSchemaFetchScanNode(
                 context.getQueryId().genPlanNodeId(),
-                storageGroupPath,
+                databasePath,
                 overlappedPatternTree,
                 templateMap,
                 withTags,
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/LogicalPlanVisitor.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/LogicalPlanVisitor.java
index 50f43f364fa..ee2ccc30586 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/LogicalPlanVisitor.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/LogicalPlanVisitor.java
@@ -785,12 +785,12 @@ public class LogicalPlanVisitor extends 
StatementVisitor<PlanNode, MPPQueryConte
   public PlanNode visitSeriesSchemaFetch(
       SeriesSchemaFetchStatement seriesSchemaFetchStatement, MPPQueryContext 
context) {
     LogicalPlanBuilder planBuilder = new LogicalPlanBuilder(analysis, context);
-    List<String> storageGroupList =
+    List<String> databaseList =
         new 
ArrayList<>(analysis.getSchemaPartitionInfo().getSchemaPartitionMap().keySet());
     return planBuilder
-        .planSchemaFetchMerge(storageGroupList)
+        .planSchemaFetchMerge(databaseList)
         .planSeriesSchemaFetchSource(
-            storageGroupList,
+            databaseList,
             seriesSchemaFetchStatement.getPatternTree(),
             seriesSchemaFetchStatement.getTemplateMap(),
             seriesSchemaFetchStatement.isWithTags(),
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/plan/node/metedata/read/SeriesSchemaFetchScanNode.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/plan/node/metedata/read/SeriesSchemaFetchScanNode.java
index 8f3bab5e9cc..f24cbbd9596 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/plan/node/metedata/read/SeriesSchemaFetchScanNode.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/plan/node/metedata/read/SeriesSchemaFetchScanNode.java
@@ -48,14 +48,14 @@ public class SeriesSchemaFetchScanNode extends 
SchemaFetchScanNode {
 
   public SeriesSchemaFetchScanNode(
       PlanNodeId id,
-      PartialPath storageGroup,
+      PartialPath database,
       PathPatternTree patternTree,
       Map<Integer, Template> templateMap,
       boolean withTags,
       boolean withAttributes,
       boolean withTemplate,
       boolean withAliasForce) {
-    super(id, storageGroup, patternTree);
+    super(id, database, patternTree);
     this.templateMap = templateMap;
     this.withTags = withTags;
     this.withAttributes = withAttributes;
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/mtree/impl/mem/MTreeBelowSGMemoryImpl.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/mtree/impl/mem/MTreeBelowSGMemoryImpl.java
index e82e39736bc..63d1e4c097f 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/mtree/impl/mem/MTreeBelowSGMemoryImpl.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/mtree/impl/mem/MTreeBelowSGMemoryImpl.java
@@ -66,6 +66,7 @@ import 
org.apache.iotdb.db.schemaengine.schemaregion.read.resp.reader.impl.Schem
 import 
org.apache.iotdb.db.schemaengine.schemaregion.read.resp.reader.impl.TimeseriesReaderWithViewFetch;
 import org.apache.iotdb.db.schemaengine.schemaregion.utils.MetaFormatUtils;
 import 
org.apache.iotdb.db.schemaengine.schemaregion.utils.filter.DeviceFilterVisitor;
+import org.apache.iotdb.db.schemaengine.template.ClusterTemplateManager;
 import org.apache.iotdb.db.schemaengine.template.Template;
 import 
org.apache.iotdb.db.storageengine.rescon.quotas.DataNodeSpaceQuotaManager;
 import org.apache.iotdb.rpc.TSStatusCode;
@@ -82,6 +83,7 @@ import org.apache.tsfile.write.schema.MeasurementSchema;
 import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -89,11 +91,14 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.NoSuchElementException;
+import java.util.Objects;
 import java.util.Set;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.function.Consumer;
 import java.util.function.Function;
 
+import static org.apache.iotdb.commons.schema.SchemaConstant.NON_TEMPLATE;
+
 /**
  * The hierarchical struct of the Metadata Tree is implemented in this class.
  *
@@ -119,50 +124,50 @@ public class MTreeBelowSGMemoryImpl {
   private final MemMTreeStore store;
 
   @SuppressWarnings("java:S3077")
-  private volatile IMemMNode storageGroupMNode;
+  private volatile IMemMNode databaseMNode;
 
   private final IMemMNode rootNode;
   private final Function<IMeasurementMNode<IMemMNode>, Map<String, String>> 
tagGetter;
   private final Function<IMeasurementMNode<IMemMNode>, Map<String, String>> 
attributeGetter;
   private final IMNodeFactory<IMemMNode> nodeFactory =
       MNodeFactoryLoader.getInstance().getMemMNodeIMNodeFactory();
-  private final int levelOfSG;
+  private final int levelOfDB;
   private final MemSchemaRegionStatistics regionStatistics;
 
   // region MTree initialization, clear and serialization
   public MTreeBelowSGMemoryImpl(
-      PartialPath storageGroupPath,
+      PartialPath databasePath,
       Function<IMeasurementMNode<IMemMNode>, Map<String, String>> tagGetter,
       Function<IMeasurementMNode<IMemMNode>, Map<String, String>> 
attributeGetter,
       MemSchemaRegionStatistics regionStatistics,
       SchemaRegionMemMetric metric) {
-    store = new MemMTreeStore(storageGroupPath, regionStatistics, metric);
+    store = new MemMTreeStore(databasePath, regionStatistics, metric);
     this.regionStatistics = regionStatistics;
-    this.storageGroupMNode = store.getRoot();
-    this.rootNode = store.generatePrefix(storageGroupPath);
-    levelOfSG = storageGroupPath.getNodeLength() - 1;
+    this.databaseMNode = store.getRoot();
+    this.rootNode = store.generatePrefix(databasePath);
+    levelOfDB = databasePath.getNodeLength() - 1;
     this.tagGetter = tagGetter;
     this.attributeGetter = attributeGetter;
   }
 
   private MTreeBelowSGMemoryImpl(
-      PartialPath storageGroupPath,
+      PartialPath databasePath,
       MemMTreeStore store,
       Function<IMeasurementMNode<IMemMNode>, Map<String, String>> tagGetter,
       Function<IMeasurementMNode<IMemMNode>, Map<String, String>> 
attributeGetter,
       MemSchemaRegionStatistics regionStatistics) {
     this.store = store;
     this.regionStatistics = regionStatistics;
-    this.storageGroupMNode = store.getRoot();
-    this.rootNode = store.generatePrefix(storageGroupPath);
-    levelOfSG = storageGroupPath.getNodeLength() - 1;
+    this.databaseMNode = store.getRoot();
+    this.rootNode = store.generatePrefix(databasePath);
+    levelOfDB = databasePath.getNodeLength() - 1;
     this.tagGetter = tagGetter;
     this.attributeGetter = attributeGetter;
   }
 
   public void clear() {
     store.clear();
-    storageGroupMNode = null;
+    databaseMNode = null;
   }
 
   public synchronized boolean createSnapshot(File snapshotDir) {
@@ -391,14 +396,14 @@ public class MTreeBelowSGMemoryImpl {
       throws MetadataException {
     String[] nodeNames = devicePath.getNodes();
     MetaFormatUtils.checkTimeseries(devicePath);
-    if (nodeNames.length == levelOfSG + 1) {
+    if (nodeNames.length == levelOfDB + 1) {
       return null;
     }
-    IMemMNode cur = storageGroupMNode;
+    IMemMNode cur = databaseMNode;
     IMemMNode child;
     String childName;
     // e.g, path = root.sg.d1.s1,  create internal nodes and set cur to sg 
node, parent of d1
-    for (int i = levelOfSG + 1; i < nodeNames.length - 1; i++) {
+    for (int i = levelOfDB + 1; i < nodeNames.length - 1; i++) {
       childName = nodeNames[i];
       child = cur.getChild(childName);
       if (child == null) {
@@ -417,13 +422,12 @@ public class MTreeBelowSGMemoryImpl {
       throws PathAlreadyExistException, ExceedQuotaException {
     if (deviceParent == null) {
       // device is sg
-      return storageGroupMNode;
+      return databaseMNode;
     }
     IMemMNode device = store.getChild(deviceParent, deviceName);
     if (device == null) {
       if (IoTDBDescriptor.getInstance().getConfig().isQuotaEnable()) {
-        if (!DataNodeSpaceQuotaManager.getInstance()
-            .checkDeviceLimit(storageGroupMNode.getName())) {
+        if 
(!DataNodeSpaceQuotaManager.getInstance().checkDeviceLimit(databaseMNode.getName()))
 {
           throw new ExceedQuotaException(
               "The number of devices has reached the upper limit",
               TSStatusCode.SPACE_QUOTA_EXCEEDED.getStatusCode());
@@ -482,8 +486,7 @@ public class MTreeBelowSGMemoryImpl {
                 devicePath.getFullPath() + "." + measurementList.get(i), 
aliasList.get(i)));
       }
       if (IoTDBDescriptor.getInstance().getConfig().isQuotaEnable()) {
-        if (!DataNodeSpaceQuotaManager.getInstance()
-            .checkTimeSeriesNum(storageGroupMNode.getName())) {
+        if 
(!DataNodeSpaceQuotaManager.getInstance().checkTimeSeriesNum(databaseMNode.getName()))
 {
           failingMeasurementMap.put(
               i,
               new ExceedQuotaException(
@@ -688,11 +691,20 @@ public class MTreeBelowSGMemoryImpl {
   public IMemMNode getDeviceNodeWithAutoCreating(PartialPath deviceId) throws 
MetadataException {
     MetaFormatUtils.checkTimeseries(deviceId);
     String[] nodeNames = deviceId.getNodes();
-    IMemMNode cur = storageGroupMNode;
+    IMemMNode cur = databaseMNode;
     IMemMNode child;
-    for (int i = levelOfSG + 1; i < nodeNames.length; i++) {
+    for (int i = levelOfDB + 1; i < nodeNames.length; i++) {
       child = cur.getChild(nodeNames[i]);
       if (child == null) {
+        if (cur.isDevice() && cur.getAsDeviceMNode().getSchemaTemplateId() != 
NON_TEMPLATE) {
+          final Template template =
+              ClusterTemplateManager.getInstance()
+                  .getTemplate(cur.getAsDeviceMNode().getSchemaTemplateId());
+          if (Objects.nonNull(template) && template.getSchema(nodeNames[i]) != 
null) {
+            throw new PathAlreadyExistException(
+                new PartialPath(Arrays.copyOf(deviceId.getNodes(), i + 
1)).getFullPath());
+          }
+        }
         child =
             store.addChild(cur, nodeNames[i], 
nodeFactory.createInternalMNode(cur, nodeNames[i]));
       }
@@ -830,9 +842,9 @@ public class MTreeBelowSGMemoryImpl {
    */
   public IMemMNode getNodeByPath(PartialPath path) throws 
PathNotExistException {
     String[] nodes = path.getNodes();
-    IMemMNode cur = storageGroupMNode;
+    IMemMNode cur = databaseMNode;
     IMemMNode next;
-    for (int i = levelOfSG + 1; i < nodes.length; i++) {
+    for (int i = levelOfDB + 1; i < nodes.length; i++) {
       next = cur.getChild(nodes[i]);
       if (next == null) {
         throw new PathNotExistException(path.getFullPath(), true);
@@ -865,9 +877,9 @@ public class MTreeBelowSGMemoryImpl {
 
   public void activateTemplate(PartialPath activatePath, Template template)
       throws MetadataException {
-    String[] nodes = activatePath.getNodes();
-    IMemMNode cur = storageGroupMNode;
-    for (int i = levelOfSG + 1; i < nodes.length; i++) {
+    final String[] nodes = activatePath.getNodes();
+    IMemMNode cur = databaseMNode;
+    for (int i = levelOfDB + 1; i < nodes.length; i++) {
       cur = cur.getChild(nodes[i]);
     }
 
@@ -968,10 +980,10 @@ public class MTreeBelowSGMemoryImpl {
   }
 
   public void activateTemplateWithoutCheck(
-      PartialPath activatePath, int templateId, boolean isAligned) {
-    String[] nodes = activatePath.getNodes();
-    IMemMNode cur = storageGroupMNode;
-    for (int i = levelOfSG + 1; i < nodes.length; i++) {
+      final PartialPath activatePath, final int templateId, final boolean 
isAligned) {
+    final String[] nodes = activatePath.getNodes();
+    IMemMNode cur = databaseMNode;
+    for (int i = levelOfDB + 1; i < nodes.length; i++) {
       cur = cur.getChild(nodes[i]);
     }
 
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/mtree/impl/pbtree/MTreeBelowSGCachedImpl.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/mtree/impl/pbtree/MTreeBelowSGCachedImpl.java
index 679bf600dfa..f79a8c2e057 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/mtree/impl/pbtree/MTreeBelowSGCachedImpl.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/mtree/impl/pbtree/MTreeBelowSGCachedImpl.java
@@ -64,6 +64,7 @@ import 
org.apache.iotdb.db.schemaengine.schemaregion.read.resp.reader.impl.Schem
 import 
org.apache.iotdb.db.schemaengine.schemaregion.read.resp.reader.impl.TimeseriesReaderWithViewFetch;
 import org.apache.iotdb.db.schemaengine.schemaregion.utils.MetaFormatUtils;
 import 
org.apache.iotdb.db.schemaengine.schemaregion.utils.filter.DeviceFilterVisitor;
+import org.apache.iotdb.db.schemaengine.template.ClusterTemplateManager;
 import org.apache.iotdb.db.schemaengine.template.Template;
 
 import com.google.common.util.concurrent.ListenableFuture;
@@ -79,6 +80,7 @@ import org.slf4j.LoggerFactory;
 import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -86,11 +88,14 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.NoSuchElementException;
+import java.util.Objects;
 import java.util.Set;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.function.Consumer;
 import java.util.function.Function;
 
+import static org.apache.iotdb.commons.schema.SchemaConstant.NON_TEMPLATE;
+
 /**
  * The hierarchical struct of the Metadata Tree is implemented in this class.
  *
@@ -117,14 +122,14 @@ public class MTreeBelowSGCachedImpl {
   private final CachedMTreeStore store;
 
   @SuppressWarnings("java:S3077")
-  private volatile ICachedMNode storageGroupMNode;
+  private volatile ICachedMNode databaseMNode;
 
   private final ICachedMNode rootNode;
   private final Function<IMeasurementMNode<ICachedMNode>, Map<String, String>> 
tagGetter;
   private final Function<IMeasurementMNode<ICachedMNode>, Map<String, String>> 
attributeGetter;
   private final IMNodeFactory<ICachedMNode> nodeFactory =
       MNodeFactoryLoader.getInstance().getCachedMNodeIMNodeFactory();
-  private final int levelOfSG;
+  private final int levelOfDB;
   private final CachedSchemaRegionStatistics regionStatistics;
 
   // region MTree initialization, clear and serialization
@@ -146,16 +151,16 @@ public class MTreeBelowSGCachedImpl {
         PBTreeFactory.getInstance()
             .createNewCachedMTreeStore(
                 storageGroupPath, schemaRegionId, regionStatistics, metric, 
flushCallback);
-    this.storageGroupMNode = store.getRoot();
-    this.storageGroupMNode.setParent(storageGroupMNode.getParent());
+    this.databaseMNode = store.getRoot();
+    this.databaseMNode.setParent(databaseMNode.getParent());
     this.rootNode = store.generatePrefix(storageGroupPath);
-    levelOfSG = storageGroupPath.getNodeLength() - 1;
+    levelOfDB = storageGroupPath.getNodeLength() - 1;
 
     // recover MNode
     try (MNodeCollector<Void, ICachedMNode> collector =
         new MNodeCollector<Void, ICachedMNode>(
             this.rootNode,
-            new PartialPath(storageGroupMNode.getFullPath()),
+            new PartialPath(databaseMNode.getFullPath()),
             this.store,
             true,
             SchemaConstant.ALL_MATCH_SCOPE) {
@@ -185,9 +190,9 @@ public class MTreeBelowSGCachedImpl {
       throws MetadataException {
     this.store = store;
     this.regionStatistics = regionStatistics;
-    this.storageGroupMNode = store.getRoot();
+    this.databaseMNode = store.getRoot();
     this.rootNode = store.generatePrefix(storageGroupPath);
-    levelOfSG = storageGroupMNode.getPartialPath().getNodeLength() - 1;
+    levelOfDB = databaseMNode.getPartialPath().getNodeLength() - 1;
     this.tagGetter = tagGetter;
     this.attributeGetter = attributeGetter;
 
@@ -195,7 +200,7 @@ public class MTreeBelowSGCachedImpl {
     try (MNodeCollector<Void, ICachedMNode> collector =
         new MNodeCollector<Void, ICachedMNode>(
             this.rootNode,
-            new PartialPath(storageGroupMNode.getFullPath()),
+            new PartialPath(databaseMNode.getFullPath()),
             this.store,
             true,
             SchemaConstant.ALL_MATCH_SCOPE) {
@@ -215,7 +220,7 @@ public class MTreeBelowSGCachedImpl {
 
   public void clear() {
     store.clear();
-    storageGroupMNode = null;
+    databaseMNode = null;
   }
 
   public boolean createSnapshot(File snapshotDir) {
@@ -583,15 +588,15 @@ public class MTreeBelowSGCachedImpl {
       throws MetadataException {
     String[] nodeNames = devicePath.getNodes();
     MetaFormatUtils.checkTimeseries(devicePath);
-    if (nodeNames.length == levelOfSG + 1) {
+    if (nodeNames.length == levelOfDB + 1) {
       return null;
     }
-    ICachedMNode cur = storageGroupMNode;
+    ICachedMNode cur = databaseMNode;
     ICachedMNode child;
     String childName;
     try {
       // e.g, path = root.sg.d1.s1,  create internal nodes and set cur to sg 
node, parent of d1
-      for (int i = levelOfSG + 1; i < nodeNames.length - 1; i++) {
+      for (int i = levelOfDB + 1; i < nodeNames.length - 1; i++) {
         childName = nodeNames[i];
         child = store.getChild(cur, childName);
         if (child == null) {
@@ -614,8 +619,8 @@ public class MTreeBelowSGCachedImpl {
       throws MetadataException {
     if (deviceParent == null) {
       // device is sg
-      pinMNode(storageGroupMNode);
-      return storageGroupMNode;
+      pinMNode(databaseMNode);
+      return databaseMNode;
     }
     ICachedMNode device = store.getChild(deviceParent, deviceName);
     if (device == null) {
@@ -823,12 +828,21 @@ public class MTreeBelowSGCachedImpl {
   public ICachedMNode getDeviceNodeWithAutoCreating(PartialPath deviceId) 
throws MetadataException {
     String[] nodeNames = deviceId.getNodes();
     MetaFormatUtils.checkTimeseries(deviceId);
-    ICachedMNode cur = storageGroupMNode;
+    ICachedMNode cur = databaseMNode;
     ICachedMNode child;
     try {
-      for (int i = levelOfSG + 1; i < nodeNames.length; i++) {
+      for (int i = levelOfDB + 1; i < nodeNames.length; i++) {
         child = store.getChild(cur, nodeNames[i]);
         if (child == null) {
+          if (cur.isDevice() && cur.getAsDeviceMNode().getSchemaTemplateId() 
!= NON_TEMPLATE) {
+            final Template template =
+                ClusterTemplateManager.getInstance()
+                    .getTemplate(cur.getAsDeviceMNode().getSchemaTemplateId());
+            if (Objects.nonNull(template) && template.getSchema(nodeNames[i]) 
!= null) {
+              throw new PathAlreadyExistException(
+                  new PartialPath(Arrays.copyOf(deviceId.getNodes(), i + 
1)).getFullPath());
+            }
+          }
           child =
               store.addChild(cur, nodeNames[i], 
nodeFactory.createInternalMNode(cur, nodeNames[i]));
         }
@@ -976,10 +990,10 @@ public class MTreeBelowSGCachedImpl {
    */
   public ICachedMNode getNodeByPath(PartialPath path) throws MetadataException 
{
     String[] nodes = path.getNodes();
-    ICachedMNode cur = storageGroupMNode;
+    ICachedMNode cur = databaseMNode;
     ICachedMNode next;
     try {
-      for (int i = levelOfSG + 1; i < nodes.length; i++) {
+      for (int i = levelOfDB + 1; i < nodes.length; i++) {
         next = store.getChild(cur, nodes[i]);
         if (next == null) {
           throw new PathNotExistException(path.getFullPath(), true);
@@ -1156,12 +1170,12 @@ public class MTreeBelowSGCachedImpl {
   public void activateTemplate(PartialPath activatePath, Template template)
       throws MetadataException {
     String[] nodes = activatePath.getNodes();
-    ICachedMNode cur = storageGroupMNode;
+    ICachedMNode cur = databaseMNode;
     ICachedMNode child;
     IDeviceMNode<ICachedMNode> entityMNode;
 
     try {
-      for (int i = levelOfSG + 1; i < nodes.length; i++) {
+      for (int i = levelOfDB + 1; i < nodes.length; i++) {
         child = store.getChild(cur, nodes[i]);
         if (child == null) {
           throw new PathNotExistException(activatePath.getFullPath());
@@ -1203,12 +1217,12 @@ public class MTreeBelowSGCachedImpl {
   public void activateTemplateWithoutCheck(
       PartialPath activatePath, int templateId, boolean isAligned) throws 
MetadataException {
     String[] nodes = activatePath.getNodes();
-    ICachedMNode cur = storageGroupMNode;
+    ICachedMNode cur = databaseMNode;
     ICachedMNode child;
     IDeviceMNode<ICachedMNode> entityMNode;
 
     try {
-      for (int i = levelOfSG + 1; i < nodes.length; i++) {
+      for (int i = levelOfDB + 1; i < nodes.length; i++) {
         child = store.getChild(cur, nodes[i]);
         if (child == null) {
           throw new PathNotExistException(activatePath.getFullPath());
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/ErrorHandlingUtils.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/ErrorHandlingUtils.java
index 08e8325c787..072cba8b55c 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/ErrorHandlingUtils.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/ErrorHandlingUtils.java
@@ -107,6 +107,8 @@ public class ErrorHandlingUtils {
             || status.getCode() == 
TSStatusCode.PLAN_FAILED_NETWORK_PARTITION.getStatusCode()
             || status.getCode() == 
TSStatusCode.SYNC_CONNECTION_ERROR.getStatusCode()
             || status.getCode() == 
TSStatusCode.CANNOT_FETCH_FI_STATE.getStatusCode()
+            || status.getCode() == 
TSStatusCode.TEMPLATE_INCOMPATIBLE.getStatusCode()
+            || status.getCode() == 
TSStatusCode.PATH_ALREADY_EXIST.getStatusCode()
             || status.getCode() == TSStatusCode.QUERY_TIMEOUT.getStatusCode()) 
{
           LOGGER.info(message);
         } else {

Reply via email to