forked from datafusion-contrib/datafusion-dft
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.rs
More file actions
443 lines (395 loc) · 20.2 KB
/
db.rs
File metadata and controls
443 lines (395 loc) · 20.2 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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// 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 std::sync::Arc;
use color_eyre::{Report, Result};
use datafusion::{
catalog::{MemoryCatalogProvider, MemorySchemaProvider},
datasource::{
file_format::{csv::CsvFormat, json::JsonFormat, parquet::ParquetFormat, FileFormat},
listing::{ListingOptions, ListingTable, ListingTableConfig, ListingTableUrl},
},
prelude::SessionContext,
};
use log::{debug, info};
use std::path::Path;
#[cfg(feature = "vortex")]
use vortex_datafusion::VortexFormat;
use crate::config::DbConfig;
/// Detects the file format based on file extension
fn detect_format(extension: &str) -> Result<(Arc<dyn FileFormat>, &'static str)> {
match extension.to_lowercase().as_str() {
"parquet" => Ok((Arc::new(ParquetFormat::new()), ".parquet")),
"csv" => Ok((Arc::new(CsvFormat::default()), ".csv")),
"json" => Ok((Arc::new(JsonFormat::default()), ".json")),
#[cfg(feature = "vortex")]
"vortex" => Ok((Arc::new(VortexFormat::default()), ".vortex")),
_ => Err(Report::msg(format!(
"Unsupported file extension: {}",
extension
))),
}
}
pub async fn register_db(ctx: &SessionContext, db_config: &DbConfig) -> Result<()> {
info!("registering tables to database");
let tables_url = db_config.path.join("tables/")?;
let listing_tables_url = ListingTableUrl::parse(tables_url.clone())?;
let store_url = listing_tables_url.object_store();
let store = ctx.runtime_env().object_store(store_url)?;
let tables_path = object_store::path::Path::from_url_path(tables_url.path())?;
let catalogs = store.list_with_delimiter(Some(&tables_path)).await?;
for catalog in catalogs.common_prefixes {
let catalog_name = catalog
.filename()
.ok_or(Report::msg("missing catalog name"))?;
info!("...handling {catalog_name} catalog");
let maybe_catalog = ctx.catalog(catalog_name);
let catalog_provider = match maybe_catalog {
Some(catalog) => catalog,
None => {
info!("...catalog does not exist, createing");
let mem_catalog_provider = Arc::new(MemoryCatalogProvider::new());
ctx.register_catalog(catalog_name, mem_catalog_provider);
ctx.catalog(catalog_name).ok_or(Report::msg(format!(
"missing catalog {catalog_name}, shouldnt be possible"
)))?
}
};
let schemas = store.list_with_delimiter(Some(&catalog)).await?;
for schema in schemas.common_prefixes {
let schema_name = schema
.filename()
.ok_or(Report::msg("missing schema name"))?;
info!("...handling {schema_name} schema");
let maybe_schema = catalog_provider.schema(schema_name);
let schema_provider = match maybe_schema {
Some(schema) => schema,
None => {
info!("...schema does not exist, creating");
let mem_schema_provider = Arc::new(MemorySchemaProvider::new());
catalog_provider.register_schema(schema_name, mem_schema_provider)?;
catalog_provider
.schema(schema_name)
.ok_or(Report::msg(format!(
"missing schema {schema_name}, shouldnt be possible"
)))?
}
};
let tables = store.list_with_delimiter(Some(&schema)).await?;
for table_path in tables.common_prefixes {
let table_name = table_path
.filename()
.ok_or(Report::msg("missing table name"))?;
info!("...handling table \"{catalog_name}.{schema_name}.{table_name}\"");
let p = tables_url
.join(&format!("{catalog_name}/"))?
.join(&format!("{schema_name}/"))?
.join(&format!("{table_name}/"))?;
let table_url = ListingTableUrl::parse(p)?;
debug!("...table url: {table_url:?}");
// List files in the table directory to detect the format
let files = store.list_with_delimiter(Some(&table_path)).await?;
// Find the first file with an extension to determine the format
let extension = files
.objects
.iter()
.find_map(|obj| {
let path = obj.location.as_ref();
Path::new(path).extension().and_then(|ext| ext.to_str())
})
.ok_or(Report::msg(format!(
"No files with extensions found in table directory: {table_name}"
)))?;
info!("...detected format: {extension}");
let (file_format, file_extension) = detect_format(extension)?;
let listing_options =
ListingOptions::new(file_format).with_file_extension(file_extension);
// Resolve the schema
let resolved_schema = listing_options
.infer_schema(&ctx.state(), &table_url)
.await?;
let config = ListingTableConfig::new(table_url)
.with_listing_options(listing_options)
.with_schema(resolved_schema);
// Create a new TableProvider
let provider = Arc::new(ListingTable::try_new(config)?);
info!("...table registered");
schema_provider.register_table(table_name.to_string(), provider)?;
}
}
}
Ok(())
}
#[cfg(test)]
mod test {
use datafusion::{
assert_batches_eq,
dataframe::DataFrameWriteOptions,
prelude::{SessionConfig, SessionContext},
};
use crate::{config::DbConfig, db::register_db};
fn setup() -> SessionContext {
let config = SessionConfig::default().with_information_schema(true);
SessionContext::new_with_config(config)
}
#[tokio::test]
async fn test_register_db_no_tables() {
let ctx = setup();
let dir = tempfile::tempdir().unwrap();
let db_path = dir.path().join("db");
let path = format!("file://{}/", db_path.to_str().unwrap());
let db_url = url::Url::parse(&path).unwrap();
let config = DbConfig { path: db_url };
register_db(&ctx, &config).await.unwrap();
let batches = ctx
.sql("SHOW TABLES")
.await
.unwrap()
.collect()
.await
.unwrap();
let expected = [
"+---------------+--------------------+-------------+------------+",
"| table_catalog | table_schema | table_name | table_type |",
"+---------------+--------------------+-------------+------------+",
"| datafusion | information_schema | tables | VIEW |",
"| datafusion | information_schema | views | VIEW |",
"| datafusion | information_schema | columns | VIEW |",
"| datafusion | information_schema | df_settings | VIEW |",
"| datafusion | information_schema | schemata | VIEW |",
"| datafusion | information_schema | routines | VIEW |",
"| datafusion | information_schema | parameters | VIEW |",
"+---------------+--------------------+-------------+------------+",
];
assert_batches_eq!(expected, &batches);
}
#[tokio::test]
async fn test_register_db_single_table() {
let ctx = setup();
let dir = tempfile::tempdir().unwrap();
let db_path = dir.path().join("db");
let path = format!("file://{}/", db_path.to_str().unwrap());
let db_url = url::Url::parse(&path).unwrap();
let config = DbConfig { path: db_url };
let data_path = db_path.join("tables").join("dft").join("stuff").join("hi");
let df = ctx.sql("SELECT 1").await.unwrap();
let write_opts = DataFrameWriteOptions::new();
df.write_parquet(data_path.as_path().to_str().unwrap(), write_opts, None)
.await
.unwrap();
register_db(&ctx, &config).await.unwrap();
let batches = ctx
.sql("SELECT * FROM information_schema.tables ORDER BY table_catalog, table_schema, table_name")
.await
.unwrap()
.collect()
.await
.unwrap();
let expected = [
"+---------------+--------------------+-------------+------------+",
"| table_catalog | table_schema | table_name | table_type |",
"+---------------+--------------------+-------------+------------+",
"| datafusion | information_schema | columns | VIEW |",
"| datafusion | information_schema | df_settings | VIEW |",
"| datafusion | information_schema | parameters | VIEW |",
"| datafusion | information_schema | routines | VIEW |",
"| datafusion | information_schema | schemata | VIEW |",
"| datafusion | information_schema | tables | VIEW |",
"| datafusion | information_schema | views | VIEW |",
"| dft | information_schema | columns | VIEW |",
"| dft | information_schema | df_settings | VIEW |",
"| dft | information_schema | parameters | VIEW |",
"| dft | information_schema | routines | VIEW |",
"| dft | information_schema | schemata | VIEW |",
"| dft | information_schema | tables | VIEW |",
"| dft | information_schema | views | VIEW |",
"| dft | stuff | hi | BASE TABLE |",
"+---------------+--------------------+-------------+------------+",
];
assert_batches_eq!(expected, &batches);
}
#[tokio::test]
async fn test_register_db_multiple_tables() {
let ctx = setup();
let dir = tempfile::tempdir().unwrap();
let db_path = dir.path().join("db");
let path = format!("file://{}/", db_path.to_str().unwrap());
let db_url = url::Url::parse(&path).unwrap();
let config = DbConfig { path: db_url };
let data_1_path = db_path.join("tables").join("dft").join("stuff").join("hi");
let data_2_path = db_path.join("tables").join("dft").join("stuff").join("bye");
let df = ctx.sql("SELECT 1").await.unwrap();
let write_opts = DataFrameWriteOptions::new();
df.clone()
.write_parquet(data_1_path.as_path().to_str().unwrap(), write_opts, None)
.await
.unwrap();
let write_opts = DataFrameWriteOptions::new();
df.write_parquet(data_2_path.as_path().to_str().unwrap(), write_opts, None)
.await
.unwrap();
register_db(&ctx, &config).await.unwrap();
let batches = ctx
.sql("SELECT * FROM information_schema.tables ORDER BY table_catalog, table_schema, table_name")
.await
.unwrap()
.collect()
.await
.unwrap();
let expected = [
"+---------------+--------------------+-------------+------------+",
"| table_catalog | table_schema | table_name | table_type |",
"+---------------+--------------------+-------------+------------+",
"| datafusion | information_schema | columns | VIEW |",
"| datafusion | information_schema | df_settings | VIEW |",
"| datafusion | information_schema | parameters | VIEW |",
"| datafusion | information_schema | routines | VIEW |",
"| datafusion | information_schema | schemata | VIEW |",
"| datafusion | information_schema | tables | VIEW |",
"| datafusion | information_schema | views | VIEW |",
"| dft | information_schema | columns | VIEW |",
"| dft | information_schema | df_settings | VIEW |",
"| dft | information_schema | parameters | VIEW |",
"| dft | information_schema | routines | VIEW |",
"| dft | information_schema | schemata | VIEW |",
"| dft | information_schema | tables | VIEW |",
"| dft | information_schema | views | VIEW |",
"| dft | stuff | bye | BASE TABLE |",
"| dft | stuff | hi | BASE TABLE |",
"+---------------+--------------------+-------------+------------+",
];
assert_batches_eq!(expected, &batches);
}
#[tokio::test]
async fn test_register_db_multiple_schemas() {
let ctx = setup();
let dir = tempfile::tempdir().unwrap();
let db_path = dir.path().join("db");
let path = format!("file://{}/", db_path.to_str().unwrap());
let db_url = url::Url::parse(&path).unwrap();
let config = DbConfig { path: db_url };
let data_1_path = db_path.join("tables").join("dft").join("stuff").join("hi");
let data_2_path = db_path
.join("tables")
.join("dft")
.join("things")
.join("bye");
let df = ctx.sql("SELECT 1").await.unwrap();
let write_opts = DataFrameWriteOptions::new();
df.clone()
.write_parquet(data_1_path.as_path().to_str().unwrap(), write_opts, None)
.await
.unwrap();
let write_opts = DataFrameWriteOptions::new();
df.write_parquet(data_2_path.as_path().to_str().unwrap(), write_opts, None)
.await
.unwrap();
register_db(&ctx, &config).await.unwrap();
let batches = ctx
.sql("SELECT * FROM information_schema.tables ORDER BY table_catalog, table_schema, table_name")
.await
.unwrap()
.collect()
.await
.unwrap();
let expected = [
"+---------------+--------------------+-------------+------------+",
"| table_catalog | table_schema | table_name | table_type |",
"+---------------+--------------------+-------------+------------+",
"| datafusion | information_schema | columns | VIEW |",
"| datafusion | information_schema | df_settings | VIEW |",
"| datafusion | information_schema | parameters | VIEW |",
"| datafusion | information_schema | routines | VIEW |",
"| datafusion | information_schema | schemata | VIEW |",
"| datafusion | information_schema | tables | VIEW |",
"| datafusion | information_schema | views | VIEW |",
"| dft | information_schema | columns | VIEW |",
"| dft | information_schema | df_settings | VIEW |",
"| dft | information_schema | parameters | VIEW |",
"| dft | information_schema | routines | VIEW |",
"| dft | information_schema | schemata | VIEW |",
"| dft | information_schema | tables | VIEW |",
"| dft | information_schema | views | VIEW |",
"| dft | stuff | hi | BASE TABLE |",
"| dft | things | bye | BASE TABLE |",
"+---------------+--------------------+-------------+------------+",
];
assert_batches_eq!(expected, &batches);
}
#[tokio::test]
async fn test_register_db_multiple_catalogs() {
let ctx = setup();
let dir = tempfile::tempdir().unwrap();
let db_path = dir.path().join("db");
let path = format!("file://{}/", db_path.to_str().unwrap());
let db_url = url::Url::parse(&path).unwrap();
let config = DbConfig { path: db_url };
let data_1_path = db_path.join("tables").join("dft2").join("stuff").join("hi");
let data_2_path = db_path
.join("tables")
.join("dft")
.join("things")
.join("bye");
let df = ctx.sql("SELECT 1").await.unwrap();
let write_opts = DataFrameWriteOptions::new();
df.clone()
.write_parquet(data_1_path.as_path().to_str().unwrap(), write_opts, None)
.await
.unwrap();
let write_opts = DataFrameWriteOptions::new();
df.write_parquet(data_2_path.as_path().to_str().unwrap(), write_opts, None)
.await
.unwrap();
register_db(&ctx, &config).await.unwrap();
let batches = ctx
.sql("SELECT * FROM information_schema.tables ORDER BY table_catalog, table_schema, table_name")
.await
.unwrap()
.collect()
.await
.unwrap();
let expected = [
"+---------------+--------------------+-------------+------------+",
"| table_catalog | table_schema | table_name | table_type |",
"+---------------+--------------------+-------------+------------+",
"| datafusion | information_schema | columns | VIEW |",
"| datafusion | information_schema | df_settings | VIEW |",
"| datafusion | information_schema | parameters | VIEW |",
"| datafusion | information_schema | routines | VIEW |",
"| datafusion | information_schema | schemata | VIEW |",
"| datafusion | information_schema | tables | VIEW |",
"| datafusion | information_schema | views | VIEW |",
"| dft | information_schema | columns | VIEW |",
"| dft | information_schema | df_settings | VIEW |",
"| dft | information_schema | parameters | VIEW |",
"| dft | information_schema | routines | VIEW |",
"| dft | information_schema | schemata | VIEW |",
"| dft | information_schema | tables | VIEW |",
"| dft | information_schema | views | VIEW |",
"| dft | things | bye | BASE TABLE |",
"| dft2 | information_schema | columns | VIEW |",
"| dft2 | information_schema | df_settings | VIEW |",
"| dft2 | information_schema | parameters | VIEW |",
"| dft2 | information_schema | routines | VIEW |",
"| dft2 | information_schema | schemata | VIEW |",
"| dft2 | information_schema | tables | VIEW |",
"| dft2 | information_schema | views | VIEW |",
"| dft2 | stuff | hi | BASE TABLE |",
"+---------------+--------------------+-------------+------------+",
];
assert_batches_eq!(expected, &batches);
}
}