Repository navigation
fix(graphlookup): make BFS traversal independent of shard row order - #5769
mengweieric merged 3 commits into
Conversation
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
PR Code Suggestions ✨Latest suggestions up to bc65529 Explore these optional code suggestions:
Previous suggestionsSuggestions up to commit 581538c
Suggestions up to commit 7823ca8
Suggestions up to commit af839fd
|
Codecov Report❌ Patch coverage is
❌ Your project check has failed because the head coverage (63.24%) is below the target coverage (99.00%). You can increase the head coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## main #5769 +/- ##
============================================
+ Coverage 62.86% 63.24% +0.38%
- Complexity 8814 8820 +6
============================================
Files 938 938
Lines 40205 40209 +4
Branches 4528 4530 +2
============================================
+ Hits 25275 25432 +157
+ Misses 14125 13955 -170
- Partials 805 822 +17
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
7823ca8 to
581538c
Compare
|
@qianheng-aws please take a look thanks |
In the original thoughts, that's implemented on purpose as some kinds of pruning optimization, which differs from MongoDB's behavior. Designed as one of the graph-search engine of LightRAG, this @mengweieric I don't know the future direction of this command. If the above defeats you mentioned will block other feature development or the current behavior deviates our expectations, I think it makes sense to make these changes. But before merging, I think we should also re-run the benchmark in LightRAG here: HKUDS/LightRAG#2785. |
| .mustNot(getQueryBuilder(fromFieldIdx, visitedValues)); | ||
| } | ||
|
|
||
| QueryBuilder query = getQueryBuilder(toFieldIdx, values); |
There was a problem hiding this comment.
After removing "mustnot..." in the query, previously emitted documents can also exhaust the result window.
So, with usePIT=false, it will return less nodes than before.
There was a problem hiding this comment.
You are right that removing the exclusion can consume more of the result window when usePIT=false. That mode is already documented as best effort in this class, and the removed exclusion was filtering out documents that must be emitted, which is the membership bug this PR fixes. The usePIT=true path is unaffected since PIT paginates completely. Exact shard-side dedup would need _id projected into the scan row, which I can file as a follow-up issue if you think it is worth tightening the fast mode.
|
@qianheng-aws Thanks for the context on the pruning intent. The concern with the old behavior is that the pruning dropped reached documents non-deterministically under multi-shard layouts, so the same query on the same data could return different membership across runs, which affects the node set LightRAG consumes, not just the edge set. On the benchmark, I ran a controlled A/B of this change directly: same cluster, same data and shard layout, only the plugin swapped, on a back-edge-heavy graph (6,804 edge docs, 5 shards, maxDepth=6, 30 timed runs per build, all counts stable):
The added time is the cost of returning documents the old code dropped, and per-document cost improved 3.8x. Workloads without edges into visited nodes see no cost delta. |
The graph lookup enumerator produced different result sets depending on the order shards returned rows within a BFS level, so the same query returned different documents on a multi-shard index than on a single shard. Three coupled causes: - visitedNodes was mutated inside the per-row loop of a level, so one row changed what later rows in the same level were allowed to contribute. - A matched document was emitted only when it still had unvisited onward edges, so a node whose edges point back to already-visited nodes was dropped, and whether that happened depended on the order above. - queryLookupTable pushed a mustNot(visited) filter to the shard, so a reached document could be excluded before the enumerator saw it. The next-level frontier is now computed against visitedNodes as of the start of the level and folded in only after the level completes, which removes the order dependence by construction. Every matched document is emitted once at its minimum depth, decoupled from whether it has unvisited onward edges. The mustNot(visited) pushdown is removed; traversal still terminates because a value enters the frontier at most once, so each value is queried at most once. Document identity is a structural key over all projected columns rather than the connectTo value, so two distinct documents sharing a connectTo value are no longer collapsed. Tests are updated to the corrected result sets and use unordered matching where the collected array order is genuinely non-contractual. A graph_multi fixture is added. One test is retained @ignore'd as the executable specification for the residual case of two documents whose projected columns are identical but whose _id differs; separating those requires projecting _id through deriveRowType, the planner rule and the enumerator. Verified on a real cluster at both 1 and 5 primary shards: 34 tests, 0 failures in both layouts. Signed-off-by: menwe <[email protected]>
The analytics-engine parquet primary format rejects multi-valued fields (both graph_airports connects arrays and graph_multi mgr arrays fail ingestion). Introduce a GRAPH_MULTI_VALUE_FIELD capability, gate CalcitePPLGraphLookupIT on it at class level, and allowlist the graph datasets in AnalyticsUnsupportedFieldStripVerifyIT. Signed-off-by: menwe <[email protected]>
…tics Covers the three traversal invariants of the order-independence fix without a cluster, via an in-memory lookup scan that evaluates the pushed terms query against fixed documents in a configurable return order: - all-reached emission: every matched document is emitted exactly once at its minimum depth, including documents whose onward edges are all visited - order independence: identical result sets for opposite row return orders, in forward and bidirectional traversal - structural identity: distinct documents sharing a connectTo value are both kept; rediscovered documents deduplicate - batch, normal, and literal start modes; null-edge terminals; maxDepth Signed-off-by: menwe <[email protected]>
581538c to
bc65529
Compare
|
If our direction is for aligning with MongoDB's behavior and eliminate non-deterministic results, this PR LGTM.
I don't quite get this point. Per our historical test, it won't miss any nodes under the current implementation.
Take the case you described for example, although documents about ORD is omitted in the |
Description
graphLookupresults currently depend on the order in which shards return documents, so the same query over the same data returns different result sets at different shard counts. Three coupled defects in the BFS enumerator:visitedNodeswas mutated inside the per-row loop of a BFS level, so sibling rows within one level interfered with each other — and shards return rows within a level in arbitrary order.!nextValues.isEmpty()), silently dropping reached nodes whose edges all point to already-visited nodes.queryLookupTablepushedmustNot(visited)into the OpenSearch query, filtering reached documents out at the shard before the enumerator could emit them.Why this is a product defect, not a test-side issue
A worked example using the
graph_airportsfixture:Query:
where airport='JFK' | graphLookup ... start=connects edge=connects-->airport. The level-0 frontier is{BOS, ORD}, so both the BOS and ORD documents are reached. What the old code returned depended on which one was processed first:JFK, PWMtovisitedNodesand is emitted. ORD's only edge (JFK) is now already visited, so ORD contributes no new frontier values and is silently dropped. Result omits ORD.JFKand is emitted. BOS still contributesPWMand is emitted. Result contains both.At 1 shard, Lucene returns rows in insertion order, so the same branch is taken every time and the output looks stable. At 5 shards, BOS and ORD live on different shards and arrival order varies per run, so the result set membership flips between runs.
This cannot be repaired in the tests:
rowsUnordered). What differs across runs is which rows exist (cardinality 1 vs 2), not their order.JFK.connectsliterally containsORD). Encoding that as the oracle would assert a wrong answer; there is no expected value that matches a non-deterministic set.graphLookupcommand to demonstrate multi-hop queries #5088) models MongoDB$graphLookup, whose contract returns every reached document exactly once and leaves only array order unspecified. Non-deterministic membership is not a permitted reading, and the!nextValues.isEmpty()emission gate carries no design comment or documentation — it conflates "does this row extend the next frontier" with "should this row be emitted."Fix
MongoDB
$graphLookupall-reached semantics, per the design reference in #5088: the next frontier is computed againstvisitedNodesas of the start of each level and folded in only after the level completes; every matched document is emitted exactly once at its minimum depth; the shard-side visited filter is removed (termination still holds — a value enters the frontier at most once); deduplication keys on all projected columns instead of the connect-to value.Known limitation (documented via an
@Ignored executable spec): two distinct documents whose projected columns are byte-for-byte identical still collapse to one result; separating them requires projecting_idthrough the operator, left as follow-up.The second commit gates
CalcitePPLGraphLookupIToff the analytics-engine route: the parquet primary format rejects multi-valued fields, which the graph fixtures require.Validation
CalcitePPLGraphLookupIT: green at 1 shard and 5 shards, both pushdown modes.integTestRemoteat 5 shards with this PR applied: 7,844 tests, 0 failures (two independent runs), plus a security-enabled (FGAC) cluster run: 0 failures.CalciteEnumerableGraphLookupTestdrives the enumerator through configurable row orders and asserts identical result sets.Related Issues
Design reference #5088.
Check List
--signoffBy submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.