Skip to content

Commit ac21ef6

Browse files
authored
fix: Using namespace from DatastoreOptions if aggregation query is not configured with one. (#1055)
fix #1054
1 parent 207a04e commit ac21ef6

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

google-cloud-datastore/src/main/java/com/google/cloud/datastore/execution/request/AggregationQueryRequestProtoPreparer.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.google.cloud.datastore.ReadOptionProtoPreparer;
2727
import com.google.cloud.datastore.StructuredQueryProtoPreparer;
2828
import com.google.cloud.datastore.aggregation.Aggregation;
29+
import com.google.common.base.MoreObjects;
2930
import com.google.datastore.v1.GqlQuery;
3031
import com.google.datastore.v1.PartitionId;
3132
import com.google.datastore.v1.Query;
@@ -92,9 +93,9 @@ private com.google.datastore.v1.AggregationQuery getAggregationQuery(
9293
private PartitionId getPartitionId(AggregationQuery aggregationQuery) {
9394
PartitionId.Builder builder =
9495
PartitionId.newBuilder().setProjectId(datastoreOptions.getProjectId());
95-
if (aggregationQuery.getNamespace() != null) {
96-
builder.setNamespaceId(aggregationQuery.getNamespace());
97-
}
96+
String namespace =
97+
MoreObjects.firstNonNull(aggregationQuery.getNamespace(), datastoreOptions.getNamespace());
98+
builder.setNamespaceId(namespace);
9899
return builder.build();
99100
}
100101
}

google-cloud-datastore/src/test/java/com/google/cloud/datastore/execution/request/AggregationQueryRequestProtoPreparerTest.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public void shouldPrepareReadOptionsWithGivenGqlQuery() {
154154
}
155155

156156
@Test
157-
public void shouldPrepareAggregationQueryWithoutNamespace() {
157+
public void shouldPrepareAggregationQueryWithNamespaceFromDatastoreOptions() {
158158
AggregationQuery structuredQueryWithoutNamespace =
159159
Query.newAggregationQueryBuilder()
160160
.addAggregation(count().as("total"))
@@ -169,8 +169,9 @@ public void shouldPrepareAggregationQueryWithoutNamespace() {
169169
protoPreparer.prepare(QueryAndReadOptions.create(gqlQueryWithoutNamespace));
170170

171171
assertThat(runAggregationQueryFromStructuredQuery.getPartitionId().getNamespaceId())
172-
.isEqualTo("");
173-
assertThat(runAggregationQueryFromGqlQuery.getPartitionId().getNamespaceId()).isEqualTo("");
172+
.isEqualTo(NAMESPACE);
173+
assertThat(runAggregationQueryFromGqlQuery.getPartitionId().getNamespaceId())
174+
.isEqualTo(NAMESPACE);
174175
}
175176

176177
private RunAggregationQueryRequest prepareQuery(AggregationQuery query, ReadOption readOption) {

0 commit comments

Comments
 (0)