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

fix unique query for non-latin predicate name and whitespace in query #9201

Merged
merged 1 commit into from
Oct 17, 2024
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
14 changes: 9 additions & 5 deletions edgraph/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1734,12 +1734,16 @@ func addQueryIfUnique(qctx context.Context, qc *queryContext) error {
continue
}
}
var predicateName string

// Wrapping predicateName with angle brackets ensures that if the predicate contains any non-Latin letters,
// the unique query will not fail. Additionally,
// it helps ensure that non-Latin predicate names are properly formatted
// during the automatic serialization of a structure into JSON.
predicateName := fmt.Sprintf("<%v>", pred.Predicate)
if pred.Lang != "" {
predicateName = fmt.Sprintf(pred.Predicate+"@%v", pred.Lang)
} else {
predicateName = pred.Predicate
predicateName = fmt.Sprintf("%v@%v", predicateName, pred.Lang)
}

uniqueVarMapKey := encodeIndex(gmuIndex, rdfIndex)
queryVar := fmt.Sprintf("__dgraph_uniquecheck_%v__", uniqueVarMapKey)
// Now, we add a query for a predicate to check if the value of the
Expand Down Expand Up @@ -1790,7 +1794,7 @@ func addQueryIfUnique(qctx context.Context, qc *queryContext) error {
if qc.req.Query == "" {
qc.req.Query = "{" + buildQuery.String()
} else {
qc.req.Query = strings.TrimRight(qc.req.Query, "}")
qc.req.Query = strings.TrimSuffix(qc.req.Query, "}")
qc.req.Query = qc.req.Query + buildQuery.String()
}
}
Expand Down
60 changes: 60 additions & 0 deletions systest/unique_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -759,3 +759,63 @@ func TestConcurrency2(t *testing.T) {
}
}
}

func TestUniqueUpsertWithoutSpaceInQuery(t *testing.T) {
schema := `email: string @unique @index(exact) .`
dg := setUpDgraph(t)
require.NoError(t, dg.SetupSchema(schema))
rdf := `_:a <email> "[email protected]" .`
query := `{UNIQS as var(func: eq(userEmail, "[email protected]")) { count: count(uid) }}`

r := &api.Request{
Query: query,
Mutations: []*api.Mutation{
{
Cond: "@if(eq(len(UNIQS), 0))",
SetNquads: []byte(rdf),
},
},
CommitNow: true,
}

ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

txn := dg.NewTxn()
_, err := txn.Do(ctx, r)
require.NoError(t, err)

txn = dg.NewTxn()
_, err = txn.Do(ctx, r)

require.Error(t, err)
}

func TestUniqueWithNonLatinPredName(t *testing.T) {
dg := setUpDgraph(t)
require.NoError(t, dg.SetupSchema(`<ईमेल#$%&>: string @unique @lang @index(exact) .`))

rdf := `_:a <ईमेल#$%&@hi> "[email protected]" . `
_, err := dg.Mutate(&api.Mutation{
SetNquads: []byte(rdf),
CommitNow: true,
})
require.NoError(t, err)
query := `{
q(func: eq(<ईमेल#$%&>@hi, "[email protected]")) {
<ईमेल#$%&>@hi
}
}`
resp, err := dg.Query(query)
require.NoError(t, err)
var data map[string]interface{}
require.NoError(t, json.Unmarshal(resp.Json, &data))
require.Equal(t, data["q"].([]interface{})[0].(map[string]interface{})["ईमेल#$%&@hi"].(string), "[email protected]")

_, err = dg.Mutate(&api.Mutation{
SetNquads: []byte(rdf),
CommitNow: true,
})
require.Error(t, err)
require.ErrorContains(t, err, "could not insert duplicate value [[email protected]] for predicate [ईमेल#$%&]")
}
Loading