Skip to content

Commit

Permalink
xsv: add --drop flag to partition command
Browse files Browse the repository at this point in the history
The --drop flag drops the partition column itself.

PR #135
  • Loading branch information
Dimagog authored and BurntSushi committed Aug 22, 2018
1 parent 72db9ed commit bc730d7
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/cmd/partition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ partition options:
-p, --prefix-length <n> Truncate the partition column after the
specified number of bytes when creating the
output file.
--drop Drop the partition column from results.
Common options:
-h, --help Display this message
Expand All @@ -48,6 +49,7 @@ struct Args {
arg_outdir: String,
flag_filename: FilenameTemplate,
flag_prefix_length: Option<usize>,
flag_drop: bool,
flag_no_headers: bool,
flag_delimiter: Option<Delimiter>,
}
Expand Down Expand Up @@ -112,12 +114,22 @@ impl Args {
// We have a new key, so make a new writer.
let mut wtr = gen.writer(&*self.arg_outdir, key)?;
if !rconfig.no_headers {
wtr.write_record(&headers)?;
if self.flag_drop {
wtr.write_record(headers.iter().enumerate()
.filter_map(|(i, e)| if i != key_col { Some(e) } else { None }))?;
} else {
wtr.write_record(&headers)?;
}
}
vacant.insert(wtr)
}
};
wtr.write_byte_record(&row)?;
if self.flag_drop {
wtr.write_record(row.iter().enumerate()
.filter_map(|(i, e)| if i != key_col { Some(e) } else { None }))?;
} else {
wtr.write_byte_record(&row)?;
}
}
Ok(())
}
Expand Down
47 changes: 47 additions & 0 deletions tests/test_partition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,31 @@ TX,Fort Worth
");
}

#[test]
fn partition_drop() {
let wrk = Workdir::new("partition");
wrk.create("in.csv", data(true));

let mut cmd = wrk.command("partition");
cmd.arg("--drop").arg("state").arg(&wrk.path(".")).arg("in.csv");
wrk.run(&mut cmd);

part_eq!(wrk, "CA.csv", "\
city
San Francisco
");
part_eq!(wrk, "NY.csv", "\
city
Manhatten
Buffalo
");
part_eq!(wrk, "TX.csv", "\
city
Dallas
Fort Worth
");
}

#[test]
fn partition_without_headers() {
let wrk = Workdir::new("partition_without_headers");
Expand All @@ -68,6 +93,28 @@ TX,Fort Worth
");
}

#[test]
fn partition_drop_without_headers() {
let wrk = Workdir::new("partition_without_headers");
wrk.create("in.csv", data(false));

let mut cmd = wrk.command("partition");
cmd.arg("--drop").arg("--no-headers").arg("1").arg(&wrk.path(".")).arg("in.csv");
wrk.run(&mut cmd);

part_eq!(wrk, "CA.csv", "\
San Francisco
");
part_eq!(wrk, "NY.csv", "\
Manhatten
Buffalo
");
part_eq!(wrk, "TX.csv", "\
Dallas
Fort Worth
");
}

#[test]
fn partition_into_new_directory() {
let wrk = Workdir::new("partition_into_new_directory");
Expand Down

0 comments on commit bc730d7

Please sign in to comment.