UID query not working with @filter

Posted by piyushGoyal2:

Hi Team,

I ran a query from Ratel where I am doing a uid query using @filter. From Ratel I can see the response is perfectly fine. Then I exported the same query into java application. Here the response seems partial in the sense that whatever data is expected after the filter is missing.
Here is the query:

String query = "query all($idVal: string, $userID: string){ getCircleByUIDAndCID(func: eq(idVal, $idVal)){ uid idVal ~accounts @filter(uid($userID)) { uid firstName belongsTo { uid name } }}}";

With parameters:

      parameterMap.put("$idVal","001w000001NFA6dAAC");
      parameterMap.put("$userID","0x9fc9");

As a next step, I thought to replace the uid query with eq on the firstName field from java client and this absolutely works fine.

Here is the query with the eq
query = "query all($idVal: string, $userName: string){ getCircleByUIDAndCID(func: eq(idVal, $idVal)){ uid idVal ~accounts @filter(eq(firstName, $userName)) { uid firstName belongsTo { uid name } }}}";
With parameters:

    parameterMap.put("$idVal","001w000001NFA6dAAC");
    parameterMap.put("$userName", "userName");

Any reasons for why uid query doesn’t work with @filter?

piyushGoyal2 commented :

As a next step as recommended by @manishrjain we looked at the DGraph logs in debug mode for a uid query with a filter and please find the screenshots attached.

MichelDiz commented :

This is indeed strange with uid func. But another tip would be try with uid_in I suspect it would work with Java. And It has the same effect.

query all($idVal: string, $userID: string){ 
getCircleByUIDAndCID(func: eq(idVal, $idVal)) @filter(uid_in(~accounts, $userID))
{ 
  uid 
  idVal 
  ~accounts { 
    uid 
    firstName 
    belongsTo { 
      uid 
      name 
    } 
  }
  }
}

campoy commented :

Hey @gitlw, any chance you could have a look at this issue?

If it’s not relevant anymore, please close it.
Thanks!

gitlw commented :

@piyushGoyal2 Thanks for reporting this issue. I tried using the following experiment and couldn’t reproduce the problem you described in the latest dgraph master.

  1. Run Dgraph cluster using the latest master branch with Commit SHA-1 : cf7c1e63
  2. Create the sample data set using the following commands
curl localhost:8180/alter -XPOST -d '
idVal: string @index(exact) .
account: uid @reverse .
firstName: string .
'
curl 'localhost:8180/mutate?commitNow=true' -H 'Content-Type:application/rdf' -XPOST -d '{
  set {
    _:one <firstName> "One" .
    _:one <account> _:two .
    _:two <idVal> "idVal" .
    
    _:three <firstName> "Three" .
    _:three <account> _:four .
    _:four <idVal> "idVal" .
  }
}'
  1. Query the data with this sample program
import io.dgraph.DgraphClient;
import io.dgraph.DgraphGrpc;
import io.dgraph.DgraphProto;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.Metadata;
import io.grpc.stub.MetadataUtils;

import java.util.HashMap;
import java.util.Map;

public class Experiment {
  private static final String TEST_HOSTNAME = "localhost";
  private static final int TEST_PORT = 9180;

  private static DgraphClient createDgraphClient(boolean withAuthHeader) {
    ManagedChannel channel =
        ManagedChannelBuilder.forAddress(TEST_HOSTNAME, TEST_PORT).usePlaintext(true).build();
    DgraphGrpc.DgraphStub stub = DgraphGrpc.newStub(channel);

    if (withAuthHeader) {
      Metadata metadata = new Metadata();
      metadata.put(
          Metadata.Key.of("auth-token", Metadata.ASCII_STRING_MARSHALLER), "the-auth-token-value");
      stub = MetadataUtils.attachHeaders(stub, metadata);
    }

    return new DgraphClient(stub);
  }

  public static void main(String[] args) {
    DgraphClient dgraphClient = createDgraphClient(false);
    final String query = "    query all($idVal: string, $userID: string)" +
        "{\n" +
        "  run(func: eq(idVal, $idVal)) {\n" +
        "    uid\n" +
        "    idVal\n" +
        "    ~account @filter(uid($userID)){\n" +
        "      uid\n" +
        "      firstName\n" +
        "    }\n" +
        "  }\n"
            + "}";

    final Map<String, String> parameterMap = new HashMap<>();
    parameterMap.put("$idVal","idVal");
    parameterMap.put("$userID", "0x01");
    DgraphProto.Response response = dgraphClient.newReadOnlyTransaction().queryWithVars(query, parameterMap);

    System.out.println(response.getJson().toStringUtf8());
  }
}

And the result I get is

{"run":[{"uid":"0x2","idVal":"idVal","~account":[{"uid":"0x1","firstName":"One"}]},{"uid":"0x4","idVal":"idVal"}]}

If I remove the @filter(uid(…)) part of the query, the result I get will be

{"run":[{"uid":"0x2","idVal":"idVal","~account":[{"uid":"0x1","firstName":"One"}]},{"uid":"0x4","idVal":"idVal","~account":[{"uid":"0x3","firstName":"Three"}]}]}

Thus the result seems correct to me.

I’m closing this issue for now, and feel free to reopen if you are still seeing this problem.

martinmr commented :

@gitlw did you try to reproduce this in one of the 1.0 releases? I just realized @piyushGoyal2 didn’t specify which version of Dgraph they are using.

Sometimes issues that are fixed in master are still present in the old versions.

gitlw commented :

@martinmr Thanks for the reminder. I’ve re-run the experiment against Dgraph version 1.0.16, and got the same result. @piyushGoyal2 can you please let us know which version of Dgraph you are using and whether you are still seeing this problem? Thanks!