-
Notifications
You must be signed in to change notification settings - Fork 340
Expand file tree
/
Copy patherrors.rs
More file actions
1510 lines (1369 loc) · 58.2 KB
/
Copy patherrors.rs
File metadata and controls
1510 lines (1369 loc) · 58.2 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.
//! Common Parquet errors and macros.
use arrow::error::ArrowError;
use datafusion::common::DataFusionError;
use datafusion_comet_common::{SparkError, SparkErrorWithContext};
use jni::errors::{Exception, ToException};
use regex::Regex;
use std::{
any::Any,
convert,
fmt::Write,
panic::UnwindSafe,
result, str,
str::Utf8Error,
sync::{Arc, Mutex},
};
// This is just a pointer. We'll be returning it from our function. We
// can't return one of the objects with lifetime information because the
// lifetime checker won't let us.
use jni::sys::{jboolean, jbyte, jchar, jdouble, jfloat, jint, jlong, jobject, jshort};
use jni::objects::{Global, JThrowable};
use jni::{strings::JNIString, Env, EnvUnowned, Outcome};
use lazy_static::lazy_static;
use parquet::errors::ParquetError;
use thiserror::Error;
lazy_static! {
static ref PANIC_BACKTRACE: Arc<Mutex<Option<String>>> = Arc::new(Mutex::new(None));
}
/// Error returned during executing operators.
#[derive(thiserror::Error, Debug)]
pub enum ExecutionError {
/// Simple error
#[allow(dead_code)]
#[error("General execution error with reason: {0}.")]
GeneralError(String),
/// Error when deserializing an operator.
#[error("Fail to deserialize to native operator with reason: {0}.")]
DeserializeError(String),
/// Error when processing Arrow array.
#[error("Fail to process Arrow array with reason: {0}.")]
ArrowError(String),
/// DataFusion error
#[error("Error from DataFusion: {0}.")]
DataFusionError(String),
#[error("{class}: {msg}")]
JavaException {
class: String,
msg: String,
throwable: Global<JThrowable<'static>>,
},
}
#[derive(thiserror::Error, Debug)]
pub enum CometError {
#[error("Configuration Error: {0}")]
Config(String),
#[error("{0}")]
NullPointer(String),
#[error("Out of bounds{0}")]
IndexOutOfBounds(usize),
#[error("Comet Internal Error: {0}")]
Internal(String),
#[error(transparent)]
Arrow {
#[from]
source: ArrowError,
},
#[error(transparent)]
Parquet {
#[from]
source: ParquetError,
},
#[error(transparent)]
Expression {
#[from]
source: ExpressionError,
},
#[error(transparent)]
Execution {
#[from]
source: ExecutionError,
},
#[error(transparent)]
IO {
#[from]
source: std::io::Error,
},
#[error(transparent)]
NumberIntFormat {
#[from]
source: std::num::ParseIntError,
},
#[error(transparent)]
NumberFloatFormat {
#[from]
source: std::num::ParseFloatError,
},
#[error(transparent)]
BoolFormat {
#[from]
source: std::str::ParseBoolError,
},
#[error(transparent)]
Format {
#[from]
source: Utf8Error,
},
#[error(transparent)]
JNI {
#[from]
source: jni::errors::Error,
},
#[error("{msg}")]
Panic { msg: String },
#[error("{msg}")]
DataFusion {
msg: String,
#[source]
source: DataFusionError,
},
/// Wraps a SparkError directly, allowing Comet to throw Spark-compatible exceptions
/// that Spark would return
#[error(transparent)]
Spark(SparkError),
#[error("{class}: {msg}")]
JavaException {
class: String,
msg: String,
throwable: Global<JThrowable<'static>>,
},
}
pub fn init() {
std::panic::set_hook(Box::new(|panic_info| {
// Log the panic message and location to stderr so it is visible in CI logs
// even if the exception message is lost crossing the FFI boundary
eprintln!("Comet native panic: {panic_info}");
// Capture the backtrace for a panic
*PANIC_BACKTRACE.lock().unwrap() =
Some(std::backtrace::Backtrace::force_capture().to_string());
}));
}
/// Converts the results from `panic::catch_unwind` (e.g. a panic) to a `CometError`
impl convert::From<Box<dyn Any + Send>> for CometError {
fn from(e: Box<dyn Any + Send>) -> Self {
CometError::Panic {
msg: match e.downcast_ref::<&str>() {
Some(s) => s.to_string(),
None => match e.downcast_ref::<String>() {
Some(msg) => msg.to_string(),
None => "unknown panic".to_string(),
},
},
}
}
}
impl From<DataFusionError> for CometError {
fn from(value: DataFusionError) -> Self {
CometError::DataFusion {
msg: value.message().to_string(),
source: value,
}
}
}
impl From<CometError> for DataFusionError {
fn from(value: CometError) -> Self {
match value {
CometError::DataFusion { msg: _, source } => source,
// Preserve the original Java throwable (e.g. a SparkRuntimeException raised by Spark's
// own codegen inside the JVM UDF kernel) as an `External` error so it survives the trip
// back through DataFusion and can be re-thrown with its exact type at the JNI boundary.
// Flattening it to a string here would surface it as a generic CometNativeException.
value @ CometError::JavaException { .. } => DataFusionError::External(Box::new(value)),
_ => DataFusionError::Execution(value.to_string()),
}
}
}
impl From<CometError> for ParquetError {
fn from(value: CometError) -> Self {
match value {
CometError::Parquet { source } => source,
_ => ParquetError::General(value.to_string()),
}
}
}
impl From<CometError> for ExecutionError {
fn from(value: CometError) -> Self {
match value {
CometError::Execution { source } => source,
CometError::JavaException {
class,
msg,
throwable,
} => ExecutionError::JavaException {
class,
msg,
throwable,
},
_ => ExecutionError::GeneralError(value.to_string()),
}
}
}
impl From<prost::DecodeError> for ExpressionError {
fn from(error: prost::DecodeError) -> ExpressionError {
ExpressionError::Deserialize(error.to_string())
}
}
impl From<prost::UnknownEnumValue> for ExpressionError {
fn from(error: prost::UnknownEnumValue) -> ExpressionError {
ExpressionError::Deserialize(error.to_string())
}
}
impl From<prost::DecodeError> for ExecutionError {
fn from(error: prost::DecodeError) -> ExecutionError {
ExecutionError::DeserializeError(error.to_string())
}
}
impl From<prost::UnknownEnumValue> for ExecutionError {
fn from(error: prost::UnknownEnumValue) -> ExecutionError {
ExecutionError::DeserializeError(error.to_string())
}
}
impl From<ArrowError> for ExecutionError {
fn from(error: ArrowError) -> ExecutionError {
ExecutionError::ArrowError(error.to_string())
}
}
impl From<ArrowError> for ExpressionError {
fn from(error: ArrowError) -> ExpressionError {
ExpressionError::ArrowError(error.to_string())
}
}
impl From<ExpressionError> for ArrowError {
fn from(error: ExpressionError) -> ArrowError {
ArrowError::ComputeError(error.to_string())
}
}
impl From<DataFusionError> for ExecutionError {
fn from(value: DataFusionError) -> Self {
ExecutionError::DataFusionError(value.message().to_string())
}
}
impl From<ExecutionError> for DataFusionError {
fn from(value: ExecutionError) -> Self {
DataFusionError::Execution(value.to_string())
}
}
impl From<ExpressionError> for DataFusionError {
fn from(value: ExpressionError) -> Self {
DataFusionError::Execution(value.to_string())
}
}
impl jni::errors::ToException for CometError {
fn to_exception(&self) -> Exception {
match self {
CometError::IndexOutOfBounds(..) => Exception {
class: "java/lang/IndexOutOfBoundsException".to_string(),
msg: self.to_string(),
},
CometError::NullPointer(..) => Exception {
class: "java/lang/NullPointerException".to_string(),
msg: self.to_string(),
},
CometError::NumberIntFormat { source: s } => Exception {
class: "java/lang/NumberFormatException".to_string(),
msg: s.to_string(),
},
CometError::NumberFloatFormat { source: s } => Exception {
class: "java/lang/NumberFormatException".to_string(),
msg: s.to_string(),
},
CometError::IO { .. } => Exception {
class: "java/io/IOException".to_string(),
msg: self.to_string(),
},
CometError::Parquet { .. } => Exception {
class: "org/apache/comet/ParquetRuntimeException".to_string(),
msg: self.to_string(),
},
CometError::Spark(spark_err) => Exception {
class: spark_err.exception_class().to_string(),
msg: spark_err.to_string(),
},
_other => Exception {
class: "org/apache/comet/CometNativeException".to_string(),
msg: self.to_string(),
},
}
}
}
/// Error returned when there is an error during executing an expression.
#[derive(thiserror::Error, Debug)]
pub enum ExpressionError {
/// Simple error
#[error("General expression error with reason {0}.")]
General(String),
/// Deserialization error
#[error("Fail to deserialize to native expression with reason {0}.")]
Deserialize(String),
/// Evaluation error
#[error("Fail to evaluate native expression with reason {0}.")]
Evaluation(String),
/// Error when processing Arrow array.
#[error("Fail to process Arrow array with reason {0}.")]
ArrowError(String),
}
/// A specialized `Result` for Comet errors.
pub type CometResult<T> = result::Result<T, CometError>;
// ----------------------------------------------------------------------
// Convenient macros for different errors
#[macro_export]
macro_rules! general_err {
($fmt:expr, $($args:expr),*) => ($crate::errors::CometError::from(parquet::errors::ParquetError::General(format!($fmt, $($args),*))));
}
/// Returns the "default value" for a type. This is used for JNI code in order to facilitate
/// returning a value in cases where an exception is thrown. This value will never be used, as the
/// JVM will note the pending exception.
///
/// Default values are often some kind of initial value, identity value, or anything else that
/// may make sense as a default.
///
/// NOTE: We can't just use [Default] since both the trait and the object are defined in other
/// crates.
/// See [Rust Compiler Error Index - E0117](https://doc.rust-lang.org/error-index.html#E0117)
pub trait JNIDefault {
fn default() -> Self;
}
impl JNIDefault for jboolean {
fn default() -> jboolean {
false
}
}
impl JNIDefault for jbyte {
fn default() -> jbyte {
0
}
}
impl JNIDefault for jchar {
fn default() -> jchar {
0
}
}
impl JNIDefault for jdouble {
fn default() -> jdouble {
0.0
}
}
impl JNIDefault for jfloat {
fn default() -> jfloat {
0.0
}
}
impl JNIDefault for jint {
fn default() -> jint {
0
}
}
impl JNIDefault for jlong {
fn default() -> jlong {
0
}
}
/// The "default value" for all returned objects, such as [jstring], [jlongArray], etc.
impl JNIDefault for jobject {
fn default() -> jobject {
std::ptr::null_mut()
}
}
impl JNIDefault for jshort {
fn default() -> jshort {
0
}
}
impl JNIDefault for () {
fn default() {}
}
// Unwrap the result returned from `panic::catch_unwind` when `Ok`, otherwise throw a
// `RuntimeException` back to the calling Java. Since a return result is required, use `JNIDefault`
// to create a reasonable result. This returned default value will be ignored due to the exception.
pub fn unwrap_or_throw_default<T: JNIDefault>(
env: &mut Env,
result: std::result::Result<T, CometError>,
) -> T {
match result {
Ok(value) => value,
Err(err) => {
let backtrace = match err {
CometError::Panic { msg: _ } => PANIC_BACKTRACE.lock().unwrap().take(),
_ => None,
};
throw_exception(env, &err, backtrace);
T::default()
}
}
}
/// Payload recovered from a DataFusionError chain.
enum SparkPayload<'a> {
JavaException(&'a Global<JThrowable<'static>>),
WithContext(&'a SparkErrorWithContext),
Bare(&'a SparkError),
}
/// Recursively unwrap `DataFusionError::Context` and `DataFusionError::External` layers
/// until a Spark-typed payload is found, or return `None`.
///
/// DataFusion 53+ wraps errors with `.context(...)` which produces
/// `DataFusionError::Context(description, Box<inner>)`. The JNI bridge must look
/// through this extra layer — and through any doubly-nested `External` — to reach
/// the `SparkError` / `SparkErrorWithContext` that carries the structured exception.
fn extract_spark_payload(err: &DataFusionError) -> Option<SparkPayload<'_>> {
match err {
DataFusionError::External(e) => {
if let Some(CometError::JavaException { throwable, .. }) =
e.downcast_ref::<CometError>()
{
return Some(SparkPayload::JavaException(throwable));
}
if let Some(ctx) = e.downcast_ref::<SparkErrorWithContext>() {
return Some(SparkPayload::WithContext(ctx));
}
if let Some(spark) = e.downcast_ref::<SparkError>() {
return Some(SparkPayload::Bare(spark));
}
// Recurse: External may wrap another DataFusionError (double-wrapping).
if let Some(inner_df) = e.downcast_ref::<DataFusionError>() {
return extract_spark_payload(inner_df);
}
None
}
// DataFusion 53 adds context via `.context(description)` which wraps the error in
// Context(description, Box<inner>). Strip the context wrapper and recurse.
DataFusionError::Context(_, inner) => extract_spark_payload(inner),
_ => None,
}
}
fn throw_exception(env: &mut Env, error: &CometError, backtrace: Option<String>) {
// If there isn't already an exception?
if !env.exception_check() {
// ... then throw new exception
// Note: in jni 0.22.x, throw/throw_new return Err(JavaException) on success
// (to signal the pending exception to Rust callers via `?`). We discard the
// result here because we're in an error-handling path and just need the
// exception to be pending in the JVM.
let _ = match error {
CometError::JavaException {
class: _,
msg: _,
throwable,
} => env.throw(throwable),
CometError::Execution {
source:
ExecutionError::JavaException {
class: _,
msg: _,
throwable,
},
} => env.throw(throwable),
// Handle all DataFusion errors, including Context-wrapped chains.
// `extract_spark_payload` recurses through Context / nested External layers to
// find the Spark-typed payload, so this arm covers:
// - DataFusionError::External(SparkErrorWithContext) (normal path)
// - DataFusionError::External(SparkError) (no query context)
// - DataFusionError::Context(_, External(SparkError)) (DF53 context wrapping)
// - DataFusionError::External(External(SparkError)) (double wrapping)
CometError::DataFusion { msg: _, source } => {
match extract_spark_payload(source) {
Some(SparkPayload::JavaException(throwable)) => {
// A Java exception captured inside a JVM UDF kernel (e.g. Spark codegen
// raising INVALID_REGEXP_REPLACE). Re-throw the original throwable so
// callers see the exact Spark exception type.
env.throw(throwable)
}
Some(SparkPayload::WithContext(ctx)) => {
let json_message = ctx.to_json();
env.throw_new(
jni::jni_str!(
"org/apache/comet/exceptions/CometQueryExecutionException"
),
JNIString::new(json_message),
)
}
Some(SparkPayload::Bare(spark_error)) => {
throw_spark_error_as_json(env, spark_error)
}
None => {
if let Some(spark_error) = try_classify_file_read_error(source) {
throw_spark_error_as_json(env, &spark_error)
} else {
throw_generic_exception(env, error, backtrace)
}
}
}
}
// Handle direct SparkError - serialize to JSON
CometError::Spark(spark_error) => throw_spark_error_as_json(env, spark_error),
_ => throw_generic_exception(env, error, backtrace),
};
}
}
/// Generic fallback throw for an error that isn't a structured `SparkError`. Recognises a
/// file-not-found arriving through non-typed wrapping paths and duplicate-field errors; otherwise
/// throws the error's natural JVM exception (with the captured backtrace when available).
fn throw_generic_exception(
env: &mut Env,
error: &CometError,
backtrace: Option<String>,
) -> jni::errors::Result<()> {
let error_msg = error.to_string();
// A file-not-found that arrived through a non-typed wrapping path (the typed classification
// is handled by `try_classify_file_read_error`).
if error_msg.contains("not found") && error_msg.contains("No such file or directory") {
let spark_error = SparkError::FileNotFound { message: error_msg };
throw_spark_error_as_json(env, &spark_error)
} else if let Some(spark_error) = try_convert_duplicate_field_error(&error_msg) {
throw_spark_error_as_json(env, &spark_error)
} else {
let exception = error.to_exception();
match backtrace {
Some(backtrace_string) => env.throw_new(
JNIString::new(exception.class),
JNIString::new(to_stacktrace_string(exception.msg, backtrace_string).unwrap()),
),
_ => env.throw_new(
JNIString::new(exception.class),
JNIString::new(exception.msg),
),
}
}
}
/// Throws a CometQueryExecutionException with JSON-encoded SparkError
fn throw_spark_error_as_json(env: &mut Env, spark_error: &SparkError) -> jni::errors::Result<()> {
// Serialize error to JSON
let json_message = spark_error.to_json();
// Throw CometQueryExecutionException with JSON message
env.throw_new(
jni::jni_str!("org/apache/comet/exceptions/CometQueryExecutionException"),
JNIString::new(json_message),
)
}
/// Classify a `DataFusionError` as a per-file read failure by TYPED variant (not message text),
/// returning `SparkError::CannotReadFile` if so. This is the structured replacement for the
/// previous JVM-side substring matching on error prose.
///
/// A file-read failure is any of:
/// - `ParquetError` (corrupt footer/page, EOF, "failed to fill whole buffer", etc.)
/// - `ObjectStore` (truncated/empty/deleted file, range errors) -- `NotFound` carries the path
/// - `ArrowError`, when it wraps a `ParquetError` (the parquet reader surfaces some failures as
/// `ArrowError::ParquetError`)
/// - `IoError` (filesystem read failures)
///
/// `Context`/`Shared` wrappers are unwrapped recursively. Note we do NOT match `Execution`/
/// `Internal`/`External`-string or `object_store::Error::Generic`: those also carry non-file
/// errors (e.g. "Hdfs support is not enabled in this build") that must surface as-is.
///
/// `file_path` is populated from `object_store::Error::NotFound { path, .. }` when available;
/// otherwise it is left empty and the JVM side fills it from the per-task file list.
fn try_classify_file_read_error(error: &DataFusionError) -> Option<SparkError> {
use datafusion::common::DataFusionError as DFE;
match error {
// A pushed-down filter predicate that throws while the scan is reading (e.g. an ANSI
// divide-by-zero in a `WHERE`, now reachable by default since Scala UDF codegen dispatch
// landed) is an EXPRESSION failure, not a file-read failure, and must not be relabelled
// FAILED_READ_FILE. DataFusion's row filter returns such a failure as `ArrowError`
// (`ComputeError`), which the parquet reader then wraps as `ParquetError::External(<arrow>)`.
// Genuine corrupt/truncated/missing-file errors are `ParquetError::General`/`EOF`/
// `External(io|object_store)` and never wrap an `ArrowError`, so this TYPED check (not the
// error-message text, which DataFusion produces via `{:?}`) tells the two apart. Bail so the
// underlying error surfaces through the normal native-exception path.
DFE::ParquetError(pe) if parquet_external_wraps_arrow_error(pe) => None,
// A genuinely-missing file (object_store NotFound) is distinct from a corrupt/truncated
// one: Spark surfaces it as `readCurrentFileNotFoundError` ("It is possible the underlying
// files have been updated."), not `cannotReadFilesError`. The NotFound may arrive directly
// (`DFE::ObjectStore`) or wrapped by the parquet reader as `ParquetError::External(..)`, so
// inspect the source chain. Delta's CDC-after-VACUUM read depends on this distinction. The
// message fallback covers the cached reader, which flattens NotFound into a sourceless string.
DFE::ParquetError(pe)
if source_chain_has_object_store_not_found(pe.as_ref())
|| parquet_message_has_object_store_not_found(pe) =>
{
Some(SparkError::FileNotFound {
message: error.to_string(),
})
}
// NB: only ParquetError / ObjectStore / ArrowError(ParquetError) are treated as file reads.
// A bare `IoError` is intentionally NOT classified here: a scan surfaces read failures as a
// typed ParquetError or ObjectStore error, whereas an `IoError` can also originate from
// non-scan paths (spill, shuffle), which must not be relabelled FAILED_READ_FILE.
DFE::ParquetError(_) => Some(SparkError::CannotReadFile {
file_path: String::new(),
message: cannot_read_file_message(error),
}),
DFE::ObjectStore(e) => match e.as_ref() {
datafusion::object_store::Error::NotFound { .. } => Some(SparkError::FileNotFound {
message: error.to_string(),
}),
_ => Some(SparkError::CannotReadFile {
file_path: String::new(),
message: cannot_read_file_message(error),
}),
},
// The parquet reader sometimes surfaces a failure as ArrowError::ParquetError.
DFE::ArrowError(e, _) => match e.as_ref() {
ArrowError::ParquetError(_) if source_chain_has_object_store_not_found(e.as_ref()) => {
Some(SparkError::FileNotFound {
message: error.to_string(),
})
}
ArrowError::ParquetError(_) => Some(SparkError::CannotReadFile {
file_path: String::new(),
message: cannot_read_file_message(error),
}),
_ => None,
},
// Unwrap context/shared wrappers and re-classify the inner error.
DFE::Context(_, inner) => try_classify_file_read_error(inner),
DFE::Shared(inner) => try_classify_file_read_error(inner),
_ => None,
}
}
/// True if `pe` is a `ParquetError::External` wrapping an `ArrowError`. DataFusion's parquet row
/// filter returns a pushed-down predicate's evaluation failure as an `ArrowError` (e.g.
/// `ComputeError` for an ANSI divide-by-zero), which the parquet reader then surfaces as
/// `ParquetError::External(<arrow error>)`. That is an expression failure that merely happened
/// during the scan, not a corrupt/truncated/missing file -- genuine read failures are
/// `ParquetError::General`/`EOF`/`External(io|object_store)` and never wrap an `ArrowError`. Matching
/// on the wrapped type (rather than the message text DataFusion builds with `{:?}`) keeps the
/// distinction robust to upstream message changes.
fn parquet_external_wraps_arrow_error(pe: &ParquetError) -> bool {
matches!(pe, ParquetError::External(inner) if inner.downcast_ref::<ArrowError>().is_some())
}
/// True if `err` or any error in its `source()` chain is an `object_store` `NotFound` -- i.e. a
/// genuinely-missing file. Used to tell a missing file apart from a corrupt/truncated one: the
/// parquet reader wraps the object_store error as `ParquetError::External(..)`, so the typed
/// `NotFound` is only reachable by walking the source chain (we match the typed variant, never the
/// message text).
fn source_chain_has_object_store_not_found(err: &(dyn std::error::Error + 'static)) -> bool {
let mut current: Option<&(dyn std::error::Error + 'static)> = Some(err);
while let Some(e) = current {
if let Some(os) = e.downcast_ref::<datafusion::object_store::Error>() {
if matches!(os, datafusion::object_store::Error::NotFound { .. }) {
return true;
}
}
current = e.source();
}
false
}
/// Fallback for the one path that loses the typed error: DataFusion's `CachedParquetFileReader`
/// flattens a missing-file NotFound into `ParquetError::General(format!(...))` with no `source()` to
/// walk. Match `object_store`'s NotFound `Display`, which is identical across stores (so it holds on
/// S3), unlike the OS-specific "No such file or directory".
fn parquet_message_has_object_store_not_found(pe: &ParquetError) -> bool {
let msg = pe.to_string();
msg.contains("Object at location") && msg.contains("not found")
}
/// Build the message for a `CannotReadFile` error. parquet-rs reports a bad magic / unreadable
/// footer as `"Invalid Parquet file. Corrupt footer"`, whereas Spark's own reader (and Spark's
/// `ParquetQuerySuite`) phrase it as `"<file> is not a Parquet file"`. Append Spark's phrasing so
/// the cause carries it; the outer `cannotReadFilesError` wrapper ("Encountered error while reading
/// file …") is unchanged, so this composes with Spark's tests without changing the FAILED_READ_FILE
/// wrapping. Other read failures (corrupt pages, EOF, IO) keep their native message verbatim.
fn cannot_read_file_message(error: &DataFusionError) -> String {
let msg = error.to_string();
if msg.contains("Invalid Parquet file") && !msg.contains("is not a Parquet file") {
format!("{msg} (file is not a Parquet file)")
} else {
msg
}
}
/// Try to convert a DataFusion "Unable to get field named" error into a SparkError.
/// DataFusion produces this error when reading Parquet files with duplicate field names
/// in case-insensitive mode. For example, if a Parquet file has columns "B" and "b",
/// DataFusion may deduplicate them and report: Unable to get field named "b". Valid
/// fields: ["A", "B"]. When the requested field has a case-insensitive match among the
/// valid fields, we convert this to Spark's _LEGACY_ERROR_TEMP_2093 error.
fn try_convert_duplicate_field_error(error_msg: &str) -> Option<SparkError> {
// Match: Schema error: Unable to get field named "X". Valid fields: [...]
lazy_static! {
static ref FIELD_RE: Regex =
Regex::new(r#"Unable to get field named "([^"]+)"\. Valid fields: \[(.+)\]"#).unwrap();
}
if let Some(caps) = FIELD_RE.captures(error_msg) {
let requested_field = caps.get(1)?.as_str();
let requested_lower = requested_field.to_lowercase();
// Parse field names from the Valid fields list: ["A", "B"] or [A, B, b]
let valid_fields_raw = caps.get(2)?.as_str();
let all_fields: Vec<String> = valid_fields_raw
.split(',')
.map(|s| s.trim().trim_matches('"').to_string())
.collect();
// Find fields that match case-insensitively
let mut matched: Vec<String> = all_fields
.into_iter()
.filter(|f| f.to_lowercase() == requested_lower)
.collect();
// Need at least one case-insensitive match to treat this as a duplicate field error.
// DataFusion may deduplicate columns case-insensitively, so the valid fields list
// might contain only one variant (e.g. "B" when file has both "B" and "b").
// If requested field differs from the match, both existed in the original file.
if matched.is_empty() {
return None;
}
// Add the requested field name if it's not already in the list (different case)
if !matched.iter().any(|f| f == requested_field) {
matched.push(requested_field.to_string());
}
let required_field_name = requested_field.to_string();
let matched_fields = format!("[{}]", matched.join(", "));
Some(SparkError::DuplicateFieldCaseInsensitive {
required_field_name,
matched_fields,
})
} else {
None
}
}
#[derive(Debug, Error)]
enum StacktraceError {
#[error("Unable to initialize message: {0}")]
Message(String),
#[error("Unable to initialize backtrace regex: {0}")]
Regex(#[from] regex::Error),
#[error("Required field missing: {0}")]
#[allow(non_camel_case_types)]
Required_Field(String),
#[error("Unable to format stacktrace element: {0}")]
Element(#[from] std::fmt::Error),
}
fn to_stacktrace_string(msg: String, backtrace_string: String) -> Result<String, StacktraceError> {
let mut res = String::new();
write!(&mut res, "{msg}").map_err(|error| StacktraceError::Message(error.to_string()))?;
// Use multi-line mode and named capture groups to identify the following stacktrace fields:
// - dc = declaredClass
// - mn = methodName
// - fn = fileName (optional)
// - line = file line number (optional)
// - col = file col number within the line (optional)
let re = Regex::new(
r"(?m)^\s*\d+: (?<dc>.*?)(?<mn>[^:]+)\n(\s*at\s+(?<fn>[^:]+):(?<line>\d+):(?<col>\d+)$)?",
)?;
for c in re.captures_iter(backtrace_string.as_str()) {
write!(
&mut res,
"\n at {}{}({}:{})",
c.name("dc")
.ok_or_else(|| StacktraceError::Required_Field("declared class".to_string()))?
.as_str(),
c.name("mn")
.ok_or_else(|| StacktraceError::Required_Field("method name".to_string()))?
.as_str(),
// There are internal calls within the backtrace that don't provide file information
c.name("fn").map(|m| m.as_str()).unwrap_or("__internal__"),
c.name("line")
.map(|m| m.as_str().parse().expect("numeric line number"))
.unwrap_or(0)
)?;
}
Ok(res)
}
// It is currently undefined behavior to unwind from Rust code into foreign code, so we can wrap
// our JNI functions and turn these panics into a `RuntimeException`.
pub fn try_unwrap_or_throw<T, F>(env: &EnvUnowned, f: F) -> T
where
T: JNIDefault,
F: FnOnce(&mut Env) -> Result<T, CometError> + UnwindSafe,
{
let raw = env.as_raw();
let mut env1 = unsafe { EnvUnowned::from_raw(raw) };
match env1.with_env(f).into_outcome() {
Outcome::Ok(value) => value,
Outcome::Err(err) => {
let mut guard = unsafe { jni::AttachGuard::from_unowned(raw) };
unwrap_or_throw_default(guard.borrow_env_mut(), Err(err))
}
Outcome::Panic(payload) => {
let mut guard = unsafe { jni::AttachGuard::from_unowned(raw) };
unwrap_or_throw_default(guard.borrow_env_mut(), Err(CometError::from(payload)))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::{
fs::File,
io,
io::Read,
path::PathBuf,
sync::{Arc, Once},
};
use jni::{
objects::{JClass, JIntArray, JString, JThrowable},
sys::{jintArray, jstring},
EnvUnowned, InitArgsBuilder, JNIVersion, JavaVM,
};
use assertables::assert_starts_with;
pub fn jvm() -> &'static Arc<JavaVM> {
static mut JVM: Option<Arc<JavaVM>> = None;
static INIT: Once = Once::new();
// Capture panic backtraces
init();
INIT.call_once(|| {
// Add comet-common classes to the classpath so we can find the Comet exception
// classes (CometNativeException, CometQueryExecutionException, etc.).
let mut common_classes = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
common_classes.push("../../common/target/classes");
let mut class_path = common_classes
.as_path()
.to_str()
.expect("common classes as an str")
.to_string();
class_path.insert_str(0, "-Djava.class.path=");
// Build the VM properties
let jvm_args = InitArgsBuilder::new()
// Pass the JNI API version (default is 8)
.version(JNIVersion::V1_8)
// You can additionally pass any JVM options (standard, like a system property,
// or VM-specific).
// Here we enable some extra JNI checks useful during development
.option("-Xcheck:jni")
.option(class_path.as_str())
.build()
.unwrap_or_else(|e| panic!("{e:#?}"));
let jvm = JavaVM::new(jvm_args).unwrap_or_else(|e| panic!("{e:#?}"));
#[allow(static_mut_refs)]
unsafe {
JVM = Some(Arc::new(jvm));
}
});
#[allow(static_mut_refs)]
unsafe {
JVM.as_ref().unwrap()
}
}
#[test]
#[cfg_attr(miri, ignore)] // miri can't call foreign function `dlopen`
pub fn error_from_panic() {
jvm()
.attach_current_thread(|env| -> jni::errors::Result<()> {
let env_unowned = unsafe { EnvUnowned::from_raw(env.get_raw()) };
try_unwrap_or_throw(&env_unowned, |_| -> CometResult<()> {
panic!("oops!");
});
assert_pending_java_exception_detailed(
env,
Some("java/lang/RuntimeException"),
Some("oops!"),
);
Ok(())
})
.unwrap();
}
// Verify that functions that return an object are handled correctly. This is basically
// a test of the "happy path".
#[test]
#[cfg_attr(miri, ignore)] // miri can't call foreign function `dlopen`
pub fn object_result() {
jvm()
.attach_current_thread(|env| -> jni::errors::Result<()> {
let clazz = env.find_class(jni::jni_str!("java/lang/Object")).unwrap();
let input = env.new_string("World").unwrap();
let actual = unsafe {
Java_Errors_hello(&EnvUnowned::from_raw(env.get_raw()), clazz, input)
};
let actual_s = unsafe { JString::from_raw(env, actual) };
let actual_string = actual_s.try_to_string(env).unwrap();
assert_eq!("Hello, World!", actual_string);
Ok(())
})
.unwrap();
}
// Verify that functions that return an native time are handled correctly. This is basically
// a test of the "happy path".
#[test]
#[cfg_attr(miri, ignore)] // miri can't call foreign function `dlopen`
pub fn jlong_result() {
jvm()
.attach_current_thread(|env| -> jni::errors::Result<()> {
// Class java.lang.object is just a stand-in
let class = env.find_class(jni::jni_str!("java/lang/Object")).unwrap();
let a: jlong = 6;
let b: jlong = 3;
let actual =
unsafe { Java_Errors_div(&EnvUnowned::from_raw(env.get_raw()), class, a, b) };
assert_eq!(2, actual);
Ok(())
})
.unwrap();
}
// Verify that functions that return an array can handle throwing exceptions. The test