You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@atlas.apache.org by si...@apache.org on 2021/08/09 20:34:47 UTC

[atlas] 02/03: ATLAS-4386 : Relationship search: Sorting does not work when passed relationship label

This is an automated email from the ASF dual-hosted git repository.

sidmishra pushed a commit to branch branch-2.0
in repository https://gitbox.apache.org/repos/asf/atlas.git

commit ef0a450ae1f13e984bc974cf82d0096275acdc2d
Author: Pinal <pinal-shah>
AuthorDate: Wed Aug 4 20:21:34 2021 +0530

    ATLAS-4386 : Relationship search: Sorting does not work when passed relationship label
    
    Signed-off-by: Pinal <pinal-shah>
    (cherry picked from commit e3cbaabfba4f0aeb606c696492747759a8d34423)
---
 .../atlas/discovery/EntityDiscoveryService.java    | 13 ++++--
 .../apache/atlas/repository/graph/GraphHelper.java | 21 +++++++++
 .../atlas/discovery/AtlasDiscoveryServiceTest.java | 51 ++++++++++++++++++++++
 3 files changed, 82 insertions(+), 3 deletions(-)

diff --git a/repository/src/main/java/org/apache/atlas/discovery/EntityDiscoveryService.java b/repository/src/main/java/org/apache/atlas/discovery/EntityDiscoveryService.java
index f2290c6..5b3b948 100644
--- a/repository/src/main/java/org/apache/atlas/discovery/EntityDiscoveryService.java
+++ b/repository/src/main/java/org/apache/atlas/discovery/EntityDiscoveryService.java
@@ -585,13 +585,20 @@ public class EntityDiscoveryService implements AtlasDiscoveryService {
         }
 
         if (attribute != null) {
+            //get end entity type through relationship attribute
             endEntityType = attribute.getReferencedEntityType(typeRegistry);
 
-            if (endEntityType != null) {
-                relation = attribute.getRelationshipEdgeLabel();
-            } else {
+            if (endEntityType == null) {
                 throw new AtlasBaseException(AtlasErrorCode.INVALID_RELATIONSHIP_ATTRIBUTE, relation, attribute.getTypeName());
             }
+            relation = attribute.getRelationshipEdgeLabel();
+        } else {
+            //get end entity type through label
+            String endEntityTypeName = GraphHelper.getReferencedEntityTypeName(entityVertex, relation);
+
+            if (StringUtils.isNotEmpty(endEntityTypeName)) {
+                endEntityType = typeRegistry.getEntityTypeByName(endEntityTypeName);
+            }
         }
 
         //validate sortBy attribute
diff --git a/repository/src/main/java/org/apache/atlas/repository/graph/GraphHelper.java b/repository/src/main/java/org/apache/atlas/repository/graph/GraphHelper.java
index ff7fd15..d5112bb 100755
--- a/repository/src/main/java/org/apache/atlas/repository/graph/GraphHelper.java
+++ b/repository/src/main/java/org/apache/atlas/repository/graph/GraphHelper.java
@@ -1605,6 +1605,27 @@ public final class GraphHelper {
         return ret;
     }
 
+    //get entity type of relationship (End vertex entity type) from relationship label
+    public static String getReferencedEntityTypeName(AtlasVertex entityVertex, String relation) {
+        String ret = null;
+        Iterator<AtlasEdge> edges    = GraphHelper.getAdjacentEdgesByLabel(entityVertex, AtlasEdgeDirection.BOTH, relation);
+
+        if (edges != null && edges.hasNext()) {
+            AtlasEdge   relationEdge = edges.next();
+            AtlasVertex outVertex    = relationEdge.getOutVertex();
+            AtlasVertex inVertex     = relationEdge.getInVertex();
+
+            if (outVertex != null && inVertex != null) {
+                String outVertexId    = outVertex.getIdForDisplay();
+                String entityVertexId = entityVertex.getIdForDisplay();
+                AtlasVertex endVertex = StringUtils.equals(outVertexId, entityVertexId) ? inVertex : outVertex;
+                ret                   = GraphHelper.getTypeName(endVertex);
+            }
+        }
+
+       return ret;
+    }
+
     public static boolean isRelationshipEdge(AtlasEdge edge) {
         if (edge == null) {
             return false;
diff --git a/repository/src/test/java/org/apache/atlas/discovery/AtlasDiscoveryServiceTest.java b/repository/src/test/java/org/apache/atlas/discovery/AtlasDiscoveryServiceTest.java
index a9fbd43..5e47295 100644
--- a/repository/src/test/java/org/apache/atlas/discovery/AtlasDiscoveryServiceTest.java
+++ b/repository/src/test/java/org/apache/atlas/discovery/AtlasDiscoveryServiceTest.java
@@ -20,6 +20,7 @@ package org.apache.atlas.discovery;
 import org.apache.atlas.ApplicationProperties;
 import org.apache.atlas.AtlasClient;
 import org.apache.atlas.BasicTestSetup;
+import org.apache.atlas.SortOrder;
 import org.apache.atlas.TestModules;
 import org.apache.atlas.exception.AtlasBaseException;
 import org.apache.atlas.model.discovery.AtlasQuickSearchResult;
@@ -54,6 +55,8 @@ public class AtlasDiscoveryServiceTest extends BasicTestSetup {
     @Inject
     private AtlasDiscoveryService discoveryService;
 
+    String salesFactGuid = null;
+
     @BeforeClass
     public void setup() throws Exception {
         super.initialize();
@@ -855,6 +858,54 @@ public class AtlasDiscoveryServiceTest extends BasicTestSetup {
         }
     }
 
+    @Test
+    public void searchRelatedEntitiesSortAsc() throws AtlasBaseException {
+        String guid = gethiveTableSalesFactGuid();
+
+        SearchParameters params = new SearchParameters();
+        params.setLimit(10);
+        AtlasSearchResult relResult  = discoveryService.searchRelatedEntities(guid, "__hive_table.columns", false, params);
+        List<AtlasEntityHeader> list = relResult.getEntities();
+
+        Assert.assertTrue(CollectionUtils.isNotEmpty(list));
+        Assert.assertTrue(list.size() == 4);
+        Assert.assertTrue(list.get(0).getDisplayText().equalsIgnoreCase("customer_id"));
+        Assert.assertTrue(list.get(3).getDisplayText().equalsIgnoreCase("time_id"));
+
+    }
+
+    @Test
+    public void searchRelatedEntitiesSortDesc() throws AtlasBaseException {
+        String guid = gethiveTableSalesFactGuid();
+
+        SearchParameters params = new SearchParameters();
+        params.setLimit(10);
+        params.setSortOrder(SortOrder.DESCENDING);
+
+        AtlasSearchResult relResult  = discoveryService.searchRelatedEntities(guid, "columns", false, params);
+        List<AtlasEntityHeader> list = relResult.getEntities();
+
+        Assert.assertTrue(CollectionUtils.isNotEmpty(list));
+        Assert.assertTrue(list.size() == 4);
+        Assert.assertTrue(list.get(3).getDisplayText().equalsIgnoreCase("customer_id"));
+        Assert.assertTrue(list.get(0).getDisplayText().equalsIgnoreCase("time_id"));
+    }
+
+    private String gethiveTableSalesFactGuid() throws AtlasBaseException {
+        if (salesFactGuid == null) {
+            SearchParameters params = new SearchParameters();
+            params.setTypeName(HIVE_TABLE_TYPE);
+            SearchParameters.FilterCriteria filterCriteria = getSingleFilterCondition("name",Operator.EQ, "sales_fact");
+            params.setEntityFilters(filterCriteria);
+            AtlasSearchResult result = discoveryService.searchWithParameters(params);
+
+            if (result != null && CollectionUtils.isNotEmpty(result.getEntities())) {
+                salesFactGuid = result.getEntities().get(0).getGuid();
+            }
+        }
+        return salesFactGuid;
+    }
+
     private void assertSearchResult(AtlasSearchResult searchResult, int expected, String query) {
         assertNotNull(searchResult);
         if(expected == 0) {