Skip to main content

TableProvider

Trait TableProvider 

Source
pub trait TableProvider:
    Any
    + Debug
    + Sync
    + Send {
Show 16 methods // Required methods fn schema(&self) -> SchemaRef; fn table_type(&self) -> TableType; fn scan<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, state: &'life1 dyn Session, projection: Option<&'life2 [usize]>, filters: &'life3 [Expr], limit: Option<usize>, ) -> Pin<Box<dyn Future<Output = Result<Arc<dyn ExecutionPlan>>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait; // Provided methods fn constraints(&self) -> Option<&Constraints> { ... } fn get_table_definition(&self) -> Option<&str> { ... } fn get_logical_plan(&self) -> Option<Cow<'_, LogicalPlan>> { ... } fn get_column_default(&self, _column: &str) -> Option<&Expr> { ... } fn scan_with_args<'a, 'life0, 'life1, 'async_trait>( &'life0 self, state: &'life1 dyn Session, args: ScanArgs<'a>, ) -> BoxFuture<'async_trait, Result<ScanResult>> where Self: 'async_trait, 'a: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait { ... } fn supports_skip_pushdown(&self) -> bool { ... } fn supports_filters_pushdown( &self, filters: &[&Expr], ) -> Result<Vec<TableProviderFilterPushDown>> { ... } fn statistics(&self) -> Option<Statistics> { ... } fn insert_into<'life0, 'life1, 'async_trait>( &'life0 self, _state: &'life1 dyn Session, _input: Arc<dyn ExecutionPlan>, _insert_op: InsertOp, ) -> Pin<Box<dyn Future<Output = Result<Arc<dyn ExecutionPlan>>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait { ... } fn delete_from<'life0, 'life1, 'async_trait>( &'life0 self, _state: &'life1 dyn Session, _filters: Vec<Expr>, ) -> BoxFuture<'async_trait, Result<Arc<dyn ExecutionPlan>>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait { ... } fn update<'life0, 'life1, 'async_trait>( &'life0 self, _state: &'life1 dyn Session, _assignments: Vec<(String, Expr)>, _filters: Vec<Expr>, ) -> BoxFuture<'async_trait, Result<Arc<dyn ExecutionPlan>>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait { ... } fn truncate<'life0, 'life1, 'async_trait>( &'life0 self, _state: &'life1 dyn Session, ) -> Pin<Box<dyn Future<Output = Result<Arc<dyn ExecutionPlan>>> + Send + 'async_trait>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait { ... } fn merge_into<'life0, 'life1, 'async_trait>( &'life0 self, _state: &'life1 dyn Session, _source: Arc<dyn ExecutionPlan>, _merge_schema: DFSchemaRef, _on: Expr, _clauses: Vec<MergeIntoClause>, ) -> BoxFuture<'async_trait, Result<Arc<dyn ExecutionPlan>>> where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait { ... }
}
Expand description

A table which can be queried and modified.

Please see CatalogProvider for details of implementing a custom catalog.

TableProvider represents a source of data which can provide data as Apache Arrow RecordBatches. Implementations of this trait provide important information for planning such as:

  1. Self::schema: The schema (columns and their types) of the table
  2. Self::supports_filters_pushdown: Should filters be pushed into this scan
  3. Self::supports_skip_pushdown: Should skip (offset) be pushed into this scan
  4. Self::scan: An ExecutionPlan that can read data

Required Methods§

Source

fn schema(&self) -> SchemaRef

Get a reference to the schema for this table

Source

fn table_type(&self) -> TableType

Get the type of this table for metadata/catalog purposes.

Source

fn scan<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, state: &'life1 dyn Session, projection: Option<&'life2 [usize]>, filters: &'life3 [Expr], limit: Option<usize>, ) -> Pin<Box<dyn Future<Output = Result<Arc<dyn ExecutionPlan>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait,

Create an ExecutionPlan for scanning the table with optional projection, filter, and limit, described below.

The returned ExecutionPlan is responsible for scanning the datasource’s partitions in a streaming, parallelized fashion.

§Projection

If specified, only a subset of columns should be returned, in the order specified. The projection is a set of indexes of the fields in Self::schema.

DataFusion provides the projection so the scan reads only the columns actually used in the query, an optimization called “Projection Pushdown”. Some datasources, such as Parquet, can use this information to go significantly faster when only a subset of columns is required.

§Filters

A list of boolean filter Exprs to evaluate during the scan, in the manner specified by Self::supports_filters_pushdown. Only rows for which all of the Exprs evaluate to true must be returned (that is, the expressions are ANDed together).

To enable filter pushdown, override Self::supports_filters_pushdown. The default implementation does not push down filters, and filters will be empty.

DataFusion pushes filters into scans whenever possible (“Filter Pushdown”). Depending on the data format and implementation, evaluating predicates during the scan can significantly improve performance.

§Note: Some columns may appear only in Filters

In some cases, a query may use a column only in a filter and the projection will not contain all columns referenced by the filter expressions.

For example, given the query SELECT t.a FROM t WHERE t.b > 5,

┌────────────────────┐
│  Projection(t.a)   │
└────────────────────┘
           ▲
           │
           │
┌────────────────────┐     Filter     ┌────────────────────┐   Projection    ┌────────────────────┐
│  Filter(t.b > 5)   │────Pushdown──▶ │  Projection(t.a)   │ ───Pushdown───▶ │  Projection(t.a)   │
└────────────────────┘                └────────────────────┘                 └────────────────────┘
           ▲                                     ▲                                      ▲
           │                                     │                                      │
           │                                     │                           ┌────────────────────┐
┌────────────────────┐                ┌────────────────────┐                 │        Scan        │
│        Scan        │                │        Scan        │                 │  filter=(t.b > 5)  │
└────────────────────┘                │  filter=(t.b > 5)  │                 │  projection=(t.a)  │
                                      └────────────────────┘                 └────────────────────┘

Initial Plan                  If `TableProviderFilterPushDown`           Projection pushdown notes that
                              returns true, filter pushdown              the scan only needs t.a
                              pushes the filter into the scan
                                                                         BUT internally evaluating the
                                                                         predicate still requires t.b
§Limit

If limit is specified, the scan must produce at most this many rows, though it may return less. Like Projection Pushdown and Filter Pushdown, DataFusion pushes LIMITs as far down in the plan as possible. This is called “Limit Pushdown”, and some sources can use the information to improve performance.

Note: If any pushed-down filters are Inexact, the LIMIT cannot be pushed down. Inexact filters do not guarantee that every filtered row is removed, so applying the limit could leave too few rows to return in the final result.

§Evaluation Order

The logical evaluation order is filters, then limit, then projection.

Note that limit applies to the filtered result, not to the unfiltered input, and projection affects only which columns are returned, not which rows qualify.

For example, if a scan receives:

  • projection = [a]
  • filters = [b > 5]
  • limit = Some(3)

It must logically produce results equivalent to:

PROJECTION a (LIMIT 3 (SCAN WHERE b > 5))

As noted above, columns referenced only by pushed-down filters may be absent from projection.

§Note

Overriding TableProvider::scan_with_args will give you access to more arguments, e.g. ScanArgs::skip

Provided Methods§

Source

fn constraints(&self) -> Option<&Constraints>

Get a reference to the constraints of the table. Returns:

  • None for tables that do not support constraints.
  • Some(&Constraints) for tables supporting constraints. Therefore, a Some(&Constraints::empty()) return value indicates that this table supports constraints, but there are no constraints.
Source

fn get_table_definition(&self) -> Option<&str>

Get the create statement used to create this table, if available.

Source

fn get_logical_plan(&self) -> Option<Cow<'_, LogicalPlan>>

Get the LogicalPlan of this table, if available.

Source

fn get_column_default(&self, _column: &str) -> Option<&Expr>

Get the default value for a column, if available.

Source

fn scan_with_args<'a, 'life0, 'life1, 'async_trait>( &'life0 self, state: &'life1 dyn Session, args: ScanArgs<'a>, ) -> BoxFuture<'async_trait, Result<ScanResult>>
where Self: 'async_trait, 'a: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Create an ExecutionPlan for scanning the table using structured arguments.

This method uses ScanArgs to pass scan parameters in a structured way and returns a ScanResult containing the execution plan.

Table providers can override this method to take advantage of additional parameters like the upcoming preferred_ordering that may not be available through other scan methods.

§Arguments
  • state - The session state containing configuration and context
  • args - Structured scan arguments including projection, filters, limit, and ordering preferences
§Returns

A ScanResult containing the ExecutionPlan for scanning the table

See Self::scan for detailed documentation about projection, filters, and limits.

§Evaluation Order

The logical evaluation order is ScanArgs::filters, then ScanArgs::skip and ScanArgs::limit, and finally projection.

Note that limit and skip apply to the filtered result, not to the unfiltered input, and projection affects only which columns are returned, not which rows qualify.

For example, if a scan receives:

  • projection = [a]
  • filters = [b > 5]
  • limit = Some(3)
  • skip = Some(5)

It must logically produce results equivalent to:

PROJECTION a (OFFSET 5 LIMIT 3 (SCAN WHERE b > 5))

As noted above, columns referenced only by pushed-down filters may be absent from projection.

Source

fn supports_skip_pushdown(&self) -> bool

Specify if DataFusion should provide the offset/skip to the TableProvider to apply during the scan.

§Note

Make sure TableProvider::scan_with_args is overridden too and ScanArgs::skip is used!

Source

fn supports_filters_pushdown( &self, filters: &[&Expr], ) -> Result<Vec<TableProviderFilterPushDown>>

Specify if DataFusion should provide filter expressions to the TableProvider to apply during the scan.

Some TableProviders can evaluate filters more efficiently than the Filter operator in DataFusion, for example by using an index.

§Parameters and Return Value

The return Vec must have one element for each element of the filters argument. The value of each element indicates if the TableProvider can apply the corresponding filter during the scan. The position in the return value corresponds to the expression in the filters parameter.

If the length of the resulting Vec does not match the filters input an error will be thrown.

Each element in the resulting Vec is one of the following:

  • Exact or Inexact: The TableProvider can apply the filter during scan
  • Unsupported: The TableProvider cannot apply the filter during scan

By default, this function returns Unsupported for all filters, meaning no filters will be provided to Self::scan.

§Example
// Define a struct that implements the TableProvider trait
#[derive(Debug)]
struct TestDataSource {}

#[async_trait]
impl TableProvider for TestDataSource {
        todo!()
    // Override the supports_filters_pushdown to evaluate which expressions
    // to accept as pushdown predicates.
    fn supports_filters_pushdown(&self, filters: &[&Expr]) -> Result<Vec<TableProviderFilterPushDown>> {
        // Process each filter
        let support: Vec<_> = filters.iter().map(|expr| {
          match expr {
            // This example only supports a between expr with a single column named "c1".
            Expr::Between(between_expr) => {
                between_expr.expr
                .try_as_col()
                .map(|column| {
                    if column.name == "c1" {
                        TableProviderFilterPushDown::Exact
                    } else {
                        TableProviderFilterPushDown::Unsupported
                    }
                })
                // If there is no column in the expr set the filter to unsupported.
                .unwrap_or(TableProviderFilterPushDown::Unsupported)
            }
            _ => {
                // For all other cases return Unsupported.
                TableProviderFilterPushDown::Unsupported
            }
        }
    }).collect();
    Ok(support)
    }
}
Source

fn statistics(&self) -> Option<Statistics>

Get statistics for this table, if available Although not presently used in mainline DataFusion, this allows implementation specific behavior for downstream repositories, in conjunction with specialized optimizer rules to perform operations such as re-ordering of joins.

Source

fn insert_into<'life0, 'life1, 'async_trait>( &'life0 self, _state: &'life1 dyn Session, _input: Arc<dyn ExecutionPlan>, _insert_op: InsertOp, ) -> Pin<Box<dyn Future<Output = Result<Arc<dyn ExecutionPlan>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Return an ExecutionPlan to insert data into this table, if supported.

The returned plan should return a single row in a UInt64 column called “count” such as the following

+-------+
| count |
+-------+
| 6     |
+-------+
§See Also

See DataSinkExec for the common pattern of inserting a streams of RecordBatches as files to an ObjectStore.

Source

fn delete_from<'life0, 'life1, 'async_trait>( &'life0 self, _state: &'life1 dyn Session, _filters: Vec<Expr>, ) -> BoxFuture<'async_trait, Result<Arc<dyn ExecutionPlan>>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Delete rows matching the filter predicates.

Returns an ExecutionPlan producing one row in a single non-null UInt64 column named count, containing the number of deleted rows. The default implementation returns a “not implemented” error.

§Filters

filters contains logical Expr predicates on the target table. The planner collects them from filters and pushed-down table scan predicates, splits AND conjunctions, removes table qualifiers, and deduplicates them. For example, t.id = 1 AND t.value > 15 becomes separate id = 1 and value > 15 expressions.

Delete a row only when every predicate evaluates to true. SQL three-valued logic applies: a false or NULL predicate leaves the row unchanged. Empty filters deletes all rows, as for DELETE FROM t without a WHERE clause.

§Execution

This method is called during physical planning, including for EXPLAIN. Perform mutations when the returned plan executes, rather than while constructing it, so planning does not change the table.

§Limitations

The method receives no row limit (#24998). Subqueries in DML expressions are not fully supported (#24654); in particular, a subquery rewritten into a join can cause predicates to be lost before this method is called.

Source

fn update<'life0, 'life1, 'async_trait>( &'life0 self, _state: &'life1 dyn Session, _assignments: Vec<(String, Expr)>, _filters: Vec<Expr>, ) -> BoxFuture<'async_trait, Result<Arc<dyn ExecutionPlan>>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Update rows matching the filter predicates.

Returns an ExecutionPlan producing one row in a single non-null UInt64 column named count, containing the number of affected rows. The default implementation returns a “not implemented” error.

§Filters

filters follows the same conventions as Self::delete_from: predicates are combined with AND, table qualifiers are removed, and only rows for which every predicate is true are updated. A false or NULL predicate leaves the row unchanged. Empty filters updates all rows.

§Assignments

assignments contains (column_name, Expr) pairs from the SET clause. The planner removes identity assignments and strips table qualifiers from the expressions. Leave columns without an assignment unchanged.

Evaluate every assignment against the row values from before the statement, and only for matching rows. For example, SET a = b, b = a exchanges the two values; an expression such as 100 / divisor must not be evaluated on rows excluded by the filters.

§Execution

Like Self::delete_from, this method is called during physical planning, including for EXPLAIN. Perform mutations when the returned plan executes so planning does not change the table.

§Limitations

Subqueries have the same limitations as in Self::delete_from (#24654). UPDATE ... FROM is not supported (#19950).

Source

fn truncate<'life0, 'life1, 'async_trait>( &'life0 self, _state: &'life1 dyn Session, ) -> Pin<Box<dyn Future<Output = Result<Arc<dyn ExecutionPlan>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Remove all rows from the table.

Should return an ExecutionPlan producing a single row with count (UInt64), representing the number of rows removed.

Source

fn merge_into<'life0, 'life1, 'async_trait>( &'life0 self, _state: &'life1 dyn Session, _source: Arc<dyn ExecutionPlan>, _merge_schema: DFSchemaRef, _on: Expr, _clauses: Vec<MergeIntoClause>, ) -> BoxFuture<'async_trait, Result<Arc<dyn ExecutionPlan>>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Merge rows from a source into this table.

The source is an ExecutionPlan representing the USING clause. The merge_schema contains the target columns followed by the source columns, preserving their logical qualifiers. Providers can use this schema to resolve the logical expressions against the combined rows they construct while executing the merge. Providers should identify target fields by this leading field range rather than comparing their qualifiers with the provider’s catalog name. The on condition is the join predicate from the ON clause. The clauses describe the WHEN MATCHED / WHEN NOT MATCHED actions. These logical expressions may contain residual subqueries. Providers must either support those subqueries or return an explicit error.

Returns an ExecutionPlan producing a single row with count (UInt64).

Implementations§

Source§

impl dyn TableProvider

Source

pub fn is<T: TableProvider>(&self) -> bool

Returns true if the table provider is of type T.

Prefer this over downcast_ref::<T>().is_some(). Works correctly when called on Arc<dyn TableProvider> via auto-deref.

Source

pub fn downcast_ref<T: TableProvider>(&self) -> Option<&T>

Attempts to downcast this table provider to a concrete type T, returning None if the provider is not of that type.

Works correctly when called on Arc<dyn TableProvider> via auto-deref, unlike (&arc as &dyn Any).downcast_ref::<T>() which would attempt to downcast the Arc itself.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§