forked from apache/datafusion-comet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
882 lines (803 loc) · 26.9 KB
/
mod.rs
File metadata and controls
882 lines (803 loc) · 26.9 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
// 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.
pub mod data_type;
pub mod encryption_support;
pub mod mutable_vector;
pub use mutable_vector::*;
#[macro_use]
pub mod util;
pub mod parquet_exec;
pub mod parquet_support;
pub mod read;
pub mod schema_adapter;
mod objectstore;
use std::collections::HashMap;
use std::task::Poll;
use std::{boxed::Box, ptr::NonNull, sync::Arc};
use crate::errors::{try_unwrap_or_throw, CometError};
use arrow::ffi::FFI_ArrowArray;
/// JNI exposed methods
use jni::JNIEnv;
use jni::{
objects::{GlobalRef, JByteBuffer, JClass},
sys::{jboolean, jbyte, jdouble, jfloat, jint, jlong, jshort},
};
use self::util::jni::TypePromotionInfo;
use crate::execution::jni_api::get_runtime;
use crate::execution::metrics::utils::update_comet_metric;
use crate::execution::operators::ExecutionError;
use crate::execution::planner::PhysicalPlanner;
use crate::execution::serde;
use crate::execution::spark_plan::SparkPlan;
use crate::execution::utils::SparkArrowConvert;
use crate::jvm_bridge::{jni_new_global_ref, JVMClasses};
use crate::parquet::data_type::AsBytes;
use crate::parquet::encryption_support::{CometEncryptionFactory, ENCRYPTION_FACTORY_ID};
use crate::parquet::parquet_exec::init_datasource_exec;
use crate::parquet::parquet_support::prepare_object_store_with_configs;
use arrow::array::{Array, RecordBatch};
use arrow::buffer::{Buffer, MutableBuffer};
use datafusion::datasource::listing::PartitionedFile;
use datafusion::execution::SendableRecordBatchStream;
use datafusion::physical_plan::ExecutionPlan;
use datafusion::prelude::{SessionConfig, SessionContext};
use futures::{poll, StreamExt};
use jni::objects::{
JBooleanArray, JByteArray, JLongArray, JMap, JObject, JObjectArray, JString, ReleaseMode,
};
use jni::sys::{jintArray, JNI_FALSE};
use object_store::path::Path;
use read::ColumnReader;
use util::jni::{convert_column_descriptor, convert_encoding, deserialize_schema};
/// Parquet read context maintained across multiple JNI calls.
struct Context {
pub column_reader: ColumnReader,
last_data_page: Option<GlobalRef>,
}
#[no_mangle]
pub extern "system" fn Java_org_apache_comet_parquet_Native_initColumnReader(
e: JNIEnv,
_jclass: JClass,
primitive_type: jint,
logical_type: jint,
read_primitive_type: jint,
jni_path: JObjectArray,
max_dl: jint,
max_rl: jint,
bit_width: jint,
read_bit_width: jint,
is_signed: jboolean,
type_length: jint,
precision: jint,
read_precision: jint,
scale: jint,
read_scale: jint,
time_unit: jint,
is_adjusted_utc: jboolean,
batch_size: jint,
use_decimal_128: jboolean,
use_legacy_date_timestamp: jboolean,
) -> jlong {
try_unwrap_or_throw(&e, |mut env| {
let desc = convert_column_descriptor(
&mut env,
primitive_type,
logical_type,
max_dl,
max_rl,
bit_width,
is_signed,
type_length,
precision,
scale,
time_unit,
is_adjusted_utc,
jni_path,
)?;
let promotion_info = TypePromotionInfo::new_from_jni(
read_primitive_type,
read_precision,
read_scale,
read_bit_width,
);
let ctx = Context {
column_reader: ColumnReader::get(
desc,
promotion_info,
batch_size as usize,
use_decimal_128 != 0,
use_legacy_date_timestamp != 0,
),
last_data_page: None,
};
let res = Box::new(ctx);
Ok(Box::into_raw(res) as i64)
})
}
/// # Safety
/// This function is inheritly unsafe since it deals with raw pointers passed from JNI.
#[no_mangle]
pub unsafe extern "system" fn Java_org_apache_comet_parquet_Native_setDictionaryPage(
e: JNIEnv,
_jclass: JClass,
handle: jlong,
page_value_count: jint,
page_data: JByteArray,
encoding: jint,
) {
try_unwrap_or_throw(&e, |env| {
let reader = get_reader(handle)?;
// convert value encoding ordinal to the native encoding definition
let encoding = convert_encoding(encoding);
// copy the input on-heap buffer to native
let page_len = env.get_array_length(&page_data)?;
let mut buffer = MutableBuffer::from_len_zeroed(page_len as usize);
env.get_byte_array_region(&page_data, 0, from_u8_slice(buffer.as_slice_mut()))?;
reader.set_dictionary_page(page_value_count as usize, buffer.into(), encoding);
Ok(())
})
}
/// # Safety
/// This function is inheritly unsafe since it deals with raw pointers passed from JNI.
#[no_mangle]
pub unsafe extern "system" fn Java_org_apache_comet_parquet_Native_setPageV1(
e: JNIEnv,
_jclass: JClass,
handle: jlong,
page_value_count: jint,
page_data: JByteArray,
value_encoding: jint,
) {
try_unwrap_or_throw(&e, |env| {
let reader = get_reader(handle)?;
// convert value encoding ordinal to the native encoding definition
let encoding = convert_encoding(value_encoding);
// copy the input on-heap buffer to native
let page_len = env.get_array_length(&page_data)?;
let mut buffer = MutableBuffer::from_len_zeroed(page_len as usize);
env.get_byte_array_region(&page_data, 0, from_u8_slice(buffer.as_slice_mut()))?;
reader.set_page_v1(page_value_count as usize, buffer.into(), encoding);
Ok(())
})
}
/// # Safety
/// This function is inheritly unsafe since it deals with raw pointers passed from JNI.
#[no_mangle]
pub unsafe extern "system" fn Java_org_apache_comet_parquet_Native_setPageBufferV1(
e: JNIEnv,
_jclass: JClass,
handle: jlong,
page_value_count: jint,
buffer: JByteBuffer,
value_encoding: jint,
) {
try_unwrap_or_throw(&e, |env| {
let ctx = get_context(handle)?;
let reader = &mut ctx.column_reader;
// convert value encoding ordinal to the native encoding definition
let encoding = convert_encoding(value_encoding);
// Convert the page to global reference so it won't get GC'd by Java. Also free the last
// page if there is any.
ctx.last_data_page = Some(env.new_global_ref(&buffer)?);
let buf_slice = env.get_direct_buffer_address(&buffer)?;
let buf_capacity = env.get_direct_buffer_capacity(&buffer)?;
unsafe {
let page_ptr = NonNull::new_unchecked(buf_slice);
let buffer = Buffer::from_custom_allocation(
page_ptr,
buf_capacity,
Arc::new(FFI_ArrowArray::empty()),
);
reader.set_page_v1(page_value_count as usize, buffer, encoding);
}
Ok(())
})
}
/// # Safety
/// This function is inheritly unsafe since it deals with raw pointers passed from JNI.
#[no_mangle]
pub unsafe extern "system" fn Java_org_apache_comet_parquet_Native_setPageV2(
e: JNIEnv,
_jclass: JClass,
handle: jlong,
page_value_count: jint,
def_level_data: JByteArray,
rep_level_data: JByteArray,
value_data: JByteArray,
value_encoding: jint,
) {
try_unwrap_or_throw(&e, |env| {
let reader = get_reader(handle)?;
// convert value encoding ordinal to the native encoding definition
let encoding = convert_encoding(value_encoding);
// copy the input on-heap buffer to native
let dl_len = env.get_array_length(&def_level_data)?;
let mut dl_buffer = MutableBuffer::from_len_zeroed(dl_len as usize);
env.get_byte_array_region(&def_level_data, 0, from_u8_slice(dl_buffer.as_slice_mut()))?;
let rl_len = env.get_array_length(&rep_level_data)?;
let mut rl_buffer = MutableBuffer::from_len_zeroed(rl_len as usize);
env.get_byte_array_region(&rep_level_data, 0, from_u8_slice(rl_buffer.as_slice_mut()))?;
let v_len = env.get_array_length(&value_data)?;
let mut v_buffer = MutableBuffer::from_len_zeroed(v_len as usize);
env.get_byte_array_region(&value_data, 0, from_u8_slice(v_buffer.as_slice_mut()))?;
reader.set_page_v2(
page_value_count as usize,
dl_buffer.into(),
rl_buffer.into(),
v_buffer.into(),
encoding,
);
Ok(())
})
}
#[no_mangle]
pub extern "system" fn Java_org_apache_comet_parquet_Native_setNull(
env: JNIEnv,
_jclass: JClass,
handle: jlong,
) {
try_unwrap_or_throw(&env, |_| {
let reader = get_reader(handle)?;
reader.set_null();
Ok(())
})
}
#[no_mangle]
pub extern "system" fn Java_org_apache_comet_parquet_Native_setBoolean(
env: JNIEnv,
_jclass: JClass,
handle: jlong,
value: jboolean,
) {
try_unwrap_or_throw(&env, |_| {
let reader = get_reader(handle)?;
reader.set_boolean(value != 0);
Ok(())
})
}
#[no_mangle]
pub extern "system" fn Java_org_apache_comet_parquet_Native_setByte(
env: JNIEnv,
_jclass: JClass,
handle: jlong,
value: jbyte,
) {
try_unwrap_or_throw(&env, |_| {
let reader = get_reader(handle)?;
reader.set_fixed::<i8>(value);
Ok(())
})
}
#[no_mangle]
pub extern "system" fn Java_org_apache_comet_parquet_Native_setShort(
env: JNIEnv,
_jclass: JClass,
handle: jlong,
value: jshort,
) {
try_unwrap_or_throw(&env, |_| {
let reader = get_reader(handle)?;
reader.set_fixed::<i16>(value);
Ok(())
})
}
#[no_mangle]
pub extern "system" fn Java_org_apache_comet_parquet_Native_setInt(
env: JNIEnv,
_jclass: JClass,
handle: jlong,
value: jint,
) {
try_unwrap_or_throw(&env, |_| {
let reader = get_reader(handle)?;
reader.set_fixed::<i32>(value);
Ok(())
})
}
#[no_mangle]
pub extern "system" fn Java_org_apache_comet_parquet_Native_setLong(
env: JNIEnv,
_jclass: JClass,
handle: jlong,
value: jlong,
) {
try_unwrap_or_throw(&env, |_| {
let reader = get_reader(handle)?;
reader.set_fixed::<i64>(value);
Ok(())
})
}
#[no_mangle]
pub extern "system" fn Java_org_apache_comet_parquet_Native_setFloat(
env: JNIEnv,
_jclass: JClass,
handle: jlong,
value: jfloat,
) {
try_unwrap_or_throw(&env, |_| {
let reader = get_reader(handle)?;
reader.set_fixed::<f32>(value);
Ok(())
})
}
#[no_mangle]
pub extern "system" fn Java_org_apache_comet_parquet_Native_setDouble(
env: JNIEnv,
_jclass: JClass,
handle: jlong,
value: jdouble,
) {
try_unwrap_or_throw(&env, |_| {
let reader = get_reader(handle)?;
reader.set_fixed::<f64>(value);
Ok(())
})
}
/// # Safety
/// This function is inheritly unsafe since it deals with raw pointers passed from JNI.
#[no_mangle]
pub unsafe extern "system" fn Java_org_apache_comet_parquet_Native_setBinary(
e: JNIEnv,
_jclass: JClass,
handle: jlong,
value: JByteArray,
) {
try_unwrap_or_throw(&e, |env| {
let reader = get_reader(handle)?;
let len = env.get_array_length(&value)?;
let mut buffer = MutableBuffer::from_len_zeroed(len as usize);
env.get_byte_array_region(&value, 0, from_u8_slice(buffer.as_slice_mut()))?;
reader.set_binary(buffer);
Ok(())
})
}
/// # Safety
/// This function is inheritly unsafe since it deals with raw pointers passed from JNI.
#[no_mangle]
pub unsafe extern "system" fn Java_org_apache_comet_parquet_Native_setDecimal(
e: JNIEnv,
_jclass: JClass,
handle: jlong,
value: JByteArray,
) {
try_unwrap_or_throw(&e, |env| {
let reader = get_reader(handle)?;
let len = env.get_array_length(&value)?;
let mut buffer = MutableBuffer::from_len_zeroed(len as usize);
env.get_byte_array_region(&value, 0, from_u8_slice(buffer.as_slice_mut()))?;
reader.set_decimal_flba(buffer);
Ok(())
})
}
#[no_mangle]
pub extern "system" fn Java_org_apache_comet_parquet_Native_setPosition(
env: JNIEnv,
_jclass: JClass,
handle: jlong,
value: jlong,
size: jint,
) {
try_unwrap_or_throw(&env, |_| {
let reader = get_reader(handle)?;
reader.set_position(value, size as usize);
Ok(())
})
}
/// # Safety
/// This function is inheritly unsafe since it deals with raw pointers passed from JNI.
#[no_mangle]
pub unsafe extern "system" fn Java_org_apache_comet_parquet_Native_setIndices(
e: JNIEnv,
_jclass: JClass,
handle: jlong,
offset: jlong,
batch_size: jint,
indices: JLongArray,
) -> jlong {
try_unwrap_or_throw(&e, |mut env| {
let reader = get_reader(handle)?;
let indices = unsafe { env.get_array_elements(&indices, ReleaseMode::NoCopyBack)? };
let len = indices.len();
// paris alternately contains start index and length of continuous indices
let pairs = unsafe { core::slice::from_raw_parts_mut(indices.as_ptr(), len) };
let mut skipped = 0;
let mut filled = 0;
for i in (0..len).step_by(2) {
let index = pairs[i];
let count = pairs[i + 1];
let skip = std::cmp::min(count, offset - skipped);
skipped += skip;
if count == skip {
continue;
} else if batch_size as i64 == filled {
break;
}
let count = std::cmp::min(count - skip, batch_size as i64 - filled);
filled += count;
reader.set_position(index + skip, count as usize);
}
Ok(filled)
})
}
/// # Safety
/// This function is inheritly unsafe since it deals with raw pointers passed from JNI.
#[no_mangle]
pub unsafe extern "system" fn Java_org_apache_comet_parquet_Native_setIsDeleted(
e: JNIEnv,
_jclass: JClass,
handle: jlong,
is_deleted: JBooleanArray,
) {
try_unwrap_or_throw(&e, |env| {
let reader = get_reader(handle)?;
let len = env.get_array_length(&is_deleted)?;
let mut buffer = MutableBuffer::from_len_zeroed(len as usize);
env.get_boolean_array_region(&is_deleted, 0, buffer.as_slice_mut())?;
reader.set_is_deleted(buffer);
Ok(())
})
}
#[no_mangle]
pub extern "system" fn Java_org_apache_comet_parquet_Native_resetBatch(
env: JNIEnv,
_jclass: JClass,
handle: jlong,
) {
try_unwrap_or_throw(&env, |_| {
let reader = get_reader(handle)?;
reader.reset_batch();
Ok(())
})
}
#[no_mangle]
pub extern "system" fn Java_org_apache_comet_parquet_Native_readBatch(
e: JNIEnv,
_jclass: JClass,
handle: jlong,
batch_size: jint,
null_pad_size: jint,
) -> jintArray {
try_unwrap_or_throw(&e, |env| {
let reader = get_reader(handle)?;
let (num_values, num_nulls) =
reader.read_batch(batch_size as usize, null_pad_size as usize);
let res = env.new_int_array(2)?;
let buf: [i32; 2] = [num_values as i32, num_nulls as i32];
env.set_int_array_region(&res, 0, &buf)?;
Ok(res.into_raw())
})
}
#[no_mangle]
pub extern "system" fn Java_org_apache_comet_parquet_Native_skipBatch(
env: JNIEnv,
_jclass: JClass,
handle: jlong,
batch_size: jint,
discard: jboolean,
) -> jint {
try_unwrap_or_throw(&env, |_| {
let reader = get_reader(handle)?;
Ok(reader.skip_batch(batch_size as usize, discard == 0) as jint)
})
}
#[no_mangle]
pub extern "system" fn Java_org_apache_comet_parquet_Native_currentBatch(
e: JNIEnv,
_jclass: JClass,
handle: jlong,
array_addr: jlong,
schema_addr: jlong,
) {
try_unwrap_or_throw(&e, |_env| {
let ctx = get_context(handle)?;
let reader = &mut ctx.column_reader;
let data = reader.current_batch()?;
data.move_to_spark(array_addr, schema_addr)
.map_err(|e| e.into())
})
}
#[inline]
fn get_context<'a>(handle: jlong) -> Result<&'a mut Context, CometError> {
unsafe {
(handle as *mut Context)
.as_mut()
.ok_or_else(|| CometError::NullPointer("null context handle".to_string()))
}
}
#[inline]
fn get_reader<'a>(handle: jlong) -> Result<&'a mut ColumnReader, CometError> {
Ok(&mut get_context(handle)?.column_reader)
}
#[no_mangle]
pub extern "system" fn Java_org_apache_comet_parquet_Native_closeColumnReader(
env: JNIEnv,
_jclass: JClass,
handle: jlong,
) {
try_unwrap_or_throw(&env, |_| {
unsafe {
let ctx = get_context(handle)?;
let _ = Box::from_raw(ctx);
};
Ok(())
})
}
fn from_u8_slice(src: &mut [u8]) -> &mut [i8] {
let raw_ptr = src.as_mut_ptr() as *mut i8;
unsafe { std::slice::from_raw_parts_mut(raw_ptr, src.len()) }
}
// TODO: (ARROW NATIVE) remove this if not needed.
enum ParquetReaderState {
Init,
Reading,
Complete,
}
/// Parquet read context maintained across multiple JNI calls.
struct BatchContext {
native_plan: Arc<SparkPlan>,
metrics_node: Arc<GlobalRef>,
batch_stream: Option<SendableRecordBatchStream>,
current_batch: Option<RecordBatch>,
reader_state: ParquetReaderState,
}
#[inline]
fn get_batch_context<'a>(handle: jlong) -> Result<&'a mut BatchContext, CometError> {
unsafe {
(handle as *mut BatchContext)
.as_mut()
.ok_or_else(|| CometError::NullPointer("null batch context handle".to_string()))
}
}
fn get_file_groups_single_file(
path: &Path,
file_size: u64,
starts: &mut [i64],
lengths: &mut [i64],
) -> Vec<Vec<PartitionedFile>> {
assert!(!starts.is_empty() && starts.len() == lengths.len());
let mut groups: Vec<PartitionedFile> = Vec::with_capacity(starts.len());
for (i, &start) in starts.iter().enumerate() {
let mut partitioned_file = PartitionedFile::new_with_range(
String::new(), // Dummy file path. We will override this with our path so that url encoding does not occur
file_size,
start,
start + lengths[i],
);
partitioned_file.object_meta.location = (*path).clone();
groups.push(partitioned_file);
}
vec![groups]
}
pub fn get_object_store_options(
env: &mut JNIEnv,
map_object: JObject,
) -> Result<HashMap<String, String>, CometError> {
let map = JMap::from_env(env, &map_object)?;
// Convert to a HashMap
let mut collected_map = HashMap::new();
map.iter(env).and_then(|mut iter| {
while let Some((key, value)) = iter.next(env)? {
let key_string: String = String::from(env.get_string(&JString::from(key))?);
let value_string: String = String::from(env.get_string(&JString::from(value))?);
collected_map.insert(key_string, value_string);
}
Ok(())
})?;
Ok(collected_map)
}
/// # Safety
/// This function is inherently unsafe since it deals with raw pointers passed from JNI.
#[no_mangle]
pub unsafe extern "system" fn Java_org_apache_comet_parquet_Native_validateObjectStoreConfig(
e: JNIEnv,
_jclass: JClass,
file_path: JString,
object_store_options: JObject,
) {
try_unwrap_or_throw(&e, |mut env| {
let session_config = SessionConfig::new();
let planner =
PhysicalPlanner::new(Arc::new(SessionContext::new_with_config(session_config)), 0);
let session_ctx = planner.session_ctx();
let path: String = env.get_string(&file_path).unwrap().into();
let object_store_config = get_object_store_options(&mut env, object_store_options)?;
let (_, _) = prepare_object_store_with_configs(
session_ctx.runtime_env(),
path.clone(),
&object_store_config,
)?;
Ok(())
})
}
/// # Safety
/// This function is inherently unsafe since it deals with raw pointers passed from JNI.
#[no_mangle]
pub unsafe extern "system" fn Java_org_apache_comet_parquet_Native_initRecordBatchReader(
e: JNIEnv,
_jclass: JClass,
file_path: JString,
file_size: jlong,
starts: JLongArray,
lengths: JLongArray,
filter: JByteArray,
required_schema: JByteArray,
data_schema: JByteArray,
session_timezone: JString,
batch_size: jint,
case_sensitive: jboolean,
object_store_options: JObject,
key_unwrapper_obj: JObject,
metrics_node: JObject,
) -> jlong {
try_unwrap_or_throw(&e, |mut env| unsafe {
JVMClasses::init(&mut env);
let session_config = SessionConfig::new().with_batch_size(batch_size as usize);
let planner =
PhysicalPlanner::new(Arc::new(SessionContext::new_with_config(session_config)), 0);
let session_ctx = planner.session_ctx();
let path: String = env.get_string(&file_path).unwrap().into();
let object_store_config = get_object_store_options(&mut env, object_store_options)?;
let (object_store_url, object_store_path) = prepare_object_store_with_configs(
session_ctx.runtime_env(),
path.clone(),
&object_store_config,
)?;
let required_schema_buffer = env.convert_byte_array(&required_schema)?;
let required_schema = Arc::new(deserialize_schema(required_schema_buffer.as_bytes())?);
let data_schema_buffer = env.convert_byte_array(&data_schema)?;
let data_schema = Arc::new(deserialize_schema(data_schema_buffer.as_bytes())?);
let data_filters = if !filter.is_null() {
let filter_buffer = env.convert_byte_array(&filter)?;
let filter_expr = serde::deserialize_expr(filter_buffer.as_slice())?;
Some(vec![
planner.create_expr(&filter_expr, Arc::clone(&data_schema))?
])
} else {
None
};
let starts = env.get_array_elements(&starts, ReleaseMode::NoCopyBack)?;
let starts = core::slice::from_raw_parts_mut(starts.as_ptr(), starts.len());
let lengths = env.get_array_elements(&lengths, ReleaseMode::NoCopyBack)?;
let lengths = core::slice::from_raw_parts_mut(lengths.as_ptr(), lengths.len());
let file_groups =
get_file_groups_single_file(&object_store_path, file_size as u64, starts, lengths);
let session_timezone: String = env.get_string(&session_timezone).unwrap().into();
// Handle key unwrapper for encrypted files
let encryption_enabled = if !key_unwrapper_obj.is_null() {
let encryption_factory = CometEncryptionFactory {
key_unwrapper: jni_new_global_ref!(env, key_unwrapper_obj)?,
};
session_ctx
.runtime_env()
.register_parquet_encryption_factory(
ENCRYPTION_FACTORY_ID,
Arc::new(encryption_factory),
);
true
} else {
false
};
let scan = init_datasource_exec(
required_schema,
Some(data_schema),
None,
None,
object_store_url,
file_groups,
None,
data_filters,
None,
session_timezone.as_str(),
case_sensitive != JNI_FALSE,
false, // schema_validation_enabled - validation is done on the Java side
false, // schema_evolution_enabled
session_ctx,
encryption_enabled,
)?;
let partition_index: usize = 0;
let batch_stream = Some(scan.execute(partition_index, session_ctx.task_ctx())?);
let ctx = BatchContext {
native_plan: Arc::new(SparkPlan::new(0, scan, vec![])),
metrics_node: Arc::new(jni_new_global_ref!(env, metrics_node)?),
batch_stream,
current_batch: None,
reader_state: ParquetReaderState::Init,
};
let res = Box::new(ctx);
Ok(Box::into_raw(res) as i64)
})
}
#[no_mangle]
pub extern "system" fn Java_org_apache_comet_parquet_Native_readNextRecordBatch(
e: JNIEnv,
_jclass: JClass,
handle: jlong,
) -> jint {
try_unwrap_or_throw(&e, |mut env| {
let context = get_batch_context(handle)?;
let mut rows_read: i32 = 0;
let batch_stream = context.batch_stream.as_mut().unwrap();
let runtime = get_runtime();
loop {
let next_item = batch_stream.next();
let poll_batch: Poll<Option<datafusion::common::Result<RecordBatch>>> =
runtime.block_on(async { poll!(next_item) });
match poll_batch {
Poll::Ready(Some(batch)) => {
let batch = batch?;
rows_read = batch.num_rows() as i32;
context.current_batch = Some(batch);
context.reader_state = ParquetReaderState::Reading;
break;
}
Poll::Ready(None) => {
// EOF
update_comet_metric(
&mut env,
context.metrics_node.as_obj(),
&context.native_plan,
)?;
context.current_batch = None;
context.reader_state = ParquetReaderState::Complete;
break;
}
Poll::Pending => {
// TODO: (ARROW NATIVE): Just keeping polling??
// Ideally we want to yield to avoid consuming CPU while blocked on IO ??
continue;
}
}
}
Ok(rows_read)
})
}
#[no_mangle]
pub extern "system" fn Java_org_apache_comet_parquet_Native_currentColumnBatch(
e: JNIEnv,
_jclass: JClass,
handle: jlong,
column_idx: jint,
array_addr: jlong,
schema_addr: jlong,
) {
try_unwrap_or_throw(&e, |_env| {
let context = get_batch_context(handle)?;
let batch_reader = context
.current_batch
.as_mut()
.ok_or_else(|| CometError::Execution {
source: ExecutionError::GeneralError("There is no more data to read".to_string()),
});
let data = batch_reader?.column(column_idx as usize).into_data();
data.move_to_spark(array_addr, schema_addr)
.map_err(|e| e.into())
})
}
#[no_mangle]
pub extern "system" fn Java_org_apache_comet_parquet_Native_closeRecordBatchReader(
env: JNIEnv,
_jclass: JClass,
handle: jlong,
) {
try_unwrap_or_throw(&env, |_| {
unsafe {
let ctx = get_batch_context(handle)?;
let _ = Box::from_raw(ctx);
};
Ok(())
})
}