Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in quering with joint labels #456

Merged
merged 5 commits into from
Apr 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -21,10 +21,12 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.baidu.hugegraph.backend.BackendException;
Expand Down Expand Up @@ -410,6 +412,25 @@ public boolean isFlattened() {
return true;
}

public boolean mayHasDupKeys(Set<HugeKeys> keys) {
Map<HugeKeys, Integer> keyCounts = new HashMap<>();
for (Condition condition : this.conditions()) {
if (!condition.isRelation()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

All conditions should be relations. Check them.

// Assume may exist duplicate keys when has nested conditions
return true;
}
Relation relation = (Relation) condition;
if (keys.contains(relation.key())) {
int keyCount = keyCounts.getOrDefault(relation.key(), 0);
if (++keyCount > 1) {
return true;
}
keyCounts.put((HugeKeys) relation.key(), keyCount);
}
}
return false;
}

public void optimized(int optimizedType) {
this.optimizedType = optimizedType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@
import com.baidu.hugegraph.util.InsertionOrderUtil;
import com.baidu.hugegraph.util.NumericUtil;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;

public final class ConditionQueryFlatten {

private static final Set<HugeKeys> SPECIAL_KEYS = ImmutableSet.of(
HugeKeys.LABEL
);

public static List<ConditionQuery> flatten(ConditionQuery query) {
if (query.isFlattened()) {
if (query.isFlattened() && !query.mayHasDupKeys(SPECIAL_KEYS)) {
return Arrays.asList(query);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4042,6 +4042,56 @@ public void testQueryVerticesByLabelsWithOneLabelNotExist() {
});
}

@Test
public void testQueryByJointLabels() {
HugeGraph graph = graph();
init5Persons();
init5Computers();
init10Vertices();

GraphTraversalSource g = graph.traversal();

List<Vertex> vertices = g.V().hasLabel("person").hasLabel("computer")
.toList();
Assert.assertEquals(0, vertices.size());
Linary marked this conversation as resolved.
Show resolved Hide resolved

vertices = g.V().hasLabel("person").hasLabel("person")
.toList();
Assert.assertEquals(5, vertices.size());

vertices = g.V().hasLabel("person", "computer").hasLabel("person")
.toList();
Assert.assertEquals(5, vertices.size());
for (Vertex vertex : vertices) {
Assert.assertEquals("person", vertex.label());
}

vertices = g.V().hasLabel("person", "computer").hasLabel("computer")
.toList();
Assert.assertEquals(5, vertices.size());
for (Vertex vertex : vertices) {
Assert.assertEquals("computer", vertex.label());
}
Linary marked this conversation as resolved.
Show resolved Hide resolved

vertices = g.V().hasLabel("person").hasLabel("person", "computer")
.toList();
Assert.assertEquals(5, vertices.size());
for (Vertex vertex : vertices) {
Assert.assertEquals("person", vertex.label());
}

vertices = g.V().hasLabel("person", "computer")
.hasLabel("person", "author").toList();
Assert.assertEquals(5, vertices.size());
for (Vertex vertex : vertices) {
Assert.assertEquals("person", vertex.label());
}

vertices = g.V().hasLabel("person", "computer")
.hasLabel("book", "language").toList();
Assert.assertEquals(0, vertices.size());
}
Linary marked this conversation as resolved.
Show resolved Hide resolved

private void init10Vertices() {
HugeGraph graph = graph();

Expand Down