gengliangwang commented on code in PR #59520: URL: https://github.com/apache/spark/pull/59520#discussion_r4558567371
########## sql/native/rust/src/abi.rs: ########## @@ -0,0 +1,1010 @@ +// 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. + +//! Bindings and adapters for version 1 of `spark_datasource.h`. + +use std::cell::RefCell; +use std::ffi::{c_char, c_int, c_void, CStr, CString}; +use std::mem::{offset_of, size_of, MaybeUninit}; +use std::sync::Arc; + +pub use arrow_array::ffi::FFI_ArrowArray as ArrowArray; +pub use arrow_array::ffi_stream::FFI_ArrowArrayStream as ArrowArrayStream; +pub use arrow_schema::ffi::FFI_ArrowSchema as ArrowSchema; +use arrow_schema::{Schema, SchemaRef}; + +use crate::bridge::{c_message, provider_call}; +use crate::host::{self, LogEnabledFunction, LogFunction}; +use crate::{CreateInfo, HostInfo, LatestOffsetInfo, Options, ReadLimit, Result, WriteMode}; + +pub const ABI_VERSION: i32 = 1; +pub const BATCH_READ: u64 = 1; +pub const MICRO_BATCH_READ: u64 = 2; +pub const BATCH_WRITE: u64 = 4; +pub const STREAMING_WRITE: u64 = 8; +pub const BATCH_TRUNCATE: u64 = 16; +pub const STREAMING_TRUNCATE: u64 = 32; +pub const ADMISSION_CONTROL: u64 = 64; +pub const READ_BATCH: i32 = 0; +pub const READ_MICRO_BATCH: i32 = 1; +pub const READ_ALL_AVAILABLE: i32 = 0; +pub const READ_MAX_ROWS: i32 = 1; +pub const WRITE_APPEND: i32 = 0; +pub const WRITE_TRUNCATE: i32 = 1; +pub const LOG_ERROR: i32 = 1; + +pub type SparkDsSink = Option<unsafe extern "C" fn(*mut c_void, *const u8, i64)>; +pub type Interrupted = unsafe extern "C" fn(*const SparkDsTaskContext) -> bool; + +// What Spark passes to the library. + +#[repr(C)] +pub struct SparkDsHost { + pub size: i64, + pub abi_version: i32, + pub spark_version: *const c_char, + pub log: Option<LogFunction>, + pub is_log_enabled: Option<LogEnabledFunction>, +} + +#[repr(C)] +#[derive(Clone, Copy)] +pub struct SparkDsCreateInfo { + pub size: i64, + pub name: *const c_char, + pub option_keys: *const *const c_char, + pub option_values: *const *const c_char, + pub num_options: i64, +} + +#[repr(C)] +#[derive(Clone, Copy)] +pub struct SparkDsReaderInfo { + pub size: i64, +} + +#[repr(C)] +#[derive(Clone, Copy)] +pub struct SparkDsPartitionsInfo { + pub size: i64, +} + +/// The domain of a column, which `push_domains` gets. The SDK binds it, so that the function +/// table has the size of the header, but does not expose it yet (SPARK-61306). +#[repr(C)] +#[derive(Clone, Copy)] +pub struct SparkDsDomain { + pub size: i64, + pub path: *const i32, + pub path_length: i64, + pub null_allowed: bool, + pub nan_allowed: bool, + pub ranges: *mut ArrowArray, + pub ranges_schema: *mut ArrowSchema, +} + +#[repr(C)] +#[derive(Clone, Copy)] +pub struct SparkDsStreamReaderInfo { + pub size: i64, + pub checkpoint_location: *const c_char, +} + +#[repr(C)] +#[derive(Clone, Copy)] +pub struct SparkDsInitialOffsetInfo { + pub size: i64, +} + +#[repr(C)] +#[derive(Clone, Copy)] +pub struct SparkDsReadLimit { + pub size: i64, + pub kind: i32, + pub max_rows: i64, +} + +#[repr(C)] +#[derive(Clone, Copy)] +pub struct SparkDsLatestOffsetInfo { + pub size: i64, + pub start: *const c_char, + pub limit: *const SparkDsReadLimit, + pub run_end: *const c_char, +} + +#[repr(C)] +#[derive(Clone, Copy)] +pub struct SparkDsStreamPartitionsInfo { + pub size: i64, + pub start: *const c_char, + pub end: *const c_char, +} + +#[repr(C)] +#[derive(Clone, Copy)] +pub struct SparkDsCommitOffsetInfo { + pub size: i64, + pub end: *const c_char, +} + +#[repr(C)] +#[derive(Clone, Copy)] +pub struct SparkDsTaskContext { + pub size: i64, + pub stage_id: i32, + pub stage_attempt_number: i32, + pub partition_id: i32, + pub attempt_number: i32, + pub task_attempt_id: i64, + pub is_interrupted: Option<Interrupted>, +} + +#[repr(C)] +#[derive(Clone, Copy)] +pub struct SparkDsReadInfo { + pub size: i64, + pub kind: i32, + pub schema: *mut ArrowSchema, + pub reader_state: *const u8, + pub reader_state_length: i64, + pub partition: *const u8, + pub partition_length: i64, +} + +#[repr(C)] +#[derive(Clone, Copy)] +pub struct SparkDsWriteInfo { + pub size: i64, + pub mode: i32, + pub query_id: *const c_char, +} + +#[repr(C)] +#[derive(Clone, Copy)] +pub struct SparkDsDataWriterInfo { + pub size: i64, + pub schema: *mut ArrowSchema, + pub writer_state: *const u8, + pub writer_state_length: i64, + pub epoch_id: i64, +} + +#[repr(C)] +#[derive(Clone, Copy)] +pub struct SparkDsFinishWriteInfo { + pub size: i64, + pub epoch_id: i64, + pub messages: *const *const u8, + pub message_lengths: *const i64, + pub num_messages: i64, + pub rejected: i64, +} + +#[repr(C)] +#[derive(Clone, Copy)] +pub struct SparkDsFinishDataWriterInfo { + pub size: i64, +} + +// What the library returns to Spark. + +/// The complete version 1 table. Unsupported operations have null function slots. +#[repr(C)] +pub struct SparkDsLibrary { + pub size: i64, + pub abi_version: i32, + pub last_error: Option<unsafe extern "C" fn() -> *const c_char>, + pub create: + Option<unsafe extern "C" fn(*const SparkDsCreateInfo, *mut u64, *mut *mut c_void) -> c_int>, + pub schema: Option<unsafe extern "C" fn(*mut c_void, *mut ArrowSchema) -> c_int>, + pub create_reader: Option< + unsafe extern "C" fn( + *mut c_void, + *mut ArrowSchema, + *const SparkDsReaderInfo, + *mut *mut c_void, + ) -> c_int, + >, + pub create_stream_reader: Option< + unsafe extern "C" fn( + *mut c_void, + *mut ArrowSchema, + *const SparkDsStreamReaderInfo, + *mut *mut c_void, + ) -> c_int, + >, + pub create_writer: Option< + unsafe extern "C" fn( + *mut c_void, + *mut ArrowSchema, + *const SparkDsWriteInfo, + SparkDsSink, + *mut c_void, + *mut *mut c_void, + ) -> c_int, + >, + pub close: Option<unsafe extern "C" fn(*mut c_void)>, + pub push_limit: Option<unsafe extern "C" fn(*mut c_void, i32, *mut bool) -> c_int>, + pub prune_columns: + Option<unsafe extern "C" fn(*mut c_void, *const *const c_char, i64) -> c_int>, + pub partitions: Option< + unsafe extern "C" fn( + *mut c_void, + *const SparkDsPartitionsInfo, + SparkDsSink, + SparkDsSink, + *mut c_void, + ) -> c_int, + >, + pub close_reader: Option<unsafe extern "C" fn(*mut c_void)>, + pub initial_offset: Option< + unsafe extern "C" fn( + *mut c_void, + *const SparkDsInitialOffsetInfo, + SparkDsSink, + *mut c_void, + ) -> c_int, + >, + pub latest_offset: Option< + unsafe extern "C" fn( + *mut c_void, + *const SparkDsLatestOffsetInfo, + SparkDsSink, + *mut c_void, + ) -> c_int, + >, + pub stream_partitions: Option< + unsafe extern "C" fn( + *mut c_void, + *const SparkDsStreamPartitionsInfo, + SparkDsSink, + SparkDsSink, + *mut c_void, + ) -> c_int, + >, + pub commit_offset: + Option<unsafe extern "C" fn(*mut c_void, *const SparkDsCommitOffsetInfo) -> c_int>, + pub close_stream_reader: Option<unsafe extern "C" fn(*mut c_void)>, + pub read: Option< + unsafe extern "C" fn( + *const SparkDsTaskContext, + *const SparkDsReadInfo, + *mut ArrowArrayStream, + ) -> c_int, + >, + pub commit: Option<unsafe extern "C" fn(*mut c_void, *const SparkDsFinishWriteInfo) -> c_int>, + pub abort: Option<unsafe extern "C" fn(*mut c_void, *const SparkDsFinishWriteInfo) -> c_int>, + pub close_writer: Option<unsafe extern "C" fn(*mut c_void)>, + pub create_data_writer: Option< + unsafe extern "C" fn( + *const SparkDsTaskContext, + *const SparkDsDataWriterInfo, + *mut *mut c_void, + ) -> c_int, + >, + pub write: Option<unsafe extern "C" fn(*mut c_void, *mut ArrowArray) -> c_int>, + pub commit_data_writer: Option< + unsafe extern "C" fn( + *mut c_void, + *const SparkDsFinishDataWriterInfo, + SparkDsSink, + *mut c_void, + ) -> c_int, + >, + pub abort_data_writer: + Option<unsafe extern "C" fn(*mut c_void, *const SparkDsFinishDataWriterInfo) -> c_int>, + pub close_data_writer: Option<unsafe extern "C" fn(*mut c_void)>, + pub default_read_limit: + Option<unsafe extern "C" fn(*mut c_void, *mut SparkDsReadLimit) -> c_int>, + // Added to version 1 by SPARK-61200, after the other functions. + pub push_domains: Option< + unsafe extern "C" fn(*mut c_void, *const *const SparkDsDomain, i64, *mut bool) -> c_int, + >, +} + +// The known sizes and field offsets on 64-bit targets, checked wherever the SDK builds. +// Tests also compare the actual Rust layouts with the current C header, using this field list. +macro_rules! layout { + ($($name:ident: $size:literal { $($field:ident: $offset:literal),* $(,)? })*) => { + #[cfg(target_pointer_width = "64")] + const _: () = { + $( + assert!(size_of::<$name>() == $size); + $(assert!(offset_of!($name, $field) == $offset);)* + )* + }; + + #[cfg(all(test, any(target_os = "linux", target_os = "macos")))] + fn c_layout_assertions() -> String { + use std::fmt::Write; + + let mut source = + String::from("#include <stddef.h>\n#include \"spark_datasource.h\"\n"); + $( + writeln!( + source, + "_Static_assert(sizeof({0}) == {1}, \"{0}: size\");", + stringify!($name), + size_of::<$name>(), + ).unwrap(); + writeln!( + source, + "_Static_assert(_Alignof({0}) == {1}, \"{0}: alignment\");", + stringify!($name), + std::mem::align_of::<$name>(), + ).unwrap(); + $( + writeln!( + source, + "_Static_assert(offsetof({0}, {1}) == {2}, \"{0}.{1}: offset\");", + stringify!($name), + stringify!($field), + offset_of!($name, $field), + ).unwrap(); + )* + )* + source + } + }; +} + +layout! { + SparkDsHost: 40 { size: 0, abi_version: 8, spark_version: 16, log: 24, is_log_enabled: 32 } + SparkDsCreateInfo: 40 { + size: 0, name: 8, option_keys: 16, option_values: 24, num_options: 32, + } + SparkDsReaderInfo: 8 { size: 0 } + SparkDsPartitionsInfo: 8 { size: 0 } + SparkDsDomain: 48 { + size: 0, path: 8, path_length: 16, null_allowed: 24, nan_allowed: 25, ranges: 32, + ranges_schema: 40, + } + SparkDsStreamReaderInfo: 16 { size: 0, checkpoint_location: 8 } + SparkDsInitialOffsetInfo: 8 { size: 0 } + SparkDsReadLimit: 24 { size: 0, kind: 8, max_rows: 16 } + SparkDsLatestOffsetInfo: 32 { size: 0, start: 8, limit: 16, run_end: 24 } + SparkDsStreamPartitionsInfo: 24 { size: 0, start: 8, end: 16 } + SparkDsCommitOffsetInfo: 16 { size: 0, end: 8 } + SparkDsTaskContext: 40 { + size: 0, stage_id: 8, stage_attempt_number: 12, partition_id: 16, attempt_number: 20, + task_attempt_id: 24, is_interrupted: 32, + } + SparkDsReadInfo: 56 { + size: 0, kind: 8, schema: 16, reader_state: 24, reader_state_length: 32, partition: 40, + partition_length: 48, + } + SparkDsWriteInfo: 24 { size: 0, mode: 8, query_id: 16 } + SparkDsDataWriterInfo: 40 { + size: 0, schema: 8, writer_state: 16, writer_state_length: 24, epoch_id: 32, + } + SparkDsFinishWriteInfo: 48 { + size: 0, epoch_id: 8, messages: 16, message_lengths: 24, num_messages: 32, rejected: 40, + } + SparkDsFinishDataWriterInfo: 8 { size: 0 } + SparkDsLibrary: 232 { + size: 0, abi_version: 8, last_error: 16, create: 24, schema: 32, create_reader: 40, + create_stream_reader: 48, create_writer: 56, close: 64, push_limit: 72, + prune_columns: 80, partitions: 88, close_reader: 96, initial_offset: 104, + latest_offset: 112, stream_partitions: 120, commit_offset: 128, + close_stream_reader: 136, read: 144, commit: 152, abort: 160, close_writer: 168, + create_data_writer: 176, write: 184, commit_data_writer: 192, abort_data_writer: 200, + close_data_writer: 208, default_read_limit: 216, push_domains: 224, + } +} + +/// A structure that Spark passes to the library, to read or to fill. +/// +/// # Safety +/// All zero bytes must be a valid value of every field after the first `MIN_SIZE` bytes, as they +/// are for numbers, pointers, and optional functions. +pub unsafe trait Input: Copy { + /// The size of the structure in Spark 4.4.0, the first release of version 1 of the ABI, which + /// is the least that Spark passes. A field that a later release appends does not change it: + /// [`input`] reads such a field and [`fill`] sets it only when Spark passes it. + const MIN_SIZE: usize; +} + +// The structures that Spark passes, with their sizes in Spark 4.4.0 on 64-bit targets. So far, +// each binding has the fields of 4.4.0, so its minimum is its size. Once a binding has a field +// that a later release appended, its minimum must be the offset of that field instead, as the +// assertion checks. +macro_rules! inputs { + ($($name:ident: $size:literal),* $(,)?) => { + $( + // SAFETY: the fields of the bindings are numbers, booleans, pointers, and optional + // functions. + unsafe impl Input for $name { + const MIN_SIZE: usize = size_of::<$name>(); + } + )* + + #[cfg(target_pointer_width = "64")] + const _: () = { + $(assert!(<$name as Input>::MIN_SIZE == $size);)* + }; + }; +} + +inputs! { + SparkDsCreateInfo: 40, + SparkDsReaderInfo: 8, + SparkDsPartitionsInfo: 8, + SparkDsDomain: 48, + SparkDsStreamReaderInfo: 16, + SparkDsInitialOffsetInfo: 8, + SparkDsReadLimit: 24, + SparkDsLatestOffsetInfo: 32, + SparkDsStreamPartitionsInfo: 24, + SparkDsCommitOffsetInfo: 16, + SparkDsTaskContext: 40, + SparkDsReadInfo: 56, + SparkDsWriteInfo: 24, + SparkDsDataWriterInfo: 40, + SparkDsFinishWriteInfo: 48, + SparkDsFinishDataWriterInfo: 8, +} + +thread_local! { + static LAST_ERROR: RefCell<Option<CString>> = const { RefCell::new(None) }; +} + +/// Converts provider failures and unwinding panics into the C error convention. +pub fn call(action: impl FnOnce() -> Result<()>) -> c_int { + LAST_ERROR.with(|error| *error.borrow_mut() = None); + let Err(message) = provider_call(action) else { + return 0; + }; + LAST_ERROR.with(|error| *error.borrow_mut() = Some(c_message(message))); + 1 +} + +/// Returns the calling thread's error until its next adapter call. +/// +/// # Safety +/// The returned pointer must not be freed or used after another adapter call on this thread. +pub unsafe extern "C" fn last_error() -> *const c_char { + LAST_ERROR.with(|error| { + error + .borrow() + .as_ref() + .map_or(std::ptr::null(), |message| message.as_ptr()) + }) +} + +/// Reads only a field that fits in a sized C structure. +/// +/// # Safety +/// `pointer` must address `size` readable bytes, and `offset` must identify a field of type `T`. +pub unsafe fn field<T: Copy>(pointer: *const u8, size: i64, offset: usize) -> Option<T> { + let end = offset.checked_add(size_of::<T>())?; + if usize::try_from(size).ok()? < end { + return None; + } + Some(unsafe { pointer.add(offset).cast::<T>().read_unaligned() }) +} + +/// # Safety +/// A non-null pointer must address the size prefix of a live C structure. +pub unsafe fn struct_size<T>(pointer: *const T) -> Result<i64> { + if pointer.is_null() { + return Err("null sized structure".into()); + } + let size = unsafe { pointer.cast::<i64>().read_unaligned() }; + if size < size_of::<i64>() as i64 { + return Err("structure is smaller than its size field".into()); + } + Ok(size) +} + +/// Copies a structure that Spark passes, which has at least the fields of Spark 4.4.0: fields are +/// only appended to the structures, so a shorter one is an error. The fields of the binding that +/// Spark does not pass, because a later release appended them, are zero. +/// +/// # Safety +/// A non-null pointer must address a live structure whose first field is its size. +pub unsafe fn input<T: Input>(pointer: *const T) -> Result<T> { + let size = unsafe { struct_size(pointer)? }; + if size < T::MIN_SIZE as i64 { + let name = std::any::type_name::<T>() + .rsplit("::") + .next() + .unwrap_or("structure"); + return Err( + format!("{name} has {size} bytes, fewer than in version 1 of the native ABI").into(), + ); + } + let mut value = MaybeUninit::<T>::zeroed(); + unsafe { + std::ptr::copy_nonoverlapping( + pointer.cast::<u8>(), + value.as_mut_ptr().cast::<u8>(), + fitting::<T>(size), + ); + // The bytes that Spark does not pass are zero, which Input makes valid. + Ok(value.assume_init()) + } +} + +/// Sets the fields of a structure that Spark passes for the library to fill, such as the +/// SparkDsReadLimit of default_read_limit, that fit in its size. +/// +/// # Safety +/// `pointer` must address a live structure whose first field is its size. +pub unsafe fn fill<T: Input>(pointer: *mut T, value: &T) -> Result<()> { + let size = unsafe { struct_size(pointer.cast_const())? }; + unsafe { + std::ptr::copy_nonoverlapping( + (value as *const T).cast::<u8>(), + pointer.cast::<u8>(), + fitting::<T>(size), + ) + }; + Ok(()) +} + +/// The bytes of a binding that fit in a structure of `size` bytes. +fn fitting<T>(size: i64) -> usize { + usize::try_from(size).map_or(size_of::<T>(), |size| size.min(size_of::<T>())) +} + +pub fn require_output<T>(pointer: *mut T) -> Result<()> { + if pointer.is_null() { + return Err("null output pointer".into()); + } + Ok(()) +} + +/// Negotiates the ABI version, installs the host's log, and runs the source's `init`. +/// +/// # Safety +/// `host` and `output` must follow the `spark_ds_init` contract of the C header. +pub unsafe fn initialize( + host: *const SparkDsHost, + output: *mut *const SparkDsLibrary, + library: &'static SparkDsLibrary, + init: fn(&HostInfo) -> Result<()>, +) -> c_int { + let result = call(|| { + require_output(output)?; + unsafe { output.write(std::ptr::null()) }; + let size = unsafe { struct_size(host)? }; + let pointer = host.cast(); + let version: i32 = unsafe { field(pointer, size, offset_of!(SparkDsHost, abi_version)) } + .ok_or("host does not provide an ABI version")?; + if version < ABI_VERSION { + return Err(format!("host ABI {version} does not support ABI {ABI_VERSION}").into()); + } + let log: Option<Option<LogFunction>> = + unsafe { field(pointer, size, offset_of!(SparkDsHost, log)) }; + let is_log_enabled: Option<Option<LogEnabledFunction>> = + unsafe { field(pointer, size, offset_of!(SparkDsHost, is_log_enabled)) }; + host::install(log.flatten(), is_log_enabled.flatten()); + let spark_version: Option<*const c_char> = + unsafe { field(pointer, size, offset_of!(SparkDsHost, spark_version)) }; + let spark_version = spark_version + .filter(|version| !version.is_null()) + .and_then(|version| unsafe { CStr::from_ptr(version) }.to_str().ok()) + .map(str::to_owned); + init(&HostInfo { + abi_version: version, + spark_version, + })?; + unsafe { output.write(library) }; + Ok(()) + }); + if result != 0 { + if let Ok(size) = unsafe { struct_size(host) } { + let log: Option<Option<LogFunction>> = + unsafe { field(host.cast(), size, offset_of!(SparkDsHost, log)) }; + if let Some(Some(log)) = log { + unsafe { log(LOG_ERROR, last_error()) }; + } + } + } + result +} + +/// # Safety +/// `data` must address `length` initialized values that remain valid for the returned borrow. +pub unsafe fn slice<'a, T>(data: *const T, length: i64) -> Result<&'a [T]> { + let length = usize::try_from(length)?; + if length == 0 { + return Ok(&[]); + } + if data.is_null() + || length + .checked_mul(size_of::<T>()) + .is_none_or(|size| size > isize::MAX as usize) + { + return Err("invalid array pointer or length".into()); + } + Ok(unsafe { std::slice::from_raw_parts(data, length) }) +} + +/// # Safety +/// A non-null `pointer` must address a NUL-terminated string, readable during this call. +pub unsafe fn string(pointer: *const c_char) -> Result<String> { + if pointer.is_null() { + return Err("null string".into()); + } + Ok(unsafe { CStr::from_ptr(pointer) }.to_str()?.to_owned()) +} + +/// # Safety +/// As for [`string`], for a pointer that may be null. +pub unsafe fn optional_string(pointer: *const c_char) -> Result<Option<String>> { + if pointer.is_null() { + Ok(None) + } else { + Ok(Some(unsafe { string(pointer)? })) + } +} + +/// # Safety +/// Each element must be a NUL-terminated UTF-8 string, readable during this call. +pub unsafe fn strings(data: *const *const c_char, length: i64) -> Result<Vec<String>> { + unsafe { slice(data, length)? } + .iter() + .map(|pointer| unsafe { string(*pointer) }) + .collect() +} + +/// # Safety +/// `info` must follow the `create` contract of the C header. +pub unsafe fn create_info(info: *const SparkDsCreateInfo) -> Result<CreateInfo> { + let info = unsafe { input(info)? }; + if info.name.is_null() { + return Err("create info has a null name".into()); + } + let name = unsafe { string(info.name)? }; + let keys = unsafe { strings(info.option_keys, info.num_options)? }; + let values = unsafe { strings(info.option_values, info.num_options)? }; + let options: Options = keys.into_iter().zip(values).collect(); + Ok(CreateInfo::new(name, options)) +} + +/// The mode of a write, and the ID of the query of a streaming write. +/// +/// # Safety +/// `info` must follow the create_writer contract of the C header. +pub unsafe fn write_info(info: *const SparkDsWriteInfo) -> Result<(WriteMode, Option<String>)> { + let info = unsafe { input(info)? }; + let mode = match info.mode { + WRITE_APPEND => WriteMode::Append, + WRITE_TRUNCATE => WriteMode::Truncate, + mode => return Err(format!("unsupported native write mode: {mode}").into()), + }; + Ok((mode, unsafe { optional_string(info.query_id)? })) +} + +/// The start, the limit, and the end of the run of a request for the latest offset, with the +/// offsets as JSON. +/// +/// # Safety +/// `info` must follow the latest_offset contract of the C header. +pub unsafe fn latest_offset_info( + info: *const SparkDsLatestOffsetInfo, +) -> Result<LatestOffsetInfo<String>> { + let info = unsafe { input(info)? }; + let limit = unsafe { input(info.limit)? }; + let limit = match limit.kind { + READ_ALL_AVAILABLE => ReadLimit::AllAvailable, + READ_MAX_ROWS => ReadLimit::MaxRows(u64::try_from(limit.max_rows)?), + kind => return Err(format!("unsupported kind of read limit: {kind}").into()), + }; + Ok(LatestOffsetInfo { + start: unsafe { string(info.start)? }, + limit, + run_end: unsafe { optional_string(info.run_end)? }, + }) +} + +/// Sets the limit that `default_read_limit` returns. +/// +/// # Safety +/// `output` must be the SparkDsReadLimit that Spark initialized with its size. +pub unsafe fn set_read_limit(output: *mut SparkDsReadLimit, limit: ReadLimit) -> Result<()> { + let mut value = unsafe { input(output.cast_const())? }; + match limit { + ReadLimit::AllAvailable => value.kind = READ_ALL_AVAILABLE, + ReadLimit::MaxRows(rows) => { + value.kind = READ_MAX_ROWS; + value.max_rows = i64::try_from(rows) + .map_err(|_| format!("a read limit of {rows} rows exceeds the native ABI"))?; + } + } + unsafe { fill(output, &value) } +} + +/// # Safety +/// Both arrays must contain count readable values. Non-null message pointers +/// must address their lengths of bytes and remain valid for the returned borrow. +pub unsafe fn messages<'a>( + pointers: *const *const u8, + lengths: *const i64, + count: i64, +) -> Result<Vec<Option<&'a [u8]>>> { + let pointers = unsafe { slice(pointers, count)? }; + let lengths = unsafe { slice(lengths, count)? }; + pointers + .iter() + .zip(lengths) + .map(|(&pointer, &length)| { + if pointer.is_null() { + if length != 0 { + return Err("null commit message must have zero length".into()); + } + Ok(None) + } else { + Ok(Some(unsafe { slice(pointer, length)? })) + } + }) + .collect() +} + +/// # Safety +/// The sink must be valid on this thread for this call and synchronously copy the bytes. +pub unsafe fn emit(callback: SparkDsSink, sink: *mut c_void, bytes: &[u8]) -> Result<()> { + let callback = callback.ok_or("null result sink")?; + unsafe { callback(sink, bytes.as_ptr(), bytes.len().try_into()?) }; + Ok(()) +} + +/// # Safety +/// `input` must be a live ArrowSchema that this call is permitted to move. +pub unsafe fn import_schema(input: *mut ArrowSchema) -> Result<SchemaRef> { + require_output(input)?; + let schema = unsafe { ArrowSchema::from_raw(input) }; + Ok(Arc::new(Schema::try_from(&schema)?)) +} + +/// # Safety +/// `output` must be writable ArrowSchema storage with a null release callback. +pub unsafe fn export_schema(schema: &SchemaRef, output: *mut ArrowSchema) -> Result<()> { + require_output(output)?; + let schema = ArrowSchema::try_from(schema.as_ref())?; + unsafe { output.write(schema) }; + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[cfg(any(target_os = "linux", target_os = "macos"))] + #[test] + fn c_header_matches_rust_layout() -> Result<()> { + use std::fs; + use std::path::{Path, PathBuf}; + use std::process::Command; + use std::time::{SystemTime, UNIX_EPOCH}; + + struct TestDirectory(PathBuf); + + impl Drop for TestDirectory { + fn drop(&mut self) { + let _ = fs::remove_dir_all(&self.0); + } + } + + let unique = SystemTime::now().duration_since(UNIX_EPOCH)?.as_nanos(); + let directory = TestDirectory(std::env::temp_dir().join(format!( + "spark-native-layout-{}-{unique}", + std::process::id() + ))); + fs::create_dir(&directory.0)?; + let source = directory.0.join("abi_layout.c"); + fs::write(&source, c_layout_assertions())?; + let header = Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../../core/src/main/resources/org/apache/spark/sql/datasource"); + + // Match the C integration harness: CC can include a launcher or leading arguments. + let compiler = std::env::var("CC").unwrap_or_default(); + let mut words = compiler.split_whitespace(); + let compiled = Command::new(words.next().unwrap_or("cc")) + .args(words) + .args(["-std=c11", "-Wall", "-Wextra", "-Werror", "-fsyntax-only"]) + .arg("-I") + .arg(header) + .arg(source) + .output()?; + assert!( + compiled.status.success(), + "Rust ABI layouts do not match spark_datasource.h:\n{}\n{}", + String::from_utf8_lossy(&compiled.stdout), + String::from_utf8_lossy(&compiled.stderr), + ); + Ok(()) + } + + #[test] + fn errors_and_panics_are_thread_local_c_strings() { + assert_ne!(call(|| Err("failed\0operation".into())), 0); + let error = unsafe { CStr::from_ptr(last_error()) }.to_str().unwrap(); + assert_eq!(error, "failed\\0operation"); + std::thread::spawn(|| { + assert!(unsafe { last_error() }.is_null()); + assert_ne!(call(|| panic!("provider panic")), 0); + let error = unsafe { CStr::from_ptr(last_error()) }.to_str().unwrap(); + assert!(error.contains("provider panic")); + }) + .join() + .unwrap(); + assert_eq!( + unsafe { CStr::from_ptr(last_error()) }.to_str().unwrap(), + error + ); + assert_eq!(call(|| Ok(())), 0); + assert!(unsafe { last_error() }.is_null()); + } + + #[test] + fn empty_arrays_accept_null_and_truncated_structures_fail() { + assert!(unsafe { strings(std::ptr::null(), 0) }.unwrap().is_empty()); + let prefix = size_of::<i64>() as i64; + let info = (&prefix as *const i64).cast::<SparkDsCreateInfo>(); + let error = unsafe { create_info(info) }.unwrap_err().to_string(); + assert_eq!( + error, + "SparkDsCreateInfo has 8 bytes, fewer than in version 1 of the native ABI" + ); + assert!(unsafe { input::<SparkDsReadInfo>(std::ptr::null()) }.is_err()); + } + + /// A structure to which a release after 4.4.0 appended `added`. + #[repr(C)] + #[derive(Clone, Copy, Debug, PartialEq)] + struct Grown { + size: i64, + value: i64, + added: i64, + } + + // SAFETY: the fields are numbers. + unsafe impl Input for Grown { + const MIN_SIZE: usize = offset_of!(Grown, added); + } + + #[test] + fn appended_fields_are_read_and_set_only_when_spark_passes_them() { + let read = |structure: &[i64]| unsafe { input(structure.as_ptr().cast::<Grown>()) }; + let grown = |size, added| Grown { + size, + value: 5, + added, + }; + // As from Spark 4.4.0, from the release that appended the field, and from a later one. + assert_eq!(read(&[16, 5]).unwrap(), grown(16, 0)); + assert_eq!(read(&[24, 5, 7]).unwrap(), grown(24, 7)); + assert_eq!(read(&[32, 5, 7, 9]).unwrap(), grown(32, 7)); Review Comment: Done in e91f9cdf51aa: The 4.4.0 read case is now `read(&[16, 5, -1])` and should still give `grown(16, 0)`. I also added a fill case that passes `[32, 0, 0, -1]` and expects `[32, 5, 7, -1]`, so the test fails if `fitting` loses its `min`. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
