Skip to content

feat(Query): Support variables inside uid_in function #5320

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

Merged
merged 40 commits into from
Jul 14, 2020

Conversation

all-seeing-code
Copy link
Contributor

@all-seeing-code all-seeing-code commented Apr 28, 2020

Motivation

This PR adds support for queries which use variable inside uid_in function:

{
  getJeunet as q(func: eq(name@fr, "Jean-Pierre Jeunet")) 
  caro(func: eq(name@en, "Marc Caro")) {
    name@en
    director.film @filter(uid_in(~director.film, uid(getJeunet) )) {
      name@en
    }
  }
}

Components affected by this PR

Nested uid_in function calls.

Does this PR break backwards compatibility?

No.

Testing

Added following tests in query1_test.go:

  1. Extended uid_in tests to work with variables
  2. Added TestUidInWithParseErrors to check that program gracefully returns error in case of malformed input and does not crash

Fixes

Fixes DGRAPH-549
Fixes #3066


This change is Reviewable

Docs Preview: Dgraph Preview

@all-seeing-code all-seeing-code requested a review from a team April 28, 2020 13:41
@all-seeing-code all-seeing-code changed the title Anurags92/nested uid fn Add nested uid fn Apr 28, 2020
@all-seeing-code all-seeing-code linked an issue Apr 30, 2020 that may be closed by this pull request
Copy link
Contributor Author

@all-seeing-code all-seeing-code left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MichelDiz No, currently it will only support one variable inside uid function. I have added tests (TestUidInWithErrors) specifically which checks the behavior which you asked for and throw an error. I have added a TODO to look into this later.

@jarifibrahim Added tests and description.

Reviewable status: 0 of 3 files reviewed, 3 unresolved discussions (waiting on @manishrjain, @martinmr, @parasssh, @pawanrawal, and @vvbalaji-dgraph)


gql/parser.go, line 1728 at r1 (raw file):

Previously, martinmr (Martin Martinez Rivera) wrote…

needs test at the parser level as well.

Yes, have added more tests and edge case handling to prevent panics.


query/query.go, line 1658 at r1 (raw file):

Previously, martinmr (Martin Martinez Rivera) wrote…

I think you can remove this special logic and simplify. Simply create a new array to store the args and set sg.SrcFunc.Args to point to that array after the for loop. That way the first item doesn't have to be handled differently.

Done.


query/query.go, line 1659 at r1 (raw file):

Previously, parasssh wrote…

add comment about the base 10.

Done.

Copy link
Contributor

@parasssh parasssh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you estimate how much additional work is it for the request by @MichelDiz above? Also, run this by Pawan. Add / Move UT to the parser_test.go.

Reviewable status: 0 of 4 files reviewed, 3 unresolved discussions (waiting on @manishrjain, @martinmr, @parasssh, @pawanrawal, and @vvbalaji-dgraph)

Copy link
Contributor

@martinmr martinmr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine to do what @MichelDiz suggested but it probably should be done in another PR.

I added some comments but they are all related to the tests. Once those are fixed it :lgtm: .

Reviewed 4 of 4 files at r2.
Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @anurags92, @manishrjain, @parasssh, @pawanrawal, and @vvbalaji-dgraph)


query/query1_test.go, line 1142 at r2 (raw file):

		},
		{
			description: "query with uid variable where school uid for <31> is <5001> and <25> is <5000>",

the description should be more general. Don't mention specific uids. It should say what general case is being tested.
Same for the other tests that contain specific numbers.

The descriptions of the tests in TestUidInWithErrors are good examples.


query/query1_test.go, line 1179 at r2 (raw file):

	}
}
func TestUidInWithErrors(t *testing.T) {

All these are parser tests so they should be moved there.

Also, the parser test should include tests to verify valid queries.

Copy link
Contributor Author

@all-seeing-code all-seeing-code left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @manishrjain, @martinmr, @parasssh, @pawanrawal, and @vvbalaji-dgraph)


query/query1_test.go, line 1142 at r2 (raw file):

Previously, martinmr (Martin Martinez Rivera) wrote…

the description should be more general. Don't mention specific uids. It should say what general case is being tested.
Same for the other tests that contain specific numbers.

The descriptions of the tests in TestUidInWithErrors are good examples.

Done.


query/query1_test.go, line 1179 at r2 (raw file):

Previously, martinmr (Martin Martinez Rivera) wrote…

All these are parser tests so they should be moved there.

Also, the parser test should include tests to verify valid queries.

Moved the TestUidInWithErrors to parser_test.go
For the second point -- wouldn't the working queries be sufficient?

Copy link
Contributor Author

@all-seeing-code all-seeing-code left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @manishrjain, @martinmr, @parasssh, @pawanrawal, and @vvbalaji-dgraph)

Copy link
Contributor

@pawanrawal pawanrawal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 2 of 4 files at r2, 2 of 2 files at r3.
Reviewable status: all files reviewed, 9 unresolved discussions (waiting on @anurags92, @manishrjain, @martinmr, @parasssh, and @vvbalaji-dgraph)


gql/parser_test.go, line 4920 at r3 (raw file):

			query: `{
				me(func: uid(1, 23, 24 )) {
					friend @filter(uid_in(school, uid(5000))) {

Its a bit weird that we don't support this case as it's the same as

friend @filter(uid_in(school, 5000))

We support passing a constant uid using the uid function at other places.
Should we support it?


query/query.go, line 1659 at r3 (raw file):

			srcFuncArgs := sg.SrcFunc.Args[:0]
			if l.Uids != nil {
				for _, uid := range l.Uids.Uids {

Could you just range over using l.Uids.GetUids() without checking l.Uids != nil as it takes care of nil


query/query.go, line 1663 at r3 (raw file):

					arg := gql.Arg{
						Value:        strconv.FormatUint(uid, 10),
						IsValueVar:   false,

Don't need to pass in false values, they are the default.


query/query1_test.go, line 1061 at r3 (raw file):

			description: "query at top level with nested UID variable",
			query: `{
				uidVar as q(func: uid(5001, 5000))

This can be a var query so that its not returned in the results.


query/query1_test.go, line 1142 at r3 (raw file):

		},
		{
			description: "query inside root with nested uid variable uid variable which resolves to two uids",

extra uid variable can be removed


query/query1_test.go, line 1192 at r3 (raw file):

		}
	}`
	expectedErr := errors.New("rpc error: code = Unknown desc = : Function 'uid_in' requires atleast 1 argument, but got 0 ([])")

hmm, this shouldn't be a runtime error, if the uid variable returns a null set we should just consider the intersection to be []. It would be weird if for the user some of the queries failed and some passed depending on which node they were starting with.

Copy link
Contributor Author

@all-seeing-code all-seeing-code left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: all files reviewed, 9 unresolved discussions (waiting on @manishrjain, @martinmr, @parasssh, @pawanrawal, and @vvbalaji-dgraph)


gql/parser_test.go, line 4920 at r3 (raw file):

Previously, pawanrawal (Pawan Rawal) wrote…

Its a bit weird that we don't support this case as it's the same as

friend @filter(uid_in(school, 5000))

We support passing a constant uid using the uid function at other places.
Should we support it?

Will create a JIRA to extend support.


query/query.go, line 1659 at r3 (raw file):

Previously, pawanrawal (Pawan Rawal) wrote…

Could you just range over using l.Uids.GetUids() without checking l.Uids != nil as it takes care of nil

Okay, thanks. Didnt know that.


query/query.go, line 1663 at r3 (raw file):

Previously, pawanrawal (Pawan Rawal) wrote…

Don't need to pass in false values, they are the default.

Cool, makes sense.


query/query1_test.go, line 1061 at r3 (raw file):

Previously, pawanrawal (Pawan Rawal) wrote…

This can be a var query so that its not returned in the results.

Yes, thanks for pointing this out.


query/query1_test.go, line 1142 at r3 (raw file):

Previously, pawanrawal (Pawan Rawal) wrote…

extra uid variable can be removed

Ok.


query/query1_test.go, line 1192 at r3 (raw file):

Previously, pawanrawal (Pawan Rawal) wrote…

hmm, this shouldn't be a runtime error, if the uid variable returns a null set we should just consider the intersection to be []. It would be weird if for the user some of the queries failed and some passed depending on which node they were starting with.

Agree, need to be changed.

Copy link
Contributor Author

@all-seeing-code all-seeing-code left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 5 files reviewed, 11 unresolved discussions (waiting on @jarifibrahim, @manishrjain, @martinmr, @parasssh, @pawanrawal, and @vvbalaji-dgraph)


gql/parser.go, line 1745 at r4 (raw file):

					function.Args = append(function.Args, Arg{Value: nestedFunc.NeedsVar[0].Name})
				default:
					return nil, itemInFunc.Errorf("Only val/count/len/uid allowed as function "+

Created a JIRA ticket since this change might allow nested uid inside other functions too. https://dgraph.atlassian.net/browse/DGRAPH-1918


gql/parser_test.go, line 4920 at r3 (raw file):

friend @filter(uid_in(school, 5000))
This JIRA ticket should be able to handle the above requirement. https://dgraph.atlassian.net/browse/DGRAPH-1919


gql/parser_test.go, line 4349 at r4 (raw file):

}

func TestEqUidFunctionErr(t *testing.T) {

This JIRA ticket should investigate this: https://dgraph.atlassian.net/browse/DGRAPH-1918


query/query.go, line 1659 at r3 (raw file):

Previously, anurags92 (Anurag) wrote…

Okay, thanks. Didnt know that.

Done.Done


query/query.go, line 1663 at r3 (raw file):

Previously, anurags92 (Anurag) wrote…

Cool, makes sense.

Done.


query/query1_test.go, line 1061 at r3 (raw file):

Previously, anurags92 (Anurag) wrote…

Yes, thanks for pointing this out.

Done.


query/query1_test.go, line 1192 at r3 (raw file):

Previously, anurags92 (Anurag) wrote…

Agree, need to be changed.

Done.

Copy link
Contributor

@pawanrawal pawanrawal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewed 1 of 4 files at r4, 4 of 4 files at r5.
Reviewable status: all files reviewed, 7 unresolved discussions (waiting on @jarifibrahim, @manishrjain, @martinmr, @parasssh, @pawanrawal, and @vvbalaji-dgraph)

Copy link
Contributor Author

@all-seeing-code all-seeing-code left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 3 of 5 files reviewed, 5 unresolved discussions (waiting on @jarifibrahim, @manishrjain, @martinmr, @parasssh, @pawanrawal, and @vvbalaji-dgraph)


gql/parser_test.go, line 4920 at r3 (raw file):

Previously, anurags92 (Anurag) wrote…

friend @filter(uid_in(school, 5000))
This JIRA ticket should be able to handle the above requirement. https://dgraph.atlassian.net/browse/DGRAPH-1919

Done.


gql/parser_test.go, line 4349 at r4 (raw file):

Previously, anurags92 (Anurag) wrote…

This JIRA ticket should investigate this: https://dgraph.atlassian.net/browse/DGRAPH-1918

Done.


query/query1_test.go, line 1179 at r2 (raw file):

Previously, anurags92 (Anurag) wrote…

Moved the TestUidInWithErrors to parser_test.go
For the second point -- wouldn't the working queries be sufficient?

Done.

@all-seeing-code all-seeing-code merged commit e9c3f05 into master Jul 14, 2020
@all-seeing-code all-seeing-code deleted the anurags92/NestedUIDFn branch July 27, 2020 18:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Add support of Value Variables for uid_in.
6 participants