Skip to content

Commit 41b1337

Browse files
committed
Merge remote-tracking branch 'apache/main' into alamb/fix_field_coercion
2 parents f1b3575 + 9d1bfc1 commit 41b1337

File tree

14 files changed

+682
-21
lines changed

14 files changed

+682
-21
lines changed

.github/workflows/rust.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ jobs:
6060
with:
6161
rust-version: stable
6262
- name: Prepare cargo build
63-
run: cargo check --profile ci --all-targets
63+
run: cargo check --profile ci --all-targets --features integration-tests
6464

6565
# cargo check common, functions and substrait with no default features
6666
linux-cargo-check-no-default-features:
@@ -92,8 +92,8 @@ jobs:
9292
- name: Check workspace in debug mode
9393
run: cargo check --profile ci --all-targets --workspace
9494

95-
- name: Check workspace with avro,json features
96-
run: cargo check --profile ci --workspace --benches --features avro,json
95+
- name: Check workspace with additional features
96+
run: cargo check --profile ci --workspace --benches --features avro,json,integration-tests
9797

9898
- name: Check Cargo.lock for datafusion-cli
9999
run: |
@@ -185,7 +185,7 @@ jobs:
185185
with:
186186
rust-version: stable
187187
- name: Run tests (excluding doctests)
188-
run: cargo test --profile ci --exclude datafusion-examples --exclude datafusion-benchmarks --workspace --lib --tests --bins --features avro,json,backtrace
188+
run: cargo test --profile ci --exclude datafusion-examples --exclude ffi_example_table_provider --exclude datafusion-benchmarks --workspace --lib --tests --bins --features avro,json,backtrace,integration-tests
189189
- name: Verify Working Directory Clean
190190
run: git diff --exit-code
191191

@@ -417,7 +417,7 @@ jobs:
417417
- name: Run tests (excluding doctests)
418418
shell: bash
419419
run: |
420-
cargo test --profile ci --lib --tests --bins --features avro,json,backtrace
420+
cargo test --profile ci --lib --tests --bins --features avro,json,backtrace,integration-tests
421421
cd datafusion-cli
422422
cargo test --profile ci --lib --tests --bins --all-features
423423

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
[![Crates.io][crates-badge]][crates-url]
2323
[![Apache licensed][license-badge]][license-url]
2424
[![Build Status][actions-badge]][actions-url]
25+
![Commit Activity][commit-activity-badge]
26+
[![Open Issues][open-issues-badge]][open-issues-url]
2527
[![Discord chat][discord-badge]][discord-url]
2628

2729
[crates-badge]: https://img.shields.io/crates/v/datafusion.svg
@@ -32,6 +34,9 @@
3234
[actions-url]: https://github.com/apache/datafusion/actions?query=branch%3Amain
3335
[discord-badge]: https://img.shields.io/discord/885562378132000778.svg?logo=discord&style=flat-square
3436
[discord-url]: https://discord.com/invite/Qw5gKqHxUM
37+
[commit-activity-badge]: https://img.shields.io/github/commit-activity/m/apache/datafusion
38+
[open-issues-badge]: https://img.shields.io/github/issues-raw/apache/datafusion
39+
[open-issues-url]: https://github.com/apache/datafusion/issues
3540

3641
[Website](https://datafusion.apache.org/) |
3742
[API Docs](https://docs.rs/datafusion/latest/datafusion/) |

ci/scripts/rust_clippy.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818
# under the License.
1919

2020
set -ex
21-
cargo clippy --all-targets --workspace --features avro,pyarrow -- -D warnings
21+
cargo clippy --all-targets --workspace --features avro,pyarrow,integration-tests -- -D warnings
2222
cd datafusion-cli
2323
cargo clippy --all-targets --all-features -- -D warnings

datafusion-examples/examples/ffi/ffi_example_table_provider/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ extern "C" fn construct_simple_table_provider() -> FFI_TableProvider {
5353

5454
let table_provider = MemTable::try_new(schema, vec![batches]).unwrap();
5555

56-
FFI_TableProvider::new(Arc::new(table_provider), true)
56+
FFI_TableProvider::new(Arc::new(table_provider), true, None)
5757
}
5858

5959
#[export_root_module]

datafusion/common/src/utils/mod.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,21 @@ pub fn set_difference<T: Borrow<usize>, S: Borrow<usize>>(
769769
.collect()
770770
}
771771

772+
/// Checks whether the given index sequence is monotonically non-decreasing.
773+
#[deprecated(since = "45.0.0", note = "Use std::Iterator::is_sorted instead")]
774+
pub fn is_sorted<T: Borrow<usize>>(sequence: impl IntoIterator<Item = T>) -> bool {
775+
// TODO: Remove this function when `is_sorted` graduates from Rust nightly.
776+
let mut previous = 0;
777+
for item in sequence.into_iter() {
778+
let current = *item.borrow();
779+
if current < previous {
780+
return false;
781+
}
782+
previous = current;
783+
}
784+
true
785+
}
786+
772787
/// Find indices of each element in `targets` inside `items`. If one of the
773788
/// elements is absent in `items`, returns an error.
774789
pub fn find_indices<T: PartialEq, S: Borrow<T>>(
@@ -1157,6 +1172,19 @@ mod tests {
11571172
assert_eq!(set_difference([3, 4, 0], [4, 1, 2]), vec![3, 0]);
11581173
}
11591174

1175+
#[test]
1176+
#[expect(deprecated)]
1177+
fn test_is_sorted() {
1178+
assert!(is_sorted::<usize>([]));
1179+
assert!(is_sorted([0]));
1180+
assert!(is_sorted([0, 3, 4]));
1181+
assert!(is_sorted([0, 1, 2]));
1182+
assert!(is_sorted([0, 1, 4]));
1183+
assert!(is_sorted([0usize; 0]));
1184+
assert!(is_sorted([1, 2]));
1185+
assert!(!is_sorted([3, 2]));
1186+
}
1187+
11601188
#[test]
11611189
fn test_find_indices() -> Result<()> {
11621190
assert_eq!(find_indices(&[0, 3, 4], [0, 3, 4])?, vec![0, 1, 2]);

datafusion/ffi/Cargo.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,25 @@ workspace = true
3333
[lib]
3434
name = "datafusion_ffi"
3535
path = "src/lib.rs"
36+
crate-type = ["cdylib", "rlib"]
3637

3738
[dependencies]
3839
abi_stable = "0.11.3"
3940
arrow = { workspace = true, features = ["ffi"] }
41+
arrow-array = { workspace = true }
42+
arrow-schema = { workspace = true }
4043
async-ffi = { version = "0.5.0", features = ["abi_stable"] }
4144
async-trait = { workspace = true }
4245
datafusion = { workspace = true, default-features = false }
4346
datafusion-proto = { workspace = true }
4447
futures = { workspace = true }
4548
log = { workspace = true }
4649
prost = { workspace = true }
50+
semver = "1.0.24"
51+
tokio = { workspace = true }
4752

4853
[dev-dependencies]
4954
doc-comment = { workspace = true }
50-
tokio = { workspace = true }
55+
56+
[features]
57+
integration-tests = []

datafusion/ffi/src/execution_plan.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use datafusion::{
2727
execution::{SendableRecordBatchStream, TaskContext},
2828
physical_plan::{DisplayAs, ExecutionPlan, PlanProperties},
2929
};
30+
use tokio::runtime::Runtime;
3031

3132
use crate::{
3233
plan_properties::FFI_PlanProperties, record_batch_stream::FFI_RecordBatchStream,
@@ -71,6 +72,7 @@ unsafe impl Sync for FFI_ExecutionPlan {}
7172
pub struct ExecutionPlanPrivateData {
7273
pub plan: Arc<dyn ExecutionPlan>,
7374
pub context: Arc<TaskContext>,
75+
pub runtime: Option<Arc<Runtime>>,
7476
}
7577

7678
unsafe extern "C" fn properties_fn_wrapper(
@@ -88,11 +90,14 @@ unsafe extern "C" fn children_fn_wrapper(
8890
let private_data = plan.private_data as *const ExecutionPlanPrivateData;
8991
let plan = &(*private_data).plan;
9092
let ctx = &(*private_data).context;
93+
let runtime = &(*private_data).runtime;
9194

9295
let children: Vec<_> = plan
9396
.children()
9497
.into_iter()
95-
.map(|child| FFI_ExecutionPlan::new(Arc::clone(child), Arc::clone(ctx)))
98+
.map(|child| {
99+
FFI_ExecutionPlan::new(Arc::clone(child), Arc::clone(ctx), runtime.clone())
100+
})
96101
.collect();
97102

98103
children.into()
@@ -105,9 +110,10 @@ unsafe extern "C" fn execute_fn_wrapper(
105110
let private_data = plan.private_data as *const ExecutionPlanPrivateData;
106111
let plan = &(*private_data).plan;
107112
let ctx = &(*private_data).context;
113+
let runtime = (*private_data).runtime.as_ref().map(Arc::clone);
108114

109115
match plan.execute(partition, Arc::clone(ctx)) {
110-
Ok(rbs) => RResult::ROk(rbs.into()),
116+
Ok(rbs) => RResult::ROk(FFI_RecordBatchStream::new(rbs, runtime)),
111117
Err(e) => RResult::RErr(
112118
format!("Error occurred during FFI_ExecutionPlan execute: {}", e).into(),
113119
),
@@ -129,7 +135,11 @@ unsafe extern "C" fn clone_fn_wrapper(plan: &FFI_ExecutionPlan) -> FFI_Execution
129135
let private_data = plan.private_data as *const ExecutionPlanPrivateData;
130136
let plan_data = &(*private_data);
131137

132-
FFI_ExecutionPlan::new(Arc::clone(&plan_data.plan), Arc::clone(&plan_data.context))
138+
FFI_ExecutionPlan::new(
139+
Arc::clone(&plan_data.plan),
140+
Arc::clone(&plan_data.context),
141+
plan_data.runtime.clone(),
142+
)
133143
}
134144

135145
impl Clone for FFI_ExecutionPlan {
@@ -140,8 +150,16 @@ impl Clone for FFI_ExecutionPlan {
140150

141151
impl FFI_ExecutionPlan {
142152
/// This function is called on the provider's side.
143-
pub fn new(plan: Arc<dyn ExecutionPlan>, context: Arc<TaskContext>) -> Self {
144-
let private_data = Box::new(ExecutionPlanPrivateData { plan, context });
153+
pub fn new(
154+
plan: Arc<dyn ExecutionPlan>,
155+
context: Arc<TaskContext>,
156+
runtime: Option<Arc<Runtime>>,
157+
) -> Self {
158+
let private_data = Box::new(ExecutionPlanPrivateData {
159+
plan,
160+
context,
161+
runtime,
162+
});
145163

146164
Self {
147165
properties: properties_fn_wrapper,
@@ -357,7 +375,7 @@ mod tests {
357375
let original_plan = Arc::new(EmptyExec::new(schema));
358376
let original_name = original_plan.name().to_string();
359377

360-
let local_plan = FFI_ExecutionPlan::new(original_plan, ctx.task_ctx());
378+
let local_plan = FFI_ExecutionPlan::new(original_plan, ctx.task_ctx(), None);
361379

362380
let foreign_plan: ForeignExecutionPlan = (&local_plan).try_into()?;
363381

datafusion/ffi/src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,18 @@ pub mod session_config;
2626
pub mod table_provider;
2727
pub mod table_source;
2828

29+
#[cfg(feature = "integration-tests")]
30+
pub mod tests;
31+
32+
/// Returns the major version of the FFI implementation. If the API evolves,
33+
/// we use the major version to identify compatibility over the unsafe
34+
/// boundary. This call is intended to be used by implementers to validate
35+
/// they have compatible libraries.
36+
pub extern "C" fn version() -> u64 {
37+
let version_str = env!("CARGO_PKG_VERSION");
38+
let version = semver::Version::parse(version_str).expect("Invalid version string");
39+
version.major
40+
}
41+
2942
#[cfg(doctest)]
3043
doc_comment::doctest!("../README.md", readme_example_test);

datafusion/ffi/src/record_batch_stream.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
use std::{ffi::c_void, task::Poll};
18+
use std::{ffi::c_void, sync::Arc, task::Poll};
1919

2020
use abi_stable::{
2121
std_types::{ROption, RResult, RString},
@@ -33,6 +33,7 @@ use datafusion::{
3333
execution::{RecordBatchStream, SendableRecordBatchStream},
3434
};
3535
use futures::{Stream, TryStreamExt};
36+
use tokio::runtime::Runtime;
3637

3738
use crate::arrow_wrappers::{WrappedArray, WrappedSchema};
3839

@@ -58,20 +59,36 @@ pub struct FFI_RecordBatchStream {
5859
pub private_data: *mut c_void,
5960
}
6061

62+
pub struct RecordBatchStreamPrivateData {
63+
pub rbs: SendableRecordBatchStream,
64+
pub runtime: Option<Arc<Runtime>>,
65+
}
66+
6167
impl From<SendableRecordBatchStream> for FFI_RecordBatchStream {
6268
fn from(stream: SendableRecordBatchStream) -> Self {
69+
Self::new(stream, None)
70+
}
71+
}
72+
73+
impl FFI_RecordBatchStream {
74+
pub fn new(stream: SendableRecordBatchStream, runtime: Option<Arc<Runtime>>) -> Self {
75+
let private_data = Box::into_raw(Box::new(RecordBatchStreamPrivateData {
76+
rbs: stream,
77+
runtime,
78+
})) as *mut c_void;
6379
FFI_RecordBatchStream {
6480
poll_next: poll_next_fn_wrapper,
6581
schema: schema_fn_wrapper,
66-
private_data: Box::into_raw(Box::new(stream)) as *mut c_void,
82+
private_data,
6783
}
6884
}
6985
}
7086

7187
unsafe impl Send for FFI_RecordBatchStream {}
7288

7389
unsafe extern "C" fn schema_fn_wrapper(stream: &FFI_RecordBatchStream) -> WrappedSchema {
74-
let stream = stream.private_data as *const SendableRecordBatchStream;
90+
let private_data = stream.private_data as *const RecordBatchStreamPrivateData;
91+
let stream = &(*private_data).rbs;
7592

7693
(*stream).schema().into()
7794
}
@@ -106,7 +123,10 @@ unsafe extern "C" fn poll_next_fn_wrapper(
106123
stream: &FFI_RecordBatchStream,
107124
cx: &mut FfiContext,
108125
) -> FfiPoll<ROption<RResult<WrappedArray, RString>>> {
109-
let stream = stream.private_data as *mut SendableRecordBatchStream;
126+
let private_data = stream.private_data as *mut RecordBatchStreamPrivateData;
127+
let stream = &mut (*private_data).rbs;
128+
129+
let _guard = (*private_data).runtime.as_ref().map(|rt| rt.enter());
110130

111131
let poll_result = cx.with_context(|std_cx| {
112132
(*stream)

0 commit comments

Comments
 (0)