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/provider/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/provider/trait.TableProvider.html
93/// [`DefaultTableSource`]: https://docs.rs/datafusion/latest/datafusion/datasource/default_table_source/struct.DefaultTableSource.html
94pub trait TableSource: Sync + Send {
95 fn as_any(&self) -> &dyn Any;
96
97 /// Get a reference to the schema for this table
98 fn schema(&self) -> SchemaRef;
99
100 /// Get primary key indices, if any
101 fn constraints(&self) -> Option<&Constraints> {
102 None
103 }
104
105 /// Get the type of this table for metadata/catalog purposes.
106 fn table_type(&self) -> TableType {
107 TableType::Base
108 }
109
110 /// Tests whether the table provider can make use of any or all filter expressions
111 /// to optimize data retrieval. Only non-volatile expressions are passed to this function.
112 fn supports_filters_pushdown(
113 &self,
114 filters: &[&Expr],
115 ) -> Result<Vec<TableProviderFilterPushDown>> {
116 Ok((0..filters.len())
117 .map(|_| TableProviderFilterPushDown::Unsupported)
118 .collect())
119 }
120
121 /// Get the Logical plan of this table provider, if available.
122 ///
123 /// For example, a view may have a logical plan, but a CSV file does not.
124 fn get_logical_plan(&self) -> Option<Cow<LogicalPlan>> {
125 None
126 }
127
128 /// Get the default value for a column, if available.
129 fn get_column_default(&self, _column: &str) -> Option<&Expr> {
130 None
131 }
132}