public void union(Block block)
    {
        currentBlockIndex++;
        ensureBlocksCapacity(currentBlockIndex + 1);
        blocks[currentBlockIndex] = block;

        int positionCount = block.getPositionCount();
        int[] positions = new int[positionCount];

        // Add the elements to the hash table. Since union can only increase the set size, there is no need to create a separate hashtable.
        int positionsIndex = 0;
        for (int i = 0; i < positionCount; i++) {
            int hashPosition = getInsertPosition(blockPositionByHash, getMaskedHash(hashPosition(elementType, block, i)), block, i);
            if (hashPosition != INVALID_POSITION) {
                // There is no need to test if adding element is successful since it's on the same hash table
                addElement(blockPositionByHash, hashPosition, block, i);
                positions[positionsIndex++] = i;
            }
        }

        getPositionsForBlocks().add(positionsList(positions, 0, positionsIndex));
        size += positionsIndex;
    }
@Test
    public void testIntersectWithDistinctValues()
    {
        OptimizedTypedSet typedSet = new OptimizedTypedSet(BIGINT, BIGINT_DISTINCT_METHOD_HANDLE, POSITIONS_PER_PAGE);

        Block block = createLongSequenceBlock(0, POSITIONS_PER_PAGE - 1).appendNull();
        typedSet.union(block);

        testIntersect(typedSet, block, block);

        block = createLongSequenceBlock(0, POSITIONS_PER_PAGE / 2 - 1).appendNull();
        testIntersect(typedSet, block, block);

        block = createLongSequenceBlock(0, 1).appendNull();
        testIntersect(typedSet, block, block);
    }