datafusion_comet_spark_expr/
error.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use arrow::error::ArrowError;
19use datafusion::common::DataFusionError;
20
21#[derive(thiserror::Error, Debug)]
22pub enum SparkError {
23    // Note that this message format is based on Spark 3.4 and is more detailed than the message
24    // returned by Spark 3.3
25    #[error("[CAST_INVALID_INPUT] The value '{value}' of the type \"{from_type}\" cannot be cast to \"{to_type}\" \
26        because it is malformed. Correct the value as per the syntax, or change its target type. \
27        Use `try_cast` to tolerate malformed input and return NULL instead. If necessary \
28        set \"spark.sql.ansi.enabled\" to \"false\" to bypass this error.")]
29    CastInvalidValue {
30        value: String,
31        from_type: String,
32        to_type: String,
33    },
34
35    #[error("[NUMERIC_VALUE_OUT_OF_RANGE] {value} cannot be represented as Decimal({precision}, {scale}). If necessary set \"spark.sql.ansi.enabled\" to \"false\" to bypass this error, and return NULL instead.")]
36    NumericValueOutOfRange {
37        value: String,
38        precision: u8,
39        scale: i8,
40    },
41
42    #[error("[CAST_OVERFLOW] The value {value} of the type \"{from_type}\" cannot be cast to \"{to_type}\" \
43        due to an overflow. Use `try_cast` to tolerate overflow and return NULL instead. If necessary \
44        set \"spark.sql.ansi.enabled\" to \"false\" to bypass this error.")]
45    CastOverFlow {
46        value: String,
47        from_type: String,
48        to_type: String,
49    },
50
51    #[error("[ARITHMETIC_OVERFLOW] {from_type} overflow. If necessary set \"spark.sql.ansi.enabled\" to \"false\" to bypass this error.")]
52    ArithmeticOverflow { from_type: String },
53
54    #[error("ArrowError: {0}.")]
55    Arrow(ArrowError),
56
57    #[error("InternalError: {0}.")]
58    Internal(String),
59}
60
61pub type SparkResult<T> = Result<T, SparkError>;
62
63impl From<ArrowError> for SparkError {
64    fn from(value: ArrowError) -> Self {
65        SparkError::Arrow(value)
66    }
67}
68
69impl From<SparkError> for DataFusionError {
70    fn from(value: SparkError) -> Self {
71        DataFusionError::External(Box::new(value))
72    }
73}