You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 24, 2026. It is now read-only.
I have 2 entities stored for Task kind under the namespace ns1 and i configure a com.google.cloud.datastore.Datastore service with ns1 namespace.
Scenario
When I try to run entity query with or without namespace through the com.google.cloud.datastore.Datastore object configured with namespace, i get same results, whereas in case of AggregationQuery i don't.
Code example
importstaticcom.google.cloud.datastore.aggregation.Aggregation.count;
importstaticcom.google.common.collect.Iterables.getOnlyElement;
importcom.google.cloud.datastore.AggregationQuery;
importcom.google.cloud.datastore.Datastore;
importcom.google.cloud.datastore.DatastoreOptions;
importcom.google.cloud.datastore.Entity;
importcom.google.cloud.datastore.EntityQuery;
importcom.google.cloud.datastore.Query;
importjava.util.ArrayList;
importjava.util.Iterator;
importjava.util.List;
publicclassPlayground {
publicstaticvoidmain(String[] args) {
DatastoreOptionsdatastoreOptions = DatastoreOptions.newBuilder()
.setNamespace("ns1")
.build();
Datastoredatastore = datastoreOptions.getService();
EntityQueryentityQueryWithNamespace = Query.newEntityQueryBuilder()
.setKind("Task")
.setNamespace("ns1")
.build();
EntityQueryentityQueryWithoutNamespace = Query.newEntityQueryBuilder()
.setKind("Task")
.build();
AggregationQueryaggregationQueryWithNamespace = Query.newAggregationQueryBuilder()
.over(Query.newKeyQueryBuilder().setKind("Task").build())
.addAggregation(count().as("my_count"))
.setNamespace("ns1")
.build();
AggregationQueryaggregationQueryWithoutNamespace = Query.newAggregationQueryBuilder()
.over(Query.newKeyQueryBuilder().setKind("Task").build())
.addAggregation(count().as("my_count"))
.build();
System.out.println("Total elements from entity query with namespace " + toList(
datastore.run(entityQueryWithNamespace)).size()); // 2System.out.println("Total elements from entity query without namespace " + toList(
datastore.run(entityQueryWithoutNamespace)).size()); // 2System.out.println("Total elements from aggregation query with namespace " + getOnlyElement(
datastore.runAggregation(aggregationQueryWithNamespace)).get("my_count")); // 2System.out.println("Total elements from aggregation query without namespace " + getOnlyElement(
datastore.runAggregation(aggregationQueryWithoutNamespace)).get("my_count")); // 0
}
privatestaticList<Entity> toList(Iterator<Entity> results) {
ArrayList<Entity> list = newArrayList<>();
while (results.hasNext()) {
list.add(results.next());
}
returnlist;
}
}
Problem
last query execution of aggregationQueryWithoutNamespace returns 0 count.
Explanation
If a namespace is configured in AggragationQuery the library will use that namespace to run that query, and if it's not present, it'll completely ignore the one configured in com.google.cloud.datastore.Datastore service and simply run the query in default namespace and hence the diff in output.
This part of code needs to be changed, where we can give priority to the namespace from AggregationQuery followed by com.google.cloud.datastore.Datastore service to keep it consistent with the Entity query.
Given
I have
2 entitiesstored forTaskkind under the namespacens1and i configure acom.google.cloud.datastore.Datastoreservice withns1namespace.Scenario
When I try to run entity query with or without namespace through the
com.google.cloud.datastore.Datastoreobject configured with namespace, i get same results, whereas in case ofAggregationQueryi don't.Code example
Problem
last query execution of
aggregationQueryWithoutNamespacereturns 0 count.Explanation
If a namespace is configured in
AggragationQuerythe library will use that namespace to run that query, and if it's not present, it'll completely ignore the one configured incom.google.cloud.datastore.Datastoreservice and simply run the query in default namespace and hence the diff in output.java-datastore/google-cloud-datastore/src/main/java/com/google/cloud/datastore/execution/request/AggregationQueryRequestProtoPreparer.java
Lines 92 to 99 in 412be61
This part of code needs to be changed, where we can give priority to the namespace from
AggregationQueryfollowed bycom.google.cloud.datastore.Datastoreservice to keep it consistent with the Entity query.Thanks!