protected function createTagIdsQuery(array $translations = null, $useAlwaysAvailable = true)
    {
        /** @var \eZ\Publish\Core\Persistence\Database\SelectQuery $query */
        $query = $this->handler->createSelectQuery();
        $query->select('DISTINCT eztags.id, eztags.keyword')
        ->from(
            $this->handler->quoteTable('eztags')
        )
        // @todo: Joining with eztags_keyword is probably a VERY bad way to gather that information
        // since it creates an additional cartesian product with translations.
        ->leftJoin(
            $this->handler->quoteTable('eztags_keyword'),
            $query->expr->lAnd(
                // eztags_keyword.locale is also part of the PK but can't be
                // easily joined with something at this level
                $query->expr->eq(
                    $this->handler->quoteColumn('keyword_id', 'eztags_keyword'),
                    $this->handler->quoteColumn('id', 'eztags')
                ),
                $query->expr->eq(
                    $this->handler->quoteColumn('status', 'eztags_keyword'),
                    $query->bindValue(1, null, PDO::PARAM_INT)
                )
            )
        );

        if (!empty($translations)) {
            if ($useAlwaysAvailable) {
                $query->where(
                    $query->expr->lOr(
                        $query->expr->in(
                            $this->handler->quoteColumn('locale', 'eztags_keyword'),
                            $translations
                        ),
                        $query->expr->lAnd(
                            $query->expr->gt(
                                $query->expr->bitAnd(
                                    $this->handler->quoteColumn('language_mask', 'eztags'),
                                    1
                                ),
                                0
                            ),
                            $query->expr->eq(
                                $this->handler->quoteColumn('main_language_id', 'eztags'),
                                $query->expr->bitAnd(
                                    $this->handler->quoteColumn('language_id', 'eztags_keyword'),
                                    -2 // -2 == PHP_INT_MAX << 1
                                )
                            )
                        )
                    )
                );
            } else {
                $query->where(
                    $query->expr->in(
                        $this->handler->quoteColumn('locale', 'eztags_keyword'),
                        $translations
                    )
                );
            }
        }

        return $query;
    }