danepitkin commented on code in PR #34227:
URL: https://github.com/apache/arrow/pull/34227#discussion_r1158994121
##########
java/dataset/src/main/cpp/jni_wrapper.cc:
##########
@@ -261,6 +264,37 @@ void JNI_OnUnload(JavaVM* vm, void* reserved) {
default_memory_pool_id = -1L;
}
+/// Unpack the named tables passed through JNI.
+///
+/// Named tables are encoded as a string array, where every two elements
+/// encode (1) the table name and (2) the address of an ArrowArrayStream
+/// containing the table data. This function will eagerly read all
+/// tables into Tables.
+std::unordered_map<std::string, std::shared_ptr<arrow::Table>>
LoadNamedTables(JNIEnv* env, jobjectArray& str_array) {
+ std::unordered_map<std::string, std::shared_ptr<arrow::Table>>
map_table_to_record_batch_reader;
+ int length = env->GetArrayLength(str_array);
+ if (length % 2 != 0) {
+ JniThrow("Review mapping of Table names to memory addresses");
Review Comment:
Optional: Can we provide a more helpful Error message? Example:
`Can not map odd number of array elements to key/value pairs`
##########
java/dataset/src/main/cpp/jni_wrapper.cc:
##########
@@ -578,3 +581,100 @@
Java_org_apache_arrow_dataset_file_JniWrapper_writeFromScannerToFile(
JniAssertOkOrThrow(arrow::dataset::FileSystemDataset::Write(options,
scanner));
JNI_METHOD_END()
}
+
+/*
+ * Class: org_apache_arrow_dataset_substrait_JniWrapper
+ * Method: executeSerializedPlanLocalFiles
+ * Signature: (Ljava/lang/String;J)V
+ */
+JNIEXPORT void JNICALL
Java_org_apache_arrow_dataset_substrait_JniWrapper_executeSerializedPlanLocalFiles
(
+ JNIEnv* env, jobject, jstring substrait_plan, jlong
c_arrow_array_stream_address_out) {
+ JNI_METHOD_START
+ auto* arrow_stream =
reinterpret_cast<ArrowArrayStream*>(c_arrow_array_stream_address_out);
+ std::shared_ptr<arrow::Buffer> buffer =
JniGetOrThrow(arrow::engine::SerializeJsonPlan(JStringToCString(env,
substrait_plan)));
+ std::shared_ptr<arrow::RecordBatchReader> reader =
JniGetOrThrow(arrow::engine::ExecuteSerializedPlan(*buffer));
+ JniAssertOkOrThrow(arrow::ExportRecordBatchReader(reader, arrow_stream));
+ JNI_METHOD_END()
+}
+
+/*
+ * Class: org_apache_arrow_dataset_substrait_JniWrapper
+ * Method: callMeRN
+ * Signature: (Ljava/lang/String;[Ljava/lang/String;J)V
+ */
+JNIEXPORT void JNICALL
Java_org_apache_arrow_dataset_substrait_JniWrapper_executeSerializedPlanNamedTables__Ljava_lang_String_2_3Ljava_lang_String_2J
(
+ JNIEnv* env, jobject, jstring plan, jobjectArray
table_to_memory_address_input, jlong memory_address_output) {
+ JNI_METHOD_START
+ // get mapping of table name to memory address
+ std::map<std::string, long> map_table_to_memory_address = ToMap(env,
table_to_memory_address_input);
+ // create table provider
+ arrow::engine::NamedTableProvider table_provider =
[&map_table_to_memory_address](const std::vector<std::string>& names, const
arrow::Schema&) {
+ std::shared_ptr<arrow::Table> output_table;
+ for (const auto& name : names) {
+ // get table from memory address provided
+ long memory_address = map_table_to_memory_address[name];
+ auto* arrow_stream_in =
reinterpret_cast<ArrowArrayStream*>(memory_address);
+ std::shared_ptr<arrow::RecordBatchReader> readerIn =
+ JniGetOrThrow(arrow::ImportRecordBatchReader(arrow_stream_in));
+ std::shared_ptr<arrow::dataset::ScannerBuilder> scanner_builder =
+ arrow::dataset::ScannerBuilder::FromRecordBatchReader(readerIn);
+ JniAssertOkOrThrow(scanner_builder->Pool(arrow::default_memory_pool()));
+ auto scanner = JniGetOrThrow(scanner_builder->Finish());
+ output_table = JniGetOrThrow(scanner->ToTable());
+ }
+ std::shared_ptr<arrow::compute::ExecNodeOptions> options =
+
std::make_shared<arrow::compute::TableSourceNodeOptions>(std::move(output_table));
+ return arrow::compute::Declaration("table_source", {}, options,
"java_source");
+ };
+ arrow::engine::ConversionOptions conversion_options;
+ conversion_options.named_table_provider = std::move(table_provider);
+ // execute plan
+ std::shared_ptr<arrow::Buffer> buffer =
JniGetOrThrow(arrow::engine::SerializeJsonPlan(JStringToCString(env, plan)));
+ std::shared_ptr<arrow::RecordBatchReader> readerOut =
JniGetOrThrow(arrow::engine::ExecuteSerializedPlan(*buffer, NULLPTR, NULLPTR,
conversion_options));
+ auto* arrow_stream_out =
reinterpret_cast<ArrowArrayStream*>(memory_address_output);
+ JniAssertOkOrThrow(arrow::ExportRecordBatchReader(readerOut,
arrow_stream_out));
+ JNI_METHOD_END()
+}
+
+/*
+ * Class: org_apache_arrow_dataset_substrait_JniWrapper
+ * Method: callMeRN
+ * Signature: (Ljava/lang/Object;[Ljava/lang/String;J)V
+ */
+JNIEXPORT void JNICALL
Java_org_apache_arrow_dataset_substrait_JniWrapper_executeSerializedPlanNamedTables__Ljava_lang_Object_2_3Ljava_lang_String_2J
(
+ JNIEnv* env, jobject, jobject plan, jobjectArray
table_to_memory_address_input, jlong memory_address_output) {
+ JNI_METHOD_START
+ // get mapping of table name to memory address
+ std::map<std::string, long> map_table_to_memory_address = ToMap(env,
table_to_memory_address_input);
+ // create table provider
+ arrow::engine::NamedTableProvider table_provider =
[&map_table_to_memory_address](const std::vector<std::string>& names, const
arrow::Schema&) {
+ std::shared_ptr<arrow::Table> output_table;
+ for (const auto& name : names) {
Review Comment:
I agree we should not duplicate this code. Let's create a separate function
instead.
##########
java/dataset/src/main/cpp/jni_wrapper.cc:
##########
@@ -261,6 +264,37 @@ void JNI_OnUnload(JavaVM* vm, void* reserved) {
default_memory_pool_id = -1L;
}
+/// Unpack the named tables passed through JNI.
+///
+/// Named tables are encoded as a string array, where every two elements
+/// encode (1) the table name and (2) the address of an ArrowArrayStream
+/// containing the table data. This function will eagerly read all
+/// tables into Tables.
+std::unordered_map<std::string, std::shared_ptr<arrow::Table>>
LoadNamedTables(JNIEnv* env, jobjectArray& str_array) {
+ std::unordered_map<std::string, std::shared_ptr<arrow::Table>>
map_table_to_record_batch_reader;
+ int length = env->GetArrayLength(str_array);
+ if (length % 2 != 0) {
+ JniThrow("Review mapping of Table names to memory addresses");
+ }
+ std::shared_ptr<arrow::Table> output_table;
+ for (int pos = 0; pos < length; pos++) {
+ auto j_string_key =
reinterpret_cast<jstring>(env->GetObjectArrayElement(str_array, pos));
+ pos++;
+ auto j_string_value =
reinterpret_cast<jstring>(env->GetObjectArrayElement(str_array, pos));
+ long memory_address = 0;
+ try {
+ memory_address = std::stol(JStringToCString(env, j_string_value));
+ } catch (...) {
+ JniThrow("Error to interpret an string address as a signed number");
Review Comment:
Optional: Update error string to be: `Failed to parse memory address from
string value`
##########
java/dataset/src/main/java/org/apache/arrow/dataset/substrait/AceroSubstraitConsumer.java:
##########
@@ -0,0 +1,155 @@
+/*
+ * 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.
+ */
+
+package org.apache.arrow.dataset.substrait;
+
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.arrow.c.ArrowArrayStream;
+import org.apache.arrow.c.Data;
+import org.apache.arrow.memory.BufferAllocator;
+import org.apache.arrow.util.AutoCloseables;
+import org.apache.arrow.vector.ipc.ArrowReader;
+
+/**
+ * Class to expose Java Substrait API for end users, currently operations
supported are only to Consume Substrait Plan
+ * in Plan format (JSON) or Binary format (ByteBuffer).
+ */
+public final class AceroSubstraitConsumer {
+ private final BufferAllocator allocator;
+
+ public AceroSubstraitConsumer(BufferAllocator allocator) {
+ this.allocator = allocator;
+ }
+
+ /**
+ * Read plain-text Substrait plan, execute and return an ArrowReader to read
Schema and ArrowRecordBatches.
+ * Needed to define a mapping name of Tables and theirs ArrowReader
representation.
+ *
+ * @param plan The JSON Substrait plan.
+
+ * @return the ArrowReader to iterate for record batches.
+ */
+ public ArrowReader runQuery(String plan) throws Exception {
+ return runQuery(plan, Collections.emptyMap());
+ }
+
+ /**
+ * Read plain-text Substrait plan, execute and return an ArrowReader to read
Schema and ArrowRecordBatches.
+ * Needed to define a mapping name of Tables and theirs ArrowReader
representation.
+ *
+ * @param plan The JSON Substrait plan.
+ * @param namedTables A mapping of named tables referenced by the plan to an
ArrowReader providing the data
+ * for the table. Contains the Table Name to Query as a
Key and ArrowReader as a Value.
+ * <pre>{@code ArrowReader nationReader = scanner.scanBatches();
+ * Map<String, ArrowReader> namedTables = new HashMap<>();
+ * namedTables.put("NATION", nationReader);}</pre>
+ * @return the ArrowReader to iterate for record batches.
+ */
+ public ArrowReader runQuery(String plan, Map<String, ArrowReader>
namedTables) throws Exception {
+ return execute(plan, namedTables);
+ }
+
+ /**
+ * Read binary Substrait plan, execute and return an ArrowReader to read
Schema and ArrowRecordBatches.
+ * Needed to define a mapping name of Tables and theirs ArrowReader
representation.
+ *
+ * @param plan the binary Substrait plan.
+ * @return the ArrowReader to iterate for record batches.
+ */
+ public ArrowReader runQuery(ByteBuffer plan) throws Exception {
+ return runQuery(plan, Collections.EMPTY_MAP);
Review Comment:
Can we use `Collections.emptyMap()` like in `public ArrowReader
runQuery(String plan)`?
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]