#csv #difference #compare #diff #primary-key #database-table

csv-diff

Compare two CSVs - with ludicrous speed 🚀

7 releases

0.1.0 Oct 30, 2023
0.1.0-beta.4 Feb 26, 2023
0.1.0-beta.1 Jan 7, 2023
0.1.0-beta.0 Dec 4, 2022
0.1.0-alpha Dec 30, 2021

#228 in Encoding

Download history 123/week @ 2023-12-13 202/week @ 2023-12-20 258/week @ 2023-12-27 190/week @ 2024-01-03 112/week @ 2024-01-10 314/week @ 2024-01-17 353/week @ 2024-01-24 221/week @ 2024-01-31 304/week @ 2024-02-07 262/week @ 2024-02-14 280/week @ 2024-02-21 340/week @ 2024-02-28 330/week @ 2024-03-06 336/week @ 2024-03-13 283/week @ 2024-03-20 253/week @ 2024-03-27

1,269 downloads per month
Used in qsv

MIT/Apache

240KB
5.5K SLoC

csv-diff

Find the difference between two CSVs - with ludicrous speed!🚀


Pipeline Status Crates.io version Downloads docs.rs docs


Documentation

https://docs.rs/csv-diff

⚠️Warning⚠️

This crate is still in it's infancy. There will be breaking changes (and dragons🐉) in the beginning.

✨ Highlights

  • 🚀 fastest CSV-diffing library in the world
    • compare two CSVs with 1,000,000 rows x 9 columns in under 600ms
  • 🧵🧶 thread-pool agnostic

Example

use std::io::Cursor;
use csv_diff::{csv_diff::CsvByteDiffLocal, csv::Csv};
use csv_diff::diff_row::{ByteRecordLineInfo, DiffByteRecord};
use std::collections::HashSet;
use std::iter::FromIterator;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // some csv data with a header, where the first column is a unique id
    let csv_data_left = "id,name,kind\n\
                        1,lemon,fruit\n\
                        2,strawberry,fruit";
    let csv_data_right = "id,name,kind\n\
                        1,lemon,fruit\n\
                        2,strawberry,nut";

    let csv_byte_diff = CsvByteDiffLocal::new()?;

    let mut diff_byte_records = csv_byte_diff.diff(
        Csv::with_reader_seek(csv_data_left.as_bytes()),
        Csv::with_reader_seek(csv_data_right.as_bytes()),
    )?;

    diff_byte_records.sort_by_line();

    let diff_byte_rows = diff_byte_records.as_slice();

    assert_eq!(
        diff_byte_rows,
        &[DiffByteRecord::Modify {
            delete: ByteRecordLineInfo::new(
                csv::ByteRecord::from(vec!["2", "strawberry", "fruit"]),
                3
            ),
            add: ByteRecordLineInfo::new(csv::ByteRecord::from(vec!["2", "strawberry", "nut"]), 3),
            field_indices: vec![2]
        }]
    );
    Ok(())
}

Getting Started

In your Cargo.toml file add the following lines under [dependencies]:

csv-diff = "0.1.0"

This will use a rayon thread-pool, but you can opt-out of it and for example use threads without a thread-pool, by opting in into the crossbeam-threads feature (and opting-out of the default features):

csv-diff = { version = "0.1.0", default-features = false, features = ["crossbeam-threads"] }

Use Case

This crate should be used on CSV data that has some sort of primary key for uniquely identifying a record. It is not a general line-by-line diffing crate. You can imagine dumping a database table in CSV format from your test and production system and comparing it with each other to find differences.

Caveats

Due to the fact that this crate is still in it's infancy, there are still some caveats, which we might resolve in the near future:

  • if both CSVs have headers, they must not be in a different ordering (see also #6 and #3)
  • resulting CSV records/lines that have differences are provided as raw bytes; you can use StringRecord::from_byte_record , provided by the csv crate, to try converting them into UTF-8 encoded records.
  • documentation must be improved

Benchmarks

You can run benchmarks with the following command:

cargo bench

Safety

This crate is implemented in 100% Safe Rust, which is ensured by using #![forbid(unsafe_code)].

MSRV

The Minimum Supported Rust Version for this crate is 1.65. An increase of MSRV will be indicated by a minor change (according to SemVer).

Credits

This crate is inspired by the CLI tool csvdiff by Aswin Karthik, which is written in Go. Definitely check it out. It is a great tool.

Additionally, this crate would not exist without the awesome Rust community and these fantastic crates 🦀:




License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~2.6–4MB
~59K SLoC