-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEAT][JAVA] Implement READERS and WRITERS for Java (#233)
- Loading branch information
Showing
74 changed files
with
5,687 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
.idea | ||
dependency-reduced-pom.xml | ||
target | ||
gar-java.iml | ||
cmake-build-debug |
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
22 changes: 22 additions & 0 deletions
22
java/src/main/cpp/ffi/jni_com_alibaba_graphar_arrow_ArrowTable_Static.cc
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,22 @@ | ||
#include <jni.h> | ||
#include <new> | ||
#include <iostream> | ||
#include "arrow/api.h" | ||
#include "arrow/c/bridge.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
// Common Stubs | ||
|
||
JNIEXPORT | ||
jlong JNICALL Java_com_alibaba_graphar_arrow_ArrowTable_1Static_1cxx_10x58c7409_nativeFromArrowArrayAndArrowSchema(JNIEnv*, jclass, jlong rv_base, jlong arg0 /* arrayAddress0 */, jlong arg1 /* schemaAddress1 */) { | ||
auto maybeRecordBatch = arrow::ImportRecordBatch(reinterpret_cast<struct ArrowArray*>(arg0), reinterpret_cast<struct ArrowSchema*>(arg1)); | ||
auto table = arrow::Table::FromRecordBatches({maybeRecordBatch.ValueOrDie()}); | ||
return reinterpret_cast<jlong>(new((void*)rv_base) arrow::Result<std::shared_ptr<arrow::Table>>(table)); | ||
} | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif |
30 changes: 30 additions & 0 deletions
30
java/src/main/java/com/alibaba/graphar/arrow/ArrowArray.java
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,30 @@ | ||
/* | ||
* Copyright 2022 Alibaba Group Holding Limited. | ||
* | ||
* Licensed 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. | ||
*/ | ||
|
||
package com.alibaba.graphar.arrow; | ||
|
||
import static com.alibaba.graphar.util.CppClassName.ARROW_ARRAY; | ||
import static com.alibaba.graphar.util.CppHeaderName.ARROW_API_H; | ||
|
||
import com.alibaba.fastffi.CXXHead; | ||
import com.alibaba.fastffi.CXXPointer; | ||
import com.alibaba.fastffi.FFIGen; | ||
import com.alibaba.fastffi.FFITypeAlias; | ||
|
||
@FFIGen | ||
@FFITypeAlias(ARROW_ARRAY) | ||
@CXXHead(ARROW_API_H) | ||
public interface ArrowArray extends CXXPointer { | ||
long length(); | ||
} |
37 changes: 37 additions & 0 deletions
37
java/src/main/java/com/alibaba/graphar/arrow/ArrowResult.java
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,37 @@ | ||
/* | ||
* Copyright 2022 Alibaba Group Holding Limited. | ||
* | ||
* Licensed 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. | ||
*/ | ||
|
||
package com.alibaba.graphar.arrow; | ||
|
||
import static com.alibaba.graphar.util.CppClassName.ARROW_RESULT; | ||
import static com.alibaba.graphar.util.CppHeaderName.ARROW_API_H; | ||
|
||
import com.alibaba.fastffi.CXXHead; | ||
import com.alibaba.fastffi.CXXReference; | ||
import com.alibaba.fastffi.FFIGen; | ||
import com.alibaba.fastffi.FFIPointer; | ||
import com.alibaba.fastffi.FFITypeAlias; | ||
|
||
@FFIGen | ||
@FFITypeAlias(ARROW_RESULT) | ||
@CXXHead(ARROW_API_H) | ||
public interface ArrowResult<T> extends FFIPointer { | ||
@CXXReference | ||
T ValueOrDie(); | ||
|
||
@CXXReference | ||
ArrowStatus status(); | ||
|
||
boolean ok(); | ||
} |
33 changes: 33 additions & 0 deletions
33
java/src/main/java/com/alibaba/graphar/arrow/ArrowStatus.java
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,33 @@ | ||
/* | ||
* Copyright 2022 Alibaba Group Holding Limited. | ||
* | ||
* Licensed 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. | ||
*/ | ||
|
||
package com.alibaba.graphar.arrow; | ||
|
||
import static com.alibaba.graphar.util.CppClassName.ARROW_STATUS; | ||
import static com.alibaba.graphar.util.CppHeaderName.ARROW_API_H; | ||
|
||
import com.alibaba.fastffi.CXXHead; | ||
import com.alibaba.fastffi.CXXReference; | ||
import com.alibaba.fastffi.FFIGen; | ||
import com.alibaba.fastffi.FFIPointer; | ||
import com.alibaba.fastffi.FFITypeAlias; | ||
import com.alibaba.graphar.stdcxx.StdString; | ||
|
||
@FFIGen | ||
@FFITypeAlias(ARROW_STATUS) | ||
@CXXHead(ARROW_API_H) | ||
public interface ArrowStatus extends FFIPointer { | ||
@CXXReference | ||
StdString message(); | ||
} |
88 changes: 88 additions & 0 deletions
88
java/src/main/java/com/alibaba/graphar/arrow/ArrowTable.java
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,88 @@ | ||
/* | ||
* Copyright 2022 Alibaba Group Holding Limited. | ||
* | ||
* Licensed 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. | ||
*/ | ||
|
||
package com.alibaba.graphar.arrow; | ||
|
||
import static com.alibaba.graphar.util.CppClassName.ARROW_TABLE; | ||
import static com.alibaba.graphar.util.CppHeaderName.ARROW_API_H; | ||
|
||
import com.alibaba.fastffi.CXXHead; | ||
import com.alibaba.fastffi.CXXPointer; | ||
import com.alibaba.fastffi.CXXValue; | ||
import com.alibaba.fastffi.FFIGen; | ||
import com.alibaba.fastffi.FFILibrary; | ||
import com.alibaba.fastffi.FFITypeAlias; | ||
import com.alibaba.fastffi.FFITypeFactory; | ||
import com.alibaba.graphar.stdcxx.StdSharedPtr; | ||
import com.alibaba.graphar.stdcxx.StdString; | ||
import org.apache.arrow.c.ArrowArray; | ||
import org.apache.arrow.c.ArrowSchema; | ||
import org.apache.arrow.c.Data; | ||
import org.apache.arrow.memory.BufferAllocator; | ||
import org.apache.arrow.vector.VectorSchemaRoot; | ||
import org.apache.arrow.vector.dictionary.DictionaryProvider; | ||
|
||
@FFIGen | ||
@FFITypeAlias(ARROW_TABLE) | ||
@CXXHead(ARROW_API_H) | ||
public interface ArrowTable extends CXXPointer { | ||
|
||
/** | ||
* Convert VectorSchemaRoot to C++ arrow::Table | ||
* | ||
* @param allocator Buffer allocator for allocating C data interface fields | ||
* @param vsr Vector schema root to export | ||
* @param provider Dictionary provider for dictionary encoded vectors (optional) | ||
* @return StdSharedPtr<ArrowTable> | ||
*/ | ||
static StdSharedPtr<ArrowTable> fromVectorSchemaRoot( | ||
BufferAllocator allocator, VectorSchemaRoot vsr, DictionaryProvider provider) { | ||
ArrowResult<StdSharedPtr<ArrowTable>> maybeTable = null; | ||
org.apache.arrow.c.ArrowArray arrowArray = ArrowArray.allocateNew(allocator); | ||
org.apache.arrow.c.ArrowSchema arrowSchema = ArrowSchema.allocateNew(allocator); | ||
Data.exportVectorSchemaRoot(allocator, vsr, provider, arrowArray, arrowSchema); | ||
maybeTable = | ||
Static.INSTANCE.fromArrowArrayAndArrowSchema( | ||
arrowArray.memoryAddress(), arrowSchema.memoryAddress()); | ||
if (!maybeTable.ok()) { | ||
throw new RuntimeException( | ||
"Error when convert C RecordBatch to C++ Table: " | ||
+ maybeTable.status().message().toJavaString()); | ||
} | ||
return maybeTable.ValueOrDie(); | ||
} | ||
|
||
long num_rows(); | ||
|
||
@CXXValue | ||
StdString ToString(); | ||
|
||
@FFIGen | ||
@FFILibrary(value = "arrow", namespace = "arrow") | ||
interface Static { | ||
Static INSTANCE = FFITypeFactory.getLibrary(ArrowTable.Static.class); | ||
|
||
/** | ||
* Convert C ArrowArray and ArrowSchema to C++ arrow::Table with JNI wrote manually | ||
* | ||
* @param arrayAddress Address of C ArrowArray | ||
* @param schemaAddress Address of C ArrowSchema | ||
* @return StdSharedPtr<ArrowTable> wrapped by ArrowResult | ||
*/ | ||
@CXXValue | ||
ArrowResult<StdSharedPtr<ArrowTable>> fromArrowArrayAndArrowSchema( | ||
@FFITypeAlias("struct ArrowArray*") long arrayAddress, | ||
@FFITypeAlias("struct ArrowSchema*") long schemaAddress); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
java/src/main/java/com/alibaba/graphar/arrow/StructArrowArray.java
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,38 @@ | ||
/* | ||
* Copyright 2022 Alibaba Group Holding Limited. | ||
* | ||
* Licensed 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. | ||
*/ | ||
|
||
package com.alibaba.graphar.arrow; | ||
|
||
import static com.alibaba.graphar.util.CppHeaderName.ARROW_API_H; | ||
import static com.alibaba.graphar.util.CppHeaderName.ARROW_C_BRIDGE_H; | ||
|
||
import com.alibaba.fastffi.CXXHead; | ||
import com.alibaba.fastffi.FFIFactory; | ||
import com.alibaba.fastffi.FFIGen; | ||
import com.alibaba.fastffi.FFIPointer; | ||
import com.alibaba.fastffi.FFITypeAlias; | ||
import com.alibaba.fastffi.FFITypeFactory; | ||
|
||
@FFIGen | ||
@CXXHead(ARROW_C_BRIDGE_H) | ||
@CXXHead(ARROW_API_H) | ||
@FFITypeAlias("struct ArrowArray") | ||
public interface StructArrowArray extends FFIPointer { | ||
Factory factory = FFITypeFactory.getFactory(StructArrowArray.class); | ||
|
||
@FFIFactory | ||
interface Factory { | ||
StructArrowArray create(); | ||
} | ||
} |
Oops, something went wrong.