World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#7688] fix(server): Fix duplicated tags when list tags for a metadata object. (#7830)
Mini Yu96574af885d9
diff --git a/clients/client-java/src/test/java/org/apache/gravitino/client/integration/test/TagIT.java b/clients/client-java/src/test/java/org/apache/gravitino/client/integration/test/TagIT.javaindex ecc4295a9a..bea92fde3f 100644--- a/clients/client-java/src/test/java/org/apache/gravitino/client/integration/test/TagIT.java+++ b/clients/client-java/src/test/java/org/apache/gravitino/client/integration/test/TagIT.java@@ -434,7 +434,7 @@ public class TagIT extends BaseIT {// Test list associated tags with details for schemaTag[] tags2 = schema.supportsTags().listTagsInfo();- Assertions.assertEquals(3, tags2.length);+ Assertions.assertEquals(2, tags2.length);Set<Tag> nonInheritedTags =Arrays.stream(tags2).filter(tag -> !tag.inherited().get()).collect(Collectors.toSet());@@ -442,10 +442,9 @@ public class TagIT extends BaseIT {Arrays.stream(tags2).filter(tag -> tag.inherited().get()).collect(Collectors.toSet());Assertions.assertEquals(2, nonInheritedTags.size());- Assertions.assertEquals(1, inheritedTags.size());+ Assertions.assertEquals(0, inheritedTags.size());Assertions.assertTrue(nonInheritedTags.contains(tag1));Assertions.assertTrue(nonInheritedTags.contains(tag2));- Assertions.assertTrue(inheritedTags.contains(tag1));Assertions.assertFalse(inheritedTags.contains(tag2));// Test get associated tag for schemadiff --git a/server/src/main/java/org/apache/gravitino/server/web/rest/MetadataObjectTagOperations.java b/server/src/main/java/org/apache/gravitino/server/web/rest/MetadataObjectTagOperations.javaindex 9c3eda52b3..2abc645e60 100644--- a/server/src/main/java/org/apache/gravitino/server/web/rest/MetadataObjectTagOperations.java+++ b/server/src/main/java/org/apache/gravitino/server/web/rest/MetadataObjectTagOperations.java@@ -20,12 +20,11 @@ package org.apache.gravitino.server.web.rest;import com.codahale.metrics.annotation.ResponseMetered;import com.codahale.metrics.annotation.Timed;-import com.google.common.collect.Lists;import java.util.Arrays;import java.util.Collections;-import java.util.List;import java.util.Locale;import java.util.Optional;+import java.util.Set;import javax.inject.Inject;import javax.servlet.http.HttpServletRequest;import javax.ws.rs.DefaultValue;@@ -51,6 +50,7 @@ import org.apache.gravitino.metrics.MetricNames;import org.apache.gravitino.server.web.Utils;import org.apache.gravitino.tag.Tag;import org.apache.gravitino.tag.TagDispatcher;+import org.glassfish.jersey.internal.guava.Sets;import org.slf4j.Logger;import org.slf4j.LoggerFactory;@@ -163,7 +163,7 @@ public class MetadataObjectTagOperations {MetadataObjects.parse(fullName, MetadataObject.Type.valueOf(type.toUpperCase(Locale.ROOT)));- List<TagDTO> tags = Lists.newArrayList();+ Set<TagDTO> tags = Sets.newHashSet();Tag[] nonInheritedTags = tagDispatcher.listTagsInfoForMetadataObject(metalake, object);if (ArrayUtils.isNotEmpty(nonInheritedTags)) {Collections.addAll(@@ -197,9 +197,8 @@ public class MetadataObjectTagOperations {return Utils.ok(new TagListResponse(tags.toArray(new TagDTO[0])));} else {- // Due to same name tag will be associated to both parent and child objects, so we- // need to deduplicate the tag names.- String[] tagNames = tags.stream().map(TagDTO::name).distinct().toArray(String[]::new);+ // We have used Set to avoid duplicate tag names+ String[] tagNames = tags.stream().map(TagDTO::name).toArray(String[]::new);LOG.info("List {} tags for object type: {}, full name: {} under metalake: {}",diff --git a/server/src/test/java/org/apache/gravitino/server/web/rest/TestMetadataObjectTagOperations.java b/server/src/test/java/org/apache/gravitino/server/web/rest/TestMetadataObjectTagOperations.javaindex 8a1d09d21e..f95f0ca5a1 100644--- a/server/src/test/java/org/apache/gravitino/server/web/rest/TestMetadataObjectTagOperations.java+++ b/server/src/test/java/org/apache/gravitino/server/web/rest/TestMetadataObjectTagOperations.java@@ -338,6 +338,47 @@ public class TestMetadataObjectTagOperations extends JerseyTest {Assertions.assertTrue(resultNames2.contains("tag3"));Assertions.assertTrue(resultNames2.contains("tag5"));Assertions.assertTrue(resultNames2.contains("tag7"));++ tableTagInfos =+ new Tag[] {+ TagEntity.builder().withName("tag5").withId(1L).withAuditInfo(testAuditInfo1).build(),+ // Tag tag3 already associated with schema+ TagEntity.builder().withName("tag3").withId(2L).withAuditInfo(testAuditInfo1).build(),+ // Tag tag1 already associated with catalog+ TagEntity.builder().withName("tag1").withId(3L).withAuditInfo(testAuditInfo1).build(),+ TagEntity.builder().withName("tag0").withId(3L).withAuditInfo(testAuditInfo1).build()+ };+ when(tagManager.listTagsInfoForMetadataObject(metalake, table)).thenReturn(tableTagInfos);++ Response response8 =+ target(basePath(metalake))+ .path(table.type().toString())+ .path(table.fullName())+ .path("tags")+ .queryParam("details", true)+ .request(MediaType.APPLICATION_JSON_TYPE)+ .accept("application/vnd.gravitino.v1+json")+ .get();++ Assertions.assertEquals(Response.Status.OK.getStatusCode(), response8.getStatus());++ TagListResponse tagListResponse8 = response8.readEntity(TagListResponse.class);+ Assertions.assertEquals(0, tagListResponse8.getCode());+ Assertions.assertEquals(4, tagListResponse8.getTags().length);++ Map<String, Tag> resultTags8 =+ Arrays.stream(tagListResponse8.getTags())+ .collect(Collectors.toMap(Tag::name, Function.identity()));++ Assertions.assertTrue(resultTags8.containsKey("tag0"));+ Assertions.assertTrue(resultTags8.containsKey("tag1"));+ Assertions.assertTrue(resultTags8.containsKey("tag3"));+ Assertions.assertTrue(resultTags8.containsKey("tag5"));++ Assertions.assertFalse(resultTags8.get("tag1").inherited().get());+ Assertions.assertFalse(resultTags8.get("tag3").inherited().get());+ Assertions.assertFalse(resultTags8.get("tag5").inherited().get());+ Assertions.assertFalse(resultTags8.get("tag0").inherited().get());}@Test