clement commented on code in PR #22175:
URL: https://github.com/apache/beam/pull/22175#discussion_r919454067


##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/firestore/QueryUtils.java:
##########
@@ -0,0 +1,402 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beam.sdk.io.gcp.firestore;
+
+import com.google.firestore.v1.Document;
+import com.google.firestore.v1.StructuredQuery;
+import com.google.firestore.v1.StructuredQuery.Direction;
+import com.google.firestore.v1.StructuredQuery.FieldFilter;
+import com.google.firestore.v1.StructuredQuery.FieldFilter.Operator;
+import com.google.firestore.v1.StructuredQuery.FieldReference;
+import com.google.firestore.v1.StructuredQuery.Filter;
+import com.google.firestore.v1.StructuredQuery.Order;
+import com.google.firestore.v1.StructuredQuery.UnaryFilter;
+import com.google.firestore.v1.Value;
+import com.google.firestore.v1.Value.ValueTypeCase;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Ascii;
+import 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList;
+import 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableSet;
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+/**
+ * Contains several internal utility functions for Firestore query handling, 
such as filling
+ * implicit ordering or escaping field references.
+ */
+class QueryUtils {
+
+  private static final ImmutableSet<Operator> INEQUALITY_FIELD_FILTER_OPS =
+      ImmutableSet.of(
+          FieldFilter.Operator.LESS_THAN,
+          FieldFilter.Operator.LESS_THAN_OR_EQUAL,
+          FieldFilter.Operator.GREATER_THAN,
+          FieldFilter.Operator.GREATER_THAN_OR_EQUAL,
+          FieldFilter.Operator.NOT_EQUAL,
+          FieldFilter.Operator.NOT_IN);
+  private static final ImmutableSet<UnaryFilter.Operator> 
INEQUALITY_UNARY_FILTER_OPS =
+      ImmutableSet.of(UnaryFilter.Operator.IS_NOT_NAN, 
UnaryFilter.Operator.IS_NOT_NULL);
+
+  /**
+   * Populates implicit orderBy of a query in accordance with our 
documentation. * Required
+   * inequality fields are appended in field name order. * __name__ is 
appended if not specified.
+   * See <a
+   * 
href=https://github.com/googleapis/googleapis/tree/master/google/firestore/v1/query.proto#L254>here</a>
+   * for more details.
+   *
+   * @param query The StructuredQuery of the original request.
+   * @return A list of additional orderBy fields, excluding the explicit ones.
+   */
+  static List<Order> getImplicitOrderBy(StructuredQuery query) {
+    List<OrderByFieldPath> expectedImplicitOrders = new ArrayList<>();
+    if (query.hasWhere()) {
+      fillInequalityFields(query.getWhere(), expectedImplicitOrders);
+    }
+    Collections.sort(expectedImplicitOrders);
+    if 
(expectedImplicitOrders.stream().noneMatch(OrderByFieldPath::isDocumentName)) {
+      expectedImplicitOrders.add(OrderByFieldPath.fromString("__name__"));
+    }
+    for (Order order : query.getOrderByList()) {
+      OrderByFieldPath orderField = 
OrderByFieldPath.fromString(order.getField().getFieldPath());
+      expectedImplicitOrders.remove(orderField);
+    }
+
+    List<Order> additionalOrders = new ArrayList<>();
+    if (!expectedImplicitOrders.isEmpty()) {
+      Direction lastDirection =
+          query.getOrderByCount() == 0
+              ? Direction.ASCENDING
+              : query.getOrderByList().get(query.getOrderByCount() - 
1).getDirection();
+
+      for (OrderByFieldPath field : expectedImplicitOrders) {
+        additionalOrders.add(
+            Order.newBuilder()
+                .setDirection(lastDirection)
+                .setField(
+                    
FieldReference.newBuilder().setFieldPath(field.getOriginalString()).build())
+                .build());
+      }
+    }
+
+    return additionalOrders;
+  }
+
+  private static void fillInequalityFields(Filter filter, 
List<OrderByFieldPath> result) {
+    switch (filter.getFilterTypeCase()) {
+      case FIELD_FILTER:
+        if 
(INEQUALITY_FIELD_FILTER_OPS.contains(filter.getFieldFilter().getOp())) {
+          OrderByFieldPath fieldPath =
+              
OrderByFieldPath.fromString(filter.getFieldFilter().getField().getFieldPath());
+          if (!result.contains(fieldPath)) {
+            result.add(fieldPath);
+          }
+        }
+        break;
+      case COMPOSITE_FILTER:
+        filter.getCompositeFilter().getFiltersList().forEach(f -> 
fillInequalityFields(f, result));
+        break;
+      case UNARY_FILTER:
+        if 
(INEQUALITY_UNARY_FILTER_OPS.contains(filter.getUnaryFilter().getOp())) {
+          OrderByFieldPath fieldPath =
+              
OrderByFieldPath.fromString(filter.getUnaryFilter().getField().getFieldPath());
+          if (!result.contains(fieldPath)) {
+            result.add(fieldPath);
+          }
+        }
+        break;
+      default:
+        break;
+    }
+  }
+
+  static @Nullable Value lookupDocumentValue(Document document, String 
fieldPath) {
+    OrderByFieldPath resolvedPath = OrderByFieldPath.fromString(fieldPath);
+    // __name__ is a special field and doesn't exist in (top-level) valueMap 
(see
+    // 
https://firebase.google.com/docs/firestore/reference/rest/v1/projects.databases.documents#Document).
+    if (resolvedPath.isDocumentName()) {
+      return Value.newBuilder().setReferenceValue(document.getName()).build();
+    }
+    return findMapValue(new ArrayList<>(resolvedPath.getSegments()), 
document.getFieldsMap());
+  }
+
+  private static @Nullable Value findMapValue(List<String> segments, 
Map<String, Value> valueMap) {
+    if (segments.isEmpty()) {
+      return null;
+    }
+    String field = segments.remove(0);
+    Value value = valueMap.get(field);
+    if (segments.isEmpty()) {
+      return value;
+    }
+    // Field path traversal is not done, recurse into map values.
+    if (value == null || 
!value.getValueTypeCase().equals(ValueTypeCase.MAP_VALUE)) {
+      return null;
+    }
+    return findMapValue(segments, value.getMapValue().getFieldsMap());
+  }
+
+  private static class OrderByFieldPath implements 
Comparable<OrderByFieldPath> {
+
+    private static final String UNQUOTED_NAME_REGEX_STRING = 
"([a-zA-Z_][a-zA-Z_0-9]*)";
+    private static final String QUOTED_NAME_REGEX_STRING = 
"(`(?:[^`\\\\]|(?:\\\\.))+`)";
+    // After each segment follows a dot and more characters, or the end of the 
string.
+    private static final Pattern FIELD_PATH_SEGMENT_REGEX =
+        Pattern.compile(
+            String.format(
+                "(?:%s|%s)(\\..+|$)", UNQUOTED_NAME_REGEX_STRING, 
QUOTED_NAME_REGEX_STRING),
+            Pattern.DOTALL);
+
+    public static OrderByFieldPath fromString(String fieldPath) {
+      if (fieldPath.isEmpty()) {
+        throw new IllegalArgumentException("Could not resolve empty field 
path");
+      }
+      String originalString = fieldPath;
+      List<String> segments = new ArrayList<>();
+      while (!fieldPath.isEmpty()) {
+        Matcher segmentMatcher = FIELD_PATH_SEGMENT_REGEX.matcher(fieldPath);
+        boolean foundMatch = segmentMatcher.lookingAt();
+        if (!foundMatch) {
+          throw new IllegalArgumentException("OrderBy field path was 
malformed");
+        }
+        String fieldName;
+        if ((fieldName = segmentMatcher.group(1)) != null) {
+          segments.add(fieldName);
+        } else if ((fieldName = segmentMatcher.group(2)) != null) {
+          String unescaped = unescapeFieldName(fieldName.substring(1, 
fieldName.length() - 1));
+          segments.add(unescaped);
+        } else {
+          throw new IllegalArgumentException("OrderBy field path was 
malformed");
+        }
+        fieldPath = fieldPath.substring(fieldName.length());
+        // Due to the regex, any non-empty fieldPath will have a dot before 
the next nested field.
+        if (fieldPath.startsWith(".")) {
+          fieldPath = fieldPath.substring(1);
+        }
+      }
+      return new OrderByFieldPath(originalString, 
ImmutableList.copyOf(segments));
+    }
+
+    private final String originalString;
+    private final ImmutableList<String> segments;
+
+    private OrderByFieldPath(String originalString, ImmutableList<String> 
segments) {
+      this.originalString = originalString;
+      this.segments = segments;
+    }
+
+    public String getOriginalString() {
+      return originalString;
+    }
+
+    public boolean isDocumentName() {
+      return segments.size() == 1 && "__name__".equals(segments.get(0));
+    }
+
+    public ImmutableList<String> getSegments() {
+      return segments;
+    }
+
+    @Override
+    public boolean equals(@Nullable Object other) {
+      if (other instanceof OrderByFieldPath) {
+        return this.segments.equals(((OrderByFieldPath) other).getSegments());
+      }
+      return super.equals(other);
+    }
+
+    @Override
+    public int hashCode() {
+      return Objects.hash(segments);
+    }
+
+    @Override
+    public int compareTo(OrderByFieldPath other) {
+      // Inspired by com.google.cloud.firestore.FieldPath.
+      int length = Math.min(this.getSegments().size(), 
other.getSegments().size());
+      for (int i = 0; i < length; i++) {
+        byte[] thisField = 
this.getSegments().get(i).getBytes(StandardCharsets.UTF_8);
+        byte[] otherField = 
other.getSegments().get(i).getBytes(StandardCharsets.UTF_8);
+        int bytes_length = Math.min(thisField.length, otherField.length);
+        for (int j = 0; j < bytes_length; j++) {
+          int cmp = Byte.compare(thisField[j], otherField[j]);

Review Comment:
   This looks like a signed byte comparator, which would be wrong (try a test 
with an ascii character vs a UTF8 multi-byte code point).
   
   If you have access to Guava's `UnsignedBytes.lexicographicalComparator` this 
can be used directly on the byte arrays returned by `getBytes`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to