mchades commented on code in PR #14967:
URL: https://github.com/apache/gravitino/pull/14967#discussion_r4582371542


##########
core/src/main/java/org/apache/gravitino/domain/DomainMembershipResolver.java:
##########
@@ -89,60 +102,192 @@ DomainMember[] listMembers(
                         || filter.domains().contains(d.name()))
             .collect(Collectors.toList());
 
-    // Expand explicit tag assignments into recorded leaf objects that may 
inherit the tag.
-    Set<MetadataObject> candidates = new LinkedHashSet<>();
+    List<DomainMember> members =
+        filter.expand()
+            ? listExpandedMembers(metalake, queriedDomain, scopeDomains, 
selectedDomains, filter)
+            : listDirectMembers(metalake, queriedDomain, scopeDomains, 
selectedDomains, filter);
+
+    members.sort(
+        Comparator.comparing((DomainMember m) -> m.object().type().name())
+            .thenComparing(m -> m.object().fullName()));
+    return members.toArray(new DomainMember[0]);
+  }
+
+  /**
+   * Lists the objects that carry a backing tag themselves, including catalogs 
and schemas, without
+   * expanding them or resolving effective tags.
+   */
+  private List<DomainMember> listDirectMembers(
+      String metalake,
+      DomainEntity queriedDomain,
+      List<DomainEntity> scopeDomains,
+      List<DomainEntity> selectedDomains,
+      DomainMemberFilter filter) {
+    Set<Long> selectedIds =
+        
selectedDomains.stream().map(DomainEntity::id).collect(Collectors.toSet());
+    boolean needsOtherBindings =
+        scopeDomains.stream()
+            .anyMatch(d -> d.backingTagId() != null && 
!selectedIds.contains(d.id()));
+    // A selective name filter can discard most candidates after name 
resolution. In that case
+    // only fetch additional paths for the remaining objects. Bound the IN 
list for large scopes.
+    boolean joinPaths =
+        needsOtherBindings
+            && !selectedIds.isEmpty()
+            && selectedIds.size() <= MEMBERSHIP_LOOKUP_BATCH_SIZE
+            && !filter.nameContains().isPresent();

Review Comment:
   A name filter can match a whole schema. I traced a selected child with 
10,000 tables in `cat.sales`, one of which also belonged to the parent. Adding 
`nameContains=cat.sales` returned identical members and relations but raised 
the H2 statement count from 3 to 22, including 20 tag lookups by object ID. Can 
we apply the name filter to the first-pass candidates and choose the second 
lookup from the number that survive? A filter matching one table can still use 
the ID path.



##########
core/src/main/java/org/apache/gravitino/storage/relational/mapper/TagMetadataObjectRelMapper.java:
##########
@@ -19,19 +19,62 @@
 package org.apache.gravitino.storage.relational.mapper;
 
 import java.util.List;
+import java.util.Set;
 import javax.annotation.Nullable;
+import org.apache.gravitino.MetadataObject;
+import org.apache.gravitino.storage.relational.po.DomainAssignmentPO;
 import org.apache.gravitino.storage.relational.po.TagMetadataObjectRelPO;
 import org.apache.gravitino.storage.relational.po.TagPO;
 import org.apache.gravitino.storage.relational.po.TagRelationTagPO;
 import org.apache.ibatis.annotations.DeleteProvider;
 import org.apache.ibatis.annotations.InsertProvider;
 import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.Select;
 import org.apache.ibatis.annotations.SelectProvider;
 import org.apache.ibatis.annotations.UpdateProvider;
 
 public interface TagMetadataObjectRelMapper {
   String TAG_METADATA_OBJECT_RELATION_TABLE_NAME = "tag_relation_meta";
 
+  /**
+   * Reads selected domain assignments before resolving object names.
+   *
+   * @param metalake The containing metalake.
+   * @param domainIds Selected domain IDs.
+   * @param types Selected object types.
+   * @param candidateDomainIds Domains supplying the candidate objects, or 
empty for all.
+   * @return Assignment rows with the matching domain ID.
+   */
+  @Select({
+    "<script>",
+    "SELECT d.domain_id AS domainId, r.tag_id AS tagId, r.tag_value AS 
tagValue,",
+    "r.metadata_object_id AS metadataObjectId, r.metadata_object_type AS 
metadataObjectType",
+    "FROM domain_meta d JOIN metalake_meta m ON m.metalake_id = d.metalake_id",
+    "JOIN tag_meta t ON t.tag_id = d.backing_tag_id AND t.metalake_id = 
m.metalake_id",
+    "JOIN tag_relation_meta r ON r.tag_id = t.tag_id",
+    "WHERE m.metalake_name = #{metalake} AND m.deleted_at = 0 AND d.deleted_at 
= 0",
+    "AND t.deleted_at = 0 AND r.deleted_at = 0",
+    "AND (d.backing_tag_value IS NULL OR d.backing_tag_value = r.tag_value)",

Review Comment:
   This predicate uses the current domain binding, while `TagMetaService` 
checks the returned rows against the snapshot supplied to `listMembers`. On H2 
I assigned both `dept=finance` and `dept=hr` to two tables, saved the Finance 
domain snapshot, then rebound it from `finance` to `hr`. Listing with the saved 
snapshot returned zero tables, although both tables match either binding. Can 
the assignment query use the supplied snapshot’s tag ID and value throughout?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to