Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 6 additions & 6 deletions src/cmd/extsort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ fn sort_csv(
}
let idx_position = curr_row.position().unwrap();

writeln!(line_wtr, "{sort_key}|{:01$}", idx_position.line(), width)?;
writeln!(line_wtr, "{sort_key}|{:01$}", idx_position.record(), width)?;
}
line_wtr.flush()?;
drop(line_wtr);
Expand Down Expand Up @@ -219,13 +219,13 @@ fn sort_csv(

let mut sorted_csv_wtr = Config::new(args.arg_output.as_ref()).writer()?;

let position_delta: u64 = if args.flag_no_headers {
1
} else {
// idx_position.record() is 1-indexed when headers are present and
// 0-indexed without; subtract accordingly to get the 0-indexed seek key.
Comment thread
jqnatividad marked this conversation as resolved.
Outdated
let position_delta: u64 = u64::from(!args.flag_no_headers);
if !args.flag_no_headers {
// Write the header row if --no-headers is false
sorted_csv_wtr.write_byte_record(&headers)?;
2
};
}

// amortize allocations
let mut record_wrk = csv::ByteRecord::new();
Expand Down
69 changes: 69 additions & 0 deletions tests/test_extsort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,72 @@ fn extsort_issue_2391() {
];
assert_eq!(got, expected);
}

#[test]
fn extsort_csvmode_no_headers() {
// Guards the no-headers CSV-mode path: every input row must appear
// exactly once in the output. Previously, hard-coding position_delta=1
// duplicated the first row and dropped the last.
let wrk = Workdir::new("extsort_csvmode_no_headers").flexible(true);
wrk.clear_contents().unwrap();

let csv = "9\n2\n5\n1\n7\n4\n8\n3\n6\n";
wrk.create_from_string("nh.csv", csv);

let mut idx_cmd = wrk.command("index");
idx_cmd.arg("nh.csv");
wrk.assert_success(&mut idx_cmd);

let mut cmd = wrk.command("extsort");
cmd.arg("nh.csv")
.args(["--select", "1"])
.arg("--no-headers");

let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["1"],
svec!["2"],
svec!["3"],
svec!["4"],
svec!["5"],
svec!["6"],
svec!["7"],
svec!["8"],
svec!["9"],
];
assert_eq!(got, expected);
}

#[test]
fn extsort_csvmode_crlf_no_headers() {
// Guards CRLF + no-headers + CSV mode: combines the two paths the
// earlier off-by-one fixes regressed individually.
let wrk = Workdir::new("extsort_csvmode_crlf_no_headers").flexible(true);
wrk.clear_contents().unwrap();

let csv = "9\r\n2\r\n5\r\n1\r\n7\r\n4\r\n8\r\n3\r\n6\r\n";
wrk.create_from_string("nh_crlf.csv", csv);

let mut idx_cmd = wrk.command("index");
idx_cmd.arg("nh_crlf.csv");
wrk.assert_success(&mut idx_cmd);

let mut cmd = wrk.command("extsort");
cmd.arg("nh_crlf.csv")
.args(["--select", "1"])
.arg("--no-headers");

let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["1"],
svec!["2"],
svec!["3"],
svec!["4"],
svec!["5"],
svec!["6"],
svec!["7"],
svec!["8"],
svec!["9"],
];
assert_eq!(got, expected);
}
Loading