Skip to main content

datafusion_catalog/
default_table_source.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//! Default TableSource implementation used in DataFusion physical plans
6
7use std::borrow::Cow;
8use std::sync::Arc;
9
10use crate::TableProvider;
11
12use arrow::datatypes::SchemaRef;
13use datafusion_common::{Constraints, internal_err};
14use datafusion_expr::{Expr, TableProviderFilterPushDown, TableSource, TableType};
15
16/// Implements [`TableSource`] for a [`TableProvider`]
17///
18/// This structure adapts a [`TableProvider`] (a physical plan trait) to the
19/// [`TableSource`] (logical plan trait).
20///
21/// It is used so logical plans in the `datafusion_expr` crate do not have a
22/// direct dependency on physical plans, such as [`TableProvider`]s.
23pub struct DefaultTableSource {
24    /// table provider
25    pub table_provider: Arc<dyn TableProvider>,
26}
27
28impl DefaultTableSource {
29    /// Create a new DefaultTableSource to wrap a TableProvider
30    pub fn new(table_provider: Arc<dyn TableProvider>) -> Self {
31        Self { table_provider }
32    }
33}
34
35impl TableSource for DefaultTableSource {
36    /// Get a reference to the schema for this table
37    fn schema(&self) -> SchemaRef {
38        self.table_provider.schema()
39    }
40
41    /// Get a reference to applicable constraints, if any exists.
42    fn constraints(&self) -> Option<&Constraints> {
43        self.table_provider.constraints()
44    }
45
46    /// Get the type of this table for metadata/catalog purposes.
47    fn table_type(&self) -> TableType {
48        self.table_provider.table_type()
49    }
50
51    /// Tests whether the table provider can make use of any or all filter expressions
52    /// to optimize data retrieval.
53    fn supports_filters_pushdown(
54        &self,
55        filter: &[&Expr],
56    ) -> datafusion_common::Result<Vec<TableProviderFilterPushDown>> {
57        self.table_provider.supports_filters_pushdown(filter)
58    }
59
60    fn supports_skip_pushdown(&self) -> bool {
61        self.table_provider.supports_skip_pushdown()
62    }
63
64    fn get_logical_plan(&'_ self) -> Option<Cow<'_, datafusion_expr::LogicalPlan>> {
65        self.table_provider.get_logical_plan()
66    }
67
68    fn get_column_default(&self, column: &str) -> Option<&Expr> {
69        self.table_provider.get_column_default(column)
70    }
71}
72
73/// Wrap TableProvider in TableSource
74pub fn provider_as_source(
75    table_provider: Arc<dyn TableProvider>,
76) -> Arc<dyn TableSource> {
77    Arc::new(DefaultTableSource::new(table_provider))
78}
79
80/// Attempt to downcast a TableSource to DefaultTableSource and access the
81/// TableProvider. This will only work with a TableSource created by DataFusion.
82pub fn source_as_provider(
83    source: &Arc<dyn TableSource>,
84) -> datafusion_common::Result<Arc<dyn TableProvider>> {
85    match source.as_ref().downcast_ref::<DefaultTableSource>() {
86        Some(source) => Ok(Arc::clone(&source.table_provider)),
87        _ => internal_err!("TableSource was not DefaultTableSource"),
88    }
89}
90
91#[test]
92fn preserves_table_type() {
93    use async_trait::async_trait;
94    use datafusion_common::DataFusionError;
95
96    #[derive(Debug)]
97    struct TestTempTable;
98
99    #[async_trait]
100    impl TableProvider for TestTempTable {
101        fn table_type(&self) -> TableType {
102            TableType::Temporary
103        }
104
105        fn schema(&self) -> SchemaRef {
106            unimplemented!()
107        }
108
109        async fn scan(
110            &self,
111            _: &dyn crate::Session,
112            _: Option<&[usize]>,
113            _: &[Expr],
114            _: Option<usize>,
115        ) -> Result<Arc<dyn datafusion_physical_plan::ExecutionPlan>, DataFusionError>
116        {
117            unimplemented!()
118        }
119    }
120
121    let table_source = DefaultTableSource::new(Arc::new(TestTempTable));
122    assert_eq!(table_source.table_type(), TableType::Temporary);
123}