Facet variables don't scope correctly

Moved from GitHub dgraph/5044

Posted by ppp225:

What version of Dgraph are you using?

v20.03.0-beta.20200320, v2.0.0-rc1

Have you tried reproducing the issue with the latest release?

yeah

What is the hardware spec (RAM, OS)?

linux, windows, 32G

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

schema

type Car {
	carName: string
	carStat: [Stat]
}
carName: string @index(hash) .
carStat: [uid] @reverse .

type Stat {
	statName: string
}
statName: string @index(hash) .

mutation

{
set {
<_:Ferrari> <carName> "Ferrari" .
<_:Porsche> <carName> "Porsche" .
<_:Speed> <statName> "Speed" .
<_:Handling> <statName> "Handling" .

<_:Ferrari> <carStat> <_:Speed>(statValue=1.0) .
<_:Ferrari> <carStat> <_:Handling>(statValue=0.6) .
<_:Porsche> <carStat> <_:Speed>(statValue=0.7) .
<_:Porsche> <carStat> <_:Handling>(statValue=0.9) .

<_:Ferrari> <dgraph.type> "Car" .
<_:Porsche> <dgraph.type> "Car" .
<_:Speed> <dgraph.type> "Stat" .
<_:Handling> <dgraph.type> "Stat" .
}
}

query

query {
  cars(func: Type(Car)) {
    carName
    carStat @facets(SV as statValue) {
      statName
      statValue: val(SV)
    }
    min: min(val(SV))
    max: max(val(SV))
    avg: avg(val(SV))
  }
  stats(func: Type(Stat)) {
    statName
    ~carStat @facets(SV2 as statValue) {
    	carName
        statValue2: val(SV2)
    }
    totalForStat: sum(val(SV2))
  }
}

Expected behaviour and actual result.

Below is the result. i.e. Ferrari has 2 edges, with a facet of “1.0” and “0.6”, but it aggregates values across all cars, which are outside of scope.

"data": {
    "cars": [
      {
        "carName": "Ferrari",
        "carStat": [
          {
            "statName": "Speed",
            "statValue": 1.7       # this should be 1.0
          },
          {
            "statName": "Handling",
            "statValue": 1.5       # this should be 0.6
          }
        ],
        "carStat|statValue": {
          "0": 1,                  # above values should be like those facets...
          "1": 0.6
        },
        "min": 1.5,                # ...so I can use the SV variable in my own calculations
        "max": 1.7,
        "avg": 1.6
      },
      {
        "carName": "Porsche",
        "carStat": [
          {
            "statName": "Speed",
            "statValue": 1.7        # same story as above
          },
          {
            "statName": "Handling",
            "statValue": 1.5
          }
        ],
        "carStat|statValue": {
          "0": 0.7,
          "1": 0.9
        },
        "min": 1.5,
        "max": 1.7,
        "avg": 1.6
      }
    ],
    "stats": [
      {
        "statName": "Speed",
        "~carStat": [
          {
            "carName": "Ferrari",
            "statValue2": 1.6
          },
          {
            "carName": "Porsche",
            "statValue2": 1.6
          }
        ],
        "~carStat|statValue": {
          "0": 1,
          "1": 0.7
        },
        "totalForStat": 3.2        # I could aggregate values myself, if required, currently value is incorrect, should be 1.7
      },
      {
        "statName": "Handling",
        "~carStat": [
          {
            "carName": "Ferrari",
            "statValue2": 1.6
          },
          {
            "carName": "Porsche",
            "statValue2": 1.6
          }
        ],
        "~carStat|statValue": {
          "0": 0.6,
          "1": 0.9
        },
        "totalForStat": 3.2        # should be 1.5
      }
    ]
  }

Additional info

If variables would work correctly, it would also be a workaround for #4907

MichelDiz commented :

This is a duplicated of Sum over facets is incorrect · Issue #4160 · dgraph-io/dgraph · GitHub

And I sure that this isn’t related to #4907 @ppp225

Cheers.

ppp225 commented :

Indeed, it looks like the underlying cause is the same as 4160. Feel free to close this in this case.

Regarding 4907: previously, the facet response for a query like this:

carStat @facets(statValue: statValue) {
    	carName
}

Would be:

carStat": [
{
  "carName": "Porsche",
  "statValue": 0.7
}
]

Currently it is:

carStat": [
  {
    "carName": "Porsche",
  }
],
"statValue": {
  "0": 0.7
},

But one could use a query like this:

carStat @facets(SV as statValue) {
    	carName
        statValue: val(SV)
    }

To get:

carStat": [
  {
    "carName": "Porsche",
    "statValue": 0.7,
  }
],
"carStat|statValue": {
  "0": 0.7
},

That’s why I said, it could be used as a workaround.