Github user cloud-fan commented on a diff in the pull request: https://github.com/apache/spark/pull/2405#discussion_r17583870 --- Diff: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/complexTypes.scala --- @@ -68,36 +72,96 @@ case class GetItem(child: Expression, ordinal: Expression) extends Expression { } } } + + def containsNullList = child match { + case n: NestedArrayRecorder if n.isValid => n.containsNullList.drop(1) + case _ => Nil + } + + def innerDataType = child match { + case n: NestedArrayRecorder if n.isValid => n.innerDataType + case _ => dataType + } } /** - * Returns the value of fields in the Struct `child`. + * Returns the value of fields in the `child`. + * The type of `child` can be struct, or array of struct, + * or array of array of struct, or array of array ... of struct. */ -case class GetField(child: Expression, fieldName: String) extends UnaryExpression { +case class GetField(child: Expression, fieldName: String) extends UnaryExpression + with NestedArrayRecorder { + type EvaluatedType = Any - def dataType = field.dataType + def dataType = buildDataType(containsNullList, field.dataType) + + private def buildDataType(l: Seq[Boolean], t: DataType): DataType = { + if (l == Nil) { + t + } else { + ArrayType(buildDataType(l.tail, t), l.head) + } + } + override def nullable = child.nullable || field.nullable override def foldable = child.foldable - protected def structType = child.dataType match { + private val _containsNullList = ArrayBuffer.empty[Boolean] + def containsNullList = { + structType + _containsNullList.toSeq + } + + def innerDataType = field.dataType + + private def getStructType(t: DataType): StructType = t match { + case ArrayType(elementType, containsNull) => + _containsNullList += containsNull + getStructType(elementType) case s: StructType => s case otherType => sys.error(s"GetField is not valid on fields of type $otherType") } + protected lazy val structType: StructType = child match { + case n: NestedArrayRecorder if n.isValid => + _containsNullList ++= n.containsNullList + getStructType(n.innerDataType) + case _ => getStructType(child.dataType) + } + lazy val field = structType.fields - .find(_.name == fieldName) - .getOrElse(sys.error(s"No such field $fieldName in ${child.dataType}")) + .find(_.name == fieldName) + .getOrElse(sys.error(s"No such field $fieldName in ${child.dataType}")) lazy val ordinal = structType.fields.indexOf(field) - override lazy val resolved = childrenResolved && child.dataType.isInstanceOf[StructType] + override lazy val resolved = childrenResolved --- End diff -- Do we still need type check here? Something like `child.dataType.isInstanceOf[StructType] || child.dataType.isInstanceOf[ArrayType]`?
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org