Skip to content

Facet variables don't scope correctly #5044

@ppp225

Description

@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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions