Skip to content

Commit 80fc0f8

Browse files
Add type checking
1 parent a42b678 commit 80fc0f8

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.spark.sql.catalyst.expressions
1818

19+
import org.apache.spark.sql.catalyst.analysis.TypeCheckResult
1920
import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenFallback, CodeGenContext, GeneratedExpressionCode}
2021
import org.apache.spark.sql.types._
2122

@@ -50,13 +51,16 @@ case class SortArray(child: Expression)
5051
override def dataType: DataType = child.dataType
5152
override def inputTypes: Seq[AbstractDataType] = Seq(ArrayType)
5253

54+
override def checkInputDataTypes(): TypeCheckResult = child.dataType match {
55+
case _ @ ArrayType(n: AtomicType, _) => TypeCheckResult.TypeCheckSuccess
56+
case other => TypeCheckResult.TypeCheckFailure(
57+
s"Type $other is not supported for ordering operations")
58+
}
59+
5360
@transient
5461
private lazy val lt: (Any, Any) => Boolean = {
5562
val ordering = child.dataType match {
56-
case ArrayType(elementType, _) => elementType match {
57-
case n: AtomicType => n.ordering.asInstanceOf[Ordering[Any]]
58-
case other => sys.error(s"Type $other does not support ordered operations")
59-
}
63+
case _ @ ArrayType(n: AtomicType, _) => n.ordering.asInstanceOf[Ordering[Any]]
6064
}
6165

6266
(left, right) => {

sql/core/src/test/scala/org/apache/spark/sql/DataFrameFunctionsSuite.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,12 @@ class DataFrameFunctionsSuite extends QueryTest {
287287
Row(Seq[Int](), Seq[String]()),
288288
Row(null, null))
289289
)
290+
291+
val df2 = Seq((Array[Array[Int]](Array(2)), "x")).toDF("a", "b")
292+
assert(intercept[AnalysisException] {
293+
df2.selectExpr("sort_array(a)").collect()
294+
}.getMessage().contains("Type ArrayType(ArrayType(IntegerType,false),true) " +
295+
"is not supported for ordering operations"))
290296
}
291297

292298
test("array size function") {

0 commit comments

Comments
 (0)