Sum should return 0 if no edge exists, also max( <variable>, 0) should work

Moved from GitHub dgraph/4158

Posted by omerose:

What version of Dgraph are you using?

v1.0.17

Have you tried reproducing the issue with the latest release?

On your site. I ran this (and reproduced) on website with the blade runner example

What is the hardware spec (RAM, OS)?

MBP 2017 16GB Ram, running inside docker

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

{
  bladerunner(func: eq(name@en, "Blade Runner")) {
    uid
    name@en
    initial_release_date
    netflix_id
    youDontHaveThisEdge {
     B as count(~provideTag)
    }
    mediaCount: sum(val(B))
  }
}

Expected behaviour and actual result.

Expected behaviour:
mediaCount should be 0

actual results:
Notice that mediaCount is absent from the response

{
  "data": {
    "bladerunner": [
      {
        "uid": "0x394c",
        "name@en": "Blade Runner",
        "initial_release_date": "1982-06-25T00:00:00Z",
        "netflix_id": "70083726",
      }
    ]
  }
}

My current workaround:

{
  bladerunner(func: eq(name@en, "Blade Runner")) {
    uid
    name@en
    initial_release_date
    netflix_id
    youDontHaveThisEdge {
     B as count(~provideTag)
    }
    mediaCount as mediaCount: sum(val(B))
    mathZero as math(0)
    mediaCount2: math( max(mediaCount, mathZero) )
  }
}

which returns:

{
  "data": {
    "bladerunner": [
      {
        "uid": "0x394c",
        "name@en": "Blade Runner",
        "initial_release_date": "1982-06-25T00:00:00Z",
        "netflix_id": "70083726",
        "val(mathZero)": 0,
        "mediaCount2": 0
      }
    ]
  }
}

Keep in mind, I also got forced to make a variable ‘math(0)’ which seems like another bug, I can’t put max( , 0), it returns nothing. If this is a different bug you can reproduce it with the above command as well.

campoy commented :

Thanks for reporting this, I’m filing it as an enhancement and we’ll get it on our roadmap soon!