-
-
Notifications
You must be signed in to change notification settings - Fork 116
fix(#4645): shortestPath edge:true ClassCastException + wrong vertex in BOTH direction #4646
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
Merged
robfrank
merged 3 commits into
main
from
fix/4645-shortestpath-edge-both-classcastexception
Jun 17, 2026
+197
−5
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| # Fix #4645 - shortestPath edge:true ClassCastException on BOTH direction with empty side | ||
|
|
||
| ## Issue | ||
|
|
||
| `shortestPath(..., {'direction':'BOTH','edge':true})` throws `ClassCastException` when one of the two directional sub-queries returns an empty edge list. | ||
|
|
||
| ## Root Cause (two bugs) | ||
|
|
||
| **Bug 1 - ClassCastException:** `EdgeToVertexIterable.iterator()` unconditionally cast the result of `edges.iterator()` to `EdgeIterator`: | ||
| ```java | ||
| return new EdgeToVertexIterator((EdgeIterator) edges.iterator(), direction); | ||
| ``` | ||
| When `direction:BOTH`, `SQLFunctionShortestPath.getVerticesAndEdges()` recurses into `OUT` and `IN`. For a vertex that has only an outgoing edge, the `IN` side returns `GraphEngine.EMPTY_EDGE_LIST`. That list's `iterator()` returns `Collections.emptyIterator()` which is NOT an `EdgeIterator`, causing `ClassCastException`. | ||
|
|
||
| **Bug 2 - Wrong vertex returned:** `EdgeToVertexIterator.next()` called `edge.getVertex(direction)` where `direction` is the traversal direction. For an OUT traversal from vertex 'a', this returns 'a' (the OUT/source end) rather than the neighbor 'b' (the IN/destination end). The BFS made no progress because every "neighbor" was the current vertex itself. | ||
|
|
||
| ## Fix | ||
|
|
||
| **EdgeToVertexIterable:** check `instanceof ResettableIterator` (the common supertype of `EdgeIterator` and `EdgeIteratorFilter`) before casting. `EMPTY_EDGE_LIST.iterator()` returns `Collections.emptyIterator()` which is not a `ResettableIterator`, so it returns an empty iterator instead. | ||
|
|
||
| **EdgeToVertexIterator:** change field type to `ResettableIterator<Edge>` and fix `next()` to use the opposite direction (`direction == OUT ? IN : OUT`), which correctly returns the neighbor vertex. | ||
|
|
||
| ## Files Changed | ||
|
|
||
| - `engine/src/main/java/com/arcadedb/graph/EdgeToVertexIterable.java` - guard against non-ResettableIterator (empty edge list) | ||
| - `engine/src/main/java/com/arcadedb/graph/EdgeToVertexIterator.java` - accept `ResettableIterator<Edge>`; fix `next()` to return the opposite-end vertex | ||
| - `engine/src/test/java/com/arcadedb/function/sql/graph/SQLFunctionShortestPathTest.java` - regression test for `edge:true` + `direction:BOTH` with asymmetric edges | ||
|
|
||
| ## Verification | ||
|
|
||
| Run: `mvn test -pl engine -Dtest=SQLFunctionShortestPathTest` | ||
|
|
||
| Results: 12 tests run, 0 failures, 0 errors. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Silently returning an empty iterator when
iteris not an instance ofResettableIteratorcan lead to silent correctness bugs if a non-empty, non-resettable iterator is passed (for example, standard Java collections likeArrayListorHashSet).If the iterator is not a
ResettableIterator, we should check if it is empty. If it is empty, returningCollections.emptyIterator()is perfectly fine. If it is not empty, we should throw an exception (or wrap it) to prevent silent data loss or incorrect query results.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolved in 705af13.
EdgeToVertexIterable.iterator()now returnsCollections.emptyIterator()only when the non-resettable iterator is empty, and throwsIllegalArgumentExceptionotherwise so a non-empty, non-resettable iterator fails loudly instead of silently dropping edges. In practice the only non-resettable case isGraphEngine.EMPTY_EDGE_LIST(always empty); a comment documents that invariant.