Skip to main content

datafusion_catalog/dynamic_file/
catalog.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5//! [`DynamicFileCatalog`] that creates tables from file paths
6
7use crate::{CatalogProvider, CatalogProviderList, SchemaProvider, TableProvider};
8use async_trait::async_trait;
9use std::fmt::Debug;
10use std::sync::Arc;
11
12/// Wrap another catalog provider list
13#[derive(Debug)]
14pub struct DynamicFileCatalog {
15    /// The inner catalog provider list
16    inner: Arc<dyn CatalogProviderList>,
17    /// The factory that can create a table provider from the file path
18    factory: Arc<dyn UrlTableFactory>,
19}
20
21impl DynamicFileCatalog {
22    pub fn new(
23        inner: Arc<dyn CatalogProviderList>,
24        factory: Arc<dyn UrlTableFactory>,
25    ) -> Self {
26        Self { inner, factory }
27    }
28}
29
30impl CatalogProviderList for DynamicFileCatalog {
31    fn register_catalog(
32        &self,
33        name: String,
34        catalog: Arc<dyn CatalogProvider>,
35    ) -> Option<Arc<dyn CatalogProvider>> {
36        self.inner.register_catalog(name, catalog)
37    }
38
39    fn deregister_catalog(
40        &self,
41        name: &str,
42        cascade: bool,
43    ) -> datafusion_common::Result<Option<Arc<dyn CatalogProvider>>> {
44        self.inner.deregister_catalog(name, cascade)
45    }
46
47    fn catalog_names(&self) -> Vec<String> {
48        self.inner.catalog_names()
49    }
50
51    fn catalog(&self, name: &str) -> Option<Arc<dyn CatalogProvider>> {
52        self.inner.catalog(name).map(|catalog| {
53            Arc::new(DynamicFileCatalogProvider::new(
54                catalog,
55                Arc::clone(&self.factory),
56            )) as _
57        })
58    }
59}
60
61/// Wraps another catalog provider
62#[derive(Debug)]
63struct DynamicFileCatalogProvider {
64    /// The inner catalog provider
65    inner: Arc<dyn CatalogProvider>,
66    /// The factory that can create a table provider from the file path
67    factory: Arc<dyn UrlTableFactory>,
68}
69
70impl DynamicFileCatalogProvider {
71    pub fn new(
72        inner: Arc<dyn CatalogProvider>,
73        factory: Arc<dyn UrlTableFactory>,
74    ) -> Self {
75        Self { inner, factory }
76    }
77}
78
79impl CatalogProvider for DynamicFileCatalogProvider {
80    fn schema_names(&self) -> Vec<String> {
81        self.inner.schema_names()
82    }
83
84    fn schema(&self, name: &str) -> Option<Arc<dyn SchemaProvider>> {
85        self.inner.schema(name).map(|schema| {
86            Arc::new(DynamicFileSchemaProvider::new(
87                schema,
88                Arc::clone(&self.factory),
89            )) as _
90        })
91    }
92
93    fn register_schema(
94        &self,
95        name: &str,
96        schema: Arc<dyn SchemaProvider>,
97    ) -> datafusion_common::Result<Option<Arc<dyn SchemaProvider>>> {
98        self.inner.register_schema(name, schema)
99    }
100}
101
102/// Implements the [DynamicFileSchemaProvider] that can create tables provider from the file path.
103///
104/// The provider will try to create a table provider from the file path if the table provider
105/// isn't exist in the inner schema provider.
106#[derive(Debug)]
107pub struct DynamicFileSchemaProvider {
108    /// The inner schema provider
109    inner: Arc<dyn SchemaProvider>,
110    /// The factory that can create a table provider from the file path
111    factory: Arc<dyn UrlTableFactory>,
112}
113
114impl DynamicFileSchemaProvider {
115    /// Create a new [DynamicFileSchemaProvider] with the given inner schema provider.
116    pub fn new(
117        inner: Arc<dyn SchemaProvider>,
118        factory: Arc<dyn UrlTableFactory>,
119    ) -> Self {
120        Self { inner, factory }
121    }
122}
123
124#[async_trait]
125impl SchemaProvider for DynamicFileSchemaProvider {
126    fn table_names(&self) -> Vec<String> {
127        self.inner.table_names()
128    }
129
130    async fn table(
131        &self,
132        name: &str,
133    ) -> datafusion_common::Result<Option<Arc<dyn TableProvider>>> {
134        if let Some(table) = self.inner.table(name).await? {
135            return Ok(Some(table));
136        }
137
138        self.factory.try_new(name).await
139    }
140
141    fn register_table(
142        &self,
143        name: String,
144        table: Arc<dyn TableProvider>,
145    ) -> datafusion_common::Result<Option<Arc<dyn TableProvider>>> {
146        self.inner.register_table(name, table)
147    }
148
149    fn deregister_table(
150        &self,
151        name: &str,
152    ) -> datafusion_common::Result<Option<Arc<dyn TableProvider>>> {
153        self.inner.deregister_table(name)
154    }
155
156    fn table_exist(&self, name: &str) -> bool {
157        self.inner.table_exist(name)
158    }
159}
160
161/// [UrlTableFactory] is a factory that can create a table provider from the given url.
162#[async_trait]
163pub trait UrlTableFactory: Debug + Sync + Send {
164    /// create a new table provider from the provided url
165    async fn try_new(
166        &self,
167        url: &str,
168    ) -> datafusion_common::Result<Option<Arc<dyn TableProvider>>>;
169}