private function quoteBucketName($bucketName, &$params)
    {
        if (!is_array($bucketName)) {
            return $this->db->quoteBucketName($bucketName);
        }

        foreach ($bucketName as $i => $bucket) {
            if ($bucket instanceof Query) {
                list($sql, $params) = $this->build($bucket, $params);

                $bucketName[$i] = "($sql) " . $this->db->quoteBucketName($i);
            }
            elseif (is_string($i)) {
                if (strpos($bucket, '(') === false) {
                    $bucket = $this->db->quoteBucketName($bucket);
                }

                $bucketName[$i] = "$bucket " . $this->db->quoteBucketName($i);
            }
            elseif (strpos($bucket, '(') === false) {
                if (preg_match('/^(.*?)(?i:\s+as|)\s+([^ ]+)$/', $bucket, $matches)) { // with alias
                    $bucketName[$i] = $this->db->quoteBucketName($matches[1]) . ' ' . $this->db->quoteBucketName($matches[2]);
                }
                else {
                    $bucketName[$i] = $this->db->quoteBucketName($bucket);
                }
            }
        }

        return reset($bucketName);
    }