google-cloud-java icon indicating copy to clipboard operation
google-cloud-java copied to clipboard

Binding a FullEntity to a GqlQuery fails

Open sai-pullabhotla opened this issue 9 years ago • 14 comments

The GqlQuery.Builder allows positional and named bindings for FullEntity values. However, when I try to use those methods, the query execution fails with the below exception. Is this supposed to work? Same error occurs when using StructuredQuery.PropertyFilter. Am I misunderstanding the purpose of these methods?

Exception in thread "main" com.google.cloud.datastore.DatastoreException: An entity value is not allowed
	at com.google.cloud.datastore.spi.DefaultDatastoreRpc.translate(DefaultDatastoreRpc.java:110)
	at com.google.cloud.datastore.spi.DefaultDatastoreRpc.translate(DefaultDatastoreRpc.java:96)
	at com.google.cloud.datastore.spi.DefaultDatastoreRpc.runQuery(DefaultDatastoreRpc.java:164)
	at com.google.cloud.datastore.DatastoreImpl$1.call(DatastoreImpl.java:93)
	at com.google.cloud.datastore.DatastoreImpl$1.call(DatastoreImpl.java:89)
	at com.google.cloud.RetryHelper.doRetry(RetryHelper.java:179)
	at com.google.cloud.RetryHelper.runWithRetries(RetryHelper.java:244)
	at com.google.cloud.datastore.DatastoreImpl.runQuery(DatastoreImpl.java:88)
	at com.google.cloud.datastore.QueryResultsImpl.sendRequest(QueryResultsImpl.java:73)
	at com.google.cloud.datastore.QueryResultsImpl.<init>(QueryResultsImpl.java:57)
	at com.google.cloud.datastore.DatastoreImpl.run(DatastoreImpl.java:82)
	at com.google.cloud.datastore.DatastoreImpl.run(DatastoreImpl.java:73)
Caused by: com.google.datastore.v1.client.DatastoreException: An entity value is not allowed, code=INVALID_ARGUMENT
	at com.google.datastore.v1.client.RemoteRpc.makeException(RemoteRpc.java:126)
	at com.google.datastore.v1.client.RemoteRpc.makeException(RemoteRpc.java:169)
	at com.google.datastore.v1.client.RemoteRpc.call(RemoteRpc.java:89)
	at com.google.datastore.v1.client.Datastore.runQuery(Datastore.java:108)
	at com.google.cloud.datastore.spi.DefaultDatastoreRpc.runQuery(DefaultDatastoreRpc.java:162)

Sample Code

Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
FullEntity<IncompleteKey> cellNumber = FullEntity.newBuilder().set("countryCode", "1").set("areaCode", "111").set("subscriberNumber", "2223333").build();
GqlQuery query = Query.newGqlQueryBuilder(Query.ResultType.ENTITY, "SELECT * FROM Contact WHERE cellNumber=@1").addBinding(cellNumber).build();
System.out.println(query);
QueryResults<Entity> results = datastore.run(query);
while (results.hasNext()) {
	System.out.println(results.next());
}

sai-pullabhotla avatar Dec 20 '16 17:12 sai-pullabhotla

@lesv Can you determine if this is release blocking?

shinfan avatar Feb 23 '17 00:02 shinfan

@tswast Care to look? I need to lookup a bunch of stuff that you might know off the top of your head.

lesv avatar Feb 23 '17 00:02 lesv

@lesv I'm not familiar with this specific feature of Datastore, either. I've only built queries with scalar types.

tswast avatar Feb 23 '17 00:02 tswast

@tswast Ah - sorry. I saw Gql and thought of you.

lesv avatar Feb 23 '17 01:02 lesv

I don't think this is a GA blocker.

It is reasonable to ask for a single entity (we provide several examples for this) - rather than a list of them, the work around is simple.

lesv avatar Feb 23 '17 02:02 lesv

@sai-pullabhotla My guess is you've moved past this, but do you remember what line was generating the error?

lesv avatar Feb 23 '17 22:02 lesv

@lesv - I don't use the Datastore for any of my production projects, rather working on an open source JPA like persistence framework (http://catatumbo.io). Not a show stopper for me by any means. I was just trying to see if it is something I needed to support in my framework, but the Rpc layer did not like it. The sample code and full stack trace is in the original ticket. Pasting it below again, just in case. In essence, binding an embedded entity to a Query parameter fails on Datastore.run(query). Not sure if this is even supported by Datastore.

Sample Code

Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
FullEntity<IncompleteKey> cellNumber = FullEntity.newBuilder().set("countryCode", "1").set("areaCode", "111").set("subscriberNumber", "2223333").build();
GqlQuery query = Query.newGqlQueryBuilder(Query.ResultType.ENTITY, "SELECT * FROM Contact WHERE cellNumber=@1").addBinding(cellNumber).build();
System.out.println(query);
QueryResults<Entity> results = datastore.run(query);
while (results.hasNext()) {
	System.out.println(results.next());
}

Stack Trace

Exception in thread "main" com.google.cloud.datastore.DatastoreException: An entity value is not allowed
	at com.google.cloud.datastore.spi.DefaultDatastoreRpc.translate(DefaultDatastoreRpc.java:110)
	at com.google.cloud.datastore.spi.DefaultDatastoreRpc.translate(DefaultDatastoreRpc.java:96)
	at com.google.cloud.datastore.spi.DefaultDatastoreRpc.runQuery(DefaultDatastoreRpc.java:164)
	at com.google.cloud.datastore.DatastoreImpl$1.call(DatastoreImpl.java:93)
	at com.google.cloud.datastore.DatastoreImpl$1.call(DatastoreImpl.java:89)
	at com.google.cloud.RetryHelper.doRetry(RetryHelper.java:179)
	at com.google.cloud.RetryHelper.runWithRetries(RetryHelper.java:244)
	at com.google.cloud.datastore.DatastoreImpl.runQuery(DatastoreImpl.java:88)
	at com.google.cloud.datastore.QueryResultsImpl.sendRequest(QueryResultsImpl.java:73)
	at com.google.cloud.datastore.QueryResultsImpl.<init>(QueryResultsImpl.java:57)
	at com.google.cloud.datastore.DatastoreImpl.run(DatastoreImpl.java:82)
	at com.google.cloud.datastore.DatastoreImpl.run(DatastoreImpl.java:73)
Caused by: com.google.datastore.v1.client.DatastoreException: An entity value is not allowed, code=INVALID_ARGUMENT
	at com.google.datastore.v1.client.RemoteRpc.makeException(RemoteRpc.java:126)
	at com.google.datastore.v1.client.RemoteRpc.makeException(RemoteRpc.java:169)
	at com.google.datastore.v1.client.RemoteRpc.call(RemoteRpc.java:89)
	at com.google.datastore.v1.client.Datastore.runQuery(Datastore.java:108)
	at com.google.cloud.datastore.spi.DefaultDatastoreRpc.runQuery(DefaultDatastoreRpc.java:162)

sai-pullabhotla avatar Feb 23 '17 23:02 sai-pullabhotla

Thanks - I'm going to mark this post GA, but should be fixed right after then.

lesv avatar Feb 23 '17 23:02 lesv

Hi @sai-pullabhotla

Not sure if you still have the issue but hope this is helpful to you. For now datastore does not support GQL queries which take an entity as a parameter according to Datastore GQL reference.

Datastore does provide several ways to query entities depending on what you want to get for the query. If you would like to get all the entities of the same kind, please try the code below (assuming you want to use GQL):

Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
KeyFactory keyFactory = datastore.newKeyFactory().setKind("KIND");
Key key1 = keyFactory.newKey("entity1-key");
Key key2 = keyFactory.newKey("entity2-key");
Entity entity1 = Entity.newBuilder(key1)
 .set("Property1", "value1")
 .build();
Entity entity2 = Entity.newBuilder(key2)
 .set("Property2", "value2")
 .build();
datastore.put(entity1, entity2);
    
GqlQuery query = Query.newGqlQueryBuilder(Query.ResultType.ENTITY, 
  "SELECT * FROM KIND").build();
QueryResults<Entity> results = datastore.run(query);
while (results.hasNext()) {
  System.out.println(results.next());
}

If you only want to get the exact entity, you can write codes similar to this:

query = Query.newGqlQueryBuilder(Query.ResultType.ENTITY,
  "SELECT * FROM KIND WHERE __key__ = @1")
  .addBinding(key1)
  .build();
results = datastore.run(query);
while (results.hasNext()) {
  System.out.println(results.next());
}

Or simply datastore.get(key1). You can also check out EntityQuery for more references. Please feel free to add comments if this does not answer your question.

yihanzhen avatar Jan 17 '18 22:01 yihanzhen

My understanding is that the methods in question are primarily for embedded entities (e.g. an Address inside a Customer entity). It is evident that such queries are not allowed/supported by the Datastore. If that's the case, the corresponding methods should be removed from the public API, or throw an UnsupportedOperationException rather than sending the query to Datastore and then fail.

sai-pullabhotla avatar Jan 22 '18 14:01 sai-pullabhotla

@sai-pullabhotla Thanks for the thoughts. cc'ing @wsh for more information.

yihanzhen avatar Jan 24 '18 18:01 yihanzhen

@sai-pullabhotla thanks for this report and your thoughts! Agreed that this is confusing.

git blame doesn't suggest anything remarkable for this method in the builder. I think it may have been added because GQL for {N}DB supports "entity values" (really, model references which the library converts) for ancestor queries. (GQL for Cloud Datastore uses the entity key instead, which GQL for {N}DB also supports.)

In any case, GQL for Cloud Datastore (and thus the GQL queries this SDK supports) cannot feature entity literals per the grammar. In terms of where we go from here, removing the method even if it's never worked correctly would break backwards compatibility and require a major version bump (boo). I think your suggestion of an UnsupportedOperationException is a good idea.

(@eddavisson should probably confirm that my/our understanding of the underlying API capabilities is correct, too.)

wsh avatar Mar 01 '18 00:03 wsh

ping @eddavisson, please let us know if you have more to comment here.

yihanzhen avatar Mar 08 '18 01:03 yihanzhen

Yep, I agree with the analysis; I don't think this particular method (public GqlQuery.Builder<V> addBinding(FullEntity<?>... value) makes sense.

I like the suggestion of having it throw. Perhaps it could throw com.google.cloud.datastore.DatastoreException for now (which is fully-backwards compatible) and also be marked deprecated. In the next major version, it can be removed entirely.

eddavisson avatar Mar 08 '18 20:03 eddavisson