diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/query/ConditionQuery.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/query/ConditionQuery.java index 8a5706a774..063d23aa6d 100644 --- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/query/ConditionQuery.java +++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/query/ConditionQuery.java @@ -288,11 +288,13 @@ public T condition(Object key) { return value; } + boolean initialized = false; Set intersectValues = InsertionOrderUtil.newSet(); for (Object value : valuesEQ) { List valueAsList = ImmutableList.of(value); - if (intersectValues.isEmpty()) { + if (!initialized) { intersectValues.addAll(valueAsList); + initialized = true; } else { CollectionUtil.intersectWithModify(intersectValues, valueAsList); @@ -301,8 +303,9 @@ public T condition(Object key) { for (Object value : valuesIN) { @SuppressWarnings("unchecked") List valueAsList = (List) value; - if (intersectValues.isEmpty()) { + if (!initialized) { intersectValues.addAll(valueAsList); + initialized = true; } else { CollectionUtil.intersectWithModify(intersectValues, valueAsList); diff --git a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/core/EdgeCoreTest.java b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/core/EdgeCoreTest.java index 265d408742..6bec6dc06f 100644 --- a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/core/EdgeCoreTest.java +++ b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/core/EdgeCoreTest.java @@ -4646,6 +4646,29 @@ public void testQueryInEdgesOfVertexByLabels() { Assert.assertEquals(3L, size); } + @Test + public void testQueryInEdgesOfVertexByConflictingLabels() { + HugeGraph graph = graph(); + init18Edges(); + + long direct = graph.traversal().V().inE("created") + .hasLabel("created", "look") + .hasLabel("authored") + .count().next(); + Assert.assertEquals(0L, direct); + + long matched = graph.traversal().V() + .match(__.as("start1") + .inE("created") + .as("m1")) + .select("m1") + .hasLabel("created", "look") + .hasLabel("authored") + .count().next(); + Assert.assertEquals(0L, matched); + Assert.assertEquals(matched, direct); + } + @Test public void testQueryInEdgesOfVertexBySortkey() { HugeGraph graph = graph(); diff --git a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/core/QueryTest.java b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/core/QueryTest.java index 88b161d32a..7d48084dbf 100644 --- a/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/core/QueryTest.java +++ b/hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/core/QueryTest.java @@ -17,8 +17,10 @@ package org.apache.hugegraph.unit.core; +import org.apache.hugegraph.backend.id.Id; import org.apache.hugegraph.backend.id.IdGenerator; import org.apache.hugegraph.backend.query.Aggregate.AggregateFunc; +import org.apache.hugegraph.backend.query.Condition; import org.apache.hugegraph.backend.query.ConditionQuery; import org.apache.hugegraph.backend.query.IdPrefixQuery; import org.apache.hugegraph.backend.query.IdQuery; @@ -30,6 +32,7 @@ import org.apache.hugegraph.type.define.HugeKeys; import org.junit.Test; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; @@ -45,6 +48,53 @@ public void testOrderBy() { query.orders()); } + @Test + public void testConditionWithEqAndIn() { + Id label1 = IdGenerator.of(1); + Id label2 = IdGenerator.of(2); + + ConditionQuery query = new ConditionQuery(HugeType.EDGE); + query.eq(HugeKeys.LABEL, label1); + query.query(Condition.in(HugeKeys.LABEL, + ImmutableList.of(label1, label2))); + + Assert.assertEquals(label1, query.condition(HugeKeys.LABEL)); + } + + @Test + public void testConditionWithConflictingEqAndIn() { + Id label1 = IdGenerator.of(1); + Id label2 = IdGenerator.of(2); + Id label3 = IdGenerator.of(3); + + ConditionQuery query = new ConditionQuery(HugeType.EDGE); + query.eq(HugeKeys.LABEL, label1); + query.eq(HugeKeys.LABEL, label2); + query.query(Condition.in(HugeKeys.LABEL, + ImmutableList.of(label1, label3))); + + Assert.assertNull(query.condition(HugeKeys.LABEL)); + } + + @Test + public void testConditionWithMultipleMatchedInValues() { + Id label1 = IdGenerator.of(1); + Id label2 = IdGenerator.of(2); + Id label3 = IdGenerator.of(3); + Id label4 = IdGenerator.of(4); + + ConditionQuery query = new ConditionQuery(HugeType.EDGE); + query.query(Condition.in(HugeKeys.LABEL, + ImmutableList.of(label1, label2, label3))); + query.query(Condition.in(HugeKeys.LABEL, + ImmutableList.of(label1, label2, label4))); + + Assert.assertThrows(IllegalStateException.class, + () -> query.condition(HugeKeys.LABEL), + e -> Assert.assertContains("Illegal key 'LABEL'", + e.getMessage())); + } + @Test public void testToString() { Query query = new Query(HugeType.VERTEX);