Sorting does not sort variables and first does not paginate properly

Moved from GitHub dgraph/4794

Posted by MiLeung:

What version of Dgraph are you using?

{“version”:“v1.2.0”,“instance”:“alpha”,“uptime”:191733}

Have you tried reproducing the issue with the latest release?

yes

What is the hardware spec (RAM, OS)?

8GB ram macOS 10.15.3

Steps to reproduce the issue (command/config used to run Dgraph).

Expected behaviour and actual result.

In the demo video above, you first see the results for all Lesson nodes with their total likes. When sorting the lessons by their total likes, only 3 lessons are returned even though in the beginning, there are 5 lessons total. Also, the lessons are not sorted as the one with the most likes (1 like) is missing. Changing the first pagination value in the query returns less than what is specified when the first pagination value is 5.

hackintoshrao commented :

Hey @MiLeung ,

Thanks for reporting the issue, and sharing the video, this makes it easy to for us to test.
We’ll get back after a quick evaluation of the reported issue.

arijitAD commented :

Can you share the Dgraph schema?

arijitAD commented :

https://dgraph.io/docs/query-language/#query-variables
From docs.

Value variables store scalar values. Value variables are a map from the UIDs of the enclosing block to the corresponding values. It therefore only makes sense to use the values from a value variable in a context that matches the same UIDs - if used in a block matching different UIDs the value variable is undefined.

The query is fixed by replacing func: uid(totalLikes) in query.
The following seems to work.
Data:

{
"set": [
    {
        "uid": "_:lesson1",
        "dgraph.type": "Lesson",
        "Lesson.reviews": [{ "uid": "_:review1" }, { "uid": "_:review2" }]
    },
    {
        "uid": "_:review1",
        "dgraph.type": "Review",
        "Lesson.liked": true
    },
    {
        "uid": "_:review2",
        "dgraph.type": "Review",
        "Lesson.liked": true
    },
    {
        "uid": "_:lesson2",
        "dgraph.type": "Lesson",
        "Lesson.reviews": [{ "uid": "_:review3" }, { "uid": "_:review4" }]
    },
    {
        "uid": "_:review3",
        "dgraph.type": "Review",
        "Lesson.liked": true
    },
    {
        "uid": "_:review4",
        "dgraph.type": "Review",
        "Lesson.liked": false
    },
    {
        "uid": "_:lesson3",
        "dgraph.type": "Lesson",
        "Lesson.reviews": [{ "uid": "_:review5" }]
    },
    {
        "uid": "_:review5",
        "dgraph.type": "Review",
        "Lesson.liked": false
    },
    {
        "uid": "_:lesson4",
        "dgraph.type": "Lesson"
    },
    {
        "uid": "_:lesson5",
        "dgraph.type": "Lesson"
    }
    ]
}

and I have used this query.

{
  var(func: type(Lesson)) {
    uid
    Lesson.reviews @filter(eq(Lesson.liked, true)) {
      uid
    }
    totalLikes as count(Lesson.reviews)
  }

  q(func: uid(totalLikes), orderdesc: val(totalLikes)) {
    uid
    totalLikes: val(totalLikes)
  }
}