"wrong use of var()" error when query variable is empty

To make this easier, I’ll begin with this mutation example. So imagine the following set up:

We have a bunch of parents which may or may not have a collection of items, each of which have an amount.

{
	"set": [
    {
      "uid": "_:first",
      "isParent": true,
      "name": "first",
      "items": [
      	{ "amount": 10 },
    		{ "amount": 2 }
      ]
    },
		{
      "uid": "_:second",
      "isParent": true,
      "name": "second",
      "items": [
      	{ "amount": 5 },
				{ "amount": 3 }
      ]
    },
    {
      "uid": "_:third",
      "name": "third",
      "isParent": true
    }
  ]
}

Notice the third node has no “items” attached to it.

Now with this query:

{
  parents(func: has(isParent)) {
    uid
    name
    items {
      itemAmount as amount
    }
    total: sum(val(itemAmount))
  }
}

I get the expected results:

{
  "data": {
    "parents": [
      {
        "uid": "0xc5b9",
        "name": "third"
      },
      {
        "uid": "0xc5ba",
        "name": "first",
        "items": [
          {
            "amount": 10,
            "uid": "0xc5bb"
          },
          {
            "amount": 2,
            "uid": "0xc5bc"
          }
        ],
        "total": 12
      },
      {
        "uid": "0xc5bd",
        "name": "second",
        "items": [
          {
            "amount": 5,
            "uid": "0xc5be"
          },
          {
            "amount": 3,
            "uid": "0xc5bf"
          }
        ],
        "total": 8
      }
    ]
  }

The problem arises if some parents are added to the database, but they do not have any “items” attached to them. So if on a fresh database we only add the following:

{
	"set": [
    {
      "uid": "_:first",
      "isParent": true,
      "name": "first"
    }
  ]
}

Then the above query causes this error:

"Wrong use of var() with [{itemAmount 2}]."

So my question is this: how can I still use that query regardless of whether or not any nodes have the “items” edge, and if not simply not display a value for “total” (as is the case for the items-less node “third” in the very first example)?

The thing is that if a single node is added with an “items” edge, then the query works fine, but I can’t guarantee there will always be a node with that edge.

Are you sure that you using this query?

{
  parents(func: has(isParent)) {
    uid
    name
    items {
      itemAmount as amount
    }
    total: sum(val(itemAmount))
  }
}

For me is working:

{
  "extensions": {
    "server_latency": {
      "parsing_ns": 30061,
      "processing_ns": 2596369,
      "encoding_ns": 881382
    },
    "txn": {
      "start_ts": 16578
    }
  },
  "data": {
    "parents": [
      {
        "uid": "0x3f7d",
        "name": "third"
      },
      {
        "uid": "0x3f7e",
        "name": "first",
        "items": [
          {
            "amount": 10
          },
          {
            "amount": 2
          }
        ],
        "total": 12
      },
      {
        "uid": "0x3f81",
        "name": "second",
        "items": [
          {
            "amount": 5
          },
          {
            "amount": 3
          }
        ],
        "total": 8
      },
      {
        "uid": "0x3f84",
        "name": "first",
        "items": [
          {
            "amount": 0
          }
        ],
        "total": 0
      },
      {
        "uid": "0x3f86",
        "name": "first"
      }
    ]
  }
}

I’ve also did a test with:

{
	"set": [
    {
      "uid": "_:first",
      "isParent": true,
      "name": "first",
      "items": [
      	{ "amount": 0 }
      ]
    }
  ]
}

Some func or indexes need at least a value. Like “0”.

Tested in:


Dgraph version   : v1.0.12
Commit SHA-1     : 60d9ef0a
Commit timestamp : 2019-03-05 17:59:30 -0800
Branch           : HEAD
Go version       : go1.11.5
1 Like

As I said, the error only happens if, on a fresh database, I have a mutataion with only nodes that don’t have the items edge. Like so:

{
	"set": [
    {
      "uid": "_:first",
      "isParent": true,
      "name": "first"
    }
  ]
}

EDIT: Just to clarify, I mean first wipe the data, then only add the above mutation, from what you’ve shown you did the above set mutation on top of the existing data, so there are no errors.

1 Like

Can you share what version you are?

1 Like

This is a bug. We added new logic to handle queries with null values, and that error you are seeing is obsolete now, it was from the previous logic.

Basically, Dgraph knows that the aggregate value itemAmount is not set (no uids match) so it will use a default value, in this case of zero, and continue on with the query.

2 Likes

Awesome! Thanks guys- I was indeed still on .11

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.