Skip to main content

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