|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +//! Factory for creating ListingTables with default options |
| 19 | +
|
| 20 | +use crate::datasource::datasource::TableProviderFactory; |
| 21 | +use crate::datasource::file_format::avro::AvroFormat; |
| 22 | +use crate::datasource::file_format::csv::CsvFormat; |
| 23 | +use crate::datasource::file_format::file_type::{FileType, GetExt}; |
| 24 | +use crate::datasource::file_format::json::JsonFormat; |
| 25 | +use crate::datasource::file_format::parquet::ParquetFormat; |
| 26 | +use crate::datasource::file_format::FileFormat; |
| 27 | +use crate::datasource::listing::{ |
| 28 | + ListingOptions, ListingTable, ListingTableConfig, ListingTableUrl, |
| 29 | +}; |
| 30 | +use crate::datasource::TableProvider; |
| 31 | +use crate::execution::context::SessionState; |
| 32 | +use async_trait::async_trait; |
| 33 | +use std::sync::Arc; |
| 34 | + |
| 35 | +/// A `TableProviderFactory` capable of creating new `ListingTable`s |
| 36 | +pub struct ListingTableFactory { |
| 37 | + file_type: FileType, |
| 38 | +} |
| 39 | + |
| 40 | +impl ListingTableFactory { |
| 41 | + /// Creates a new `ListingTableFactory` |
| 42 | + pub fn new(file_type: FileType) -> Self { |
| 43 | + Self { file_type } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +#[async_trait] |
| 48 | +impl TableProviderFactory for ListingTableFactory { |
| 49 | + async fn create( |
| 50 | + &self, |
| 51 | + state: &SessionState, |
| 52 | + url: &str, |
| 53 | + ) -> datafusion_common::Result<Arc<dyn TableProvider>> { |
| 54 | + let file_extension = self.file_type.get_ext(); |
| 55 | + |
| 56 | + let file_format: Arc<dyn FileFormat> = match self.file_type { |
| 57 | + FileType::CSV => Arc::new(CsvFormat::default()), |
| 58 | + FileType::PARQUET => Arc::new(ParquetFormat::default()), |
| 59 | + FileType::AVRO => Arc::new(AvroFormat::default()), |
| 60 | + FileType::JSON => Arc::new(JsonFormat::default()), |
| 61 | + }; |
| 62 | + |
| 63 | + let options = ListingOptions { |
| 64 | + format: file_format, |
| 65 | + collect_stat: true, |
| 66 | + file_extension: file_extension.to_owned(), |
| 67 | + target_partitions: 1, |
| 68 | + table_partition_cols: vec![], |
| 69 | + }; |
| 70 | + |
| 71 | + let table_path = ListingTableUrl::parse(url)?; |
| 72 | + let resolved_schema = options.infer_schema(state, &table_path).await?; |
| 73 | + let config = ListingTableConfig::new(table_path) |
| 74 | + .with_listing_options(options) |
| 75 | + .with_schema(resolved_schema); |
| 76 | + let table = ListingTable::try_new(config)?; |
| 77 | + Ok(Arc::new(table)) |
| 78 | + } |
| 79 | +} |
0 commit comments