code: protected int applyDistinct() {
  		int numAutoSelections = 0;
  		boolean distinct;
  		if (query.autoDistinct) {
  			// distinct for many join/fetches or manual
  			// in case of ViewQuery we may not need this here, but we need to do
  			// the selection of order-by columns below
  			distinct = query.autoDistinct && !query.autoGroupBy && backend.hasManyRootsFetchesOrJoins();
  			if (distinct) {
  				// we also need to select sorted attributes (for references)
  				numAutoSelections = addOrderExpressionsToSelection();
  			}
  		} else {
  			distinct = query.distinct;
  		}
  		if (distinct) {
  			backend.distinct();
  		}
  		return numAutoSelections;
  	}
docs: Adds order expressions to selection if in "auto distinct" mode and
  the query performs a join or fetch on a relation. In
  this case, attributes from referenced entities inlucded in the sort
  clause must be added to the select clause as well.
  
  @return number of attributes that were needed to compute a distinct