-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathmain.rs
More file actions
193 lines (176 loc) · 7.46 KB
/
Copy pathmain.rs
File metadata and controls
193 lines (176 loc) · 7.46 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
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
use std::ffi::OsStr;
use std::fs;
use std::sync::Arc;
use datafusion::execution::options::{
ArrowReadOptions, AvroReadOptions, CsvReadOptions, NdJsonReadOptions, ParquetReadOptions,
};
use datafusion::prelude::SessionContext;
use datafusion_postgres::{serve, ServerOptions}; // Assuming the crate name is `datafusion_postgres`
use structopt::StructOpt;
#[derive(Debug, StructOpt)]
#[structopt(
name = "datafusion-postgres",
about = "A postgres interface for datafusion. Serve any CSV/JSON/Arrow files as tables."
)]
struct Opt {
/// CSV files to register as table, using syntax `table_name:file_path`
#[structopt(long("csv"))]
csv_tables: Vec<String>,
/// JSON files to register as table, using syntax `table_name:file_path`
#[structopt(long("json"))]
json_tables: Vec<String>,
/// Arrow files to register as table, using syntax `table_name:file_path`
#[structopt(long("arrow"))]
arrow_tables: Vec<String>,
/// Parquet files to register as table, using syntax `table_name:file_path`
#[structopt(long("parquet"))]
parquet_tables: Vec<String>,
/// Avro files to register as table, using syntax `table_name:file_path`
#[structopt(long("avro"))]
avro_tables: Vec<String>,
/// Directory to serve, all supported files will be registered as tables
#[structopt(long("dir"), short("d"))]
directory: Option<String>,
/// Port the server listens to, default to 5432
#[structopt(short, default_value = "5432")]
port: u16,
/// Host address the server listens to, default to 127.0.0.1
#[structopt(long("host"), default_value = "127.0.0.1")]
host: String,
}
fn parse_table_def(table_def: &str) -> (&str, &str) {
table_def
.split_once(':')
.expect("Use this pattern to register table: table_name:file_path")
}
impl Opt {
fn include_directory_files(&mut self) -> Result<(), Box<dyn std::error::Error>> {
if let Some(directory) = &self.directory {
match fs::read_dir(directory) {
Ok(entries) => {
for entry in entries.flatten() {
let path = entry.path();
if !path.is_file() {
continue;
}
if let Some(ext) = path.extension().and_then(OsStr::to_str) {
let ext_lower = ext.to_lowercase();
if let Some(base_name) = path.file_stem().and_then(|s| s.to_str()) {
match ext_lower.as_ref() {
"json" => {
self.json_tables.push(format!(
"{}:{}",
base_name,
path.to_string_lossy()
));
}
"avro" => {
self.avro_tables.push(format!(
"{}:{}",
base_name,
path.to_string_lossy()
));
}
"parquet" => {
self.parquet_tables.push(format!(
"{}:{}",
base_name,
path.to_string_lossy()
));
}
"csv" => {
self.csv_tables.push(format!(
"{}:{}",
base_name,
path.to_string_lossy()
));
}
"arrow" => {
self.arrow_tables.push(format!(
"{}:{}",
base_name,
path.to_string_lossy()
));
}
_ => {}
}
}
}
}
}
Err(e) => {
return Err(format!("Failed to load directory {}: {}", directory, e).into());
}
}
}
Ok(())
}
}
async fn setup_session_context(
session_context: &SessionContext,
opts: &Opt,
) -> Result<(), Box<dyn std::error::Error>> {
// Register CSV tables
for (table_name, table_path) in opts.csv_tables.iter().map(|s| parse_table_def(s.as_ref())) {
session_context
.register_csv(table_name, table_path, CsvReadOptions::default())
.await
.map_err(|e| format!("Failed to register CSV table '{}': {}", table_name, e))?;
println!("Loaded {} as table {}", table_path, table_name);
}
// Register JSON tables
for (table_name, table_path) in opts.json_tables.iter().map(|s| parse_table_def(s.as_ref())) {
session_context
.register_json(table_name, table_path, NdJsonReadOptions::default())
.await
.map_err(|e| format!("Failed to register JSON table '{}': {}", table_name, e))?;
println!("Loaded {} as table {}", table_path, table_name);
}
// Register Arrow tables
for (table_name, table_path) in opts
.arrow_tables
.iter()
.map(|s| parse_table_def(s.as_ref()))
{
session_context
.register_arrow(table_name, table_path, ArrowReadOptions::default())
.await
.map_err(|e| format!("Failed to register Arrow table '{}': {}", table_name, e))?;
println!("Loaded {} as table {}", table_path, table_name);
}
// Register Parquet tables
for (table_name, table_path) in opts
.parquet_tables
.iter()
.map(|s| parse_table_def(s.as_ref()))
{
session_context
.register_parquet(table_name, table_path, ParquetReadOptions::default())
.await
.map_err(|e| format!("Failed to register Parquet table '{}': {}", table_name, e))?;
println!("Loaded {} as table {}", table_path, table_name);
}
// Register Avro tables
for (table_name, table_path) in opts.avro_tables.iter().map(|s| parse_table_def(s.as_ref())) {
session_context
.register_avro(table_name, table_path, AvroReadOptions::default())
.await
.map_err(|e| format!("Failed to register Avro table '{}': {}", table_name, e))?;
println!("Loaded {} as table {}", table_path, table_name);
}
Ok(())
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut opts = Opt::from_args();
opts.include_directory_files()?;
let session_context = SessionContext::new();
setup_session_context(&session_context, &opts).await?;
let server_options = ServerOptions::new()
.with_host(opts.host)
.with_port(opts.port);
serve(Arc::new(session_context), &server_options)
.await
.map_err(|e| format!("Failed to run server: {}", e))?;
Ok(())
}