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;
  }
@Test
  public void getImplicitOrderBy_nameInWhere() {
    StructuredQuery.Builder builder = testQuery.toBuilder();
    builder
        .getWhereBuilder()
        .getCompositeFilterBuilder()
        .addFilters(
            Filter.newBuilder()
                .setFieldFilter(
                    FieldFilter.newBuilder()
                        .setField(FieldReference.newBuilder().setFieldPath("__name__"))
                        .setOp(FieldFilter.Operator.NOT_EQUAL)
                        .setValue(Value.newBuilder().setStringValue(""))));
    testQuery = builder.build();
    // WHERE (`z€a`.a.a != "" AND `b` > "") AND c == "" AND `z$` > "456" AND `z` > "123" AND z IS
    // NOT NAN AND __name__ != "" ORDER BY b DESC
    // -> (ORDER BY b DESC) + __name__ DESC, `z` DESC, `z$` DESC, `z€a`.a.a DESC
    List<Order> expected =
        ImmutableList.of(
            Order.newBuilder()
                .setField(FieldReference.newBuilder().setFieldPath("__name__"))
                .setDirection(Direction.DESCENDING)
                .build(),
            Order.newBuilder()
                .setField(FieldReference.newBuilder().setFieldPath("`z`"))
                .setDirection(Direction.DESCENDING)
                .build(),
            Order.newBuilder()
                .setField(FieldReference.newBuilder().setFieldPath("`z$`"))
                .setDirection(Direction.DESCENDING)
                .build(),
            Order.newBuilder()
                .setField(FieldReference.newBuilder().setFieldPath("`z€a`.a.a"))
                .setDirection(Direction.DESCENDING)
                .build());
    List<Order> actual = QueryUtils.getImplicitOrderBy(testQuery);
    assertEquals(expected, actual);
  }