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

[BUG]: @unique mutation/query parser is not whitespace insensitive #9102

Open
ppp225 opened this issue Jun 15, 2024 · 1 comment
Open

[BUG]: @unique mutation/query parser is not whitespace insensitive #9102

ppp225 opened this issue Jun 15, 2024 · 1 comment
Labels
kind/bug Something is broken.

Comments

@ppp225
Copy link

ppp225 commented Jun 15, 2024

What version of Dgraph are you using?

v24.0.0, docker image

Tell us a little more about your go-environment?

No response

Have you tried reproducing the issue with the latest release?

None

What is the hardware spec (RAM, CPU, OS)?

not applicable

What steps will reproduce the bug?

apply the following schema

userEmail: string @index(hash) @unique @upsert .

run the mutation through dgo:

package main

import (
	"context"
	"encoding/json"
	"log"
	"time"

	"github.com/dgraph-io/dgo/v230"
	"github.com/dgraph-io/dgo/v230/protos/api"
	"google.golang.org/grpc"
)

type DbUser struct {
	UID   string `json:"uid,omitempty"`
	Email string `json:"userEmail,omitempty"`
}

func main() {
	user := DbUser{Email: "[email protected]"}
	userInsert, _ := json.Marshal(&user)

	q := `{UNIQS as var(func: eq(userEmail, "[email protected]")) { count: count(uid) }}`

	r := &api.Request{
		Query: q,
		Mutations: []*api.Mutation{
			{
				Cond:    "@if(eq(len(UNIQS), 0))",
				SetJson: userInsert,
			},
		},
	}

	gc, err := grpc.Dial("localhost:9080", grpc.WithInsecure())
	if err != nil {
		log.Fatal(err)
	}

	DG := dgo.NewDgraphClient(
		api.NewDgraphClient(gc),
	)

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

	txn := DG.NewTxn()
	_, err = txn.Do(ctx, r)
	if err != nil {
		panic(err)
	}

	txn.Commit(ctx)
}

Expected behavior and actual result.

Mutation fails with error:

rpc error: code = Unknown desc = while lexing {UNIQS as var(func: eq(userEmail, "[email protected]")) { count: count(uid) __dgraph_uniquecheck_0__ as var(func: eq(userEmail,"[email protected]"))__dgraph_upsertcheck_0__ as var(func: uid(0)) @filter(eq(len(UNIQS), 0))
} at line 2 column 1: Unclosed action

I expect it to work.

Additional information

Works without @unique.
Works with @unique, when we modify the query to contain a space before the closing bracket:

	q := `{UNIQS as var(func: eq(userEmail, "[email protected]")) { count: count(uid) }}` // errors
	q := `{UNIQS as var(func: eq(userEmail, "[email protected]")) { count: count(uid) } }` // works
@ppp225 ppp225 added the kind/bug Something is broken. label Jun 15, 2024
@AoAnima
Copy link

AoAnima commented Jun 25, 2024

this is because in the dgraph/edgraph
/server.go
uses a string.trimRight(qc.req.Query, "}") , which should be replaced with qc.req.Query = strings.TrimSuffix(qc.req.Query, "}") so that only one closing "}" is deleted

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Something is broken.
Development

No branches or pull requests

2 participants