Skip to content

Commit 6ff7c7d

Browse files
committed
Check ascendingOrder type in sort_array function rather than throwing ClassCastException
1 parent f39852e commit 6ff7c7d

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,13 @@ case class SortArray(base: Expression, ascendingOrder: Expression)
124124

125125
override def checkInputDataTypes(): TypeCheckResult = base.dataType match {
126126
case ArrayType(dt, _) if RowOrdering.isOrderable(dt) =>
127-
TypeCheckResult.TypeCheckSuccess
127+
ascendingOrder match {
128+
case Literal(_: Boolean, BooleanType) =>
129+
TypeCheckResult.TypeCheckSuccess
130+
case _ =>
131+
TypeCheckResult.TypeCheckFailure(
132+
"Sort order in second argument requires a boolean literal.")
133+
}
128134
case ArrayType(dt, _) =>
129135
TypeCheckResult.TypeCheckFailure(
130136
s"$prettyName does not support sorting array of type ${dt.simpleString}")

sql/core/src/test/resources/sql-tests/inputs/array.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ select
7171
sort_array(timestamp_array)
7272
from primitive_arrays;
7373

74+
-- sort_array with an invalid string literal for the argument of sort order.
75+
select sort_array(array('b', 'd'), '1');
76+
77+
-- sort_array with an invalid null literal casted as boolean for the argument of sort order.
78+
select sort_array(array('b', 'd'), cast(NULL as boolean));
79+
7480
-- size
7581
select
7682
size(boolean_array),

sql/core/src/test/resources/sql-tests/results/array.sql.out

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,23 @@ struct<sort_array(boolean_array, true):array<boolean>,sort_array(tinyint_array,
124124
-- !query 8 output
125125
[true] [1,2] [1,2] [1,2] [1,2] [9223372036854775808,9223372036854775809] [1.0,2.0] [1.0,2.0] [2016-03-13,2016-03-14] [2016-11-12 20:54:00.0,2016-11-15 20:54:00.0]
126126

127-
128127
-- !query 9
128+
select sort_array(array('b', 'd'), '1')
129+
-- !query 9 schema
130+
struct<>
131+
-- !query 9 output
132+
org.apache.spark.sql.AnalysisException
133+
cannot resolve 'sort_array(array('b', 'd'), '1')' due to data type mismatch: Sort order in second argument requires a boolean literal.; line 1 pos 7
134+
135+
-- !query 10
136+
select sort_array(array('b', 'd'), cast(NULL as boolean))
137+
-- !query 10 schema
138+
struct<>
139+
-- !query 10 output
140+
org.apache.spark.sql.AnalysisException
141+
cannot resolve 'sort_array(array('b', 'd'), CAST(NULL AS BOOLEAN))' due to data type mismatch: Sort order in second argument requires a boolean literal.; line 1 pos 7
142+
143+
-- !query 11
129144
select
130145
size(boolean_array),
131146
size(tinyint_array),
@@ -138,7 +153,7 @@ select
138153
size(date_array),
139154
size(timestamp_array)
140155
from primitive_arrays
141-
-- !query 9 schema
156+
-- !query 11 schema
142157
struct<size(boolean_array):int,size(tinyint_array):int,size(smallint_array):int,size(int_array):int,size(bigint_array):int,size(decimal_array):int,size(double_array):int,size(float_array):int,size(date_array):int,size(timestamp_array):int>
143-
-- !query 9 output
158+
-- !query 11 output
144159
1 2 2 2 2 2 2 2 2 2

0 commit comments

Comments
 (0)