def CompositeIndexForQuery(query):
  required = True

  kind = query.kind()
  ancestor = query.has_ancestor()
  filters = query.filter_list()
  orders = query.order_list()

  for filter in filters:
    assert filter.op() != datastore_pb.Query_Filter.IN, 'Filter.op()==IN'
    nprops = len(filter.property_list())
    assert nprops == 1, 'Filter has %s properties, expected 1' % nprops

  if not kind:
    required = False

  (filters, orders) = RemoveNativelySupportedComponents(filters, orders)

  eq_filters = [f for f in filters if f.op() in EQUALITY_OPERATORS]
  ineq_filters = [f for f in filters if f.op() in INEQUALITY_OPERATORS]
  exists_filters = [f for f in filters if f.op() in EXISTS_OPERATORS]
  assert (len(eq_filters) + len(ineq_filters) +
          len(exists_filters)) == len(filters), 'Not all filters used'

  if (kind and not ineq_filters and not exists_filters and
      not orders):
    names = set(f.property(0).name() for f in eq_filters)
    if not names.intersection(datastore_types._SPECIAL_PROPERTIES):
      required = False

  ineq_property = None
  if ineq_filters:
    ineq_property = ineq_filters[0].property(0).name()
    for filter in ineq_filters:
      assert filter.property(0).name() == ineq_property

  props = []

  for f in eq_filters:
    prop = f.property(0)
    props.append((prop.name(), ASCENDING))

  props.sort()

  if ineq_property:
    if orders:
      assert ineq_property == orders[0].property()
    else:
      props.append((ineq_property, ASCENDING))

  for order in orders:
    props.append((order.property(), order.direction()))

  for filter in exists_filters:
    prop = filter.property(0)
    prop_name = prop.name()
    for name, direction in props:
      if name == prop_name:
        break
    else:
      props.append((prop_name, ASCENDING))

  if kind and not ancestor and len(props) <= 1:
    required = False

    if props:
      prop, dir = props[0]
      if prop in datastore_types._SPECIAL_PROPERTIES and dir is DESCENDING:
        required = True

  unique_names = set(name for name, dir in props)
  if len(props) > 1 and len(unique_names) == 1:
    required = False

  return (required, kind, ancestor, tuple(props), len(eq_filters))
Return the result as a list of ``(item, count)`` pairs sorted by item.
dataset/ETHPy150Open CollabQ/CollabQ/.google_appengine/google/appengine/datastore/datastore_index.py/CompositeIndexForQuery--crossover with--dataset/ETHPy150Open mikeorr/WebHelpers2/webhelpers2/containers.py/Counter.get_sorted_items