def merge_rows(archive, document, version, writer, partial=False,
    unchanged_cells=None, origin=None):
    """Merge rows in the given archive and database snapshot. Outputs the
    merged rows in the resulting archive to the given archive writer.

    Parameters
    ----------
    archive: histore.archive.base.Archive
        Archive containing previous snapshots of the dataset.
    document: histore.document.base.Document
        Dataset snapshot that is being merged into the archive.
    version: int
        Identifier of the new archive version.
    writer: histore.archive.writer.ArchiveWriter
        Consumer for rows in the new archive version.
    partial: bool, default=False
        Flag indicating whether the given document is a partial document. For
        partial documents missing rows in the archive are considered unchanged
        with respect to the given origin.
    unchanged_cells: set, default=None
        Set of identifier for columns whose cell values are not included in
        the values dictionary for document rows but that remain unchanged with
        respect to the specified source version (origin).
    origin: int, default=None
        Version that the row values originate from. Rows that remain unchanged
        have the timestamp extended for the version of origin.
    """
    arch_reader = archive.reader()
    doc_reader = document.reader()
    arch_row = arch_reader.next()
    doc_row = doc_reader.next()
    while arch_row is not None and doc_row is not None:
        comp = arch_row.comp(doc_row.key)
        if comp < 0:
            if partial:
                arch_row = arch_row.extend(version=version, origin=origin)
            writer.write_archive_row(arch_row)
            arch_row = arch_reader.next()
        elif comp > 0:
            writer.write_document_row(row=doc_row, version=version)
            doc_row = doc_reader.next()
        else:
            arch_row = arch_row.merge(values=doc_row.values, pos=doc_row.
                pos, version=version, unchanged_cells=unchanged_cells,
                origin=origin)
            writer.write_archive_row(arch_row)
            arch_row = arch_reader.next()
            doc_row = doc_reader.next()
    while arch_row is not None:
        if partial:
            arch_row = arch_row.extend(version=version, origin=origin)
        writer.write_archive_row(arch_row)
        arch_row = arch_reader.next()
    while doc_row is not None:
        writer.write_document_row(row=doc_row, version=version)
        doc_row = doc_reader.next()
