datafusion_catalog/schema.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//! Describes the interface and built-in implementations of schemas,
19//! representing collections of named tables.
20
21use async_trait::async_trait;
22use datafusion_common::{exec_err, DataFusionError};
23use std::any::Any;
24use std::fmt::Debug;
25use std::sync::Arc;
26
27use crate::table::TableProvider;
28use datafusion_common::Result;
29
30/// Represents a schema, comprising a number of named tables.
31///
32/// Please see [`CatalogProvider`] for details of implementing a custom catalog.
33///
34/// [`CatalogProvider`]: super::CatalogProvider
35#[async_trait]
36pub trait SchemaProvider: Debug + Sync + Send {
37 /// Returns the owner of the Schema, default is None. This value is reported
38 /// as part of `information_tables.schemata
39 fn owner_name(&self) -> Option<&str> {
40 None
41 }
42
43 /// Returns this `SchemaProvider` as [`Any`] so that it can be downcast to a
44 /// specific implementation.
45 fn as_any(&self) -> &dyn Any;
46
47 /// Retrieves the list of available table names in this schema.
48 fn table_names(&self) -> Vec<String>;
49
50 /// Retrieves a specific table from the schema by name, if it exists,
51 /// otherwise returns `None`.
52 async fn table(
53 &self,
54 name: &str,
55 ) -> Result<Option<Arc<dyn TableProvider>>, DataFusionError>;
56
57 /// If supported by the implementation, adds a new table named `name` to
58 /// this schema.
59 ///
60 /// If a table of the same name was already registered, returns "Table
61 /// already exists" error.
62 #[allow(unused_variables)]
63 fn register_table(
64 &self,
65 name: String,
66 table: Arc<dyn TableProvider>,
67 ) -> Result<Option<Arc<dyn TableProvider>>> {
68 exec_err!("schema provider does not support registering tables")
69 }
70
71 /// If supported by the implementation, removes the `name` table from this
72 /// schema and returns the previously registered [`TableProvider`], if any.
73 ///
74 /// If no `name` table exists, returns Ok(None).
75 #[allow(unused_variables)]
76 fn deregister_table(&self, name: &str) -> Result<Option<Arc<dyn TableProvider>>> {
77 exec_err!("schema provider does not support deregistering tables")
78 }
79
80 /// Returns true if table exist in the schema provider, false otherwise.
81 fn table_exist(&self, name: &str) -> bool;
82}