Skip to content

Commit

Permalink
compare: locate additional columns to the left of diff columns; add t…
Browse files Browse the repository at this point in the history
…est (#146)
  • Loading branch information
liquidaty authored Dec 31, 2023
1 parent 8541f33 commit 7b64541
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 30 deletions.
56 changes: 28 additions & 28 deletions app/compare.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,17 @@ static void zsv_compare_output_tuple(struct zsv_compare_data *data,
}
}

// output additional columns
for(struct zsv_compare_added_column *ac = data->added_columns; ac; ac = ac->next) {
if(!ac->input) {
if(data->writer.type != ZSV_COMPARE_OUTPUT_TYPE_JSON)
zsv_compare_output_str(data, NULL, ZSV_WRITER_SAME_ROW, 0);
} else {
struct zsv_cell c = data->get_cell(ac->input, ac->col_ix);
zsv_compare_output_strn(data, c.str, c.len, ZSV_WRITER_SAME_ROW, c.quoted);
}
}

// output column name of this cell
zsv_compare_output_str(data, colname, ZSV_WRITER_SAME_ROW, 1);

Expand All @@ -168,17 +179,6 @@ static void zsv_compare_output_tuple(struct zsv_compare_data *data,
}
}

// output additional columns
for(struct zsv_compare_added_column *ac = data->added_columns; ac; ac = ac->next) {
if(!ac->input) {
if(data->writer.type != ZSV_COMPARE_OUTPUT_TYPE_JSON)
zsv_compare_output_str(data, NULL, ZSV_WRITER_SAME_ROW, 0);
} else {
struct zsv_cell c = data->get_cell(ac->input, ac->col_ix);
zsv_compare_output_strn(data, c.str, c.len, ZSV_WRITER_SAME_ROW, c.quoted);
}
}

if(data->writer.type == ZSV_COMPARE_OUTPUT_TYPE_JSON)
zsv_compare_json_row_end(data);
}
Expand Down Expand Up @@ -359,6 +359,10 @@ static void zsv_compare_output_begin(struct zsv_compare_data *data) {
1);
}

// write additional column names
for(struct zsv_compare_added_column *ac = data->added_columns; ac; ac = ac->next)
zsv_compare_header_str(data, ac->colname->name, ZSV_WRITER_SAME_ROW, 1);

// write "Column"
zsv_compare_header_str(data, (const unsigned char *)"Column", ZSV_WRITER_SAME_ROW, 0);

Expand All @@ -368,10 +372,6 @@ static void zsv_compare_output_begin(struct zsv_compare_data *data) {
zsv_compare_header_str(data, (const unsigned char *)input->path, ZSV_WRITER_SAME_ROW, 1);
}

// write additional column names
for(struct zsv_compare_added_column *ac = data->added_columns; ac; ac = ac->next)
zsv_compare_header_str(data, ac->colname->name, ZSV_WRITER_SAME_ROW, 1);

if(data->writer.type == ZSV_COMPARE_OUTPUT_TYPE_JSON && !data->writer.object)
jsonwriter_end_array(data->writer.handle.jsw);
}
Expand Down Expand Up @@ -600,19 +600,19 @@ static int compare_usage() {
static const char *usage[] = {
"Usage: compare [options] [file1.csv] [file2.csv] [...]",
"Options:",
" -h,--help : show usage",
" -k,--key <field> : specify a field to match rows on",
" can be specified multiple times",
" -a,--add <field> : specify an additional field to output",
" will use the [first input] source",
" --sort : sort on keys before comparing",
" --sort-in-memory : for sorting, use in-memory instead of temporary db",
" (see https://www.sqlite.org/inmemorydb.html)",
" --json : output as JSON",
" --json-compact : output as compact JSON",
" --json-object : output as an array of objects",
" --print-key-colname : when outputting key column diffs,",
" print column name instead of <key>",
" -h,--help : show usage",
" -k,--key <colname> : specify a column to match rows on",
" can be specified multiple times",
" -a,--add <colname> : specify an additional column to output",
" will use the [first input] source",
" --sort : sort on keys before comparing",
" --sort-in-memory : for sorting, use in-memory instead of temporary db",
" (see https://www.sqlite.org/inmemorydb.html)",
" --json : output as JSON",
" --json-compact : output as compact JSON",
" --json-object : output as an array of objects",
" --print-key-colname: when outputting key column diffs,",
" print column name instead of <key>",
"",
"NOTES",
"",
Expand Down
6 changes: 6 additions & 0 deletions app/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -494,3 +494,9 @@ test-compare: test-%: ${BUILD_DIR}/bin/zsv_%${EXE}

@(${PREFIX} $< compare/t1.csv compare/t7.csv compare/t3.csv --print-key-colname -k c ${REDIRECT1} ${TMP_DIR}/[email protected] && \
${CMP} ${TMP_DIR}/[email protected] expected/[email protected] && ${TEST_PASS} || ${TEST_FAIL})

@(${PREFIX} $< compare/t1.csv compare/t7.csv compare/t3.csv --print-key-colname -k c ${REDIRECT1} ${TMP_DIR}/[email protected] && \
${CMP} ${TMP_DIR}/[email protected] expected/[email protected] && ${TEST_PASS} || ${TEST_FAIL})

@(${PREFIX} $< ../../data/compare/t1.csv ../../data/compare/t2.csv --add AccentCity --sort -k country -k city ${REDIRECT1} ${TMP_DIR}/[email protected] && \
${CMP} ${TMP_DIR}/[email protected] expected/[email protected] && ${TEST_PASS} || ${TEST_FAIL})
9 changes: 9 additions & 0 deletions app/test/expected/test-compare.out10
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
country,city,AccentCity,Column,../../data/compare/t1.csv,../../data/compare/t2.csv
cn,fulongling,Placken,<key>,Missing,
de,placken,Placken,Longitude,8.433333,10.4
ie,burtown cross roads,Oggal,<key>,Missing,
kr,chusamdong,Cvetovo,<key>,Missing,
pl,ciesle male,Ciesle Male,AccentCity,Ciesle Male,Ciesle XXX
ru,chishmabash,Chishmabash,<key>,,Missing
tr,yenioe,Lituhi Mission,<key>,Missing,
zr,kakova,Kakova,Region,9,XX
12 changes: 12 additions & 0 deletions data/compare/t1.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Country,City,AccentCity,Region,Population,Latitude,Longitude
de,placken,Placken,7,,52.15,8.433333
gb,duffryn,Duffryn,Z3,,51.4375,-3.304444
id,kemitang,Kemitang,7,,-7.544444,109.183333
ie,oggal,Oggal,2,,54.2597222,-7.9266667
mk,cvetovo,Cvetovo,92,,41.8580556,21.4097222
pl,ciesle male,Ciesle Male,86,,52.176861,17.649143
ru,chishmabash,Chishmabash,73,,55.4708,53.8996
tz,lituhi mission,Lituhi Mission,14,,-10.55,34.6
us,steep falls,Steep Falls,ME,,43.7938889,-70.6530556
za,hota,Hota,5,,-31.640685,27.681357
zr,kakova,Kakova,9,,0.7333333,29.0166667
15 changes: 15 additions & 0 deletions data/compare/t2.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Region,Population,Latitude,Longitude,Country,City,AccentCity
92,,41.8580556,21.4097222,mk,cvetovo,Cvetovo
86,,52.176861,17.649143,pl,ciesle male,Ciesle XXX
39,,41.677473,27.067859,tr,yenioe,Yenioe
14,,-10.55,34.6,tz,lituhi mission,Lituhi Mission
ME,,43.7938889,-70.6530556,us,steep falls,Steep Falls
5,,-31.640685,27.681357,za,hota,Hota
XX,,0.7333333,29.0166667,zr,kakova,Kakova
3,,26.934908,115.628584,cn,fulongling,Fulongling
7,,52.15,10.4,de,placken,Placken
Z3,,51.4375,-3.304444,gb,duffryn,Duffryn
7,,-7.544444,109.183333,id,kemitang,Kemitang
12,,52.9911111,-6.8938889,ie,burtown cross roads,Burtown XXX
2,,54.2597222,-7.9266667,ie,oggal,Oggal
16,,34.788889,127.670833,kr,chusamdong,Chusamdong
5 changes: 3 additions & 2 deletions docs/compare.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ Row matching and sorting is handled as follows:

Imagine we want to compare the following two tables:

### Table 1 (t1.csv)
### Table 1 [t1.csv](../data/compare/t1.csv)


|Country|City|AccentCity|Region|Population|Latitude|Longitude|
|--|--|--|--|--|--|--|
Expand All @@ -76,7 +77,7 @@ Imagine we want to compare the following two tables:
|za|hota|Hota|05||-31.640685|27.681357|
|zr|kakova|Kakova|09||0.7333333|29.0166667|

### Table 2 (t2.csv)
### Table 2 [t2.csv](../data/compare/t2.csv)

|Region|Population|Latitude|Longitude|Country|City|AccentCity|
|--|--|--|--|--|--|--|
Expand Down

0 comments on commit 7b64541

Please sign in to comment.