Skip to content

Commit 2568d6a

Browse files
committed
add missing functions
1 parent 392dd84 commit 2568d6a

40 files changed

+1494
-247
lines changed

google-cloud-firestore/src/main/java/com/google/cloud/firestore/AggregateQuerySnapshot.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.cloud.firestore;
1818

19+
import com.google.api.core.InternalApi;
1920
import com.google.api.core.InternalExtensionOnly;
2021
import com.google.cloud.Timestamp;
2122
import com.google.firestore.v1.Value;
@@ -189,4 +190,9 @@ public boolean equals(Object object) {
189190
public int hashCode() {
190191
return Objects.hash(query, data);
191192
}
193+
194+
@InternalApi
195+
Map<String, Value> getData() {
196+
return data;
197+
}
192198
}

google-cloud-firestore/src/main/java/com/google/cloud/firestore/FieldPath.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.cloud.firestore;
1818

19+
import com.google.api.core.InternalApi;
1920
import com.google.auto.value.AutoValue;
2021
import com.google.common.base.Preconditions;
2122
import com.google.common.collect.ImmutableList;
@@ -79,7 +80,8 @@ static boolean isDocumentId(String path) {
7980
}
8081

8182
/** Returns a field path from a dot separated string. Does not support escaping. */
82-
static FieldPath fromDotSeparatedString(String field) {
83+
@InternalApi
84+
public static FieldPath fromDotSeparatedString(String field) {
8385
if (PROHIBITED_CHARACTERS.matcher(field).matches()) {
8486
throw new IllegalArgumentException("Use FieldPath.of() for field names containing '˜*/[]'.");
8587
}

google-cloud-firestore/src/main/java/com/google/cloud/firestore/Pipeline.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.google.cloud.firestore.pipeline.PaginatingPipeline;
1010
import com.google.cloud.firestore.pipeline.expressions.AggregatorTarget;
1111
import com.google.cloud.firestore.pipeline.expressions.Expr;
12+
import com.google.cloud.firestore.pipeline.expressions.ExprWithAlias;
1213
import com.google.cloud.firestore.pipeline.expressions.Field;
1314
import com.google.cloud.firestore.pipeline.expressions.Fields;
1415
import com.google.cloud.firestore.pipeline.expressions.FilterCondition;
@@ -137,6 +138,9 @@ private Map<String, Expr> projectablesToMap(Selectable... selectables) {
137138
if (fieldsProj.getFields() != null) {
138139
fieldsProj.getFields().forEach(f -> projMap.put(f.getPath().getEncodedPath(), f));
139140
}
141+
} else if (proj instanceof ExprWithAlias) {
142+
ExprWithAlias exprWithAlias = (ExprWithAlias) proj;
143+
projMap.put(exprWithAlias.getAlias(), exprWithAlias.getExpr());
140144
}
141145
}
142146
return projMap;

google-cloud-firestore/src/main/java/com/google/cloud/firestore/PipelineResult.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ static PipelineResult fromDocument(
8181
FirestoreRpcContext<?> rpcContext, Timestamp readTime, Document document) {
8282
return new PipelineResult(
8383
rpcContext,
84-
new DocumentReference(rpcContext, ResourcePath.create(document.getName())),
84+
document.getName().isEmpty()
85+
? null
86+
: new DocumentReference(rpcContext, ResourcePath.create(document.getName())),
8587
document.getFieldsMap(),
8688
readTime,
8789
Timestamp.fromProto(document.getUpdateTime()),

google-cloud-firestore/src/main/java/com/google/cloud/firestore/PipelineUtils.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
import com.google.cloud.firestore.Query.UnaryFilterInternal;
1616
import com.google.cloud.firestore.pipeline.PaginatingPipeline;
1717
import com.google.cloud.firestore.pipeline.expressions.AggregatorTarget;
18-
import com.google.cloud.firestore.pipeline.expressions.Constant;
1918
import com.google.cloud.firestore.pipeline.expressions.Field;
2019
import com.google.cloud.firestore.pipeline.expressions.FilterCondition;
20+
import com.google.common.collect.Lists;
2121
import com.google.firestore.v1.Cursor;
2222
import com.google.firestore.v1.Value;
2323
import java.util.List;
@@ -53,20 +53,13 @@ static FilterCondition toPipelineFilterCondition(FilterInternal f) {
5353
return Field.of(fieldPath).arrayContains(value);
5454
case IN:
5555
List<Value> valuesList = value.getArrayValue().getValuesList();
56-
return inAny(
57-
Field.of(fieldPath),
58-
valuesList.stream().map(Constant::of).collect(Collectors.toList()));
56+
return inAny(Field.of(fieldPath), Lists.newArrayList(valuesList));
5957
case ARRAY_CONTAINS_ANY:
6058
List<Value> valuesListAny = value.getArrayValue().getValuesList();
61-
return arrayContainsAny(
62-
Field.of(fieldPath),
63-
valuesListAny.stream().map(Constant::of).collect(Collectors.toList()));
59+
return arrayContainsAny(Field.of(fieldPath), valuesListAny.toArray());
6460
case NOT_IN:
6561
List<Value> notInValues = value.getArrayValue().getValuesList();
66-
return not(
67-
inAny(
68-
Field.of(fieldPath),
69-
notInValues.stream().map(Constant::of).collect(Collectors.toList())));
62+
return not(inAny(Field.of(fieldPath), Lists.newArrayList(notInValues)));
7063
default:
7164
// Handle OPERATOR_UNSPECIFIED and UNRECOGNIZED cases as needed
7265
throw new IllegalArgumentException("Unsupported operator: " + comparisonFilter.operator);
@@ -165,13 +158,11 @@ static AggregatorTarget toPipelineAggregatorTarget(AggregateField f) {
165158

166159
switch (operator) {
167160
case "sum":
168-
return Field.of(fieldPath)
169-
.sum()
170-
.toField(f.getAlias());
161+
return Field.of(fieldPath).sum().toField(f.getAlias());
171162

172163
case "count":
173164
return countAll().toField(f.getAlias());
174-
case "avg":
165+
case "average":
175166
return Field.of(fieldPath).avg().toField(f.getAlias());
176167
default:
177168
// Handle the 'else' case appropriately in your Java code

google-cloud-firestore/src/main/java/com/google/cloud/firestore/Query.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static com.google.cloud.firestore.PipelineUtils.toPaginatedPipeline;
2020
import static com.google.cloud.firestore.PipelineUtils.toPipelineFilterCondition;
21+
import static com.google.cloud.firestore.pipeline.expressions.Function.and;
2122
import static com.google.common.collect.Lists.reverse;
2223
import static com.google.firestore.v1.StructuredQuery.FieldFilter.Operator.ARRAY_CONTAINS;
2324
import static com.google.firestore.v1.StructuredQuery.FieldFilter.Operator.ARRAY_CONTAINS_ANY;
@@ -40,6 +41,7 @@
4041
import com.google.auto.value.AutoValue;
4142
import com.google.cloud.Timestamp;
4243
import com.google.cloud.firestore.Query.QueryOptions.Builder;
44+
import com.google.cloud.firestore.pipeline.expressions.Exists;
4345
import com.google.cloud.firestore.pipeline.expressions.Field;
4446
import com.google.cloud.firestore.pipeline.expressions.Ordering;
4547
import com.google.cloud.firestore.pipeline.expressions.Selectable;
@@ -2122,11 +2124,11 @@ public AggregateQuery aggregate(
21222124
@Nonnull
21232125
public Pipeline toPipeline() {
21242126
// From
2125-
Pipeline ppl = this.options.getAllDescendants() ?
2126-
Pipeline.fromCollectionGroup(
2127-
this.options.getCollectionId())
2128-
: Pipeline.fromCollection(
2129-
this.options.getParentPath().append(this.options.getCollectionId()).getPath());
2127+
Pipeline ppl =
2128+
this.options.getAllDescendants()
2129+
? Pipeline.fromCollectionGroup(this.options.getCollectionId())
2130+
: Pipeline.fromCollection(
2131+
this.options.getParentPath().append(this.options.getCollectionId()).getPath());
21302132

21312133
// Filters
21322134
for (FilterInternal f : this.options.getFilters()) {
@@ -2156,11 +2158,28 @@ public Pipeline toPipeline() {
21562158
? Ordering.Direction.ASCENDING
21572159
: Ordering.Direction.DESCENDING))
21582160
.collect(Collectors.toList());
2161+
2162+
// Add exists filters to match Query's implicit orderby semantics.
2163+
List<Exists> exists =
2164+
normalizedOrderbys.stream()
2165+
// .filter(order -> !order.fieldReference.getFieldPath().equals("__name__"))
2166+
.map(order -> Field.of(order.fieldReference.getFieldPath()).exists())
2167+
.collect(Collectors.toList());
2168+
if (exists.size() > 1) {
2169+
ppl =
2170+
ppl.filter(
2171+
and(exists.get(0), exists.subList(1, exists.size()).toArray(new Exists[] {})));
2172+
} else if (exists.size() == 1) {
2173+
ppl = ppl.filter(exists.get(0));
2174+
}
2175+
21592176
ppl = ppl.sort(orders, Density.REQUIRED, Truncation.UNSPECIFIED);
21602177
}
21612178

21622179
// Cursors, Limit and Offset
2163-
if (this.options.getStartCursor() != null || this.options.getEndCursor() != null || this.options.getLimitType() == LimitType.Last) {
2180+
if (this.options.getStartCursor() != null
2181+
|| this.options.getEndCursor() != null
2182+
|| this.options.getLimitType() == LimitType.Last) {
21642183
ppl =
21652184
toPaginatedPipeline(
21662185
ppl,
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.google.cloud.firestore.pipeline.expressions;
2+
3+
import com.google.api.core.InternalApi;
4+
import com.google.common.collect.Lists;
5+
6+
public final class Add extends Function {
7+
@InternalApi
8+
Add(Expr left, Expr right) {
9+
super("add", Lists.newArrayList(left, right));
10+
}
11+
}

google-cloud-firestore/src/main/java/com/google/cloud/firestore/pipeline/expressions/AggregatorTarget.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.google.cloud.firestore.pipeline.expressions;
22

3-
public final class AggregatorTarget implements Selectable{
3+
public final class AggregatorTarget implements Selectable {
44
private final Accumulator accumulator;
55
private final String fieldName;
66

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.google.cloud.firestore.pipeline.expressions;
2+
3+
import com.google.common.collect.Lists;
4+
import java.util.List;
5+
6+
public final class ArrayConcat extends Function {
7+
ArrayConcat(Expr array, List<Expr> rest) {
8+
super("array_concat", Lists.newArrayList(array, new ListOfExprs(rest)));
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.google.cloud.firestore.pipeline.expressions;
2+
3+
import com.google.common.collect.Lists;
4+
import java.util.List;
5+
6+
public final class ArrayContainsAll extends Function implements FilterCondition {
7+
ArrayContainsAll(Expr array, List<Expr> elements) {
8+
super("array_contains_all", Lists.newArrayList(array, new ListOfExprs(elements)));
9+
}
10+
}

0 commit comments

Comments
 (0)