Github user hvanhovell commented on a diff in the pull request:
https://github.com/apache/spark/pull/14444#discussion_r76662951
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/complexTypeCreator.scala
---
@@ -172,60 +172,47 @@ case class CreateMap(children: Seq[Expression])
extends Expression {
}
/**
- * Returns a Row containing the evaluation of all children expressions.
- */
-@ExpressionDescription(
- usage = "_FUNC_(col1, col2, col3, ...) - Creates a struct with the given
field values.")
-case class CreateStruct(children: Seq[Expression]) extends Expression {
-
- override def foldable: Boolean = children.forall(_.foldable)
-
- override lazy val dataType: StructType = {
- val fields = children.zipWithIndex.map { case (child, idx) =>
+* a helper mixin to encapsulate [[CreateStruct]] => [[CreateNamedStruct]]
transformation.
+*/
+sealed trait CreateStructLikeFactory[T <: Expression] extends
(Seq[Expression] => T) {
+ protected case class CretaeStructHelper( children : Seq[Expression] ) {
+ private def attributeNames: Seq[String] = for {
+ (child, idx) <- children.zipWithIndex
+ } yield {
child match {
- case ne: NamedExpression =>
- StructField(ne.name, ne.dataType, ne.nullable, ne.metadata)
- case _ =>
- StructField(s"col${idx + 1}", child.dataType, child.nullable,
Metadata.empty)
+ case ne: NamedExpression => ne.name
+ case _ => s"col${idx + 1}"
}
}
- StructType(fields)
- }
-
- override def nullable: Boolean = false
-
- override def eval(input: InternalRow): Any = {
- InternalRow(children.map(_.eval(input)): _*)
- }
- override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
- val rowClass = classOf[GenericInternalRow].getName
- val values = ctx.freshName("values")
- ctx.addMutableState("Object[]", values, s"this.$values = null;")
+ private def mkNamedStructArgs: Seq[Expression] = {
+ for {
+ (name, expression) <- attributeNames.zip(children)
+ nameLiteral = Literal(name)
+ newChild <- Seq(nameLiteral, expression)
+ } yield{
+ newChild
+ }
+ }
- ev.copy(code = s"""
- boolean ${ev.isNull} = false;
- this.$values = new Object[${children.size}];""" +
- ctx.splitExpressions(
- ctx.INPUT_ROW,
- children.zipWithIndex.map { case (e, i) =>
- val eval = e.genCode(ctx)
- eval.code + s"""
- if (${eval.isNull}) {
- $values[$i] = null;
- } else {
- $values[$i] = ${eval.value};
- }"""
- }) +
- s"""
- final InternalRow ${ev.value} = new $rowClass($values);
- this.$values = null;
- """)
+ def safe: CreateNamedStruct = CreateNamedStruct(mkNamedStructArgs)
+ def unsafe: CreateNamedStructUnsafe =
CreateNamedStructUnsafe(mkNamedStructArgs)
}
-
- override def prettyName: String = "struct"
}
+/**
+ * Returns a Row containing the evaluation of all children expressions.
+ */
+object CreateStruct extends CreateStructLikeFactory[CreateNamedStruct]{
--- End diff --
We could add the entire constructor for the function registry here.
---
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 [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]