Apache DataFusion SQL Query Engine
refactor: move `DataSource` to `datafusion-datasource` (#14671)
logan-keedeb66beb8d509c
diff --git a/Cargo.lock b/Cargo.lockindex 357f44c17f..716e0cf103 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -674,9 +674,9 @@ dependencies = [[[package]]name = "aws-sdk-sso"-version = "1.58.0"+version = "1.59.0"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "16ff718c9ee45cc1ebd4774a0e086bb80a6ab752b4902edf1c9f56b86ee1f770"+checksum = "00a35fc7e74f5be45839eb753568535c074a592185dd0a2d406685018d581c43"dependencies = ["aws-credential-types","aws-runtime",@@ -696,9 +696,9 @@ dependencies = [[[package]]name = "aws-sdk-ssooidc"-version = "1.59.0"+version = "1.60.0"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "5183e088715cc135d8d396fdd3bc02f018f0da4c511f53cb8d795b6a31c55809"+checksum = "f8fa655b4f313124ce272cbc38c5fef13793c832279cec750103e5e6b71a54b8"dependencies = ["aws-credential-types","aws-runtime",@@ -718,9 +718,9 @@ dependencies = [[[package]]name = "aws-sdk-sts"-version = "1.59.0"+version = "1.60.0"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "c9f944ef032717596639cea4a2118a3a457268ef51bbb5fde9637e54c465da00"+checksum = "dc1cfe5e16b90421ea031f4c6348b534ef442e76f6bf4a1b2b592c12cc2c6af9"dependencies = ["aws-credential-types","aws-runtime",@@ -741,9 +741,9 @@ dependencies = [[[package]]name = "aws-sigv4"-version = "1.2.8"+version = "1.2.9"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "0bc5bbd1e4a2648fd8c5982af03935972c24a2f9846b396de661d351ee3ce837"+checksum = "9bfe75fad52793ce6dec0dc3d4b1f388f038b5eb866c8d4d7f3a8e21b5ea5051"dependencies = ["aws-credential-types","aws-smithy-http",@@ -1078,7 +1078,7 @@ dependencies = ["hyperlocal","log","pin-project-lite",- "rustls 0.23.22",+ "rustls 0.23.23","rustls-native-certs 0.8.1","rustls-pemfile 2.2.0","rustls-pki-types",@@ -1249,9 +1249,9 @@ checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"[[package]]name = "cc"-version = "1.2.13"+version = "1.2.14"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "c7777341816418c02e033934a09f20dc0ccaf65a5201ef8a450ae0105a573fda"+checksum = "0c3d1b2e905a3a7b00a6141adb0e4c0bb941d11caf55349d863942a1cc44e3c9"dependencies = ["jobserver","libc",@@ -1643,9 +1643,9 @@ dependencies = [[[package]]name = "csv-core"-version = "0.1.11"+version = "0.1.12"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "5efa2b3d7902f4b634a20cae3c9c4e6209dc4779feb6863329607560143efa70"+checksum = "7d02f3b0da4c6504f86e9cd789d8dbafab48c2321be74e9987593de5a894d93d"dependencies = ["memchr",]@@ -1918,6 +1918,8 @@ dependencies = ["datafusion-common-runtime","datafusion-execution","datafusion-expr",+ "datafusion-physical-expr",+ "datafusion-physical-expr-common","datafusion-physical-plan","flate2","futures",@@ -2230,6 +2232,7 @@ version = "45.0.0"dependencies = ["arrow","datafusion-common",+ "datafusion-datasource","datafusion-execution","datafusion-expr","datafusion-expr-common",@@ -2563,9 +2566,9 @@ dependencies = [[[package]]name = "equivalent"-version = "1.0.1"+version = "1.0.2"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"+checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"[[package]]name = "errno"@@ -2924,9 +2927,9 @@ dependencies = [[[package]]name = "h2"-version = "0.4.7"+version = "0.4.8"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "ccae279728d634d083c00f6099cb58f01cc99c145b84b8be2f6c74618d79922e"+checksum = "5017294ff4bb30944501348f6f8e42e6ad28f42c8bbef7a74029aff064a4e3c2"dependencies = ["atomic-waker","bytes",@@ -3134,7 +3137,7 @@ dependencies = ["bytes","futures-channel","futures-util",- "h2 0.4.7",+ "h2 0.4.8","http 1.2.0","http-body 1.0.1","httparse",@@ -3187,7 +3190,7 @@ dependencies = ["http 1.2.0","hyper 1.6.0","hyper-util",- "rustls 0.23.22",+ "rustls 0.23.23","rustls-native-certs 0.8.1","rustls-pki-types","tokio",@@ -3796,9 +3799,9 @@ dependencies = [[[package]]name = "miniz_oxide"-version = "0.8.3"+version = "0.8.4"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "b8402cab7aefae129c6977bb0ff1b8fd9a04eb5b51efc50a70bea51cda0c7924"+checksum = "b3b1c9bd4fe1f0f8b387f6eb9eb3b4a1aa26185e5750efb9140301703f62cd1b"dependencies = ["adler2",]@@ -4494,9 +4497,9 @@ dependencies = [[[package]]name = "psm"-version = "0.1.24"+version = "0.1.25"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "200b9ff220857e53e184257720a14553b2f4aa02577d2ed9842d45d4b9654810"+checksum = "f58e5423e24c18cc840e1c98370b3993c6649cd1678b4d24318bcf0a083cbe88"dependencies = ["cc",]@@ -4611,7 +4614,7 @@ dependencies = ["quinn-proto","quinn-udp","rustc-hash",- "rustls 0.23.22",+ "rustls 0.23.23","socket2","thiserror 2.0.11","tokio",@@ -4629,7 +4632,7 @@ dependencies = ["rand 0.8.5","ring","rustc-hash",- "rustls 0.23.22",+ "rustls 0.23.23","rustls-pki-types","slab","thiserror 2.0.11",@@ -4640,9 +4643,9 @@ dependencies = [[[package]]name = "quinn-udp"-version = "0.5.9"+version = "0.5.10"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "1c40286217b4ba3a71d644d752e6a0b71f13f1b6a2c5311acfcbe0c2418ed904"+checksum = "e46f3055866785f6b92bc6164b76be02ca8f2eb4b002c0354b28cf4c119e5944"dependencies = ["cfg_aliases","libc",@@ -4695,8 +4698,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "3779b94aeb87e8bd4e834cee3650289ee9e0d5677f976ecdb6d219e5f4f6cd94"dependencies = ["rand_chacha 0.9.0",- "rand_core 0.9.0",- "zerocopy 0.8.17",+ "rand_core 0.9.1",+ "zerocopy 0.8.18",][[package]]@@ -4716,7 +4719,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"dependencies = ["ppv-lite86",- "rand_core 0.9.0",+ "rand_core 0.9.1",][[package]]@@ -4730,12 +4733,12 @@ dependencies = [[[package]]name = "rand_core"-version = "0.9.0"+version = "0.9.1"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "b08f3c9802962f7e1b25113931d94f43ed9725bebc59db9d0c3e9a23b67e15ff"+checksum = "a88e0da7a2c97baa202165137c158d0a2e824ac465d13d81046727b34cb247d3"dependencies = ["getrandom 0.3.1",- "zerocopy 0.8.17",+ "zerocopy 0.8.18",][[package]]@@ -4896,7 +4899,7 @@ dependencies = ["bytes","futures-core","futures-util",- "h2 0.4.7",+ "h2 0.4.8","http 1.2.0","http-body 1.0.1","http-body-util",@@ -4911,7 +4914,7 @@ dependencies = ["percent-encoding","pin-project-lite","quinn",- "rustls 0.23.22",+ "rustls 0.23.23","rustls-native-certs 0.8.1","rustls-pemfile 2.2.0","rustls-pki-types",@@ -4934,15 +4937,14 @@ dependencies = [[[package]]name = "ring"-version = "0.17.8"+version = "0.17.9"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "c17fa4cb658e3583423e915b9f3acc01cceaee1860e33d59ebae66adc3a2dc0d"+checksum = "e75ec5e92c4d8aede845126adc388046234541629e76029599ed35a003c7ed24"dependencies = ["cc","cfg-if","getrandom 0.2.15","libc",- "spin","untrusted","windows-sys 0.52.0",]@@ -5088,9 +5090,9 @@ dependencies = [[[package]]name = "rustls"-version = "0.23.22"+version = "0.23.23"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "9fb9263ab4eb695e42321db096e3b8fbd715a59b154d5c88d82db2175b681ba7"+checksum = "47796c98c480fce5406ef69d1c76378375492c3b0a0de587be0c1d9feb12f395"dependencies = ["once_cell","ring",@@ -5506,9 +5508,9 @@ dependencies = [[[package]]name = "smallvec"-version = "1.13.2"+version = "1.14.0"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"+checksum = "7fcf8323ef1faaee30a44a340193b1ac6814fd9b7b4e88e9d4519a3e4abe1cfd"[[package]]name = "snafu"@@ -5565,12 +5567,6 @@ dependencies = ["windows-sys 0.52.0",]-[[package]]-name = "spin"-version = "0.9.8"-source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"-[[package]]name = "sqllogictest"version = "0.27.2"@@ -6133,7 +6129,7 @@ version = "0.26.1"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "5f6d0975eaace0cf0fcadee4e4aaa5da15b5c079146f2cffb67c113be122bf37"dependencies = [- "rustls 0.23.22",+ "rustls 0.23.23","tokio",]@@ -6204,7 +6200,7 @@ dependencies = ["axum","base64 0.22.1","bytes",- "h2 0.4.7",+ "h2 0.4.8","http 1.2.0","http-body 1.0.1","http-body-util",@@ -6360,9 +6356,9 @@ dependencies = [[[package]]name = "typenum"-version = "1.17.0"+version = "1.18.0"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"+checksum = "1dccffe3ce07af9386bfd29e80c0ab1a8205a2fc34e4bcd40364df902cfa8f3f"[[package]]name = "typify"@@ -7094,11 +7090,11 @@ dependencies = [[[package]]name = "zerocopy"-version = "0.8.17"+version = "0.8.18"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "aa91407dacce3a68c56de03abe2760159582b846c6a4acd2f456618087f12713"+checksum = "79386d31a42a4996e3336b0919ddb90f81112af416270cff95b5f5af22b839c2"dependencies = [- "zerocopy-derive 0.8.17",+ "zerocopy-derive 0.8.18",][[package]]@@ -7114,9 +7110,9 @@ dependencies = [[[package]]name = "zerocopy-derive"-version = "0.8.17"+version = "0.8.18"source = "registry+https://github.com/rust-lang/crates.io-index"-checksum = "06718a168365cad3d5ff0bb133aad346959a2074bd4a85c121255a11304a8626"+checksum = "76331675d372f91bf8d17e13afbd5fe639200b73d01f0fc748bb059f9cca2db7"dependencies = ["proc-macro2","quote",diff --git a/datafusion-cli/src/functions.rs b/datafusion-cli/src/functions.rsindex b5bcb8243e..13d2d5fd35 100644--- a/datafusion-cli/src/functions.rs+++ b/datafusion-cli/src/functions.rs@@ -28,10 +28,10 @@ use arrow::record_batch::RecordBatch;use arrow::util::pretty::pretty_format_batches;use datafusion::catalog::{Session, TableFunctionImpl};use datafusion::common::{plan_err, Column};+use datafusion::datasource::memory::MemorySourceConfig;use datafusion::datasource::TableProvider;use datafusion::error::Result;use datafusion::logical_expr::Expr;-use datafusion::physical_plan::memory::MemorySourceConfig;use datafusion::physical_plan::ExecutionPlan;use datafusion::scalar::ScalarValue;diff --git a/datafusion-examples/examples/parquet_exec_visitor.rs b/datafusion-examples/examples/parquet_exec_visitor.rsindex 20809a1121..6c9f1a3544 100644--- a/datafusion-examples/examples/parquet_exec_visitor.rs+++ b/datafusion-examples/examples/parquet_exec_visitor.rs@@ -20,10 +20,10 @@ use std::sync::Arc;use datafusion::datasource::file_format::parquet::ParquetFormat;use datafusion::datasource::listing::{ListingOptions, PartitionedFile};use datafusion::datasource::physical_plan::{FileScanConfig, ParquetSource};+use datafusion::datasource::source::DataSourceExec;use datafusion::error::DataFusionError;use datafusion::execution::context::SessionContext;use datafusion::physical_plan::metrics::MetricValue;-use datafusion::physical_plan::source::DataSourceExec;use datafusion::physical_plan::{execute_stream, visit_execution_plan, ExecutionPlan, ExecutionPlanVisitor,};diff --git a/datafusion-examples/examples/remote_catalog.rs b/datafusion-examples/examples/remote_catalog.rsindex f84c6a0302..70c0963545 100644--- a/datafusion-examples/examples/remote_catalog.rs+++ b/datafusion-examples/examples/remote_catalog.rs@@ -36,9 +36,9 @@ use datafusion::catalog::TableProvider;use datafusion::catalog::{AsyncSchemaProvider, Session};use datafusion::common::Result;use datafusion::common::{assert_batches_eq, internal_datafusion_err, plan_err};+use datafusion::datasource::memory::MemorySourceConfig;use datafusion::execution::SendableRecordBatchStream;use datafusion::logical_expr::{Expr, TableType};-use datafusion::physical_plan::memory::MemorySourceConfig;use datafusion::physical_plan::stream::RecordBatchStreamAdapter;use datafusion::physical_plan::ExecutionPlan;use datafusion::prelude::{DataFrame, SessionContext};diff --git a/datafusion-examples/examples/simple_udtf.rs b/datafusion-examples/examples/simple_udtf.rsindex afba4c390f..d2b2d1bf96 100644--- a/datafusion-examples/examples/simple_udtf.rs+++ b/datafusion-examples/examples/simple_udtf.rs@@ -23,13 +23,13 @@ use datafusion::arrow::record_batch::RecordBatch;use datafusion::catalog::Session;use datafusion::catalog::TableFunctionImpl;use datafusion::common::{plan_err, ScalarValue};+use datafusion::datasource::memory::MemorySourceConfig;use datafusion::datasource::TableProvider;use datafusion::error::Result;use datafusion::execution::context::ExecutionProps;use datafusion::logical_expr::simplify::SimplifyContext;use datafusion::logical_expr::{Expr, TableType};use datafusion::optimizer::simplify_expressions::ExprSimplifier;-use datafusion::physical_plan::memory::MemorySourceConfig;use datafusion::physical_plan::ExecutionPlan;use datafusion::prelude::*;use std::fs::File;diff --git a/datafusion/core/Cargo.toml b/datafusion/core/Cargo.tomlindex 87a37248a3..1e0f27ccdf 100644--- a/datafusion/core/Cargo.toml+++ b/datafusion/core/Cargo.toml@@ -221,3 +221,7 @@ required-features = ["nested_expressions"][[bench]]harness = falsename = "dataframe"++[[bench]]+harness = false+name = "spm"diff --git a/datafusion/core/benches/physical_plan.rs b/datafusion/core/benches/physical_plan.rsindex 53c245ecc2..aae1457ab9 100644--- a/datafusion/core/benches/physical_plan.rs+++ b/datafusion/core/benches/physical_plan.rs@@ -33,9 +33,9 @@ use datafusion::physical_plan::sorts::sort_preserving_merge::SortPreservingMergeuse datafusion::physical_plan::{collect,expressions::{col, PhysicalSortExpr},- memory::MemorySourceConfig,};use datafusion::prelude::SessionContext;+use datafusion_datasource::memory::MemorySourceConfig;use datafusion_physical_expr_common::sort_expr::LexOrdering;// Initialize the operator using the provided record batches and the sort keydiff --git a/datafusion/core/benches/sort.rs b/datafusion/core/benches/sort.rsindex 4d71d4c56a..8f0b3753f6 100644--- a/datafusion/core/benches/sort.rs+++ b/datafusion/core/benches/sort.rs@@ -79,12 +79,13 @@ use datafusion::physical_plan::sorts::sort::SortExec;use datafusion::{execution::context::TaskContext,physical_plan::{- coalesce_partitions::CoalescePartitionsExec, memory::MemorySourceConfig,+ coalesce_partitions::CoalescePartitionsExec,sorts::sort_preserving_merge::SortPreservingMergeExec, ExecutionPlan,ExecutionPlanProperties,},prelude::SessionContext,};+use datafusion_datasource::memory::MemorySourceConfig;use datafusion_physical_expr::{expressions::col, PhysicalSortExpr};use datafusion_physical_expr_common::sort_expr::LexOrdering;diff --git a/datafusion/physical-plan/benches/spm.rs b/datafusion/core/benches/spm.rssimilarity index 98%rename from datafusion/physical-plan/benches/spm.rsrename to datafusion/core/benches/spm.rsindex 3a2ecb5739..63b06f20cd 100644--- a/datafusion/physical-plan/benches/spm.rs+++ b/datafusion/core/benches/spm.rs@@ -27,7 +27,7 @@ use datafusion_physical_plan::{collect, ExecutionPlan};use criterion::async_executor::FuturesExecutor;use criterion::{black_box, criterion_group, criterion_main, Criterion};-use datafusion_physical_plan::memory::MemorySourceConfig;+use datafusion_datasource::memory::MemorySourceConfig;fn generate_spm_for_round_robin_tie_breaker(has_same_value: bool,diff --git a/datafusion/core/src/datasource/empty.rs b/datafusion/core/src/datasource/empty.rsindex abda7fa9ec..77686c5eb7 100644--- a/datafusion/core/src/datasource/empty.rs+++ b/datafusion/core/src/datasource/empty.rs@@ -28,7 +28,8 @@ use datafusion_common::project_schema;use crate::datasource::{TableProvider, TableType};use crate::error::Result;use crate::logical_expr::Expr;-use crate::physical_plan::{empty::EmptyExec, ExecutionPlan};+use datafusion_physical_plan::empty::EmptyExec;+use datafusion_physical_plan::ExecutionPlan;/// An empty plan that is useful for testing and generating plans/// without mapping them to actual data.diff --git a/datafusion/core/src/datasource/listing/table.rs b/datafusion/core/src/datasource/listing/table.rsindex 3be8af59ea..819da155a1 100644--- a/datafusion/core/src/datasource/listing/table.rs+++ b/datafusion/core/src/datasource/listing/table.rs@@ -37,7 +37,8 @@ use datafusion_common::{config_err, DataFusionError, Result};use datafusion_expr::dml::InsertOp;use datafusion_expr::{utils::conjunction, Expr, TableProviderFilterPushDown};use datafusion_expr::{SortExpr, TableType};-use datafusion_physical_plan::{empty::EmptyExec, ExecutionPlan, Statistics};+use datafusion_physical_plan::empty::EmptyExec;+use datafusion_physical_plan::{ExecutionPlan, Statistics};use arrow::datatypes::{DataType, Field, Schema, SchemaBuilder, SchemaRef};use datafusion_common::{diff --git a/datafusion/core/src/datasource/memory.rs b/datafusion/core/src/datasource/memory.rsindex 94c6e45804..b8bec41007 100644--- a/datafusion/core/src/datasource/memory.rs+++ b/datafusion/core/src/datasource/memory.rs@@ -38,11 +38,11 @@ use arrow::datatypes::SchemaRef;use arrow::record_batch::RecordBatch;use datafusion_catalog::Session;use datafusion_common::{not_impl_err, plan_err, Constraints, DFSchema, SchemaExt};+pub use datafusion_datasource::memory::MemorySourceConfig;+pub use datafusion_datasource::source::DataSourceExec;use datafusion_execution::TaskContext;use datafusion_expr::dml::InsertOp;use datafusion_expr::SortExpr;-use datafusion_physical_plan::memory::MemorySourceConfig;-use datafusion_physical_plan::source::DataSourceExec;use async_trait::async_trait;use futures::StreamExt;diff --git a/datafusion/core/src/datasource/mod.rs b/datafusion/core/src/datasource/mod.rsindex 12dd9d7cab..96687913fb 100644--- a/datafusion/core/src/datasource/mod.rs+++ b/datafusion/core/src/datasource/mod.rs@@ -36,6 +36,8 @@ mod statistics;pub mod stream;pub mod view;+pub use datafusion_datasource::source;+// backwards compatibilitypub use self::default_table_source::{provider_as_source, source_as_provider, DefaultTableSource,diff --git a/datafusion/core/src/datasource/physical_plan/arrow_file.rs b/datafusion/core/src/datasource/physical_plan/arrow_file.rsindex 4a7cdc192c..c6e05893a9 100644--- a/datafusion/core/src/datasource/physical_plan/arrow_file.rs+++ b/datafusion/core/src/datasource/physical_plan/arrow_file.rs@@ -32,12 +32,12 @@ use arrow::datatypes::SchemaRef;use arrow_ipc::reader::FileDecoder;use datafusion_common::config::ConfigOptions;use datafusion_common::{Constraints, Statistics};+use datafusion_datasource::source::DataSourceExec;use datafusion_execution::{SendableRecordBatchStream, TaskContext};use datafusion_physical_expr::{EquivalenceProperties, Partitioning};use datafusion_physical_expr_common::sort_expr::LexOrdering;use datafusion_physical_plan::execution_plan::{Boundedness, EmissionType};use datafusion_physical_plan::metrics::{ExecutionPlanMetricsSet, MetricsSet};-use datafusion_physical_plan::source::DataSourceExec;use datafusion_physical_plan::{DisplayAs, DisplayFormatType, ExecutionPlan, PlanProperties,};diff --git a/datafusion/core/src/datasource/physical_plan/avro.rs b/datafusion/core/src/datasource/physical_plan/avro.rsindex b0a1d8c8c9..1a88dc31a6 100644--- a/datafusion/core/src/datasource/physical_plan/avro.rs+++ b/datafusion/core/src/datasource/physical_plan/avro.rs@@ -29,12 +29,12 @@ use crate::error::Result;use arrow::datatypes::SchemaRef;use datafusion_common::{Constraints, Statistics};+use datafusion_datasource::source::DataSourceExec;use datafusion_execution::{SendableRecordBatchStream, TaskContext};use datafusion_physical_expr::{EquivalenceProperties, Partitioning};use datafusion_physical_expr_common::sort_expr::LexOrdering;use datafusion_physical_plan::execution_plan::{Boundedness, EmissionType};use datafusion_physical_plan::metrics::{ExecutionPlanMetricsSet, MetricsSet};-use datafusion_physical_plan::source::DataSourceExec;use datafusion_physical_plan::{DisplayAs, DisplayFormatType, ExecutionPlan, PlanProperties,};diff --git a/datafusion/core/src/datasource/physical_plan/csv.rs b/datafusion/core/src/datasource/physical_plan/csv.rsindex c0952229b5..412c90726a 100644--- a/datafusion/core/src/datasource/physical_plan/csv.rs+++ b/datafusion/core/src/datasource/physical_plan/csv.rs@@ -37,13 +37,13 @@ use arrow::csv;use arrow::datatypes::SchemaRef;use datafusion_common::config::ConfigOptions;use datafusion_common::{Constraints, Statistics};+use datafusion_datasource::source::DataSourceExec;use datafusion_execution::{SendableRecordBatchStream, TaskContext};use datafusion_physical_expr::{EquivalenceProperties, Partitioning};use datafusion_physical_expr_common::sort_expr::LexOrdering;use datafusion_physical_plan::execution_plan::{Boundedness, EmissionType};use datafusion_physical_plan::metrics::{ExecutionPlanMetricsSet, MetricsSet};use datafusion_physical_plan::projection::ProjectionExec;-use datafusion_physical_plan::source::DataSourceExec;use datafusion_physical_plan::{DisplayAs, DisplayFormatType, PlanProperties};use futures::{StreamExt, TryStreamExt};@@ -409,7 +409,7 @@ impl ExecutionPlan for CsvExec {/// # };/// # use datafusion::datasource::physical_plan::CsvSource;/// # use datafusion_execution::object_store::ObjectStoreUrl;-/// # use datafusion_physical_plan::source::DataSourceExec;+/// # use datafusion::datasource::source::DataSourceExec;////// # let object_store_url = ObjectStoreUrl::local_filesystem();/// # let file_schema = Arc::new(Schema::empty());diff --git a/datafusion/core/src/datasource/physical_plan/file_scan_config.rs b/datafusion/core/src/datasource/physical_plan/file_scan_config.rsindex 123ecc2f95..4996b6d97b 100644--- a/datafusion/core/src/datasource/physical_plan/file_scan_config.rs+++ b/datafusion/core/src/datasource/physical_plan/file_scan_config.rs@@ -36,13 +36,13 @@ use datafusion_physical_expr::{EquivalenceProperties, LexOrdering, Partitioning}use crate::datasource::data_source::FileSource;pub use datafusion_datasource::file_scan_config::*;+use datafusion_datasource::source::{DataSource, DataSourceExec};use datafusion_execution::{SendableRecordBatchStream, TaskContext};use datafusion_physical_plan::display::{display_orderings, ProjectSchemaDisplay};use datafusion_physical_plan::metrics::ExecutionPlanMetricsSet;use datafusion_physical_plan::projection::{all_alias_free_columns, new_projections_for_columns, ProjectionExec,};-use datafusion_physical_plan::source::{DataSource, DataSourceExec};use datafusion_physical_plan::{DisplayAs, DisplayFormatType, ExecutionPlan};/// Convert type to a type suitable for use as a [`ListingTable`]diff --git a/datafusion/core/src/datasource/physical_plan/json.rs b/datafusion/core/src/datasource/physical_plan/json.rsindex 590b1cb88d..249f50efa5 100644--- a/datafusion/core/src/datasource/physical_plan/json.rs+++ b/datafusion/core/src/datasource/physical_plan/json.rs@@ -35,12 +35,12 @@ use crate::physical_plan::{ExecutionPlan, ExecutionPlanProperties};use arrow::json::ReaderBuilder;use arrow::{datatypes::SchemaRef, json};use datafusion_common::{Constraints, Statistics};+use datafusion_datasource::source::DataSourceExec;use datafusion_execution::{SendableRecordBatchStream, TaskContext};use datafusion_physical_expr::{EquivalenceProperties, Partitioning};use datafusion_physical_expr_common::sort_expr::LexOrdering;use datafusion_physical_plan::execution_plan::{Boundedness, EmissionType};use datafusion_physical_plan::metrics::{ExecutionPlanMetricsSet, MetricsSet};-use datafusion_physical_plan::source::DataSourceExec;use datafusion_physical_plan::{DisplayAs, DisplayFormatType, PlanProperties};use futures::{StreamExt, TryStreamExt};diff --git a/datafusion/core/src/datasource/physical_plan/parquet/mod.rs b/datafusion/core/src/datasource/physical_plan/parquet/mod.rsindex 4bd43cd1aa..2a2d6d7fef 100644--- a/datafusion/core/src/datasource/physical_plan/parquet/mod.rs+++ b/datafusion/core/src/datasource/physical_plan/parquet/mod.rs@@ -50,10 +50,10 @@ pub use access_plan::{ParquetAccessPlan, RowGroupAccess};use arrow::datatypes::SchemaRef;use datafusion_common::config::ConfigOptions;use datafusion_common::Constraints;+use datafusion_datasource::source::DataSourceExec;use datafusion_physical_expr::{EquivalenceProperties, LexOrdering, PhysicalExpr};use datafusion_physical_optimizer::pruning::PruningPredicate;use datafusion_physical_plan::execution_plan::{Boundedness, EmissionType};-use datafusion_physical_plan::source::DataSourceExec;pub use metrics::ParquetFileMetrics;pub use page_filter::PagePruningAccessPlanFilter;pub use reader::{DefaultParquetFileReaderFactory, ParquetFileReaderFactory};@@ -579,10 +579,10 @@ mod tests {use arrow::record_batch::RecordBatch;use bytes::{BufMut, BytesMut};use datafusion_common::{assert_contains, ScalarValue};+ use datafusion_datasource::source::DataSourceExec;use datafusion_expr::{col, lit, when, Expr};use datafusion_physical_expr::planner::logical2physical;use datafusion_physical_plan::metrics::{ExecutionPlanMetricsSet, MetricsSet};- use datafusion_physical_plan::source::DataSourceExec;use datafusion_physical_plan::{ExecutionPlan, ExecutionPlanProperties};use crate::datasource::physical_plan::parquet::source::ParquetSource;diff --git a/datafusion/core/src/datasource/physical_plan/parquet/source.rs b/datafusion/core/src/datasource/physical_plan/parquet/source.rsindex 2188111207..810a16de41 100644--- a/datafusion/core/src/datasource/physical_plan/parquet/source.rs+++ b/datafusion/core/src/datasource/physical_plan/parquet/source.rs@@ -81,7 +81,7 @@ use object_store::ObjectStore;/// # use datafusion::datasource::listing::PartitionedFile;/// # use datafusion_execution::object_store::ObjectStoreUrl;/// # use datafusion_physical_expr::expressions::lit;-/// # use datafusion_physical_plan::source::DataSourceExec;+/// # use datafusion::datasource::source::DataSourceExec;/// # use datafusion_common::config::TableParquetOptions;////// # let file_schema = Arc::new(Schema::empty());@@ -160,7 +160,7 @@ use object_store::ObjectStore;/// # use arrow::datatypes::Schema;/// # use datafusion::datasource::physical_plan::FileScanConfig;/// # use datafusion::datasource::listing::PartitionedFile;-/// # use datafusion_physical_plan::source::DataSourceExec;+/// # use datafusion::datasource::source::DataSourceExec;////// # fn parquet_exec() -> DataSourceExec { unimplemented!() }/// // Split a single DataSourceExec into multiple DataSourceExecs, one for each file@@ -202,7 +202,7 @@ use object_store::ObjectStore;/// # use datafusion::datasource::physical_plan::FileScanConfig;/// # use datafusion::datasource::physical_plan::parquet::source::ParquetSource;/// # use datafusion_execution::object_store::ObjectStoreUrl;-/// # use datafusion_physical_plan::source::DataSourceExec;+/// # use datafusion::datasource::source::DataSourceExec;////// # fn schema() -> SchemaRef {/// # Arc::new(Schema::empty())diff --git a/datafusion/core/src/physical_planner.rs b/datafusion/core/src/physical_planner.rsindex d73b7d8153..a74cdcc592 100644--- a/datafusion/core/src/physical_planner.rs+++ b/datafusion/core/src/physical_planner.rs@@ -38,7 +38,6 @@ use crate::logical_expr::{use crate::physical_expr::{create_physical_expr, create_physical_exprs};use crate::physical_plan::aggregates::{AggregateExec, AggregateMode, PhysicalGroupBy};use crate::physical_plan::analyze::AnalyzeExec;-use crate::physical_plan::empty::EmptyExec;use crate::physical_plan::explain::ExplainExec;use crate::physical_plan::expressions::PhysicalSortExpr;use crate::physical_plan::filter::FilterExec;@@ -48,7 +47,6 @@ use crate::physical_plan::joins::{};use crate::physical_plan::limit::{GlobalLimitExec, LocalLimitExec};use crate::physical_plan::projection::ProjectionExec;-use crate::physical_plan::recursive_query::RecursiveQueryExec;use crate::physical_plan::repartition::RepartitionExec;use crate::physical_plan::sorts::sort::SortExec;use crate::physical_plan::union::UnionExec;@@ -58,6 +56,8 @@ use crate::physical_plan::{displayable, windows, ExecutionPlan, ExecutionPlanProperties, InputOrderMode,Partitioning, PhysicalExpr, WindowExpr,};+use datafusion_physical_plan::empty::EmptyExec;+use datafusion_physical_plan::recursive_query::RecursiveQueryExec;use arrow::array::{builder::StringBuilder, RecordBatch};use arrow::compute::SortOptions;@@ -68,6 +68,7 @@ use datafusion_common::{exec_err, internal_datafusion_err, internal_err, not_impl_err, plan_err, DFSchema,ScalarValue,};+use datafusion_datasource::memory::MemorySourceConfig;use datafusion_expr::dml::{CopyTo, InsertOp};use datafusion_expr::expr::{physical_name, AggregateFunction, AggregateFunctionParams, Alias, GroupingSet,@@ -84,7 +85,6 @@ use datafusion_physical_expr::expressions::Literal;use datafusion_physical_expr::LexOrdering;use datafusion_physical_optimizer::PhysicalOptimizerRule;use datafusion_physical_plan::execution_plan::InvariantLevel;-use datafusion_physical_plan::memory::MemorySourceConfig;use datafusion_physical_plan::placeholder_row::PlaceholderRowExec;use datafusion_physical_plan::unnest::ListUnnest;diff --git a/datafusion/core/src/test/mod.rs b/datafusion/core/src/test/mod.rsindex ba85f9afb6..c569113a27 100644--- a/datafusion/core/src/test/mod.rs+++ b/datafusion/core/src/test/mod.rs@@ -42,7 +42,7 @@ use arrow::array::{self, Array, ArrayRef, Decimal128Builder, Int32Array};use arrow::datatypes::{DataType, Field, Schema, SchemaRef};use arrow::record_batch::RecordBatch;use datafusion_common::DataFusionError;-use datafusion_physical_plan::source::DataSourceExec;+use datafusion_datasource::source::DataSourceExec;#[cfg(feature = "compression")]use bzip2::write::BzEncoder;diff --git a/datafusion/core/src/test_util/parquet.rs b/datafusion/core/src/test_util/parquet.rsindex 0e0090ef02..fc98b43051 100644--- a/datafusion/core/src/test_util/parquet.rs+++ b/datafusion/core/src/test_util/parquet.rs@@ -37,7 +37,7 @@ use crate::physical_plan::metrics::MetricsSet;use crate::physical_plan::ExecutionPlan;use crate::prelude::{Expr, SessionConfig, SessionContext};-use datafusion_physical_plan::source::DataSourceExec;+use datafusion_datasource::source::DataSourceExec;use object_store::path::Path;use object_store::ObjectMeta;use parquet::arrow::ArrowWriter;diff --git a/datafusion/core/tests/fuzz_cases/aggregate_fuzz.rs b/datafusion/core/tests/fuzz_cases/aggregate_fuzz.rsindex 5e1f263b4c..1025a49ea1 100644--- a/datafusion/core/tests/fuzz_cases/aggregate_fuzz.rs+++ b/datafusion/core/tests/fuzz_cases/aggregate_fuzz.rs@@ -30,6 +30,8 @@ use arrow::datatypes::{};use arrow::util::pretty::pretty_format_batches;use datafusion::common::Result;+use datafusion::datasource::memory::MemorySourceConfig;+use datafusion::datasource::source::DataSourceExec;use datafusion::datasource::MemTable;use datafusion::physical_expr::aggregate::AggregateExprBuilder;use datafusion::physical_plan::aggregates::{@@ -43,8 +45,6 @@ use datafusion_functions_aggregate::sum::sum_udaf;use datafusion_physical_expr::expressions::col;use datafusion_physical_expr::PhysicalSortExpr;use datafusion_physical_expr_common::sort_expr::LexOrdering;-use datafusion_physical_plan::memory::MemorySourceConfig;-use datafusion_physical_plan::source::DataSourceExec;use datafusion_physical_plan::InputOrderMode;use test_utils::{add_empty_batches, StringBatchGenerator};diff --git a/datafusion/core/tests/fuzz_cases/join_fuzz.rs b/datafusion/core/tests/fuzz_cases/join_fuzz.rsindex 5dd29f90ef..da93dd5edf 100644--- a/datafusion/core/tests/fuzz_cases/join_fuzz.rs+++ b/datafusion/core/tests/fuzz_cases/join_fuzz.rs@@ -26,6 +26,8 @@ use arrow::datatypes::Schema;use arrow::record_batch::RecordBatch;use arrow::util::pretty::pretty_format_batches;use datafusion::common::JoinSide;+use datafusion::datasource::memory::MemorySourceConfig;+use datafusion::datasource::source::DataSourceExec;use datafusion::logical_expr::{JoinType, Operator};use datafusion::physical_expr::expressions::BinaryExpr;use datafusion::physical_plan::collect;@@ -38,8 +40,6 @@ use datafusion::prelude::{SessionConfig, SessionContext};use datafusion_common::ScalarValue;use datafusion_physical_expr::expressions::Literal;use datafusion_physical_expr::PhysicalExprRef;-use datafusion_physical_plan::memory::MemorySourceConfig;-use datafusion_physical_plan::source::DataSourceExec;use itertools::Itertools;use rand::Rng;diff --git a/datafusion/core/tests/fuzz_cases/merge_fuzz.rs b/datafusion/core/tests/fuzz_cases/merge_fuzz.rsindex 35fca789dd..92f3755250 100644--- a/datafusion/core/tests/fuzz_cases/merge_fuzz.rs+++ b/datafusion/core/tests/fuzz_cases/merge_fuzz.rs@@ -24,10 +24,10 @@ use arrow::{compute::SortOptions,record_batch::RecordBatch,};+use datafusion::datasource::memory::MemorySourceConfig;use datafusion::physical_plan::{collect,expressions::{col, PhysicalSortExpr},- memory::MemorySourceConfig,sorts::sort_preserving_merge::SortPreservingMergeExec,};use datafusion::prelude::{SessionConfig, SessionContext};diff --git a/datafusion/core/tests/fuzz_cases/sort_fuzz.rs b/datafusion/core/tests/fuzz_cases/sort_fuzz.rsindex 51a5bc87ef..0b0f0aa2f1 100644--- a/datafusion/core/tests/fuzz_cases/sort_fuzz.rs+++ b/datafusion/core/tests/fuzz_cases/sort_fuzz.rs@@ -24,6 +24,7 @@ use arrow::{compute::SortOptions,record_batch::RecordBatch,};+use datafusion::datasource::memory::MemorySourceConfig;use datafusion::execution::runtime_env::RuntimeEnvBuilder;use datafusion::physical_plan::expressions::PhysicalSortExpr;use datafusion::physical_plan::sorts::sort::SortExec;@@ -33,7 +34,6 @@ use datafusion_common::cast::as_int32_array;use datafusion_execution::memory_pool::GreedyMemoryPool;use datafusion_physical_expr::expressions::col;use datafusion_physical_expr_common::sort_expr::LexOrdering;-use datafusion_physical_plan::memory::MemorySourceConfig;use rand::Rng;use test_utils::{batches_to_vec, partitions_to_sorted_vec};diff --git a/datafusion/core/tests/fuzz_cases/sort_preserving_repartition_fuzz.rs b/datafusion/core/tests/fuzz_cases/sort_preserving_repartition_fuzz.rsindex d23408743f..06b93d41af 100644--- a/datafusion/core/tests/fuzz_cases/sort_preserving_repartition_fuzz.rs+++ b/datafusion/core/tests/fuzz_cases/sort_preserving_repartition_fuzz.rs@@ -44,9 +44,9 @@ mod sp_repartition_fuzz_tests {};use test_utils::add_empty_batches;+ use datafusion::datasource::memory::MemorySourceConfig;+ use datafusion::datasource::source::DataSourceExec;use datafusion_physical_expr_common::sort_expr::LexOrdering;- use datafusion_physical_plan::memory::MemorySourceConfig;- use datafusion_physical_plan::source::DataSourceExec;use itertools::izip;use rand::{rngs::StdRng, seq::SliceRandom, Rng, SeedableRng};diff --git a/datafusion/core/tests/fuzz_cases/window_fuzz.rs b/datafusion/core/tests/fuzz_cases/window_fuzz.rsindex 4a484221a8..a7f9e38c9a 100644--- a/datafusion/core/tests/fuzz_cases/window_fuzz.rs+++ b/datafusion/core/tests/fuzz_cases/window_fuzz.rs@@ -22,6 +22,8 @@ use arrow::compute::{concat_batches, SortOptions};use arrow::datatypes::SchemaRef;use arrow::record_batch::RecordBatch;use arrow::util::pretty::pretty_format_batches;+use datafusion::datasource::memory::MemorySourceConfig;+use datafusion::datasource::source::DataSourceExec;use datafusion::functions_window::row_number::row_number_udwf;use datafusion::physical_plan::sorts::sort::SortExec;use datafusion::physical_plan::windows::{@@ -48,8 +50,6 @@ use datafusion_functions_window::rank::{dense_rank_udwf, rank_udwf};use datafusion_physical_expr::expressions::{cast, col, lit};use datafusion_physical_expr::{PhysicalExpr, PhysicalSortExpr};use datafusion_physical_expr_common::sort_expr::LexOrdering;-use datafusion_physical_plan::memory::MemorySourceConfig;-use datafusion_physical_plan::source::DataSourceExec;use rand::distributions::Alphanumeric;use rand::rngs::StdRng;diff --git a/datafusion/core/tests/memory_limit/mod.rs b/datafusion/core/tests/memory_limit/mod.rsindex 669294d38a..a1985a1aa4 100644--- a/datafusion/core/tests/memory_limit/mod.rs+++ b/datafusion/core/tests/memory_limit/mod.rs@@ -27,6 +27,8 @@ use arrow::array::{ArrayRef, DictionaryArray, RecordBatch};use arrow::compute::SortOptions;use arrow::datatypes::{Int32Type, SchemaRef};use datafusion::assert_batches_eq;+use datafusion::datasource::memory::MemorySourceConfig;+use datafusion::datasource::source::DataSourceExec;use datafusion::datasource::{MemTable, TableProvider};use datafusion::execution::disk_manager::DiskManagerConfig;use datafusion::execution::runtime_env::RuntimeEnvBuilder;@@ -46,8 +48,6 @@ use datafusion_expr::{Expr, TableType};use datafusion_physical_expr::{LexOrdering, PhysicalSortExpr};use datafusion_physical_optimizer::join_selection::JoinSelection;use datafusion_physical_optimizer::PhysicalOptimizerRule;-use datafusion_physical_plan::memory::MemorySourceConfig;-use datafusion_physical_plan::source::DataSourceExec;use datafusion_physical_plan::spill::get_record_batch_memory_size;use test_utils::AccessLogGenerator;diff --git a/datafusion/core/tests/parquet/file_statistics.rs b/datafusion/core/tests/parquet/file_statistics.rsindex 82024a731e..ad75cf2607 100644--- a/datafusion/core/tests/parquet/file_statistics.rs+++ b/datafusion/core/tests/parquet/file_statistics.rs@@ -22,6 +22,7 @@ use datafusion::datasource::file_format::parquet::ParquetFormat;use datafusion::datasource::listing::{ListingOptions, ListingTable, ListingTableConfig, ListingTableUrl,};+use datafusion::datasource::source::DataSourceExec;use datafusion::datasource::TableProvider;use datafusion::execution::context::SessionState;use datafusion::execution::session_state::SessionStateBuilder;@@ -34,7 +35,6 @@ use datafusion_execution::cache::cache_unit::{use datafusion_execution::config::SessionConfig;use datafusion_execution::runtime_env::RuntimeEnvBuilder;use datafusion_expr::{col, lit, Expr};-use datafusion_physical_plan::source::DataSourceExec;use datafusion::datasource::physical_plan::FileScanConfig;use tempfile::tempdir;diff --git a/datafusion/core/tests/parquet/page_pruning.rs b/datafusion/core/tests/parquet/page_pruning.rsindex ea86bf3685..fe96a2eb5e 100644--- a/datafusion/core/tests/parquet/page_pruning.rs+++ b/datafusion/core/tests/parquet/page_pruning.rs@@ -25,6 +25,7 @@ use datafusion::datasource::file_format::FileFormat;use datafusion::datasource::listing::PartitionedFile;use datafusion::datasource::object_store::ObjectStoreUrl;use datafusion::datasource::physical_plan::{FileScanConfig, ParquetSource};+use datafusion::datasource::source::DataSourceExec;use datafusion::execution::context::SessionState;use datafusion::physical_plan::metrics::MetricValue;use datafusion::physical_plan::ExecutionPlan;@@ -33,7 +34,6 @@ use datafusion_common::{ScalarValue, ToDFSchema};use datafusion_expr::execution_props::ExecutionProps;use datafusion_expr::{col, lit, Expr};use datafusion_physical_expr::create_physical_expr;-use datafusion_physical_plan::source::DataSourceExec;use futures::StreamExt;use object_store::path::Path;diff --git a/datafusion/core/tests/parquet/utils.rs b/datafusion/core/tests/parquet/utils.rsindex dd5541461f..8cb50b22cf 100644--- a/datafusion/core/tests/parquet/utils.rs+++ b/datafusion/core/tests/parquet/utils.rs@@ -18,8 +18,8 @@//! Utilities for parquet testsuse datafusion::datasource::physical_plan::{FileScanConfig, ParquetSource};+use datafusion::datasource::source::DataSourceExec;use datafusion_physical_plan::metrics::MetricsSet;-use datafusion_physical_plan::source::DataSourceExec;use datafusion_physical_plan::{accept, ExecutionPlan, ExecutionPlanVisitor};/// Find the metrics from the first DataSourceExec encountered in the plandiff --git a/datafusion/core/tests/physical_optimizer/aggregate_statistics.rs b/datafusion/core/tests/physical_optimizer/aggregate_statistics.rsindex 1757c7150b..a79d743cb2 100644--- a/datafusion/core/tests/physical_optimizer/aggregate_statistics.rs+++ b/datafusion/core/tests/physical_optimizer/aggregate_statistics.rs@@ -22,6 +22,8 @@ use crate::physical_optimizer::test_utils::TestAggregate;use arrow::array::Int32Array;use arrow::datatypes::{DataType, Field, Schema};use arrow::record_batch::RecordBatch;+use datafusion::datasource::memory::MemorySourceConfig;+use datafusion::datasource::source::DataSourceExec;use datafusion_common::cast::as_int64_array;use datafusion_common::config::ConfigOptions;use datafusion_common::Result;@@ -36,9 +38,7 @@ use datafusion_physical_plan::aggregates::PhysicalGroupBy;use datafusion_physical_plan::coalesce_partitions::CoalescePartitionsExec;use datafusion_physical_plan::common;use datafusion_physical_plan::filter::FilterExec;-use datafusion_physical_plan::memory::MemorySourceConfig;use datafusion_physical_plan::projection::ProjectionExec;-use datafusion_physical_plan::source::DataSourceExec;use datafusion_physical_plan::ExecutionPlan;/// Mock data using a MemorySourceConfig which has an exact count statisticdiff --git a/datafusion/core/tests/physical_optimizer/enforce_distribution.rs b/datafusion/core/tests/physical_optimizer/enforce_distribution.rsindex 50c67f09c7..66d1380e09 100644--- a/datafusion/core/tests/physical_optimizer/enforce_distribution.rs+++ b/datafusion/core/tests/physical_optimizer/enforce_distribution.rs@@ -31,6 +31,7 @@ use datafusion::datasource::file_format::file_compression_type::FileCompressionTuse datafusion::datasource::listing::PartitionedFile;use datafusion::datasource::object_store::ObjectStoreUrl;use datafusion::datasource::physical_plan::{CsvSource, FileScanConfig, ParquetSource};+use datafusion::datasource::source::DataSourceExec;use datafusion_common::error::Result;use datafusion_common::tree_node::{Transformed, TransformedResult, TreeNode};use datafusion_common::ScalarValue;@@ -57,7 +58,6 @@ use datafusion_physical_plan::limit::{GlobalLimitExec, LocalLimitExec};use datafusion_physical_plan::projection::ProjectionExec;use datafusion_physical_plan::sorts::sort::SortExec;use datafusion_physical_plan::sorts::sort_preserving_merge::SortPreservingMergeExec;-use datafusion_physical_plan::source::DataSourceExec;use datafusion_physical_plan::union::UnionExec;use datafusion_physical_plan::ExecutionPlanProperties;use datafusion_physical_plan::PlanProperties;diff --git a/datafusion/core/tests/physical_optimizer/projection_pushdown.rs b/datafusion/core/tests/physical_optimizer/projection_pushdown.rsindex 89bd97881e..dfba57a584 100644--- a/datafusion/core/tests/physical_optimizer/projection_pushdown.rs+++ b/datafusion/core/tests/physical_optimizer/projection_pushdown.rs@@ -21,7 +21,9 @@ use std::sync::Arc;use arrow::compute::SortOptions;use arrow::datatypes::{DataType, Field, Schema, SchemaRef};use datafusion::datasource::listing::PartitionedFile;+use datafusion::datasource::memory::MemorySourceConfig;use datafusion::datasource::physical_plan::{CsvSource, FileScanConfig};+use datafusion::datasource::source::DataSourceExec;use datafusion_common::config::ConfigOptions;use datafusion_common::Result;use datafusion_common::{JoinSide, JoinType, ScalarValue};@@ -46,12 +48,10 @@ use datafusion_physical_plan::joins::{HashJoinExec, NestedLoopJoinExec, PartitionMode, StreamJoinPartitionMode,SymmetricHashJoinExec,};-use datafusion_physical_plan::memory::MemorySourceConfig;use datafusion_physical_plan::projection::{update_expr, ProjectionExec};use datafusion_physical_plan::repartition::RepartitionExec;use datafusion_physical_plan::sorts::sort::SortExec;use datafusion_physical_plan::sorts::sort_preserving_merge::SortPreservingMergeExec;-use datafusion_physical_plan::source::DataSourceExec;use datafusion_physical_plan::streaming::PartitionStream;use datafusion_physical_plan::streaming::StreamingTableExec;use datafusion_physical_plan::union::UnionExec;diff --git a/datafusion/core/tests/physical_optimizer/replace_with_order_preserving_variants.rs b/datafusion/core/tests/physical_optimizer/replace_with_order_preserving_variants.rsindex d9b569dfa6..58eb866c59 100644--- a/datafusion/core/tests/physical_optimizer/replace_with_order_preserving_variants.rs+++ b/datafusion/core/tests/physical_optimizer/replace_with_order_preserving_variants.rs@@ -32,13 +32,13 @@ use datafusion_physical_plan::coalesce_partitions::CoalescePartitionsExec;use datafusion_physical_plan::collect;use datafusion_physical_plan::filter::FilterExec;use datafusion_physical_plan::joins::{HashJoinExec, PartitionMode};-use datafusion_physical_plan::memory::MemorySourceConfig;+use datafusion::datasource::memory::MemorySourceConfig;use datafusion_physical_plan::repartition::RepartitionExec;use datafusion_physical_plan::sorts::sort::SortExec;use datafusion_physical_plan::{displayable, get_plan_string, ExecutionPlan, Partitioning,};-use datafusion_physical_plan::source::DataSourceExec;+use datafusion::datasource::source::DataSourceExec;use datafusion_common::tree_node::{TransformedResult, TreeNode};use datafusion_common::Result;use datafusion_expr::{JoinType, Operator};diff --git a/datafusion/core/tests/physical_optimizer/test_utils.rs b/datafusion/core/tests/physical_optimizer/test_utils.rsindex 162f93facc..e4d72c112c 100644--- a/datafusion/core/tests/physical_optimizer/test_utils.rs+++ b/datafusion/core/tests/physical_optimizer/test_utils.rs@@ -26,7 +26,9 @@ use arrow::compute::SortOptions;use arrow::datatypes::{DataType, Field, Schema, SchemaRef};use arrow::record_batch::RecordBatch;use datafusion::datasource::listing::PartitionedFile;+use datafusion::datasource::memory::MemorySourceConfig;use datafusion::datasource::physical_plan::{FileScanConfig, ParquetSource};+use datafusion::datasource::source::DataSourceExec;use datafusion_common::config::ConfigOptions;use datafusion_common::tree_node::{Transformed, TransformedResult, TreeNode};use datafusion_common::utils::expr::COUNT_STAR_EXPANSION;@@ -52,11 +54,9 @@ use datafusion_physical_plan::filter::FilterExec;use datafusion_physical_plan::joins::utils::{JoinFilter, JoinOn};use datafusion_physical_plan::joins::{HashJoinExec, PartitionMode, SortMergeJoinExec};use datafusion_physical_plan::limit::{GlobalLimitExec, LocalLimitExec};-use datafusion_physical_plan::memory::MemorySourceConfig;use datafusion_physical_plan::repartition::RepartitionExec;use datafusion_physical_plan::sorts::sort::SortExec;use datafusion_physical_plan::sorts::sort_preserving_merge::SortPreservingMergeExec;-use datafusion_physical_plan::source::DataSourceExec;use datafusion_physical_plan::streaming::{PartitionStream, StreamingTableExec};use datafusion_physical_plan::tree_node::PlanContext;use datafusion_physical_plan::union::UnionExec;diff --git a/datafusion/core/tests/sql/path_partition.rs b/datafusion/core/tests/sql/path_partition.rsindex 6345f5e435..1a19bfe9e8 100644--- a/datafusion/core/tests/sql/path_partition.rs+++ b/datafusion/core/tests/sql/path_partition.rs@@ -26,6 +26,7 @@ use std::sync::Arc;use arrow::datatypes::DataType;use datafusion::datasource::listing::ListingTableUrl;use datafusion::datasource::physical_plan::{FileScanConfig, ParquetSource};+use datafusion::datasource::source::DataSourceExec;use datafusion::{assert_batches_sorted_eq,datasource::{@@ -43,7 +44,6 @@ use datafusion_common::ScalarValue;use datafusion_execution::config::SessionConfig;use datafusion_expr::{col, lit, Expr, Operator};use datafusion_physical_expr::expressions::{BinaryExpr, Column, Literal};-use datafusion_physical_plan::source::DataSourceExec;use async_trait::async_trait;use bytes::Bytes;diff --git a/datafusion/core/tests/user_defined/user_defined_table_functions.rs b/datafusion/core/tests/user_defined/user_defined_table_functions.rsindex 0ec9a5fd76..618f0590ab 100644--- a/datafusion/core/tests/user_defined/user_defined_table_functions.rs+++ b/datafusion/core/tests/user_defined/user_defined_table_functions.rs@@ -26,6 +26,7 @@ use arrow::csv::ReaderBuilder;use datafusion::arrow::datatypes::SchemaRef;use datafusion::arrow::record_batch::RecordBatch;+use datafusion::datasource::memory::MemorySourceConfig;use datafusion::datasource::TableProvider;use datafusion::error::Result;use datafusion::execution::TaskContext;@@ -35,7 +36,6 @@ use datafusion_catalog::Session;use datafusion_catalog::TableFunctionImpl;use datafusion_common::{assert_batches_eq, DFSchema, ScalarValue};use datafusion_expr::{EmptyRelation, Expr, LogicalPlan, Projection, TableType};-use datafusion_physical_plan::memory::MemorySourceConfig;use async_trait::async_trait;diff --git a/datafusion/datasource/Cargo.toml b/datafusion/datasource/Cargo.tomlindex bf1f8789ac..a1d7af106f 100644--- a/datafusion/datasource/Cargo.toml+++ b/datafusion/datasource/Cargo.toml@@ -52,6 +52,8 @@ datafusion-common = { workspace = true, features = ["object_store"] }datafusion-common-runtime = { workspace = true }datafusion-execution = { workspace = true }datafusion-expr = { workspace = true }+datafusion-physical-expr = { workspace = true }+datafusion-physical-expr-common = { workspace = true }datafusion-physical-plan = { workspace = true }flate2 = { version = "1.0.24", optional = true }futures = { workspace = true }diff --git a/datafusion/datasource/src/memory.rs b/datafusion/datasource/src/memory.rsnew file mode 100644index 0000000000..efb178ad07--- /dev/null+++ b/datafusion/datasource/src/memory.rs@@ -0,0 +1,926 @@+// Licensed to the Apache Software Foundation (ASF) under one+// or more contributor license agreements. See the NOTICE file+// distributed with this work for additional information+// regarding copyright ownership. The ASF licenses this file+// to you under the Apache License, Version 2.0 (the+// "License"); you may not use this file except in compliance+// with the License. You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing,+// software distributed under the License is distributed on an+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY+// KIND, either express or implied. See the License for the+// specific language governing permissions and limitations+// under the License.++//! Execution plan for reading in-memory batches of data++use std::any::Any;+use std::fmt;+use std::sync::Arc;++use crate::source::{DataSource, DataSourceExec};+use datafusion_physical_plan::execution_plan::{Boundedness, EmissionType};+use datafusion_physical_plan::memory::MemoryStream;+use datafusion_physical_plan::projection::{+ all_alias_free_columns, new_projections_for_columns, ProjectionExec,+};+use datafusion_physical_plan::{+ common, ColumnarValue, DisplayAs, DisplayFormatType, ExecutionPlan, Partitioning,+ PhysicalExpr, PlanProperties, SendableRecordBatchStream, Statistics,+};++use arrow::array::{RecordBatch, RecordBatchOptions};+use arrow::datatypes::{Schema, SchemaRef};+use datafusion_common::{+ internal_err, plan_err, project_schema, Constraints, Result, ScalarValue,+};+use datafusion_execution::TaskContext;+use datafusion_physical_expr::equivalence::ProjectionMapping;+use datafusion_physical_expr::expressions::Column;+use datafusion_physical_expr::utils::collect_columns;+use datafusion_physical_expr::{EquivalenceProperties, LexOrdering};++/// Execution plan for reading in-memory batches of data+#[derive(Clone)]+#[deprecated(+ since = "46.0.0",+ note = "use MemorySourceConfig and DataSourceExec instead"+)]+pub struct MemoryExec {+ inner: DataSourceExec,+ /// The partitions to query+ partitions: Vec<Vec<RecordBatch>>,+ /// Optional projection+ projection: Option<Vec<usize>>,+ // Sort information: one or more equivalent orderings+ sort_information: Vec<LexOrdering>,+ /// if partition sizes should be displayed+ show_sizes: bool,+}++#[allow(unused, deprecated)]+impl fmt::Debug for MemoryExec {+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {+ self.inner.fmt_as(DisplayFormatType::Default, f)+ }+}++#[allow(unused, deprecated)]+impl DisplayAs for MemoryExec {+ fn fmt_as(&self, t: DisplayFormatType, f: &mut fmt::Formatter) -> fmt::Result {+ self.inner.fmt_as(t, f)+ }+}++#[allow(unused, deprecated)]+impl ExecutionPlan for MemoryExec {+ fn name(&self) -> &'static str {+ "MemoryExec"+ }++ /// Return a reference to Any that can be used for downcasting+ fn as_any(&self) -> &dyn Any {+ self+ }++ fn properties(&self) -> &PlanProperties {+ self.inner.properties()+ }++ fn children(&self) -> Vec<&Arc<dyn ExecutionPlan>> {+ // This is a leaf node and has no children+ vec![]+ }++ fn with_new_children(+ self: Arc<Self>,+ children: Vec<Arc<dyn ExecutionPlan>>,+ ) -> Result<Arc<dyn ExecutionPlan>> {+ // MemoryExec has no children+ if children.is_empty() {+ Ok(self)+ } else {+ internal_err!("Children cannot be replaced in {self:?}")+ }+ }++ fn execute(+ &self,+ partition: usize,+ context: Arc<TaskContext>,+ ) -> Result<SendableRecordBatchStream> {+ self.inner.execute(partition, context)+ }++ /// We recompute the statistics dynamically from the arrow metadata as it is pretty cheap to do so+ fn statistics(&self) -> Result<Statistics> {+ self.inner.statistics()+ }++ fn try_swapping_with_projection(+ &self,+ projection: &ProjectionExec,+ ) -> Result<Option<Arc<dyn ExecutionPlan>>> {+ self.inner.try_swapping_with_projection(projection)+ }+}++#[allow(unused, deprecated)]+impl MemoryExec {+ /// Create a new execution plan for reading in-memory record batches+ /// The provided `schema` should not have the projection applied.+ pub fn try_new(+ partitions: &[Vec<RecordBatch>],+ schema: SchemaRef,+ projection: Option<Vec<usize>>,+ ) -> Result<Self> {+ let source = MemorySourceConfig::try_new(partitions, schema, projection.clone())?;+ let data_source = DataSourceExec::new(Arc::new(source));+ Ok(Self {+ inner: data_source,+ partitions: partitions.to_vec(),+ projection,+ sort_information: vec![],+ show_sizes: true,+ })+ }++ /// Create a new execution plan from a list of constant values (`ValuesExec`)+ pub fn try_new_as_values(+ schema: SchemaRef,+ data: Vec<Vec<Arc<dyn PhysicalExpr>>>,+ ) -> Result<Self> {+ if data.is_empty() {+ return plan_err!("Values list cannot be empty");+ }++ let n_row = data.len();+ let n_col = schema.fields().len();++ // We have this single row batch as a placeholder to satisfy evaluation argument+ // and generate a single output row+ let placeholder_schema = Arc::new(Schema::empty());+ let placeholder_batch = RecordBatch::try_new_with_options(+ Arc::clone(&placeholder_schema),+ vec![],+ &RecordBatchOptions::new().with_row_count(Some(1)),+ )?;++ // Evaluate each column+ let arrays = (0..n_col)+ .map(|j| {+ (0..n_row)+ .map(|i| {+ let expr = &data[i][j];+ let result = expr.evaluate(&placeholder_batch)?;++ match result {+ ColumnarValue::Scalar(scalar) => Ok(scalar),+ ColumnarValue::Array(array) if array.len() == 1 => {+ ScalarValue::try_from_array(&array, 0)+ }+ ColumnarValue::Array(_) => {+ plan_err!("Cannot have array values in a values list")+ }+ }+ })+ .collect::<Result<Vec<_>>>()+ .and_then(ScalarValue::iter_to_array)+ })+ .collect::<Result<Vec<_>>>()?;++ let batch = RecordBatch::try_new_with_options(+ Arc::clone(&schema),+ arrays,+ &RecordBatchOptions::new().with_row_count(Some(n_row)),+ )?;++ let partitions = vec![batch];+ Self::try_new_from_batches(Arc::clone(&schema), partitions)+ }++ /// Create a new plan using the provided schema and batches.+ ///+ /// Errors if any of the batches don't match the provided schema, or if no+ /// batches are provided.+ pub fn try_new_from_batches(+ schema: SchemaRef,+ batches: Vec<RecordBatch>,+ ) -> Result<Self> {+ if batches.is_empty() {+ return plan_err!("Values list cannot be empty");+ }++ for batch in &batches {+ let batch_schema = batch.schema();+ if batch_schema != schema {+ return plan_err!(+ "Batch has invalid schema. Expected: {}, got: {}",+ schema,+ batch_schema+ );+ }+ }++ let partitions = vec![batches];+ let source = MemorySourceConfig {+ partitions: partitions.clone(),+ schema: Arc::clone(&schema),+ projected_schema: Arc::clone(&schema),+ projection: None,+ sort_information: vec![],+ show_sizes: true,+ fetch: None,+ };+ let data_source = DataSourceExec::new(Arc::new(source));+ Ok(Self {+ inner: data_source,+ partitions,+ projection: None,+ sort_information: vec![],+ show_sizes: true,+ })+ }++ fn memory_source_config(&self) -> MemorySourceConfig {+ self.inner+ .source()+ .as_any()+ .downcast_ref::<MemorySourceConfig>()+ .unwrap()+ .clone()+ }++ pub fn with_constraints(mut self, constraints: Constraints) -> Self {+ self.inner = self.inner.with_constraints(constraints);+ self+ }++ /// Set `show_sizes` to determine whether to display partition sizes+ pub fn with_show_sizes(mut self, show_sizes: bool) -> Self {+ let mut memory_source = self.memory_source_config();+ memory_source.show_sizes = show_sizes;+ self.show_sizes = show_sizes;+ self.inner = DataSourceExec::new(Arc::new(memory_source));+ self+ }++ /// Ref to constraints+ pub fn constraints(&self) -> &Constraints {+ self.properties().equivalence_properties().constraints()+ }++ /// Ref to partitions+ pub fn partitions(&self) -> &[Vec<RecordBatch>] {+ &self.partitions+ }++ /// Ref to projection+ pub fn projection(&self) -> &Option<Vec<usize>> {+ &self.projection+ }++ /// Show sizes+ pub fn show_sizes(&self) -> bool {+ self.show_sizes+ }++ /// Ref to sort information+ pub fn sort_information(&self) -> &[LexOrdering] {+ &self.sort_information+ }++ /// A memory table can be ordered by multiple expressions simultaneously.+ /// [`EquivalenceProperties`] keeps track of expressions that describe the+ /// global ordering of the schema. These columns are not necessarily same; e.g.+ /// ```text+ /// ┌-------┐+ /// | a | b |+ /// |---|---|+ /// | 1 | 9 |+ /// | 2 | 8 |+ /// | 3 | 7 |+ /// | 5 | 5 |+ /// └---┴---┘+ /// ```+ /// where both `a ASC` and `b DESC` can describe the table ordering. With+ /// [`EquivalenceProperties`], we can keep track of these equivalences+ /// and treat `a ASC` and `b DESC` as the same ordering requirement.+ ///+ /// Note that if there is an internal projection, that projection will be+ /// also applied to the given `sort_information`.+ pub fn try_with_sort_information(+ mut self,+ sort_information: Vec<LexOrdering>,+ ) -> Result<Self> {+ self.sort_information = sort_information.clone();+ let mut memory_source = self.memory_source_config();+ memory_source = memory_source.try_with_sort_information(sort_information)?;+ self.inner = DataSourceExec::new(Arc::new(memory_source));+ Ok(self)+ }++ /// Arc clone of ref to original schema+ pub fn original_schema(&self) -> SchemaRef {+ Arc::clone(&self.inner.schema())+ }++ /// This function creates the cache object that stores the plan properties such as schema, equivalence properties, ordering, partitioning, etc.+ fn compute_properties(+ schema: SchemaRef,+ orderings: &[LexOrdering],+ constraints: Constraints,+ partitions: &[Vec<RecordBatch>],+ ) -> PlanProperties {+ PlanProperties::new(+ EquivalenceProperties::new_with_orderings(schema, orderings)+ .with_constraints(constraints),+ Partitioning::UnknownPartitioning(partitions.len()),+ EmissionType::Incremental,+ Boundedness::Bounded,+ )+ }+}++/// Data source configuration for reading in-memory batches of data+#[derive(Clone)]+pub struct MemorySourceConfig {+ /// The partitions to query+ partitions: Vec<Vec<RecordBatch>>,+ /// Schema representing the data before projection+ schema: SchemaRef,+ /// Schema representing the data after the optional projection is applied+ projected_schema: SchemaRef,+ /// Optional projection+ projection: Option<Vec<usize>>,+ /// Sort information: one or more equivalent orderings+ sort_information: Vec<LexOrdering>,+ /// if partition sizes should be displayed+ show_sizes: bool,+ /// The maximum number of records to read from this plan. If `None`,+ /// all records after filtering are returned.+ fetch: Option<usize>,+}++impl DataSource for MemorySourceConfig {+ fn open(+ &self,+ partition: usize,+ _context: Arc<TaskContext>,+ ) -> Result<SendableRecordBatchStream> {+ Ok(Box::pin(+ MemoryStream::try_new(+ self.partitions[partition].clone(),+ Arc::clone(&self.projected_schema),+ self.projection.clone(),+ )?+ .with_fetch(self.fetch),+ ))+ }++ fn as_any(&self) -> &dyn Any {+ self+ }++ fn fmt_as(&self, t: DisplayFormatType, f: &mut fmt::Formatter) -> fmt::Result {+ match t {+ DisplayFormatType::Default | DisplayFormatType::Verbose => {+ let partition_sizes: Vec<_> =+ self.partitions.iter().map(|b| b.len()).collect();++ let output_ordering = self+ .sort_information+ .first()+ .map(|output_ordering| {+ format!(", output_ordering={}", output_ordering)+ })+ .unwrap_or_default();++ let eq_properties = self.eq_properties();+ let constraints = eq_properties.constraints();+ let constraints = if constraints.is_empty() {+ String::new()+ } else {+ format!(", {}", constraints)+ };++ let limit = self+ .fetch+ .map_or(String::new(), |limit| format!(", fetch={}", limit));+ if self.show_sizes {+ write!(+ f,+ "partitions={}, partition_sizes={partition_sizes:?}{limit}{output_ordering}{constraints}",+ partition_sizes.len(),+ )+ } else {+ write!(+ f,+ "partitions={}{limit}{output_ordering}{constraints}",+ partition_sizes.len(),+ )+ }+ }+ }+ }++ fn output_partitioning(&self) -> Partitioning {+ Partitioning::UnknownPartitioning(self.partitions.len())+ }++ fn eq_properties(&self) -> EquivalenceProperties {+ EquivalenceProperties::new_with_orderings(+ Arc::clone(&self.projected_schema),+ self.sort_information.as_slice(),+ )+ }++ fn statistics(&self) -> Result<Statistics> {+ Ok(common::compute_record_batch_statistics(+ &self.partitions,+ &self.schema,+ self.projection.clone(),+ ))+ }++ fn with_fetch(&self, limit: Option<usize>) -> Option<Arc<dyn DataSource>> {+ let source = self.clone();+ Some(Arc::new(source.with_limit(limit)))+ }++ fn fetch(&self) -> Option<usize> {+ self.fetch+ }++ fn try_swapping_with_projection(+ &self,+ projection: &ProjectionExec,+ ) -> Result<Option<Arc<dyn ExecutionPlan>>> {+ // If there is any non-column or alias-carrier expression, Projection should not be removed.+ // This process can be moved into MemoryExec, but it would be an overlap of their responsibility.+ all_alias_free_columns(projection.expr())+ .then(|| {+ let all_projections = (0..self.schema.fields().len()).collect();+ let new_projections = new_projections_for_columns(+ projection,+ self.projection().as_ref().unwrap_or(&all_projections),+ );++ MemorySourceConfig::try_new_exec(+ self.partitions(),+ self.original_schema(),+ Some(new_projections),+ )+ .map(|e| e as _)+ })+ .transpose()+ }+}++impl MemorySourceConfig {+ /// Create a new `MemorySourceConfig` for reading in-memory record batches+ /// The provided `schema` should not have the projection applied.+ pub fn try_new(+ partitions: &[Vec<RecordBatch>],+ schema: SchemaRef,+ projection: Option<Vec<usize>>,+ ) -> Result<Self> {+ let projected_schema = project_schema(&schema, projection.as_ref())?;+ Ok(Self {+ partitions: partitions.to_vec(),+ schema,+ projected_schema,+ projection,+ sort_information: vec![],+ show_sizes: true,+ fetch: None,+ })+ }++ /// Create a new `DataSourceExec` plan for reading in-memory record batches+ /// The provided `schema` should not have the projection applied.+ pub fn try_new_exec(+ partitions: &[Vec<RecordBatch>],+ schema: SchemaRef,+ projection: Option<Vec<usize>>,+ ) -> Result<Arc<DataSourceExec>> {+ let source = Self::try_new(partitions, schema, projection)?;+ Ok(Arc::new(DataSourceExec::new(Arc::new(source))))+ }++ /// Create a new execution plan from a list of constant values (`ValuesExec`)+ pub fn try_new_as_values(+ schema: SchemaRef,+ data: Vec<Vec<Arc<dyn PhysicalExpr>>>,+ ) -> Result<Arc<DataSourceExec>> {+ if data.is_empty() {+ return plan_err!("Values list cannot be empty");+ }++ let n_row = data.len();+ let n_col = schema.fields().len();++ // We have this single row batch as a placeholder to satisfy evaluation argument+ // and generate a single output row+ let placeholder_schema = Arc::new(Schema::empty());+ let placeholder_batch = RecordBatch::try_new_with_options(+ Arc::clone(&placeholder_schema),+ vec![],+ &RecordBatchOptions::new().with_row_count(Some(1)),+ )?;++ // Evaluate each column+ let arrays = (0..n_col)+ .map(|j| {+ (0..n_row)+ .map(|i| {+ let expr = &data[i][j];+ let result = expr.evaluate(&placeholder_batch)?;++ match result {+ ColumnarValue::Scalar(scalar) => Ok(scalar),+ ColumnarValue::Array(array) if array.len() == 1 => {+ ScalarValue::try_from_array(&array, 0)+ }+ ColumnarValue::Array(_) => {+ plan_err!("Cannot have array values in a values list")+ }+ }+ })+ .collect::<Result<Vec<_>>>()+ .and_then(ScalarValue::iter_to_array)+ })+ .collect::<Result<Vec<_>>>()?;++ let batch = RecordBatch::try_new_with_options(+ Arc::clone(&schema),+ arrays,+ &RecordBatchOptions::new().with_row_count(Some(n_row)),+ )?;++ let partitions = vec![batch];+ Self::try_new_from_batches(Arc::clone(&schema), partitions)+ }++ /// Create a new plan using the provided schema and batches.+ ///+ /// Errors if any of the batches don't match the provided schema, or if no+ /// batches are provided.+ pub fn try_new_from_batches(+ schema: SchemaRef,+ batches: Vec<RecordBatch>,+ ) -> Result<Arc<DataSourceExec>> {+ if batches.is_empty() {+ return plan_err!("Values list cannot be empty");+ }++ for batch in &batches {+ let batch_schema = batch.schema();+ if batch_schema != schema {+ return plan_err!(+ "Batch has invalid schema. Expected: {}, got: {}",+ schema,+ batch_schema+ );+ }+ }++ let partitions = vec![batches];+ let source = Self {+ partitions,+ schema: Arc::clone(&schema),+ projected_schema: Arc::clone(&schema),+ projection: None,+ sort_information: vec![],+ show_sizes: true,+ fetch: None,+ };+ Ok(Arc::new(DataSourceExec::new(Arc::new(source))))+ }++ /// Set the limit of the files+ pub fn with_limit(mut self, limit: Option<usize>) -> Self {+ self.fetch = limit;+ self+ }++ /// Set `show_sizes` to determine whether to display partition sizes+ pub fn with_show_sizes(mut self, show_sizes: bool) -> Self {+ self.show_sizes = show_sizes;+ self+ }++ /// Ref to partitions+ pub fn partitions(&self) -> &[Vec<RecordBatch>] {+ &self.partitions+ }++ /// Ref to projection+ pub fn projection(&self) -> &Option<Vec<usize>> {+ &self.projection+ }++ /// Show sizes+ pub fn show_sizes(&self) -> bool {+ self.show_sizes+ }++ /// Ref to sort information+ pub fn sort_information(&self) -> &[LexOrdering] {+ &self.sort_information+ }++ /// A memory table can be ordered by multiple expressions simultaneously.+ /// [`EquivalenceProperties`] keeps track of expressions that describe the+ /// global ordering of the schema. These columns are not necessarily same; e.g.+ /// ```text+ /// ┌-------┐+ /// | a | b |+ /// |---|---|+ /// | 1 | 9 |+ /// | 2 | 8 |+ /// | 3 | 7 |+ /// | 5 | 5 |+ /// └---┴---┘+ /// ```+ /// where both `a ASC` and `b DESC` can describe the table ordering. With+ /// [`EquivalenceProperties`], we can keep track of these equivalences+ /// and treat `a ASC` and `b DESC` as the same ordering requirement.+ ///+ /// Note that if there is an internal projection, that projection will be+ /// also applied to the given `sort_information`.+ pub fn try_with_sort_information(+ mut self,+ mut sort_information: Vec<LexOrdering>,+ ) -> Result<Self> {+ // All sort expressions must refer to the original schema+ let fields = self.schema.fields();+ let ambiguous_column = sort_information+ .iter()+ .flat_map(|ordering| ordering.clone())+ .flat_map(|expr| collect_columns(&expr.expr))+ .find(|col| {+ fields+ .get(col.index())+ .map(|field| field.name() != col.name())+ .unwrap_or(true)+ });+ if let Some(col) = ambiguous_column {+ return internal_err!(+ "Column {:?} is not found in the original schema of the MemorySourceConfig",+ col+ );+ }++ // If there is a projection on the source, we also need to project orderings+ if let Some(projection) = &self.projection {+ let base_eqp = EquivalenceProperties::new_with_orderings(+ self.original_schema(),+ &sort_information,+ );+ let proj_exprs = projection+ .iter()+ .map(|idx| {+ let base_schema = self.original_schema();+ let name = base_schema.field(*idx).name();+ (Arc::new(Column::new(name, *idx)) as _, name.to_string())+ })+ .collect::<Vec<_>>();+ let projection_mapping =+ ProjectionMapping::try_new(&proj_exprs, &self.original_schema())?;+ sort_information = base_eqp+ .project(&projection_mapping, Arc::clone(&self.projected_schema))+ .into_oeq_class()+ .into_inner();+ }++ self.sort_information = sort_information;+ Ok(self)+ }++ /// Arc clone of ref to original schema+ pub fn original_schema(&self) -> SchemaRef {+ Arc::clone(&self.schema)+ }+}++#[cfg(test)]+mod memory_source_tests {+ use std::sync::Arc;++ use crate::memory::MemorySourceConfig;+ use crate::source::DataSourceExec;+ use datafusion_physical_plan::ExecutionPlan;++ use arrow::compute::SortOptions;+ use arrow::datatypes::{DataType, Field, Schema};+ use datafusion_physical_expr::expressions::col;+ use datafusion_physical_expr::PhysicalSortExpr;+ use datafusion_physical_expr_common::sort_expr::LexOrdering;++ #[test]+ fn test_memory_order_eq() -> datafusion_common::Result<()> {+ let schema = Arc::new(Schema::new(vec![+ Field::new("a", DataType::Int64, false),+ Field::new("b", DataType::Int64, false),+ Field::new("c", DataType::Int64, false),+ ]));+ let sort1 = LexOrdering::new(vec![+ PhysicalSortExpr {+ expr: col("a", &schema)?,+ options: SortOptions::default(),+ },+ PhysicalSortExpr {+ expr: col("b", &schema)?,+ options: SortOptions::default(),+ },+ ]);+ let sort2 = LexOrdering::new(vec![PhysicalSortExpr {+ expr: col("c", &schema)?,+ options: SortOptions::default(),+ }]);+ let mut expected_output_order = LexOrdering::default();+ expected_output_order.extend(sort1.clone());+ expected_output_order.extend(sort2.clone());++ let sort_information = vec![sort1.clone(), sort2.clone()];+ let mem_exec = Arc::new(DataSourceExec::new(Arc::new(+ MemorySourceConfig::try_new(&[vec![]], schema, None)?+ .try_with_sort_information(sort_information)?,+ )));++ assert_eq!(+ mem_exec.properties().output_ordering().unwrap(),+ &expected_output_order+ );+ let eq_properties = mem_exec.properties().equivalence_properties();+ assert!(eq_properties.oeq_class().contains(&sort1));+ assert!(eq_properties.oeq_class().contains(&sort2));+ Ok(())+ }+}++#[cfg(test)]+mod tests {+ use super::*;+ use arrow::array::{ArrayRef, Int32Array};+ use datafusion_physical_plan::expressions::lit;+ use std::collections::HashMap;++ use arrow::datatypes::{DataType, Field};+ use datafusion_common::assert_batches_eq;+ use datafusion_common::stats::{ColumnStatistics, Precision};+ use futures::StreamExt;++ // Return a RecordBatch with a single Int32 array with values (0..sz) in a field named "i"+ pub fn make_partition(sz: i32) -> RecordBatch {+ let seq_start = 0;+ let seq_end = sz;+ let values = (seq_start..seq_end).collect::<Vec<_>>();+ let schema = Arc::new(Schema::new(vec![Field::new("i", DataType::Int32, true)]));+ let arr = Arc::new(Int32Array::from(values));+ let arr = arr as ArrayRef;++ RecordBatch::try_new(schema, vec![arr]).unwrap()+ }++ #[tokio::test]+ async fn exec_with_limit() -> Result<()> {+ let task_ctx = Arc::new(TaskContext::default());+ let batch = make_partition(7);+ let schema = batch.schema();+ let batches = vec![batch.clone(), batch];++ let exec = MemorySourceConfig::try_new_from_batches(schema, batches).unwrap();+ assert_eq!(exec.fetch(), None);++ let exec = exec.with_fetch(Some(4)).unwrap();+ assert_eq!(exec.fetch(), Some(4));++ let mut it = exec.execute(0, task_ctx)?;+ let mut results = vec![];+ while let Some(batch) = it.next().await {+ results.push(batch?);+ }++ let expected = [+ "+---+", "| i |", "+---+", "| 0 |", "| 1 |", "| 2 |", "| 3 |", "+---+",+ ];+ assert_batches_eq!(expected, &results);+ Ok(())+ }++ /// Get the schema for the aggregate_test_* csv files+ pub fn aggr_test_schema() -> SchemaRef {+ let mut f1 = Field::new("c1", DataType::Utf8, false);+ f1.set_metadata(HashMap::from_iter(vec![("testing".into(), "test".into())]));+ let schema = Schema::new(vec![+ f1,+ Field::new("c2", DataType::UInt32, false),+ Field::new("c3", DataType::Int8, false),+ Field::new("c4", DataType::Int16, false),+ Field::new("c5", DataType::Int32, false),+ Field::new("c6", DataType::Int64, false),+ Field::new("c7", DataType::UInt8, false),+ Field::new("c8", DataType::UInt16, false),+ Field::new("c9", DataType::UInt32, false),+ Field::new("c10", DataType::UInt64, false),+ Field::new("c11", DataType::Float32, false),+ Field::new("c12", DataType::Float64, false),+ Field::new("c13", DataType::Utf8, false),+ ]);++ Arc::new(schema)+ }++ #[tokio::test]+ async fn values_empty_case() -> Result<()> {+ let schema = aggr_test_schema();+ let empty = MemorySourceConfig::try_new_as_values(schema, vec![]);+ assert!(empty.is_err());+ Ok(())+ }++ #[test]+ fn new_exec_with_batches() {+ let batch = make_partition(7);+ let schema = batch.schema();+ let batches = vec![batch.clone(), batch];+ let _exec = MemorySourceConfig::try_new_from_batches(schema, batches).unwrap();+ }++ #[test]+ fn new_exec_with_batches_empty() {+ let batch = make_partition(7);+ let schema = batch.schema();+ let _ = MemorySourceConfig::try_new_from_batches(schema, Vec::new()).unwrap_err();+ }++ #[test]+ fn new_exec_with_batches_invalid_schema() {+ let batch = make_partition(7);+ let batches = vec![batch.clone(), batch];++ let invalid_schema = Arc::new(Schema::new(vec![+ Field::new("col0", DataType::UInt32, false),+ Field::new("col1", DataType::Utf8, false),+ ]));+ let _ = MemorySourceConfig::try_new_from_batches(invalid_schema, batches)+ .unwrap_err();+ }++ // Test issue: https://github.com/apache/datafusion/issues/8763+ #[test]+ fn new_exec_with_non_nullable_schema() {+ let schema = Arc::new(Schema::new(vec![Field::new(+ "col0",+ DataType::UInt32,+ false,+ )]));+ let _ = MemorySourceConfig::try_new_as_values(+ Arc::clone(&schema),+ vec![vec![lit(1u32)]],+ )+ .unwrap();+ // Test that a null value is rejected+ let _ = MemorySourceConfig::try_new_as_values(+ schema,+ vec![vec![lit(ScalarValue::UInt32(None))]],+ )+ .unwrap_err();+ }++ #[test]+ fn values_stats_with_nulls_only() -> Result<()> {+ let data = vec![+ vec![lit(ScalarValue::Null)],+ vec![lit(ScalarValue::Null)],+ vec![lit(ScalarValue::Null)],+ ];+ let rows = data.len();+ let values = MemorySourceConfig::try_new_as_values(+ Arc::new(Schema::new(vec![Field::new("col0", DataType::Null, true)])),+ data,+ )?;++ assert_eq!(+ values.statistics()?,+ Statistics {+ num_rows: Precision::Exact(rows),+ total_byte_size: Precision::Exact(8), // not important+ column_statistics: vec![ColumnStatistics {+ null_count: Precision::Exact(rows), // there are only nulls+ distinct_count: Precision::Absent,+ max_value: Precision::Absent,+ min_value: Precision::Absent,+ sum_value: Precision::Absent,+ },],+ }+ );++ Ok(())+ }+}diff --git a/datafusion/datasource/src/mod.rs b/datafusion/datasource/src/mod.rsindex 2fc2da6489..8183d7b532 100644--- a/datafusion/datasource/src/mod.rs+++ b/datafusion/datasource/src/mod.rs@@ -30,6 +30,8 @@ pub mod file_meta;pub mod file_scan_config;pub mod file_sink_config;pub mod file_stream;+pub mod memory;+pub mod source;pub mod url;pub mod write;use chrono::TimeZone;diff --git a/datafusion/physical-plan/src/source.rs b/datafusion/datasource/src/source.rssimilarity index 95%rename from datafusion/physical-plan/src/source.rsrename to datafusion/datasource/src/source.rsindex 0c1dfddd26..1b0d769029 100644--- a/datafusion/physical-plan/src/source.rs+++ b/datafusion/datasource/src/source.rs@@ -20,10 +20,12 @@ use std::fmt;use std::fmt::{Debug, Formatter};use std::sync::Arc;-use crate::execution_plan::{Boundedness, EmissionType};-use crate::metrics::{ExecutionPlanMetricsSet, MetricsSet};-use crate::projection::ProjectionExec;-use crate::{DisplayAs, DisplayFormatType, ExecutionPlan, PlanProperties};+use datafusion_physical_plan::execution_plan::{Boundedness, EmissionType};+use datafusion_physical_plan::metrics::{ExecutionPlanMetricsSet, MetricsSet};+use datafusion_physical_plan::projection::ProjectionExec;+use datafusion_physical_plan::{+ DisplayAs, DisplayFormatType, ExecutionPlan, PlanProperties,+};use datafusion_common::config::ConfigOptions;use datafusion_common::{Constraints, Statistics};diff --git a/datafusion/physical-optimizer/Cargo.toml b/datafusion/physical-optimizer/Cargo.tomlindex e8473e6556..9b7e6a0f5d 100644--- a/datafusion/physical-optimizer/Cargo.toml+++ b/datafusion/physical-optimizer/Cargo.toml@@ -40,6 +40,7 @@ recursive_protection = ["dep:recursive"][dependencies]arrow = { workspace = true }datafusion-common = { workspace = true, default-features = true }+datafusion-datasource = { workspace = true }datafusion-execution = { workspace = true }datafusion-expr = { workspace = true }datafusion-expr-common = { workspace = true, default-features = true }diff --git a/datafusion/physical-plan/Cargo.toml b/datafusion/physical-plan/Cargo.tomlindex ae0ea60e1a..4a10398e5a 100644--- a/datafusion/physical-plan/Cargo.toml+++ b/datafusion/physical-plan/Cargo.toml@@ -77,10 +77,6 @@ tokio = { workspace = true, features = ["parking_lot",] }-[[bench]]-harness = false-name = "spm"-[[bench]]harness = falsename = "partial_ordering"diff --git a/datafusion/physical-plan/src/aggregates/mod.rs b/datafusion/physical-plan/src/aggregates/mod.rsindex 85b41da857..0947a2ff55 100644--- a/datafusion/physical-plan/src/aggregates/mod.rs+++ b/datafusion/physical-plan/src/aggregates/mod.rs@@ -1347,10 +1347,10 @@ mod tests {use crate::common::collect;use crate::execution_plan::Boundedness;use crate::expressions::col;- use crate::memory::MemorySourceConfig;use crate::metrics::MetricValue;use crate::test::assert_is_pending;use crate::test::exec::{assert_strong_count_converges_to_zero, BlockingExec};+ use crate::test::TestMemoryExec;use crate::RecordBatchStream;use arrow::array::{@@ -2207,7 +2207,7 @@ mod tests {vec![test_last_value_agg_expr(&schema, sort_options)?]};- let memory_exec = MemorySourceConfig::try_new_exec(+ let memory_exec = TestMemoryExec::try_new_exec(&[vec![partition1],vec![partition2],@@ -2442,11 +2442,8 @@ mod tests {}).collect();- let input = MemorySourceConfig::try_new_exec(- &[input_batches],- Arc::clone(&schema),- None,- )?;+ let input =+ TestMemoryExec::try_new_exec(&[input_batches], Arc::clone(&schema), None)?;let aggregate_exec = Arc::new(AggregateExec::try_new(AggregateMode::Single,@@ -2557,7 +2554,7 @@ mod tests {.build().map(Arc::new)?];- let input = MemorySourceConfig::try_new_exec(+ let input = TestMemoryExec::try_new_exec(&[vec![batch.clone()]],Arc::<Schema>::clone(&batch.schema()),None,@@ -2627,7 +2624,7 @@ mod tests {];let input =- MemorySourceConfig::try_new_exec(&[input_data], Arc::clone(&schema), None)?;+ TestMemoryExec::try_new_exec(&[input_data], Arc::clone(&schema), None)?;let aggregate_exec = Arc::new(AggregateExec::try_new(AggregateMode::Partial,group_by,@@ -2714,7 +2711,7 @@ mod tests {];let input =- MemorySourceConfig::try_new_exec(&[input_data], Arc::clone(&schema), None)?;+ TestMemoryExec::try_new_exec(&[input_data], Arc::clone(&schema), None)?;let aggregate_exec = Arc::new(AggregateExec::try_new(AggregateMode::Partial,group_by,@@ -2829,7 +2826,7 @@ mod tests {create_record_batch(&schema, (vec![2, 3, 4, 4], vec![1.0, 2.0, 3.0, 4.0]))?,];let plan: Arc<dyn ExecutionPlan> =- MemorySourceConfig::try_new_exec(&[batches], Arc::clone(&schema), None)?;+ TestMemoryExec::try_new_exec(&[batches], Arc::clone(&schema), None)?;let grouping_set = PhysicalGroupBy::new(vec![(col("a", &schema)?, "a".to_string())],diff --git a/datafusion/physical-plan/src/empty.rs b/datafusion/physical-plan/src/empty.rsindex 5168c3cc10..c4e738cb3a 100644--- a/datafusion/physical-plan/src/empty.rs+++ b/datafusion/physical-plan/src/empty.rs@@ -20,10 +20,10 @@use std::any::Any;use std::sync::Arc;-use super::{common, DisplayAs, PlanProperties, SendableRecordBatchStream, Statistics};+use crate::memory::MemoryStream;+use crate::{common, DisplayAs, PlanProperties, SendableRecordBatchStream, Statistics};use crate::{execution_plan::{Boundedness, EmissionType},- memory::MemoryStream,DisplayFormatType, ExecutionPlan, Partitioning,};diff --git a/datafusion/physical-plan/src/filter.rs b/datafusion/physical-plan/src/filter.rsindex 5866f0938e..a66873bc65 100644--- a/datafusion/physical-plan/src/filter.rs+++ b/datafusion/physical-plan/src/filter.rs@@ -624,7 +624,6 @@ mod tests {use crate::expressions::*;use crate::test;use crate::test::exec::StatisticsExec;-use arrow::datatypes::{Field, Schema, UnionFields, UnionMode};use datafusion_common::ScalarValue;diff --git a/datafusion/physical-plan/src/joins/hash_join.rs b/datafusion/physical-plan/src/joins/hash_join.rsindex 2983478ada..23ffd2a28d 100644--- a/datafusion/physical-plan/src/joins/hash_join.rs+++ b/datafusion/physical-plan/src/joins/hash_join.rs@@ -1638,7 +1638,7 @@ impl EmbeddedProjection for HashJoinExec {#[cfg(test)]mod tests {use super::*;- use crate::memory::MemorySourceConfig;+ use crate::test::TestMemoryExec;use crate::{common, expressions::Column, repartition::RepartitionExec, test::build_table_i32,test::exec::MockExec,@@ -1680,7 +1680,7 @@ mod tests {) -> Arc<dyn ExecutionPlan> {let batch = build_table_i32(a, b, c);let schema = batch.schema();- MemorySourceConfig::try_new_exec(&[vec![batch]], schema, None).unwrap()+ TestMemoryExec::try_new_exec(&[vec![batch]], schema, None).unwrap()}fn join(@@ -2083,7 +2083,7 @@ mod tests {build_table_i32(("a1", &vec![2]), ("b2", &vec![2]), ("c1", &vec![9]));let schema = batch1.schema();let left =- MemorySourceConfig::try_new_exec(&[vec![batch1], vec![batch2]], schema, None)+ TestMemoryExec::try_new_exec(&[vec![batch1], vec![batch2]], schema, None).unwrap();let right = build_table(@@ -2155,7 +2155,7 @@ mod tests {let schema = batch1.schema();let left =- MemorySourceConfig::try_new_exec(&[vec![batch1], vec![batch2]], schema, None)+ TestMemoryExec::try_new_exec(&[vec![batch1], vec![batch2]], schema, None).unwrap();let right = build_table(("a2", &vec![20, 30, 10]),@@ -2209,7 +2209,7 @@ mod tests {build_table_i32(("a2", &vec![30]), ("b1", &vec![5]), ("c2", &vec![90]));let schema = batch1.schema();let right =- MemorySourceConfig::try_new_exec(&[vec![batch1], vec![batch2]], schema, None)+ TestMemoryExec::try_new_exec(&[vec![batch1], vec![batch2]], schema, None).unwrap();let on = vec![(@@ -2288,8 +2288,7 @@ mod tests {) -> Arc<dyn ExecutionPlan> {let batch = build_table_i32(a, b, c);let schema = batch.schema();- MemorySourceConfig::try_new_exec(&[vec![batch.clone(), batch]], schema, None)- .unwrap()+ TestMemoryExec::try_new_exec(&[vec![batch.clone(), batch]], schema, None).unwrap()}#[apply(batch_sizes)]@@ -2394,8 +2393,7 @@ mod tests {Arc::new(Column::new_with_schema("b1", &right.schema()).unwrap()) as _,)];let schema = right.schema();- let right =- MemorySourceConfig::try_new_exec(&[vec![right]], schema, None).unwrap();+ let right = TestMemoryExec::try_new_exec(&[vec![right]], schema, None).unwrap();let join = join(left, right, on, &JoinType::Left, false).unwrap();let columns = columns(&join.schema());@@ -2432,8 +2430,7 @@ mod tests {Arc::new(Column::new_with_schema("b2", &right.schema()).unwrap()) as _,)];let schema = right.schema();- let right =- MemorySourceConfig::try_new_exec(&[vec![right]], schema, None).unwrap();+ let right = TestMemoryExec::try_new_exec(&[vec![right]], schema, None).unwrap();let join = join(left, right, on, &JoinType::Full, false).unwrap();let columns = columns(&join.schema());@@ -3738,13 +3735,12 @@ mod tests {let n: ArrayRef = Arc::new(Int32Array::from(vec![1, 2, 3]));let batch = RecordBatch::try_new(Arc::clone(&schema), vec![dates, n])?;let left =- MemorySourceConfig::try_new_exec(&[vec![batch]], Arc::clone(&schema), None)+ TestMemoryExec::try_new_exec(&[vec![batch]], Arc::clone(&schema), None).unwrap();let dates: ArrayRef = Arc::new(Date32Array::from(vec![19108, 19108, 19109]));let n: ArrayRef = Arc::new(Int32Array::from(vec![4, 5, 6]));let batch = RecordBatch::try_new(Arc::clone(&schema), vec![dates, n])?;- let right =- MemorySourceConfig::try_new_exec(&[vec![batch]], schema, None).unwrap();+ let right = TestMemoryExec::try_new_exec(&[vec![batch]], schema, None).unwrap();let on = vec![(Arc::new(Column::new_with_schema("date", &left.schema()).unwrap()) as _,Arc::new(Column::new_with_schema("date", &right.schema()).unwrap()) as _,@@ -4034,7 +4030,7 @@ mod tests {("b1", &vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 0]),("c1", &vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 0]),);- let left = MemorySourceConfig::try_new_exec(+ let left = TestMemoryExec::try_new_exec(&[vec![left_batch.clone()], vec![left_batch.clone()]],left_batch.schema(),None,@@ -4045,7 +4041,7 @@ mod tests {("b2", &vec![12, 13]),("c2", &vec![14, 15]),);- let right = MemorySourceConfig::try_new_exec(+ let right = TestMemoryExec::try_new_exec(&[vec![right_batch.clone()], vec![right_batch.clone()]],right_batch.schema(),None,@@ -4130,7 +4126,7 @@ mod tests {).unwrap();let schema_ref = batch.schema();- MemorySourceConfig::try_new_exec(&[vec![batch]], schema_ref, None).unwrap()+ TestMemoryExec::try_new_exec(&[vec![batch]], schema_ref, None).unwrap()}#[tokio::test]diff --git a/datafusion/physical-plan/src/joins/nested_loop_join.rs b/datafusion/physical-plan/src/joins/nested_loop_join.rsindex 6de6b3b4df..64dfc8219b 100644--- a/datafusion/physical-plan/src/joins/nested_loop_join.rs+++ b/datafusion/physical-plan/src/joins/nested_loop_join.rs@@ -1030,8 +1030,7 @@ impl EmbeddedProjection for NestedLoopJoinExec {#[cfg(test)]pub(crate) mod tests {use super::*;- use crate::memory::MemorySourceConfig;- use crate::source::DataSourceExec;+ use crate::test::TestMemoryExec;use crate::{common, expressions::Column, repartition::RepartitionExec, test::build_table_i32,};@@ -1072,7 +1071,7 @@ pub(crate) mod tests {};let mut source =- MemorySourceConfig::try_new(&[batches], Arc::clone(&schema), None).unwrap();+ TestMemoryExec::try_new(&[batches], Arc::clone(&schema), None).unwrap();if !sorted_column_names.is_empty() {let mut sort_info = LexOrdering::default();for name in sorted_column_names {@@ -1089,7 +1088,7 @@ pub(crate) mod tests {source = source.try_with_sort_information(vec![sort_info]).unwrap();}- Arc::new(DataSourceExec::new(Arc::new(source)))+ Arc::new(TestMemoryExec::update_cache(Arc::new(source)))}fn build_left_table() -> Arc<dyn ExecutionPlan> {diff --git a/datafusion/physical-plan/src/joins/sort_merge_join.rs b/datafusion/physical-plan/src/joins/sort_merge_join.rsindex a3e835c641..6c933ca218 100644--- a/datafusion/physical-plan/src/joins/sort_merge_join.rs+++ b/datafusion/physical-plan/src/joins/sort_merge_join.rs@@ -2547,7 +2547,7 @@ mod tests {use crate::joins::sort_merge_join::{get_corrected_filter_mask, JoinedRecordBatches};use crate::joins::utils::{ColumnIndex, JoinFilter, JoinOn};use crate::joins::SortMergeJoinExec;- use crate::memory::MemorySourceConfig;+ use crate::test::TestMemoryExec;use crate::test::{build_table_i32, build_table_i32_two_cols};use crate::{common, ExecutionPlan};@@ -2558,12 +2558,12 @@ mod tests {) -> Arc<dyn ExecutionPlan> {let batch = build_table_i32(a, b, c);let schema = batch.schema();- MemorySourceConfig::try_new_exec(&[vec![batch]], schema, None).unwrap()+ TestMemoryExec::try_new_exec(&[vec![batch]], schema, None).unwrap()}fn build_table_from_batches(batches: Vec<RecordBatch>) -> Arc<dyn ExecutionPlan> {let schema = batches.first().unwrap().schema();- MemorySourceConfig::try_new_exec(&[batches], schema, None).unwrap()+ TestMemoryExec::try_new_exec(&[batches], schema, None).unwrap()}fn build_date_table(@@ -2588,7 +2588,7 @@ mod tests {.unwrap();let schema = batch.schema();- MemorySourceConfig::try_new_exec(&[vec![batch]], schema, None).unwrap()+ TestMemoryExec::try_new_exec(&[vec![batch]], schema, None).unwrap()}fn build_date64_table(@@ -2613,7 +2613,7 @@ mod tests {.unwrap();let schema = batch.schema();- MemorySourceConfig::try_new_exec(&[vec![batch]], schema, None).unwrap()+ TestMemoryExec::try_new_exec(&[vec![batch]], schema, None).unwrap()}/// returns a table with 3 columns of i32 in memory@@ -2636,7 +2636,7 @@ mod tests {],).unwrap();- MemorySourceConfig::try_new_exec(&[vec![batch]], schema, None).unwrap()+ TestMemoryExec::try_new_exec(&[vec![batch]], schema, None).unwrap()}pub fn build_table_two_cols(@@ -2645,7 +2645,7 @@ mod tests {) -> Arc<dyn ExecutionPlan> {let batch = build_table_i32_two_cols(a, b);let schema = batch.schema();- MemorySourceConfig::try_new_exec(&[vec![batch]], schema, None).unwrap()+ TestMemoryExec::try_new_exec(&[vec![batch]], schema, None).unwrap()}fn join(diff --git a/datafusion/physical-plan/src/joins/test_utils.rs b/datafusion/physical-plan/src/joins/test_utils.rsindex 9932c647be..e70007aa65 100644--- a/datafusion/physical-plan/src/joins/test_utils.rs+++ b/datafusion/physical-plan/src/joins/test_utils.rs@@ -23,9 +23,8 @@ use crate::joins::utils::{JoinFilter, JoinOn};use crate::joins::{HashJoinExec, PartitionMode, StreamJoinPartitionMode, SymmetricHashJoinExec,};-use crate::memory::MemorySourceConfig;use crate::repartition::RepartitionExec;-use crate::source::DataSourceExec;+use crate::test::TestMemoryExec;use crate::{common, ExecutionPlan, ExecutionPlanProperties, Partitioning};use arrow::array::{@@ -530,14 +529,14 @@ pub fn create_memory_table(right_sorted: Vec<LexOrdering>,) -> Result<(Arc<dyn ExecutionPlan>, Arc<dyn ExecutionPlan>)> {let left_schema = left_partition[0].schema();- let left = MemorySourceConfig::try_new(&[left_partition], left_schema, None)?+ let left = TestMemoryExec::try_new(&[left_partition], left_schema, None)?.try_with_sort_information(left_sorted)?;let right_schema = right_partition[0].schema();- let right = MemorySourceConfig::try_new(&[right_partition], right_schema, None)?+ let right = TestMemoryExec::try_new(&[right_partition], right_schema, None)?.try_with_sort_information(right_sorted)?;Ok((- Arc::new(DataSourceExec::new(Arc::new(left))),- Arc::new(DataSourceExec::new(Arc::new(right))),+ Arc::new(TestMemoryExec::update_cache(Arc::new(left))),+ Arc::new(TestMemoryExec::update_cache(Arc::new(right))),))}diff --git a/datafusion/physical-plan/src/lib.rs b/datafusion/physical-plan/src/lib.rsindex 06fe23d2ff..6ddaef1a2d 100644--- a/datafusion/physical-plan/src/lib.rs+++ b/datafusion/physical-plan/src/lib.rs@@ -83,13 +83,11 @@ pub mod unnest;pub mod values;pub mod windows;pub mod work_table;-pub mod udaf {pub use datafusion_expr::StatisticsArgs;pub use datafusion_physical_expr::aggregate::AggregateFunctionExpr;}pub mod coalesce;-pub mod source;#[cfg(test)]pub mod test;diff --git a/datafusion/physical-plan/src/memory.rs b/datafusion/physical-plan/src/memory.rsindex 0077804bdf..fd338cc913 100644--- a/datafusion/physical-plan/src/memory.rs+++ b/datafusion/physical-plan/src/memory.rs@@ -22,696 +22,22 @@ use std::fmt;use std::sync::Arc;use std::task::{Context, Poll};-use super::{- common, ColumnarValue, DisplayAs, DisplayFormatType, ExecutionPlan, Partitioning,- PhysicalExpr, PlanProperties, RecordBatchStream, SendableRecordBatchStream,- Statistics,-};use crate::execution_plan::{Boundedness, EmissionType};-use crate::projection::{- all_alias_free_columns, new_projections_for_columns, ProjectionExec,+use crate::{+ DisplayAs, DisplayFormatType, ExecutionPlan, Partitioning, PlanProperties,+ RecordBatchStream, SendableRecordBatchStream, Statistics,};-use crate::source::{DataSource, DataSourceExec};-use arrow::array::{RecordBatch, RecordBatchOptions};-use arrow::datatypes::{Schema, SchemaRef};-use datafusion_common::{- internal_err, plan_err, project_schema, Constraints, Result, ScalarValue,-};+use arrow::array::RecordBatch;+use arrow::datatypes::SchemaRef;+use datafusion_common::{internal_err, Result};use datafusion_execution::memory_pool::MemoryReservation;use datafusion_execution::TaskContext;-use datafusion_physical_expr::equivalence::ProjectionMapping;-use datafusion_physical_expr::expressions::Column;-use datafusion_physical_expr::utils::collect_columns;-use datafusion_physical_expr::{EquivalenceProperties, LexOrdering};+use datafusion_physical_expr::EquivalenceProperties;use futures::Stream;use parking_lot::RwLock;-/// Execution plan for reading in-memory batches of data-#[derive(Clone)]-#[deprecated(- since = "46.0.0",- note = "use MemorySourceConfig and DataSourceExec instead"-)]-pub struct MemoryExec {- inner: DataSourceExec,- /// The partitions to query- partitions: Vec<Vec<RecordBatch>>,- /// Optional projection- projection: Option<Vec<usize>>,- // Sort information: one or more equivalent orderings- sort_information: Vec<LexOrdering>,- /// if partition sizes should be displayed- show_sizes: bool,-}--#[allow(unused, deprecated)]-impl fmt::Debug for MemoryExec {- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {- self.inner.fmt_as(DisplayFormatType::Default, f)- }-}--#[allow(unused, deprecated)]-impl DisplayAs for MemoryExec {- fn fmt_as(&self, t: DisplayFormatType, f: &mut fmt::Formatter) -> fmt::Result {- self.inner.fmt_as(t, f)- }-}--#[allow(unused, deprecated)]-impl ExecutionPlan for MemoryExec {- fn name(&self) -> &'static str {- "MemoryExec"- }-- /// Return a reference to Any that can be used for downcasting- fn as_any(&self) -> &dyn Any {- self- }-- fn properties(&self) -> &PlanProperties {- self.inner.properties()- }-- fn children(&self) -> Vec<&Arc<dyn ExecutionPlan>> {- // This is a leaf node and has no children- vec![]- }-- fn with_new_children(- self: Arc<Self>,- children: Vec<Arc<dyn ExecutionPlan>>,- ) -> Result<Arc<dyn ExecutionPlan>> {- // MemoryExec has no children- if children.is_empty() {- Ok(self)- } else {- internal_err!("Children cannot be replaced in {self:?}")- }- }-- fn execute(- &self,- partition: usize,- context: Arc<TaskContext>,- ) -> Result<SendableRecordBatchStream> {- self.inner.execute(partition, context)- }-- /// We recompute the statistics dynamically from the arrow metadata as it is pretty cheap to do so- fn statistics(&self) -> Result<Statistics> {- self.inner.statistics()- }-- fn try_swapping_with_projection(- &self,- projection: &ProjectionExec,- ) -> Result<Option<Arc<dyn ExecutionPlan>>> {- self.inner.try_swapping_with_projection(projection)- }-}--#[allow(unused, deprecated)]-impl MemoryExec {- /// Create a new execution plan for reading in-memory record batches- /// The provided `schema` should not have the projection applied.- pub fn try_new(- partitions: &[Vec<RecordBatch>],- schema: SchemaRef,- projection: Option<Vec<usize>>,- ) -> Result<Self> {- let source = MemorySourceConfig::try_new(partitions, schema, projection.clone())?;- let data_source = DataSourceExec::new(Arc::new(source));- Ok(Self {- inner: data_source,- partitions: partitions.to_vec(),- projection,- sort_information: vec![],- show_sizes: true,- })- }-- /// Create a new execution plan from a list of constant values (`ValuesExec`)- pub fn try_new_as_values(- schema: SchemaRef,- data: Vec<Vec<Arc<dyn PhysicalExpr>>>,- ) -> Result<Self> {- if data.is_empty() {- return plan_err!("Values list cannot be empty");- }-- let n_row = data.len();- let n_col = schema.fields().len();-- // We have this single row batch as a placeholder to satisfy evaluation argument- // and generate a single output row- let placeholder_schema = Arc::new(Schema::empty());- let placeholder_batch = RecordBatch::try_new_with_options(- Arc::clone(&placeholder_schema),- vec![],- &RecordBatchOptions::new().with_row_count(Some(1)),- )?;-- // Evaluate each column- let arrays = (0..n_col)- .map(|j| {- (0..n_row)- .map(|i| {- let expr = &data[i][j];- let result = expr.evaluate(&placeholder_batch)?;-- match result {- ColumnarValue::Scalar(scalar) => Ok(scalar),- ColumnarValue::Array(array) if array.len() == 1 => {- ScalarValue::try_from_array(&array, 0)- }- ColumnarValue::Array(_) => {- plan_err!("Cannot have array values in a values list")- }- }- })- .collect::<Result<Vec<_>>>()- .and_then(ScalarValue::iter_to_array)- })- .collect::<Result<Vec<_>>>()?;-- let batch = RecordBatch::try_new_with_options(- Arc::clone(&schema),- arrays,- &RecordBatchOptions::new().with_row_count(Some(n_row)),- )?;-- let partitions = vec![batch];- Self::try_new_from_batches(Arc::clone(&schema), partitions)- }-- /// Create a new plan using the provided schema and batches.- ///- /// Errors if any of the batches don't match the provided schema, or if no- /// batches are provided.- pub fn try_new_from_batches(- schema: SchemaRef,- batches: Vec<RecordBatch>,- ) -> Result<Self> {- if batches.is_empty() {- return plan_err!("Values list cannot be empty");- }-- for batch in &batches {- let batch_schema = batch.schema();- if batch_schema != schema {- return plan_err!(- "Batch has invalid schema. Expected: {}, got: {}",- schema,- batch_schema- );- }- }-- let partitions = vec![batches];- let source = MemorySourceConfig {- partitions: partitions.clone(),- schema: Arc::clone(&schema),- projected_schema: Arc::clone(&schema),- projection: None,- sort_information: vec![],- show_sizes: true,- fetch: None,- };- let data_source = DataSourceExec::new(Arc::new(source));- Ok(Self {- inner: data_source,- partitions,- projection: None,- sort_information: vec![],- show_sizes: true,- })- }-- fn memory_source_config(&self) -> MemorySourceConfig {- self.inner- .source()- .as_any()- .downcast_ref::<MemorySourceConfig>()- .unwrap()- .clone()- }-- pub fn with_constraints(mut self, constraints: Constraints) -> Self {- self.inner = self.inner.with_constraints(constraints);- self- }-- /// Set `show_sizes` to determine whether to display partition sizes- pub fn with_show_sizes(mut self, show_sizes: bool) -> Self {- let mut memory_source = self.memory_source_config();- memory_source.show_sizes = show_sizes;- self.show_sizes = show_sizes;- self.inner = DataSourceExec::new(Arc::new(memory_source));- self- }-- /// Ref to constraints- pub fn constraints(&self) -> &Constraints {- self.properties().equivalence_properties().constraints()- }-- /// Ref to partitions- pub fn partitions(&self) -> &[Vec<RecordBatch>] {- &self.partitions- }-- /// Ref to projection- pub fn projection(&self) -> &Option<Vec<usize>> {- &self.projection- }-- /// Show sizes- pub fn show_sizes(&self) -> bool {- self.show_sizes- }-- /// Ref to sort information- pub fn sort_information(&self) -> &[LexOrdering] {- &self.sort_information- }-- /// A memory table can be ordered by multiple expressions simultaneously.- /// [`EquivalenceProperties`] keeps track of expressions that describe the- /// global ordering of the schema. These columns are not necessarily same; e.g.- /// ```text- /// ┌-------┐- /// | a | b |- /// |---|---|- /// | 1 | 9 |- /// | 2 | 8 |- /// | 3 | 7 |- /// | 5 | 5 |- /// └---┴---┘- /// ```- /// where both `a ASC` and `b DESC` can describe the table ordering. With- /// [`EquivalenceProperties`], we can keep track of these equivalences- /// and treat `a ASC` and `b DESC` as the same ordering requirement.- ///- /// Note that if there is an internal projection, that projection will be- /// also applied to the given `sort_information`.- pub fn try_with_sort_information(- mut self,- sort_information: Vec<LexOrdering>,- ) -> Result<Self> {- self.sort_information = sort_information.clone();- let mut memory_source = self.memory_source_config();- memory_source = memory_source.try_with_sort_information(sort_information)?;- self.inner = DataSourceExec::new(Arc::new(memory_source));- Ok(self)- }-- /// Arc clone of ref to original schema- pub fn original_schema(&self) -> SchemaRef {- Arc::clone(&self.inner.schema())- }-- /// This function creates the cache object that stores the plan properties such as schema, equivalence properties, ordering, partitioning, etc.- fn compute_properties(- schema: SchemaRef,- orderings: &[LexOrdering],- constraints: Constraints,- partitions: &[Vec<RecordBatch>],- ) -> PlanProperties {- PlanProperties::new(- EquivalenceProperties::new_with_orderings(schema, orderings)- .with_constraints(constraints),- Partitioning::UnknownPartitioning(partitions.len()),- EmissionType::Incremental,- Boundedness::Bounded,- )- }-}--/// Data source configuration for reading in-memory batches of data-#[derive(Clone)]-pub struct MemorySourceConfig {- /// The partitions to query- partitions: Vec<Vec<RecordBatch>>,- /// Schema representing the data before projection- schema: SchemaRef,- /// Schema representing the data after the optional projection is applied- projected_schema: SchemaRef,- /// Optional projection- projection: Option<Vec<usize>>,- /// Sort information: one or more equivalent orderings- sort_information: Vec<LexOrdering>,- /// if partition sizes should be displayed- show_sizes: bool,- /// The maximum number of records to read from this plan. If `None`,- /// all records after filtering are returned.- fetch: Option<usize>,-}--impl DataSource for MemorySourceConfig {- fn open(- &self,- partition: usize,- _context: Arc<TaskContext>,- ) -> Result<SendableRecordBatchStream> {- Ok(Box::pin(- MemoryStream::try_new(- self.partitions[partition].clone(),- Arc::clone(&self.projected_schema),- self.projection.clone(),- )?- .with_fetch(self.fetch),- ))- }-- fn as_any(&self) -> &dyn Any {- self- }-- fn fmt_as(&self, t: DisplayFormatType, f: &mut fmt::Formatter) -> fmt::Result {- match t {- DisplayFormatType::Default | DisplayFormatType::Verbose => {- let partition_sizes: Vec<_> =- self.partitions.iter().map(|b| b.len()).collect();-- let output_ordering = self- .sort_information- .first()- .map(|output_ordering| {- format!(", output_ordering={}", output_ordering)- })- .unwrap_or_default();-- let eq_properties = self.eq_properties();- let constraints = eq_properties.constraints();- let constraints = if constraints.is_empty() {- String::new()- } else {- format!(", {}", constraints)- };-- let limit = self- .fetch- .map_or(String::new(), |limit| format!(", fetch={}", limit));- if self.show_sizes {- write!(- f,- "partitions={}, partition_sizes={partition_sizes:?}{limit}{output_ordering}{constraints}",- partition_sizes.len(),- )- } else {- write!(- f,- "partitions={}{limit}{output_ordering}{constraints}",- partition_sizes.len(),- )- }- }- }- }-- fn output_partitioning(&self) -> Partitioning {- Partitioning::UnknownPartitioning(self.partitions.len())- }-- fn eq_properties(&self) -> EquivalenceProperties {- EquivalenceProperties::new_with_orderings(- Arc::clone(&self.projected_schema),- self.sort_information.as_slice(),- )- }-- fn statistics(&self) -> Result<Statistics> {- Ok(common::compute_record_batch_statistics(- &self.partitions,- &self.schema,- self.projection.clone(),- ))- }-- fn with_fetch(&self, limit: Option<usize>) -> Option<Arc<dyn DataSource>> {- let source = self.clone();- Some(Arc::new(source.with_limit(limit)))- }-- fn fetch(&self) -> Option<usize> {- self.fetch- }-- fn try_swapping_with_projection(- &self,- projection: &ProjectionExec,- ) -> Result<Option<Arc<dyn ExecutionPlan>>> {- // If there is any non-column or alias-carrier expression, Projection should not be removed.- // This process can be moved into MemoryExec, but it would be an overlap of their responsibility.- all_alias_free_columns(projection.expr())- .then(|| {- let all_projections = (0..self.schema.fields().len()).collect();- let new_projections = new_projections_for_columns(- projection,- self.projection().as_ref().unwrap_or(&all_projections),- );-- MemorySourceConfig::try_new_exec(- self.partitions(),- self.original_schema(),- Some(new_projections),- )- .map(|e| e as _)- })- .transpose()- }-}--impl MemorySourceConfig {- /// Create a new `MemorySourceConfig` for reading in-memory record batches- /// The provided `schema` should not have the projection applied.- pub fn try_new(- partitions: &[Vec<RecordBatch>],- schema: SchemaRef,- projection: Option<Vec<usize>>,- ) -> Result<Self> {- let projected_schema = project_schema(&schema, projection.as_ref())?;- Ok(Self {- partitions: partitions.to_vec(),- schema,- projected_schema,- projection,- sort_information: vec![],- show_sizes: true,- fetch: None,- })- }-- /// Create a new `DataSourceExec` plan for reading in-memory record batches- /// The provided `schema` should not have the projection applied.- pub fn try_new_exec(- partitions: &[Vec<RecordBatch>],- schema: SchemaRef,- projection: Option<Vec<usize>>,- ) -> Result<Arc<DataSourceExec>> {- let source = Self::try_new(partitions, schema, projection)?;- Ok(Arc::new(DataSourceExec::new(Arc::new(source))))- }-- /// Create a new execution plan from a list of constant values (`ValuesExec`)- pub fn try_new_as_values(- schema: SchemaRef,- data: Vec<Vec<Arc<dyn PhysicalExpr>>>,- ) -> Result<Arc<DataSourceExec>> {- if data.is_empty() {- return plan_err!("Values list cannot be empty");- }-- let n_row = data.len();- let n_col = schema.fields().len();-- // We have this single row batch as a placeholder to satisfy evaluation argument- // and generate a single output row- let placeholder_schema = Arc::new(Schema::empty());- let placeholder_batch = RecordBatch::try_new_with_options(- Arc::clone(&placeholder_schema),- vec![],- &RecordBatchOptions::new().with_row_count(Some(1)),- )?;-- // Evaluate each column- let arrays = (0..n_col)- .map(|j| {- (0..n_row)- .map(|i| {- let expr = &data[i][j];- let result = expr.evaluate(&placeholder_batch)?;-- match result {- ColumnarValue::Scalar(scalar) => Ok(scalar),- ColumnarValue::Array(array) if array.len() == 1 => {- ScalarValue::try_from_array(&array, 0)- }- ColumnarValue::Array(_) => {- plan_err!("Cannot have array values in a values list")- }- }- })- .collect::<Result<Vec<_>>>()- .and_then(ScalarValue::iter_to_array)- })- .collect::<Result<Vec<_>>>()?;-- let batch = RecordBatch::try_new_with_options(- Arc::clone(&schema),- arrays,- &RecordBatchOptions::new().with_row_count(Some(n_row)),- )?;-- let partitions = vec![batch];- Self::try_new_from_batches(Arc::clone(&schema), partitions)- }-- /// Create a new plan using the provided schema and batches.- ///- /// Errors if any of the batches don't match the provided schema, or if no- /// batches are provided.- pub fn try_new_from_batches(- schema: SchemaRef,- batches: Vec<RecordBatch>,- ) -> Result<Arc<DataSourceExec>> {- if batches.is_empty() {- return plan_err!("Values list cannot be empty");- }-- for batch in &batches {- let batch_schema = batch.schema();- if batch_schema != schema {- return plan_err!(- "Batch has invalid schema. Expected: {}, got: {}",- schema,- batch_schema- );- }- }-- let partitions = vec![batches];- let source = Self {- partitions,- schema: Arc::clone(&schema),- projected_schema: Arc::clone(&schema),- projection: None,- sort_information: vec![],- show_sizes: true,- fetch: None,- };- Ok(Arc::new(DataSourceExec::new(Arc::new(source))))- }-- /// Set the limit of the files- pub fn with_limit(mut self, limit: Option<usize>) -> Self {- self.fetch = limit;- self- }-- /// Set `show_sizes` to determine whether to display partition sizes- pub fn with_show_sizes(mut self, show_sizes: bool) -> Self {- self.show_sizes = show_sizes;- self- }-- /// Ref to partitions- pub fn partitions(&self) -> &[Vec<RecordBatch>] {- &self.partitions- }-- /// Ref to projection- pub fn projection(&self) -> &Option<Vec<usize>> {- &self.projection- }-- /// Show sizes- pub fn show_sizes(&self) -> bool {- self.show_sizes- }-- /// Ref to sort information- pub fn sort_information(&self) -> &[LexOrdering] {- &self.sort_information- }-- /// A memory table can be ordered by multiple expressions simultaneously.- /// [`EquivalenceProperties`] keeps track of expressions that describe the- /// global ordering of the schema. These columns are not necessarily same; e.g.- /// ```text- /// ┌-------┐- /// | a | b |- /// |---|---|- /// | 1 | 9 |- /// | 2 | 8 |- /// | 3 | 7 |- /// | 5 | 5 |- /// └---┴---┘- /// ```- /// where both `a ASC` and `b DESC` can describe the table ordering. With- /// [`EquivalenceProperties`], we can keep track of these equivalences- /// and treat `a ASC` and `b DESC` as the same ordering requirement.- ///- /// Note that if there is an internal projection, that projection will be- /// also applied to the given `sort_information`.- pub fn try_with_sort_information(- mut self,- mut sort_information: Vec<LexOrdering>,- ) -> Result<Self> {- // All sort expressions must refer to the original schema- let fields = self.schema.fields();- let ambiguous_column = sort_information- .iter()- .flat_map(|ordering| ordering.clone())- .flat_map(|expr| collect_columns(&expr.expr))- .find(|col| {- fields- .get(col.index())- .map(|field| field.name() != col.name())- .unwrap_or(true)- });- if let Some(col) = ambiguous_column {- return internal_err!(- "Column {:?} is not found in the original schema of the MemorySourceConfig",- col- );- }-- // If there is a projection on the source, we also need to project orderings- if let Some(projection) = &self.projection {- let base_eqp = EquivalenceProperties::new_with_orderings(- self.original_schema(),- &sort_information,- );- let proj_exprs = projection- .iter()- .map(|idx| {- let base_schema = self.original_schema();- let name = base_schema.field(*idx).name();- (Arc::new(Column::new(name, *idx)) as _, name.to_string())- })- .collect::<Vec<_>>();- let projection_mapping =- ProjectionMapping::try_new(&proj_exprs, &self.original_schema())?;- sort_information = base_eqp- .project(&projection_mapping, Arc::clone(&self.projected_schema))- .into_oeq_class()- .into_inner();- }-- self.sort_information = sort_information;- Ok(self)- }-- /// Arc clone of ref to original schema- pub fn original_schema(&self) -> SchemaRef {- Arc::clone(&self.schema)- }-}-/// Iterator over batchespub struct MemoryStream {/// Vector of record batches@@ -746,13 +72,13 @@ impl MemoryStream {}/// Set the memory reservation for the data- pub(super) fn with_reservation(mut self, reservation: MemoryReservation) -> Self {+ pub fn with_reservation(mut self, reservation: MemoryReservation) -> Self {self.reservation = Some(reservation);self}/// Set the number of rows to produce- pub(super) fn with_fetch(mut self, fetch: Option<usize>) -> Self {+ pub fn with_fetch(mut self, fetch: Option<usize>) -> Self {self.fetch = fetch;self}@@ -962,62 +288,6 @@ impl RecordBatchStream for LazyMemoryStream {}}-#[cfg(test)]-mod memory_exec_tests {- use std::sync::Arc;-- use crate::memory::MemorySourceConfig;- use crate::source::DataSourceExec;- use crate::ExecutionPlan;-- use arrow::compute::SortOptions;- use arrow::datatypes::{DataType, Field, Schema};- use datafusion_physical_expr::expressions::col;- use datafusion_physical_expr::PhysicalSortExpr;- use datafusion_physical_expr_common::sort_expr::LexOrdering;-- #[test]- fn test_memory_order_eq() -> datafusion_common::Result<()> {- let schema = Arc::new(Schema::new(vec![- Field::new("a", DataType::Int64, false),- Field::new("b", DataType::Int64, false),- Field::new("c", DataType::Int64, false),- ]));- let sort1 = LexOrdering::new(vec![- PhysicalSortExpr {- expr: col("a", &schema)?,- options: SortOptions::default(),- },- PhysicalSortExpr {- expr: col("b", &schema)?,- options: SortOptions::default(),- },- ]);- let sort2 = LexOrdering::new(vec![PhysicalSortExpr {- expr: col("c", &schema)?,- options: SortOptions::default(),- }]);- let mut expected_output_order = LexOrdering::default();- expected_output_order.extend(sort1.clone());- expected_output_order.extend(sort2.clone());-- let sort_information = vec![sort1.clone(), sort2.clone()];- let mem_exec = Arc::new(DataSourceExec::new(Arc::new(- MemorySourceConfig::try_new(&[vec![]], schema, None)?- .try_with_sort_information(sort_information)?,- )));-- assert_eq!(- mem_exec.properties().output_ordering().unwrap(),- &expected_output_order- );- let eq_properties = mem_exec.properties().equivalence_properties();- assert!(eq_properties.oeq_class().contains(&sort1));- assert!(eq_properties.oeq_class().contains(&sort2));- Ok(())- }-}-#[cfg(test)]mod lazy_memory_tests {use super::*;@@ -1137,129 +407,3 @@ mod lazy_memory_tests {Ok(())}}--#[cfg(test)]-mod tests {- use super::*;- use crate::expressions::lit;- use crate::test::{self, make_partition};-- use arrow::datatypes::{DataType, Field};- use datafusion_common::assert_batches_eq;- use datafusion_common::stats::{ColumnStatistics, Precision};- use futures::StreamExt;-- #[tokio::test]- async fn exec_with_limit() -> Result<()> {- let task_ctx = Arc::new(TaskContext::default());- let batch = make_partition(7);- let schema = batch.schema();- let batches = vec![batch.clone(), batch];-- let exec = MemorySourceConfig::try_new_from_batches(schema, batches).unwrap();- assert_eq!(exec.fetch(), None);-- let exec = exec.with_fetch(Some(4)).unwrap();- assert_eq!(exec.fetch(), Some(4));-- let mut it = exec.execute(0, task_ctx)?;- let mut results = vec![];- while let Some(batch) = it.next().await {- results.push(batch?);- }-- let expected = [- "+---+", "| i |", "+---+", "| 0 |", "| 1 |", "| 2 |", "| 3 |", "+---+",- ];- assert_batches_eq!(expected, &results);- Ok(())- }-- #[tokio::test]- async fn values_empty_case() -> Result<()> {- let schema = test::aggr_test_schema();- let empty = MemorySourceConfig::try_new_as_values(schema, vec![]);- assert!(empty.is_err());- Ok(())- }-- #[test]- fn new_exec_with_batches() {- let batch = make_partition(7);- let schema = batch.schema();- let batches = vec![batch.clone(), batch];- let _exec = MemorySourceConfig::try_new_from_batches(schema, batches).unwrap();- }-- #[test]- fn new_exec_with_batches_empty() {- let batch = make_partition(7);- let schema = batch.schema();- let _ = MemorySourceConfig::try_new_from_batches(schema, Vec::new()).unwrap_err();- }-- #[test]- fn new_exec_with_batches_invalid_schema() {- let batch = make_partition(7);- let batches = vec![batch.clone(), batch];-- let invalid_schema = Arc::new(Schema::new(vec![- Field::new("col0", DataType::UInt32, false),- Field::new("col1", DataType::Utf8, false),- ]));- let _ = MemorySourceConfig::try_new_from_batches(invalid_schema, batches)- .unwrap_err();- }-- // Test issue: https://github.com/apache/datafusion/issues/8763- #[test]- fn new_exec_with_non_nullable_schema() {- let schema = Arc::new(Schema::new(vec![Field::new(- "col0",- DataType::UInt32,- false,- )]));- let _ = MemorySourceConfig::try_new_as_values(- Arc::clone(&schema),- vec![vec![lit(1u32)]],- )- .unwrap();- // Test that a null value is rejected- let _ = MemorySourceConfig::try_new_as_values(- schema,- vec![vec![lit(ScalarValue::UInt32(None))]],- )- .unwrap_err();- }-- #[test]- fn values_stats_with_nulls_only() -> Result<()> {- let data = vec![- vec![lit(ScalarValue::Null)],- vec![lit(ScalarValue::Null)],- vec![lit(ScalarValue::Null)],- ];- let rows = data.len();- let values = MemorySourceConfig::try_new_as_values(- Arc::new(Schema::new(vec![Field::new("col0", DataType::Null, true)])),- data,- )?;-- assert_eq!(- values.statistics()?,- Statistics {- num_rows: Precision::Exact(rows),- total_byte_size: Precision::Exact(8), // not important- column_statistics: vec![ColumnStatistics {- null_count: Precision::Exact(rows), // there are only nulls- distinct_count: Precision::Absent,- max_value: Precision::Absent,- min_value: Precision::Absent,- sum_value: Precision::Absent,- },],- }- );-- Ok(())- }-}diff --git a/datafusion/physical-plan/src/placeholder_row.rs b/datafusion/physical-plan/src/placeholder_row.rsindex 6a8f247ec0..6e31f601e1 100644--- a/datafusion/physical-plan/src/placeholder_row.rs+++ b/datafusion/physical-plan/src/placeholder_row.rs@@ -20,10 +20,10 @@use std::any::Any;use std::sync::Arc;-use super::{common, DisplayAs, PlanProperties, SendableRecordBatchStream, Statistics};use crate::execution_plan::{Boundedness, EmissionType};-use crate::{memory::MemoryStream, DisplayFormatType, ExecutionPlan, Partitioning};-+use crate::memory::MemoryStream;+use crate::{common, DisplayAs, PlanProperties, SendableRecordBatchStream, Statistics};+use crate::{DisplayFormatType, ExecutionPlan, Partitioning};use arrow::array::{ArrayRef, NullArray};use arrow::array::{RecordBatch, RecordBatchOptions};use arrow::datatypes::{DataType, Field, Fields, Schema, SchemaRef};@@ -178,7 +178,8 @@ impl ExecutionPlan for PlaceholderRowExec {#[cfg(test)]mod tests {use super::*;- use crate::{test, with_new_children_if_necessary};+ use crate::test;+ use crate::with_new_children_if_necessary;#[test]fn with_new_children() -> Result<()> {diff --git a/datafusion/physical-plan/src/recursive_query.rs b/datafusion/physical-plan/src/recursive_query.rsindex bf7d2c7f27..05b78e4e1d 100644--- a/datafusion/physical-plan/src/recursive_query.rs+++ b/datafusion/physical-plan/src/recursive_query.rs@@ -21,12 +21,12 @@ use std::any::Any;use std::sync::Arc;use std::task::{Context, Poll};-use super::{+use super::work_table::{ReservedBatches, WorkTable, WorkTableExec};+use crate::execution_plan::{Boundedness, EmissionType};+use crate::{metrics::{BaselineMetrics, ExecutionPlanMetricsSet, MetricsSet},- work_table::{ReservedBatches, WorkTable, WorkTableExec},PlanProperties, RecordBatchStream, SendableRecordBatchStream, Statistics,};-use crate::execution_plan::{Boundedness, EmissionType};use crate::{DisplayAs, DisplayFormatType, ExecutionPlan};use arrow::datatypes::SchemaRef;@@ -156,10 +156,10 @@ impl ExecutionPlan for RecursiveQueryExec {vec![false, false]}- fn required_input_distribution(&self) -> Vec<datafusion_physical_expr::Distribution> {+ fn required_input_distribution(&self) -> Vec<crate::Distribution> {vec![- datafusion_physical_expr::Distribution::SinglePartition,- datafusion_physical_expr::Distribution::SinglePartition,+ crate::Distribution::SinglePartition,+ crate::Distribution::SinglePartition,]}diff --git a/datafusion/physical-plan/src/repartition/mod.rs b/datafusion/physical-plan/src/repartition/mod.rsindex 25668fa67d..40e68cfcae 100644--- a/datafusion/physical-plan/src/repartition/mod.rs+++ b/datafusion/physical-plan/src/repartition/mod.rs@@ -1051,6 +1051,7 @@ mod tests {use std::collections::HashSet;use super::*;+ use crate::test::TestMemoryExec;use crate::{test::{assert_is_pending,@@ -1059,7 +1060,7 @@ mod tests {ErrorExec, MockExec,},},- {collect, expressions::col, memory::MemorySourceConfig},+ {collect, expressions::col},};use arrow::array::{ArrayRef, StringArray, UInt32Array};@@ -1164,11 +1165,8 @@ mod tests {) -> Result<Vec<Vec<RecordBatch>>> {let task_ctx = Arc::new(TaskContext::default());// create physical plan- let exec = MemorySourceConfig::try_new_exec(- &input_partitions,- Arc::clone(schema),- None,- )?;+ let exec =+ TestMemoryExec::try_new_exec(&input_partitions, Arc::clone(schema), None)?;let exec = RepartitionExec::try_new(exec, partitioning)?;// execute and collect results@@ -1559,11 +1557,8 @@ mod tests {let task_ctx = Arc::new(task_ctx);// create physical plan- let exec = MemorySourceConfig::try_new_exec(- &input_partitions,- Arc::clone(&schema),- None,- )?;+ let exec =+ TestMemoryExec::try_new_exec(&input_partitions, Arc::clone(&schema), None)?;let exec = RepartitionExec::try_new(exec, partitioning)?;// pull partitions@@ -1604,8 +1599,7 @@ mod test {use arrow::datatypes::{DataType, Field, Schema};use super::*;- use crate::memory::MemorySourceConfig;- use crate::source::DataSourceExec;+ use crate::test::TestMemoryExec;use crate::union::UnionExec;use datafusion_physical_expr::expressions::col;@@ -1711,15 +1705,15 @@ mod test {}fn memory_exec(schema: &SchemaRef) -> Arc<dyn ExecutionPlan> {- MemorySourceConfig::try_new_exec(&[vec![]], Arc::clone(schema), None).unwrap()+ TestMemoryExec::try_new_exec(&[vec![]], Arc::clone(schema), None).unwrap()}fn sorted_memory_exec(schema: &SchemaRef,sort_exprs: LexOrdering,) -> Arc<dyn ExecutionPlan> {- Arc::new(DataSourceExec::new(Arc::new(- MemorySourceConfig::try_new(&[vec![]], Arc::clone(schema), None)+ Arc::new(TestMemoryExec::update_cache(Arc::new(+ TestMemoryExec::try_new(&[vec![]], Arc::clone(schema), None).unwrap().try_with_sort_information(vec![sort_exprs]).unwrap(),diff --git a/datafusion/physical-plan/src/sorts/partial_sort.rs b/datafusion/physical-plan/src/sorts/partial_sort.rsindex eeef73c45f..dc03c012d9 100644--- a/datafusion/physical-plan/src/sorts/partial_sort.rs+++ b/datafusion/physical-plan/src/sorts/partial_sort.rs@@ -466,11 +466,11 @@ mod tests {use crate::collect;use crate::expressions::col;use crate::expressions::PhysicalSortExpr;- use crate::memory::MemorySourceConfig;use crate::sorts::sort::SortExec;use crate::test;use crate::test::assert_is_pending;use crate::test::exec::{assert_strong_count_converges_to_zero, BlockingExec};+ use crate::test::TestMemoryExec;use super::*;@@ -696,7 +696,7 @@ mod tests {);let schema = batch1.schema();- MemorySourceConfig::try_new_exec(+ TestMemoryExec::try_new_exec(&[vec![batch1, batch2, batch3, batch4]],Arc::clone(&schema),None,@@ -881,7 +881,7 @@ mod tests {let batch = RecordBatch::try_new(Arc::clone(&schema), vec![data])?;let input =- MemorySourceConfig::try_new_exec(&[vec![batch]], Arc::clone(&schema), None)?;+ TestMemoryExec::try_new_exec(&[vec![batch]], Arc::clone(&schema), None)?;let partial_sort_exec = Arc::new(PartialSortExec::new(LexOrdering::new(vec![PhysicalSortExpr {@@ -987,7 +987,7 @@ mod tests {options: option_desc,},]),- MemorySourceConfig::try_new_exec(&[vec![batch]], schema, None)?,+ TestMemoryExec::try_new_exec(&[vec![batch]], schema, None)?,2,));diff --git a/datafusion/physical-plan/src/sorts/sort.rs b/datafusion/physical-plan/src/sorts/sort.rsindex 649468260e..30b5abcf88 100644--- a/datafusion/physical-plan/src/sorts/sort.rs+++ b/datafusion/physical-plan/src/sorts/sort.rs@@ -1142,10 +1142,10 @@ mod tests {use crate::collect;use crate::execution_plan::Boundedness;use crate::expressions::col;- use crate::memory::MemorySourceConfig;use crate::test;use crate::test::assert_is_pending;use crate::test::exec::{assert_strong_count_converges_to_zero, BlockingExec};+ use crate::test::TestMemoryExec;use arrow::array::*;use arrow::compute::SortOptions;@@ -1531,7 +1531,7 @@ mod tests {let batch = RecordBatch::try_new(Arc::clone(&schema), vec![data]).unwrap();let input =- MemorySourceConfig::try_new_exec(&[vec![batch]], Arc::clone(&schema), None)+ TestMemoryExec::try_new_exec(&[vec![batch]], Arc::clone(&schema), None).unwrap();let sort_exec = Arc::new(SortExec::new(@@ -1602,7 +1602,7 @@ mod tests {},},]),- MemorySourceConfig::try_new_exec(&[vec![batch]], Arc::clone(&schema), None)?,+ TestMemoryExec::try_new_exec(&[vec![batch]], Arc::clone(&schema), None)?,));assert_eq!(DataType::Int32, *sort_exec.schema().field(0).data_type());@@ -1688,7 +1688,7 @@ mod tests {},},]),- MemorySourceConfig::try_new_exec(&[vec![batch]], schema, None)?,+ TestMemoryExec::try_new_exec(&[vec![batch]], schema, None)?,));assert_eq!(DataType::Float32, *sort_exec.schema().field(0).data_type());diff --git a/datafusion/physical-plan/src/sorts/sort_preserving_merge.rs b/datafusion/physical-plan/src/sorts/sort_preserving_merge.rsindex 2cc55d6029..454a068551 100644--- a/datafusion/physical-plan/src/sorts/sort_preserving_merge.rs+++ b/datafusion/physical-plan/src/sorts/sort_preserving_merge.rs@@ -384,12 +384,12 @@ mod tests {use crate::coalesce_partitions::CoalescePartitionsExec;use crate::execution_plan::{Boundedness, EmissionType};use crate::expressions::col;- use crate::memory::MemorySourceConfig;use crate::metrics::{MetricValue, Timestamp};use crate::repartition::RepartitionExec;use crate::sorts::sort::SortExec;use crate::stream::RecordBatchReceiverStream;use crate::test::exec::{assert_strong_count_converges_to_zero, BlockingExec};+ use crate::test::TestMemoryExec;use crate::test::{self, assert_is_pending, make_partition};use crate::{collect, common};@@ -451,7 +451,7 @@ mod tests {]);let repartition_exec = RepartitionExec::try_new(- MemorySourceConfig::try_new_exec(&[rbs], schema, None).unwrap(),+ TestMemoryExec::try_new_exec(&[rbs], schema, None).unwrap(),Partitioning::RoundRobinBatch(2),)?;let coalesce_batches_exec =@@ -543,7 +543,7 @@ mod tests {let schema = batch.schema();let sort = LexOrdering::default(); // no sort expressions- let exec = MemorySourceConfig::try_new_exec(+ let exec = TestMemoryExec::try_new_exec(&[vec![batch.clone()], vec![batch]],schema,None,@@ -736,7 +736,7 @@ mod tests {options: Default::default(),},]);- let exec = MemorySourceConfig::try_new_exec(partitions, schema, None).unwrap();+ let exec = TestMemoryExec::try_new_exec(partitions, schema, None).unwrap();let merge = Arc::new(SortPreservingMergeExec::new(sort, exec));let collected = collect(merge, context).await.unwrap();@@ -844,7 +844,7 @@ mod tests {let sorted = basic_sort(csv, sort, context).await;let split: Vec<_> = sizes.iter().map(|x| split_batch(&sorted, *x)).collect();- Ok(MemorySourceConfig::try_new_exec(&split, sorted.schema(), None).unwrap())+ Ok(TestMemoryExec::try_new_exec(&split, sorted.schema(), None).unwrap())}#[tokio::test]@@ -972,8 +972,8 @@ mod tests {},},]);- let exec = MemorySourceConfig::try_new_exec(&[vec![b1], vec![b2]], schema, None)- .unwrap();+ let exec =+ TestMemoryExec::try_new_exec(&[vec![b1], vec![b2]], schema, None).unwrap();let merge = Arc::new(SortPreservingMergeExec::new(sort, exec));let collected = collect(merge, task_ctx).await.unwrap();@@ -1015,8 +1015,7 @@ mod tests {nulls_first: true,},}]);- let exec =- MemorySourceConfig::try_new_exec(&[vec![batch]], schema, None).unwrap();+ let exec = TestMemoryExec::try_new_exec(&[vec![batch]], schema, None).unwrap();let merge =Arc::new(SortPreservingMergeExec::new(sort, exec).with_fetch(Some(2)));@@ -1051,8 +1050,7 @@ mod tests {nulls_first: true,},}]);- let exec =- MemorySourceConfig::try_new_exec(&[vec![batch]], schema, None).unwrap();+ let exec = TestMemoryExec::try_new_exec(&[vec![batch]], schema, None).unwrap();let merge = Arc::new(SortPreservingMergeExec::new(sort, exec));let collected = collect(merge, task_ctx).await.unwrap();@@ -1161,8 +1159,8 @@ mod tests {expr: col("b", &schema).unwrap(),options: Default::default(),}]);- let exec = MemorySourceConfig::try_new_exec(&[vec![b1], vec![b2]], schema, None)- .unwrap();+ let exec =+ TestMemoryExec::try_new_exec(&[vec![b1], vec![b2]], schema, None).unwrap();let merge = Arc::new(SortPreservingMergeExec::new(sort, exec));let collected = collect(Arc::clone(&merge) as Arc<dyn ExecutionPlan>, task_ctx)@@ -1273,7 +1271,7 @@ mod tests {},}]);- let exec = MemorySourceConfig::try_new_exec(&partitions, schema, None).unwrap();+ let exec = TestMemoryExec::try_new_exec(&partitions, schema, None).unwrap();let merge = Arc::new(SortPreservingMergeExec::new(sort, exec));let collected = collect(merge, task_ctx).await.unwrap();diff --git a/datafusion/physical-plan/src/test.rs b/datafusion/physical-plan/src/test.rsindex ad0e43503b..7d0e377845 100644--- a/datafusion/physical-plan/src/test.rs+++ b/datafusion/physical-plan/src/test.rs@@ -17,27 +17,337 @@//! Utilities for testing datafusion-physical-plan+use std::any::Any;use std::collections::HashMap;+use std::fmt;+use std::fmt::{Debug, Formatter};use std::pin::Pin;use std::sync::Arc;+use std::task::Context;++use crate::common;+use crate::execution_plan::{Boundedness, EmissionType};+use crate::memory::MemoryStream;+use crate::metrics::MetricsSet;+use crate::stream::RecordBatchStreamAdapter;+use crate::streaming::PartitionStream;+use crate::ExecutionPlan;+use crate::{DisplayAs, DisplayFormatType, PlanProperties};use arrow::array::{Array, ArrayRef, Int32Array, RecordBatch};use arrow_schema::{DataType, Field, Schema, SchemaRef};+use datafusion_common::{+ config::ConfigOptions, internal_err, project_schema, Result, Statistics,+};use datafusion_execution::{SendableRecordBatchStream, TaskContext};-use futures::{Future, FutureExt};+use datafusion_physical_expr::{+ equivalence::ProjectionMapping, expressions::Column, utils::collect_columns,+ EquivalenceProperties, LexOrdering, Partitioning,+};-use crate::memory::MemorySourceConfig;-use crate::source::DataSourceExec;-use crate::stream::RecordBatchStreamAdapter;-use crate::streaming::PartitionStream;-use crate::ExecutionPlan;+use futures::{Future, FutureExt};pub mod exec;+/// `TestMemoryExec` is a mock equivalent to [`MemorySourceConfig`] with [`ExecutionPlan`] implemented for testing.+/// i.e. It has some but not all the functionality of [`MemorySourceConfig`].+/// This implements an in-memory DataSource rather than explicitly implementing a trait.+/// It is implemented in this manner to keep relevant unit tests in place+/// while avoiding circular dependencies between `datafusion-physical-plan` and `datafusion-datasource`.+///+/// [`MemorySourceConfig`]: https://github.com/apache/datafusion/tree/main/datafusion/datasource/src/memory.rs+#[derive(Clone, Debug)]+pub struct TestMemoryExec {+ /// The partitions to query+ partitions: Vec<Vec<RecordBatch>>,+ /// Schema representing the data before projection+ schema: SchemaRef,+ /// Schema representing the data after the optional projection is applied+ projected_schema: SchemaRef,+ /// Optional projection+ projection: Option<Vec<usize>>,+ /// Sort information: one or more equivalent orderings+ sort_information: Vec<LexOrdering>,+ /// if partition sizes should be displayed+ show_sizes: bool,+ /// The maximum number of records to read from this plan. If `None`,+ /// all records after filtering are returned.+ fetch: Option<usize>,+ cache: PlanProperties,+}++impl DisplayAs for TestMemoryExec {+ fn fmt_as(&self, t: DisplayFormatType, f: &mut Formatter) -> fmt::Result {+ write!(f, "DataSourceExec: ")?;+ match t {+ DisplayFormatType::Default | DisplayFormatType::Verbose => {+ let partition_sizes: Vec<_> =+ self.partitions.iter().map(|b| b.len()).collect();++ let output_ordering = self+ .sort_information+ .first()+ .map(|output_ordering| {+ format!(", output_ordering={}", output_ordering)+ })+ .unwrap_or_default();++ let eq_properties = self.eq_properties();+ let constraints = eq_properties.constraints();+ let constraints = if constraints.is_empty() {+ String::new()+ } else {+ format!(", {}", constraints)+ };++ let limit = self+ .fetch+ .map_or(String::new(), |limit| format!(", fetch={}", limit));+ if self.show_sizes {+ write!(+ f,+ "partitions={}, partition_sizes={partition_sizes:?}{limit}{output_ordering}{constraints}",+ partition_sizes.len(),+ )+ } else {+ write!(+ f,+ "partitions={}{limit}{output_ordering}{constraints}",+ partition_sizes.len(),+ )+ }+ }+ }+ }+}++impl ExecutionPlan for TestMemoryExec {+ fn name(&self) -> &'static str {+ "DataSourceExec"+ }++ fn as_any(&self) -> &dyn Any {+ unimplemented!()+ }++ fn properties(&self) -> &PlanProperties {+ &self.cache+ }++ fn children(&self) -> Vec<&Arc<dyn ExecutionPlan>> {+ Vec::new()+ }++ fn with_new_children(+ self: Arc<Self>,+ _: Vec<Arc<dyn ExecutionPlan>>,+ ) -> Result<Arc<dyn ExecutionPlan>> {+ unimplemented!()+ }++ fn repartitioned(+ &self,+ _target_partitions: usize,+ _config: &ConfigOptions,+ ) -> Result<Option<Arc<dyn ExecutionPlan>>> {+ unimplemented!()+ }++ fn execute(+ &self,+ partition: usize,+ context: Arc<TaskContext>,+ ) -> Result<SendableRecordBatchStream> {+ self.open(partition, context)+ }++ fn metrics(&self) -> Option<MetricsSet> {+ unimplemented!()+ }++ fn statistics(&self) -> Result<Statistics> {+ self.statistics()+ }++ fn fetch(&self) -> Option<usize> {+ self.fetch+ }+}++impl TestMemoryExec {+ fn open(+ &self,+ partition: usize,+ _context: Arc<TaskContext>,+ ) -> Result<SendableRecordBatchStream> {+ Ok(Box::pin(+ MemoryStream::try_new(+ self.partitions[partition].clone(),+ Arc::clone(&self.projected_schema),+ self.projection.clone(),+ )?+ .with_fetch(self.fetch),+ ))+ }++ fn compute_properties(&self) -> PlanProperties {+ PlanProperties::new(+ self.eq_properties(),+ self.output_partitioning(),+ EmissionType::Incremental,+ Boundedness::Bounded,+ )+ }++ fn output_partitioning(&self) -> Partitioning {+ Partitioning::UnknownPartitioning(self.partitions.len())+ }++ fn eq_properties(&self) -> EquivalenceProperties {+ EquivalenceProperties::new_with_orderings(+ Arc::clone(&self.projected_schema),+ self.sort_information.as_slice(),+ )+ }++ fn statistics(&self) -> Result<Statistics> {+ Ok(common::compute_record_batch_statistics(+ &self.partitions,+ &self.schema,+ self.projection.clone(),+ ))+ }++ pub fn try_new(+ partitions: &[Vec<RecordBatch>],+ schema: SchemaRef,+ projection: Option<Vec<usize>>,+ ) -> Result<Self> {+ let projected_schema = project_schema(&schema, projection.as_ref())?;+ Ok(Self {+ partitions: partitions.to_vec(),+ schema,+ cache: PlanProperties::new(+ EquivalenceProperties::new_with_orderings(+ Arc::clone(&projected_schema),+ vec![].as_slice(),+ ),+ Partitioning::UnknownPartitioning(partitions.len()),+ EmissionType::Incremental,+ Boundedness::Bounded,+ ),+ projected_schema,+ projection,+ sort_information: vec![],+ show_sizes: true,+ fetch: None,+ })+ }++ /// Create a new `DataSourceExec` Equivalent plan for reading in-memory record batches+ /// The provided `schema` should not have the projection applied.+ pub fn try_new_exec(+ partitions: &[Vec<RecordBatch>],+ schema: SchemaRef,+ projection: Option<Vec<usize>>,+ ) -> Result<Arc<TestMemoryExec>> {+ let mut source = Self::try_new(partitions, schema, projection)?;+ let cache = source.compute_properties();+ source.cache = cache;+ Ok(Arc::new(source))+ }++ // Equivalent of `DataSourceExec::new`+ pub fn update_cache(source: Arc<TestMemoryExec>) -> TestMemoryExec {+ let cache = source.compute_properties();+ let source = &*source;+ let mut source = source.clone();+ source.cache = cache;+ source+ }++ /// Set the limit of the files+ pub fn with_limit(mut self, limit: Option<usize>) -> Self {+ self.fetch = limit;+ self+ }++ /// Ref to partitions+ pub fn partitions(&self) -> &[Vec<RecordBatch>] {+ &self.partitions+ }++ /// Ref to projection+ pub fn projection(&self) -> &Option<Vec<usize>> {+ &self.projection+ }++ /// Ref to sort information+ pub fn sort_information(&self) -> &[LexOrdering] {+ &self.sort_information+ }++ /// refer to `try_with_sort_information` at MemorySourceConfig for more information.+ /// https://github.com/apache/datafusion/tree/main/datafusion/datasource/src/memory.rs+ pub fn try_with_sort_information(+ mut self,+ mut sort_information: Vec<LexOrdering>,+ ) -> Result<Self> {+ // All sort expressions must refer to the original schema+ let fields = self.schema.fields();+ let ambiguous_column = sort_information+ .iter()+ .flat_map(|ordering| ordering.clone())+ .flat_map(|expr| collect_columns(&expr.expr))+ .find(|col| {+ fields+ .get(col.index())+ .map(|field| field.name() != col.name())+ .unwrap_or(true)+ });+ if let Some(col) = ambiguous_column {+ return internal_err!(+ "Column {:?} is not found in the original schema of the TestMemoryExec",+ col+ );+ }++ // If there is a projection on the source, we also need to project orderings+ if let Some(projection) = &self.projection {+ let base_eqp = EquivalenceProperties::new_with_orderings(+ self.original_schema(),+ &sort_information,+ );+ let proj_exprs = projection+ .iter()+ .map(|idx| {+ let base_schema = self.original_schema();+ let name = base_schema.field(*idx).name();+ (Arc::new(Column::new(name, *idx)) as _, name.to_string())+ })+ .collect::<Vec<_>>();+ let projection_mapping =+ ProjectionMapping::try_new(&proj_exprs, &self.original_schema())?;+ sort_information = base_eqp+ .project(&projection_mapping, Arc::clone(&self.projected_schema))+ .into_oeq_class()+ .into_inner();+ }++ self.sort_information = sort_information;+ Ok(self)+ }++ /// Arc clone of ref to original schema+ pub fn original_schema(&self) -> SchemaRef {+ Arc::clone(&self.schema)+ }+}+/// Asserts that given future is pending.pub fn assert_is_pending<'a, T>(fut: &mut Pin<Box<dyn Future<Output = T> + Send + 'a>>) {let waker = futures::task::noop_waker();- let mut cx = futures::task::Context::from_waker(&waker);+ let mut cx = Context::from_waker(&waker);let poll = fut.poll_unpin(&mut cx);assert!(poll.is_pending());@@ -117,7 +427,7 @@ pub fn build_table_scan_i32() -> Arc<dyn ExecutionPlan> {let batch = build_table_i32(a, b, c);let schema = batch.schema();- MemorySourceConfig::try_new_exec(&[vec![batch]], schema, None).unwrap()+ TestMemoryExec::try_new_exec(&[vec![batch]], schema, None).unwrap()}/// Return a RecordBatch with a single Int32 array with values (0..sz) in a field named "i"@@ -157,26 +467,24 @@ pub fn scan_partitioned_utf8(partitions: usize) -> Arc<dyn ExecutionPlan> {}/// Returns a `DataSourceExec` that scans `partitions` of 100 batches each-pub fn mem_exec(partitions: usize) -> DataSourceExec {+pub fn mem_exec(partitions: usize) -> TestMemoryExec {let data: Vec<Vec<_>> = (0..partitions).map(|_| vec![make_partition(100)]).collect();let schema = data[0][0].schema();let projection = None;- DataSourceExec::new(Arc::new(- MemorySourceConfig::try_new(&data, schema, projection).unwrap(),- ))++ TestMemoryExec::try_new(&data, schema, projection).unwrap()}-pub fn mem_exec_utf8(partitions: usize) -> DataSourceExec {+pub fn mem_exec_utf8(partitions: usize) -> TestMemoryExec {let data: Vec<Vec<_>> = (0..partitions).map(|_| vec![make_partition_utf8(100)]).collect();let schema = data[0][0].schema();let projection = None;- DataSourceExec::new(Arc::new(- MemorySourceConfig::try_new(&data, schema, projection).unwrap(),- ))++ TestMemoryExec::try_new(&data, schema, projection).unwrap()}// Construct a stream partition for test purposesdiff --git a/datafusion/physical-plan/src/union.rs b/datafusion/physical-plan/src/union.rsindex e1972d267b..68d1803b71 100644--- a/datafusion/physical-plan/src/union.rs+++ b/datafusion/physical-plan/src/union.rs@@ -640,10 +640,9 @@ fn stats_union(mut left: Statistics, right: Statistics) -> Statistics {mod tests {use super::*;use crate::collect;- use crate::memory::MemorySourceConfig;use crate::test;+ use crate::test::TestMemoryExec;- use crate::source::DataSourceExec;use arrow::compute::SortOptions;use arrow::datatypes::DataType;use datafusion_common::ScalarValue;@@ -865,12 +864,12 @@ mod tests {.iter().map(|ordering| convert_to_sort_exprs(ordering)).collect::<Vec<_>>();- let child1 = Arc::new(DataSourceExec::new(Arc::new(- MemorySourceConfig::try_new(&[], Arc::clone(&schema), None)?+ let child1 = Arc::new(TestMemoryExec::update_cache(Arc::new(+ TestMemoryExec::try_new(&[], Arc::clone(&schema), None)?.try_with_sort_information(first_orderings)?,)));- let child2 = Arc::new(DataSourceExec::new(Arc::new(- MemorySourceConfig::try_new(&[], Arc::clone(&schema), None)?+ let child2 = Arc::new(TestMemoryExec::update_cache(Arc::new(+ TestMemoryExec::try_new(&[], Arc::clone(&schema), None)?.try_with_sort_information(second_orderings)?,)));diff --git a/datafusion/physical-plan/src/values.rs b/datafusion/physical-plan/src/values.rsindex 6ab5cc84a2..b90c50510c 100644--- a/datafusion/physical-plan/src/values.rs+++ b/datafusion/physical-plan/src/values.rs@@ -20,13 +20,12 @@use std::any::Any;use std::sync::Arc;-use super::{common, DisplayAs, PlanProperties, SendableRecordBatchStream, Statistics};use crate::execution_plan::{Boundedness, EmissionType};+use crate::memory::MemoryStream;+use crate::{common, DisplayAs, PlanProperties, SendableRecordBatchStream, Statistics};use crate::{- memory::MemoryStream, ColumnarValue, DisplayFormatType, ExecutionPlan, Partitioning,- PhysicalExpr,+ ColumnarValue, DisplayFormatType, ExecutionPlan, Partitioning, PhysicalExpr,};-use arrow::datatypes::{Schema, SchemaRef};use arrow::record_batch::{RecordBatch, RecordBatchOptions};use datafusion_common::{internal_err, plan_err, Result, ScalarValue};diff --git a/datafusion/physical-plan/src/windows/bounded_window_agg_exec.rs b/datafusion/physical-plan/src/windows/bounded_window_agg_exec.rsindex 1e21d0757c..c78c870ff3 100644--- a/datafusion/physical-plan/src/windows/bounded_window_agg_exec.rs+++ b/datafusion/physical-plan/src/windows/bounded_window_agg_exec.rs@@ -1190,9 +1190,9 @@ mod tests {use crate::common::collect;use crate::expressions::PhysicalSortExpr;- use crate::memory::MemorySourceConfig;use crate::projection::ProjectionExec;use crate::streaming::{PartitionStream, StreamingTableExec};+ use crate::test::TestMemoryExec;use crate::windows::{create_udwf_window_expr, create_window_expr, BoundedWindowAggExec, InputOrderMode,};@@ -1551,7 +1551,7 @@ mod tests {vec![Arc::new(arrow::array::Int32Array::from(vec![1, 2, 3]))],)?;- let memory_exec = MemorySourceConfig::try_new_exec(+ let memory_exec = TestMemoryExec::try_new_exec(&[vec![batch.clone(), batch.clone(), batch.clone()]],Arc::clone(&schema),None,diff --git a/datafusion/physical-plan/src/work_table.rs b/datafusion/physical-plan/src/work_table.rsindex a6ced527cb..d3d29bfad7 100644--- a/datafusion/physical-plan/src/work_table.rs+++ b/datafusion/physical-plan/src/work_table.rs@@ -20,12 +20,12 @@use std::any::Any;use std::sync::{Arc, Mutex};-use super::{+use crate::execution_plan::{Boundedness, EmissionType};+use crate::memory::MemoryStream;+use crate::{metrics::{ExecutionPlanMetricsSet, MetricsSet},SendableRecordBatchStream, Statistics,};-use crate::execution_plan::{Boundedness, EmissionType};-use crate::memory::MemoryStream;use crate::{DisplayAs, DisplayFormatType, ExecutionPlan, PlanProperties};use arrow::datatypes::SchemaRef;diff --git a/datafusion/proto/src/physical_plan/mod.rs b/datafusion/proto/src/physical_plan/mod.rsindex a575a42d0b..2c59625558 100644--- a/datafusion/proto/src/physical_plan/mod.rs+++ b/datafusion/proto/src/physical_plan/mod.rs@@ -32,6 +32,7 @@ use datafusion::datasource::file_format::parquet::ParquetSink;#[cfg(feature = "parquet")]use datafusion::datasource::physical_plan::ParquetSource;use datafusion::datasource::physical_plan::{AvroSource, CsvSource, FileScanConfig};+use datafusion::datasource::source::DataSourceExec;use datafusion::execution::runtime_env::RuntimeEnv;use datafusion::execution::FunctionRegistry;use datafusion::physical_expr::aggregate::AggregateFunctionExpr;@@ -57,7 +58,6 @@ use datafusion::physical_plan::projection::ProjectionExec;use datafusion::physical_plan::repartition::RepartitionExec;use datafusion::physical_plan::sorts::sort::SortExec;use datafusion::physical_plan::sorts::sort_preserving_merge::SortPreservingMergeExec;-use datafusion::physical_plan::source::DataSourceExec;use datafusion::physical_plan::union::{InterleaveExec, UnionExec};use datafusion::physical_plan::unnest::{ListUnnest, UnnestExec};use datafusion::physical_plan::windows::{BoundedWindowAggExec, WindowAggExec};diff --git a/datafusion/substrait/src/physical_plan/producer.rs b/datafusion/substrait/src/physical_plan/producer.rsindex 3fc94a3344..e8c1573122 100644--- a/datafusion/substrait/src/physical_plan/producer.rs+++ b/datafusion/substrait/src/physical_plan/producer.rs@@ -23,8 +23,8 @@ use crate::variation_const::{};use datafusion::arrow::datatypes::DataType;+use datafusion::datasource::source::DataSourceExec;use datafusion::error::{DataFusionError, Result};-use datafusion::physical_plan::source::DataSourceExec;use datafusion::physical_plan::{displayable, ExecutionPlan};use datafusion::datasource::physical_plan::{FileScanConfig, ParquetSource};