Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: extsort underflow in CSV mode #2412

Merged
merged 6 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions resources/test/issue2391-test_ids.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
pnm,tc_id,pc_id
405,139280,9730000630075
405,139281,9730000630075
131,139282862,9730065908379
138,139282863,9730065908379
138,139282864,9730065908379
405,139282865,9730065908379
138,139282866,9730065908379
138,139282867,9730065908379
138,139282868,9730065908379
138,139282869,9730065908379
138,139282870,9730065908379
138,139282871,9730065908379
252,139282,9730000630075
241,139283,9730000630075
272,139284,9730000630075
273,139285,9730000630075
2 changes: 1 addition & 1 deletion src/cmd/extsort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ fn sort_csv(
return fail!("Failed to retrieve position: invalid integer");
};

idxfile.seek(position - position_delta)?;
idxfile.seek(position.saturating_sub(position_delta))?;
idxfile.read_byte_record(&mut record_wrk)?;
sorted_csv_wtr.write_byte_record(&record_wrk)?;
}
Expand Down
65 changes: 65 additions & 0 deletions tests/test_extsort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,68 @@ fn extsort_csvmode() {

assert_eq!(dos2unix(&sorted_output), dos2unix(&expected_csv));
}

#[test]
fn extsort_issue_2391() {
let wrk = Workdir::new("extsort_issue_2391").flexible(true);
wrk.clear_contents().unwrap();

let unsorted_csv = wrk.load_test_resource("issue2391-test_ids.csv");
wrk.create_from_string("issue2391-test_ids.csv", &unsorted_csv);
// create index
let mut cmd_wrk = wrk.command("index");
cmd_wrk.arg("issue2391-test_ids.csv");

wrk.assert_success(&mut cmd_wrk);

// as git mangles line endings, we need to convert manually to CRLF as per issue 2391
// see https://github.com/dathere/qsv/issues/2391
// convert LF to CRLF in test file to ensure consistent line endings
#[cfg(target_os = "windows")]
{
let mut cmd = wrk.command("cmd");
cmd.args([
"/C",
"type issue2391-test_ids.csv > issue2391-test_ids.tmp.csv && move /Y \
issue2391-test_ids.tmp.csv issue2391-test_ids.csv",
]);
wrk.output(&mut cmd);
}
#[cfg(not(target_os = "windows"))]
{
let mut cmd = wrk.command("sh");
cmd.args([
"-c",
"sed 's/$/\r/' issue2391-test_ids.csv > issue2391-test_ids.tmp.csv && mv \
issue2391-test_ids.tmp.csv issue2391-test_ids.csv",
]);
wrk.output(&mut cmd);
}

let mut cmd = wrk.command("extsort");
cmd.arg("issue2391-test_ids.csv")
.args(["--select", "tc_id,pnm,pc_id"]);

wrk.assert_success(&mut cmd);
let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["pnm", "tc_id", "pc_id"],
svec!["405", "139280", "9730000630075"],
svec!["405", "139281", "9730000630075"],
svec!["252", "139282", "9730000630075"],
svec!["131", "139282862", "9730065908379"],
svec!["138", "139282863", "9730065908379"],
svec!["138", "139282864", "9730065908379"],
svec!["405", "139282865", "9730065908379"],
svec!["138", "139282866", "9730065908379"],
svec!["138", "139282867", "9730065908379"],
svec!["138", "139282868", "9730065908379"],
svec!["138", "139282869", "9730065908379"],
svec!["138", "139282870", "9730065908379"],
svec!["138", "139282871", "9730065908379"],
svec!["241", "139283", "9730000630075"],
svec!["272", "139284", "9730000630075"],
svec!["273", "139285", "9730000630075"],
];
assert_eq!(got, expected);
}
Loading