private function renameGroupByFields(SelectQuery $query, $namesMap = null, $originalSource = null) {
        if($namesMap == null) {
            $namesMap = $this->getColumnNames();
        }
        $groupByFields = &$query->getGroupByFields();
        if (!empty($groupByFields)) {
            $source = $query->getTable();
            $newGroupByFields = [];
            foreach ($groupByFields as $field) {
                if ($field == '*') {
                    foreach ($namesMap as $aliasName => $columnName) {
                        $newGroupByFields[$columnName] = 1;
                    }
                    continue;

                } else if (!empty($originalSource) && $field == "$originalSource.*") {

                    foreach ($namesMap as $aliasName => $columnName) {
                        $newGroupByFields["$source.$columnName"] = 1;
                    }
                    continue;

                } else {
                    $newName = $this->getFieldNewName($field, $namesMap, $originalSource, $source);
                    if($newName != null) {
                        $newGroupByFields[$newName] = 1;
                    }
                }
            }
            if (!empty($newGroupByFields)) {
                $query->groupByFields(array_keys($newGroupByFields));
            }
        }
    }