Skip to content

fix(core): Add maxfrontiersize to shortest path query #9382

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
merged 8 commits into from
Apr 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dql/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2691,7 +2691,7 @@ func validKeyAtRoot(k string) bool {
switch k {
case "func", "orderasc", "orderdesc", "first", "offset", "after":
return true
case "from", "to", "numpaths", "minweight", "maxweight":
case "from", "to", "numpaths", "minweight", "maxweight", "maxfrontiersize":
// Specific to shortest path
return true
case "depth":
Expand Down
3 changes: 2 additions & 1 deletion dql/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1302,7 +1302,7 @@ func TestParseQueryWithMultipleVar(t *testing.T) {
func TestParseShortestPath(t *testing.T) {
query := `
{
shortest(from:0x0a, to:0x0b, numpaths: 3, minweight: 3, maxweight: 6) {
shortest(from:0x0a, to:0x0b, numpaths: 3, minweight: 3, maxweight: 6, maxfrontiersize: 1) {
friends
name
}
Expand All @@ -1317,6 +1317,7 @@ func TestParseShortestPath(t *testing.T) {
require.Equal(t, "3", res.Query[0].Args["numpaths"])
require.Equal(t, "3", res.Query[0].Args["minweight"])
require.Equal(t, "6", res.Query[0].Args["maxweight"])
require.Equal(t, "1", res.Query[0].Args["maxfrontiersize"])
}

func TestParseShortestPathWithUidVars(t *testing.T) {
Expand Down
16 changes: 15 additions & 1 deletion query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ type params struct {
MaxWeight float64
// MinWeight is the min weight allowed in a path returned by the shortest path algorithm.
MinWeight float64
// MaxFrontierSize limits the number of candidate paths stored in the priority queue.
// During shortest path computation. This prevents out-of-memory errors on large graphs
// but may affect solution optimality if set too low.
MaxFrontierSize int64

// ExploreDepth is used by recurse and shortest path queries to specify the maximum graph
// depth to explore.
Expand Down Expand Up @@ -714,6 +718,16 @@ func (args *params) fill(gq *dql.GraphQuery) error {
args.MinWeight = -math.MaxFloat64
}

if v, ok := gq.Args["maxfrontiersize"]; ok {
maxfrontiersize, err := strconv.ParseInt(v, 0, 64)
if err != nil {
return err
}
args.MaxFrontierSize = maxfrontiersize
} else if !ok {
args.MaxFrontierSize = math.MaxInt64
}

if gq.ShortestPathArgs.From == nil || gq.ShortestPathArgs.To == nil {
return errors.Errorf("from/to can't be nil for shortest path")
}
Expand Down Expand Up @@ -2640,7 +2654,7 @@ func (sg *SubGraph) sortAndPaginateUsingVar(ctx context.Context) error {
func isValidArg(a string) bool {
switch a {
case "numpaths", "from", "to", "orderasc", "orderdesc", "first", "offset", "after", "depth",
"minweight", "maxweight":
"minweight", "maxweight", "maxfrontiersize":
return true
}
return false
Expand Down
6 changes: 6 additions & 0 deletions query/shortest.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,9 @@ func runKShortestPaths(ctx context.Context, sg *SubGraph) ([]*SubGraph, error) {
hop: item.hop + 1,
path: route{route: curPath},
}
if int64(pq.Len()) > sg.Params.MaxFrontierSize {
pq.Pop()
}
heap.Push(&pq, node)
}
// Return the popped nodes path to pool.
Expand Down Expand Up @@ -558,6 +561,9 @@ func shortestPath(ctx context.Context, sg *SubGraph) ([]*SubGraph, error) {
cost: nodeCost,
hop: item.hop + 1,
}
if int64(pq.Len()) > sg.Params.MaxFrontierSize {
pq.Pop()
}
heap.Push(&pq, node)
} else {
// We've already seen this node. So, just update the cost
Expand Down
Binary file added systest/shortest-path/graph.rdf.gz
Binary file not shown.
Binary file added systest/shortest-path/graph.schema.gz
Binary file not shown.
61 changes: 61 additions & 0 deletions systest/shortest-path/shortest_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//go:build integration2

/*
* SPDX-FileCopyrightText: © Hypermode Inc. <hello@hypermode.com>
* SPDX-License-Identifier: Apache-2.0
*/

package main

import (
"context"
"testing"
"time"

"github.com/hypermodeinc/dgraph/v25/dgraphapi"
"github.com/hypermodeinc/dgraph/v25/dgraphtest"
"github.com/hypermodeinc/dgraph/v25/x"

"github.com/stretchr/testify/require"
)

func TestShortestPath(t *testing.T) {
conf := dgraphtest.NewClusterConfig().WithNumAlphas(1).WithNumZeros(1).WithReplicas(1).WithACL(time.Hour)
c, err := dgraphtest.NewLocalCluster(conf)
require.NoError(t, err)
defer func() { c.Cleanup(t.Failed()) }()
require.NoError(t, c.Start())

err = c.LiveLoad(dgraphtest.LiveOpts{
DataFiles: []string{"graph.rdf.gz"},
SchemaFiles: []string{"graph.schema.gz"},
GqlSchemaFiles: []string{},
})
require.NoError(t, err)

gc, cleanup, err := c.Client()
require.NoError(t, err)
defer cleanup()
require.NoError(t, gc.LoginIntoNamespace(context.Background(),
dgraphapi.DefaultUser, dgraphapi.DefaultPassword, x.GalaxyNamespace))

_, err = gc.Query(`
{
q(func: eq(guid, "85270d10-560e-4cc8-8703-4b4c563a2f4e")) {
a as uid
}
q1(func: eq(guid, "4a520068-80b6-42f2-9019-4e6ef8a02bb3")) {
b as uid
}

path as shortest(from: uid(a), to: uid(b), numpaths: 5, maxfrontiersize: 10000) {
connected_to @facets(weight)
}

path(func: uid(path)) {
uid
}
}
`)
require.NoError(t, err)
}