You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/05/28 10:30:46 UTC

[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #2622: Export minimum C API and examples for C, Ruby and Python

alamb commented on code in PR #2622:
URL: https://github.com/apache/arrow-datafusion/pull/2622#discussion_r884115250


##########
datafusion/c/src/lib.rs:
##########
@@ -0,0 +1,178 @@
+// 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::boxed::Box;
+use std::ffi::CStr;
+use std::ffi::CString;
+use std::future::Future;
+use std::sync::Arc;
+
+use datafusion::dataframe::DataFrame;
+use datafusion::execution::context::SessionContext;
+
+#[repr(C)]
+pub struct DFError {
+    code: u32,
+    message: *mut libc::c_char,
+}
+
+impl DFError {
+    pub fn new(code: u32, message: *mut libc::c_char) -> Self {
+        Self { code, message }
+    }
+}
+
+#[no_mangle]
+#[allow(clippy::not_unsafe_ptr_arg_deref)]
+pub extern "C" fn df_error_new(code: u32, message: *const libc::c_char) -> *mut DFError {
+    let error = DFError::new(code, unsafe { libc::strdup(message) });
+    Box::into_raw(Box::new(error))
+}
+
+/// # Safety
+///
+/// This function should not be called with `error` that is not
+/// created by `df_errro_new()`.
+///
+/// This function should not be called for the same `error` multiple
+/// times.
+#[no_mangle]
+pub unsafe extern "C" fn df_error_free(error: *mut DFError) {
+    libc::free((*error).message as *mut libc::c_void);
+    Box::from_raw(error);
+}
+
+/// # Safety
+///
+/// This function should not be called with `error` that is not
+/// created by `df_errro_new()`.
+///
+/// This function should not be called with `error` that is freed by
+/// `df_error_free()`.
+#[no_mangle]
+pub unsafe extern "C" fn df_error_get_message(
+    error: *mut DFError,
+) -> *const libc::c_char {
+    (*error).message
+}
+
+trait IntoDFError {
+    type Value;
+    fn into_df_error(
+        self,
+        error: *mut *mut DFError,
+        error_value: Option<Self::Value>,
+    ) -> Option<Self::Value>;
+}
+
+impl<V, E: std::fmt::Display> IntoDFError for Result<V, E> {
+    type Value = V;
+    fn into_df_error(
+        self,
+        error: *mut *mut DFError,
+        error_value: Option<Self::Value>,
+    ) -> Option<Self::Value> {
+        match self {
+            Ok(value) => Some(value),
+            Err(e) => {
+                if !error.is_null() {
+                    let c_string_message = match CString::new(format!("{}", e)) {
+                        Ok(c_string_message) => c_string_message,
+                        Err(_) => return error_value,
+                    };
+                    unsafe {
+                        *error = df_error_new(1, c_string_message.as_ptr());
+                    };
+                }
+                error_value
+            }
+        }
+    }
+}
+
+fn block_on<F: Future>(future: F) -> F::Output {
+    tokio::runtime::Runtime::new().unwrap().block_on(future)

Review Comment:
   ```suggestion
       tokio::runtime::Runtime::new().expect("Can not create tokio runtime").block_on(future)
   ```



##########
datafusion/c/src/lib.rs:
##########
@@ -0,0 +1,178 @@
+// 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::boxed::Box;
+use std::ffi::CStr;
+use std::ffi::CString;
+use std::future::Future;
+use std::sync::Arc;
+
+use datafusion::dataframe::DataFrame;
+use datafusion::execution::context::SessionContext;
+
+#[repr(C)]
+pub struct DFError {
+    code: u32,
+    message: *mut libc::c_char,
+}
+
+impl DFError {
+    pub fn new(code: u32, message: *mut libc::c_char) -> Self {
+        Self { code, message }
+    }
+}
+
+#[no_mangle]
+#[allow(clippy::not_unsafe_ptr_arg_deref)]
+pub extern "C" fn df_error_new(code: u32, message: *const libc::c_char) -> *mut DFError {
+    let error = DFError::new(code, unsafe { libc::strdup(message) });
+    Box::into_raw(Box::new(error))
+}
+
+/// # Safety
+///
+/// This function should not be called with `error` that is not
+/// created by `df_errro_new()`.
+///
+/// This function should not be called for the same `error` multiple
+/// times.
+#[no_mangle]
+pub unsafe extern "C" fn df_error_free(error: *mut DFError) {
+    libc::free((*error).message as *mut libc::c_void);
+    Box::from_raw(error);
+}
+
+/// # Safety
+///
+/// This function should not be called with `error` that is not
+/// created by `df_errro_new()`.
+///
+/// This function should not be called with `error` that is freed by
+/// `df_error_free()`.
+#[no_mangle]
+pub unsafe extern "C" fn df_error_get_message(
+    error: *mut DFError,
+) -> *const libc::c_char {
+    (*error).message
+}
+
+trait IntoDFError {
+    type Value;
+    fn into_df_error(
+        self,
+        error: *mut *mut DFError,
+        error_value: Option<Self::Value>,
+    ) -> Option<Self::Value>;
+}
+
+impl<V, E: std::fmt::Display> IntoDFError for Result<V, E> {
+    type Value = V;
+    fn into_df_error(
+        self,
+        error: *mut *mut DFError,
+        error_value: Option<Self::Value>,
+    ) -> Option<Self::Value> {
+        match self {
+            Ok(value) => Some(value),
+            Err(e) => {
+                if !error.is_null() {
+                    let c_string_message = match CString::new(format!("{}", e)) {

Review Comment:
   ```suggestion
                       let c_string_message = match CString::new(e.to_string()) {
   ```



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org