Skip to content

Commit abd427a

Browse files
committed
skip unsafe count optimization for nested predicates
1 parent a802912 commit abd427a

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/traversal/optimize/HugeCountStrategy.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,28 @@ private boolean doStrategy(final Step step) {
272272
return false;
273273
}
274274

275+
final P<?> predicate = ((IsStep<?>) step.getNextStep()).getPredicate();
276+
if (this.hasNestedConnectivePredicate(predicate)) {
277+
return false;
278+
}
279+
275280
final Step parent = step.getTraversal().getParent().asStep();
276281
return (parent instanceof FilterStep || parent.getLabels().isEmpty()) &&
277282
!(parent.getNextStep() instanceof MatchStep.MatchEndStep &&
278283
((MatchStep.MatchEndStep) parent.getNextStep())
279284
.getMatchKey().isPresent());
280285
}
286+
287+
private boolean hasNestedConnectivePredicate(P<?> predicate) {
288+
if (!(predicate instanceof ConnectiveP)) {
289+
return false;
290+
}
291+
292+
for (P<?> child : ((ConnectiveP<?>) predicate).getPredicates()) {
293+
if (child instanceof ConnectiveP) {
294+
return true;
295+
}
296+
}
297+
return false;
298+
}
281299
}

hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/core/CountStrategyCoreTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,40 @@ public void testWhereCountGteNegativeDoesNotBuildInvalidRange() {
209209
Assert.assertEquals(4L, count);
210210
}
211211

212+
@Test
213+
public void testWhereCountNestedConnectivePredicate() {
214+
this.initSchema();
215+
Vertex source = graph().addVertex(T.label, "person", "name", "source");
216+
Vertex target = graph().addVertex(T.label, "person", "name", "target");
217+
source.addEdge("knows", target);
218+
commitTx();
219+
220+
long count = graph().traversal().V(source.id())
221+
.where(__.both("knows").count()
222+
.is(P.<Long>outside(1L, 18L)
223+
.and(P.gte(0L))))
224+
.count().next();
225+
226+
Assert.assertEquals(0L, count);
227+
}
228+
229+
@Test
230+
public void testWhereCountNegatedNestedConnectivePredicate() {
231+
this.initSchema();
232+
Vertex source = graph().addVertex(T.label, "person", "name", "source");
233+
Vertex target = graph().addVertex(T.label, "person", "name", "target");
234+
source.addEdge("knows", target);
235+
commitTx();
236+
237+
long count = graph().traversal().V(source.id())
238+
.where(__.both("knows").count()
239+
.is(P.not(P.<Long>outside(1L, 18L)
240+
.and(P.gte(0L)))))
241+
.count().next();
242+
243+
Assert.assertEquals(1L, count);
244+
}
245+
212246
@Test
213247
public void testRepeatAfterTextRangeFilterWithEmptyResult() {
214248
this.initTextRangeSchema(true);

0 commit comments

Comments
 (0)