Skip to content
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

feat(dgraph): Add suport for RDF query. #6038

Merged
merged 16 commits into from
Aug 7, 2020
2 changes: 2 additions & 0 deletions edgraph/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,8 @@ func processQuery(ctx context.Context, qc *queryContext) (*api.Response, error)
respMap["types"] = formatTypes(er.Types)
}
resp.Json, err = json.Marshal(respMap)
} else if qc.req.RdfFormat {
resp.Rdf, err = query.ToRDF(qc.latency, er.Subgraphs)
} else {
resp.Json, err = query.ToJson(qc.latency, er.Subgraphs)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/blevesearch/snowballstem v0.0.0-20180110192139-26b06a2c243d // indirect
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd
github.com/dgraph-io/badger/v2 v2.0.1-rc1.0.20200715005050-3ffaf3cd1d4a
github.com/dgraph-io/dgo/v200 v200.0.0-20200401175452-e463f9234453
github.com/dgraph-io/dgo/v200 v200.0.0-20200720091212-f41d047addd1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove these changes from the merge, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

github.com/dgraph-io/ristretto v0.0.3-0.20200630154024-f66de99634de
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,10 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgraph-io/badger v1.6.0 h1:DshxFxZWXUcO0xX476VJC07Xsr6ZCBVRHKZ93Oh7Evo=
github.com/dgraph-io/badger v1.6.0/go.mod h1:zwt7syl517jmP8s94KqSxTlM6IMsdhYy6psNgSztDR4=
github.com/dgraph-io/badger/v2 v2.0.1-rc1.0.20200711090415-0dfb8b45d4a4 h1:M85aROJxDOXmp8mZQN6x04EraKDGOF2RL3ox/6LZdjo=
github.com/dgraph-io/badger/v2 v2.0.1-rc1.0.20200711090415-0dfb8b45d4a4/go.mod h1:26P/7fbL4kUZVEVKLAKXkBXKOydDmM2p1e+NhhnBCAE=
github.com/dgraph-io/badger/v2 v2.0.1-rc1.0.20200715005050-3ffaf3cd1d4a h1:k8A4B5IEFzH33Px1N7K0FpMszEFTZGQb7HCRBslMB+s=
github.com/dgraph-io/badger/v2 v2.0.1-rc1.0.20200715005050-3ffaf3cd1d4a/go.mod h1:26P/7fbL4kUZVEVKLAKXkBXKOydDmM2p1e+NhhnBCAE=
github.com/dgraph-io/dgo/v200 v200.0.0-20200401175452-e463f9234453 h1:DTgOrw91nMIukDm/WEvdobPLl0LgeDd/JE66+24jBks=
github.com/dgraph-io/dgo/v200 v200.0.0-20200401175452-e463f9234453/go.mod h1:Co+FwJrnndSrPORO8Gdn20dR7FPTfmXr0W/su0Ve/Ig=
github.com/dgraph-io/dgo/v200 v200.0.0-20200720091212-f41d047addd1 h1:bB3SjXpTJ7YcYheynrAlc5K0nlmNgygsj8WHlhRncig=
github.com/dgraph-io/dgo/v200 v200.0.0-20200720091212-f41d047addd1/go.mod h1:Co+FwJrnndSrPORO8Gdn20dR7FPTfmXr0W/su0Ve/Ig=
github.com/dgraph-io/ristretto v0.0.3-0.20200630154024-f66de99634de h1:t0UHb5vdojIDUqktM6+xJAfScFBsVpXZmqC9dsgJmeA=
github.com/dgraph-io/ristretto v0.0.3-0.20200630154024-f66de99634de/go.mod h1:KPxhHT9ZxKefz+PCeOGsrHpl1qZ7i70dGTu2u+Ahh6E=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
Expand Down
11 changes: 11 additions & 0 deletions query/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ func processQuery(ctx context.Context, t *testing.T, query string) (string, erro
return string(jsonResponse), err
}

func processQueryRDF(ctx context.Context, t *testing.T, query string) (string, error) {
txn := client.NewTxn()
defer txn.Discard(ctx)

res, err := txn.Do(ctx, &api.Request{
Query: query,
RdfFormat: true,
})
return string(res.Rdf), err
}

func processQueryNoErr(t *testing.T, query string) string {
res, err := processQuery(context.Background(), t, query)
require.NoError(t, err)
Expand Down
166 changes: 166 additions & 0 deletions query/outputrdf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* Copyright 2017-2020 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package query

import (
"fmt"

"github.com/dgraph-io/dgraph/algo"
"github.com/dgraph-io/dgraph/protos/pb"
"github.com/dgraph-io/dgraph/types"
"github.com/pkg/errors"
)

// ToRDF converts the given subgraph list into rdf format.
func ToRDF(l *Latency, sgl []*SubGraph) ([]byte, error) {
output := ""
for _, sg := range sgl {
// Skip parent graph. we don't want parent values.
for _, child := range sg.Children {
if err := castToRDF(&output, child); err != nil {
return nil, err
}
}

}
return []byte(output), nil
}

// castToRDF converts the given subgraph to RDF and appends to the
// output string.
func castToRDF(output *string, sg *SubGraph) error {

uidCount := sg.Attr == "uid" && sg.Params.DoCount && sg.IsInternal()
if uidCount {
return errors.New("uid count is not supported in the rdf output format")
}

if sg.Params.Normalize {
return errors.New("normalize directive is not supported in the rdf output format")
}

if sg.Params.IgnoreReflex {
return errors.New("ignorereflex directive is not supported in the rdf output format")
}

if sg.SrcFunc != nil && sg.SrcFunc.Name == "checkpwd" {
return errors.New("chkpwd function is not supported in the rdf output format")
}

if sg.SrcUIDs != nil {
// Get RDF for the given subgraph.
if err := rdfForSubgraph(output, sg); err != nil {
return err
}
}

// Recursively cnvert RDF for the children graph.
for _, child := range sg.Children {
err := castToRDF(output, child)
if err != nil {
return err
}
}
return nil
}

// rdfForSubgraph generates RDF and appends to the output parameter.
func rdfForSubgraph(output *string, sg *SubGraph) error {
for i, uid := range sg.SrcUIDs.Uids {
if sg.Params.IgnoreResult {
// Skip ignored values.
continue
}

if sg.IsInternal() {
if sg.Params.Expand != "" {
continue
}
// Check if we have val for the given uid. If you got uid then populate
// the rdf.
val, ok := sg.Params.UidToVal[uid]
if !ok && val.Value == nil {
continue
}
outputval, err := valToBytes(val)
if err != nil {
return err
}
*output += fmt.Sprintf("<%#x> <%s> %s .\n", uid, sg.aggWithVarFieldName(),
string(outputval))
continue
}

switch {
case len(sg.counts) > 0:
// Add count rdf.
rdfForCount(output, uid, sg.counts[i], sg)
case i < len(sg.uidMatrix) && len(sg.uidMatrix[i].Uids) != 0:
// Add posting list relation.
rdfForUIDList(output, uid, sg.uidMatrix[i], sg)
case i < len(sg.valueMatrix):
// TODO: add facet.
rdfForValueList(output, uid, sg.valueMatrix[i], sg.fieldName())
}
}
return nil
}

// rdfForCount returns rdf for count fucntion.
func rdfForCount(output *string, subject uint64, count uint32, sg *SubGraph) {
fieldName := sg.Params.Alias
if fieldName == "" {
fieldName = fmt.Sprintf("count(%s)", sg.Attr)
}
*output += fmt.Sprintf("<%#x> <%s> %d .\n", subject, fieldName, count)
}

// rdfForUIDList returns rdf for uid list.
func rdfForUIDList(output *string, subject uint64, list *pb.List, sg *SubGraph) {
for _, destUID := range list.Uids {
if algo.IndexOf(sg.DestUIDs, destUID) < 0 {
// This uid is filtered.
continue
}
// TODO: do the right RDF format.
*output += fmt.Sprintf("<%#x> <%s> <%#x> .\n", subject, sg.fieldName(), destUID)
}
}

// rdfForValueList returns rdf for the value list.
func rdfForValueList(output *string, subject uint64, valueList *pb.ValueList, attr string) error {
if attr == "uid" {
*output += fmt.Sprintf("<%#x> <%s> <%#x> .\n", subject, attr, subject)
return nil
}
for _, destValue := range valueList.Values {
val, err := convertWithBestEffort(destValue, attr)
if err != nil {
return err
}
outputval, err := valToBytes(val)
if err != nil {
return err
}
switch val.Tid {
case types.UidID:
*output += fmt.Sprintf("<%#x> <%s> <%s> .\n", subject, attr, string(outputval))
default:
*output += fmt.Sprintf("<%#x> <%s> %s .\n", subject, attr, string(outputval))
}
}
return nil
}
50 changes: 50 additions & 0 deletions query/rdf_result_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2020 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package query

import (
"context"
"testing"

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

func TestRDFResult(t *testing.T) {
query := `{
friends_15_and_19(func: uid(1)) {
name
friend @filter(ge(age, 15) AND lt(age, 19)) {
name
age
}
}
}`

rdf, err := processQueryRDF(context.Background(), t, query)
require.NoError(t, err)
require.Equal(t, string(rdf), `<0x1> <name> "Michonne" .
<0x1> <friend> <0x17> .
<0x1> <friend> <0x18> .
<0x1> <friend> <0x19> .
<0x17> <name> "Rick Grimes" .
<0x18> <name> "Glenn Rhee" .
<0x19> <name> "Daryl Dixon" .
<0x17> <age> 15 .
<0x18> <age> 15 .
<0x19> <age> 17 .
`)
}