This is an automated email from the ASF dual-hosted git repository.

gengliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/master by this push:
     new 7f9c226e098 [SPARK-42057][SQL][PROTOBUF] Fix how exception is handled 
in error reporting
7f9c226e098 is described below

commit 7f9c226e0981fe2e9feb641fceff074f87292287
Author: Raghu Angadi <[email protected]>
AuthorDate: Fri Jan 13 20:30:04 2023 -0800

    [SPARK-42057][SQL][PROTOBUF] Fix how exception is handled in error reporting
    
    ### What changes were proposed in this pull request?
    
    Protobuf connector related error handlers incorrectly report the exception. 
This is makes it hard for users to see actual issue. E.g. if there is a 
`FileNotFoundException` these error handlers use pass `exception.getCause()` 
rather than passing `exception`. As result, we lose the information that it was 
a `FileNotFoundException`
    
    This PR fixes that.
    
    ### Why are the changes needed?
     This improves error reporting.
    
    ### Does this PR introduce _any_ user-facing change?
    No.
    
    ### How was this patch tested?
     -  A new test is added.
    
    Closes #39536 from rangadi/fix-exception-cause.
    
    Authored-by: Raghu Angadi <[email protected]>
    Signed-off-by: Gengliang Wang <[email protected]>
---
 .../spark/sql/protobuf/utils/ProtobufUtils.scala   |  2 +-
 .../sql/protobuf/ProtobufFunctionsSuite.scala      | 22 ++++++++++++++++++++++
 .../spark/sql/errors/QueryCompilationErrors.scala  | 12 ++++++------
 3 files changed, 29 insertions(+), 7 deletions(-)

diff --git 
a/connector/protobuf/src/main/scala/org/apache/spark/sql/protobuf/utils/ProtobufUtils.scala
 
b/connector/protobuf/src/main/scala/org/apache/spark/sql/protobuf/utils/ProtobufUtils.scala
index 0ecb728060d..49313a3ce91 100644
--- 
a/connector/protobuf/src/main/scala/org/apache/spark/sql/protobuf/utils/ProtobufUtils.scala
+++ 
b/connector/protobuf/src/main/scala/org/apache/spark/sql/protobuf/utils/ProtobufUtils.scala
@@ -229,7 +229,7 @@ private[sql] object ProtobufUtils extends Logging {
       fileDescriptorSet = DescriptorProtos.FileDescriptorSet.parseFrom(dscFile)
     } catch {
       case ex: InvalidProtocolBufferException =>
-        throw QueryCompilationErrors.descrioptorParseError(descFilePath, ex)
+        throw QueryCompilationErrors.descriptorParseError(descFilePath, ex)
       case ex: IOException =>
         throw 
QueryCompilationErrors.cannotFindDescriptorFileError(descFilePath, ex)
     }
diff --git 
a/connector/protobuf/src/test/scala/org/apache/spark/sql/protobuf/ProtobufFunctionsSuite.scala
 
b/connector/protobuf/src/test/scala/org/apache/spark/sql/protobuf/ProtobufFunctionsSuite.scala
index 79d7f96414b..30b38eafd78 100644
--- 
a/connector/protobuf/src/test/scala/org/apache/spark/sql/protobuf/ProtobufFunctionsSuite.scala
+++ 
b/connector/protobuf/src/test/scala/org/apache/spark/sql/protobuf/ProtobufFunctionsSuite.scala
@@ -1248,6 +1248,28 @@ class ProtobufFunctionsSuite extends QueryTest with 
SharedSparkSession with Prot
     testFromProtobufWithOptions(df, expectedDfTwo, optionsTwo)
   }
 
+  test("Verify exceptions are correctly propagated with errors") {
+    // This triggers an query compilation error and ensures that original 
exception is
+    // also included in in the exception.
+
+    val invalidDescPath = "/non/existent/path.desc"
+
+    val ex = intercept[AnalysisException] {
+      Seq(Array[Byte]())
+        .toDF()
+        .select(
+          functions.from_protobuf($"value", "SomeMessage", invalidDescPath)
+        ).collect()
+    }
+    checkError(
+      ex,
+      errorClass = "PROTOBUF_DESCRIPTOR_FILE_NOT_FOUND",
+      parameters = Map("filePath" -> "/non/existent/path.desc")
+    )
+    assert(ex.getCause != null)
+    assert(ex.getCause.getMessage.matches(".*No such file.*"), 
ex.getCause.getMessage())
+  }
+
   def testFromProtobufWithOptions(
     df: DataFrame,
     expectedDf: DataFrame,
diff --git 
a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala
 
b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala
index 25005a1f609..ff456ba850b 100644
--- 
a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala
+++ 
b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryCompilationErrors.scala
@@ -3239,7 +3239,7 @@ private[sql] object QueryCompilationErrors extends 
QueryErrorsBase {
       messageParameters = Map(
         "protobufType" -> protobufType,
         "toType" -> toSQLType(sqlType)),
-      cause = Option(cause.getCause))
+      cause = Option(cause))
   }
 
   def cannotConvertSqlTypeToProtobufError(
@@ -3251,7 +3251,7 @@ private[sql] object QueryCompilationErrors extends 
QueryErrorsBase {
       messageParameters = Map(
         "protobufType" -> protobufType,
         "toType" -> toSQLType(sqlType)),
-      cause = Option(cause.getCause))
+      cause = Option(cause))
   }
 
   def protobufTypeUnsupportedYetError(protobufType: String): Throwable = {
@@ -3301,25 +3301,25 @@ private[sql] object QueryCompilationErrors extends 
QueryErrorsBase {
       messageParameters = Map("messageName" -> messageName))
   }
 
-  def descrioptorParseError(descFilePath: String, cause: Throwable): Throwable 
= {
+  def descriptorParseError(descFilePath: String, cause: Throwable): Throwable 
= {
     new AnalysisException(
       errorClass = "CANNOT_PARSE_PROTOBUF_DESCRIPTOR",
       messageParameters = Map("descFilePath" -> descFilePath),
-      cause = Option(cause.getCause))
+      cause = Option(cause))
   }
 
   def cannotFindDescriptorFileError(filePath: String, cause: Throwable): 
Throwable = {
     new AnalysisException(
       errorClass = "PROTOBUF_DESCRIPTOR_FILE_NOT_FOUND",
       messageParameters = Map("filePath" -> filePath),
-      cause = Option(cause.getCause))
+      cause = Option(cause))
   }
 
   def failedParsingDescriptorError(descFilePath: String, cause: Throwable): 
Throwable = {
     new AnalysisException(
       errorClass = "CANNOT_CONSTRUCT_PROTOBUF_DESCRIPTOR",
       messageParameters = Map("descFilePath" -> descFilePath),
-      cause = Option(cause.getCause))
+      cause = Option(cause))
   }
 
   def foundRecursionInProtobufSchema(fieldDescriptor: String): Throwable = {


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

Reply via email to