timsaucer commented on code in PR #19223:
URL: https://github.com/apache/datafusion/pull/19223#discussion_r2606002100


##########
datafusion/ffi/src/session/mod.rs:
##########
@@ -0,0 +1,614 @@
+// 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.
+
+use std::any::Any;
+use std::collections::HashMap;
+use std::ffi::c_void;
+use std::sync::Arc;
+
+use abi_stable::std_types::{RHashMap, RResult, RStr, RString, RVec};
+use abi_stable::StableAbi;
+use arrow_schema::ffi::FFI_ArrowSchema;
+use arrow_schema::SchemaRef;
+use async_ffi::{FfiFuture, FutureExt};
+use async_trait::async_trait;
+use datafusion_common::config::{ConfigOptions, TableOptions};
+use datafusion_common::{DFSchema, DataFusionError};
+use datafusion_execution::config::SessionConfig;
+use datafusion_execution::runtime_env::RuntimeEnv;
+use datafusion_execution::TaskContext;
+use datafusion_expr::execution_props::ExecutionProps;
+use datafusion_expr::{
+    AggregateUDF, AggregateUDFImpl, Expr, LogicalPlan, ScalarUDF, 
ScalarUDFImpl,
+    WindowUDF, WindowUDFImpl,
+};
+use datafusion_physical_expr::PhysicalExpr;
+use datafusion_physical_plan::ExecutionPlan;
+use datafusion_proto::bytes::{logical_plan_from_bytes, logical_plan_to_bytes};
+use datafusion_proto::logical_plan::from_proto::parse_expr;
+use datafusion_proto::logical_plan::to_proto::serialize_expr;
+use datafusion_proto::logical_plan::{
+    DefaultLogicalExtensionCodec, LogicalExtensionCodec,
+};
+use datafusion_proto::protobuf::LogicalExprNode;
+use datafusion_session::Session;
+use prost::Message;
+use tokio::runtime::Handle;
+
+use crate::arrow_wrappers::WrappedSchema;
+use crate::execution::{FFI_TaskContext, FFI_TaskContextProvider};
+use crate::execution_plan::FFI_ExecutionPlan;
+use crate::physical_expr::FFI_PhysicalExpr;
+use crate::proto::logical_extension_codec::FFI_LogicalExtensionCodec;
+use crate::session::config::FFI_SessionConfig;
+use crate::udaf::FFI_AggregateUDF;
+use crate::udf::FFI_ScalarUDF;
+use crate::udwf::FFI_WindowUDF;
+use crate::util::FFIResult;
+use crate::{df_result, rresult, rresult_return};
+
+pub mod config;
+
+/// A stable struct for sharing [`Session`] across FFI boundaries.
+#[repr(C)]
+#[derive(Debug, StableAbi)]
+#[allow(non_camel_case_types)]
+pub struct FFI_Session {
+    pub session_id: unsafe extern "C" fn(&Self) -> RStr,
+
+    pub config: unsafe extern "C" fn(&Self) -> FFI_SessionConfig,
+
+    pub create_physical_plan:
+        unsafe extern "C" fn(
+            &Self,
+            logical_plan_serialized: RVec<u8>,
+        ) -> FfiFuture<FFIResult<FFI_ExecutionPlan>>,
+
+    pub create_physical_expr: unsafe extern "C" fn(
+        &Self,
+        expr_serialized: RVec<u8>,
+        schema: WrappedSchema,
+    ) -> FFIResult<FFI_PhysicalExpr>,
+
+    pub scalar_functions: unsafe extern "C" fn(&Self) -> RHashMap<RString, 
FFI_ScalarUDF>,
+
+    pub aggregate_functions:
+        unsafe extern "C" fn(&Self) -> RHashMap<RString, FFI_AggregateUDF>,
+
+    pub window_functions: unsafe extern "C" fn(&Self) -> RHashMap<RString, 
FFI_WindowUDF>,
+
+    // TODO: Expand scope of FFI to include runtime environment
+    // pub runtime_env: unsafe extern "C" fn(&Self) -> FFI_RuntimeEnv,
+
+    // pub execution_props: unsafe extern "C" fn(&Self) -> FFI_ExecutionProps,
+    pub table_options: unsafe extern "C" fn(&Self) -> RHashMap<RString, 
RString>,
+
+    pub default_table_options: unsafe extern "C" fn(&Self) -> 
RHashMap<RString, RString>,
+
+    pub task_ctx: unsafe extern "C" fn(&Self) -> FFI_TaskContext,
+
+    pub logical_codec: FFI_LogicalExtensionCodec,
+
+    /// Used to create a clone on the provider of the registry. This should
+    /// only need to be called by the receiver of the plan.
+    pub clone: unsafe extern "C" fn(plan: &Self) -> Self,
+
+    /// Release the memory of the private data when it is no longer being used.
+    pub release: unsafe extern "C" fn(arg: &mut Self),
+
+    /// Return the major DataFusion version number of this registry.
+    pub version: unsafe extern "C" fn() -> u64,
+
+    /// Internal data. This is only to be accessed by the provider of the plan.
+    /// A [`ForeignSession`] should never attempt to access this data.
+    pub private_data: *mut c_void,
+
+    /// Utility to identify when FFI objects are accessed locally through
+    /// the foreign interface.
+    pub library_marker_id: extern "C" fn() -> usize,
+}
+
+unsafe impl Send for FFI_Session {}
+unsafe impl Sync for FFI_Session {}
+
+struct SessionPrivateData<'a> {
+    session: &'a (dyn Session + Send + Sync),
+    runtime: Option<Handle>,
+}

Review Comment:
   Yes, I think the two options are:
   - update `TableProvider` to pass an `Arc<dyn Session>`. This is a breaking 
change that would require all implementers to support. Maybe that is ok?
   - Your approach of removing `pub` and restricting this to the crate where we 
know the two use cases have already guaranteed the lifetime of the Session 
during their methods. 
   
   I'm tempted to go with the second. It becomes a small implementation problem 
because the code that uses this is not in this PR so it generates clippy errors 
for unused functions once they are no longer `pub`. I think for this PR I will 
add some strongly worded documentation and then in the follow on PR that 
updates TableProvider we can revisit if we want to remove the `pub` 



-- 
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]

Reply via email to