@@ -50,7 +50,7 @@ use datafusion::dataframe::DataFrame;
5050use datafusion:: dataframe:: DataFrameWriteOptions ;
5151use datafusion:: error:: DataFusionError ;
5252use datafusion:: execution:: disk_manager:: { DiskManagerBuilder , DiskManagerMode } ;
53- use datafusion:: execution:: runtime_env:: RuntimeEnvBuilder ;
53+ use datafusion:: execution:: runtime_env:: { RuntimeEnv , RuntimeEnvBuilder } ;
5454use datafusion:: execution:: SendableRecordBatchStream ;
5555use datafusion:: logical_expr:: Expr ;
5656use datafusion:: logical_expr:: { col, Partitioning , ScalarUDF , Signature , SortExpr } ;
@@ -117,6 +117,42 @@ fn install_memory_tracker(
117117 tracker
118118}
119119
120+ /// Build a `SessionContext` from a prepared config + runtime env. When
121+ /// `spark_functions` is set, register Apache Spark-compatible functions and
122+ /// expression planners via the `datafusion-spark` crate (requires the `spark`
123+ /// Cargo feature); otherwise build the plain context. Returns an error if the
124+ /// caller asked for Spark functions in a build that compiled the feature off.
125+ fn build_session_context (
126+ config : SessionConfig ,
127+ runtime_env : Arc < RuntimeEnv > ,
128+ spark_functions : bool ,
129+ ) -> JniResult < SessionContext > {
130+ if spark_functions {
131+ #[ cfg( feature = "spark" ) ]
132+ {
133+ use datafusion:: execution:: SessionStateBuilder ;
134+ use datafusion_spark:: SessionStateBuilderSpark ;
135+ // Order matters: `with_spark_features` runs after
136+ // `with_default_features` so Spark implementations override the
137+ // DataFusion built-ins of the same name.
138+ let state = SessionStateBuilder :: new ( )
139+ . with_config ( config)
140+ . with_runtime_env ( runtime_env)
141+ . with_default_features ( )
142+ . with_spark_features ( )
143+ . build ( ) ;
144+ Ok ( SessionContext :: new_with_state ( state) )
145+ }
146+ #[ cfg( not( feature = "spark" ) ) ]
147+ {
148+ let _ = ( config, runtime_env) ;
149+ Err ( "spark Cargo feature is not enabled in this build of datafusion-jni" . into ( ) )
150+ }
151+ } else {
152+ Ok ( SessionContext :: new_with_config_rt ( config, runtime_env) )
153+ }
154+ }
155+
120156#[ no_mangle]
121157pub extern "system" fn Java_org_apache_datafusion_SessionContext_createSessionContext < ' local > (
122158 mut env : JNIEnv < ' local > ,
@@ -225,8 +261,9 @@ pub extern "system" fn Java_org_apache_datafusion_SessionContext_createSessionCo
225261 // update two atomics.
226262 let tracker = install_memory_tracker ( & mut runtime_builder) ;
227263
228- let runtime_env = runtime_builder. build ( ) ?;
229- let ctx = SessionContext :: new_with_config_rt ( config, Arc :: new ( runtime_env) ) ;
264+ let runtime_env = Arc :: new ( runtime_builder. build ( ) ?) ;
265+ let ctx =
266+ build_session_context ( config, runtime_env, opts. spark_functions . unwrap_or ( false ) ) ?;
230267
231268 // Object-store registrations come last because they need a built
232269 // RuntimeEnv to register against. A failure here drops `ctx` (and its
0 commit comments