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

jshao pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gravitino.git


The following commit(s) were added to refs/heads/main by this push:
     new 10b2fb0d53 [#6570] improvement(core): Optimize fetching entity parent 
id logic (#6574)
10b2fb0d53 is described below

commit 10b2fb0d53091e22d9966020373bc9ddf913d273
Author: Mini Yu <y...@datastrato.com>
AuthorDate: Fri Mar 28 13:13:06 2025 +0800

    [#6570] improvement(core): Optimize fetching entity parent id logic (#6574)
    
    ### What changes were proposed in this pull request?
    
    Use `JOIN` to avoid fetching id several times when getting the parents
    of a fileset/table/topic/model
    
    ### Why are the changes needed?
    
    It's very time-consuming to retrieve id from database several times.
    
    Fix: #6570
    
    ### Does this PR introduce _any_ user-facing change?
    
    N/A
    
    ### How was this patch tested?
    
    Existing UTs
---
 .../hadoop/TestHadoopCatalogOperations.java        |  69 ++++++++++-
 .../catalog/kafka/TestKafkaCatalogOperations.java  |   4 +
 .../storage/relational/helper/CatalogIds.java      |  37 ++++++
 .../storage/relational/helper/SchemaIds.java       |  43 +++++++
 .../relational/mapper/CatalogMetaMapper.java       |   7 ++
 .../mapper/CatalogMetaSQLProviderFactory.java      |   5 +
 .../relational/mapper/SchemaMetaMapper.java        |   9 ++
 .../mapper/SchemaMetaSQLProviderFactory.java       |   9 ++
 .../provider/base/CatalogMetaBaseSQLProvider.java  |   9 ++
 .../provider/base/SchemaMetaBaseSQLProvider.java   |  17 +++
 .../relational/service/CatalogMetaService.java     |   7 ++
 .../relational/service/CommonMetaService.java      | 127 +++++++++++++++------
 .../relational/service/SchemaMetaService.java      |  10 ++
 13 files changed, 319 insertions(+), 34 deletions(-)

diff --git 
a/catalogs/catalog-hadoop/src/test/java/org/apache/gravitino/catalog/hadoop/TestHadoopCatalogOperations.java
 
b/catalogs/catalog-hadoop/src/test/java/org/apache/gravitino/catalog/hadoop/TestHadoopCatalogOperations.java
index 717a76cf9e..deff740dd2 100644
--- 
a/catalogs/catalog-hadoop/src/test/java/org/apache/gravitino/catalog/hadoop/TestHadoopCatalogOperations.java
+++ 
b/catalogs/catalog-hadoop/src/test/java/org/apache/gravitino/catalog/hadoop/TestHadoopCatalogOperations.java
@@ -79,8 +79,11 @@ import org.apache.gravitino.file.FilesetChange;
 import org.apache.gravitino.storage.IdGenerator;
 import org.apache.gravitino.storage.RandomIdGenerator;
 import org.apache.gravitino.storage.relational.RelationalEntityStore;
+import org.apache.gravitino.storage.relational.helper.CatalogIds;
+import org.apache.gravitino.storage.relational.helper.SchemaIds;
 import org.apache.gravitino.storage.relational.service.CatalogMetaService;
 import org.apache.gravitino.storage.relational.service.MetalakeMetaService;
+import org.apache.gravitino.storage.relational.service.SchemaMetaService;
 import org.apache.gravitino.utils.NameIdentifierUtil;
 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.fs.FileSystem;
@@ -221,10 +224,69 @@ public class TestHadoopCatalogOperations {
         .when(spyCatalogMetaService)
         .getCatalogIdByMetalakeIdAndName(Mockito.anyLong(), 
Mockito.anyString());
 
+    SchemaMetaService serviceMetaService = SchemaMetaService.getInstance();
+    SchemaMetaService spySchemaMetaService = Mockito.spy(serviceMetaService);
+
+    doReturn(new CatalogIds(1L, 1L))
+        .when(spyCatalogMetaService)
+        .getCatalogIdByMetalakeAndCatalogName(Mockito.anyString(), 
Mockito.anyString());
+
+    doReturn(new SchemaIds(1L, 1L, 1L))
+        .when(spySchemaMetaService)
+        .getSchemaIdByMetalakeNameAndCatalogNameAndSchemaName(
+            Mockito.anyString(), Mockito.anyString(), Mockito.eq("schema11"));
+
+    for (int i = 10; i < 30; i++) {
+      doReturn(new SchemaIds(1L, 1L, (long) i))
+          .when(spySchemaMetaService)
+          .getSchemaIdByMetalakeNameAndCatalogNameAndSchemaName(
+              Mockito.anyString(), Mockito.anyString(), Mockito.eq("schema" + 
i));
+    }
+
+    Stream<Arguments> argumentsStream = testRenameArguments();
+    argumentsStream.forEach(
+        arguments -> {
+          String oldName = (String) arguments.get()[0];
+          String newName = (String) arguments.get()[1];
+          long schemaId = idGenerator.nextId();
+          doReturn(new SchemaIds(1L, 1L, schemaId))
+              .when(spySchemaMetaService)
+              .getSchemaIdByMetalakeNameAndCatalogNameAndSchemaName(
+                  Mockito.anyString(), Mockito.anyString(), Mockito.eq("s24_" 
+ oldName));
+          doReturn(new SchemaIds(1L, 1L, schemaId))
+              .when(spySchemaMetaService)
+              .getSchemaIdByMetalakeNameAndCatalogNameAndSchemaName(
+                  Mockito.anyString(), Mockito.anyString(), Mockito.eq("s24_" 
+ newName));
+        });
+
+    locationArguments()
+        .forEach(
+            arguments -> {
+              String name = (String) arguments.get()[0];
+              long schemaId = idGenerator.nextId();
+              doReturn(new SchemaIds(1L, 1L, schemaId))
+                  .when(spySchemaMetaService)
+                  .getSchemaIdByMetalakeNameAndCatalogNameAndSchemaName(
+                      Mockito.anyString(), Mockito.anyString(), 
Mockito.eq("s1_" + name));
+            });
+
+    locationWithPlaceholdersArguments()
+        .forEach(
+            arguments -> {
+              String name = (String) arguments.get()[0];
+              long schemaId = idGenerator.nextId();
+              doReturn(new SchemaIds(1L, 1L, schemaId))
+                  .when(spySchemaMetaService)
+                  .getSchemaIdByMetalakeNameAndCatalogNameAndSchemaName(
+                      Mockito.anyString(), Mockito.anyString(), 
Mockito.eq("s1_" + name));
+            });
+
     MockedStatic<MetalakeMetaService> metalakeMetaServiceMockedStatic =
         Mockito.mockStatic(MetalakeMetaService.class);
     MockedStatic<CatalogMetaService> catalogMetaServiceMockedStatic =
         Mockito.mockStatic(CatalogMetaService.class);
+    MockedStatic<SchemaMetaService> schemaMetaServiceMockedStatic =
+        Mockito.mockStatic(SchemaMetaService.class);
 
     metalakeMetaServiceMockedStatic
         .when(MetalakeMetaService::getInstance)
@@ -232,6 +294,9 @@ public class TestHadoopCatalogOperations {
     catalogMetaServiceMockedStatic
         .when(CatalogMetaService::getInstance)
         .thenReturn(spyCatalogMetaService);
+    schemaMetaServiceMockedStatic
+        .when(SchemaMetaService::getInstance)
+        .thenReturn(spySchemaMetaService);
   }
 
   @AfterAll
@@ -936,8 +1001,8 @@ public class TestHadoopCatalogOperations {
 
   @Test
   public void testGetFileLocation() throws IOException {
-    String schemaName = "schema1024";
-    String comment = "comment1024";
+    String schemaName = "schema29";
+    String comment = "schema29";
     String schemaPath = TEST_ROOT_PATH + "/" + schemaName;
     createSchema(schemaName, comment, null, schemaPath);
 
diff --git 
a/catalogs/catalog-kafka/src/test/java/org/apache/gravitino/catalog/kafka/TestKafkaCatalogOperations.java
 
b/catalogs/catalog-kafka/src/test/java/org/apache/gravitino/catalog/kafka/TestKafkaCatalogOperations.java
index fa956b78f3..d09a33a634 100644
--- 
a/catalogs/catalog-kafka/src/test/java/org/apache/gravitino/catalog/kafka/TestKafkaCatalogOperations.java
+++ 
b/catalogs/catalog-kafka/src/test/java/org/apache/gravitino/catalog/kafka/TestKafkaCatalogOperations.java
@@ -69,6 +69,7 @@ import org.apache.gravitino.meta.AuditInfo;
 import org.apache.gravitino.meta.CatalogEntity;
 import org.apache.gravitino.storage.IdGenerator;
 import org.apache.gravitino.storage.RandomIdGenerator;
+import org.apache.gravitino.storage.relational.helper.CatalogIds;
 import org.apache.gravitino.storage.relational.service.CatalogMetaService;
 import org.apache.gravitino.storage.relational.service.MetalakeMetaService;
 import org.apache.kafka.common.config.TopicConfig;
@@ -160,6 +161,9 @@ public class TestKafkaCatalogOperations extends 
KafkaClusterEmbedded {
     doReturn(1L)
         .when(spyCatalogMetaService)
         .getCatalogIdByMetalakeIdAndName(Mockito.anyLong(), 
Mockito.anyString());
+    doReturn(new CatalogIds(1L, 1L))
+        .when(spyCatalogMetaService)
+        .getCatalogIdByMetalakeAndCatalogName(Mockito.anyString(), 
Mockito.anyString());
 
     MockedStatic<MetalakeMetaService> metalakeMetaServiceMockedStatic =
         Mockito.mockStatic(MetalakeMetaService.class);
diff --git 
a/core/src/main/java/org/apache/gravitino/storage/relational/helper/CatalogIds.java
 
b/core/src/main/java/org/apache/gravitino/storage/relational/helper/CatalogIds.java
new file mode 100644
index 0000000000..ebcf83901a
--- /dev/null
+++ 
b/core/src/main/java/org/apache/gravitino/storage/relational/helper/CatalogIds.java
@@ -0,0 +1,37 @@
+/*
+ * 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.gravitino.storage.relational.helper;
+
+public class CatalogIds {
+  private final Long metalakeId;
+  private final Long catalogId;
+
+  public CatalogIds(Long metalakeId, Long catalogId) {
+    this.metalakeId = metalakeId;
+    this.catalogId = catalogId;
+  }
+
+  public Long getMetalakeId() {
+    return metalakeId;
+  }
+
+  public Long getCatalogId() {
+    return catalogId;
+  }
+}
diff --git 
a/core/src/main/java/org/apache/gravitino/storage/relational/helper/SchemaIds.java
 
b/core/src/main/java/org/apache/gravitino/storage/relational/helper/SchemaIds.java
new file mode 100644
index 0000000000..32ce14c2e7
--- /dev/null
+++ 
b/core/src/main/java/org/apache/gravitino/storage/relational/helper/SchemaIds.java
@@ -0,0 +1,43 @@
+/*
+ * 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.gravitino.storage.relational.helper;
+
+public class SchemaIds {
+  private final Long metalakeId;
+  private final Long catalogId;
+  private final Long schemaId;
+
+  public SchemaIds(Long metalakeId, Long catalogId, Long schemaId) {
+    this.metalakeId = metalakeId;
+    this.catalogId = catalogId;
+    this.schemaId = schemaId;
+  }
+
+  public Long getMetalakeId() {
+    return metalakeId;
+  }
+
+  public Long getCatalogId() {
+    return catalogId;
+  }
+
+  public Long getSchemaId() {
+    return schemaId;
+  }
+}
diff --git 
a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/CatalogMetaMapper.java
 
b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/CatalogMetaMapper.java
index 28423d75b5..f74be4275e 100644
--- 
a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/CatalogMetaMapper.java
+++ 
b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/CatalogMetaMapper.java
@@ -20,6 +20,7 @@
 package org.apache.gravitino.storage.relational.mapper;
 
 import java.util.List;
+import org.apache.gravitino.storage.relational.helper.CatalogIds;
 import org.apache.gravitino.storage.relational.po.CatalogPO;
 import org.apache.ibatis.annotations.DeleteProvider;
 import org.apache.ibatis.annotations.InsertProvider;
@@ -87,4 +88,10 @@ public interface CatalogMetaMapper {
       method = "deleteCatalogMetasByLegacyTimeline")
   Integer deleteCatalogMetasByLegacyTimeline(
       @Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit);
+
+  @SelectProvider(
+      type = CatalogMetaSQLProviderFactory.class,
+      method = "selectCatalogIdByMetalakeNameAndCatalogName")
+  CatalogIds selectCatalogIdByMetalakeNameAndCatalogName(
+      @Param("metalakeName") String metalakeName, @Param("catalogName") String 
catalogName);
 }
diff --git 
a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/CatalogMetaSQLProviderFactory.java
 
b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/CatalogMetaSQLProviderFactory.java
index bfde8a034a..e54a1481b1 100644
--- 
a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/CatalogMetaSQLProviderFactory.java
+++ 
b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/CatalogMetaSQLProviderFactory.java
@@ -71,6 +71,11 @@ public class CatalogMetaSQLProviderFactory {
     return getProvider().selectCatalogMetaByMetalakeIdAndName(metalakeId, 
name);
   }
 
+  public static String selectCatalogIdByMetalakeNameAndCatalogName(
+      @Param("metalakeName") String metalakeName, @Param("catalogName") String 
catalogName) {
+    return 
getProvider().selectCatalogIdByMetalakeNameAndCatalogName(metalakeName, 
catalogName);
+  }
+
   public static String selectCatalogMetaById(@Param("catalogId") Long 
catalogId) {
     return getProvider().selectCatalogMetaById(catalogId);
   }
diff --git 
a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/SchemaMetaMapper.java
 
b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/SchemaMetaMapper.java
index 49598ce727..e1816a3277 100644
--- 
a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/SchemaMetaMapper.java
+++ 
b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/SchemaMetaMapper.java
@@ -20,6 +20,7 @@
 package org.apache.gravitino.storage.relational.mapper;
 
 import java.util.List;
+import org.apache.gravitino.storage.relational.helper.SchemaIds;
 import org.apache.gravitino.storage.relational.po.SchemaPO;
 import org.apache.ibatis.annotations.DeleteProvider;
 import org.apache.ibatis.annotations.InsertProvider;
@@ -91,4 +92,12 @@ public interface SchemaMetaMapper {
       method = "deleteSchemaMetasByLegacyTimeline")
   Integer deleteSchemaMetasByLegacyTimeline(
       @Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit);
+
+  @SelectProvider(
+      type = SchemaMetaSQLProviderFactory.class,
+      method = "selectSchemaIdByMetalakeNameAndCatalogNameAndSchemaName")
+  SchemaIds selectSchemaIdByMetalakeNameAndCatalogNameAndSchemaName(
+      @Param("metalakeName") String metalakeName,
+      @Param("catalogName") String catalogName,
+      @Param("schemaName") String schemaName);
 }
diff --git 
a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/SchemaMetaSQLProviderFactory.java
 
b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/SchemaMetaSQLProviderFactory.java
index 9f1669e476..cbab45733c 100644
--- 
a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/SchemaMetaSQLProviderFactory.java
+++ 
b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/SchemaMetaSQLProviderFactory.java
@@ -103,4 +103,13 @@ public class SchemaMetaSQLProviderFactory {
       @Param("legacyTimeline") Long legacyTimeline, @Param("limit") int limit) 
{
     return getProvider().deleteSchemaMetasByLegacyTimeline(legacyTimeline, 
limit);
   }
+
+  public static String selectSchemaIdByMetalakeNameAndCatalogNameAndSchemaName(
+      @Param("metalakeName") String metalakeName,
+      @Param("catalogName") String catalogName,
+      @Param("schemaName") String schemaName) {
+    return getProvider()
+        .selectSchemaIdByMetalakeNameAndCatalogNameAndSchemaName(
+            metalakeName, catalogName, schemaName);
+  }
 }
diff --git 
a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/base/CatalogMetaBaseSQLProvider.java
 
b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/base/CatalogMetaBaseSQLProvider.java
index 3b2f603c4b..6a62044cbf 100644
--- 
a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/base/CatalogMetaBaseSQLProvider.java
+++ 
b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/base/CatalogMetaBaseSQLProvider.java
@@ -74,6 +74,15 @@ public class CatalogMetaBaseSQLProvider {
         + " WHERE metalake_id = #{metalakeId} AND catalog_name = 
#{catalogName} AND deleted_at = 0";
   }
 
+  public String selectCatalogIdByMetalakeNameAndCatalogName(
+      @Param("metalakeName") String metalakeName, @Param("catalogName") String 
catalogName) {
+    return "SELECT me.metalake_id as metalakeId, ca.catalog_id as catalogId 
FROM "
+        + TABLE_NAME
+        + " ca INNER JOIN metalake_meta me ON ca.metalake_id = me.metalake_id"
+        + " WHERE me.metalake_name = #{metalakeName} AND ca.catalog_name = 
#{catalogName} "
+        + " AND ca.deleted_at = 0 AND me.deleted_at = 0";
+  }
+
   public String selectCatalogMetaById(@Param("catalogId") Long catalogId) {
     return "SELECT catalog_id as catalogId, catalog_name as catalogName,"
         + " metalake_id as metalakeId, type, provider,"
diff --git 
a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/base/SchemaMetaBaseSQLProvider.java
 
b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/base/SchemaMetaBaseSQLProvider.java
index 84ffcf8408..09d00c5861 100644
--- 
a/core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/base/SchemaMetaBaseSQLProvider.java
+++ 
b/core/src/main/java/org/apache/gravitino/storage/relational/mapper/provider/base/SchemaMetaBaseSQLProvider.java
@@ -190,4 +190,21 @@ public class SchemaMetaBaseSQLProvider {
         + TABLE_NAME
         + " WHERE deleted_at > 0 AND deleted_at < #{legacyTimeline} LIMIT 
#{limit}";
   }
+
+  public String selectSchemaIdByMetalakeNameAndCatalogNameAndSchemaName(
+      @Param("metalakeName") String metalakeName,
+      @Param("catalogName") String catalogName,
+      @Param("schemaName") String schemaName) {
+    return "SELECT metalake_meta.metalake_id as metalakeId, 
catalog_meta.catalog_id as catalogId, "
+        + " schema_id as schemaId"
+        + " FROM metalake_meta"
+        + " JOIN catalog_meta ON metalake_meta.metalake_id = 
catalog_meta.metalake_id"
+        + " JOIN schema_meta ON catalog_meta.catalog_id = 
schema_meta.catalog_id"
+        + " WHERE metalake_name = #{metalakeName}"
+        + " AND catalog_name = #{catalogName}"
+        + " AND schema_name = #{schemaName}"
+        + " AND schema_meta.deleted_at = 0"
+        + " AND catalog_meta.deleted_at = 0"
+        + " AND metalake_meta.deleted_at = 0";
+  }
 }
diff --git 
a/core/src/main/java/org/apache/gravitino/storage/relational/service/CatalogMetaService.java
 
b/core/src/main/java/org/apache/gravitino/storage/relational/service/CatalogMetaService.java
index 310b8cc08e..71b700e1b5 100644
--- 
a/core/src/main/java/org/apache/gravitino/storage/relational/service/CatalogMetaService.java
+++ 
b/core/src/main/java/org/apache/gravitino/storage/relational/service/CatalogMetaService.java
@@ -33,6 +33,7 @@ import org.apache.gravitino.exceptions.NoSuchEntityException;
 import org.apache.gravitino.exceptions.NonEmptyEntityException;
 import org.apache.gravitino.meta.CatalogEntity;
 import org.apache.gravitino.meta.SchemaEntity;
+import org.apache.gravitino.storage.relational.helper.CatalogIds;
 import org.apache.gravitino.storage.relational.mapper.CatalogMetaMapper;
 import org.apache.gravitino.storage.relational.mapper.FilesetMetaMapper;
 import org.apache.gravitino.storage.relational.mapper.FilesetVersionMapper;
@@ -80,6 +81,12 @@ public class CatalogMetaService {
     return catalogPO;
   }
 
+  public CatalogIds getCatalogIdByMetalakeAndCatalogName(String metalakeName, 
String catalogName) {
+    return SessionUtils.getWithoutCommit(
+        CatalogMetaMapper.class,
+        mapper -> 
mapper.selectCatalogIdByMetalakeNameAndCatalogName(metalakeName, catalogName));
+  }
+
   // Catalog may be deleted, so the CatalogPO may be null.
   @Nullable
   public CatalogPO getCatalogPOById(Long catalogId) {
diff --git 
a/core/src/main/java/org/apache/gravitino/storage/relational/service/CommonMetaService.java
 
b/core/src/main/java/org/apache/gravitino/storage/relational/service/CommonMetaService.java
index bdab2ad9fe..aa18d4bc56 100644
--- 
a/core/src/main/java/org/apache/gravitino/storage/relational/service/CommonMetaService.java
+++ 
b/core/src/main/java/org/apache/gravitino/storage/relational/service/CommonMetaService.java
@@ -20,7 +20,11 @@
 package org.apache.gravitino.storage.relational.service;
 
 import com.google.common.base.Preconditions;
+import org.apache.gravitino.Entity;
 import org.apache.gravitino.Namespace;
+import org.apache.gravitino.exceptions.NoSuchEntityException;
+import org.apache.gravitino.storage.relational.helper.CatalogIds;
+import org.apache.gravitino.storage.relational.helper.SchemaIds;
 
 /** The service class for common metadata operations. */
 public class CommonMetaService {
@@ -36,26 +40,52 @@ public class CommonMetaService {
     Preconditions.checkArgument(
         !namespace.isEmpty() && namespace.levels().length <= 3,
         "Namespace should not be empty and length should be less than or equal 
to 3.");
-    Long parentEntityId = null;
-    if (namespace.levels().length >= 1) {
-      parentEntityId = 
MetalakeMetaService.getInstance().getMetalakeIdByName(namespace.level(0));
-    }
 
-    if (namespace.levels().length >= 2) {
-      parentEntityId =
-          CatalogMetaService.getInstance()
-              .getCatalogIdByMetalakeIdAndName(parentEntityId, 
namespace.level(1));
-    }
+    int length = namespace.levels().length;
+    Long parentEntityId;
+    switch (length) {
+      case 1:
+        // Parent is a metalake
+        parentEntityId = 
MetalakeMetaService.getInstance().getMetalakeIdByName(namespace.level(0));
+        if (parentEntityId == null) {
+          throw new NoSuchEntityException(
+              NoSuchEntityException.NO_SUCH_ENTITY_MESSAGE,
+              Entity.EntityType.METALAKE.name().toLowerCase(),
+              namespace);
+        }
+
+        return parentEntityId;
+      case 2:
+        // Parent is a catalog
+        CatalogIds catalogIds =
+            CatalogMetaService.getInstance()
+                .getCatalogIdByMetalakeAndCatalogName(namespace.level(0), 
namespace.level(1));
+        parentEntityId = catalogIds == null ? null : catalogIds.getCatalogId();
+        if (parentEntityId == null) {
+          throw new NoSuchEntityException(
+              NoSuchEntityException.NO_SUCH_ENTITY_MESSAGE,
+              Entity.EntityType.CATALOG.name().toLowerCase(),
+              namespace);
+        }
 
-    if (namespace.levels().length >= 3) {
-      parentEntityId =
-          SchemaMetaService.getInstance()
-              .getSchemaIdByCatalogIdAndName(parentEntityId, 
namespace.level(2));
+        return parentEntityId;
+      case 3:
+        // Parent is a schema
+        SchemaIds schemaIds =
+            SchemaMetaService.getInstance()
+                .getSchemaIdByMetalakeNameAndCatalogNameAndSchemaName(
+                    namespace.level(0), namespace.level(1), 
namespace.level(2));
+        parentEntityId = schemaIds == null ? null : schemaIds.getSchemaId();
+        if (parentEntityId == null) {
+          throw new NoSuchEntityException(
+              NoSuchEntityException.NO_SUCH_ENTITY_MESSAGE,
+              Entity.EntityType.SCHEMA.name().toLowerCase(),
+              namespace);
+        }
+        return parentEntityId;
+      default:
+        throw new IllegalArgumentException("Namespace length should be less 
than or equal to 3.");
     }
-    Preconditions.checkState(
-        parentEntityId != null && parentEntityId > 0,
-        "Parent entity id should not be null and should be greater than 0.");
-    return parentEntityId;
   }
 
   public Long[] getParentEntityIdsByNamespace(Namespace namespace) {
@@ -63,23 +93,56 @@ public class CommonMetaService {
         !namespace.isEmpty() && namespace.levels().length <= 3,
         "Namespace should not be empty and length should be less than or equal 
to 3.");
     Long[] parentEntityIds = new Long[namespace.levels().length];
-    if (namespace.levels().length >= 1) {
-      parentEntityIds[0] =
-          
MetalakeMetaService.getInstance().getMetalakeIdByName(namespace.level(0));
-    }
 
-    if (namespace.levels().length >= 2) {
-      parentEntityIds[1] =
-          CatalogMetaService.getInstance()
-              .getCatalogIdByMetalakeIdAndName(parentEntityIds[0], 
namespace.level(1));
-    }
+    int length = namespace.levels().length;
+    switch (length) {
+      case 1:
+        // Parent is a metalake
+        parentEntityIds[0] =
+            
MetalakeMetaService.getInstance().getMetalakeIdByName(namespace.level(0));
+        if (parentEntityIds[0] == null) {
+          throw new NoSuchEntityException(
+              NoSuchEntityException.NO_SUCH_ENTITY_MESSAGE,
+              Entity.EntityType.METALAKE.name().toLowerCase(),
+              namespace);
+        }
 
-    if (namespace.levels().length >= 3) {
-      parentEntityIds[2] =
-          SchemaMetaService.getInstance()
-              .getSchemaIdByCatalogIdAndName(parentEntityIds[1], 
namespace.level(2));
-    }
+        return parentEntityIds;
+      case 2:
+        // Parent is a catalog
+        CatalogIds catalogIds =
+            CatalogMetaService.getInstance()
+                .getCatalogIdByMetalakeAndCatalogName(namespace.level(0), 
namespace.level(1));
+        parentEntityIds[0] = catalogIds == null ? null : 
catalogIds.getMetalakeId();
+        parentEntityIds[1] = catalogIds == null ? null : 
catalogIds.getCatalogId();
+
+        if (parentEntityIds[1] == null) {
+          throw new NoSuchEntityException(
+              NoSuchEntityException.NO_SUCH_ENTITY_MESSAGE,
+              Entity.EntityType.CATALOG.name().toLowerCase(),
+              namespace);
+        }
+        return parentEntityIds;
+      case 3:
+        // Parent is a schema
+        SchemaIds schemaIds =
+            SchemaMetaService.getInstance()
+                .getSchemaIdByMetalakeNameAndCatalogNameAndSchemaName(
+                    namespace.level(0), namespace.level(1), 
namespace.level(2));
+        parentEntityIds[0] = schemaIds == null ? null : 
schemaIds.getMetalakeId();
+        parentEntityIds[1] = schemaIds == null ? null : 
schemaIds.getCatalogId();
+        parentEntityIds[2] = schemaIds == null ? null : 
schemaIds.getSchemaId();
 
-    return parentEntityIds;
+        if (parentEntityIds[2] == null) {
+          throw new NoSuchEntityException(
+              NoSuchEntityException.NO_SUCH_ENTITY_MESSAGE,
+              Entity.EntityType.SCHEMA.name().toLowerCase(),
+              namespace);
+        }
+
+        return parentEntityIds;
+      default:
+        throw new IllegalArgumentException("Namespace length should be less 
than or equal to 3.");
+    }
   }
 }
diff --git 
a/core/src/main/java/org/apache/gravitino/storage/relational/service/SchemaMetaService.java
 
b/core/src/main/java/org/apache/gravitino/storage/relational/service/SchemaMetaService.java
index f300e70cae..447f3405c6 100644
--- 
a/core/src/main/java/org/apache/gravitino/storage/relational/service/SchemaMetaService.java
+++ 
b/core/src/main/java/org/apache/gravitino/storage/relational/service/SchemaMetaService.java
@@ -34,6 +34,7 @@ import org.apache.gravitino.meta.FilesetEntity;
 import org.apache.gravitino.meta.ModelEntity;
 import org.apache.gravitino.meta.SchemaEntity;
 import org.apache.gravitino.meta.TableEntity;
+import org.apache.gravitino.storage.relational.helper.SchemaIds;
 import org.apache.gravitino.storage.relational.mapper.FilesetMetaMapper;
 import org.apache.gravitino.storage.relational.mapper.FilesetVersionMapper;
 import org.apache.gravitino.storage.relational.mapper.ModelMetaMapper;
@@ -78,6 +79,15 @@ public class SchemaMetaService {
     return schemaPO;
   }
 
+  public SchemaIds getSchemaIdByMetalakeNameAndCatalogNameAndSchemaName(
+      String metalakeName, String catalogName, String schemaName) {
+    return SessionUtils.getWithoutCommit(
+        SchemaMetaMapper.class,
+        mapper ->
+            mapper.selectSchemaIdByMetalakeNameAndCatalogNameAndSchemaName(
+                metalakeName, catalogName, schemaName));
+  }
+
   // Schema may be deleted, so the SchemaPO may be null.
   public SchemaPO getSchemaPOById(Long schemaId) {
     return SessionUtils.getWithoutCommit(

Reply via email to