Skip to content

Commit

Permalink
fix: can't query edges by multi labels + properties (#1737)
Browse files Browse the repository at this point in the history
* fix: can't query edges by multi labels + properties

fix: #1735 #1736
Change-Id: I28e3a209074abaab8c8271775679d54845e6e785
  • Loading branch information
javeme authored Jan 21, 2022
1 parent 848b8fb commit cadeade
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.baidu.hugegraph.structure.HugeProperty;
import com.baidu.hugegraph.type.HugeType;
import com.baidu.hugegraph.type.define.HugeKeys;
import com.baidu.hugegraph.util.CollectionUtil;
import com.baidu.hugegraph.util.E;
import com.baidu.hugegraph.util.InsertionOrderUtil;
import com.baidu.hugegraph.util.LongEncoding;
Expand Down Expand Up @@ -217,23 +218,65 @@ public Relation relation(Id key){

@Watched
public <T> T condition(Object key) {
List<Object> values = new ArrayList<>();
List<Object> valuesEQ = InsertionOrderUtil.newList();
List<Object> valuesIN = InsertionOrderUtil.newList();
for (Condition c : this.conditions) {
if (c.isRelation()) {
Condition.Relation r = (Condition.Relation) c;
if (r.key().equals(key) && (r.relation() == RelationType.EQ ||
r.relation() == RelationType.IN)) {
values.add(r.value());
if (r.key().equals(key)) {
if (r.relation() == RelationType.EQ) {
valuesEQ.add(r.value());
} else if (r.relation() == RelationType.IN) {
Object value = r.value();
assert value instanceof List;
valuesIN.add(value);
}
}
}
}
if (values.isEmpty()) {
if (valuesEQ.isEmpty() && valuesIN.isEmpty()) {
return null;
}
E.checkState(values.size() == 1,
"Illegal key '%s' with more than one value", key);
if (valuesEQ.size() == 1 && valuesIN.size() == 0) {
@SuppressWarnings("unchecked")
T value = (T) valuesEQ.get(0);
return value;
}
if (valuesEQ.size() == 0 && valuesIN.size() == 1) {
@SuppressWarnings("unchecked")
T value = (T) valuesIN.get(0);
return value;
}

Set<Object> intersectValues = InsertionOrderUtil.newSet();
for (Object value : valuesEQ) {
List<Object> valueAsList = ImmutableList.of(value);
if (intersectValues.isEmpty()) {
intersectValues.addAll(valueAsList);
} else {
CollectionUtil.intersectWithModify(intersectValues,
valueAsList);
}
}
for (Object value : valuesIN) {
@SuppressWarnings("unchecked")
List<Object> valueAsList = (List<Object>) value;
if (intersectValues.isEmpty()) {
intersectValues.addAll(valueAsList);
} else {
CollectionUtil.intersectWithModify(intersectValues,
valueAsList);
}
}

if (intersectValues.size() == 0) {
return null;
}
E.checkState(intersectValues.size() == 1,
"Illegal key '%s' with more than one value: %s",
key, intersectValues);
@SuppressWarnings("unchecked")
T value = (T) values.get(0);
T value = (T) intersectValues.iterator().next();
return value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ protected ConditionQuery constructEdgesQuery(
ConditionQuery query = GraphTransaction.constructEdgesQuery(
vertex, direction, edgeLabels);
// Query by sort-keys
if (withEdgeCond && edgeLabels.length > 0) {
if (withEdgeCond && edgeLabels.length == 1) {
TraversalUtil.fillConditionQuery(query, this.hasContainers, graph);
if (!GraphTransaction.matchPartialEdgeSortKeys(query, graph)) {
// Can't query by sysprop and by index (HugeGraph-749)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3581,12 +3581,12 @@ public void testQueryOutEdgesOfVertexBySortkeyWithRange() {
Assert.assertEquals(3, edges.size());

edges = graph.traversal().V(v1).outE("call")
.has("calltime", P.between("2017-5-2","2017-5-4"))
.has("calltime", P.between("2017-5-2", "2017-5-4"))
.toList();
Assert.assertEquals(5, edges.size());

edges = graph.traversal().V(v1).outE("call")
.has("calltime", P.between("2017-5-2","2017-5-4"))
.has("calltime", P.between("2017-5-2", "2017-5-4"))
.where(__.not(__.otherV().hasId((v10086.id()))))
.toList();
Assert.assertEquals(3, edges.size());
Expand Down Expand Up @@ -4549,6 +4549,33 @@ public void testQueryInEdgesOfVertexByLabelAndFilter() {
Assert.assertEquals(1, edges.size());
}

@Test
public void testQueryInEdgesOfVertexByLabels() {
HugeGraph graph = graph();
init18Edges();

long size;
size = graph.traversal().V().outE("created", "authored", "look")
.hasLabel("authored", "created")
.count().next();
Assert.assertEquals(5L, size);

size = graph.traversal().V().outE("created", "authored", "look")
.hasLabel("authored", "friend")
.count().next();
Assert.assertEquals(3L, size);

size = graph.traversal().V().outE("created")
.hasLabel("authored", "created")
.count().next();
Assert.assertEquals(2L, size);

size = graph.traversal().V().inE("created", "authored", "look")
.has("score", 3)
.count().next();
Assert.assertEquals(3L, size);
}

@Test
public void testQueryInEdgesOfVertexBySortkey() {
HugeGraph graph = graph();
Expand Down Expand Up @@ -7242,7 +7269,7 @@ public void testAddEdgeWithCollectionIndex() {


List<Edge> edges = graph.traversal().E()
.has("related","tags",
.has("related", "tags",
ConditionP.contains("gremlin"))
.toList();

Expand Down

0 comments on commit cadeade

Please sign in to comment.