Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,28 @@ private boolean doStrategy(final Step step) {
return false;
}

final P<?> predicate = ((IsStep<?>) step.getNextStep()).getPredicate();
if (this.hasNestedConnectivePredicate(predicate)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

‼️ Critical: flat connective predicates remain semantically unsafe

This guard only rejects a ConnectiveP whose child is another ConnectiveP, so flat expressions such as P.eq(0L).and(P.gt(0L)) and P.lte(0L).or(P.gte(1L)) still enter the optimization. The loop then derives useNotStep from only the first range-driving child and removes the complete count().is(...); on an empty adjacency the contradiction can return true, and on a non-empty adjacency the tautology can return false. Both shapes were reproduced against the current strategy on TinkerPop 3.5.1. Please disable the destructive rewrite for every ConnectiveP unless equivalence is derived from the complete boolean expression; the conservative fix is to skip all ConnectiveP values here. Add flat AND/OR regressions for both zero and nonzero counts.

return false;
}

final Step parent = step.getTraversal().getParent().asStep();
return (parent instanceof FilterStep || parent.getLabels().isEmpty()) &&
!(parent.getNextStep() instanceof MatchStep.MatchEndStep &&
((MatchStep.MatchEndStep) parent.getNextStep())
.getMatchKey().isPresent());
}

private boolean hasNestedConnectivePredicate(P<?> predicate) {
if (!(predicate instanceof ConnectiveP)) {
return false;
}

for (P<?> child : ((ConnectiveP<?>) predicate).getPredicates()) {
if (child instanceof ConnectiveP) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,40 @@ public void testWhereCountGteNegativeDoesNotBuildInvalidRange() {
Assert.assertEquals(4L, count);
}

@Test
public void testWhereCountNestedConnectivePredicate() {
this.initSchema();
Vertex source = graph().addVertex(T.label, "person", "name", "source");
Vertex target = graph().addVertex(T.label, "person", "name", "target");
source.addEdge("knows", target);
commitTx();

long count = graph().traversal().V(source.id())
.where(__.both("knows").count()
.is(P.<Long>outside(1L, 18L)
.and(P.gte(0L))))
.count().next();

Assert.assertEquals(0L, count);
}

@Test
public void testWhereCountNegatedNestedConnectivePredicate() {
this.initSchema();
Vertex source = graph().addVertex(T.label, "person", "name", "source");
Vertex target = graph().addVertex(T.label, "person", "name", "target");
source.addEdge("knows", target);
commitTx();

long count = graph().traversal().V(source.id())
.where(__.both("knows").count()
.is(P.not(P.<Long>outside(1L, 18L)
.and(P.gte(0L)))))
.count().next();

Assert.assertEquals(1L, count);
}

@Test
public void testRepeatAfterTextRangeFilterWithEmptyResult() {
this.initTextRangeSchema(true);
Expand Down
Loading