mbutrovich commented on code in PR #304:
URL: https://github.com/apache/datafusion-spark/pull/304#discussion_r4605598682


##########
crates/spark/src/error.rs:
##########
@@ -0,0 +1,293 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for 
additional information regarding copyright ownership
+// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) 
under one or more contributor license agreements
+
+//! Typed Spark errors.
+//!
+//! A [`SparkError`] renders the message Spark raises for the same condition,
+//! including its error class (such as `[CAST_OVERFLOW]`). It reaches callers 
as
+//! [`DataFusionError::External`], so an engine that maps errors back to Spark
+//! exceptions, such as Comet, can recover the variant with
+//! [`SparkError::find`] instead of parsing the message.
+
+use std::error::Error;
+use std::fmt::{Display, Formatter};
+
+use datafusion_common::DataFusionError;
+
+/// An error that Spark raises with a specific error class.
+///
+/// The messages follow Spark 4.x's
+/// 
[`error-conditions.json`](https://github.com/apache/spark/blob/v4.2.0/common/utils/src/main/resources/error/error-conditions.json).
+/// Spark 3.x appended `If necessary set "spark.sql.ansi.enabled" to "false" to
+/// bypass this error.` to the `CAST_INVALID_INPUT` and `CAST_OVERFLOW`
+/// messages, and 4.0 dropped it.
+/// Spark quotes values and types in the forms its `toSQLValue` and `toSQLType`
+/// produce. The caller passes them already formatted, such as `2147483648L`,
+/// `NaN`, or `'abc'` from [`SparkError::sql_string`], and `"BIGINT"` without 
the
+/// quotes, since only the caller knows the Spark type. Every message ends with
+/// the error's SQLSTATE, as Spark's do.
+///
+/// The variants are `#[non_exhaustive]` so that fields can be added later.
+/// Code outside this crate matches them with `..`.
+#[derive(Debug, Clone, PartialEq, Eq)]
+#[non_exhaustive]
+pub enum SparkError {
+    /// `CAST_INVALID_INPUT`: a value that cannot be parsed as the target type,
+    /// in ANSI mode. Spark raises it as a `SparkNumberFormatException`.
+    #[non_exhaustive]
+    CastInvalidValue {
+        value: String,
+        from_type: String,
+        to_type: String,
+    },
+    /// The same error class and message as [`SparkError::CastInvalidValue`],
+    /// for a cast to a date or timestamp. Spark raises it as a
+    /// `SparkDateTimeException` rather than a `SparkNumberFormatException`.
+    #[non_exhaustive]
+    InvalidInputInCastToDatetime {
+        value: String,
+        from_type: String,
+        to_type: String,
+    },
+    /// `CAST_OVERFLOW`: a value outside the range of the target type, in ANSI
+    /// mode.
+    #[non_exhaustive]
+    CastOverflow {
+        value: String,
+        from_type: String,
+        to_type: String,
+    },
+    /// `NUMERIC_VALUE_OUT_OF_RANGE.WITH_SUGGESTION`: a value that does not fit
+    /// the target decimal precision and scale, in ANSI mode.
+    #[non_exhaustive]
+    NumericValueOutOfRange {
+        value: String,
+        precision: u8,
+        scale: i8,
+    },
+    /// `NUMERIC_OUT_OF_SUPPORTED_RANGE`: a string with more than 38 digits 
cast
+    /// to a decimal.
+    #[non_exhaustive]
+    NumericOutOfSupportedRange { value: String },
+}
+
+impl SparkError {
+    /// The Spark error class, such as `CAST_OVERFLOW`.
+    pub fn error_class(&self) -> &'static str {
+        match self {
+            Self::CastInvalidValue { .. } | Self::InvalidInputInCastToDatetime 
{ .. } => {
+                "CAST_INVALID_INPUT"
+            }
+            Self::CastOverflow { .. } => "CAST_OVERFLOW",
+            Self::NumericValueOutOfRange { .. } => {
+                "NUMERIC_VALUE_OUT_OF_RANGE.WITH_SUGGESTION"
+            }
+            Self::NumericOutOfSupportedRange { .. } => 
"NUMERIC_OUT_OF_SUPPORTED_RANGE",
+        }
+    }
+
+    /// The SQLSTATE that Spark reports with the error class.
+    pub fn sql_state(&self) -> &'static str {
+        match self {
+            Self::CastInvalidValue { .. } | Self::InvalidInputInCastToDatetime 
{ .. } => {
+                "22018"
+            }
+            Self::CastOverflow { .. }
+            | Self::NumericValueOutOfRange { .. }
+            | Self::NumericOutOfSupportedRange { .. } => "22003",
+        }
+    }
+
+    /// A string value as Spark's `toSQLValue` renders it in a message: quoted,
+    /// with backslashes and quotes escaped, so `a'b` becomes `'a\'b'`.
+    pub fn sql_string(value: &str) -> String {
+        format!("'{}'", value.replace('\\', "\\\\").replace('\'', "\\'"))
+    }

Review Comment:
   Now that the variants are `#[non_exhaustive]`, code outside this crate can't 
construct them, so it has no use for `sql_string`. Only the tests call it 
today. Could it be `pub(crate)`? Since a `pub(crate)` function with no caller 
would trip `dead_code`, it may be simplest to add it with the string-cast port 
that calls it, and have these tests pass `'abc'` directly. As a `pub` item it 
becomes permanent API once released.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to