-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathread.rs
195 lines (166 loc) · 6.01 KB
/
read.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
use crate::api::Result;
use crate::command::args;
use crate::output::{OutputFormat, OutputWriter};
use crate::reader::ParquetFile;
use clap::{App, Arg, ArgMatches, SubCommand};
use std::io::Write;
pub fn def() -> App<'static> {
SubCommand::with_name("read")
.about("Read rows from parquet")
.arg(
Arg::with_name("columns")
.help("Select columns from parquet")
.takes_value(true)
.long("columns")
.multiple(true)
.short('c'),
)
.arg(
Arg::with_name("search")
.validator(args::validate_filter)
.help("Search columns")
.takes_value(true)
.long("search")
.multiple(true)
.short('s'),
)
.arg(
Arg::with_name("limit")
.validator(args::validate_number)
.help("Max number of rows")
.default_value("500")
.long("limit")
.short('l'),
)
.arg(
Arg::with_name("format")
.help("Output format")
.possible_values(OutputFormat::values())
.default_value("table")
.long("format")
.short('f'),
)
.arg(
Arg::with_name("path")
.validator(args::validate_path)
.help("Path to parquet")
.required(true)
.index(1),
)
}
pub fn run<W: Write>(matches: &ArgMatches, out: &mut W) -> Result<()> {
let format = args::output_format_value(matches, "format")?;
let columns = args::string_values(matches, "columns")?;
let search = args::filter_values(matches, "search")?;
let limit = args::usize_value(matches, "limit")?;
let path = args::path_value(matches, "path")?;
let parquet = ParquetFile::from(path)
.with_fields(columns)
.with_filters(search);
let headers = parquet.field_names()?;
let iter = parquet.iter().take(limit);
let mut writer = OutputWriter::new(headers, iter).format(format);
writer.write(out)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::api;
use std::io::Cursor;
use std::str;
#[test]
fn test_read_simple_messages() {
let mut output = Cursor::new(Vec::new());
let parquet = api::tests::temp_file("msg", ".parquet");
let path_str = parquet.path().to_str().unwrap();
let path = parquet.path();
let expected = vec![
"field_int32 field_int64",
"1 11",
"2 22",
"",
]
.join("\n");
let subcomand = def();
let arg_vec = vec!["read", path_str, "-l=2", "-c=field_int32,field_int64"];
let args = subcomand.get_matches_from_safe(arg_vec).unwrap();
let msgs = api::tests::create_simple_messages(3);
api::tests::write_simple_messages_parquet(path, &msgs);
assert!(run(&args, &mut output).is_ok());
let vec = output.into_inner();
let actual = str::from_utf8(&vec).unwrap();
assert_eq!(actual, expected);
}
#[test]
fn test_read_simple_messages_with_filters() {
let mut output = Cursor::new(Vec::new());
let parquet = api::tests::temp_file("msg", ".parquet");
let path_str = parquet.path().to_str().unwrap();
let path = parquet.path();
let expected = vec![
"field_int32 field_string",
"1 \"odd 11111\"",
"3 \"odd 33333\"",
"",
]
.join("\n");
let subcomand = def();
let msgs = api::tests::create_simple_messages(3);
let args = subcomand
.get_matches_from_safe(vec![
"read",
path_str,
"-s=field_string:odd",
"-c=field_int32,field_string",
])
.unwrap();
api::tests::write_simple_messages_parquet(path, &msgs);
assert!(run(&args, &mut output).is_ok());
let vec = output.into_inner();
let actual = str::from_utf8(&vec).unwrap();
assert_eq!(actual, expected);
}
#[test]
fn test_read_simple_messages_with_format_vertical() {
let mut output = Cursor::new(Vec::new());
let parquet = api::tests::temp_file("msg", ".parquet");
let path_str = parquet.path().to_str().unwrap();
let path = parquet.path();
let expected = vec![
"",
"field_int32: 1",
"field_int64: 11",
"",
"field_int32: 2",
"field_int64: 22",
"",
]
.join("\n");
let subcomand = def();
let msgs = api::tests::create_simple_messages(2);
let arg_vec = vec!["read", path_str, "-f=v", "-c=field_int32,field_int64"];
let args = subcomand.get_matches_from_safe(arg_vec).unwrap();
api::tests::write_simple_messages_parquet(path, &msgs);
assert!(run(&args, &mut output).is_ok());
let vec = output.into_inner();
let actual = str::from_utf8(&vec).unwrap();
assert_eq!(actual, expected);
}
#[test]
fn test_read_simple_messages_with_format_csv() {
let mut output = Cursor::new(Vec::new());
let parquet = api::tests::temp_file("msg", ".parquet");
let path_str = parquet.path().to_str().unwrap();
let path = parquet.path();
let subcomand = def();
let msgs = api::tests::create_simple_messages(2);
let arg_vec = vec!["read", path_str, "-f=csv", "-c=field_int32,field_string"];
let args = subcomand.get_matches_from_safe(arg_vec).unwrap();
api::tests::write_simple_messages_parquet(path, &msgs);
assert!(run(&args, &mut output).is_ok());
let vec = output.into_inner();
let actual = str::from_utf8(&vec).unwrap();
let expected = "field_int32,field_string\n1,\"odd 11111\"\n2,\"even 22222\"\n";
assert_eq!(actual, expected);
}
}