You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by ma...@apache.org on 2022/06/27 06:36:13 UTC

[spark] branch master updated: [SPARK-39432][SQL] Return ELEMENT_AT_BY_INDEX_ZERO from element_at(*, 0)

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

maxgekk 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 a7177f6f842 [SPARK-39432][SQL] Return ELEMENT_AT_BY_INDEX_ZERO from element_at(*, 0)
a7177f6f842 is described below

commit a7177f6f842270e75c6515f65aa667c8be4aab4b
Author: panbingkun <pb...@gmail.com>
AuthorDate: Mon Jun 27 09:35:59 2022 +0300

    [SPARK-39432][SQL] Return ELEMENT_AT_BY_INDEX_ZERO from element_at(*, 0)
    
    ## What changes were proposed in this pull request?
    SELECT element_at(array('a', 'b', 'c'), index) FROM VALUES(0), (2) AS T(index);
    This should roll into ELEMENT_AT_BY_INDEX_ZERO.
    
    ### Why are the changes needed?
    element_at(*, 0) should return ELEMENT_AT_BY_INDEX_ZERO to unify errors representation for users.
    
    ### Does this PR introduce any user-facing change?
    Yes, it changes an user-facing error message.
    
    ### How was this patch tested?
    Add new UT & By running manual test:
    ```scala
    > sql("SELECT element_at(array('a', 'b', 'c'), index) FROM VALUES(0), (2) AS T(index)").collect()
    ```
    throws error:
    ```
    19:20:13.012 ERROR org.apache.spark.executor.Executor: Exception in task 0.0 in stage 0.0 (TID 0)
    org.apache.spark.SparkRuntimeException: [ELEMENT_AT_BY_INDEX_ZERO] The index 0 is invalid. The indexes can be < 0 and > 0 (the first element has index 1).
            at org.apache.spark.sql.errors.QueryExecutionErrors$.elementAtByIndexZeroError(QueryExecutionErrors.scala:1203)
            at org.apache.spark.sql.errors.QueryExecutionErrors.elementAtByIndexZeroError(QueryExecutionErrors.scala)
    ...
    ```
    
    Closes #36855 from panbingkun/SPARK-39432.
    
    Authored-by: panbingkun <pb...@gmail.com>
    Signed-off-by: Max Gekk <ma...@gmail.com>
---
 core/src/main/resources/error/error-classes.json               |  5 +++++
 .../spark/sql/catalyst/expressions/collectionOperations.scala  |  4 ++--
 .../org/apache/spark/sql/errors/QueryExecutionErrors.scala     |  5 +++--
 .../sql/catalyst/expressions/CollectionExpressionsSuite.scala  |  5 -----
 .../src/test/resources/sql-tests/results/ansi/array.sql.out    |  8 ++++----
 .../resources/sql-tests/results/ansi/string-functions.sql.out  |  4 ++--
 .../resources/sql-tests/results/ansi/try_element_at.sql.out    |  4 ++--
 sql/core/src/test/resources/sql-tests/results/array.sql.out    |  4 ++--
 .../test/resources/sql-tests/results/string-functions.sql.out  |  4 ++--
 .../test/resources/sql-tests/results/try_element_at.sql.out    |  4 ++--
 .../apache/spark/sql/errors/QueryExecutionErrorsSuite.scala    | 10 ++++++++++
 11 files changed, 34 insertions(+), 23 deletions(-)

diff --git a/core/src/main/resources/error/error-classes.json b/core/src/main/resources/error/error-classes.json
index f9257b6c21b..5395ab0e26e 100644
--- a/core/src/main/resources/error/error-classes.json
+++ b/core/src/main/resources/error/error-classes.json
@@ -70,6 +70,11 @@
     ],
     "sqlState" : "23000"
   },
+  "ELEMENT_AT_BY_INDEX_ZERO" : {
+    "message" : [
+      "The index 0 is invalid. An index shall be either < 0 or > 0 (the first element has index 1)."
+    ]
+  },
   "FAILED_EXECUTE_UDF" : {
     "message" : [
       "Failed to execute user defined function (<functionName>: (<signature>) => <result>)"
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala
index eac2c915d91..c74f8e1a685 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala
@@ -2187,7 +2187,7 @@ case class ElementAt(
           }
         } else {
           val idx = if (index == 0) {
-            throw QueryExecutionErrors.sqlArrayIndexNotStartAtOneError()
+            throw QueryExecutionErrors.elementAtByIndexZeroError()
           } else if (index > 0) {
             index - 1
           } else {
@@ -2243,7 +2243,7 @@ case class ElementAt(
              |  $indexOutOfBoundBranch
              |} else {
              |  if ($index == 0) {
-             |    throw QueryExecutionErrors.sqlArrayIndexNotStartAtOneError();
+             |    throw QueryExecutionErrors.elementAtByIndexZeroError();
              |  } else if ($index > 0) {
              |    $index--;
              |  } else {
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala
index 657c7c74b9e..bfdf9942a57 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/errors/QueryExecutionErrors.scala
@@ -1199,8 +1199,9 @@ private[sql] object QueryExecutionErrors extends QueryErrorsBase {
       "length must be greater than or equal to 0.")
   }
 
-  def sqlArrayIndexNotStartAtOneError(): ArrayIndexOutOfBoundsException = {
-    new ArrayIndexOutOfBoundsException("SQL array indices start at 1")
+  def elementAtByIndexZeroError(): SparkRuntimeException = {
+    new SparkRuntimeException(errorClass = "ELEMENT_AT_BY_INDEX_ZERO",
+      messageParameters = Array.empty)
   }
 
   def concatArraysWithElementsExceedLimitError(numberOfElements: Long): Throwable = {
diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CollectionExpressionsSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CollectionExpressionsSuite.scala
index a8c4b16c7a0..c14a0839b1a 100644
--- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CollectionExpressionsSuite.scala
+++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CollectionExpressionsSuite.scala
@@ -2345,11 +2345,6 @@ class CollectionExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper
         } else {
           checkEvaluation(expr, null)
         }
-
-        // SQL array indices start at 1 exception throws for both mode.
-        expr = ElementAt(array, Literal(0))
-        val errMsg = "SQL array indices start at 1"
-        checkExceptionInExpression[Exception](expr, errMsg)
       }
     }
   }
diff --git a/sql/core/src/test/resources/sql-tests/results/ansi/array.sql.out b/sql/core/src/test/resources/sql-tests/results/ansi/array.sql.out
index 6e2d2e478bc..8152dc25217 100644
--- a/sql/core/src/test/resources/sql-tests/results/ansi/array.sql.out
+++ b/sql/core/src/test/resources/sql-tests/results/ansi/array.sql.out
@@ -188,8 +188,8 @@ select element_at(array(1, 2, 3), 0)
 -- !query schema
 struct<>
 -- !query output
-java.lang.ArrayIndexOutOfBoundsException
-SQL array indices start at 1
+org.apache.spark.SparkRuntimeException
+[ELEMENT_AT_BY_INDEX_ZERO] The index 0 is invalid. An index shall be either < 0 or > 0 (the first element has index 1).
 
 
 -- !query
@@ -378,8 +378,8 @@ select element_at(array(1, 2, 3), 0)
 -- !query schema
 struct<>
 -- !query output
-java.lang.ArrayIndexOutOfBoundsException
-SQL array indices start at 1
+org.apache.spark.SparkRuntimeException
+[ELEMENT_AT_BY_INDEX_ZERO] The index 0 is invalid. An index shall be either < 0 or > 0 (the first element has index 1).
 
 
 -- !query
diff --git a/sql/core/src/test/resources/sql-tests/results/ansi/string-functions.sql.out b/sql/core/src/test/resources/sql-tests/results/ansi/string-functions.sql.out
index 899135610fc..ffa600f66fd 100644
--- a/sql/core/src/test/resources/sql-tests/results/ansi/string-functions.sql.out
+++ b/sql/core/src/test/resources/sql-tests/results/ansi/string-functions.sql.out
@@ -174,8 +174,8 @@ SELECT split_part('11.12.13', '.', 0)
 -- !query schema
 struct<>
 -- !query output
-java.lang.ArrayIndexOutOfBoundsException
-SQL array indices start at 1
+org.apache.spark.SparkRuntimeException
+[ELEMENT_AT_BY_INDEX_ZERO] The index 0 is invalid. An index shall be either < 0 or > 0 (the first element has index 1).
 
 
 -- !query
diff --git a/sql/core/src/test/resources/sql-tests/results/ansi/try_element_at.sql.out b/sql/core/src/test/resources/sql-tests/results/ansi/try_element_at.sql.out
index c1c0980ea52..defac2ea5d6 100644
--- a/sql/core/src/test/resources/sql-tests/results/ansi/try_element_at.sql.out
+++ b/sql/core/src/test/resources/sql-tests/results/ansi/try_element_at.sql.out
@@ -4,8 +4,8 @@ SELECT try_element_at(array(1, 2, 3), 0)
 -- !query schema
 struct<>
 -- !query output
-java.lang.ArrayIndexOutOfBoundsException
-SQL array indices start at 1
+org.apache.spark.SparkRuntimeException
+[ELEMENT_AT_BY_INDEX_ZERO] The index 0 is invalid. An index shall be either < 0 or > 0 (the first element has index 1).
 
 
 -- !query
diff --git a/sql/core/src/test/resources/sql-tests/results/array.sql.out b/sql/core/src/test/resources/sql-tests/results/array.sql.out
index 78f4c85fa05..25bc001d3d6 100644
--- a/sql/core/src/test/resources/sql-tests/results/array.sql.out
+++ b/sql/core/src/test/resources/sql-tests/results/array.sql.out
@@ -180,8 +180,8 @@ select element_at(array(1, 2, 3), 0)
 -- !query schema
 struct<>
 -- !query output
-java.lang.ArrayIndexOutOfBoundsException
-SQL array indices start at 1
+org.apache.spark.SparkRuntimeException
+[ELEMENT_AT_BY_INDEX_ZERO] The index 0 is invalid. An index shall be either < 0 or > 0 (the first element has index 1).
 
 
 -- !query
diff --git a/sql/core/src/test/resources/sql-tests/results/string-functions.sql.out b/sql/core/src/test/resources/sql-tests/results/string-functions.sql.out
index faf6165a888..0ac05bb29c9 100644
--- a/sql/core/src/test/resources/sql-tests/results/string-functions.sql.out
+++ b/sql/core/src/test/resources/sql-tests/results/string-functions.sql.out
@@ -166,8 +166,8 @@ SELECT split_part('11.12.13', '.', 0)
 -- !query schema
 struct<>
 -- !query output
-java.lang.ArrayIndexOutOfBoundsException
-SQL array indices start at 1
+org.apache.spark.SparkRuntimeException
+[ELEMENT_AT_BY_INDEX_ZERO] The index 0 is invalid. An index shall be either < 0 or > 0 (the first element has index 1).
 
 
 -- !query
diff --git a/sql/core/src/test/resources/sql-tests/results/try_element_at.sql.out b/sql/core/src/test/resources/sql-tests/results/try_element_at.sql.out
index c1c0980ea52..defac2ea5d6 100644
--- a/sql/core/src/test/resources/sql-tests/results/try_element_at.sql.out
+++ b/sql/core/src/test/resources/sql-tests/results/try_element_at.sql.out
@@ -4,8 +4,8 @@ SELECT try_element_at(array(1, 2, 3), 0)
 -- !query schema
 struct<>
 -- !query output
-java.lang.ArrayIndexOutOfBoundsException
-SQL array indices start at 1
+org.apache.spark.SparkRuntimeException
+[ELEMENT_AT_BY_INDEX_ZERO] The index 0 is invalid. An index shall be either < 0 or > 0 (the first element has index 1).
 
 
 -- !query
diff --git a/sql/core/src/test/scala/org/apache/spark/sql/errors/QueryExecutionErrorsSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/errors/QueryExecutionErrorsSuite.scala
index ff269271ebc..8bf90d43565 100644
--- a/sql/core/src/test/scala/org/apache/spark/sql/errors/QueryExecutionErrorsSuite.scala
+++ b/sql/core/src/test/scala/org/apache/spark/sql/errors/QueryExecutionErrorsSuite.scala
@@ -639,6 +639,16 @@ class QueryExecutionErrorsSuite
       sqlState = None,
       matchPVals = true)
   }
+
+  test("ELEMENT_AT_BY_INDEX_ZERO: element_at from array by index zero") {
+    checkError(
+      exception = intercept[SparkRuntimeException](
+        sql("select element_at(array(1, 2, 3, 4, 5), 0)").collect()
+      ),
+      errorClass = "ELEMENT_AT_BY_INDEX_ZERO",
+      Map.empty
+    )
+  }
 }
 
 class FakeFileSystemSetPermission extends LocalFileSystem {


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org