diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/analyze/load/TreeSchemaAutoCreatorAndVerifier.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/analyze/load/TreeSchemaAutoCreatorAndVerifier.java index 6d1704c0cfd0a..2e50a88f5d13c 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/analyze/load/TreeSchemaAutoCreatorAndVerifier.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/analyze/load/TreeSchemaAutoCreatorAndVerifier.java @@ -359,6 +359,12 @@ private void autoCreateDatabase() final PartialPath devicePath = new PartialPath(device); final String[] devicePrefixNodes = devicePath.getNodes(); + for (final String node : devicePrefixNodes) { + if (node == null || node.isEmpty()) { + throw new LoadAnalyzeException( + new IllegalPathException(devicePath.getFullPath()).getMessage()); + } + } if (devicePrefixNodes.length < databasePrefixNodesLength) { throw new LoadAnalyzeException( String.format( diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/analyze/load/LoadTsFileAnalyzerTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/analyze/load/LoadTsFileAnalyzerTest.java index 68f4bde29e6a6..0b6e5350882a6 100644 --- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/analyze/load/LoadTsFileAnalyzerTest.java +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/analyze/load/LoadTsFileAnalyzerTest.java @@ -200,6 +200,35 @@ public void testTreeSchemaVerifierShouldThrowMismatchWhenVerifyingDataType() thr } } + @Test + public void testTreeSchemaVerifierShouldRejectDeviceWithEmptyPathNode() throws Exception { + final File tsFile = File.createTempFile("load-tree-illegal-device", ".tsfile"); + + try (final LoadTsFileAnalyzer analyzer = + new LoadTsFileAnalyzer( + LoadTsFileStatement.createUnchecked(tsFile.getAbsolutePath()), + false, + new MPPQueryContext(new QueryId("load_tree_illegal_device_test")))) { + final TreeSchemaAutoCreatorAndVerifier verifier = + new TreeSchemaAutoCreatorAndVerifier(analyzer); + try { + final IDeviceID device = new StringArrayDeviceID(new String[] {"root", ""}); + getTreeSchemaCache(verifier) + .addTimeSeries(device, new MeasurementSchema("s1", TSDataType.INT32)); + + final InvocationTargetException exception = + Assert.assertThrows( + InvocationTargetException.class, + () -> getAutoCreateDatabaseMethod().invoke(verifier)); + Assert.assertTrue(exception.getCause() instanceof LoadAnalyzeException); + } finally { + verifier.close(); + } + } finally { + Assert.assertTrue(tsFile.delete()); + } + } + @Test public void testPipeGeneratedLoadMissingSchemaShouldBeTemporaryWhenAutoCreateDisabled() throws Exception { @@ -316,6 +345,13 @@ private Method getVerifyTreeSchemaMethod() throws NoSuchMethodException { return method; } + private Method getAutoCreateDatabaseMethod() throws NoSuchMethodException { + final Method method = + TreeSchemaAutoCreatorAndVerifier.class.getDeclaredMethod("autoCreateDatabase"); + method.setAccessible(true); + return method; + } + private org.apache.iotdb.commons.queryengine.plan.relational.metadata.TableSchema createTableSchema(final TSDataType fieldType) { return new org.apache.iotdb.commons.queryengine.plan.relational.metadata.TableSchema(