javeme commented on code in PR #2408:
URL:
https://github.com/apache/incubator-hugegraph/pull/2408#discussion_r1462876457
##########
hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/tx/GraphTransaction.java:
##########
@@ -1450,25 +1451,50 @@ private Query optimizeQuery(ConditionQuery query) {
// Optimize edge query
if (query.resultType().isEdge() && label != null &&
query.condition(HugeKeys.OWNER_VERTEX) != null &&
- query.condition(HugeKeys.DIRECTION) != null &&
- matchEdgeSortKeys(query, false, this.graph())) {
- // Query edge by sourceVertex + direction + label + sort-values
- query.optimized(OptimizedType.SORT_KEYS);
- query = query.copy();
- // Serialize sort-values
- List<Id> keys = this.graph().edgeLabel(label).sortKeys();
- List<Condition> conditions =
- GraphIndexTransaction.constructShardConditions(
- query, keys, HugeKeys.SORT_VALUES);
- query.query(conditions);
- /*
- * Reset all userprop since transferred to sort-keys, ignore other
- * userprop(if exists) that it will be filtered by
queryEdges(Query)
- */
- query.resetUserpropConditions();
+ query.condition(HugeKeys.DIRECTION) != null) {
+
+ Directions dir = query.condition(HugeKeys.DIRECTION);
+ EdgeLabel edgeLabel = this.graph().edgeLabel(label);
+
+ if (query.containsRelation(HugeKeys.OWNER_VERTEX,
Condition.RelationType.IN)) {
+ // For IN queries, filter out non-adjacent vertices.
+ ArrayList<Id> vertexIdList =
query.condition(HugeKeys.OWNER_VERTEX);
+ List<Id> filterVertexList =
vertexIdList.stream().filter(vertexId -> {
+ Vertex vertex = this.graph().vertex(vertexId);
+ VertexLabel vertexLabel =
graph().vertexLabel(vertex.label());
+ return edgeLabel.linkWithLabel(vertexLabel.id(),
dir.type());
+ }).collect(Collectors.toList());
+ vertexIdList.clear();
+ vertexIdList.addAll(filterVertexList);
+ } else if (query.containsRelation(HugeKeys.OWNER_VERTEX,
Condition.RelationType.EQ)) {
+ // For EQ queries, return emptyQuery() if it is a non-adjacent
vertex.
+ Id vertexId = query.condition(HugeKeys.OWNER_VERTEX);
+ Vertex vertex = this.graph().vertex(vertexId);
+ VertexLabel vertexLabel = graph().vertexLabel(vertex.label());
+ if (!edgeLabel.linkWithLabel(vertexLabel.id(), dir.type())) {
+ return new Query(query.resultType());
Review Comment:
also add some comments here like "Return empty query to skip storage query"
##########
hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/tx/GraphTransaction.java:
##########
@@ -1450,25 +1451,50 @@ private Query optimizeQuery(ConditionQuery query) {
// Optimize edge query
if (query.resultType().isEdge() && label != null &&
query.condition(HugeKeys.OWNER_VERTEX) != null &&
- query.condition(HugeKeys.DIRECTION) != null &&
- matchEdgeSortKeys(query, false, this.graph())) {
- // Query edge by sourceVertex + direction + label + sort-values
- query.optimized(OptimizedType.SORT_KEYS);
- query = query.copy();
- // Serialize sort-values
- List<Id> keys = this.graph().edgeLabel(label).sortKeys();
- List<Condition> conditions =
- GraphIndexTransaction.constructShardConditions(
- query, keys, HugeKeys.SORT_VALUES);
- query.query(conditions);
- /*
- * Reset all userprop since transferred to sort-keys, ignore other
- * userprop(if exists) that it will be filtered by
queryEdges(Query)
- */
- query.resetUserpropConditions();
+ query.condition(HugeKeys.DIRECTION) != null) {
+
+ Directions dir = query.condition(HugeKeys.DIRECTION);
+ EdgeLabel edgeLabel = this.graph().edgeLabel(label);
+
+ if (query.containsRelation(HugeKeys.OWNER_VERTEX,
Condition.RelationType.IN)) {
+ // For IN queries, filter out non-adjacent vertices.
+ ArrayList<Id> vertexIdList =
query.condition(HugeKeys.OWNER_VERTEX);
+ List<Id> filterVertexList =
vertexIdList.stream().filter(vertexId -> {
+ Vertex vertex = this.graph().vertex(vertexId);
+ VertexLabel vertexLabel =
graph().vertexLabel(vertex.label());
+ return edgeLabel.linkWithLabel(vertexLabel.id(),
dir.type());
+ }).collect(Collectors.toList());
+ vertexIdList.clear();
+ vertexIdList.addAll(filterVertexList);
+ } else if (query.containsRelation(HugeKeys.OWNER_VERTEX,
Condition.RelationType.EQ)) {
+ // For EQ queries, return emptyQuery() if it is a non-adjacent
vertex.
Review Comment:
prefer `For EQ query, just skip query if adjacent schema is unavailable.`
##########
hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/tx/GraphTransaction.java:
##########
@@ -1450,25 +1451,50 @@ private Query optimizeQuery(ConditionQuery query) {
// Optimize edge query
if (query.resultType().isEdge() && label != null &&
query.condition(HugeKeys.OWNER_VERTEX) != null &&
- query.condition(HugeKeys.DIRECTION) != null &&
- matchEdgeSortKeys(query, false, this.graph())) {
- // Query edge by sourceVertex + direction + label + sort-values
- query.optimized(OptimizedType.SORT_KEYS);
- query = query.copy();
- // Serialize sort-values
- List<Id> keys = this.graph().edgeLabel(label).sortKeys();
- List<Condition> conditions =
- GraphIndexTransaction.constructShardConditions(
- query, keys, HugeKeys.SORT_VALUES);
- query.query(conditions);
- /*
- * Reset all userprop since transferred to sort-keys, ignore other
- * userprop(if exists) that it will be filtered by
queryEdges(Query)
- */
- query.resetUserpropConditions();
+ query.condition(HugeKeys.DIRECTION) != null) {
+
+ Directions dir = query.condition(HugeKeys.DIRECTION);
+ EdgeLabel edgeLabel = this.graph().edgeLabel(label);
+
+ if (query.containsRelation(HugeKeys.OWNER_VERTEX,
Condition.RelationType.IN)) {
+ // For IN queries, filter out non-adjacent vertices.
Review Comment:
`For IN query, filter schema non-adjacent vertices.`?
##########
hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/schema/EdgeLabel.java:
##########
@@ -98,6 +98,15 @@ public boolean linkWithLabel(Id id) {
return this.sourceLabel.equals(id) || this.targetLabel.equals(id);
}
+ public boolean linkWithLabel(Id id, HugeType hugeType) {
Review Comment:
hugeType => type?
##########
hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/tx/GraphTransaction.java:
##########
@@ -1450,25 +1451,50 @@ private Query optimizeQuery(ConditionQuery query) {
// Optimize edge query
if (query.resultType().isEdge() && label != null &&
query.condition(HugeKeys.OWNER_VERTEX) != null &&
- query.condition(HugeKeys.DIRECTION) != null &&
- matchEdgeSortKeys(query, false, this.graph())) {
- // Query edge by sourceVertex + direction + label + sort-values
- query.optimized(OptimizedType.SORT_KEYS);
- query = query.copy();
- // Serialize sort-values
- List<Id> keys = this.graph().edgeLabel(label).sortKeys();
- List<Condition> conditions =
- GraphIndexTransaction.constructShardConditions(
- query, keys, HugeKeys.SORT_VALUES);
- query.query(conditions);
- /*
- * Reset all userprop since transferred to sort-keys, ignore other
- * userprop(if exists) that it will be filtered by
queryEdges(Query)
- */
- query.resetUserpropConditions();
+ query.condition(HugeKeys.DIRECTION) != null) {
+
+ Directions dir = query.condition(HugeKeys.DIRECTION);
+ EdgeLabel edgeLabel = this.graph().edgeLabel(label);
+
+ if (query.containsRelation(HugeKeys.OWNER_VERTEX,
Condition.RelationType.IN)) {
+ // For IN queries, filter out non-adjacent vertices.
+ ArrayList<Id> vertexIdList =
query.condition(HugeKeys.OWNER_VERTEX);
+ List<Id> filterVertexList =
vertexIdList.stream().filter(vertexId -> {
+ Vertex vertex = this.graph().vertex(vertexId);
+ VertexLabel vertexLabel =
graph().vertexLabel(vertex.label());
+ return edgeLabel.linkWithLabel(vertexLabel.id(),
dir.type());
+ }).collect(Collectors.toList());
+ vertexIdList.clear();
+ vertexIdList.addAll(filterVertexList);
+ } else if (query.containsRelation(HugeKeys.OWNER_VERTEX,
Condition.RelationType.EQ)) {
+ // For EQ queries, return emptyQuery() if it is a non-adjacent
vertex.
+ Id vertexId = query.condition(HugeKeys.OWNER_VERTEX);
+ Vertex vertex = this.graph().vertex(vertexId);
+ VertexLabel vertexLabel = graph().vertexLabel(vertex.label());
+ if (!edgeLabel.linkWithLabel(vertexLabel.id(), dir.type())) {
+ return new Query(query.resultType());
+ }
+ }
- LOG.debug("Query edges by sortKeys: {}", query);
- return query;
+ if (matchEdgeSortKeys(query, false, this.graph())) {
+ // Query edge by sourceVertex + direction + label + sort-values
+ query.optimized(OptimizedType.SORT_KEYS);
+ query = query.copy();
+ // Serialize sort-values
+ List<Id> keys = this.graph().edgeLabel(label).sortKeys();
+ List<Condition> conditions =
+ GraphIndexTransaction.constructShardConditions(
Review Comment:
can we move to the previous line?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]