From 8337137e16be637d98e9e16d0856a5b276f18569 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=AE=AA=E0=AE=BE=E0=AE=B2=E0=AE=BE=E0=AE=9C=E0=AE=BF?= Date: Mon, 20 Jul 2020 16:34:12 +0530 Subject: [PATCH 01/14] add rdf output MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: பாலாஜி --- query/outputrdf.go | 159 +++++++++++++++++++++++++++++++++++++++ query/rdf_result_test.go | 50 ++++++++++++ 2 files changed, 209 insertions(+) create mode 100644 query/outputrdf.go create mode 100644 query/rdf_result_test.go diff --git a/query/outputrdf.go b/query/outputrdf.go new file mode 100644 index 00000000000..6787ed0dd6e --- /dev/null +++ b/query/outputrdf.go @@ -0,0 +1,159 @@ +/* + * 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 { + 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()) + } + } + } + + // Recursively cast the RDF of childrens. + for _, child := range sg.Children { + err := castToRDF(output, child) + if err != nil { + return err + } + } + + 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 +} diff --git a/query/rdf_result_test.go b/query/rdf_result_test.go new file mode 100644 index 00000000000..f925d6448ab --- /dev/null +++ b/query/rdf_result_test.go @@ -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> "Michonne" +<0x1> <0x17> +<0x1> <0x18> +<0x1> <0x19> +<0x17> "Rick Grimes" +<0x18> "Glenn Rhee" +<0x19> "Daryl Dixon" +<0x17> 15 +<0x18> 15 +<0x19> 17 +`) +} From 2791414e40a076dd2881613ab84836846dee86bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=AE=AA=E0=AE=BE=E0=AE=B2=E0=AE=BE=E0=AE=9C=E0=AE=BF?= Date: Mon, 20 Jul 2020 16:35:22 +0530 Subject: [PATCH 02/14] minor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: பாலாஜி --- edgraph/server.go | 2 ++ go.mod | 2 +- go.sum | 6 ++---- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/edgraph/server.go b/edgraph/server.go index b46494c89bc..384f54a973a 100644 --- a/edgraph/server.go +++ b/edgraph/server.go @@ -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) } diff --git a/go.mod b/go.mod index bc259071435..b869b89f723 100644 --- a/go.mod +++ b/go.mod @@ -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 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 diff --git a/go.sum b/go.sum index 3dab6944453..4ea3c9fd0fe 100644 --- a/go.sum +++ b/go.sum @@ -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= From e180a008b5508172118a260d3e60cc51f700441f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=AE=AA=E0=AE=BE=E0=AE=B2=E0=AE=BE=E0=AE=9C=E0=AE=BF?= Date: Mon, 20 Jul 2020 16:37:11 +0530 Subject: [PATCH 03/14] minor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: பாலாஜி --- query/common_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/query/common_test.go b/query/common_test.go index ce977ef6d01..b691f8e1a6a 100644 --- a/query/common_test.go +++ b/query/common_test.go @@ -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) From d7b53e6af13c6c030e45c7d0a074f98e998800d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=AE=AA=E0=AE=BE=E0=AE=B2=E0=AE=BE=E0=AE=9C=E0=AE=BF?= Date: Mon, 20 Jul 2020 16:47:16 +0530 Subject: [PATCH 04/14] minor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: பாலாஜி --- query/outputrdf.go | 84 +++++++++++++++++++++++++--------------------- 1 file changed, 46 insertions(+), 38 deletions(-) diff --git a/query/outputrdf.go b/query/outputrdf.go index 6787ed0dd6e..4fb611b3afa 100644 --- a/query/outputrdf.go +++ b/query/outputrdf.go @@ -61,54 +61,62 @@ func castToRDF(output *string, sg *SubGraph) error { } if sg.SrcUIDs != nil { - 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()) - } + // Get RDF for the given subgraph. + if err := rdfForSubgraph(output, sg); err != nil { + return err } } - // Recursively cast the RDF of childrens. + // 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 } From f580f740edb4521c0675d32927064f2b5c6a5909 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=AE=AA=E0=AE=BE=E0=AE=B2=E0=AE=BE=E0=AE=9C=E0=AE=BF?= Date: Mon, 20 Jul 2020 16:51:09 +0530 Subject: [PATCH 05/14] remove space MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: பாலாஜி --- query/outputrdf.go | 1 - 1 file changed, 1 deletion(-) diff --git a/query/outputrdf.go b/query/outputrdf.go index 4fb611b3afa..1b676c50bf4 100644 --- a/query/outputrdf.go +++ b/query/outputrdf.go @@ -80,7 +80,6 @@ func castToRDF(output *string, sg *SubGraph) error { // 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 From 5cd81ab8fd1f7dfe713d62b0bc11ddf9969f39a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=AE=AA=E0=AE=BE=E0=AE=B2=E0=AE=BE=E0=AE=9C=E0=AE=BF?= Date: Mon, 27 Jul 2020 13:31:10 +0530 Subject: [PATCH 06/14] fix rdf MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: பாலாஜி --- query/outputrdf.go | 12 ++++++------ query/rdf_result_test.go | 20 ++++++++++---------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/query/outputrdf.go b/query/outputrdf.go index 1b676c50bf4..baf1082bd2a 100644 --- a/query/outputrdf.go +++ b/query/outputrdf.go @@ -99,7 +99,7 @@ func rdfForSubgraph(output *string, sg *SubGraph) error { if err != nil { return err } - *output += fmt.Sprintf("<%#x> <%s> %s\n", uid, sg.aggWithVarFieldName(), + *output += fmt.Sprintf("<%#x> <%s> %s .\n", uid, sg.aggWithVarFieldName(), string(outputval)) continue } @@ -125,7 +125,7 @@ func rdfForCount(output *string, subject uint64, count uint32, sg *SubGraph) { if fieldName == "" { fieldName = fmt.Sprintf("count(%s)", sg.Attr) } - *output += fmt.Sprintf("<%#x> <%s> %d \n", subject, fieldName, count) + *output += fmt.Sprintf("<%#x> <%s> %d .\n", subject, fieldName, count) } // rdfForUIDList returns rdf for uid list. @@ -136,14 +136,14 @@ func rdfForUIDList(output *string, subject uint64, list *pb.List, sg *SubGraph) continue } // TODO: do the right RDF format. - *output += fmt.Sprintf("<%#x> <%s> <%#x> \n", subject, sg.fieldName(), destUID) + *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) + *output += fmt.Sprintf("<%#x> <%s> <%#x> .\n", subject, attr, subject) return nil } for _, destValue := range valueList.Values { @@ -157,9 +157,9 @@ func rdfForValueList(output *string, subject uint64, valueList *pb.ValueList, at } switch val.Tid { case types.UidID: - *output += fmt.Sprintf("<%#x> <%s> <%s> \n", subject, attr, string(outputval)) + *output += fmt.Sprintf("<%#x> <%s> <%s> .\n", subject, attr, string(outputval)) default: - *output += fmt.Sprintf("<%#x> <%s> %s \n", subject, attr, string(outputval)) + *output += fmt.Sprintf("<%#x> <%s> %s .\n", subject, attr, string(outputval)) } } return nil diff --git a/query/rdf_result_test.go b/query/rdf_result_test.go index f925d6448ab..7803bb0e007 100644 --- a/query/rdf_result_test.go +++ b/query/rdf_result_test.go @@ -36,15 +36,15 @@ func TestRDFResult(t *testing.T) { rdf, err := processQueryRDF(context.Background(), t, query) require.NoError(t, err) - require.Equal(t, string(rdf), `<0x1> "Michonne" -<0x1> <0x17> -<0x1> <0x18> -<0x1> <0x19> -<0x17> "Rick Grimes" -<0x18> "Glenn Rhee" -<0x19> "Daryl Dixon" -<0x17> 15 -<0x18> 15 -<0x19> 17 + require.Equal(t, string(rdf), `<0x1> "Michonne" . +<0x1> <0x17> . +<0x1> <0x18> . +<0x1> <0x19> . +<0x17> "Rick Grimes" . +<0x18> "Glenn Rhee" . +<0x19> "Daryl Dixon" . +<0x17> 15 . +<0x18> 15 . +<0x19> 17 . `) } From cc9294c063f154a81b1b750deaa24cd74617bd3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=AE=AA=E0=AE=BE=E0=AE=B2=E0=AE=BE=E0=AE=9C=E0=AE=BF?= Date: Wed, 29 Jul 2020 16:10:33 +0530 Subject: [PATCH 07/14] add test cases and fix comments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: பாலாஜி --- query/outputrdf.go | 135 ++++++++++++++++++++++++++------------- query/rdf_result_test.go | 111 ++++++++++++++++++++++++++++++++ 2 files changed, 202 insertions(+), 44 deletions(-) diff --git a/query/outputrdf.go b/query/outputrdf.go index baf1082bd2a..25c184c9a3b 100644 --- a/query/outputrdf.go +++ b/query/outputrdf.go @@ -13,63 +13,61 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package query import ( + "bytes" "fmt" + "strconv" "github.com/dgraph-io/dgraph/algo" "github.com/dgraph-io/dgraph/protos/pb" "github.com/dgraph-io/dgraph/types" + "github.com/dgraph-io/dgraph/x" "github.com/pkg/errors" ) +// rdfBuilder is used to generate RDF from subgraph. +type rdfBuilder struct { + buf *bytes.Buffer +} + // ToRDF converts the given subgraph list into rdf format. func ToRDF(l *Latency, sgl []*SubGraph) ([]byte, error) { - output := "" + b := &rdfBuilder{ + buf: &bytes.Buffer{}, + } for _, sg := range sgl { + if err := validateSubGraphForRDF(sg); err != nil { + return nil, err + } // Skip parent graph. we don't want parent values. for _, child := range sg.Children { - if err := castToRDF(&output, child); err != nil { + if err := b.castToRDF(child); err != nil { return nil, err } } } - return []byte(output), nil + return b.buf.Bytes(), 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") +func (b *rdfBuilder) castToRDF(sg *SubGraph) error { + if err := validateSubGraphForRDF(sg); err != nil { + return err } - - 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 { + if err := b.rdfForSubgraph(sg); err != nil { return err } } - // Recursively cnvert RDF for the children graph. for _, child := range sg.Children { - err := castToRDF(output, child) + err := b.castToRDF(child) if err != nil { return err } @@ -78,13 +76,12 @@ func castToRDF(output *string, sg *SubGraph) error { } // rdfForSubgraph generates RDF and appends to the output parameter. -func rdfForSubgraph(output *string, sg *SubGraph) error { +func (b *rdfBuilder) rdfForSubgraph(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 @@ -99,68 +96,118 @@ func rdfForSubgraph(output *string, sg *SubGraph) error { if err != nil { return err } - *output += fmt.Sprintf("<%#x> <%s> %s .\n", uid, sg.aggWithVarFieldName(), - string(outputval)) + b.writeRDF(uid, []byte(sg.aggWithVarFieldName()), outputval) continue } - switch { case len(sg.counts) > 0: // Add count rdf. - rdfForCount(output, uid, sg.counts[i], sg) + b.rdfForCount(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) + b.rdfForUIDList(uid, sg.uidMatrix[i], sg) case i < len(sg.valueMatrix): // TODO: add facet. - rdfForValueList(output, uid, sg.valueMatrix[i], sg.fieldName()) + b.rdfForValueList(uid, sg.valueMatrix[i], sg.fieldName()) } } return nil } +func (b *rdfBuilder) writeRDF(subject uint64, predicate []byte, object []byte) { + // add subject + b.writeTriple([]byte(fmt.Sprintf("%#x", subject))) + x.Check(b.buf.WriteByte(' ')) + // add predicate + b.writeTriple(predicate) + x.Check(b.buf.WriteByte(' ')) + // add object + x.Check2(b.buf.Write(object)) + x.Check(b.buf.WriteByte(' ')) + x.Check(b.buf.WriteByte('.')) + x.Check(b.buf.WriteByte('\n')) +} + +func (b *rdfBuilder) writeTriple(val []byte) { + x.Check(b.buf.WriteByte('<')) + x.Check2(b.buf.Write(val)) + x.Check(b.buf.WriteByte('>')) +} + // rdfForCount returns rdf for count fucntion. -func rdfForCount(output *string, subject uint64, count uint32, sg *SubGraph) { +func (b *rdfBuilder) rdfForCount(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) + b.writeRDF(subject, []byte(fieldName), []byte(strconv.FormatUint(uint64(count), 10))) } // rdfForUIDList returns rdf for uid list. -func rdfForUIDList(output *string, subject uint64, list *pb.List, sg *SubGraph) { +func (b *rdfBuilder) rdfForUIDList(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) + // Build object. + b.writeRDF( + subject, + []byte(sg.fieldName()), + buildTriple([]byte(fmt.Sprintf("%#x", destUID)))) } } // rdfForValueList returns rdf for the value list. -func rdfForValueList(output *string, subject uint64, valueList *pb.ValueList, attr string) error { +func (b *rdfBuilder) rdfForValueList(subject uint64, valueList *pb.ValueList, attr string) { if attr == "uid" { - *output += fmt.Sprintf("<%#x> <%s> <%#x> .\n", subject, attr, subject) - return nil + b.writeRDF(subject, + []byte(attr), + buildTriple([]byte(fmt.Sprintf("%#x", subject)))) + return } for _, destValue := range valueList.Values { val, err := convertWithBestEffort(destValue, attr) if err != nil { - return err + continue } outputval, err := valToBytes(val) if err != nil { - return err + continue } switch val.Tid { case types.UidID: - *output += fmt.Sprintf("<%#x> <%s> <%s> .\n", subject, attr, string(outputval)) + b.writeRDF(subject, []byte(attr), buildTriple(outputval)) default: - *output += fmt.Sprintf("<%#x> <%s> %s .\n", subject, attr, string(outputval)) + b.writeRDF(subject, []byte(attr), outputval) } } +} + +func buildTriple(val []byte) []byte { + buf := make([]byte, 0, 2+len(val)) + buf = append(buf, '<') + buf = append(buf, val...) + buf = append(buf, '>') + return buf +} + +func validateSubGraphForRDF(sg *SubGraph) error { + if sg.IsGroupBy() { + return errors.New("groupby is not supported in rdf output format") + } + 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") + } return nil } diff --git a/query/rdf_result_test.go b/query/rdf_result_test.go index 7803bb0e007..2c58fba13d4 100644 --- a/query/rdf_result_test.go +++ b/query/rdf_result_test.go @@ -48,3 +48,114 @@ func TestRDFResult(t *testing.T) { <0x19> 17 . `) } + +func TestRDFNormalize(t *testing.T) { + query := ` + { + me(func: uid(0x01)) @normalize { + mn: name + gender + friend { + n: name + d: dob + friend { + fn : name + } + } + son { + sn: name + } + } + }` + _, err := processQueryRDF(context.Background(), t, query) + require.Error(t, err, "normalize directive is not supported in the rdf output format") +} + +func TestRDFGroupBy(t *testing.T) { + query := ` + { + me(func: uid(1, 23, 24, 25, 31)) @groupby(age) { + count(uid) + } + }` + _, err := processQueryRDF(context.Background(), t, query) + require.Contains(t, err.Error(), "groupby is not supported in rdf output format") +} + +func TestRDFUidCount(t *testing.T) { + query := ` + { + me(func: gt(count(friend), 0)) { + count(uid) + } + }` + _, err := processQueryRDF(context.Background(), t, query) + require.Contains(t, err.Error(), "uid count is not supported in the rdf output format") +} + +func TestRDFIngoreReflex(t *testing.T) { + query := ` + { + me(func:anyofterms(name, "Michonne Rick Daryl")) @ignoreReflex { + name + friend { + name + friend { + name + } + } + } + }` + _, err := processQueryRDF(context.Background(), t, query) + require.Contains(t, err.Error(), + "ignorereflex directive is not supported in the rdf output format") +} + +func TestRDFCheckPwd(t *testing.T) { + query := ` + { + me(func: uid(0x01)) { + expand(_all_) + checkpwd(password, "12345") + } + } + ` + _, err := processQueryRDF(context.Background(), t, query) + require.Contains(t, err.Error(), + "chkpwd function is not supported in the rdf output format") +} + +func TestRDFPredicateCount(t *testing.T) { + query := ` + { + me(func:anyofterms(name, "Michonne Rick Daryl")) { + name + count(friend) + friend { + name + } + } + } + ` + + rdf, err := processQueryRDF(context.Background(), t, query) + require.NoError(t, err) + require.Equal(t, `<0x1> "Michonne" . +<0x17> "Rick Grimes" . +<0x19> "Daryl Dixon" . +<0x1> 5 . +<0x17> 1 . +<0x19> 0 . +<0x1> <0x17> . +<0x1> <0x18> . +<0x1> <0x19> . +<0x1> <0x1f> . +<0x1> <0x65> . +<0x17> <0x1> . +<0x1> "Michonne" . +<0x17> "Rick Grimes" . +<0x18> "Glenn Rhee" . +<0x19> "Daryl Dixon" . +<0x1f> "Andrea" . +`, rdf) +} From 900a6751f6c3d71bea0abd570debb56ad56a898a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=AE=AA=E0=AE=BE=E0=AE=B2=E0=AE=BE=E0=AE=9C=E0=AE=BF?= Date: Wed, 29 Jul 2020 16:15:18 +0530 Subject: [PATCH 08/14] get new dgo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: பாலாஜி --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index a3d0573f9f6..fd4309a8852 100644 --- a/go.mod +++ b/go.mod @@ -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.20200718033852-37ee16d8ad1c - github.com/dgraph-io/dgo/v200 v200.0.0-20200401175452-e463f9234453 + github.com/dgraph-io/dgo/v200 v200.0.0-20200720091212-f41d047addd1 github.com/dgraph-io/graphql-transport-ws v0.0.0-20200715131837-c0460019ead2 github.com/dgraph-io/ristretto v0.0.3-0.20200630154024-f66de99634de github.com/dgrijalva/jwt-go v3.2.0+incompatible diff --git a/go.sum b/go.sum index 59a58118f0d..a1c21578adc 100644 --- a/go.sum +++ b/go.sum @@ -82,6 +82,8 @@ github.com/dgraph-io/badger/v2 v2.0.1-rc1.0.20200718033852-37ee16d8ad1c h1:LoEZf github.com/dgraph-io/badger/v2 v2.0.1-rc1.0.20200718033852-37ee16d8ad1c/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/graphql-transport-ws v0.0.0-20200715131837-c0460019ead2 h1:NSl3XXyON9bgmBJSAvr5FPrgILAovtoTs7FwdtaZZq0= github.com/dgraph-io/graphql-transport-ws v0.0.0-20200715131837-c0460019ead2/go.mod h1:7z3c/5w0sMYYZF5bHsrh8IH4fKwG5O5Y70cPH1ZLLRQ= github.com/dgraph-io/ristretto v0.0.3-0.20200630154024-f66de99634de h1:t0UHb5vdojIDUqktM6+xJAfScFBsVpXZmqC9dsgJmeA= From b9c5c202f2e500a56e6d038f4ba1bf0f02852ba4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=AE=AA=E0=AE=BE=E0=AE=B2=E0=AE=BE=E0=AE=9C=E0=AE=BF?= Date: Wed, 29 Jul 2020 16:33:21 +0530 Subject: [PATCH 09/14] ignore facets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: பாலாஜி --- query/outputrdf.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/query/outputrdf.go b/query/outputrdf.go index 25c184c9a3b..f0571519f68 100644 --- a/query/outputrdf.go +++ b/query/outputrdf.go @@ -48,7 +48,6 @@ func ToRDF(l *Latency, sgl []*SubGraph) ([]byte, error) { return nil, err } } - } return b.buf.Bytes(), nil } @@ -67,8 +66,7 @@ func (b *rdfBuilder) castToRDF(sg *SubGraph) error { } // Recursively cnvert RDF for the children graph. for _, child := range sg.Children { - err := b.castToRDF(child) - if err != nil { + if err := b.castToRDF(child); err != nil { return err } } @@ -107,7 +105,6 @@ func (b *rdfBuilder) rdfForSubgraph(sg *SubGraph) error { // Add posting list relation. b.rdfForUIDList(uid, sg.uidMatrix[i], sg) case i < len(sg.valueMatrix): - // TODO: add facet. b.rdfForValueList(uid, sg.valueMatrix[i], sg.fieldName()) } } @@ -209,5 +206,8 @@ func validateSubGraphForRDF(sg *SubGraph) error { if sg.SrcFunc != nil && sg.SrcFunc.Name == "checkpwd" { return errors.New("chkpwd function is not supported in the rdf output format") } + if len(sg.facetsMatrix) != 0 { + return errors.New("facet is not supported in the rdf output format") + } return nil } From 6b0f0c6ed08e66e32ddda5bd25846c90529e1b32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=AE=AA=E0=AE=BE=E0=AE=B2=E0=AE=BE=E0=AE=9C=E0=AE=BF?= Date: Wed, 29 Jul 2020 16:39:45 +0530 Subject: [PATCH 10/14] add test for facets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: பாலாஜி --- query/common_test.go | 3 +++ query/rdf_result_test.go | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/query/common_test.go b/query/common_test.go index 8834983fc0f..aff9768fc00 100644 --- a/query/common_test.go +++ b/query/common_test.go @@ -77,6 +77,9 @@ func processQueryRDF(ctx context.Context, t *testing.T, query string) (string, e Query: query, RdfFormat: true, }) + if err != nil { + return "", err + } return string(res.Rdf), err } diff --git a/query/rdf_result_test.go b/query/rdf_result_test.go index 2c58fba13d4..b3d267a46ff 100644 --- a/query/rdf_result_test.go +++ b/query/rdf_result_test.go @@ -159,3 +159,15 @@ func TestRDFPredicateCount(t *testing.T) { <0x1f> "Andrea" . `, rdf) } + +func TestRDFFacets(t *testing.T) { + query := ` + { + shortest(from: 1, to:1001, numpaths: 4) { + path @facets(weight) + } + }` + _, err := processQueryRDF(context.Background(), t, query) + require.Contains(t, err.Error(), + "facet is not supported in the rdf output format") +} From 863615237e17ba44687f5cf15153542dc99a5788 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=AE=AA=E0=AE=BE=E0=AE=B2=E0=AE=BE=E0=AE=9C=E0=AE=BF?= Date: Thu, 30 Jul 2020 12:09:36 +0530 Subject: [PATCH 11/14] fix facets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: பாலாஜி --- query/outputrdf.go | 4 ++-- query/rdf_result_test.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/query/outputrdf.go b/query/outputrdf.go index f0571519f68..e8f1b16e108 100644 --- a/query/outputrdf.go +++ b/query/outputrdf.go @@ -206,8 +206,8 @@ func validateSubGraphForRDF(sg *SubGraph) error { if sg.SrcFunc != nil && sg.SrcFunc.Name == "checkpwd" { return errors.New("chkpwd function is not supported in the rdf output format") } - if len(sg.facetsMatrix) != 0 { - return errors.New("facet is not supported in the rdf output format") + if sg.Params.Facet != nil && !sg.Params.ExpandAll { + return errors.New("facets are not supported in the rdf output format") } return nil } diff --git a/query/rdf_result_test.go b/query/rdf_result_test.go index b3d267a46ff..65dc73f8e32 100644 --- a/query/rdf_result_test.go +++ b/query/rdf_result_test.go @@ -36,7 +36,7 @@ func TestRDFResult(t *testing.T) { rdf, err := processQueryRDF(context.Background(), t, query) require.NoError(t, err) - require.Equal(t, string(rdf), `<0x1> "Michonne" . + require.Equal(t, rdf, `<0x1> "Michonne" . <0x1> <0x17> . <0x1> <0x18> . <0x1> <0x19> . @@ -169,5 +169,5 @@ func TestRDFFacets(t *testing.T) { }` _, err := processQueryRDF(context.Background(), t, query) require.Contains(t, err.Error(), - "facet is not supported in the rdf output format") + "facets are not supported in the rdf output format") } From 08db521beb72524ad51f3327bfc87238c2d3d568 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=AE=AA=E0=AE=BE=E0=AE=B2=E0=AE=BE=E0=AE=9C=E0=AE=BF?= Date: Wed, 5 Aug 2020 10:19:16 +0530 Subject: [PATCH 12/14] fix quote error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: பாலாஜி --- edgraph/server.go | 2 +- go.mod | 2 +- go.sum | 2 ++ query/common_test.go | 4 ++-- query/outputrdf.go | 14 +++++++++++++ query/rdf_result_test.go | 44 ++++++++++++++++++++++++++++++++++------ 6 files changed, 58 insertions(+), 10 deletions(-) diff --git a/edgraph/server.go b/edgraph/server.go index 384f54a973a..f683cb861fb 100644 --- a/edgraph/server.go +++ b/edgraph/server.go @@ -1074,7 +1074,7 @@ 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 { + } else if qc.req.RespFormat == api.Request_RDF { resp.Rdf, err = query.ToRDF(qc.latency, er.Subgraphs) } else { resp.Json, err = query.ToJson(qc.latency, er.Subgraphs) diff --git a/go.mod b/go.mod index fd4309a8852..3be77eb37c1 100644 --- a/go.mod +++ b/go.mod @@ -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.20200718033852-37ee16d8ad1c - github.com/dgraph-io/dgo/v200 v200.0.0-20200720091212-f41d047addd1 + github.com/dgraph-io/dgo/v200 v200.0.0-20200804060423-c5d7b9097381 github.com/dgraph-io/graphql-transport-ws v0.0.0-20200715131837-c0460019ead2 github.com/dgraph-io/ristretto v0.0.3-0.20200630154024-f66de99634de github.com/dgrijalva/jwt-go v3.2.0+incompatible diff --git a/go.sum b/go.sum index a1c21578adc..ac2c6130199 100644 --- a/go.sum +++ b/go.sum @@ -84,6 +84,8 @@ github.com/dgraph-io/dgo/v200 v200.0.0-20200401175452-e463f9234453 h1:DTgOrw91nM 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/dgo/v200 v200.0.0-20200804060423-c5d7b9097381 h1:g5JuBmQHe5c5X7siWhv5TOINQpPwWbDYRvwvSa8sg5Q= +github.com/dgraph-io/dgo/v200 v200.0.0-20200804060423-c5d7b9097381/go.mod h1:rHa+h3kI4M8ASOirxyIyNeXBfHFgeskVUum2OrDMN3U= github.com/dgraph-io/graphql-transport-ws v0.0.0-20200715131837-c0460019ead2 h1:NSl3XXyON9bgmBJSAvr5FPrgILAovtoTs7FwdtaZZq0= github.com/dgraph-io/graphql-transport-ws v0.0.0-20200715131837-c0460019ead2/go.mod h1:7z3c/5w0sMYYZF5bHsrh8IH4fKwG5O5Y70cPH1ZLLRQ= github.com/dgraph-io/ristretto v0.0.3-0.20200630154024-f66de99634de h1:t0UHb5vdojIDUqktM6+xJAfScFBsVpXZmqC9dsgJmeA= diff --git a/query/common_test.go b/query/common_test.go index aff9768fc00..65ab6b00081 100644 --- a/query/common_test.go +++ b/query/common_test.go @@ -74,8 +74,8 @@ func processQueryRDF(ctx context.Context, t *testing.T, query string) (string, e defer txn.Discard(ctx) res, err := txn.Do(ctx, &api.Request{ - Query: query, - RdfFormat: true, + Query: query, + RespFormat: api.Request_RDF, }) if err != nil { return "", err diff --git a/query/outputrdf.go b/query/outputrdf.go index e8f1b16e108..7b2074757d7 100644 --- a/query/outputrdf.go +++ b/query/outputrdf.go @@ -175,6 +175,12 @@ func (b *rdfBuilder) rdfForValueList(subject uint64, valueList *pb.ValueList, at switch val.Tid { case types.UidID: b.writeRDF(subject, []byte(attr), buildTriple(outputval)) + case types.IntID: + b.writeRDF(subject, []byte(attr), quotedNumber(outputval)) + case types.FloatID: + b.writeRDF(subject, []byte(attr), quotedNumber(outputval)) + case types.GeoID: + // SKIP GEO ID. Since we don't support geo in RDF format. default: b.writeRDF(subject, []byte(attr), outputval) } @@ -211,3 +217,11 @@ func validateSubGraphForRDF(sg *SubGraph) error { } return nil } + +func quotedNumber(val []byte) []byte { + tmpVal := make([]byte, 0, len(val)+2) + tmpVal = append(tmpVal, '"') + tmpVal = append(tmpVal, val...) + tmpVal = append(tmpVal, '"') + return tmpVal +} diff --git a/query/rdf_result_test.go b/query/rdf_result_test.go index 65dc73f8e32..e91b5effff6 100644 --- a/query/rdf_result_test.go +++ b/query/rdf_result_test.go @@ -43,9 +43,9 @@ func TestRDFResult(t *testing.T) { <0x17> "Rick Grimes" . <0x18> "Glenn Rhee" . <0x19> "Daryl Dixon" . -<0x17> 15 . -<0x18> 15 . -<0x19> 17 . +<0x17> "15" . +<0x18> "15" . +<0x19> "17" . `) } @@ -143,9 +143,9 @@ func TestRDFPredicateCount(t *testing.T) { require.Equal(t, `<0x1> "Michonne" . <0x17> "Rick Grimes" . <0x19> "Daryl Dixon" . -<0x1> 5 . -<0x17> 1 . -<0x19> 0 . +<0x1> "5" . +<0x17> "1" . +<0x19> "0" . <0x1> <0x17> . <0x1> <0x18> . <0x1> <0x19> . @@ -171,3 +171,35 @@ func TestRDFFacets(t *testing.T) { require.Contains(t, err.Error(), "facets are not supported in the rdf output format") } + +func TestDateRDF(t *testing.T) { + query := ` + { + me(func: uid(0x01)) { + name + gender + friend(orderdesc: film.film.initial_release_date) { + name + film.film.initial_release_date + } + } + } + ` + rdf, err := processQueryRDF(context.Background(), t, query) + require.NoError(t, err) + require.Equal(t, rdf, `<0x1> "Michonne" . +<0x1> "female" . +<0x1> <0x19> . +<0x1> <0x18> . +<0x1> <0x17> . +<0x1> <0x1f> . +<0x17> "Rick Grimes" . +<0x18> "Glenn Rhee" . +<0x19> "Daryl Dixon" . +<0x1f> "Andrea" . +<0x17> "1900-01-02T00:00:00Z" . +<0x18> "1909-05-05T00:00:00Z" . +<0x19> "1929-01-10T00:00:00Z" . +<0x1f> "1801-01-15T00:00:00Z" . +`) +} From 0b0ade036460791857a886995c98e028f59aeb98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=AE=AA=E0=AE=BE=E0=AE=B2=E0=AE=BE=E0=AE=9C=E0=AE=BF?= Date: Thu, 6 Aug 2020 19:40:38 +0530 Subject: [PATCH 13/14] fix go.mod MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: பாலாஜி --- go.mod | 2 +- go.sum | 8 ++------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index 3be77eb37c1..ea3143419af 100644 --- a/go.mod +++ b/go.mod @@ -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.20200718033852-37ee16d8ad1c - github.com/dgraph-io/dgo/v200 v200.0.0-20200804060423-c5d7b9097381 + github.com/dgraph-io/dgo/v200 v200.0.0-20200805103119-a3544c464dd6 github.com/dgraph-io/graphql-transport-ws v0.0.0-20200715131837-c0460019ead2 github.com/dgraph-io/ristretto v0.0.3-0.20200630154024-f66de99634de github.com/dgrijalva/jwt-go v3.2.0+incompatible diff --git a/go.sum b/go.sum index ac2c6130199..ec27cecb7a8 100644 --- a/go.sum +++ b/go.sum @@ -80,12 +80,8 @@ github.com/dgraph-io/badger v1.6.0 h1:DshxFxZWXUcO0xX476VJC07Xsr6ZCBVRHKZ93Oh7Ev github.com/dgraph-io/badger v1.6.0/go.mod h1:zwt7syl517jmP8s94KqSxTlM6IMsdhYy6psNgSztDR4= github.com/dgraph-io/badger/v2 v2.0.1-rc1.0.20200718033852-37ee16d8ad1c h1:LoEZfU53r3H1et4WY9M0h1c3fuCljBnn3pk/7TB5eWY= github.com/dgraph-io/badger/v2 v2.0.1-rc1.0.20200718033852-37ee16d8ad1c/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/dgo/v200 v200.0.0-20200804060423-c5d7b9097381 h1:g5JuBmQHe5c5X7siWhv5TOINQpPwWbDYRvwvSa8sg5Q= -github.com/dgraph-io/dgo/v200 v200.0.0-20200804060423-c5d7b9097381/go.mod h1:rHa+h3kI4M8ASOirxyIyNeXBfHFgeskVUum2OrDMN3U= +github.com/dgraph-io/dgo/v200 v200.0.0-20200805103119-a3544c464dd6 h1:toHzMCdCUgYsjM0cW9+wafnKFXfp1HizIJUyzihN+vk= +github.com/dgraph-io/dgo/v200 v200.0.0-20200805103119-a3544c464dd6/go.mod h1:rHa+h3kI4M8ASOirxyIyNeXBfHFgeskVUum2OrDMN3U= github.com/dgraph-io/graphql-transport-ws v0.0.0-20200715131837-c0460019ead2 h1:NSl3XXyON9bgmBJSAvr5FPrgILAovtoTs7FwdtaZZq0= github.com/dgraph-io/graphql-transport-ws v0.0.0-20200715131837-c0460019ead2/go.mod h1:7z3c/5w0sMYYZF5bHsrh8IH4fKwG5O5Y70cPH1ZLLRQ= github.com/dgraph-io/ristretto v0.0.3-0.20200630154024-f66de99634de h1:t0UHb5vdojIDUqktM6+xJAfScFBsVpXZmqC9dsgJmeA= From 52fd7fe90f96b44f93d1613d1f00621e81f72b2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=AE=AA=E0=AE=BE=E0=AE=B2=E0=AE=BE=E0=AE=9C=E0=AE=BF?= Date: Fri, 7 Aug 2020 11:09:14 +0530 Subject: [PATCH 14/14] fix quoted number issue MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: பாலாஜி --- query/outputrdf.go | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/query/outputrdf.go b/query/outputrdf.go index 7b2074757d7..6fc364aaeec 100644 --- a/query/outputrdf.go +++ b/query/outputrdf.go @@ -90,9 +90,9 @@ func (b *rdfBuilder) rdfForSubgraph(sg *SubGraph) error { if !ok && val.Value == nil { continue } - outputval, err := valToBytes(val) + outputval, err := getObjectVal(val) if err != nil { - return err + continue } b.writeRDF(uid, []byte(sg.aggWithVarFieldName()), outputval) continue @@ -137,7 +137,8 @@ func (b *rdfBuilder) rdfForCount(subject uint64, count uint32, sg *SubGraph) { if fieldName == "" { fieldName = fmt.Sprintf("count(%s)", sg.Attr) } - b.writeRDF(subject, []byte(fieldName), []byte(strconv.FormatUint(uint64(count), 10))) + b.writeRDF(subject, []byte(fieldName), + quotedNumber([]byte(strconv.FormatUint(uint64(count), 10)))) } // rdfForUIDList returns rdf for uid list. @@ -168,22 +169,30 @@ func (b *rdfBuilder) rdfForValueList(subject uint64, valueList *pb.ValueList, at if err != nil { continue } - outputval, err := valToBytes(val) + outputval, err := getObjectVal(val) if err != nil { continue } - switch val.Tid { - case types.UidID: - b.writeRDF(subject, []byte(attr), buildTriple(outputval)) - case types.IntID: - b.writeRDF(subject, []byte(attr), quotedNumber(outputval)) - case types.FloatID: - b.writeRDF(subject, []byte(attr), quotedNumber(outputval)) - case types.GeoID: - // SKIP GEO ID. Since we don't support geo in RDF format. - default: - b.writeRDF(subject, []byte(attr), outputval) - } + b.writeRDF(subject, []byte(attr), outputval) + } +} + +func getObjectVal(v types.Val) ([]byte, error) { + outputval, err := valToBytes(v) + if err != nil { + return nil, err + } + switch v.Tid { + case types.UidID: + return buildTriple(outputval), nil + case types.IntID: + return quotedNumber(outputval), nil + case types.FloatID: + return quotedNumber(outputval), nil + case types.GeoID: + return nil, errors.New("Geo id is not supported in rdf output") + default: + return outputval, nil } }