From dad72d2f776fe0ef5a4c93422f340320b83acf43 Mon Sep 17 00:00:00 2001 From: Giuseppe Villani Date: Thu, 24 Nov 2022 19:33:25 +0100 Subject: [PATCH] [Zl3h9sM8] apoc.schema.relationships return wrong output for relationship indexes (neo4j/apoc#247) * [Zl3h9sM8] apoc.schema.relationships return wrong output for relationship indexes * added getIndexType(indexDescriptor) method * changed attr names --- .../apoc/result/IndexConstraintNodeInfo.java | 6 +++--- .../IndexConstraintRelationshipInfo.java | 9 ++++++--- core/src/main/java/apoc/schema/Schemas.java | 18 ++++++++++++------ .../schema/SchemasEnterpriseFeaturesTest.java | 2 ++ .../src/test/java/apoc/schema/SchemasTest.java | 9 ++++++--- 5 files changed, 29 insertions(+), 15 deletions(-) diff --git a/core/src/main/java/apoc/result/IndexConstraintNodeInfo.java b/core/src/main/java/apoc/result/IndexConstraintNodeInfo.java index 75a25c1d4d..f3a251a28a 100644 --- a/core/src/main/java/apoc/result/IndexConstraintNodeInfo.java +++ b/core/src/main/java/apoc/result/IndexConstraintNodeInfo.java @@ -34,18 +34,18 @@ public class IndexConstraintNodeInfo { * @param label * @param properties * @param status status of the index, if it's a constraint it will be empty - * @param type if it is an index type will be "INDEX" otherwise it will be the type of constraint + * @param schemaType if it is an index type will be "INDEX" otherwise it will be the type of constraint * @param failure * @param populationProgress * @param size * @param userDescription */ - public IndexConstraintNodeInfo(String name, Object label, List properties, String status, String type, String failure, float populationProgress, long size, double valuesSelectivity, String userDescription) { + public IndexConstraintNodeInfo(String name, Object label, List properties, String status, String schemaType, String failure, float populationProgress, long size, double valuesSelectivity, String userDescription) { this.name = name; this.label = label; this.properties = properties; this.status = status; - this.type = type; + this.type = schemaType; this.failure = failure; this.populationProgress = populationProgress; this.size = size; diff --git a/core/src/main/java/apoc/result/IndexConstraintRelationshipInfo.java b/core/src/main/java/apoc/result/IndexConstraintRelationshipInfo.java index 9f1af7e69b..a51e03e7dd 100644 --- a/core/src/main/java/apoc/result/IndexConstraintRelationshipInfo.java +++ b/core/src/main/java/apoc/result/IndexConstraintRelationshipInfo.java @@ -9,16 +9,19 @@ public class IndexConstraintRelationshipInfo { public final String name; - public final Object type; + public final String type; public final List properties; public final String status; + + public final Object relationshipType; - public IndexConstraintRelationshipInfo(String name, Object type, List properties, String status) { + public IndexConstraintRelationshipInfo(String name, String schemaType, List properties, String status, Object relationshipType) { this.name = name; - this.type = type; + this.type = schemaType; this.properties = properties; this.status = status; + this.relationshipType = relationshipType; } } diff --git a/core/src/main/java/apoc/schema/Schemas.java b/core/src/main/java/apoc/schema/Schemas.java index f81d971caf..62c41ee2e3 100644 --- a/core/src/main/java/apoc/schema/Schemas.java +++ b/core/src/main/java/apoc/schema/Schemas.java @@ -528,7 +528,7 @@ private IndexConstraintNodeInfo nodeInfoFromIndexDefinition(IndexDescriptor inde labelName, properties, schemaRead.indexGetState(indexDescriptor).toString(), - !indexDescriptor.isUnique() ? "INDEX" : "UNIQUENESS", + getIndexType(indexDescriptor), schemaRead.indexGetState(indexDescriptor).equals(InternalIndexState.FAILED) ? schemaRead.indexGetFailure(indexDescriptor) : "NO FAILURE", schemaRead.indexGetPopulationProgress(indexDescriptor).getCompleted() / schemaRead.indexGetPopulationProgress(indexDescriptor).getTotal() * 100, schemaRead.indexSize(indexDescriptor), @@ -542,7 +542,7 @@ private IndexConstraintNodeInfo nodeInfoFromIndexDefinition(IndexDescriptor inde labelName, properties, "NOT_FOUND", - !indexDescriptor.isUnique() ? "INDEX" : "UNIQUENESS", + getIndexType(indexDescriptor), "NOT_FOUND", 0,0,0, indexDescriptor.userDescription(tokens) @@ -568,14 +568,15 @@ private IndexConstraintRelationshipInfo relationshipInfoFromIndexDescription(Ind .mapToObj(tokens::propertyKeyGetName) .collect(Collectors.toList()); try { - return new IndexConstraintRelationshipInfo(getSchemaInfoName(relName, properties), relName, properties, schemaRead.indexGetState(indexDescriptor).toString()); + return new IndexConstraintRelationshipInfo(getSchemaInfoName(relName, properties), "INDEX", properties, schemaRead.indexGetState(indexDescriptor).toString(), relName); } catch (IndexNotFoundKernelException e) { return new IndexConstraintRelationshipInfo( // Pretty print for index name getSchemaInfoName(relName, properties), - relName, + getIndexType(indexDescriptor), properties, - "NOT_FOUND" + "NOT_FOUND", + relName ); } } @@ -591,10 +592,15 @@ private IndexConstraintRelationshipInfo relationshipInfoFromConstraintDefinition String.format("CONSTRAINT %s", constraintDefinition.toString()), constraintDefinition.getConstraintType().name(), Iterables.asList(constraintDefinition.getPropertyKeys()), - "" + "", + constraintDefinition.getRelationshipType().name() ); } + private static String getIndexType(IndexDescriptor indexDescriptor) { + return indexDescriptor.isUnique() ? "UNIQUENESS" : "INDEX"; + } + private String getSchemaInfoName(Object labelOrType, List properties) { final String labelOrTypeAsString = labelOrType instanceof String ? (String) labelOrType : StringUtils.join(labelOrType, ","); return String.format(":%s(%s)", labelOrTypeAsString, StringUtils.join(properties, ",")); diff --git a/core/src/test/java/apoc/schema/SchemasEnterpriseFeaturesTest.java b/core/src/test/java/apoc/schema/SchemasEnterpriseFeaturesTest.java index ff278213d1..c52f48f5f1 100644 --- a/core/src/test/java/apoc/schema/SchemasEnterpriseFeaturesTest.java +++ b/core/src/test/java/apoc/schema/SchemasEnterpriseFeaturesTest.java @@ -373,6 +373,7 @@ public void testSchemaRelationships() { Map r = result.next(); assertEquals("CONSTRAINT ON ()-[liked:LIKED]-() ASSERT (liked.day) IS NOT NULL", r.get("name")); assertEquals("RELATIONSHIP_PROPERTY_EXISTENCE", r.get("type")); + assertEquals("LIKED", r.get("relationshipType")); assertEquals(asList("day"), r.get("properties")); assertEquals(StringUtils.EMPTY, r.get("status")); assertFalse(result.hasNext()); @@ -397,6 +398,7 @@ public void testSchemaNodeWithRelationshipsConstraintsAndViceVersa() { Map r = result.next(); assertEquals("CONSTRAINT ON ()-[liked:LIKED]-() ASSERT (liked.day) IS NOT NULL", r.get("name")); assertEquals("RELATIONSHIP_PROPERTY_EXISTENCE", r.get("type")); + assertEquals("LIKED", r.get("relationshipType")); assertEquals(asList("day"), r.get("properties")); assertEquals(StringUtils.EMPTY, r.get("status")); assertFalse(result.hasNext()); diff --git a/core/src/test/java/apoc/schema/SchemasTest.java b/core/src/test/java/apoc/schema/SchemasTest.java index a03c48f8ee..b99f939b6e 100644 --- a/core/src/test/java/apoc/schema/SchemasTest.java +++ b/core/src/test/java/apoc/schema/SchemasTest.java @@ -347,7 +347,8 @@ public void testRelIndex() { testCall(db, "CALL apoc.schema.relationships()", row -> { assertEquals(":KNOWS(id,since)", row.get("name")); assertEquals("ONLINE", row.get("status")); - assertEquals("KNOWS", row.get("type")); + assertEquals("KNOWS", row.get("relationshipType")); + assertEquals("INDEX", row.get("type")); assertEquals(List.of("id", "since"), row.get("properties")); }); } @@ -710,7 +711,8 @@ public void testLookupIndexes() { testCall(db, "CALL apoc.schema.relationships()", (row) -> { assertEquals(":" + TOKEN_REL_TYPE + "()", row.get("name")); assertEquals("ONLINE", row.get("status")); - assertEquals(TOKEN_REL_TYPE, row.get("type")); + assertEquals("INDEX", row.get("type")); + assertEquals(TOKEN_REL_TYPE, row.get("relationshipType")); assertTrue(((List)row.get("properties")).isEmpty()); }); } @@ -753,8 +755,9 @@ public void testIndexesWithMultipleLabelsAndRelTypes() { Map r = result.next(); assertEquals(":[TYPE_1, TYPE_2],(alpha,beta)", r.get("name")); assertEquals("ONLINE", r.get("status")); - assertEquals(List.of("TYPE_1", "TYPE_2"), r.get("type")); + assertEquals(List.of("TYPE_1", "TYPE_2"), r.get("relationshipType")); assertEquals(List.of("alpha", "beta"), r.get("properties")); + assertEquals("INDEX", r.get("type")); assertFalse(result.hasNext()); }); }