-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathjni_wrapper.cc
More file actions
1358 lines (1231 loc) · 50.3 KB
/
jni_wrapper.cc
File metadata and controls
1358 lines (1231 loc) · 50.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 <mutex>
#include <utility>
#include <unordered_map>
#include "arrow/array.h"
#include "arrow/array/concatenate.h"
#include "arrow/c/bridge.h"
#include "arrow/c/helpers.h"
#include "arrow/compute/initialize.h"
#include "arrow/dataset/api.h"
#include "arrow/dataset/file_base.h"
#include "arrow/dataset/file_parquet.h"
#ifdef ARROW_CSV
#include "arrow/dataset/file_csv.h"
#endif
#include "arrow/filesystem/api.h"
#include "arrow/filesystem/path_util.h"
#include "arrow/engine/substrait/util.h"
#include "arrow/engine/substrait/serde.h"
#include "arrow/engine/substrait/relation.h"
#include "arrow/ipc/api.h"
#include "arrow/util/iterator.h"
#include "arrow/util/compression.h"
#include "parquet/arrow/writer.h"
#include "parquet/file_writer.h"
#include "parquet/stream_writer.h"
#include "arrow/io/file.h"
#include "jni_util.h"
#include "org_apache_arrow_dataset_file_JniWrapper.h"
#include "org_apache_arrow_dataset_jni_JniWrapper.h"
#include "org_apache_arrow_dataset_jni_NativeMemoryPool.h"
#include "org_apache_arrow_dataset_substrait_JniWrapper.h"
namespace {
jclass illegal_access_exception_class;
jclass illegal_argument_exception_class;
jclass runtime_exception_class;
jclass java_reservation_listener_class;
jmethodID reserve_memory_method;
jmethodID unreserve_memory_method;
jlong default_memory_pool_id = -1L;
jint JNI_VERSION = JNI_VERSION_10;
class JniPendingException : public std::runtime_error {
public:
explicit JniPendingException(const std::string& arg, jthrowable cause)
: runtime_error(arg), cause_(cause) {}
jthrowable GetCause() const { return cause_; }
bool HasCause() const { return cause_ != nullptr; }
private:
jthrowable cause_;
};
void ThrowPendingException(const std::string& message, jthrowable cause = nullptr) {
throw JniPendingException(message, cause);
}
void ThrowIfError(const arrow::Status& status) {
const std::shared_ptr<arrow::StatusDetail>& detail = status.detail();
const std::shared_ptr<const arrow::dataset::jni::JavaErrorDetail>& maybe_java =
std::dynamic_pointer_cast<const arrow::dataset::jni::JavaErrorDetail>(detail);
if (maybe_java != nullptr) {
ThrowPendingException(status.message(), maybe_java->GetCause());
return;
}
if (!status.ok()) {
ThrowPendingException(status.message());
}
}
class JNIEnvGuard {
public:
explicit JNIEnvGuard(JavaVM* vm) : vm_(vm), env_(nullptr), should_detach_(false) {
JNIEnv* env;
jint code = vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION);
if (code == JNI_EDETACHED) {
JavaVMAttachArgs args;
args.version = JNI_VERSION;
args.name = NULL;
args.group = NULL;
code = vm->AttachCurrentThread(reinterpret_cast<void**>(&env), &args);
should_detach_ = (code == JNI_OK);
}
if (code != JNI_OK) {
ThrowPendingException("Failed to attach the current thread to a Java VM");
}
env_ = env;
}
JNIEnv* env() { return env_; }
~JNIEnvGuard() {
if (should_detach_) {
vm_->DetachCurrentThread();
should_detach_ = false;
}
}
private:
JavaVM* vm_;
JNIEnv* env_;
bool should_detach_;
};
template <typename T>
T JniGetOrThrow(arrow::Result<T> result) {
const arrow::Status& status = result.status();
ThrowIfError(status);
return std::move(result).ValueOrDie();
}
void JniAssertOkOrThrow(arrow::Status status) { ThrowIfError(status); }
void JniThrow(std::string message) { ThrowPendingException(message); }
arrow::Result<std::shared_ptr<arrow::dataset::FileFormat>> GetFileFormat(
jint file_format_id) {
switch (file_format_id) {
case 0:
return std::make_shared<arrow::dataset::ParquetFileFormat>();
case 1:
return std::make_shared<arrow::dataset::IpcFileFormat>();
#ifdef ARROW_ORC
case 2:
return std::make_shared<arrow::dataset::OrcFileFormat>();
#endif
#ifdef ARROW_CSV
case 3:
return std::make_shared<arrow::dataset::CsvFileFormat>();
#endif
#ifdef ARROW_JSON
case 4:
return std::make_shared<arrow::dataset::JsonFileFormat>();
#endif
default:
std::string error_message =
"illegal file format id: " + std::to_string(file_format_id);
return arrow::Status::Invalid(error_message);
}
}
class ReserveFromJava : public arrow::dataset::jni::ReservationListener {
public:
ReserveFromJava(JavaVM* vm, jobject java_reservation_listener)
: vm_(vm), java_reservation_listener_(java_reservation_listener) {}
arrow::Status OnReservation(int64_t size) override {
try {
JNIEnvGuard guard(vm_);
JNIEnv* env = guard.env();
env->CallObjectMethod(java_reservation_listener_, reserve_memory_method, size);
RETURN_NOT_OK(arrow::dataset::jni::CheckException(env));
return arrow::Status::OK();
} catch (const JniPendingException& e) {
return arrow::Status::Invalid(e.what());
}
}
arrow::Status OnRelease(int64_t size) override {
try {
JNIEnvGuard guard(vm_);
JNIEnv* env = guard.env();
env->CallObjectMethod(java_reservation_listener_, unreserve_memory_method, size);
RETURN_NOT_OK(arrow::dataset::jni::CheckException(env));
return arrow::Status::OK();
} catch (const JniPendingException& e) {
return arrow::Status::Invalid(e.what());
}
}
jobject GetJavaReservationListener() { return java_reservation_listener_; }
private:
JavaVM* vm_;
jobject java_reservation_listener_;
};
/// \class DisposableScannerAdaptor
/// \brief An adaptor that iterates over a Scanner instance then returns RecordBatches
/// directly.
///
/// This lessens the complexity of the JNI bridge to make sure it to be easier to
/// maintain. On Java-side, NativeScanner can only produces a single NativeScanTask
/// instance during its whole lifecycle. Each task stands for a DisposableScannerAdaptor
/// instance through JNI bridge.
///
class DisposableScannerAdaptor {
public:
DisposableScannerAdaptor(std::shared_ptr<arrow::dataset::Scanner> scanner,
arrow::dataset::TaggedRecordBatchIterator batch_itr)
: scanner_(std::move(scanner)), batch_itr_(std::move(batch_itr)) {}
static arrow::Result<std::shared_ptr<DisposableScannerAdaptor>> Create(
std::shared_ptr<arrow::dataset::Scanner> scanner) {
ARROW_ASSIGN_OR_RAISE(auto batch_itr, scanner->ScanBatches());
return std::make_shared<DisposableScannerAdaptor>(scanner, std::move(batch_itr));
}
arrow::Result<std::shared_ptr<arrow::RecordBatch>> Next() {
ARROW_ASSIGN_OR_RAISE(std::shared_ptr<arrow::RecordBatch> batch, NextBatch());
return batch;
}
const std::shared_ptr<arrow::dataset::Scanner>& GetScanner() const { return scanner_; }
private:
std::shared_ptr<arrow::dataset::Scanner> scanner_;
arrow::dataset::TaggedRecordBatchIterator batch_itr_;
arrow::Result<std::shared_ptr<arrow::RecordBatch>> NextBatch() {
ARROW_ASSIGN_OR_RAISE(auto batch, batch_itr_.Next())
return batch.record_batch;
}
};
// Adapter to wrap Java OutputStream as Arrow OutputStream
class JavaOutputStreamAdapter : public arrow::io::OutputStream {
public:
JavaOutputStreamAdapter(JNIEnv* env, jobject java_output_stream)
: java_output_stream_(env->NewGlobalRef(java_output_stream)),
position_(0) {
JavaVM* vm;
env->GetJavaVM(&vm);
vm_ = vm;
// Get method IDs (cache them as they're valid for the lifetime of the class)
JNIEnv* current_env = GetEnv();
if (current_env) {
jclass output_stream_class = current_env->GetObjectClass(java_output_stream);
write_method_ = current_env->GetMethodID(output_stream_class, "write", "([BII)V");
flush_method_ = current_env->GetMethodID(output_stream_class, "flush", "()V");
close_method_ = current_env->GetMethodID(output_stream_class, "close", "()V");
current_env->DeleteLocalRef(output_stream_class);
}
}
~JavaOutputStreamAdapter() override {
JNIEnv* env = GetEnv();
if (env && java_output_stream_) {
env->DeleteGlobalRef(java_output_stream_);
}
}
arrow::Status Close() override {
JNIEnv* env = GetEnv();
if (!env) {
return arrow::Status::IOError("Failed to get JNI environment");
}
if (java_output_stream_) {
env->CallVoidMethod(java_output_stream_, close_method_);
RETURN_NOT_OK(CheckJniException(env));
env->DeleteGlobalRef(java_output_stream_);
java_output_stream_ = nullptr;
}
return arrow::Status::OK();
}
bool closed() const override { return java_output_stream_ == nullptr; }
arrow::Result<int64_t> Tell() const override { return position_; }
arrow::Status Write(const void* data, int64_t nbytes) override {
JNIEnv* env = GetEnv();
if (!env) {
return arrow::Status::IOError("Failed to get JNI environment");
}
if (!java_output_stream_) {
return arrow::Status::IOError("OutputStream is closed");
}
// Create byte array
jbyteArray byte_array = env->NewByteArray(static_cast<jsize>(nbytes));
if (byte_array == nullptr) {
return arrow::Status::OutOfMemory("Failed to allocate byte array");
}
// Copy data to byte array
env->SetByteArrayRegion(byte_array, 0, static_cast<jsize>(nbytes),
reinterpret_cast<const jbyte*>(data));
// Call Java write method
env->CallVoidMethod(java_output_stream_, write_method_, byte_array, 0,
static_cast<jint>(nbytes));
env->DeleteLocalRef(byte_array);
RETURN_NOT_OK(CheckJniException(env));
position_ += nbytes;
return arrow::Status::OK();
}
arrow::Status Flush() override {
JNIEnv* env = GetEnv();
if (!env) {
return arrow::Status::IOError("Failed to get JNI environment");
}
if (!java_output_stream_) {
return arrow::Status::IOError("OutputStream is closed");
}
env->CallVoidMethod(java_output_stream_, flush_method_);
return CheckJniException(env);
}
private:
JNIEnv* GetEnv() const {
JNIEnv* env;
if (vm_->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION) != JNI_OK) {
return nullptr;
}
return env;
}
arrow::Status CheckJniException(JNIEnv* env) {
if (env->ExceptionCheck()) {
jthrowable exception = env->ExceptionOccurred();
env->ExceptionClear();
std::string error_msg = "Java exception occurred in OutputStream";
// Try to get exception message
jclass exception_class = env->GetObjectClass(exception);
jmethodID get_message_method =
env->GetMethodID(exception_class, "toString", "()Ljava/lang/String;");
if (get_message_method) {
jstring message =
(jstring)env->CallObjectMethod(exception, get_message_method);
if (message) {
const char* msg_chars = env->GetStringUTFChars(message, nullptr);
error_msg = std::string(msg_chars);
env->ReleaseStringUTFChars(message, msg_chars);
env->DeleteLocalRef(message);
}
}
env->DeleteLocalRef(exception);
env->DeleteLocalRef(exception_class);
return arrow::Status::IOError(error_msg);
}
return arrow::Status::OK();
}
JavaVM* vm_;
jobject java_output_stream_;
jmethodID write_method_;
jmethodID flush_method_;
jmethodID close_method_;
int64_t position_;
};
struct ParquetWriterHolder {
std::unique_ptr<parquet::arrow::FileWriter> writer;
std::shared_ptr<arrow::io::OutputStream> output_stream;
std::shared_ptr<arrow::Schema> schema;
};
// Helper function to build WriterProperties from Java ParquetWriterProperties object
std::shared_ptr<parquet::WriterProperties> BuildWriterProperties(JNIEnv* env,
jobject java_properties) {
parquet::WriterProperties::Builder builder;
if (java_properties == nullptr) {
return builder.build();
}
jclass props_class = env->GetObjectClass(java_properties);
// Get maxRowGroupLength
jmethodID get_max_row_group_method =
env->GetMethodID(props_class, "getMaxRowGroupLength", "()J");
if (get_max_row_group_method) {
jlong max_row_group = env->CallLongMethod(java_properties, get_max_row_group_method);
if (max_row_group > 0) {
builder.max_row_group_length(static_cast<int64_t>(max_row_group));
}
}
// Get writeBatchSize
jmethodID get_write_batch_size_method =
env->GetMethodID(props_class, "getWriteBatchSize", "()J");
if (get_write_batch_size_method) {
jlong write_batch_size = env->CallLongMethod(java_properties, get_write_batch_size_method);
if (write_batch_size > 0) {
builder.write_batch_size(static_cast<int64_t>(write_batch_size));
}
}
// Get dataPageSize
jmethodID get_data_page_size_method =
env->GetMethodID(props_class, "getDataPageSize", "()J");
if (get_data_page_size_method) {
jlong data_page_size = env->CallLongMethod(java_properties, get_data_page_size_method);
if (data_page_size > 0) {
builder.data_pagesize(static_cast<int64_t>(data_page_size));
}
}
// Get compressionCodec
jmethodID get_compression_codec_method =
env->GetMethodID(props_class, "getCompressionCodec", "()Ljava/lang/String;");
if (get_compression_codec_method) {
jstring codec_str = (jstring)env->CallObjectMethod(java_properties, get_compression_codec_method);
if (codec_str != nullptr) {
std::string codec_name = arrow::dataset::jni::JStringToCString(env, codec_str);
// Use Arrow's Codec::GetCompressionType to parse compression name
auto arrow_compression_result = arrow::util::Codec::GetCompressionType(codec_name);
if (arrow_compression_result.ok()) {
// Parquet WriterProperties::Builder can accept Arrow Compression::type directly
arrow::Compression::type compression = arrow_compression_result.ValueOrDie();
// Set compression for all columns (using Arrow compression type directly)
builder.compression(compression);
} else {
// If parsing fails, log a warning but continue with UNCOMPRESSED
// This allows the code to work even with unsupported compression types
}
env->DeleteLocalRef(codec_str);
}
}
// Get compressionLevel
jmethodID get_compression_level_method =
env->GetMethodID(props_class, "getCompressionLevel", "()I");
if (get_compression_level_method) {
jint comp_level = env->CallIntMethod(java_properties, get_compression_level_method);
if (comp_level > 0) {
builder.compression_level(comp_level);
}
}
// Get writePageIndex
jmethodID get_write_page_index_method =
env->GetMethodID(props_class, "getWritePageIndex", "()Z");
if (get_write_page_index_method) {
jboolean write_index = env->CallBooleanMethod(java_properties, get_write_page_index_method);
if (write_index) {
builder.enable_write_page_index();
}
}
env->DeleteLocalRef(props_class);
return builder.build();
}
// Helper function to build ArrowWriterProperties
std::shared_ptr<parquet::ArrowWriterProperties> BuildArrowWriterProperties(JNIEnv* env,
jobject java_properties) {
parquet::ArrowWriterProperties::Builder builder;
if (java_properties == nullptr) {
return builder.build();
}
jclass props_class = env->GetObjectClass(java_properties);
// Get useThreads (for ArrowWriterProperties)
jmethodID get_use_threads_method =
env->GetMethodID(props_class, "getUseThreads", "()Z");
if (get_use_threads_method) {
jboolean use_threads = env->CallBooleanMethod(java_properties, get_use_threads_method);
builder.set_use_threads(use_threads);
}
env->DeleteLocalRef(props_class);
return builder.build();
}
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("Partition column '", ref.ToString(), "' is not in dataset schema");
}
}
return schema(std::move(columns))->WithMetadata(input->metadata());
}
} // namespace
using arrow::dataset::jni::CreateGlobalClassReference;
using arrow::dataset::jni::CreateNativeRef;
using arrow::dataset::jni::FromSchemaByteArray;
using arrow::dataset::jni::GetMethodID;
using arrow::dataset::jni::JStringToCString;
using arrow::dataset::jni::ReleaseNativeRef;
using arrow::dataset::jni::RetrieveNativeInstance;
using arrow::dataset::jni::ToSchemaByteArray;
using arrow::dataset::jni::ToStringVector;
using arrow::dataset::jni::ReservationListenableMemoryPool;
using arrow::dataset::jni::ReservationListener;
#define JNI_METHOD_START try {
// macro ended
#define JNI_METHOD_END(fallback_expr) \
} \
catch (JniPendingException & e) { \
if (e.HasCause()) { \
env->Throw(e.GetCause()); \
return fallback_expr; \
} \
env->ThrowNew(runtime_exception_class, e.what()); \
return fallback_expr; \
}
// macro ended
jint JNI_OnLoad(JavaVM* vm, void* reserved) {
JNIEnv* env;
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION) != JNI_OK) {
return JNI_ERR;
}
JNI_METHOD_START
illegal_access_exception_class =
CreateGlobalClassReference(env, "Ljava/lang/IllegalAccessException;");
illegal_argument_exception_class =
CreateGlobalClassReference(env, "Ljava/lang/IllegalArgumentException;");
runtime_exception_class =
CreateGlobalClassReference(env, "Ljava/lang/RuntimeException;");
java_reservation_listener_class =
CreateGlobalClassReference(env,
"Lorg/apache/arrow/"
"dataset/jni/ReservationListener;");
reserve_memory_method =
JniGetOrThrow(GetMethodID(env, java_reservation_listener_class, "reserve", "(J)V"));
unreserve_memory_method = JniGetOrThrow(
GetMethodID(env, java_reservation_listener_class, "unreserve", "(J)V"));
default_memory_pool_id = reinterpret_cast<jlong>(arrow::default_memory_pool());
return JNI_VERSION;
JNI_METHOD_END(JNI_ERR)
}
void JNI_OnUnload(JavaVM* vm, void* reserved) {
JNIEnv* env;
vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION);
env->DeleteGlobalRef(illegal_access_exception_class);
env->DeleteGlobalRef(illegal_argument_exception_class);
env->DeleteGlobalRef(runtime_exception_class);
env->DeleteGlobalRef(java_reservation_listener_class);
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, const 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("Cannot map odd number of array elements to key/value pairs");
}
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));
uintptr_t memory_address = 0;
try {
memory_address = std::stol(JStringToCString(env, j_string_value));
} catch(const std::exception& ex) {
JniThrow("Failed to parse memory address from string value. Error: " + std::string(ex.what()));
} catch (...) {
JniThrow("Failed to parse memory address from string value.");
}
auto* arrow_stream_in = reinterpret_cast<ArrowArrayStream*>(memory_address);
std::shared_ptr<arrow::RecordBatchReader> readerIn = JniGetOrThrow(arrow::ImportRecordBatchReader(arrow_stream_in));
output_table = JniGetOrThrow(readerIn->ToTable());
map_table_to_record_batch_reader[JStringToCString(env, j_string_key)] = output_table;
}
return map_table_to_record_batch_reader;
}
/// Find the arrow Table associated with a given table name
std::shared_ptr<arrow::Table> GetTableByName(const std::vector<std::string>& names,
const std::unordered_map<std::string, std::shared_ptr<arrow::Table>>& tables) {
if (names.size() != 1) {
JniThrow("Tables with hierarchical names are not supported");
}
const auto& it = tables.find(names[0]);
if (it == tables.end()) {
JniThrow("Table is referenced, but not provided: " + names[0]);
}
return it->second;
}
std::shared_ptr<arrow::Buffer> LoadArrowBufferFromByteBuffer(JNIEnv* env, jobject byte_buffer) {
const auto *buff = reinterpret_cast<jbyte*>(env->GetDirectBufferAddress(byte_buffer));
int length = env->GetDirectBufferCapacity(byte_buffer);
std::shared_ptr<arrow::Buffer> buffer = JniGetOrThrow(arrow::AllocateBuffer(length));
std::memcpy(buffer->mutable_data(), buff, length);
return buffer;
}
inline bool ParseBool(const std::string& value) { return value == "true" ? true : false; }
inline char ParseChar(const std::string& key, const std::string& value) {
if (value.size() != 1) {
JniThrow("Option " + key + " should be a char, but is " + value);
}
return value.at(0);
}
/// \brief Construct FragmentScanOptions from config map
#ifdef ARROW_CSV
bool SetCsvConvertOptions(arrow::csv::ConvertOptions& options, const std::string& key,
const std::string& value) {
if (key == "column_types") {
int64_t schema_address = std::stol(value);
ArrowSchema* c_schema = reinterpret_cast<ArrowSchema*>(schema_address);
auto schema = JniGetOrThrow(arrow::ImportSchema(c_schema));
auto& column_types = options.column_types;
for (auto field : schema->fields()) {
column_types[field->name()] = field->type();
}
} else if (key == "strings_can_be_null") {
options.strings_can_be_null = ParseBool(value);
} else if (key == "check_utf8") {
options.check_utf8 = ParseBool(value);
} else if (key == "null_values") {
options.null_values = {value};
} else if (key == "true_values") {
options.true_values = {value};
} else if (key == "false_values") {
options.false_values = {value};
} else if (key == "quoted_strings_can_be_null") {
options.quoted_strings_can_be_null = ParseBool(value);
} else if (key == "auto_dict_encode") {
options.auto_dict_encode = ParseBool(value);
} else if (key == "auto_dict_max_cardinality") {
options.auto_dict_max_cardinality = std::stoi(value);
} else if (key == "decimal_point") {
options.decimal_point = ParseChar(key, value);
} else if (key == "include_missing_columns") {
options.include_missing_columns = ParseBool(value);
} else {
return false;
}
return true;
}
bool SetCsvParseOptions(arrow::csv::ParseOptions& options, const std::string& key,
const std::string& value) {
if (key == "delimiter") {
options.delimiter = ParseChar(key, value);
} else if (key == "quoting") {
options.quoting = ParseBool(value);
} else if (key == "quote_char") {
options.quote_char = ParseChar(key, value);
} else if (key == "double_quote") {
options.double_quote = ParseBool(value);
} else if (key == "escaping") {
options.escaping = ParseBool(value);
} else if (key == "escape_char") {
options.escape_char = ParseChar(key, value);
} else if (key == "newlines_in_values") {
options.newlines_in_values = ParseBool(value);
} else if (key == "ignore_empty_lines") {
options.ignore_empty_lines = ParseBool(value);
} else {
return false;
}
return true;
}
bool SetCsvReadOptions(arrow::csv::ReadOptions& options, const std::string& key,
const std::string& value) {
if (key == "use_threads") {
options.use_threads = ParseBool(value);
} else if (key == "block_size") {
options.block_size = std::stoi(value);
} else if (key == "skip_rows") {
options.skip_rows = std::stoi(value);
} else if (key == "skip_rows_after_names") {
options.skip_rows_after_names = std::stoi(value);
} else if (key == "autogenerate_column_names") {
options.autogenerate_column_names = ParseBool(value);
} else {
return false;
}
return true;
}
std::shared_ptr<arrow::dataset::FragmentScanOptions> ToCsvFragmentScanOptions(
const std::unordered_map<std::string, std::string>& configs) {
std::shared_ptr<arrow::dataset::CsvFragmentScanOptions> options =
std::make_shared<arrow::dataset::CsvFragmentScanOptions>();
for (const auto& [key, value] : configs) {
bool setValid = SetCsvParseOptions(options->parse_options, key, value) ||
SetCsvConvertOptions(options->convert_options, key, value) ||
SetCsvReadOptions(options->read_options, key, value);
if (!setValid) {
JniThrow("Config " + key + " is not supported.");
}
}
return options;
}
#endif
arrow::Result<std::shared_ptr<arrow::dataset::FragmentScanOptions>>
GetFragmentScanOptions(jint file_format_id,
const std::unordered_map<std::string, std::string>& configs) {
switch (file_format_id) {
#ifdef ARROW_CSV
case 3:
return ToCsvFragmentScanOptions(configs);
#endif
default:
return arrow::Status::Invalid("Illegal file format id: ", file_format_id);
}
}
std::unordered_map<std::string, std::string> ToStringMap(JNIEnv* env,
jobjectArray& str_array) {
int length = env->GetArrayLength(str_array);
std::unordered_map<std::string, std::string> map;
map.reserve(length / 2);
for (int i = 0; i < length; i += 2) {
auto key = reinterpret_cast<jstring>(env->GetObjectArrayElement(str_array, i));
auto value = reinterpret_cast<jstring>(env->GetObjectArrayElement(str_array, i + 1));
map[JStringToCString(env, key)] = JStringToCString(env, value);
}
return map;
}
/*
* Class: org_apache_arrow_dataset_jni_NativeMemoryPool
* Method: getDefaultMemoryPool
* Signature: ()J
*/
JNIEXPORT jlong JNICALL
Java_org_apache_arrow_dataset_jni_NativeMemoryPool_getDefaultMemoryPool(JNIEnv* env,
jclass) {
JNI_METHOD_START
return default_memory_pool_id;
JNI_METHOD_END(-1L)
}
/*
* Class: org_apache_arrow_dataset_jni_NativeMemoryPool
* Method: createListenableMemoryPool
* Signature: (Lorg/apache/arrow/memory/ReservationListener;)J
*/
JNIEXPORT jlong JNICALL
Java_org_apache_arrow_dataset_jni_NativeMemoryPool_createListenableMemoryPool(
JNIEnv* env, jclass, jobject jlistener) {
JNI_METHOD_START
jobject jlistener_ref = env->NewGlobalRef(jlistener);
JavaVM* vm;
if (env->GetJavaVM(&vm) != JNI_OK) {
JniThrow("Unable to get JavaVM instance");
}
std::shared_ptr<ReservationListener> listener =
std::make_shared<ReserveFromJava>(vm, jlistener_ref);
auto memory_pool =
new ReservationListenableMemoryPool(arrow::default_memory_pool(), listener);
return reinterpret_cast<jlong>(memory_pool);
JNI_METHOD_END(-1L)
}
/*
* Class: org_apache_arrow_dataset_jni_NativeMemoryPool
* Method: releaseMemoryPool
* Signature: (J)V
*/
JNIEXPORT void JNICALL
Java_org_apache_arrow_dataset_jni_NativeMemoryPool_releaseMemoryPool(
JNIEnv* env, jclass, jlong memory_pool_id) {
JNI_METHOD_START
if (memory_pool_id == default_memory_pool_id) {
return;
}
ReservationListenableMemoryPool* pool =
reinterpret_cast<ReservationListenableMemoryPool*>(memory_pool_id);
if (pool == nullptr) {
return;
}
std::shared_ptr<ReserveFromJava> rm =
std::dynamic_pointer_cast<ReserveFromJava>(pool->get_listener());
if (rm == nullptr) {
delete pool;
return;
}
delete pool;
env->DeleteGlobalRef(rm->GetJavaReservationListener());
JNI_METHOD_END()
}
/*
* Class: org_apache_arrow_dataset_jni_NativeMemoryPool
* Method: bytesAllocated
* Signature: (J)J
*/
JNIEXPORT jlong JNICALL Java_org_apache_arrow_dataset_jni_NativeMemoryPool_bytesAllocated(
JNIEnv* env, jclass, jlong memory_pool_id) {
JNI_METHOD_START
arrow::MemoryPool* pool = reinterpret_cast<arrow::MemoryPool*>(memory_pool_id);
if (pool == nullptr) {
JniThrow("Memory pool instance not found. It may not exist or have been closed");
}
return pool->bytes_allocated();
JNI_METHOD_END(-1L)
}
/*
* Class: org_apache_arrow_dataset_jni_JniWrapper
* Method: closeDatasetFactory
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_org_apache_arrow_dataset_jni_JniWrapper_closeDatasetFactory(
JNIEnv* env, jobject, jlong id) {
JNI_METHOD_START
ReleaseNativeRef<arrow::dataset::DatasetFactory>(id);
JNI_METHOD_END()
}
/*
* Class: org_apache_arrow_dataset_jni_JniWrapper
* Method: inspectSchema
* Signature: (J)[B
*/
JNIEXPORT jbyteArray JNICALL Java_org_apache_arrow_dataset_jni_JniWrapper_inspectSchema(
JNIEnv* env, jobject, jlong dataset_factor_id) {
JNI_METHOD_START
std::shared_ptr<arrow::dataset::DatasetFactory> d =
RetrieveNativeInstance<arrow::dataset::DatasetFactory>(dataset_factor_id);
std::shared_ptr<arrow::Schema> schema = JniGetOrThrow(d->Inspect());
return JniGetOrThrow(ToSchemaByteArray(env, schema));
JNI_METHOD_END(nullptr)
}
/*
* Class: org_apache_arrow_dataset_jni_JniWrapper
* Method: createDataset
* Signature: (J[B)J
*/
JNIEXPORT jlong JNICALL Java_org_apache_arrow_dataset_jni_JniWrapper_createDataset(
JNIEnv* env, jobject, jlong dataset_factory_id, jbyteArray schema_bytes) {
JNI_METHOD_START
std::shared_ptr<arrow::dataset::DatasetFactory> d =
RetrieveNativeInstance<arrow::dataset::DatasetFactory>(dataset_factory_id);
std::shared_ptr<arrow::Schema> schema;
schema = JniGetOrThrow(FromSchemaByteArray(env, schema_bytes));
std::shared_ptr<arrow::dataset::Dataset> dataset = JniGetOrThrow(d->Finish(schema));
return CreateNativeRef(dataset);
JNI_METHOD_END(-1L)
}
/*
* Class: org_apache_arrow_dataset_jni_JniWrapper
* Method: closeDataset
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_org_apache_arrow_dataset_jni_JniWrapper_closeDataset(
JNIEnv* env, jobject, jlong id) {
JNI_METHOD_START
ReleaseNativeRef<arrow::dataset::Dataset>(id);
JNI_METHOD_END()
}
/*
* Class: org_apache_arrow_dataset_jni_JniWrapper
* Method: createScanner
* Signature:
* (J[Ljava/lang/String;Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;JI;[Ljava/lang/String;J)J
*/
JNIEXPORT jlong JNICALL Java_org_apache_arrow_dataset_jni_JniWrapper_createScanner(
JNIEnv* env, jobject, jlong dataset_id, jobjectArray columns,
jobject substrait_projection, jobject substrait_filter, jlong batch_size,
jint file_format_id, jobjectArray options, jlong memory_pool_id) {
JNI_METHOD_START
arrow::MemoryPool* pool = reinterpret_cast<arrow::MemoryPool*>(memory_pool_id);
if (pool == nullptr) {
JniThrow("Memory pool does not exist or has been closed");
}
std::shared_ptr<arrow::dataset::Dataset> dataset =
RetrieveNativeInstance<arrow::dataset::Dataset>(dataset_id);
std::shared_ptr<arrow::dataset::ScannerBuilder> scanner_builder =
JniGetOrThrow(dataset->NewScan());
JniAssertOkOrThrow(scanner_builder->Pool(pool));
if (columns != nullptr) {
std::vector<std::string> column_vector = ToStringVector(env, columns);
JniAssertOkOrThrow(scanner_builder->Project(column_vector));
}
if (substrait_projection != nullptr) {
std::shared_ptr<arrow::Buffer> buffer = LoadArrowBufferFromByteBuffer(env,
substrait_projection);
std::vector<arrow::compute::Expression> project_exprs;
std::vector<std::string> project_names;
arrow::engine::BoundExpressions bounded_expression =
JniGetOrThrow(arrow::engine::DeserializeExpressions(*buffer));
for(arrow::engine::NamedExpression& named_expression :
bounded_expression.named_expressions) {
project_exprs.push_back(std::move(named_expression.expression));
project_names.push_back(std::move(named_expression.name));
}
JniAssertOkOrThrow(scanner_builder->Project(std::move(project_exprs), std::move(project_names)));
}
if (substrait_filter != nullptr) {
std::shared_ptr<arrow::Buffer> buffer = LoadArrowBufferFromByteBuffer(env,
substrait_filter);
std::optional<arrow::compute::Expression> filter_expr = std::nullopt;
arrow::engine::BoundExpressions bounded_expression =
JniGetOrThrow(arrow::engine::DeserializeExpressions(*buffer));
for(arrow::engine::NamedExpression& named_expression :
bounded_expression.named_expressions) {
filter_expr = named_expression.expression;
if (named_expression.expression.type()->id() == arrow::Type::BOOL) {
filter_expr = named_expression.expression;
} else {
JniThrow("There is no filter expression in the expression provided");
}
}
if (filter_expr == std::nullopt) {
JniThrow("The filter expression has not been provided");
}
JniAssertOkOrThrow(scanner_builder->Filter(*filter_expr));
}
if (file_format_id != -1 && options != nullptr) {
std::unordered_map<std::string, std::string> option_map = ToStringMap(env, options);
std::shared_ptr<arrow::dataset::FragmentScanOptions> scan_options =
JniGetOrThrow(GetFragmentScanOptions(file_format_id, option_map));
JniAssertOkOrThrow(scanner_builder->FragmentScanOptions(scan_options));
}
JniAssertOkOrThrow(scanner_builder->BatchSize(batch_size));
auto scanner = JniGetOrThrow(scanner_builder->Finish());
std::shared_ptr<DisposableScannerAdaptor> scanner_adaptor =
JniGetOrThrow(DisposableScannerAdaptor::Create(scanner));
jlong id = CreateNativeRef(scanner_adaptor);
return id;
JNI_METHOD_END(-1L)
}
/*
* Class: org_apache_arrow_dataset_jni_JniWrapper
* Method: closeScanner
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_org_apache_arrow_dataset_jni_JniWrapper_closeScanner(
JNIEnv* env, jobject, jlong scanner_id) {
JNI_METHOD_START
ReleaseNativeRef<DisposableScannerAdaptor>(scanner_id);
JNI_METHOD_END()
}
/*
* Class: org_apache_arrow_dataset_jni_JniWrapper
* Method: getSchemaFromScanner
* Signature: (J)[B
*/
JNIEXPORT jbyteArray JNICALL
Java_org_apache_arrow_dataset_jni_JniWrapper_getSchemaFromScanner(JNIEnv* env, jobject,
jlong scanner_id) {
JNI_METHOD_START
std::shared_ptr<arrow::Schema> schema =
RetrieveNativeInstance<DisposableScannerAdaptor>(scanner_id)
->GetScanner()
->options()
->projected_schema;
return JniGetOrThrow(ToSchemaByteArray(env, schema));
JNI_METHOD_END(nullptr)
}
/*
* Class: org_apache_arrow_dataset_jni_JniWrapper
* Method: nextRecordBatch
* Signature: (JJ)Z
*/
JNIEXPORT jboolean JNICALL Java_org_apache_arrow_dataset_jni_JniWrapper_nextRecordBatch(
JNIEnv* env, jobject, jlong scanner_id, jlong struct_array) {