ZhYQ1 commented on code in PR #430:
URL: https://github.com/apache/hugegraph-ai/pull/430#discussion_r4559941664


##########
hugegraph-llm/src/hugegraph_llm/nodes/hugegraph_node/graph_query_node.py:
##########
@@ -361,17 +366,24 @@ def _subgraph_query(self, context: Dict[str, Any]) -> 
Dict[str, Any]:
 
             vertex_knowledge = 
self._format_graph_from_vertex(query_result=vertexes)
             paths: List[Any] = []
-            # TODO: use generator or asyncio to speed up the query logic
-            for matched_vid in matched_vids:
+            # Batch the per-vid neighbour traversals: one gremlin exec per
+            # group instead of one per matched vid, so a large id set no longer
+            # triggers one server round-trip per id. Batch data is concatenated
+            # in matched_vids order, so the path list keeps the per-vid order.
+            batch_size = min(NEIGHBOR_QUERY_BATCH_SIZE, len(matched_vids))
+            for start in range(0, len(matched_vids), batch_size):
+                batch = matched_vids[start : start + batch_size]
                 gremlin_query = VID_QUERY_NEIGHBOR_TPL.format(
-                    keywords=f"'{matched_vid}'",
+                    keywords=", ".join(f"'{vid}'" for vid in batch),

Review Comment:
   Thanks for catching this —  it was a real behaviour regression, not just an 
edge case.
   
   Fixed in e3409f6. The traversal after `g.V(...)` is now wrapped in 
`local(__. ...)`, so each matched vid runs the inner `repeat ... path ... 
limit(max_items)` in its own scope. `bothE().limit(edge_limit)`, `dedup()` and 
`.limit(max_items)` are now per vid instead of sharing one edge budget / dedup 
set / cap across the batch, while the query count is still `1 + ceil(N / 10)` 
(N=25: 4 execs, unchanged).
   
   Generated query shape:
   
       g.V('v1', ..., 'v10')
       .local(
          __.repeat(
             bothE('edge').limit(edge_limit).otherV().dedup()
          ).times(max_deep).emit()
          .simplePath()
          .path()
          .by(project('label', 'id', 'props')...)
          .by(project('label', 'inV', 'outV', 'props')...)
          .limit(max_items)
       )
       .toList()
   
   Semantics are back to the old per-vid loop: each vid contributes its own 
paths, capped independently by `max_items`, with no cross-vid contamination of 
the edge limit or `dedup()` set.
   
   Tests added in `src/tests/nodes/test_graph_query_node.py`, both asserting 
the generated query keeps the per-vid scope:
   
   - `test_neighbor_query_wraps_per_vid_traversal_in_local_scope` — `bothE(`, 
`dedup()` and both `.limit(` steps are inside `local(...)`, with the global 
`.toList()` outside.
   - `test_neighbor_query_keeps_limits_inside_local_not_global` — no `limit(` / 
`dedup()` leaks ahead of `.local(`, and both limits are scoped inside it.
   
   Verification:
   
       ruff check <both files>            -> All checks passed
       ruff format --check <both files>   -> 2 files already formatted
       PYTHONPATH=src pytest src/tests/nodes -q -> 11 passed
   
   Branch was also rebased onto current `upstream/ontogeny`, so the diff is now 
just the two files above.



-- 
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]

Reply via email to