Skip to content

Commit d92050f

Browse files
committed
fix(core): normalize boolean range predicates
1 parent 4cd2049 commit d92050f

2 files changed

Lines changed: 44 additions & 6 deletions

File tree

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

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.apache.hugegraph.structure.HugeElement;
4747
import org.apache.hugegraph.structure.HugeProperty;
4848
import org.apache.hugegraph.type.HugeType;
49+
import org.apache.hugegraph.type.define.DataType;
4950
import org.apache.hugegraph.type.define.Directions;
5051
import org.apache.hugegraph.type.define.HugeKeys;
5152
import org.apache.hugegraph.util.CollectionUtil;
@@ -419,9 +420,9 @@ public static Condition convOr(HugeGraph graph,
419420
return cond;
420421
}
421422

422-
private static Condition.Relation convCompare2Relation(HugeGraph graph,
423-
HugeType type,
424-
HasContainer has) {
423+
private static Condition convCompare2Relation(HugeGraph graph,
424+
HugeType type,
425+
HasContainer has) {
425426
assert type.isGraph();
426427
BiPredicate<?, ?> bp = has.getPredicate().getBiPredicate();
427428
assert bp instanceof Compare;
@@ -459,16 +460,21 @@ private static Condition.Relation convCompare2SyspropRelation(HugeGraph graph,
459460
}
460461
}
461462

462-
private static Condition.Relation convCompare2UserpropRelation(HugeGraph graph,
463-
HugeType type,
464-
HasContainer has) {
463+
private static Condition convCompare2UserpropRelation(HugeGraph graph,
464+
HugeType type,
465+
HasContainer has) {
465466
BiPredicate<?, ?> bp = has.getPredicate().getBiPredicate();
466467
assert bp instanceof Compare;
467468

468469
String key = has.getKey();
469470
PropertyKey pkey = graph.propertyKey(key);
470471
Id pkeyId = pkey.id();
471472
Object value = validPropertyValue(has.getValue(), pkey);
473+
if (pkey.dataType() == DataType.BOOLEAN &&
474+
value instanceof Boolean) {
475+
return convCompare2BooleanUserpropRelation((Compare) bp, pkeyId,
476+
(Boolean) value);
477+
}
472478

473479
switch ((Compare) bp) {
474480
case eq:
@@ -488,6 +494,36 @@ private static Condition.Relation convCompare2UserpropRelation(HugeGraph graph,
488494
}
489495
}
490496

497+
private static Condition convCompare2BooleanUserpropRelation(Compare compare,
498+
Id key,
499+
Boolean value) {
500+
switch (compare) {
501+
case eq:
502+
return Condition.eq(key, value);
503+
case neq:
504+
return Condition.neq(key, value);
505+
case gt:
506+
return value ? impossibleBooleanCondition(key) :
507+
Condition.eq(key, true);
508+
case gte:
509+
return value ? Condition.eq(key, true) :
510+
Condition.in(key, ImmutableList.of(false, true));
511+
case lt:
512+
return value ? Condition.eq(key, false) :
513+
impossibleBooleanCondition(key);
514+
case lte:
515+
return value ? Condition.in(key, ImmutableList.of(false, true)) :
516+
Condition.eq(key, false);
517+
default:
518+
throw new AssertionError(compare);
519+
}
520+
}
521+
522+
private static Condition impossibleBooleanCondition(Id key) {
523+
return Condition.and(Condition.eq(key, false),
524+
Condition.eq(key, true));
525+
}
526+
491527
private static Condition convRelationType2Relation(HugeGraph graph,
492528
HugeType type,
493529
HasContainer has) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7121,6 +7121,8 @@ public void testQueryEdgeWithNullablePropertyInCompositeIndex() {
71217121
public void testQueryEdgeByBooleanRangePredicate() {
71227122
HugeGraph graph = graph();
71237123
initStrikeIndex();
7124+
graph.schema().indexLabel("strikeByArrested").onE("strike").secondary()
7125+
.by("arrested").create();
71247126

71257127
Vertex louise = graph.addVertex(T.label, "person", "name", "Louise",
71267128
"city", "Beijing", "age", 21);

0 commit comments

Comments
 (0)