Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use datafusion::arrow::array::{UInt8Builder, UInt64Builder};
use datafusion::arrow::datatypes::{DataType, Field, Schema, SchemaRef};
use datafusion::arrow::record_batch::RecordBatch;
use datafusion::common::assert_batches_eq;
use datafusion::common::tree_node::TreeNodeRecursion;
use datafusion::datasource::{TableProvider, TableType, provider_as_source};
use datafusion::error::Result;
use datafusion::execution::context::TaskContext;
Expand Down Expand Up @@ -314,4 +315,13 @@ impl ExecutionPlan for CustomExec {
None,
)?))
}

fn apply_expressions(
&self,
_f: &mut dyn FnMut(
&Arc<dyn datafusion::physical_plan::PhysicalExpr>,
) -> Result<TreeNodeRecursion>,
) -> Result<TreeNodeRecursion> {
Ok(TreeNodeRecursion::Continue)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use arrow::array::record_batch;
use arrow::record_batch::RecordBatch;
use arrow_schema::SchemaRef;
use datafusion::common::tree_node::TreeNodeRecursion;
use datafusion::common::{exec_datafusion_err, internal_err};
use datafusion::datasource::{DefaultTableSource, memory::MemTable};
use datafusion::error::Result;
Expand Down Expand Up @@ -291,4 +292,13 @@ impl ExecutionPlan for BufferingExecutionPlan {
}),
)))
}

fn apply_expressions(
&self,
_f: &mut dyn FnMut(
&Arc<dyn datafusion::physical_plan::PhysicalExpr>,
) -> Result<TreeNodeRecursion>,
) -> Result<TreeNodeRecursion> {
Ok(TreeNodeRecursion::Continue)
}
}
19 changes: 19 additions & 0 deletions datafusion-examples/examples/proto/composed_extension_codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use std::sync::Arc;

use datafusion::common::Result;
use datafusion::common::internal_err;
use datafusion::common::tree_node::TreeNodeRecursion;
use datafusion::execution::TaskContext;
use datafusion::physical_plan::{DisplayAs, ExecutionPlan};
use datafusion::prelude::SessionContext;
Expand Down Expand Up @@ -124,6 +125,15 @@ impl ExecutionPlan for ParentExec {
) -> Result<datafusion::physical_plan::SendableRecordBatchStream> {
unreachable!()
}

fn apply_expressions(
&self,
_f: &mut dyn FnMut(
&Arc<dyn datafusion::physical_plan::PhysicalExpr>,
) -> Result<TreeNodeRecursion>,
) -> Result<TreeNodeRecursion> {
Ok(TreeNodeRecursion::Continue)
}
}

/// A PhysicalExtensionCodec that can serialize and deserialize ParentExec
Expand Down Expand Up @@ -202,6 +212,15 @@ impl ExecutionPlan for ChildExec {
) -> Result<datafusion::physical_plan::SendableRecordBatchStream> {
unreachable!()
}

fn apply_expressions(
&self,
_f: &mut dyn FnMut(
&Arc<dyn datafusion::physical_plan::PhysicalExpr>,
) -> Result<TreeNodeRecursion>,
) -> Result<TreeNodeRecursion> {
Ok(TreeNodeRecursion::Continue)
}
}

/// A PhysicalExtensionCodec that can serialize and deserialize ChildExec
Expand Down
18 changes: 17 additions & 1 deletion datafusion-examples/examples/relation_planner/table_sample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ use datafusion::{
};
use datafusion_common::{
DFSchemaRef, DataFusionError, Result, Statistics, internal_err, not_impl_err,
plan_datafusion_err, plan_err,
plan_datafusion_err, plan_err, tree_node::TreeNodeRecursion,
};
use datafusion_expr::physical_planning_context::PhysicalPlanningContext;
use datafusion_expr::{
Expand Down Expand Up @@ -749,6 +749,22 @@ impl ExecutionPlan for SampleExec {

Ok(Arc::new(stats))
}

fn apply_expressions(
&self,
f: &mut dyn FnMut(
&Arc<dyn datafusion::physical_plan::PhysicalExpr>,
) -> Result<TreeNodeRecursion>,
) -> Result<TreeNodeRecursion> {
datafusion::physical_plan::apply_expression_roots(
self.cache
.output_ordering()
.into_iter()
.flatten()
.map(|sort_expr| &sort_expr.expr),
f,
)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question for reviewers - this pattern repeats a lot. Is it worth adding a helper?

apply_expression_roots(
    properties
        .output_ordering()
        .into_iter()
        .flatten()
        .map(|sort_expr| &sort_expr.expr),
    f
)

The projection pattern also repeats a bit

      crate::apply_expression_roots(
          self.projector
              .projection()
              .as_ref()
              .iter()
              .map(|proj_expr| &proj_expr.expr),
          f,
      )

}
}

/// Bernoulli sampler: includes each row with probability `(upper - lower)`.
Expand Down
14 changes: 11 additions & 3 deletions datafusion/catalog/src/memory/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use arrow::compute::{and, filter_record_batch};
use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
use arrow::record_batch::RecordBatch;
use datafusion_common::error::Result;
use datafusion_common::tree_node::TreeNodeRecursion;
use datafusion_common::{Constraints, DFSchema, SchemaExt, not_impl_err, plan_err};
use datafusion_datasource::memory::{MemSink, MemorySourceConfig};
use datafusion_datasource::sink::DataSinkExec;
Expand All @@ -39,13 +40,13 @@ use datafusion_expr::dml::InsertOp;
use datafusion_expr::physical_planning_context::PhysicalPlanningContext;
use datafusion_expr::{Expr, SortExpr, TableType};
use datafusion_physical_expr::{
LexOrdering, PhysicalExpr, create_physical_expr, create_physical_sort_exprs,
LexOrdering, create_physical_expr, create_physical_sort_exprs,
};
use datafusion_physical_plan::repartition::RepartitionExec;
use datafusion_physical_plan::stream::RecordBatchStreamAdapter;
use datafusion_physical_plan::{
DisplayAs, DisplayFormatType, ExecutionPlan, Partitioning, PlanProperties,
collect_partitioned,
DisplayAs, DisplayFormatType, ExecutionPlan, Partitioning, PhysicalExpr,
PlanProperties, collect_partitioned,
};
use datafusion_session::Session;

Expand Down Expand Up @@ -597,4 +598,11 @@ impl ExecutionPlan for DmlResultExec {
stream,
)))
}

fn apply_expressions(
&self,
_f: &mut dyn FnMut(&Arc<dyn PhysicalExpr>) -> Result<TreeNodeRecursion>,
) -> Result<TreeNodeRecursion> {
Ok(TreeNodeRecursion::Continue)
}
}
25 changes: 25 additions & 0 deletions datafusion/core/src/physical_planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4711,6 +4711,13 @@ mod tests {
) -> Result<SendableRecordBatchStream> {
unimplemented!("NoOpExecutionPlan::execute");
}

fn apply_expressions(
&self,
_f: &mut dyn FnMut(&Arc<dyn PhysicalExpr>) -> Result<TreeNodeRecursion>,
) -> Result<TreeNodeRecursion> {
Ok(TreeNodeRecursion::Continue)
}
}

struct ExpressionExtensionPlanner;
Expand Down Expand Up @@ -4878,6 +4885,12 @@ digraph {
) -> Result<SendableRecordBatchStream> {
unimplemented!()
}
fn apply_expressions(
&self,
_f: &mut dyn FnMut(&Arc<dyn PhysicalExpr>) -> Result<TreeNodeRecursion>,
) -> Result<TreeNodeRecursion> {
Ok(TreeNodeRecursion::Continue)
}
}
impl DisplayAs for OkExtensionNode {
fn fmt_as(&self, _t: DisplayFormatType, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down Expand Up @@ -4924,6 +4937,12 @@ digraph {
) -> Result<SendableRecordBatchStream> {
unimplemented!()
}
fn apply_expressions(
&self,
_f: &mut dyn FnMut(&Arc<dyn PhysicalExpr>) -> Result<TreeNodeRecursion>,
) -> Result<TreeNodeRecursion> {
Ok(TreeNodeRecursion::Continue)
}
}
impl DisplayAs for InvariantFailsExtensionNode {
fn fmt_as(&self, _t: DisplayFormatType, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down Expand Up @@ -5048,6 +5067,12 @@ digraph {
) -> Result<SendableRecordBatchStream> {
unimplemented!()
}
fn apply_expressions(
&self,
_f: &mut dyn FnMut(&Arc<dyn PhysicalExpr>) -> Result<TreeNodeRecursion>,
) -> Result<TreeNodeRecursion> {
Ok(TreeNodeRecursion::Continue)
}
}
impl DisplayAs for ExecutableInvariantFails {
fn fmt_as(&self, _t: DisplayFormatType, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down
17 changes: 17 additions & 0 deletions datafusion/core/tests/custom_sources_cases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use datafusion_catalog::Session;
use datafusion_common::cast::as_primitive_array;
use datafusion_common::project_schema;
use datafusion_common::stats::Precision;
use datafusion_common::tree_node::TreeNodeRecursion;
use datafusion_physical_expr::EquivalenceProperties;
use datafusion_physical_plan::PlanProperties;
use datafusion_physical_plan::StatisticsArgs;
Expand Down Expand Up @@ -209,6 +210,22 @@ impl ExecutionPlan for CustomExecutionPlan {
.collect(),
}))
}

fn apply_expressions(
&self,
f: &mut dyn FnMut(
&Arc<dyn datafusion::physical_plan::PhysicalExpr>,
) -> Result<TreeNodeRecursion>,
) -> Result<TreeNodeRecursion> {
datafusion::physical_plan::apply_expression_roots(
self.cache
.output_ordering()
.into_iter()
.flatten()
.map(|sort_expr| &sort_expr.expr),
f,
)
}
}

#[async_trait]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use datafusion::prelude::*;
use datafusion::scalar::ScalarValue;
use datafusion_catalog::Session;
use datafusion_common::cast::as_primitive_array;
use datafusion_common::tree_node::TreeNodeRecursion;
use datafusion_common::{DataFusionError, internal_err, not_impl_err};
use datafusion_expr::expr::{BinaryExpr, Cast};
use datafusion_functions_aggregate::expr_fn::count;
Expand Down Expand Up @@ -148,6 +149,22 @@ impl ExecutionPlan for CustomPlan {
})),
)))
}

fn apply_expressions(
&self,
f: &mut dyn FnMut(
&Arc<dyn datafusion::physical_plan::PhysicalExpr>,
) -> Result<TreeNodeRecursion>,
) -> Result<TreeNodeRecursion> {
datafusion::physical_plan::apply_expression_roots(
self.cache
.output_ordering()
.into_iter()
.flatten()
.map(|sort_expr| &sort_expr.expr),
f,
)
}
}

#[derive(Clone, Debug)]
Expand Down
17 changes: 17 additions & 0 deletions datafusion/core/tests/custom_sources_cases/statistics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use datafusion::{
scalar::ScalarValue,
};
use datafusion_catalog::Session;
use datafusion_common::tree_node::TreeNodeRecursion;
use datafusion_common::{project_schema, stats::Precision};
use datafusion_physical_expr::EquivalenceProperties;
use datafusion_physical_plan::execution_plan::{Boundedness, EmissionType};
Expand Down Expand Up @@ -185,6 +186,22 @@ impl ExecutionPlan for StatisticsValidation {
Ok(Arc::new(self.stats.clone()))
}
}

fn apply_expressions(
&self,
f: &mut dyn FnMut(
&Arc<dyn datafusion::physical_plan::PhysicalExpr>,
) -> Result<TreeNodeRecursion>,
) -> Result<TreeNodeRecursion> {
datafusion::physical_plan::apply_expression_roots(
self.cache
.output_ordering()
.into_iter()
.flatten()
.map(|sort_expr| &sort_expr.expr),
f,
)
}
}

fn init_ctx(stats: Statistics, schema: Schema) -> Result<SessionContext> {
Expand Down
17 changes: 17 additions & 0 deletions datafusion/core/tests/fuzz_cases/once_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

use arrow_schema::SchemaRef;
use datafusion_common::internal_datafusion_err;
use datafusion_common::tree_node::TreeNodeRecursion;
use datafusion_execution::{SendableRecordBatchStream, TaskContext};
use datafusion_physical_expr::{EquivalenceProperties, Partitioning};
use datafusion_physical_plan::execution_plan::{Boundedness, EmissionType};
Expand Down Expand Up @@ -105,4 +106,20 @@ impl ExecutionPlan for OnceExec {

stream.ok_or_else(|| internal_datafusion_err!("Stream already consumed"))
}

fn apply_expressions(
&self,
f: &mut dyn FnMut(
&Arc<dyn datafusion_physical_plan::PhysicalExpr>,
) -> datafusion_common::Result<TreeNodeRecursion>,
) -> datafusion_common::Result<TreeNodeRecursion> {
datafusion_physical_plan::apply_expression_roots(
self.cache
.output_ordering()
.into_iter()
.flatten()
.map(|sort_expr| &sort_expr.expr),
f,
)
}
}
25 changes: 24 additions & 1 deletion datafusion/core/tests/physical_optimizer/enforce_distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ use datafusion::prelude::{SessionConfig, SessionContext};
use datafusion_common::ScalarValue;
use datafusion_common::config::CsvOptions;
use datafusion_common::error::Result;
use datafusion_common::tree_node::{Transformed, TransformedResult, TreeNode};
use datafusion_common::tree_node::{
Transformed, TransformedResult, TreeNode, TreeNodeRecursion,
};
use datafusion_datasource::file_groups::FileGroup;
use datafusion_datasource::file_scan_config::FileScanConfigBuilder;
use datafusion_expr::{JoinType, Operator};
Expand Down Expand Up @@ -203,6 +205,20 @@ impl ExecutionPlan for SortRequiredExec {
)))
}

fn apply_expressions(
&self,
f: &mut dyn FnMut(&Arc<dyn PhysicalExpr>) -> Result<TreeNodeRecursion>,
) -> Result<TreeNodeRecursion> {
datafusion_physical_plan::apply_expression_roots(
self.cache
.output_ordering()
.into_iter()
.flatten()
.map(|sort_expr| &sort_expr.expr),
f,
)
}

fn execute(
&self,
_partition: usize,
Expand Down Expand Up @@ -290,6 +306,13 @@ impl ExecutionPlan for SinglePartitionMaintainsOrderExec {
Ok(Arc::new(Self::new(child)))
}

fn apply_expressions(
&self,
_f: &mut dyn FnMut(&Arc<dyn PhysicalExpr>) -> Result<TreeNodeRecursion>,
) -> Result<TreeNodeRecursion> {
Ok(TreeNodeRecursion::Continue)
}

fn execute(
&self,
_partition: usize,
Expand Down
Loading
Loading