Skip to content

Commit 127705e

Browse files
committed
test TableProvider
1 parent dc9dd5e commit 127705e

File tree

1 file changed

+75
-2
lines changed
  • datafusion/core/src/test_util

1 file changed

+75
-2
lines changed

datafusion/core/src/test_util/mod.rs

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub mod csv;
2424

2525
use std::any::Any;
2626
use std::collections::HashMap;
27+
use std::fmt::{Debug, Formatter};
2728
use std::fs::File;
2829
use std::io::Write;
2930
use std::path::Path;
@@ -43,7 +44,7 @@ use crate::prelude::{CsvReadOptions, SessionContext};
4344
use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
4445
use arrow::record_batch::RecordBatch;
4546
use datafusion_catalog::Session;
46-
use datafusion_common::TableReference;
47+
use datafusion_common::{DataFusionError, TableReference};
4748
use datafusion_expr::{CreateExternalTable, Expr, SortExpr, TableType};
4849

4950
use async_trait::async_trait;
@@ -53,6 +54,8 @@ use tempfile::TempDir;
5354
#[cfg(feature = "parquet")]
5455
pub use datafusion_common::test_util::parquet_test_data;
5556
pub use datafusion_common::test_util::{arrow_test_data, get_data_dir};
57+
use datafusion_execution::TaskContext;
58+
use datafusion_physical_plan::{DisplayAs, DisplayFormatType, PlanProperties};
5659

5760
/// Scan an empty data source, mainly used in tests
5861
pub fn scan_empty(
@@ -217,7 +220,7 @@ impl TableProvider for TestTableProvider {
217220
_filters: &[Expr],
218221
_limit: Option<usize>,
219222
) -> Result<Arc<dyn ExecutionPlan>> {
220-
unimplemented!("TestTableProvider is a stub for testing.")
223+
Ok(Arc::new(TestTableExec {}))
221224
}
222225
}
223226

@@ -272,3 +275,73 @@ pub fn bounded_stream(batch: RecordBatch, limit: usize) -> SendableRecordBatchSt
272275
batch,
273276
})
274277
}
278+
279+
#[derive(Debug)]
280+
struct TestTableExec {}
281+
282+
impl DisplayAs for TestTableExec {
283+
fn fmt_as(&self, _t: DisplayFormatType, f: &mut Formatter) -> std::fmt::Result {
284+
write!(f, "TestTableExec")
285+
}
286+
}
287+
288+
impl ExecutionPlan for TestTableExec {
289+
fn name(&self) -> &str {
290+
"TestTableExec"
291+
}
292+
293+
fn as_any(&self) -> &dyn Any {
294+
self
295+
}
296+
297+
fn properties(&self) -> &PlanProperties {
298+
unimplemented!()
299+
}
300+
301+
fn children(&self) -> Vec<&Arc<dyn ExecutionPlan>> {
302+
vec![]
303+
}
304+
305+
fn with_new_children(
306+
self: Arc<Self>,
307+
_children: Vec<Arc<dyn ExecutionPlan>>,
308+
) -> Result<Arc<dyn ExecutionPlan>> {
309+
Ok(self)
310+
}
311+
312+
fn execute(
313+
&self,
314+
_partition: usize,
315+
_context: Arc<TaskContext>,
316+
) -> Result<SendableRecordBatchStream> {
317+
Err(DataFusionError::NotImplemented("for test".to_string()))
318+
}
319+
}
320+
321+
#[cfg(test)]
322+
mod tests {
323+
use crate::prelude::SessionContext;
324+
use crate::test_util::TestTableProvider;
325+
use datafusion_catalog::TableProvider;
326+
use datafusion_execution::TaskContext;
327+
use std::sync::Arc;
328+
329+
#[tokio::test]
330+
async fn table_provider_scan_err() -> datafusion_common::Result<()> {
331+
let provider = TestTableProvider {
332+
url: "test".to_string(),
333+
schema: Arc::new(arrow_schema::Schema::empty()),
334+
};
335+
let exec = provider
336+
.scan(&SessionContext::new().state(), None, &[], None)
337+
.await?;
338+
let result = exec.execute(1, Arc::new(TaskContext::default()));
339+
let error = result.err();
340+
assert_eq!(
341+
error.unwrap().strip_backtrace(),
342+
"This feature is not implemented: for test"
343+
);
344+
345+
Ok(())
346+
}
347+
}

0 commit comments

Comments
 (0)