Skip to content
Merged
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions datafusion/ffi/integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ datafusion-ffi = { workspace = true }
[dev-dependencies]
arrow = { workspace = true }
arrow-schema = { workspace = true }
async-trait = { workspace = true }
datafusion = { workspace = true, default-features = false, features = ["sql"] }
datafusion-catalog = { workspace = true }
datafusion-common = { workspace = true }
Expand Down
101 changes: 100 additions & 1 deletion datafusion/ffi/integration-tests/tests/cases/execution_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ mod tests {
ExecutionPlanPrivateData, FFI_ExecutionPlan, ForeignExecutionPlan,
tests::EmptyExec,
};
use datafusion_ffi::tests::utils::{get_byte_metrics_exec, get_module};
use datafusion_ffi::physical_expr::ForeignPhysicalExpr;
use datafusion_ffi::tests::ForeignLibraryModule;
use datafusion_ffi::tests::utils::{
get_byte_metrics_exec, get_module, get_module_copy,
};
use datafusion_physical_expr_common::metrics::{MetricCategory, MetricValue};
use datafusion_physical_expr_common::physical_expr::PhysicalExpr;
use datafusion_physical_plan::execution_plan::InvariantLevel;
use datafusion_physical_plan::{
ChildrenPropertiesMode, ExecutionPlan, Partitioning, ReplaceChildrenOptions,
Expand Down Expand Up @@ -163,6 +168,41 @@ mod tests {
Ok(())
}

/// `ForeignPhysicalExpr` equality may only look inside an expression
/// created by the same library image. Expressions from two separately
/// loaded copies of the cdylib must compare unequal even when they are
/// structurally identical, while two from the same copy compare equal.
#[test]
fn test_ffi_physical_expr_eq_across_library_images() -> Result<(), DataFusionError> {
fn range_sort_expr(
module: &ForeignLibraryModule,
) -> Result<Arc<dyn PhysicalExpr>, DataFusionError> {
let plan = (module.create_exec_with_statistics)();
let plan: Arc<dyn ExecutionPlan> = (&plan).try_into()?;
let Partitioning::Range(range) = plan.properties().output_partitioning()
else {
panic!("expected range partitioning");
};
Ok(Arc::clone(&range.ordering().first().expr))
}

let library_b = get_module_copy("physical_expr_eq_library_b")?;
let library_c = get_module_copy("physical_expr_eq_library_c")?;

let b_1 = range_sort_expr(&library_b)?;
let b_2 = range_sort_expr(&library_b)?;
let c = range_sort_expr(&library_c)?;
assert!(b_1.downcast_ref::<ForeignPhysicalExpr>().is_some());
assert!(c.downcast_ref::<ForeignPhysicalExpr>().is_some());
assert_eq!(b_1.to_string(), c.to_string());

assert_eq!(&b_1, &b_2);
assert_ne!(&b_1, &c);
assert_ne!(&c, &b_1);

Ok(())
}

#[test]
fn test_ffi_execution_plan_expressions_cross_library() -> Result<(), DataFusionError>
{
Expand Down Expand Up @@ -283,3 +323,62 @@ mod tests {
Ok(())
}
}

mod foreign_requirements {
use std::sync::Arc;

use async_trait::async_trait;
use datafusion::catalog::{Session, TableProvider};
use datafusion::logical_expr::{Expr, TableType};
use datafusion::prelude::SessionContext;
use datafusion_common::Result;
use datafusion_ffi::tests::utils::get_requirements_exec;
use datafusion_physical_plan::{ExecutionPlan, displayable};

/// A host-side table whose scan returns a plan built by another library.
#[derive(Debug)]
struct ForeignScanTable(Arc<dyn ExecutionPlan>);

#[async_trait]
impl TableProvider for ForeignScanTable {
fn schema(&self) -> arrow_schema::SchemaRef {
self.0.schema()
}
fn table_type(&self) -> TableType {
TableType::Base
}
async fn scan(
&self,
_: &dyn Session,
_: Option<&[usize]>,
_: &[Expr],
_: Option<usize>,
) -> Result<Arc<dyn ExecutionPlan>> {
Ok(Arc::clone(&self.0))
}
}

/// A correct foreign subtree (its top node's requirement is met by its own
/// child) must plan on the host without errors or extra repartitions/sorts.
#[tokio::test]
async fn foreign_requirements_met_by_foreign_child() -> Result<()> {
for hard_ordering in [false, true] {
let ffi = get_requirements_exec(hard_ordering)?;
let foreign: Arc<dyn ExecutionPlan> = (&ffi).try_into()?;
let ctx = SessionContext::new();
ctx.register_table("t", Arc::new(ForeignScanTable(foreign)))?;
let plan = ctx
.sql("SELECT * FROM t")
.await?
.create_physical_plan()
.await?;
let display = displayable(plan.as_ref()).indent(false).to_string();
assert!(
!display.contains("\n RepartitionExec: partitioning=Hash")
&& !display.contains("\n SortExec"),
"redundant repartition or sort added:\n{display}"
);
}
Ok(())
}
}
Loading
Loading