diff --git a/query/outputrdf.go b/query/outputrdf.go index d39834681b9..7d07ecfb332 100644 --- a/query/outputrdf.go +++ b/query/outputrdf.go @@ -75,6 +75,19 @@ func (b *rdfBuilder) castToRDF(sg *SubGraph) error { // rdfForSubgraph generates RDF and appends to the output parameter. func (b *rdfBuilder) rdfForSubgraph(sg *SubGraph) error { + // handle the case of recurse queries + // Do not generate RDF if all the children of sg null uidMatrix + nonNullChild := false + for _, ch := range sg.Children { + if len(ch.uidMatrix) != 0 { + nonNullChild = true + } + } + + if len(sg.Children) > 0 && !nonNullChild { + return nil + } + for i, uid := range sg.SrcUIDs.Uids { if sg.Params.IgnoreResult { // Skip ignored values. @@ -101,7 +114,7 @@ func (b *rdfBuilder) rdfForSubgraph(sg *SubGraph) error { case len(sg.counts) > 0: // Add count rdf. b.rdfForCount(uid, sg.counts[i], sg) - case i < len(sg.uidMatrix) && len(sg.uidMatrix[i].Uids) != 0: + case i < len(sg.uidMatrix) && len(sg.uidMatrix[i].Uids) != 0 && len(sg.Children) > 0: // Add posting list relation. b.rdfForUIDList(uid, sg.uidMatrix[i], sg) case i < len(sg.valueMatrix): @@ -154,11 +167,8 @@ func (b *rdfBuilder) rdfForUIDList(subject uint64, list *pb.List, sg *SubGraph) } // rdfForValueList returns rdf for the value list. +// Ignore RDF's for the attirbute `uid`. func (b *rdfBuilder) rdfForValueList(subject uint64, valueList *pb.ValueList, attr string) { - if attr == "uid" { - b.writeRDF(subject, []byte(attr), x.ToHex(subject, true)) - return - } for _, destValue := range valueList.Values { val, err := convertWithBestEffort(destValue, attr) if err != nil { diff --git a/query/rdf_result_test.go b/query/rdf_result_test.go index e91b5effff6..eb908b423c0 100644 --- a/query/rdf_result_test.go +++ b/query/rdf_result_test.go @@ -111,6 +111,38 @@ func TestRDFIngoreReflex(t *testing.T) { "ignorereflex directive is not supported in the rdf output format") } +func TestRDFRecurse(t *testing.T) { + query := ` + { + me(func: anyofterms(name, "Michonne Rick Daryl")) @recurse(depth: 1, loop: true) { + name + friend + } + }` + rdf, err := processQueryRDF(context.Background(), t, query) + require.NoError(t, err) + require.Equal(t, rdf, `<0x1> "Michonne" . +<0x17> "Rick Grimes" . +<0x19> "Daryl Dixon" . +`) +} + +func TestRDFIgnoreUid(t *testing.T) { + query := ` + { + me(func: anyofterms(name, "Michonne Rick Daryl")) { + uid + name + } + }` + rdf, err := processQueryRDF(context.Background(), t, query) + require.NoError(t, err) + require.Equal(t, rdf, `<0x1> "Michonne" . +<0x17> "Rick Grimes" . +<0x19> "Daryl Dixon" . +`) +} + func TestRDFCheckPwd(t *testing.T) { query := ` {