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

New upsert block #3412

Merged
merged 7 commits into from
Jun 20, 2019
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
21 changes: 21 additions & 0 deletions chunker/json/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"strconv"
"strings"
"unicode"

"github.com/dgraph-io/dgo/protos/api"
"github.com/dgraph-io/dgraph/query"
Expand All @@ -33,6 +34,16 @@ import (
"github.com/twpayne/go-geom/encoding/geojson"
)

func stripSpaces(str string) string {
return strings.Map(func(r rune) rune {
if unicode.IsSpace(r) {
return -1
}

return r
}, str)
}

func parseFacets(m map[string]interface{}, prefix string) ([]*api.Facet, error) {
// This happens at root.
if prefix == "" {
Expand Down Expand Up @@ -134,6 +145,13 @@ func handleBasicType(k string, v interface{}, op int, nq *api.NQuad) error {
return nil
}

// Handle the uid function in upsert block
s := stripSpaces(v)
if strings.HasPrefix(s, "uid(") {
nq.ObjectId = s
return nil
}

// In RDF, we assume everything is default (types.DefaultID), but in JSON we assume string
// (StringID). But this value will be checked against the schema so we don't overshadow a
// password value (types.PasswordID) - Issue#2623
Expand Down Expand Up @@ -222,10 +240,13 @@ func mapToNquads(m map[string]interface{}, idx *int, op int, parentPred string)
uid = uint64(ui)

case string:
s := stripSpaces(uidVal)
if len(uidVal) == 0 {
uid = 0
} else if ok := strings.HasPrefix(uidVal, "_:"); ok {
mr.uid = uidVal
} else if ok := strings.HasPrefix(s, "uid("); ok {
mr.uid = s
} else if u, err := strconv.ParseUint(uidVal, 0, 64); err == nil {
uid = u
} else {
Expand Down
46 changes: 37 additions & 9 deletions chunker/rdf/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,17 @@ L:
case itemSubject:
rnq.Subject = strings.Trim(item.Val, " ")

case itemVarKeyword:
it.Next()
if item = it.Item(); item.Typ != itemLeftRound {
return rnq, errors.Errorf("Expected '(', found: %s", item.Val)
}
it.Next()
if item = it.Item(); item.Typ != itemVarName {
return rnq, errors.Errorf("Expected variable name, found: %s", item.Val)
case itemSubjectFunc:
var err error
if rnq.Subject, err = parseFunction(it); err != nil {
return rnq, err
}

it.Next() // parse ')'
case itemObjectFunc:
var err error
if rnq.ObjectId, err = parseFunction(it); err != nil {
return rnq, err
}

case itemPredicate:
// Here we split predicate and lang directive (ex: "name@en"), if needed.
Expand Down Expand Up @@ -202,6 +202,34 @@ L:
return rnq, nil
}

// parseFunction parses uid(<var name>) and returns
// uid(<var name>) after striping whitespace if any
func parseFunction(it *lex.ItemIterator) (string, error) {
item := it.Item()
s := item.Val

it.Next()
if item = it.Item(); item.Typ != itemLeftRound {
return "", errors.Errorf("Expected '(', found: %s", item.Val)
}

it.Next()
if item = it.Item(); item.Typ != itemVarName {
return "", errors.Errorf("Expected variable name, found: %s", item.Val)
}
if strings.TrimSpace(item.Val) == "" {
return "", errors.Errorf("Empty variable name in function call")
}
s += "(" + item.Val + ")"

it.Next()
if item = it.Item(); item.Typ != itemRightRound {
return "", errors.Errorf("Expected ')', found: %s", item.Val)
}

return s, nil
}

func parseFacets(it *lex.ItemIterator, rnq *api.NQuad) error {
if !it.Next() {
return errors.Errorf("Unexpected end of facets.")
Expand Down
73 changes: 73 additions & 0 deletions chunker/rdf/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,79 @@ var testNQuads = []struct {
input: `<alice> <age> "13"^^<xs:double> (salary=NaN) .`,
expectedErr: true,
},
{
input: `uid(v) <lives> "\x02 wonderland" .`,
nq: api.NQuad{
Subject: "uid(v)",
Predicate: "lives",
ObjectValue: &api.Value{Val: &api.Value_DefaultVal{DefaultVal: "\x02 wonderland"}},
},
expectedErr: false,
},
{
input: `uid ( v ) <lives> "vrinadavan" .`,
nq: api.NQuad{
Subject: "uid(v)",
Predicate: "lives",
ObjectValue: &api.Value{Val: &api.Value_DefaultVal{DefaultVal: "vrinadavan"}},
},
expectedErr: false,
},
{
input: `uid ( val ) <lives> "vrinadavan" .`,
nq: api.NQuad{
Subject: "uid(val)",
Predicate: "lives",
ObjectValue: &api.Value{Val: &api.Value_DefaultVal{DefaultVal: "vrinadavan"}},
},
expectedErr: false,
},
{
input: `uid ( val ) <lives> uid(g) .`,
nq: api.NQuad{
Subject: "uid(val)",
Predicate: "lives",
ObjectId: "uid(g)",
},
expectedErr: false,
},
{
input: `uid ( val ) <lives> uid ( g ) .`,
nq: api.NQuad{
Subject: "uid(val)",
Predicate: "lives",
ObjectId: "uid(g)",
},
expectedErr: false,
},
{
input: `uid ( val <lives> uid ( g ) .`,
expectedErr: true,
},
{
input: `uid val ) <lives> uid ( g ) .`,
expectedErr: true,
},
{
input: `ui(uid) <lives> uid ( g ) .`,
expectedErr: true,
},
{
input: `uid()) <lives> uid ( g ) .`,
expectedErr: true,
},
{
input: `uid() <lives> uid ( g ) .`,
expectedErr: true,
},
{
input: `uid(a) <lives> uid ( ) .`,
expectedErr: true,
},
{
input: `uid(a) lives> uid ( ) .`,
expectedErr: true,
},
}

func TestLex(t *testing.T) {
Expand Down
71 changes: 36 additions & 35 deletions chunker/rdf/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,24 @@ import (

// The constants represent different types of lexed Items possible for an rdf N-Quad.
const (
itemText lex.ItemType = 5 + iota // plain text
itemSubject // subject, 6
itemPredicate // predicate, 7
itemObject // object, 8
itemLabel // label, 9
itemLiteral // literal, 10
itemLanguage // language, 11
itemObjectType // object type, 12
itemValidEnd // end with dot, 13
itemComment // comment, 14
itemComma // comma, 15
itemEqual // equal, 16
itemLeftRound // '(', 17
itemRightRound // ')', 18
itemStar // *, 19
itemVarKeyword // var, 20
itemVarName // 21
itemText lex.ItemType = 5 + iota // plain text
itemSubject // subject, 6
itemPredicate // predicate, 7
itemObject // object, 8
itemLabel // label, 9
itemLiteral // literal, 10
itemLanguage // language, 11
itemObjectType // object type, 12
itemValidEnd // end with dot, 13
itemComment // comment, 14
itemComma // comma, 15
itemEqual // equal, 16
itemLeftRound // '(', 17
itemRightRound // ')', 18
itemStar // *, 19
itemSubjectFunc // uid, 20
itemObjectFunc // uid, 21
itemVarName // 22
)

// These constants keep a track of the depth while parsing an rdf N-Quad.
Expand Down Expand Up @@ -137,6 +138,7 @@ func lexText(l *lex.Lexer) lex.StateFn {
l.Depth = atSubject
}

// TODO(Aman): add support for more functions here.
case r == 'u':
if l.Depth != atSubject && l.Depth != atObject {
return l.Errorf("Unexpected char 'u'")
Expand Down Expand Up @@ -422,40 +424,39 @@ func lexComment(l *lex.Lexer) lex.StateFn {
func lexVariable(l *lex.Lexer) lex.StateFn {
var r rune

// TODO(Aman): add support for more functions here.
for _, c := range "uid" {
if r = l.Next(); r != c {
return l.Errorf("Unexpected char '%c' when parsing var keyword", r)
return l.Errorf("Unexpected char '%c' when parsing uid keyword", r)
}
}
l.Emit(itemVarKeyword)
if l.Depth == atObject {
l.Emit(itemObjectFunc)
} else if l.Depth == atSubject {
l.Emit(itemSubjectFunc)
}
l.IgnoreRun(isSpace)

if r = l.Next(); r != '(' {
return l.Errorf("Expected '(' after var keyword, found: '%c'", r)
return l.Errorf("Expected '(' after uid keyword, found: '%c'", r)
}
l.Emit(itemLeftRound)

l.IgnoreRun(isSpace)

for {
r := l.Next()
if r == lex.EOF {
return l.Errorf("Unexpected end of input while reading variable name.")
}
if r == ')' {
l.Backup()
break
}
if isSpace(r) {
break
}
// TODO(Aman): we support all characters in variable names except space and
// right bracket. we should support only limited characters in variable names.
// For now, this is fine because variables names must be used once in query
// block before they can be used here. And, we throw an error if number of
// used variables are different than number of defined variables.
acceptVar := func(r rune) bool { return !(isSpace(r) || r == ')') }
if _, valid := l.AcceptRun(acceptVar); !valid {
return l.Errorf("Unexpected end of input while reading variable name")
}
l.Emit(itemVarName)

l.IgnoreRun(isSpace)

if r = l.Next(); r != ')' {
return l.Errorf("Expected ')' while reading var, found: '%c'", r)
return l.Errorf("Expected ')' while reading uid func, found: '%c'", r)
}
l.Emit(itemRightRound)
l.Depth++
Expand Down
7 changes: 7 additions & 0 deletions dgraph/cmd/alpha/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,13 @@ func mutationHandler(w http.ResponseWriter, r *http.Request) {
if delJSON, ok := ms["delete"]; ok && delJSON != nil {
mu.DeleteJson = delJSON.bs
}
if queryText, ok := ms["query"]; ok && queryText != nil {
mu.Query, err = strconv.Unquote(string(queryText.bs))
if err != nil {
x.SetStatus(w, x.ErrorInvalidRequest, err.Error())
return
}
}

case "application/rdf":
// Parse N-Quads.
Expand Down
Loading