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


##########
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:
   Addressed in eeaf0c76e4. The resolver now reads the selected domain first, 
even for small scopes. An empty selection returns immediately. In the 
single-table case, the second lookup uses the selected table ID regardless of 
how many assignments the other domain has.
   
   I tested your one-table / 10,000 disjoint assignments case on MySQL 8.0.46 
and PostgreSQL 13.23. On both engines the old plan already used an index, so I 
didn’t see the large scan you observed on H2. The new path adds one round trip 
(2 → 3 SELECTs). Over 24 measured runs, median resolver time went from 3.83 to 
6.16 ms on MySQL and from 2.28 to 4.43 ms on PostgreSQL.



##########
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:
   Addressed in eeaf0c76e4. The second pass now attaches each remaining domain 
path to the object from the first pass using its type and ID. It still checks 
the backing tag ID and value. I added a regression test that verifies the 
second pass returns the same object instance.
   
   With 1,001 selected views, 502 visible domains and one overlapping view, the 
second pass issued just the assignment query. The view, schema and catalog 
lookups disappeared from the SQL trace. Total SELECTs fell from 8 to 5 on both 
engines, with identical members and paths. Over 24 measured runs, median 
resolver time fell from 38.41 to 31.46 ms on MySQL and from 32.21 to 24.17 ms 
on PostgreSQL. With only two visible domains, reading the selection first added 
a round trip (4 → 5 SELECTs), and the same workload was slower on both engines.



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