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
6 changes: 3 additions & 3 deletions native/core/src/execution/jni_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1641,10 +1641,10 @@ fn execute_plan(
exec_context.stream = Some(stream);
}
exec_context.root_op = Some(root_op);
} else {
// Pull input batches
pull_input_batches(exec_context)?;
}
// JVM input is pulled only when the stream waits for it, in `next_batch` below. An operator
// above a scan, such as a `BatchSplitExec`, can still hold rows of the last batch it took,
// and a limit can finish on them without the JVM producing another batch.
Comment on lines +1645 to +1647

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The doc comment on pull_input_batches still says "we pull input batches here and insert them into scan operators before polling the stream". After this change it only runs from the pending branch of next_batch, after a poll, so that comment should change with this one. It also still fills every JVM-fed scan whose slot is empty, not only the one the plan waits on, so with two JVM inputs a pending poll on one side still reads the other side ahead. That part is unchanged from main, but "only when the stream waits for it" reads stronger than what happens.


// Wait no later than when metrics are due, so a plan that runs long before its first batch,
// such as a native shuffle write, still publishes on the interval. A publish while waiting
Expand Down
50 changes: 49 additions & 1 deletion native/core/src/execution/operators/dynamic_filter/join/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use datafusion::physical_plan::projection::ProjectionExec;
use datafusion::physical_plan::statistics::{StatisticsArgs, StatisticsContext};
use datafusion::physical_plan::{ChildrenPropertiesMode, Distribution, ReplaceChildrenOptions};
use datafusion::prelude::{SessionConfig, SessionContext};
use datafusion_comet_operators::CometFilterExec;
use datafusion_comet_operators::{BatchSplitExec, CometFilterExec};
use datafusion_comet_spark_expr::RandExpr;
use datafusion_datasource::file::FileSource;
use parquet::arrow::ArrowWriter;
Expand Down Expand Up @@ -1108,6 +1108,54 @@ async fn run_parquet_join(values: Vec<i32>, enabled: bool) -> (usize, usize, usi
)
}

#[tokio::test]
async fn reader_filter_attaches_through_a_batch_split() {
// The planner puts a CometBatchSplitExec under a filter whose predicate expands a string
// scalar. It only slices batches, so a reader filter below it discards the same rows.
let mut config = SessionConfig::new()
.with_target_partitions(1)
.with_parquet_page_index_pruning(false);
config.options_mut().execution.parquet.pushdown_filters = false;
let session = Arc::new(SessionContext::new_with_config(config));
let (_file, scan) = parquet_probe((0..400).collect(), &session, 100);
let split: Arc<dyn ExecutionPlan> = Arc::new(BatchSplitExec::new(
Arc::clone(&scan) as Arc<dyn ExecutionPlan>,
7,
));
let predicate: Arc<dyn PhysicalExpr> =
Arc::new(IsNotNullExpr::new(Arc::new(Column::new("key", 0))));
let probe = Arc::new(CometFilterExec::from_datafusion(
FilterExec::try_new(predicate, split).unwrap(),
));
let join = single_key_join_plans(two_batch_build(), probe, PartitionMode::Partitioned);
let wrapper =
DynamicFilterJoinExec::new(&join, session.copied_config().options().as_ref().clone())
.unwrap();
let runtime = wrapper.build_runtime_join().unwrap();
assert!(runtime.reader_filter_attached);
let consumer = runtime
.join
.right()
.downcast_ref::<DynamicFilterExec>()
.unwrap();
let filter = consumer.children()[0]
.downcast_ref::<CometFilterExec>()
.unwrap();
let split = filter.input().downcast_ref::<BatchSplitExec>().unwrap();
assert_eq!(split.batch_size(), 7);
let reader = split.children()[0]
.downcast_ref::<DataSourceExec>()
.unwrap();
let (_, source) = reader.downcast_to_file_source::<ParquetSource>().unwrap();
let reader_filter = source.filter().unwrap();
let reader_filter = find_dynamic_filter(&reader_filter).unwrap();
let join_filter = produced_join_filter(&runtime.join);
assert_eq!(
reader_filter.expression_id().unwrap(),
join_filter.expression_id().unwrap()
);
}

#[tokio::test]
async fn broadcast_filter_reaches_parquet_reader_after_complete_build() {
let mut config = SessionConfig::new()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use datafusion::physical_expr::PhysicalExpr;
use datafusion::physical_plan::ExecutionPlan;

use crate::parquet::file_error_context::ParquetErrorContext;
use datafusion_comet_operators::CometFilterExec;
use datafusion_comet_operators::{BatchSplitExec, CometFilterExec};

mod schema_adapter;

Expand Down Expand Up @@ -98,6 +98,15 @@ pub(super) fn try_attach_parquet_reader_filter(
.map(|reader| filter.with_execution_input(reader)),
);
}
// The batch split that bounds a scalar expansion only slices batches, so a
// reader filter below it discards the same rows.
if let Some(split) = input.downcast_ref::<BatchSplitExec>() {
return Ok(
try_attach_parquet_reader_filter(split.input(), predicate, config)?.map(|reader| {
Arc::new(BatchSplitExec::new(reader, split.batch_size())) as Arc<dyn ExecutionPlan>
}),
);
}
// Spark inserts IS NOT NULL residuals above equijoin inputs, including AND
// chains of inferred null checks. A reader predicate can cross those direct
// checks because both operations only discard rows. Broader deterministic
Expand Down
7 changes: 4 additions & 3 deletions native/core/src/execution/operators/projection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,11 @@ impl OperatorBuilder for ProjectionBuilder {
}
}
}
let projection = Arc::new(ProjectionExec::try_new(
exprs.iter().cloned(),
let input = planner.bound_scalar_expansion(
Arc::clone(&child.native_plan),
)?);
exprs.iter().map(|expr| &expr.expr),
);
let projection = Arc::new(ProjectionExec::try_new(exprs.iter().cloned(), input)?);

let spark_plan = if child.plan_id == spark_plan.plan_id {
let mut additional_native_plans = vec![Arc::clone(&child.native_plan)];
Expand Down
106 changes: 97 additions & 9 deletions native/core/src/execution/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ mod literal;
pub mod macros;
mod merge_rows;
pub mod operator_registry;
mod scalar_expansion;
mod shuffle;
mod window;
mod write;
Expand Down Expand Up @@ -1759,10 +1760,12 @@ impl PhysicalPlanner {
let predicate =
self.create_expr(filter.predicate.as_ref().unwrap(), child.schema())?;

let input =
self.bound_scalar_expansion(Arc::clone(&child.native_plan), [&predicate]);
Comment thread
zhangfengcdt marked this conversation as resolved.
Resolved
// FilterExec merges its output up to its own batch size, 8192 rows by default.
// Use the session batch size so it doesn't build batches bigger than that.
let filter: Arc<dyn ExecutionPlan> = Arc::new(
FilterExecBuilder::new(predicate, Arc::clone(&child.native_plan))
FilterExecBuilder::new(predicate, input)
.with_batch_size(self.session_ctx.copied_config().batch_size())
.build()?,
);
Expand Down Expand Up @@ -1853,19 +1856,28 @@ impl PhysicalPlanner {
let (scans, shuffle_scans, child) =
self.create_plan(&children[0], inputs, partition_count)?;

let exprs: Result<Vec<PhysicalSortExpr>, ExecutionError> = sort
let exprs: Vec<PhysicalSortExpr> = sort
.sort_orders
.iter()
.map(|expr| self.create_sort_expr(expr, child.schema()))
.collect();
.collect::<Result<_, ExecutionError>>()?;

let fetch = sort.fetch.map(|num| num as usize);

let sort_plan = SortExec::new(
LexOrdering::new(exprs?).unwrap(),
Arc::clone(&child.native_plan),
)
.with_fetch(fetch);
// A TopK evaluates its keys on each batch it receives. A full sort concatenates
// its buffered input before evaluating them, so a split would not bound it, and
// DataFusion's `ExternalSorter` reserves each slice of a shared buffer as if it
// owned the buffer (apache/datafusion#25804).
let input = if fetch.is_some() {
self.bound_scalar_expansion(
Arc::clone(&child.native_plan),
exprs.iter().map(|sort| &sort.expr),
)
} else {
Arc::clone(&child.native_plan)
};
let sort_plan =
SortExec::new(LexOrdering::new(exprs).unwrap(), input).with_fetch(fetch);
let mut sort_exec: Arc<dyn ExecutionPlan> = if sort.dynamic_filter_enabled {
match TopKReaderFilterExec::try_new(
&sort_plan,
Expand Down Expand Up @@ -3238,7 +3250,7 @@ mod tests {
use crate::execution::shuffle::CometPartitioning;
use crate::parquet::parquet_support::SparkParquetOptions;
use crate::parquet::schema_adapter::SparkPhysicalExprAdapterFactory;
use datafusion_comet_operators::{PartitionedRankLimitExec, WindowFnKind};
use datafusion_comet_operators::{BatchSplitExec, PartitionedRankLimitExec, WindowFnKind};
use datafusion_comet_proto::spark_expression::expr::ExprStruct;
use datafusion_comet_proto::spark_expression::ListLiteral;
use datafusion_comet_proto::{
Expand Down Expand Up @@ -4368,6 +4380,82 @@ mod tests {
);
}

#[test]
fn sort_bounds_the_scalar_its_keys_expand() {
// A TopK evaluates its keys on each batch it inserts, expanding a string scalar in a key
// to the batch's length, so the keys get the bound a projection's expressions get. A full
// sort gets no split: it evaluates its keys after concatenating its buffered input.
let string_type = spark_expression::DataType {
type_id: spark_expression::data_type::DataTypeId::String as i32,
type_info: None,
};
let scan = Operator {
op_struct: Some(OpStruct::Scan(spark_operator::Scan {
fields: vec![string_type.clone()],
source: String::new(),
})),
..Default::default()
};
let wide = Expr {
expr_struct: Some(Literal(spark_expression::Literal {
value: Some(literal::Value::StringVal("x".repeat(256 * 1024))),
datatype: Some(string_type.clone()),
is_null: false,
})),
..Default::default()
};
let column = Expr {
expr_struct: Some(Bound(spark_expression::BoundReference {
index: 0,
datatype: Some(string_type.clone()),
})),
..Default::default()
};
let key = Expr {
expr_struct: Some(ScalarFunc(spark_expression::ScalarFunc {
func: "concat".to_string(),
args: vec![wide, column],
return_type: Some(string_type),
..Default::default()
})),
..Default::default()
};
for fetch in [None, Some(1)] {
let sort = Operator {
children: vec![scan.clone()],
op_struct: Some(OpStruct::Sort(spark_operator::Sort {
sort_orders: vec![Expr {
expr_struct: Some(SortOrder(Box::new(spark_expression::SortOrder {
child: Some(Box::new(key.clone())),
direction: spark_expression::SortDirection::Ascending as i32,
null_ordering: spark_expression::NullOrdering::NullsFirst as i32,
}))),
..Default::default()
}],
fetch,
..Default::default()
})),
..Default::default()
};
let (_, _, planned) = PhysicalPlanner::default()
.create_plan(&sort, &mut vec![], 1)
.unwrap();
let sort = planned.native_plan.downcast_ref::<SortExec>().unwrap();
assert_eq!(fetch.map(|n| n as usize), sort.fetch());
match fetch {
None => assert!(!sort.input().is::<BatchSplitExec>()),
Some(_) => {
let split = sort
.input()
.downcast_ref::<BatchSplitExec>()
.expect("a split under the TopK");
// A 256 KiB scalar expanded to 4096 rows takes 1 GiB.
assert_eq!(4096, split.batch_size());
}
}
}
}

#[test]
fn declared_order_reaches_a_scan_of_one_file_group() {
// A scan of files reads the files of its one file group one after the other, so the order
Expand Down
12 changes: 10 additions & 2 deletions native/core/src/execution/planner/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,20 @@ impl OperatorBuilder for HashAggBuilder {
mode
};

let filter_exprs = filter_exprs?;
let input_exprs: Vec<Arc<dyn PhysicalExpr>> = group_by
.input_exprs()
.into_iter()
.chain(aggr_expr.iter().flat_map(|expr| expr.expressions()))
.chain(filter_exprs.iter().flatten().cloned())
.collect();
let input = planner.bound_scalar_expansion(Arc::clone(&child.native_plan), &input_exprs);
let aggregate = datafusion::physical_plan::aggregates::AggregateExec::try_new(
mode,
group_by,
aggr_expr,
filter_exprs?,
Arc::clone(&child.native_plan),
filter_exprs,
input,
Arc::clone(&schema),
)?;
let aggregate: Arc<dyn ExecutionPlan> = if agg.dynamic_filter_enabled {
Expand Down
Loading