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_malformedWhereThrows() {
    testQuery =
        testQuery
            .toBuilder()
            .setWhere(
                Filter.newBuilder()
                    .setUnaryFilter(
                        UnaryFilter.newBuilder()
                            .setField(FieldReference.newBuilder().setFieldPath(""))
                            .setOp(UnaryFilter.Operator.IS_NOT_NAN)))
            .build();
    assertThrows(IllegalArgumentException.class, () -> QueryUtils.getImplicitOrderBy(testQuery));
  }