forked from dathere/qsv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoinp.rs
225 lines (196 loc) · 7.84 KB
/
joinp.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
static USAGE: &str = r#"
Joins two sets of CSV data on the specified columns using the Pola.rs engine.
The default join operation is an 'inner' join. This corresponds to the
intersection of rows on the keys specified.
The columns arguments specify the columns to join for each input. Columns can
be referenced by name. Specify multiple columns by separating them with a comma.
Both columns1 and columns2 must specify exactly the same number of columns.
Returns the shape of the join result (number of rows, number of columns) to stderr.
Usage:
qsv joinp [options] <columns1> <input1> <columns2> <input2>
qsv joinp --cross <input1> <input2>
qsv joinp --help
joinp arguments:
Both <input1> and <input2> files need to have headers. Stdin is not supported.
joinp options:
--left Do a 'left outer' join. This returns all rows in
first CSV data set, including rows with no
corresponding row in the second data set. When no
corresponding row exists, it is padded out with
empty fields.
--outer Do a 'full outer' join. This returns all rows in
both data sets with matching records joined. If
there is no match, the missing side will be padded
out with empty fields.
--cross USE WITH CAUTION.
This returns the cartesian product of the CSV
data sets given. The number of rows return is
equal to N * M, where N and M correspond to the
number of rows in the given data sets, respectively.
The columns1 and columns2 arguments are ignored.
--semi This returns only the rows in the first CSV data set
that have a corresponding row in the second CSV data
set. The output schema is the same as the first data set.
--anti This returns only the rows in the first CSV data set
that do not have a corresponding row in the second
CSV data set. The output schema is the same as the
first dataset.
--nulls When set, joins will work on empty fields.
Otherwise, empty fields are completely ignored.
Common options:
-h, --help Display this message
-o, --output <file> Write output to <file> instead of stdout.
-d, --delimiter <arg> The field delimiter for reading CSV data.
Must be a single character. (default: ,)
-Q, --quiet Do not return join shape to stderr.
"#;
use std::{
fs::File,
io::{self, Write},
path::Path,
str,
};
use polars::{
frame::hash_join::JoinType,
prelude::{CsvWriter, LazyCsvReader, LazyFileListReader, LazyFrame, SerWriter},
};
use serde::Deserialize;
use crate::{config::Delimiter, util, CliError, CliResult};
#[derive(Deserialize)]
struct Args {
arg_columns1: String,
arg_input1: String,
arg_columns2: String,
arg_input2: String,
flag_left: bool,
flag_outer: bool,
flag_cross: bool,
flag_semi: bool,
flag_anti: bool,
flag_output: Option<String>,
flag_nulls: bool,
flag_delimiter: Option<Delimiter>,
flag_quiet: bool,
}
impl From<polars::error::PolarsError> for CliError {
fn from(err: polars::error::PolarsError) -> CliError {
CliError::Other(format!("Polars error: {err:?}"))
}
}
pub fn run(argv: &[&str]) -> CliResult<()> {
let args: Args = util::get_args(USAGE, argv)?;
let join = args.new_join()?;
let join_shape = match (
args.flag_left,
args.flag_outer,
args.flag_cross,
args.flag_semi,
args.flag_anti,
) {
(false, false, false, false, false) => join.polars_join(JoinType::Inner),
(true, false, false, false, false) => join.polars_join(JoinType::Left),
(false, true, false, false, false) => join.polars_join(JoinType::Outer),
(false, false, true, false, false) => join.polars_join(JoinType::Cross),
(false, false, false, true, false) => join.polars_join(JoinType::Semi),
(false, false, false, false, true) => join.polars_join(JoinType::Anti),
_ => fail!("Please pick exactly one join operation."),
}?;
if !args.flag_quiet {
eprintln!("{join_shape:?}");
}
Ok(())
}
struct JoinStruct {
lf1: LazyFrame,
sel1: String,
lf2: LazyFrame,
sel2: String,
output: Option<String>,
delim: u8,
}
impl JoinStruct {
fn polars_join(self, jointype: JoinType) -> CliResult<(usize, usize)> {
let selcols1: Vec<_> = self.sel1.split(',').map(polars::lazy::dsl::col).collect();
let selcols2: Vec<_> = self.sel2.split(',').map(polars::lazy::dsl::col).collect();
let selcols1_len = selcols1.len();
let selcols2_len = selcols2.len();
if selcols1_len != selcols2_len {
return fail_clierror!(
"Both columns1 ({selcols1:?}) and columns2 ({selcols2:?}) must specify the same \
number of columns ({selcols1_len } != {selcols2_len})."
);
}
let optimize_all = polars::lazy::frame::OptState {
projection_pushdown: true,
predicate_pushdown: true,
type_coercion: true,
simplify_expr: true,
file_caching: true,
slice_pushdown: true,
streaming: true,
};
let mut join_results = if jointype == JoinType::Cross {
self.lf1
.with_optimizations(optimize_all)
.join_builder()
.with(self.lf2.with_optimizations(optimize_all))
.how(JoinType::Cross)
.force_parallel(true)
.finish()
.collect()?
} else {
self.lf1
.with_optimizations(optimize_all)
.join_builder()
.with(self.lf2.with_optimizations(optimize_all))
.left_on(selcols1)
.right_on(selcols2)
.how(jointype)
.force_parallel(true)
.finish()
.collect()?
};
// no need to use buffered writer here, as CsvWriter already does that
let mut out_writer = match self.output {
Some(output_file) => {
let path = Path::new(&output_file);
Box::new(File::create(path).unwrap()) as Box<dyn Write>
}
None => Box::new(io::stdout()) as Box<dyn Write>,
};
// shape is the number of rows and columns
let join_shape = join_results.shape();
CsvWriter::new(&mut out_writer)
.has_header(true)
.with_delimiter(self.delim)
.finish(&mut join_results)?;
Ok(join_shape)
}
}
impl Args {
fn new_join(&self) -> CliResult<JoinStruct> {
let delim = if let Some(delimiter) = self.flag_delimiter {
delimiter.as_byte()
} else {
b','
};
let lf1 = LazyCsvReader::new(&self.arg_input1)
.has_header(true)
.with_missing_is_null(self.flag_nulls)
.with_delimiter(delim)
.finish()?;
let lf2 = LazyCsvReader::new(&self.arg_input2)
.has_header(true)
.with_missing_is_null(self.flag_nulls)
.with_delimiter(delim)
.finish()?;
Ok(JoinStruct {
lf1,
sel1: self.arg_columns1.clone(),
lf2,
sel2: self.arg_columns2.clone(),
output: self.flag_output.clone(),
delim,
})
}
}