forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
07f55c5
commit c27400e
Showing
13 changed files
with
183 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# arrow_wrap_unwrap | ||
|
||
This is some arbitrary code to demonstrate [wrapping and | ||
unwrapping](https://arrow.apache.org/docs/python/integration/extending.html) | ||
functionality. This tooling allows you to interop pyarrow objects with Cython or Arrow | ||
C++. | ||
|
||
Instructions: | ||
|
||
``` | ||
python setup.py build_ext --inplace | ||
./run_demo.py | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# distutils: language=c++ | ||
|
||
import pyarrow as pa | ||
import pyarrow.acero as pac | ||
import pyarrow.compute as pc | ||
|
||
from pyarrow cimport * | ||
from pyarrow.lib cimport * | ||
from pyarrow.lib_compute cimport * | ||
from pyarrow.lib_acero cimport * | ||
|
||
def unwrap_wrap_arr(array): | ||
cdef shared_ptr[CArray] arr = pyarrow_unwrap_array(array) | ||
out = pyarrow_wrap_array(arr) | ||
return out | ||
|
||
def unwrap_wrap_declaration(declaration): | ||
cdef CDeclaration decl = pyarrow_unwrap_declaration(declaration) | ||
output = pyarrow_wrap_declaration(decl) | ||
return output | ||
|
||
def unwrap_wrap_options(options): | ||
cdef shared_ptr[CExecNodeOptions] decl = pyarrow_unwrap_exec_node_options(options) | ||
output = pyarrow_wrap_exec_node_options(decl) | ||
return output | ||
|
||
def unwrap_wrap_expression(expression): | ||
cdef CExpression expr = pyarrow_unwrap_expression(expression) | ||
output = pyarrow_wrap_expression(expr) | ||
return output | ||
|
||
|
||
def run_test(): | ||
print("Starting demo") | ||
arr = pa.array(["a", "b", "a"]) | ||
|
||
print("Wrapping and unwrapping array") | ||
arr = unwrap_wrap_arr(arr) | ||
print(arr) | ||
|
||
print("Constructing a table") | ||
table = pa.Table.from_arrays([arr], names=["foo"]) | ||
print(table) | ||
|
||
expression = (pc.field("foo") == pc.scalar("a")) | ||
print("Wrapping and unwrapping expression") | ||
expression = unwrap_wrap_expression(expression) | ||
print(expression) | ||
|
||
print("Filtering the table") | ||
table = table.filter(expression) | ||
|
||
print("Running a no-op acero node") | ||
options = pac.TableSourceNodeOptions(table) | ||
options = unwrap_wrap_options(options) | ||
|
||
source_node = pac.Declaration("table_source", options, []) | ||
source_node = unwrap_wrap_declaration(source_node) | ||
print(source_node.to_table()) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import example | ||
|
||
if __name__ == '__main__': | ||
example.run_test() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from setuptools import setup | ||
from Cython.Build import cythonize | ||
|
||
import os | ||
import numpy as np | ||
import pyarrow as pa | ||
|
||
|
||
ext_modules = cythonize("example.pyx") | ||
|
||
for ext in ext_modules: | ||
# The Numpy C headers are currently required | ||
ext.include_dirs.append(np.get_include()) | ||
ext.include_dirs.append(pa.get_include()) | ||
ext.libraries.extend(pa.get_libraries()) | ||
ext.library_dirs.extend(pa.get_library_dirs()) | ||
|
||
if os.name == 'posix': | ||
ext.extra_compile_args.append('-std=c++17') | ||
|
||
setup(ext_modules=ext_modules) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,3 @@ | |
|
||
// For backward compatibility. | ||
#include "arrow/python/lib_api.h" | ||
#include "arrow/python/lib_compute_api.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// 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. | ||
#include "arrow/compute/expression.h" | ||
|
||
#include "arrow/python/lib_compute_api.h" | ||
#include "arrow/python/pyarrow_compute.h" | ||
#include "arrow/python/wrap_macros.h" | ||
|
||
namespace arrow { | ||
namespace py { | ||
|
||
int import_pyarrow_compute() { return ::import_pyarrow__lib_compute(); } | ||
|
||
DEFINE_WRAP_FUNCTIONS(expression, compute::Expression, out.is_valid()) | ||
|
||
} // namespace py | ||
} // namespace arrow |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// 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. | ||
|
||
#pragma once | ||
|
||
#include "arrow/python/visibility.h" | ||
#include "arrow/python/wrap_macros.h" | ||
|
||
// Work around ARROW-2317 (C linkage warning from Cython) | ||
extern "C++" { | ||
|
||
namespace arrow { | ||
|
||
// Forward declarations. Actual wrappers/unwrappers are in pyarrow_compute.{h,cc} | ||
namespace compute { | ||
class Expression; | ||
} | ||
|
||
namespace py { | ||
|
||
ARROW_PYTHON_EXPORT int import_pyarrow_compute(); | ||
|
||
DECLARE_WRAP_FUNCTIONS(expression, compute::Expression) | ||
|
||
} // namespace py | ||
} // namespace arrow | ||
|
||
} // extern "C++" |