datafusion/datasource/provider.rs
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//! Data source traits
19
20use std::sync::Arc;
21
22use async_trait::async_trait;
23use datafusion_catalog::Session;
24use datafusion_expr::CreateExternalTable;
25pub use datafusion_expr::{TableProviderFilterPushDown, TableType};
26
27use crate::catalog::{TableProvider, TableProviderFactory};
28use crate::datasource::listing_table_factory::ListingTableFactory;
29use crate::datasource::stream::StreamTableFactory;
30use crate::error::Result;
31
32/// The default [`TableProviderFactory`]
33///
34/// If [`CreateExternalTable`] is unbounded calls [`StreamTableFactory::create`],
35/// otherwise calls [`ListingTableFactory::create`]
36#[derive(Debug, Default)]
37pub struct DefaultTableFactory {
38 stream: StreamTableFactory,
39 listing: ListingTableFactory,
40}
41
42impl DefaultTableFactory {
43 /// Creates a new [`DefaultTableFactory`]
44 pub fn new() -> Self {
45 Self::default()
46 }
47}
48
49#[async_trait]
50impl TableProviderFactory for DefaultTableFactory {
51 async fn create(
52 &self,
53 state: &dyn Session,
54 cmd: &CreateExternalTable,
55 ) -> Result<Arc<dyn TableProvider>> {
56 let mut unbounded = cmd.unbounded;
57 for (k, v) in &cmd.options {
58 if k.eq_ignore_ascii_case("unbounded") && v.eq_ignore_ascii_case("true") {
59 unbounded = true
60 }
61 }
62
63 match unbounded {
64 true => self.stream.create(state, cmd).await,
65 false => self.listing.create(state, cmd).await,
66 }
67 }
68}