Skip to content

Commit

Permalink
[Zl3h9sM8] apoc.schema.relationships return wrong output for relation…
Browse files Browse the repository at this point in the history
…ship indexes (neo4j/apoc#247)

* [Zl3h9sM8] apoc.schema.relationships return wrong output for relationship indexes

* added getIndexType(indexDescriptor) method

* changed attr names
  • Loading branch information
vga91 committed Jan 10, 2023
1 parent 420d24a commit dad72d2
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 15 deletions.
6 changes: 3 additions & 3 deletions core/src/main/java/apoc/result/IndexConstraintNodeInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> properties, String status, String type, String failure, float populationProgress, long size, double valuesSelectivity, String userDescription) {
public IndexConstraintNodeInfo(String name, Object label, List<String> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ public class IndexConstraintRelationshipInfo {

public final String name;

public final Object type;
public final String type;

public final List<String> properties;

public final String status;

public final Object relationshipType;

public IndexConstraintRelationshipInfo(String name, Object type, List<String> properties, String status) {
public IndexConstraintRelationshipInfo(String name, String schemaType, List<String> properties, String status, Object relationshipType) {
this.name = name;
this.type = type;
this.type = schemaType;
this.properties = properties;
this.status = status;
this.relationshipType = relationshipType;
}
}
18 changes: 12 additions & 6 deletions core/src/main/java/apoc/schema/Schemas.java
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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)
Expand All @@ -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
);
}
}
Expand All @@ -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<String> properties) {
final String labelOrTypeAsString = labelOrType instanceof String ? (String) labelOrType : StringUtils.join(labelOrType, ",");
return String.format(":%s(%s)", labelOrTypeAsString, StringUtils.join(properties, ","));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ public void testSchemaRelationships() {
Map<String, Object> 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());
Expand All @@ -397,6 +398,7 @@ public void testSchemaNodeWithRelationshipsConstraintsAndViceVersa() {
Map<String, Object> 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());
Expand Down
9 changes: 6 additions & 3 deletions core/src/test/java/apoc/schema/SchemasTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
});
}
Expand Down Expand Up @@ -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());
});
}
Expand Down Expand Up @@ -753,8 +755,9 @@ public void testIndexesWithMultipleLabelsAndRelTypes() {
Map<String, Object> 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());
});
}
Expand Down

0 comments on commit dad72d2

Please sign in to comment.