Why do the variables in these synonym(I think) queries return two different results?

Hello,
I have two versions of a query, and I feel like they should return the same result. They don’t – and I hope someone can explain why.

query version 1:

    {
        var(func: eq(xids, "test@email.com")) {
            A as ~audit.entity {
                expand(_all_)
            }
        } 
        
        res(func: eq(xids, "test@email.com")) {
            xids
            phones @facets(confidence) {
                phone.number
                phone.ext
                count(~audit.attribute @filter(uid(A)))
            }
        }
    }

result:

{'res': [{'phones': [{'count(~audit.attribute)': 3,
                      'phone.ext': 456,
                      'phone.number': '123-555-5555',
                      'phones|confidence': 85}],
          'xids': ['03D42343FMTDFM', 'test@email.com']}]}

query version 2:

    {
        res(func: eq(xids, "test@email.com")) {
            xids
            A as ~audit.entity
            phones @facets(confidence) {
                phone.number
                phone.ext
                count(~audit.attribute @filter(uid(A)))
            }
        }
    }

result:

{'res': [{'phones': [{'count(~audit.attribute)': 0,
                      'phone.ext': 456,
                      'phone.number': '123-555-5555',
                      'phones|confidence': 85}],
          'xids': ['03D42343FMTDFM', 'test@email.com']}]}

Notice 'count(~audit.attribute)' in the first result is 3, but in the second it’s 0. They use the exact same node selection statement, shouldn’t they return the same node list for ~audit.entity?

The goal of this query is as follows:
“Get me all of B that points to A then the count of C that points to B and also points to A”

I’m very much new to dgraph, so if there’s a way to do this more efficiently, I’d love to hear it. But either way, I’m interested in why these two queries give different results.

Thanks!

I believe it is cus you have not expanded audit.entity - Try:

{
        res(func: eq(xids, "test@email.com")) {
            xids
            A as ~audit.entity {
                expand(_all_)
            }
            phones @facets(confidence) {
                phone.number
                phone.ext
                count(~audit.attribute @filter(uid(A)))
            }
        }
    }

Hi, when I try that, it shows me the result of that predicate, but the count is still 0

{'res': [{'phones': [{'count(~audit.attribute)': 0,
                      'phone.ext': 456,
                      'phone.number': '123-555-5555',
                      'phones|confidence': 85}],
          'xids': ['03D42343FMTDFM', 'test@email.com'],
          '~audit.entity': [{'created_at': '2018-12-13T12:00:00Z'},
                            {'created_at': '2018-12-13T12:00:00Z'},
                            {'created_at': '2018-12-13T12:00:00Z'}]}]}

Which makes it even more strange, because I can now clearly see there are 3 nodes to count…

I also tested it the other way. I tried this version where I use the var block but don’t expand it

    {
        var(func: eq(xids, "test@email.com")) {
            A as ~audit.entity
        } 
        
        res(func: eq(xids, "test@email.com")) {
            xids
            phones @facets(confidence) {
                phone.number
                phone.ext
                count(~audit.attribute @filter(uid(A)))
            }
        }
    }

The result is exactly the same, with the proper count:

{'res': [{'phones': [{'count(~audit.attribute)': 3,
                      'phone.ext': 456,
                      'phone.number': '123-555-5555',
                      'phones|confidence': 85}],
          'xids': ['03D42343FMTDFM', 'test@email.com']}]}

Wait, audit.entity is related to “test@email.com” or phones of “test@email.com” - This seems diff levels.

Ok, so i’m adding a better example of the issue in one query and with much more context:

    {
        tmp(func: eq(xids, "test@email.com")) {
            uids_in_B: B as ~audit.entity {
                uid
            }
        } 

        res(func: eq(xids, "test@email.com")) {
            uid
            ids
            uids_in_A: A as ~audit.entity {
                uid
            }
            phones @facets(confidence) {
                uid
                phone.number
                phone.ext
                count_of_A_with_filter: count(~audit.attribute @filter(uid(A)))
                count_of_B_with_filter: count(~audit.attribute @filter(uid(B)))
                count_no_filter: count(~audit.attribute)
                uids_in_count_before_filter: ~audit.attribute {
                    uid
                }
            }
        }
    }

Result:

{'res': [{'phones': [{'count_no_filter': 4,
                      'count_of_A_with_filter': 0,
                      'count_of_B_with_filter': 3,
                      'phone.ext': 456,
                      'phone.number': '123-555-5555',
                      'phones|confidence': 85,
                      'uid': '0xc4f0',
                      'uids_in_count_before_filter': [{'uid': '0xc4f1'},
                                                      {'uid': '0xc4f2'},
                                                      {'uid': '0xc4f5'},
                                                      {'uid': '0xc4f6'}]}],
          'uid': '0xc4ef',
          'uids_in_A': [{'uid': '0xc4f1'},
                        {'uid': '0xc4f2'},
                        {'uid': '0xc4f5'}]}],
 'tmp': [{'uids_in_B': [{'uid': '0xc4f1'},
                        {'uid': '0xc4f2'},
                        {'uid': '0xc4f5'}]}]}

So the question is, why is count_of_A_with_filter different than count_of_B_with_filter

Yes, it is different levels, but those levels “point” to the same set of nodes, which is what I’m attempting to count. Sorry, i’m not very familiar with the terminology yet.

A -(phones)-> B -(~audit.attribute)-> C -(audit.entity)-> A

I’m trying to get the count of C, that have edges with B and A, as a predicate of B

Okay, It seems should work indeed. But I’m not sure. I’ll ask @gus for solve this doubt; But I feel it don’t for some reason.

This looks like it could be a bug. Which version are you using? Do mind testing this using master? Thanks

I was running 1.0.8 (i’m using docker-compose, which pulled latest at the time), but I have now run it using master and I get the same result.