pinal-shah commented on code in PR #478:
URL: https://github.com/apache/atlas/pull/478#discussion_r3182249811


##########
repository/src/main/java/org/apache/atlas/repository/store/graph/v2/EntityGraphMapper.java:
##########
@@ -1533,6 +1527,88 @@ private void mapRelationshipAttributes(AtlasEntity 
entity, AtlasEntityType entit
         LOG.debug("<== mapRelationshipAttributes({}, {})", op, 
entity.getTypeName());
     }
 
+    private void mapRelationshipAttribute(AtlasEntity entity, AtlasEntityType 
entityType, String attrName, Object attrValue, AtlasVertex vertex, 
EntityOperation op, EntityMutationContext context) throws AtlasBaseException {
+        LOG.debug("==> mapRelationshipAttribute({}, {})", attrName, 
entity.getTypeName());
+        Set<String> relationshipTypeNames = 
entityType.getAttributeRelationshipTypes(attrName);
+
+        if (CollectionUtils.isEmpty(relationshipTypeNames)) {
+            mapRelationshipAttributeUsingInferredType(entityType, attrName, 
attrValue, vertex, op, context);
+            return;
+        }
+
+        if (attrValue instanceof Collection) {
+            Collection<?> relatedObjects = (Collection<?>) attrValue;
+
+            // Group related objects by their appropriate relationship type
+            // e.g., hive_table elements should use hive_table_db 
relationship, delta_table elements should use delta_table_db
+            Map<String, List<Object>> elementsByRelationshipType = 
groupElementsByRelationshipType(
+                    relatedObjects, attrName, relationshipTypeNames);
+
+            for (Map.Entry<String, List<Object>> entry : 
elementsByRelationshipType.entrySet()) {
+                String       relationshipTypeName = entry.getKey();
+                List<Object> filteredElements     = entry.getValue();
+
+                AtlasAttribute attribute = 
entityType.getRelationshipAttribute(attrName, relationshipTypeName);
+
+                if (attribute != null && 
CollectionUtils.isNotEmpty(filteredElements)) {
+                    // Use the same collection type as the original (List or 
Set)
+                    Object filteredValue = 
createCollectionOfSameType(attrValue, filteredElements);
+
+                    LOG.debug("Processing relationship type {} for attribute 
{} with {} elements", relationshipTypeName, attrName, filteredElements.size());
+
+                    mapAttribute(attribute, filteredValue, vertex, op, 
context);
+                }
+            }
+        } else if (attrValue instanceof Map) {
+            LOG.warn("mapRelationshipAttribute: attribute {} on {}: Map-valued 
relationship attribute is not supported", attrName, entity.getTypeName());
+        } else if (relationshipTypeNames.size() == 1) {

Review Comment:
   please review to remove 
   ```
   else if (relationshipTypeNames.size() == 1) {
               String         onlyRelationshipType = 
relationshipTypeNames.iterator().next();
               AtlasAttribute attribute            = 
entityType.getRelationshipAttribute(attrName, onlyRelationshipType);
               mapAttribute(attribute, attrValue, vertex, op, context);
           } else {
               mapRelationshipAttributeUsingInferredType(entityType, attrName, 
attrValue, vertex, op, context);
           }
   ```
   
   after addressing previous comment



##########
repository/src/main/java/org/apache/atlas/repository/store/graph/v2/EntityGraphMapper.java:
##########
@@ -1533,6 +1527,88 @@ private void mapRelationshipAttributes(AtlasEntity 
entity, AtlasEntityType entit
         LOG.debug("<== mapRelationshipAttributes({}, {})", op, 
entity.getTypeName());
     }
 
+    private void mapRelationshipAttribute(AtlasEntity entity, AtlasEntityType 
entityType, String attrName, Object attrValue, AtlasVertex vertex, 
EntityOperation op, EntityMutationContext context) throws AtlasBaseException {

Review Comment:
   No usage of AtlasEntity entity parameter in mapRelationshipAttribute()
   you can use AtlasEntityType entityType



##########
repository/src/main/java/org/apache/atlas/repository/store/graph/v2/EntityGraphMapper.java:
##########
@@ -1533,6 +1527,88 @@ private void mapRelationshipAttributes(AtlasEntity 
entity, AtlasEntityType entit
         LOG.debug("<== mapRelationshipAttributes({}, {})", op, 
entity.getTypeName());
     }
 
+    private void mapRelationshipAttribute(AtlasEntity entity, AtlasEntityType 
entityType, String attrName, Object attrValue, AtlasVertex vertex, 
EntityOperation op, EntityMutationContext context) throws AtlasBaseException {
+        LOG.debug("==> mapRelationshipAttribute({}, {})", attrName, 
entity.getTypeName());
+        Set<String> relationshipTypeNames = 
entityType.getAttributeRelationshipTypes(attrName);
+
+        if (CollectionUtils.isEmpty(relationshipTypeNames)) {

Review Comment:
   ```
       if ((CollectionUtils.isNotEmpty(relationshipTypeNames) && 
relationshipTypeNames.size() == 1) || (!(attrValue instanceof Collection) && 
!(attrValue instanceof Map)) || CollectionUtils.isEmpty(relationshipTypeNames)) 
{
           mapRelationshipAttributeUsingInferredType(entityType, attrName, 
attrValue, vertex, op, context);
           return;
       }
   ```
   Please review if above can be added
   Also please add helper method to check condition, something like
   
   isAttributeWithMulipleRelationshipTypes() {}



##########
repository/src/main/java/org/apache/atlas/repository/store/graph/v2/EntityGraphMapper.java:
##########
@@ -1533,6 +1527,88 @@ private void mapRelationshipAttributes(AtlasEntity 
entity, AtlasEntityType entit
         LOG.debug("<== mapRelationshipAttributes({}, {})", op, 
entity.getTypeName());
     }
 
+    private void mapRelationshipAttribute(AtlasEntity entity, AtlasEntityType 
entityType, String attrName, Object attrValue, AtlasVertex vertex, 
EntityOperation op, EntityMutationContext context) throws AtlasBaseException {
+        LOG.debug("==> mapRelationshipAttribute({}, {})", attrName, 
entity.getTypeName());
+        Set<String> relationshipTypeNames = 
entityType.getAttributeRelationshipTypes(attrName);
+
+        if (CollectionUtils.isEmpty(relationshipTypeNames)) {
+            mapRelationshipAttributeUsingInferredType(entityType, attrName, 
attrValue, vertex, op, context);
+            return;
+        }
+
+        if (attrValue instanceof Collection) {
+            Collection<?> relatedObjects = (Collection<?>) attrValue;
+
+            // Group related objects by their appropriate relationship type
+            // e.g., hive_table elements should use hive_table_db 
relationship, delta_table elements should use delta_table_db
+            Map<String, List<Object>> elementsByRelationshipType = 
groupElementsByRelationshipType(
+                    relatedObjects, attrName, relationshipTypeNames);
+
+            for (Map.Entry<String, List<Object>> entry : 
elementsByRelationshipType.entrySet()) {
+                String       relationshipTypeName = entry.getKey();
+                List<Object> filteredElements     = entry.getValue();
+
+                AtlasAttribute attribute = 
entityType.getRelationshipAttribute(attrName, relationshipTypeName);
+
+                if (attribute != null && 
CollectionUtils.isNotEmpty(filteredElements)) {
+                    // Use the same collection type as the original (List or 
Set)
+                    Object filteredValue = 
createCollectionOfSameType(attrValue, filteredElements);
+
+                    LOG.debug("Processing relationship type {} for attribute 
{} with {} elements", relationshipTypeName, attrName, filteredElements.size());
+
+                    mapAttribute(attribute, filteredValue, vertex, op, 
context);
+                }
+            }
+        } else if (attrValue instanceof Map) {
+            LOG.warn("mapRelationshipAttribute: attribute {} on {}: Map-valued 
relationship attribute is not supported", attrName, entity.getTypeName());
+        } else if (relationshipTypeNames.size() == 1) {
+            String         onlyRelationshipType = 
relationshipTypeNames.iterator().next();
+            AtlasAttribute attribute            = 
entityType.getRelationshipAttribute(attrName, onlyRelationshipType);
+            mapAttribute(attribute, attrValue, vertex, op, context);
+        } else {
+            mapRelationshipAttributeUsingInferredType(entityType, attrName, 
attrValue, vertex, op, context);
+        }
+
+        LOG.debug("<== mapRelationshipAttribute({}, {})", attrName, 
entity.getTypeName());
+    }
+
+    private void mapRelationshipAttributeUsingInferredType(AtlasEntityType 
entityType, String attrName, Object attrValue, AtlasVertex vertex, 
EntityOperation op, EntityMutationContext context) throws AtlasBaseException {
+        String         relationType = 
AtlasEntityUtil.getRelationshipType(attrValue);
+        AtlasAttribute attribute    = 
entityType.getRelationshipAttribute(attrName, relationType);
+        mapAttribute(attribute, attrValue, vertex, op, context);
+    }
+
+    private Map<String, List<Object>> 
groupElementsByRelationshipType(Collection<?> relatedObjects, String attrName, 
Set<String> relationshipTypeNames) {
+        Map<String, List<Object>> elementsByRelationshipType = new HashMap<>();
+
+        // Group related objects by their appropriate relationship type
+        for (Object element : relatedObjects) {
+            String relationshipType = 
AtlasEntityUtil.getRelationshipType(element);
+
+            if (StringUtils.isEmpty(relationshipType) && relationshipTypeNames 
!= null && relationshipTypeNames.size() == 1) {

Review Comment:
   Please review to remove 1588 to 1590 line after addressing above comments



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