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


##########
core/src/main/java/org/apache/gravitino/domain/DomainMembershipResolver.java:
##########
@@ -89,60 +102,201 @@ 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();
+    Map<MetadataObject.Type, Map<Long, MetadataObject>> candidates =
+        new EnumMap<>(MetadataObject.Type.class);
+    Table<MetadataObject.Type, Long, DirectMemberPaths> relations = 
HashBasedTable.create();
+    List<DomainMember> members = new ArrayList<>();
     try {
-      for (DomainEntity selectedDomain : selectedDomains) {
-        if (selectedDomain.backingTagId() == null) {
+      Set<MetadataObject.Type> types =
+          filter.types().isEmpty() ? DIRECT_MEMBER_TYPES : filter.types();
+      List<DomainEntity> lookupDomains = joinPaths ? scopeDomains : 
selectedDomains;
+      Map<Long, List<GenericEntity>> assigned;
+      if (joinPaths
+          && scopeDomains.stream()
+                  .filter(d -> d.backingTagId() != null)
+                  .limit(MEMBERSHIP_LOOKUP_BATCH_SIZE + 1)
+                  .count()
+              > MEMBERSHIP_LOOKUP_BATCH_SIZE) {

Review Comment:
   With 500 or fewer bound domains, a domain filter takes the joined path even 
when it selects a single table. On H2 2.2.224, with one table in the selected 
domain and 10,000 disjoint table assignments in the other, `EXPLAIN ANALYZE` 
reported a scan count of 10,003 on the outer `tag_relation_meta`. Reading the 
selected assignments first and then looking up that object ID gave scan counts 
of 2 and 3. Can we use the selected candidate count to choose the lookup for 
small scopes too?



##########
core/src/main/java/org/apache/gravitino/domain/DomainMembershipResolver.java:
##########
@@ -89,60 +102,201 @@ 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();
+    Map<MetadataObject.Type, Map<Long, MetadataObject>> candidates =
+        new EnumMap<>(MetadataObject.Type.class);
+    Table<MetadataObject.Type, Long, DirectMemberPaths> relations = 
HashBasedTable.create();
+    List<DomainMember> members = new ArrayList<>();
     try {
-      for (DomainEntity selectedDomain : selectedDomains) {
-        if (selectedDomain.backingTagId() == null) {
+      Set<MetadataObject.Type> types =
+          filter.types().isEmpty() ? DIRECT_MEMBER_TYPES : filter.types();
+      List<DomainEntity> lookupDomains = joinPaths ? scopeDomains : 
selectedDomains;
+      Map<Long, List<GenericEntity>> assigned;
+      if (joinPaths
+          && scopeDomains.stream()
+                  .filter(d -> d.backingTagId() != null)
+                  .limit(MEMBERSHIP_LOOKUP_BATCH_SIZE + 1)
+                  .count()
+              > MEMBERSHIP_LOOKUP_BATCH_SIZE) {
+        assigned =
+            new HashMap<>(
+                listAssignments(metalake, selectedDomains, types, 
Collections.emptySet()));
+        if (assigned.values().stream().allMatch(List::isEmpty)) {
+          return members;
+        }
+        List<DomainEntity> remaining =
+            scopeDomains.stream()
+                .filter(d -> d.backingTagId() != null && 
!selectedIds.contains(d.id()))
+                .collect(Collectors.toList());
+        joinPaths = fewerDomainBatches(assigned, remaining.size());
+        if (joinPaths) {
+          assigned.putAll(listAssignments(metalake, remaining, types, 
selectedIds));

Review Comment:
   This second call resolves names that the first call already loaded. For 
example, with 1,001 selected views and one remaining batch of domains, the 
heuristic picks the join. If even one view overlaps, that costs four more 
queries (assignments, views, schemas, and catalogs), versus three object-ID 
batches. The display name already comes from the selected assignment. Could 
this second pass keep the tag/value checks and attach the remaining relations 
by object type and ID without looking up names again?



-- 
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