Skip to main content

datafusion_session/
schema.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//! Describes the interface and built-in implementations of schemas,
6//! representing collections of named tables.
7
8use async_trait::async_trait;
9use datafusion_common::{DataFusionError, exec_err};
10use std::any::Any;
11use std::fmt::Debug;
12use std::sync::Arc;
13
14use crate::table::TableProvider;
15use datafusion_common::Result;
16use datafusion_expr::TableType;
17
18/// Represents a schema, comprising a number of named tables.
19///
20/// Please see [`CatalogProvider`] for details of implementing a custom catalog.
21///
22/// [`CatalogProvider`]: super::CatalogProvider
23#[async_trait]
24pub trait SchemaProvider: Any + Debug + Sync + Send {
25    /// Returns the owner of the Schema, default is None. This value is reported
26    /// as part of `information_schema.schemata`.
27    fn owner_name(&self) -> Option<&str> {
28        None
29    }
30
31    /// Retrieves the list of available table names in this schema.
32    fn table_names(&self) -> Vec<String>;
33
34    /// Retrieves a specific table from the schema by name, if it exists,
35    /// otherwise returns `None`.
36    async fn table(
37        &self,
38        name: &str,
39    ) -> Result<Option<Arc<dyn TableProvider>>, DataFusionError>;
40
41    /// Retrieves the type of a specific table from the schema by name, if it exists, otherwise
42    /// returns `None`.  Implementations for which this operation is cheap but [Self::table] is
43    /// expensive can override this to improve operations that only need the type, e.g.
44    /// `SELECT * FROM information_schema.tables`.
45    async fn table_type(&self, name: &str) -> Result<Option<TableType>> {
46        self.table(name).await.map(|o| o.map(|t| t.table_type()))
47    }
48
49    /// If supported by the implementation, adds a new table named `name` to
50    /// this schema.
51    ///
52    /// If a table of the same name was already registered, returns "Table
53    /// already exists" error.
54    #[expect(unused_variables)]
55    fn register_table(
56        &self,
57        name: String,
58        table: Arc<dyn TableProvider>,
59    ) -> Result<Option<Arc<dyn TableProvider>>> {
60        exec_err!("schema provider does not support registering tables")
61    }
62
63    /// If supported by the implementation, removes the `name` table from this
64    /// schema and returns the previously registered [`TableProvider`], if any.
65    ///
66    /// If no `name` table exists, returns Ok(None).
67    #[expect(unused_variables)]
68    fn deregister_table(&self, name: &str) -> Result<Option<Arc<dyn TableProvider>>> {
69        exec_err!("schema provider does not support deregistering tables")
70    }
71
72    /// Returns true if table exist in the schema provider, false otherwise.
73    fn table_exist(&self, name: &str) -> bool;
74}
75
76impl dyn SchemaProvider {
77    /// Returns `true` if the schema provider is of type `T`.
78    ///
79    /// Prefer this over `downcast_ref::<T>().is_some()`. Works correctly when
80    /// called on `Arc<dyn SchemaProvider>` via auto-deref.
81    pub fn is<T: SchemaProvider>(&self) -> bool {
82        (self as &dyn Any).is::<T>()
83    }
84
85    /// Attempts to downcast this schema provider to a concrete type `T`,
86    /// returning `None` if the provider is not of that type.
87    ///
88    /// Works correctly when called on `Arc<dyn SchemaProvider>` via auto-deref,
89    /// unlike `(&arc as &dyn Any).downcast_ref::<T>()` which would attempt to
90    /// downcast the `Arc` itself.
91    pub fn downcast_ref<T: SchemaProvider>(&self) -> Option<&T> {
92        (self as &dyn Any).downcast_ref()
93    }
94}