protected function _buildSubquery($query)
    {
        $filterQuery = clone $query;
        $filterQuery->disableAutoFields();
        $filterQuery->mapReduce(null, null, true);
        $filterQuery->formatResults(null, true);
        $filterQuery->contain([], true);
        $filterQuery->setValueBinder(new ValueBinder());

        if (!$filterQuery->clause('limit')) {
            $filterQuery->limit(null);
            $filterQuery->order([], true);
            $filterQuery->offset(null);
        }

        $fields = $this->_subqueryFields($query);
        $filterQuery->select($fields['select'], true)->group($fields['group']);

        return $filterQuery;
    }