|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use datafusion::dataframe::DataFrame; |
| 19 | +use datafusion::prelude::SessionContext; |
| 20 | +use datafusion_proto::logical_plan::{AsLogicalPlan, DefaultLogicalExtensionCodec}; |
| 21 | +use datafusion_proto::protobuf::LogicalPlanNode; |
| 22 | +use jni::objects::{JByteArray, JClass}; |
| 23 | +use jni::sys::jlong; |
| 24 | +use jni::JNIEnv; |
| 25 | +use prost::Message; |
| 26 | + |
| 27 | +use crate::errors::{try_unwrap_or_throw, JniResult}; |
| 28 | +use crate::runtime; |
| 29 | + |
| 30 | +#[no_mangle] |
| 31 | +pub extern "system" fn Java_org_apache_datafusion_SessionContext_createDataFrameFromProto<'local>( |
| 32 | + mut env: JNIEnv<'local>, |
| 33 | + _class: JClass<'local>, |
| 34 | + handle: jlong, |
| 35 | + plan_bytes: JByteArray<'local>, |
| 36 | +) -> jlong { |
| 37 | + try_unwrap_or_throw(&mut env, 0, |env| -> JniResult<jlong> { |
| 38 | + if handle == 0 { |
| 39 | + return Err("SessionContext handle is null".into()); |
| 40 | + } |
| 41 | + let ctx = unsafe { &*(handle as *const SessionContext) }; |
| 42 | + let bytes: Vec<u8> = env.convert_byte_array(&plan_bytes)?; |
| 43 | + let node = LogicalPlanNode::decode(bytes.as_slice())?; |
| 44 | + let codec = DefaultLogicalExtensionCodec {}; |
| 45 | + let task_ctx = ctx.task_ctx(); |
| 46 | + let plan = node.try_into_logical_plan(task_ctx.as_ref(), &codec)?; |
| 47 | + let df: DataFrame = runtime().block_on(ctx.execute_logical_plan(plan))?; |
| 48 | + Ok(Box::into_raw(Box::new(df)) as jlong) |
| 49 | + }) |
| 50 | +} |
0 commit comments