lidavidm commented on code in PR #14151:
URL: https://github.com/apache/arrow/pull/14151#discussion_r1000600324


##########
java/dataset/src/main/cpp/jni_wrapper.cc:
##########
@@ -170,6 +172,21 @@ class DisposableScannerAdaptor {
   }
 };
 
+arrow::Result<std::shared_ptr<arrow::Schema>> SchemaFromColumnNames(
+    const std::shared_ptr<arrow::Schema>& input,
+    const std::vector<std::string>& column_names) {
+  std::vector<std::shared_ptr<arrow::Field>> columns;
+  for (arrow::FieldRef ref : column_names) {
+    auto maybe_field = ref.GetOne(*input);
+    if (maybe_field.ok()) {
+      columns.push_back(std::move(maybe_field).ValueOrDie());
+    } else {
+      return arrow::Status::Invalid("The provided column name is not in arrow 
schema");

Review Comment:
   ```suggestion
         return arrow::Status::Invalid("Partition column '", ref.ToString(), "' 
is not in dataset schema");
   ```



##########
java/dataset/src/main/cpp/jni_wrapper.cc:
##########
@@ -510,3 +526,49 @@ 
Java_org_apache_arrow_dataset_file_JniWrapper_makeFileSystemDatasetFactory(
   return CreateNativeRef(d);
   JNI_METHOD_END(-1L)
 }
+
+/*
+ * Class:     org_apache_arrow_dataset_file_JniWrapper
+ * Method:    writeFromScannerToFile
+ * Signature:
+ * (JJJLjava/lang/String;[Ljava/lang/String;ILjava/lang/String;)V
+ */
+JNIEXPORT void JNICALL
+Java_org_apache_arrow_dataset_file_JniWrapper_writeFromScannerToFile(
+    JNIEnv* env, jobject, jlong c_arrow_array_stream_address,
+    jlong file_format_id, jstring uri, jobjectArray partition_columns,
+    jint max_partitions, jstring base_name_template) {
+  JNI_METHOD_START
+  JavaVM* vm;
+  if (env->GetJavaVM(&vm) != JNI_OK) {
+    JniThrow("Unable to get JavaVM instance");
+  }
+
+  auto* arrow_stream = 
reinterpret_cast<ArrowArrayStream*>(c_arrow_array_stream_address);
+  std::shared_ptr<arrow::RecordBatchReader> reader =
+      JniGetOrThrow(arrow::ImportRecordBatchReader(arrow_stream));
+  std::shared_ptr<arrow::dataset::ScannerBuilder> scanner_builder =
+      arrow::dataset::ScannerBuilder::FromRecordBatchReader(reader);
+  scanner_builder->Pool(arrow::default_memory_pool());
+  auto scanner = scanner_builder->Finish().ValueOrDie();

Review Comment:
   We must check for error here



##########
java/dataset/src/main/cpp/jni_wrapper.cc:
##########
@@ -510,3 +526,49 @@ 
Java_org_apache_arrow_dataset_file_JniWrapper_makeFileSystemDatasetFactory(
   return CreateNativeRef(d);
   JNI_METHOD_END(-1L)
 }
+
+/*
+ * Class:     org_apache_arrow_dataset_file_JniWrapper
+ * Method:    writeFromScannerToFile
+ * Signature:
+ * (JJJLjava/lang/String;[Ljava/lang/String;ILjava/lang/String;)V
+ */
+JNIEXPORT void JNICALL
+Java_org_apache_arrow_dataset_file_JniWrapper_writeFromScannerToFile(
+    JNIEnv* env, jobject, jlong c_arrow_array_stream_address,
+    jlong file_format_id, jstring uri, jobjectArray partition_columns,
+    jint max_partitions, jstring base_name_template) {
+  JNI_METHOD_START
+  JavaVM* vm;
+  if (env->GetJavaVM(&vm) != JNI_OK) {
+    JniThrow("Unable to get JavaVM instance");
+  }
+
+  auto* arrow_stream = 
reinterpret_cast<ArrowArrayStream*>(c_arrow_array_stream_address);
+  std::shared_ptr<arrow::RecordBatchReader> reader =
+      JniGetOrThrow(arrow::ImportRecordBatchReader(arrow_stream));
+  std::shared_ptr<arrow::dataset::ScannerBuilder> scanner_builder =
+      arrow::dataset::ScannerBuilder::FromRecordBatchReader(reader);
+  scanner_builder->Pool(arrow::default_memory_pool());

Review Comment:
   We must check for error here



##########
java/dataset/src/main/java/org/apache/arrow/dataset/file/DatasetFileWriter.java:
##########
@@ -0,0 +1,69 @@
+/*
+ * 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.file;
+
+import org.apache.arrow.c.ArrowArrayStream;
+import org.apache.arrow.c.ArrowSchema;
+import org.apache.arrow.c.Data;
+import org.apache.arrow.dataset.scanner.ArrowScannerReader;
+import org.apache.arrow.dataset.scanner.ScanTask;
+import org.apache.arrow.dataset.scanner.Scanner;
+import org.apache.arrow.memory.BufferAllocator;
+import org.apache.arrow.util.AutoCloseables;
+import org.apache.arrow.vector.ipc.ArrowReader;
+import org.apache.arrow.vector.util.SchemaUtility;
+
+import java.util.Iterator;
+
+/**
+ * JNI-based utility to write datasets into files. It internally depends on 
C++ static method
+ * FileSystemDataset::Write.
+ */
+public class DatasetFileWriter {
+
+  /**
+   * Write a ArrowReader accepting Scanner into files.

Review Comment:
   The docstring is still incorrect



##########
java/dataset/src/main/java/org/apache/arrow/dataset/scanner/ArrowScannerReader.java:
##########
@@ -0,0 +1,106 @@
+/*
+ * 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.scanner;
+
+import org.apache.arrow.memory.BufferAllocator;
+import org.apache.arrow.vector.VectorLoader;
+import org.apache.arrow.vector.VectorUnloader;
+import org.apache.arrow.vector.ipc.ArrowReader;
+import org.apache.arrow.vector.ipc.message.ArrowDictionaryBatch;
+import org.apache.arrow.vector.ipc.message.ArrowRecordBatch;
+import org.apache.arrow.vector.types.pojo.Schema;
+
+import java.io.IOException;
+import java.util.Iterator;
+
+public class ArrowScannerReader extends ArrowReader {
+  private final Scanner scanner;
+
+  private Iterator<? extends ScanTask> taskIterator;
+
+  private ScanTask currentTask = null;
+  private ArrowReader currentReader = null;
+
+  public ArrowScannerReader(Scanner scanner, BufferAllocator allocator) {
+    super(allocator);
+    this.scanner = scanner;
+    this.taskIterator = scanner.scan().iterator();
+    if (taskIterator.hasNext()) {
+      currentTask = taskIterator.next();
+      currentReader = currentTask.execute();
+    }
+  }
+
+  @Override
+  protected void loadRecordBatch(ArrowRecordBatch batch) {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  protected void loadDictionary(ArrowDictionaryBatch dictionaryBatch) {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  public boolean loadNextBatch() throws IOException {
+    if (currentReader == null) return false;
+    boolean result = currentReader.loadNextBatch();
+
+    if (!result) {
+      try {
+        currentTask.close();
+        currentReader.close();
+      } catch (Exception e) {
+        throw new IOException(e);
+      }
+
+      while (!result) {
+        if (!taskIterator.hasNext()) {
+          return false;
+        } else {
+          currentTask = taskIterator.next();
+          currentReader = currentTask.execute();
+          result = currentReader.loadNextBatch();
+        }
+      }
+    }
+
+    VectorLoader loader = new VectorLoader(this.getVectorSchemaRoot());
+    VectorUnloader unloader =
+        new VectorUnloader(currentReader.getVectorSchemaRoot());
+    try(ArrowRecordBatch recordBatch = unloader.getRecordBatch()) {
+      loader.load(recordBatch);
+    }
+    return true;
+  }
+
+  @Override
+  public long bytesRead() {
+    return 0L;
+  }
+
+  @Override
+  protected void closeReadSource() throws IOException {
+    // no-op

Review Comment:
   We need to close resources here



##########
java/dataset/src/main/java/org/apache/arrow/dataset/file/DatasetFileWriter.java:
##########
@@ -0,0 +1,69 @@
+/*
+ * 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.file;
+
+import org.apache.arrow.c.ArrowArrayStream;
+import org.apache.arrow.c.ArrowSchema;
+import org.apache.arrow.c.Data;
+import org.apache.arrow.dataset.scanner.ArrowScannerReader;
+import org.apache.arrow.dataset.scanner.ScanTask;
+import org.apache.arrow.dataset.scanner.Scanner;
+import org.apache.arrow.memory.BufferAllocator;
+import org.apache.arrow.util.AutoCloseables;
+import org.apache.arrow.vector.ipc.ArrowReader;
+import org.apache.arrow.vector.util.SchemaUtility;
+
+import java.util.Iterator;
+
+/**
+ * JNI-based utility to write datasets into files. It internally depends on 
C++ static method
+ * FileSystemDataset::Write.
+ */
+public class DatasetFileWriter {
+
+  /**
+   * Write a ArrowReader accepting Scanner into files.

Review Comment:
   ```suggestion
      * Write the contents of an ArrowReader as a dataset.
   ```



-- 
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]

Reply via email to