Skip to main content

datafusion_expr/
table_source.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//! Table source
19
20use crate::{Expr, LogicalPlan};
21
22use arrow::datatypes::SchemaRef;
23use datafusion_common::{Constraints, Result};
24
25use std::{any::Any, borrow::Cow};
26
27/// Indicates how a filter expression is handled by
28/// [`TableProvider::scan`].
29///
30/// Filter expressions are boolean expressions used to reduce the number of
31/// rows that are read from a table. Only rows that evaluate to `true` ("pass
32/// the filter") are returned. Rows that evaluate to `false` or `NULL` are
33/// omitted.
34///
35/// [`TableProvider::scan`]: https://docs.rs/datafusion/latest/datafusion/datasource/trait.TableProvider.html#tymethod.scan
36#[derive(Debug, Clone, PartialEq, Eq)]
37pub enum TableProviderFilterPushDown {
38    /// The filter cannot be used by the provider and will not be pushed down.
39    Unsupported,
40    /// The filter can be used, but the provider might still return some tuples
41    /// that do not pass the filter.
42    ///
43    /// In this case, DataFusion applies an additional `Filter` operation
44    /// after the scan to ensure all rows are filtered correctly.
45    Inexact,
46    /// The provider **guarantees** that it will omit **only** tuples which
47    /// pass the filter.
48    ///
49    /// In this case, DataFusion will not apply additional filtering.
50    Exact,
51}
52
53/// Indicates the type of this table for metadata/catalog purposes.
54#[derive(Debug, Clone, Copy, PartialEq, Eq)]
55pub enum TableType {
56    /// An ordinary physical table.
57    Base,
58    /// A non-materialized table that itself uses a query internally to provide data.
59    View,
60    /// A transient table.
61    Temporary,
62}
63
64impl std::fmt::Display for TableType {
65    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66        match self {
67            TableType::Base => write!(f, "Base"),
68            TableType::View => write!(f, "View"),
69            TableType::Temporary => write!(f, "Temporary"),
70        }
71    }
72}
73
74/// Planning time information about a table.
75///
76/// This trait is used during logical query planning and optimizations, and
77/// provides a subset of the [`TableProvider`] trait, such as schema information
78/// and filter push-down capabilities. The [`TableProvider`] trait provides
79/// additional information needed for physical query execution, such as the
80/// ability to perform a scan or insert data.
81///
82/// # See Also:
83///
84/// [`DefaultTableSource`]  to go from [`TableProvider`], to `TableSource`
85///
86/// # Rationale
87///
88/// The reason for having two separate traits is to avoid having the logical
89/// plan code be dependent on the DataFusion execution engine. Some projects use
90/// DataFusion's logical plans and have their own execution engine.
91///
92/// [`TableProvider`]: https://docs.rs/datafusion/latest/datafusion/datasource/trait.TableProvider.html
93/// [`DefaultTableSource`]: https://docs.rs/datafusion/latest/datafusion/datasource/default_table_source/struct.DefaultTableSource.html
94pub trait TableSource: Any + Sync + Send {
95    /// Get a reference to the schema for this table
96    fn schema(&self) -> SchemaRef;
97
98    /// Get primary key indices, if any
99    fn constraints(&self) -> Option<&Constraints> {
100        None
101    }
102
103    /// Get the type of this table for metadata/catalog purposes.
104    fn table_type(&self) -> TableType {
105        TableType::Base
106    }
107
108    /// Tests whether the table provider can make use of any or all filter expressions
109    /// to optimize data retrieval. Only non-volatile expressions are passed to this function.
110    fn supports_filters_pushdown(
111        &self,
112        filters: &[&Expr],
113    ) -> Result<Vec<TableProviderFilterPushDown>> {
114        Ok((0..filters.len())
115            .map(|_| TableProviderFilterPushDown::Unsupported)
116            .collect())
117    }
118
119    /// Get the Logical plan of this table provider, if available.
120    ///
121    /// For example, a view may have a logical plan, but a CSV file does not.
122    fn get_logical_plan(&'_ self) -> Option<Cow<'_, LogicalPlan>> {
123        None
124    }
125
126    /// Get the default value for a column, if available.
127    fn get_column_default(&self, _column: &str) -> Option<&Expr> {
128        None
129    }
130}
131
132impl dyn TableSource {
133    pub fn is<T: TableSource>(&self) -> bool {
134        (self as &dyn Any).is::<T>()
135    }
136
137    pub fn downcast_ref<T: TableSource>(&self) -> Option<&T> {
138        (self as &dyn Any).downcast_ref()
139    }
140}