diff --git a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/execution/request/AggregationQueryRequestProtoPreparer.java b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/execution/request/AggregationQueryRequestProtoPreparer.java index b5da8d9fe..8f4e51d6e 100644 --- a/google-cloud-datastore/src/main/java/com/google/cloud/datastore/execution/request/AggregationQueryRequestProtoPreparer.java +++ b/google-cloud-datastore/src/main/java/com/google/cloud/datastore/execution/request/AggregationQueryRequestProtoPreparer.java @@ -26,6 +26,7 @@ import com.google.cloud.datastore.ReadOptionProtoPreparer; import com.google.cloud.datastore.StructuredQueryProtoPreparer; import com.google.cloud.datastore.aggregation.Aggregation; +import com.google.common.base.MoreObjects; import com.google.datastore.v1.GqlQuery; import com.google.datastore.v1.PartitionId; import com.google.datastore.v1.Query; @@ -92,9 +93,9 @@ private com.google.datastore.v1.AggregationQuery getAggregationQuery( private PartitionId getPartitionId(AggregationQuery aggregationQuery) { PartitionId.Builder builder = PartitionId.newBuilder().setProjectId(datastoreOptions.getProjectId()); - if (aggregationQuery.getNamespace() != null) { - builder.setNamespaceId(aggregationQuery.getNamespace()); - } + String namespace = + MoreObjects.firstNonNull(aggregationQuery.getNamespace(), datastoreOptions.getNamespace()); + builder.setNamespaceId(namespace); return builder.build(); } } diff --git a/google-cloud-datastore/src/test/java/com/google/cloud/datastore/execution/request/AggregationQueryRequestProtoPreparerTest.java b/google-cloud-datastore/src/test/java/com/google/cloud/datastore/execution/request/AggregationQueryRequestProtoPreparerTest.java index 6301ebeff..934e3a4c7 100644 --- a/google-cloud-datastore/src/test/java/com/google/cloud/datastore/execution/request/AggregationQueryRequestProtoPreparerTest.java +++ b/google-cloud-datastore/src/test/java/com/google/cloud/datastore/execution/request/AggregationQueryRequestProtoPreparerTest.java @@ -154,7 +154,7 @@ public void shouldPrepareReadOptionsWithGivenGqlQuery() { } @Test - public void shouldPrepareAggregationQueryWithoutNamespace() { + public void shouldPrepareAggregationQueryWithNamespaceFromDatastoreOptions() { AggregationQuery structuredQueryWithoutNamespace = Query.newAggregationQueryBuilder() .addAggregation(count().as("total")) @@ -169,8 +169,9 @@ public void shouldPrepareAggregationQueryWithoutNamespace() { protoPreparer.prepare(QueryAndReadOptions.create(gqlQueryWithoutNamespace)); assertThat(runAggregationQueryFromStructuredQuery.getPartitionId().getNamespaceId()) - .isEqualTo(""); - assertThat(runAggregationQueryFromGqlQuery.getPartitionId().getNamespaceId()).isEqualTo(""); + .isEqualTo(NAMESPACE); + assertThat(runAggregationQueryFromGqlQuery.getPartitionId().getNamespaceId()) + .isEqualTo(NAMESPACE); } private RunAggregationQueryRequest prepareQuery(AggregationQuery query, ReadOption readOption) {