protected function buildUnion(array $statements, string $sql): string
    {
        if (isset($statements['unions']) === false || \count($statements['unions']) === 0) {
            return $sql;
        }

        foreach ((array)$statements['unions'] as $i => $union) {
            /* @var $queryBuilder QueryBuilderHandler */
            $queryBuilder = $union['query'];

            if ($i === 0) {
                $sql .= ')';
            }

            $type = ($union['type'] !== QueryBuilderHandler::UNION_TYPE_NONE) ? $union['type'] . ' ' : '';
            $sql .= sprintf(' UNION %s(%s)', $type, $queryBuilder->getQuery('select')->getRawSql());
        }

        return sprintf('(%s', $sql);
    }