Skip to main content

datafusion_catalog/
empty.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//! [`EmptyTable`] useful for testing.
6
7use std::sync::Arc;
8
9use arrow::datatypes::*;
10use async_trait::async_trait;
11use datafusion_common::{Result, project_schema};
12use datafusion_expr::{Expr, TableType};
13use datafusion_physical_plan::ExecutionPlan;
14use datafusion_physical_plan::empty::EmptyExec;
15
16use crate::Session;
17use crate::TableProvider;
18
19/// An empty plan that is useful for testing and generating plans
20/// without mapping them to actual data.
21#[derive(Debug)]
22pub struct EmptyTable {
23    schema: SchemaRef,
24    partitions: usize,
25}
26
27impl EmptyTable {
28    /// Initialize a new `EmptyTable` from a schema.
29    pub fn new(schema: SchemaRef) -> Self {
30        Self {
31            schema,
32            partitions: 1,
33        }
34    }
35
36    /// Creates a new EmptyTable with specified partition number.
37    pub fn with_partitions(mut self, partitions: usize) -> Self {
38        self.partitions = partitions;
39        self
40    }
41}
42
43#[async_trait]
44impl TableProvider for EmptyTable {
45    fn schema(&self) -> SchemaRef {
46        Arc::clone(&self.schema)
47    }
48
49    fn table_type(&self) -> TableType {
50        TableType::Base
51    }
52
53    async fn scan(
54        &self,
55        _state: &dyn Session,
56        projection: Option<&[usize]>,
57        _filters: &[Expr],
58        _limit: Option<usize>,
59    ) -> Result<Arc<dyn ExecutionPlan>> {
60        // even though there is no data, projections apply
61        let projected_schema = project_schema(&self.schema, projection)?;
62        Ok(Arc::new(
63            EmptyExec::new(projected_schema).with_partitions(self.partitions),
64        ))
65    }
66}