forked from apache/datafusion
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.rs
More file actions
144 lines (122 loc) · 5.15 KB
/
Copy pathconvert.rs
File metadata and controls
144 lines (122 loc) · 5.15 KB
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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
use datafusion_common::instant::Instant;
use std::fs;
use std::path::{Path, PathBuf};
use datafusion::common::not_impl_err;
use datafusion::error::Result;
use datafusion::prelude::*;
use parquet::basic::Compression;
use parquet::file::properties::WriterProperties;
use structopt::StructOpt;
use super::get_tbl_tpch_table_schema;
use super::TPCH_TABLES;
/// Convert tpch .slt files to .parquet or .csv files
#[derive(Debug, StructOpt)]
pub struct ConvertOpt {
/// Path to csv files
#[structopt(parse(from_os_str), required = true, short = "i", long = "input")]
input_path: PathBuf,
/// Output path
#[structopt(parse(from_os_str), required = true, short = "o", long = "output")]
output_path: PathBuf,
/// Output file format: `csv` or `parquet`
#[structopt(short = "f", long = "format")]
file_format: String,
/// Compression to use when writing Parquet files
#[structopt(short = "c", long = "compression", default_value = "zstd")]
compression: String,
/// Number of partitions to produce
#[structopt(short = "n", long = "partitions", default_value = "1")]
partitions: usize,
/// Batch size when reading CSV or Parquet files
#[structopt(short = "s", long = "batch-size", default_value = "8192")]
batch_size: usize,
}
impl ConvertOpt {
pub async fn run(self) -> Result<()> {
let compression = self.compression()?;
let input_path = self.input_path.to_str().unwrap();
let output_path = self.output_path.to_str().unwrap();
let output_root_path = Path::new(output_path);
for table in TPCH_TABLES {
let start = Instant::now();
let schema = get_tbl_tpch_table_schema(table);
let input_path = format!("{input_path}/{table}.tbl");
let options = CsvReadOptions::new()
.schema(&schema)
.has_header(false)
.delimiter(b'|')
.file_extension(".tbl");
let config = SessionConfig::new().with_batch_size(self.batch_size);
let ctx = SessionContext::new_with_config(config);
// build plan to read the TBL file
let mut csv = ctx.read_csv(&input_path, options).await?;
// Select all apart from the padding column
let selection = csv
.schema()
.iter()
.take(schema.fields.len() - 1)
.map(col)
.collect();
csv = csv.select(selection)?;
// optionally, repartition the file
let partitions = self.partitions;
if partitions > 1 {
csv = csv.repartition(Partitioning::RoundRobinBatch(partitions))?
}
// create the physical plan
let csv = csv.create_physical_plan().await?;
let output_path = output_root_path.join(table);
let output_path = output_path.to_str().unwrap().to_owned();
fs::create_dir_all(&output_path)?;
println!(
"Converting '{}' to {} files in directory '{}'",
&input_path, self.file_format, &output_path
);
match self.file_format.as_str() {
"csv" => ctx.write_csv(csv, output_path).await?,
"parquet" => {
let props = WriterProperties::builder()
.set_compression(compression)
.build();
ctx.write_parquet(csv, output_path, Some(props)).await?
}
other => {
return not_impl_err!("Invalid output format: {other}");
}
}
println!("Conversion completed in {} ms", start.elapsed().as_millis());
}
Ok(())
}
/// return the compression method to use when writing parquet
fn compression(&self) -> Result<Compression> {
Ok(match self.compression.as_str() {
"none" => Compression::UNCOMPRESSED,
"snappy" => Compression::SNAPPY,
"brotli" => Compression::BROTLI(Default::default()),
"gzip" => Compression::GZIP(Default::default()),
"lz4" => Compression::LZ4,
"lz0" => Compression::LZO,
"zstd" => Compression::ZSTD(Default::default()),
other => {
return not_impl_err!("Invalid compression format: {other}");
}
})
}
}