From 19fa1ce014e9e6481bff526830a9fceaaf6cbe48 Mon Sep 17 00:00:00 2001 From: Nelson Pecora Date: Thu, 18 Feb 2021 11:33:50 -0500 Subject: [PATCH 1/5] add failing test for deeply-nested uid facets --- chunker/json_parser_test.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/chunker/json_parser_test.go b/chunker/json_parser_test.go index be3209adbf0..23d7d25fc71 100644 --- a/chunker/json_parser_test.go +++ b/chunker/json_parser_test.go @@ -713,6 +713,38 @@ func TestNquadsFromJsonFacets2(t *testing.T) { checkCount(t, fastNQ, "friend", 1) } +func TestNquadsFromJsonFacets20000000(t *testing.T) { + // Dave has uid facets which should go on the edge between Alice and Dave, + // AND Emily has uid facets which should go on the edge between Dave and Emily + json := `[ + { + "name":"Alice", + "friend":[ + { + "name":"Dave", + "friend|close":"true", + "friend":[ + { + "name":"Emily", + "friend|close":true + } + ] + } + ] + } + ]` + + nq, err := Parse([]byte(json), SetNquads) + require.NoError(t, err) + require.Equal(t, 3, len(nq)) + checkCount(t, nq, "friend", 2) + + fastNQ, err := FastParse([]byte(json), SetNquads) + require.NoError(t, err) + require.Equal(t, 3, len(fastNQ)) + checkCount(t, fastNQ, "friend", 2) +} + // Test valid facets json. func TestNquadsFromJsonFacets3(t *testing.T) { json := ` From c5a92b6164d8b09ae70a0f630a1e99a8e9176d31 Mon Sep 17 00:00:00 2001 From: Nelson Pecora Date: Thu, 18 Feb 2021 11:43:22 -0500 Subject: [PATCH 2/5] move test and give it a better name --- chunker/json_parser_test.go | 64 ++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/chunker/json_parser_test.go b/chunker/json_parser_test.go index 23d7d25fc71..dd0c92b43d9 100644 --- a/chunker/json_parser_test.go +++ b/chunker/json_parser_test.go @@ -713,38 +713,6 @@ func TestNquadsFromJsonFacets2(t *testing.T) { checkCount(t, fastNQ, "friend", 1) } -func TestNquadsFromJsonFacets20000000(t *testing.T) { - // Dave has uid facets which should go on the edge between Alice and Dave, - // AND Emily has uid facets which should go on the edge between Dave and Emily - json := `[ - { - "name":"Alice", - "friend":[ - { - "name":"Dave", - "friend|close":"true", - "friend":[ - { - "name":"Emily", - "friend|close":true - } - ] - } - ] - } - ]` - - nq, err := Parse([]byte(json), SetNquads) - require.NoError(t, err) - require.Equal(t, 3, len(nq)) - checkCount(t, nq, "friend", 2) - - fastNQ, err := FastParse([]byte(json), SetNquads) - require.NoError(t, err) - require.Equal(t, 3, len(fastNQ)) - checkCount(t, fastNQ, "friend", 2) -} - // Test valid facets json. func TestNquadsFromJsonFacets3(t *testing.T) { json := ` @@ -952,6 +920,38 @@ func TestNquadsFromJsonFacets4(t *testing.T) { } } +func TestNquadsFromJsonFacets5(t *testing.T) { + // Dave has uid facets which should go on the edge between Alice and Dave, + // AND Emily has uid facets which should go on the edge between Dave and Emily + json := `[ + { + "name":"Alice", + "friend":[ + { + "name":"Dave", + "friend|close":"true", + "friend":[ + { + "name":"Emily", + "friend|close":true + } + ] + } + ] + } + ]` + + nq, err := Parse([]byte(json), SetNquads) + require.NoError(t, err) + require.Equal(t, 3, len(nq)) + checkCount(t, nq, "friend", 2) + + fastNQ, err := FastParse([]byte(json), SetNquads) + require.NoError(t, err) + require.Equal(t, 3, len(fastNQ)) + checkCount(t, fastNQ, "friend", 2) +} + func TestNquadsFromJsonError1(t *testing.T) { p := Person{ Name: "Alice", From d313217a11e09ab20b6f246617eddfde366b5c76 Mon Sep 17 00:00:00 2001 From: Karl McGuire Date: Thu, 18 Feb 2021 15:56:16 -0500 Subject: [PATCH 3/5] fix --- chunker/json_parser.go | 20 ++++++++++++++------ chunker/json_parser_test.go | 22 +++++++++++----------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/chunker/json_parser.go b/chunker/json_parser.go index cbaa75aefdc..ea2631ec644 100644 --- a/chunker/json_parser.go +++ b/chunker/json_parser.go @@ -564,14 +564,22 @@ func (buf *NQuadBuffer) mapToNquads(m map[string]interface{}, op int, parentPred buf.PushPredHint(pred, pb.Metadata_SINGLE) case []interface{}: buf.PushPredHint(pred, pb.Metadata_LIST) - // TODO(Ashish): We need to call this only in case of scalarlist, for other lists - // this can be avoided. - facetsMapSlice, err := parseMapFacets(mf, prefix) - if err != nil { - return mr, err - } + // NOTE: facetsMapSlice should be empty unless this is a scalar list + var facetsMapSlice []map[int]*api.Facet for idx, item := range v { + if idx == 0 { + switch item.(type) { + case string, float64, json.Number, int64: + var err error + facetsMapSlice, err = parseMapFacets(mf, prefix) + if err != nil { + return mr, err + } + default: + } + } + nq := api.NQuad{ Subject: mr.uid, Predicate: pred, diff --git a/chunker/json_parser_test.go b/chunker/json_parser_test.go index dd0c92b43d9..da9c821229c 100644 --- a/chunker/json_parser_test.go +++ b/chunker/json_parser_test.go @@ -925,15 +925,15 @@ func TestNquadsFromJsonFacets5(t *testing.T) { // AND Emily has uid facets which should go on the edge between Dave and Emily json := `[ { - "name":"Alice", - "friend":[ + "name": "Alice", + "friend": [ { - "name":"Dave", - "friend|close":"true", - "friend":[ + "name": "Dave", + "friend|close": "true", + "friend": [ { - "name":"Emily", - "friend|close":true + "name": "Emily", + "friend|close": true } ] } @@ -943,13 +943,13 @@ func TestNquadsFromJsonFacets5(t *testing.T) { nq, err := Parse([]byte(json), SetNquads) require.NoError(t, err) - require.Equal(t, 3, len(nq)) - checkCount(t, nq, "friend", 2) + require.Equal(t, 5, len(nq)) + checkCount(t, nq, "friend", 1) fastNQ, err := FastParse([]byte(json), SetNquads) require.NoError(t, err) - require.Equal(t, 3, len(fastNQ)) - checkCount(t, fastNQ, "friend", 2) + require.Equal(t, 5, len(fastNQ)) + checkCount(t, fastNQ, "friend", 1) } func TestNquadsFromJsonError1(t *testing.T) { From 95a93c435f83e9f2b22639e8c4f3e637ed645898 Mon Sep 17 00:00:00 2001 From: Karl McGuire Date: Thu, 18 Feb 2021 16:16:12 -0500 Subject: [PATCH 4/5] fix json typo --- chunker/json_parser_test.go | 2 +- go.mod | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/chunker/json_parser_test.go b/chunker/json_parser_test.go index da9c821229c..63a3cb36ebf 100644 --- a/chunker/json_parser_test.go +++ b/chunker/json_parser_test.go @@ -929,7 +929,7 @@ func TestNquadsFromJsonFacets5(t *testing.T) { "friend": [ { "name": "Dave", - "friend|close": "true", + "friend|close": true, "friend": [ { "name": "Emily", diff --git a/go.mod b/go.mod index d7e1b334eeb..62bd3455d5f 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/Shopify/sarama v1.27.2 github.com/blevesearch/bleve v1.0.13 github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd + github.com/davecgh/go-spew v1.1.1 github.com/dgraph-io/badger/v3 v3.0.0-20210209201111-6c35ad6c28e0 github.com/dgraph-io/dgo/v200 v200.0.0-20210212152539-e0a5bde40ba2 github.com/dgraph-io/gqlgen v0.13.2 From 93d8b0a4d7efed1f6764a23e304ffe5506b65599 Mon Sep 17 00:00:00 2001 From: Karl McGuire Date: Thu, 18 Feb 2021 17:02:36 -0500 Subject: [PATCH 5/5] tidy --- go.mod | 2 -- go.sum | 2 -- 2 files changed, 4 deletions(-) diff --git a/go.mod b/go.mod index 62bd3455d5f..366571db8c3 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,6 @@ require ( github.com/Shopify/sarama v1.27.2 github.com/blevesearch/bleve v1.0.13 github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd - github.com/davecgh/go-spew v1.1.1 github.com/dgraph-io/badger/v3 v3.0.0-20210209201111-6c35ad6c28e0 github.com/dgraph-io/dgo/v200 v200.0.0-20210212152539-e0a5bde40ba2 github.com/dgraph-io/gqlgen v0.13.2 @@ -72,5 +71,4 @@ require ( gopkg.in/DataDog/dd-trace-go.v1 v1.13.1 // indirect gopkg.in/square/go-jose.v2 v2.3.1 gopkg.in/yaml.v2 v2.2.4 - src.techknowlogick.com/xgo v1.0.1-0.20200714173810-96de19c19b14 ) diff --git a/go.sum b/go.sum index dc71951ac5d..195532d1645 100644 --- a/go.sum +++ b/go.sum @@ -893,5 +893,3 @@ rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8 rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= sourcegraph.com/sourcegraph/appdash v0.0.0-20180110180208-2cc67fd64755/go.mod h1:hI742Nqp5OhwiqlzhgfbWU4mW4yO10fP+LoT9WOswdU= sourcegraph.com/sourcegraph/appdash-data v0.0.0-20151005221446-73f23eafcf67/go.mod h1:L5q+DGLGOQFpo1snNEkLOJT2d1YTW66rWNzatr3He1k= -src.techknowlogick.com/xgo v1.0.1-0.20200714173810-96de19c19b14 h1:e8P02bjvCTmi33s1MoinoTwMwrCq7+omCS3/3+GZWXA= -src.techknowlogick.com/xgo v1.0.1-0.20200714173810-96de19c19b14/go.mod h1:31CE1YKtDOrKTk9PSnjTpe6YbO6W/0LTYZ1VskL09oU=