Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ARROW-17297: [Java][Doc] Adding documentation to interact between C++ to Java via C Data Interface #13788

Merged
merged 8 commits into from
Aug 5, 2022

Conversation

davisusanibar
Copy link
Contributor

Adding documentation to interact between C++ to Java via C Data Interface for:

  • ImportArray
  • ImportRecordBatch

@lidavidm
Copy link
Member

lidavidm commented Aug 3, 2022

Can you create a JIRA? This isn't minor

@@ -33,7 +33,10 @@ Python communication using the C Data Interface.
Java to C++
-----------

Example: Share an Int64 array from C++ to Java:
Share an Int64 array from C++ to Java
=====================================
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the wrong level of header

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

docs/source/java/cdata.rst Outdated Show resolved Hide resolved
docs/source/java/cdata.rst Outdated Show resolved Hide resolved
docs/source/java/cdata.rst Outdated Show resolved Hide resolved
return root;
}

public static void main(String[] args) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need a main method, do we?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleted

docs/source/java/cdata.rst Outdated Show resolved Hide resolved
docs/source/java/cdata.rst Outdated Show resolved Hide resolved
docs/source/java/cdata.rst Outdated Show resolved Hide resolved
docs/source/java/cdata.rst Outdated Show resolved Hide resolved
docs/source/java/cdata.rst Outdated Show resolved Hide resolved
@davisusanibar davisusanibar changed the title MINOR: [Java] Adding documentation to interact between C++ to Java via C Data Interface ARROW-17297: [Java][Doc] Adding documentation to interact between C++ to Java via C Data Interface Aug 3, 2022
@github-actions
Copy link

github-actions bot commented Aug 3, 2022

@github-actions
Copy link

github-actions bot commented Aug 3, 2022

⚠️ Ticket has not been started in JIRA, please click 'Start Progress'.


.. code-block:: java

import org.apache.arrow.c.ArrowArray;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, but everything is indented one space too many here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's actually true of all the code blocks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed

int status = JNI_CreateJavaVM(jvm, (void **) &env, &vm_args);
if (status < 0) {
std::cout << "\n<<<<< Unable to Launch JVM >>>>>\n" << std::endl;
std::abort();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requires #include <cstdlib>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I suppose the abort isn't necessary, maybe just explicitly return nullptr and it'll exit in main()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed

jclass javaClassToBeCalledByCpp = NULL;
javaClassToBeCalledByCpp = env->FindClass("ToBeCalledByCpp");
if (javaClassToBeCalledByCpp != NULL) {
jmethodID fillVector = NULL;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the other uses of NULL to be nullptr

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though, why separate the declaration and assignment? Just do it in one line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

find_package(JNI REQUIRED)
find_package(Arrow REQUIRED)
message(STATUS "Arrow version: ${ARROW_VERSION}")
message(${ARROW_FULL_SO_VERSION})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be updated like the line above

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Message not needed, deleted

JNIEnv *env;
JavaVM *jvm;
env = CreateVM(&jvm);
if (env == nullptr) return 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use EXIT_FAILURE here too as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

struct ArrowSchema arrowSchema;
struct ArrowArray arrowArray;
std::cout << "\n<<<<< C++ to Java for Arrays >>>>>\n" << std::endl;
env->CallStaticVoidMethod(javaClassToBeCalledByCpp, fillVector, reinterpret_cast<uintptr_t>(&arrowSchema),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would wrap lines to be a little shorter here if possible to make sure it's readable in Sphinx

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

docs/source/java/cdata.rst Outdated Show resolved Hide resolved
docs/source/java/cdata.rst Outdated Show resolved Hide resolved
docs/source/java/cdata.rst Outdated Show resolved Hide resolved
docs/source/java/cdata.rst Outdated Show resolved Hide resolved
Comment on lines 242 to 243
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to use Java 11 here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleted

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<arrow.version>8.0.0</arrow.version>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update to 9.0.0?

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<arrow.version>8.0.0</arrow.version>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might as well update to 9.0.0 now

<repositories>
<repository>
<id>arrow-nightly</id>
<url>https://nightlies.apache.org/arrow/java</url>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to use nightlies right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleted

<archive>
<manifest>
<mainClass>
ToBeCalledByCpp
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the mainClass config?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleted

struct ArrowArray arrowArray;
std::cout << "\n<<<<< C++ to Java for RecordBatch >>>>>\n" << std::endl;
env->CallStaticVoidMethod(javaClassToBeCalledByCpp, fillVectorSchemaRoot,
static_cast<long>(reinterpret_cast<uintptr_t>(&arrowSchema)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need the static_cast<long> above as well then?

(Also, shouldn't it be jlong?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of these kind of declaration work well (I don't know what of this should I use?)

        env->CallStaticVoidMethod(javaClassToBeCalledByCpp, fillVector,
                                  static_cast<long>(reinterpret_cast<uintptr_t>(&arrowSchema)),
                                  static_cast<long>(reinterpret_cast<uintptr_t>(&arrowArray)));
        env->CallStaticVoidMethod(javaClassToBeCalledByCpp, fillVector,
                                  reinterpret_cast<uintptr_t>(&arrowSchema),
                                  reinterpret_cast<uintptr_t>(&arrowArray));
        env->CallStaticVoidMethod(javaClassToBeCalledByCpp, fillVector,
                                  (long)(&arrowSchema),
                                  (long)(&arrowArray));
        env->CallStaticVoidMethod(javaClassToBeCalledByCpp, fillVector,
                                  (jlong)(&arrowSchema),
                                  (jlong)(&arrowArray));
        env->CallStaticVoidMethod(javaClassToBeCalledByCpp, fillVector,
                                  (uintptr_t)(&arrowSchema),
                                  (uintptr_t)(&arrowArray));

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be the most compliant, I think it should be:

        env->CallStaticVoidMethod(javaClassToBeCalledByCpp, fillVector,
                                  static_cast<jlong>(reinterpret_cast<uintptr_t>(&arrowSchema)),
                                  static_cast<jlong>(reinterpret_cast<uintptr_t>(&arrowArray)));

(note jlong not long)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

docs/source/java/cdata.rst Outdated Show resolved Hide resolved
docs/source/java/cdata.rst Outdated Show resolved Hide resolved
docs/source/java/cdata.rst Outdated Show resolved Hide resolved
struct ArrowArray arrowArray;
std::cout << "\n<<<<< C++ to Java for RecordBatch >>>>>\n" << std::endl;
env->CallStaticVoidMethod(javaClassToBeCalledByCpp, fillVectorSchemaRoot,
static_cast<long>(reinterpret_cast<uintptr_t>(&arrowSchema)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be the most compliant, I think it should be:

        env->CallStaticVoidMethod(javaClassToBeCalledByCpp, fillVector,
                                  static_cast<jlong>(reinterpret_cast<uintptr_t>(&arrowSchema)),
                                  static_cast<jlong>(reinterpret_cast<uintptr_t>(&arrowArray)));

(note jlong not long)

docs/source/java/cdata.rst Outdated Show resolved Hide resolved
docs/source/java/cdata.rst Outdated Show resolved Hide resolved
docs/source/java/cdata.rst Outdated Show resolved Hide resolved
}
}

public static FieldVector populateFieldVectorToExport(){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't you make this private?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed

}
}

public static VectorSchemaRoot populateVectorSchemaRootToExport(){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed

public class ToBeCalledByCpp {
final static BufferAllocator allocator = new RootAllocator();

public static void fillVector(long schemaAddress, long arrayAddress){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add small docstring describing the two public methods?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

docs/source/java/cdata.rst Outdated Show resolved Hide resolved
docs/source/java/cdata.rst Outdated Show resolved Hide resolved
@lidavidm
Copy link
Member

lidavidm commented Aug 5, 2022

Lint issues are addressed by #13809

@lidavidm lidavidm merged commit d26489c into apache:master Aug 5, 2022
@ursabot
Copy link

ursabot commented Aug 6, 2022

Benchmark runs are scheduled for baseline = bc7de40 and contender = d26489c. d26489c is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
Conbench compare runs links:
[Finished ⬇️0.0% ⬆️0.0%] ec2-t3-xlarge-us-east-2
[Finished ⬇️0.65% ⬆️0.03%] test-mac-arm
[Finished ⬇️0.27% ⬆️0.0%] ursa-i9-9960x
[Finished ⬇️0.43% ⬆️0.07%] ursa-thinkcentre-m75q
Buildkite builds:
[Finished] d26489c3 ec2-t3-xlarge-us-east-2
[Finished] d26489c3 test-mac-arm
[Finished] d26489c3 ursa-i9-9960x
[Finished] d26489c3 ursa-thinkcentre-m75q
[Finished] bc7de406 ec2-t3-xlarge-us-east-2
[Finished] bc7de406 test-mac-arm
[Finished] bc7de406 ursa-i9-9960x
[Finished] bc7de406 ursa-thinkcentre-m75q
Supported benchmarks:
ec2-t3-xlarge-us-east-2: Supported benchmark langs: Python, R. Runs only benchmarks with cloud = True
test-mac-arm: Supported benchmark langs: C++, Python, R
ursa-i9-9960x: Supported benchmark langs: Python, R, JavaScript
ursa-thinkcentre-m75q: Supported benchmark langs: C++, Java

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants