This is an automated email from the ASF dual-hosted git repository. wenchen 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 a2448a4 [SPARK-38304][SQL] Elt() should return null if index is null under ANSI mode a2448a4 is described below commit a2448a4068d255fac951be2dcf36db08145533e7 Author: Gengliang Wang <gengli...@apache.org> AuthorDate: Wed Feb 23 23:19:56 2022 +0800 [SPARK-38304][SQL] Elt() should return null if index is null under ANSI mode ### What changes were proposed in this pull request? Elt() should return null if the input index is null under ANSI mode, which is consistent with MySQL where the function is from. Before changes: <img width="824" alt="image" src="https://user-images.githubusercontent.com/1097932/155308033-2e47b49a-b98b-4fd6-b1f1-d89762452fba.png"> After changes: The query returns null. ### Why are the changes needed? Bug fix ### Does this PR introduce _any_ user-facing change? Yes, SQL function Elt() returns null if the input index is null under ANSI mode, instead of runtime error. ### How was this patch tested? UT Closes #35629 from gengliangwang/fixEltErrorMsg. Authored-by: Gengliang Wang <gengli...@apache.org> Signed-off-by: Wenchen Fan <wenc...@databricks.com> --- .../catalyst/expressions/stringExpressions.scala | 22 ++++++++------ .../src/test/resources/sql-tests/inputs/array.sql | 4 +++ .../resources/sql-tests/results/ansi/array.sql.out | 34 +++++++++++++++++++++- .../test/resources/sql-tests/results/array.sql.out | 34 +++++++++++++++++++++- 4 files changed, 83 insertions(+), 11 deletions(-) diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringExpressions.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringExpressions.scala index 021ddbe..b9670646 100755 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringExpressions.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringExpressions.scala @@ -362,15 +362,19 @@ case class Elt( ev.copy( code""" |${index.code} - |final int $indexVal = ${index.value}; - |${CodeGenerator.JAVA_BOOLEAN} $indexMatched = false; - |$inputVal = null; - |do { - | $codes - |} while (false); - |$indexOutOfBoundBranch - |final ${CodeGenerator.javaType(dataType)} ${ev.value} = $inputVal; - |final boolean ${ev.isNull} = ${ev.value} == null; + |boolean ${ev.isNull} = ${index.isNull}; + |${CodeGenerator.javaType(dataType)} ${ev.value} = null; + |if (!${index.isNull}) { + | final int $indexVal = ${index.value}; + | ${CodeGenerator.JAVA_BOOLEAN} $indexMatched = false; + | $inputVal = null; + | do { + | $codes + | } while (false); + | $indexOutOfBoundBranch + | ${ev.value} = $inputVal; + | ${ev.isNull} = ${ev.value} == null; + |} """.stripMargin) } diff --git a/sql/core/src/test/resources/sql-tests/inputs/array.sql b/sql/core/src/test/resources/sql-tests/inputs/array.sql index f73b653..0223ce5 100644 --- a/sql/core/src/test/resources/sql-tests/inputs/array.sql +++ b/sql/core/src/test/resources/sql-tests/inputs/array.sql @@ -99,6 +99,10 @@ select element_at(array(1, 2, 3), 0); select elt(4, '123', '456'); select elt(0, '123', '456'); select elt(-1, '123', '456'); +select elt(null, '123', '456'); +select elt(null, '123', null); +select elt(1, '123', null); +select elt(2, '123', null); select array(1, 2, 3)[5]; select array(1, 2, 3)[-1]; 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 b412493..f2b3552 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 @@ -1,5 +1,5 @@ -- Automatically generated by SQLQueryTestSuite --- Number of queries: 29 +-- Number of queries: 33 -- !query @@ -217,6 +217,38 @@ Invalid index: -1, numElements: 2. If necessary set spark.sql.ansi.enabled to fa -- !query +select elt(null, '123', '456') +-- !query schema +struct<elt(NULL, 123, 456):string> +-- !query output +NULL + + +-- !query +select elt(null, '123', null) +-- !query schema +struct<elt(NULL, 123, NULL):string> +-- !query output +NULL + + +-- !query +select elt(1, '123', null) +-- !query schema +struct<elt(1, 123, NULL):string> +-- !query output +123 + + +-- !query +select elt(2, '123', null) +-- !query schema +struct<elt(2, 123, NULL):string> +-- !query output +NULL + + +-- !query select array(1, 2, 3)[5] -- !query schema struct<> 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 76fdf03..9d42b8a 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 @@ -1,5 +1,5 @@ -- Automatically generated by SQLQueryTestSuite --- Number of queries: 20 +-- Number of queries: 24 -- !query @@ -212,6 +212,38 @@ NULL -- !query +select elt(null, '123', '456') +-- !query schema +struct<elt(NULL, 123, 456):string> +-- !query output +NULL + + +-- !query +select elt(null, '123', null) +-- !query schema +struct<elt(NULL, 123, NULL):string> +-- !query output +NULL + + +-- !query +select elt(1, '123', null) +-- !query schema +struct<elt(1, 123, NULL):string> +-- !query output +123 + + +-- !query +select elt(2, '123', null) +-- !query schema +struct<elt(2, 123, NULL):string> +-- !query output +NULL + + +-- !query select array(1, 2, 3)[5] -- !query schema struct<array(1, 2, 3)[5]:int> --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@spark.apache.org For additional commands, e-mail: commits-h...@spark.apache.org