Github user cloud-fan commented on a diff in the pull request:
https://github.com/apache/spark/pull/22745#discussion_r227355778
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/objects/objects.scala
---
@@ -1787,3 +1787,75 @@ case class ValidateExternalType(child: Expression,
expected: DataType)
ev.copy(code = code, isNull = input.isNull)
}
}
+
+object GetArrayFromMap {
+ abstract class Source
+ case class Key() extends Source
+ case class Value() extends Source
+}
+
+/**
+ * Extracts a key/value array from a Map expression.
+ *
+ * @param child a Map expression to extract array from
+ * @param source source of array elements, can be `Key` or `Value`
+ */
+case class GetArrayFromMap(
+ child: Expression,
+ source: GetArrayFromMap.Source) extends Expression with
NonSQLExpression {
+
+ import GetArrayFromMap._
+
+ private val functionName: String = source match {
+ case Key() => "keyArray"
+ case Value() => "valueArray"
+ }
+
+ private lazy val encodedFunctionName =
TermName(functionName).encodedName.toString
+
+ override def nullable: Boolean = child.nullable
+ override def children: Seq[Expression] = child :: Nil
+
+ lazy val dataType: DataType = {
+ child.dataType match {
+ case MapType(kt, vt, _) =>
+ source match {
+ case Key() => ArrayType(kt)
+ case Value() => ArrayType(vt)
+ }
+ case other =>
+ throw new RuntimeException(
+ s"Can't extract array from $child: need map type but got
${other.catalogString}")
+ }
+ }
+
+ override def eval(input: InternalRow): Any = {
+ val mapObj = child.eval(input)
+ if (!mapObj.isInstanceOf[MapData]) {
--- End diff --
we don't need this check. `eval` is performance critical and we should
assume there is no bug. We don't have this check in other expressions either.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]