Github user jaceklaskowski commented on a diff in the pull request: https://github.com/apache/spark/pull/14444#discussion_r73336967 --- Diff: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala --- @@ -2065,18 +2065,28 @@ object EliminateUnions extends Rule[LogicalPlan] { */ object CleanupAliases extends Rule[LogicalPlan] { private def trimAliases(e: Expression): Expression = { - var stop = false e.transformDown { - // CreateStruct is a special case, we need to retain its top level Aliases as they decide the - // name of StructField. We also need to stop transform down this expression, or the Aliases - // under CreateStruct will be mistakenly trimmed. - case c: CreateStruct if !stop => - stop = true - c.copy(children = c.children.map(trimNonTopLevelAliases)) - case c: CreateStructUnsafe if !stop => - stop = true - c.copy(children = c.children.map(trimNonTopLevelAliases)) - case Alias(child, _) if !stop => child + /** + * [[CreateStruct]] is a special case as it uses its top level Aliases to decide the + * name of StructField. + * we tackle this by replacing [[CreateStruct]] with [[CreateNamedStruct]] + * which encodes the field names as child literals. + */ + case c @ CreateStruct(children) => + CreateNamedStruct( mkNamedStructArgs(c.dataType, children)) + case c @ CreateStructUnsafe(children) => + CreateNamedStructUnsafe( mkNamedStructArgs(c.dataType, children)) + case Alias(child, _) => child + } + } + + private def mkNamedStructArgs( structType : StructType, attributeExpressions: Seq[Expression]) = { + for { + (name, expression) <- structType.fieldNames.zip(attributeExpressions) + nameLiteral = Literal(name) + newChild <- Seq(nameLiteral, expression) + } yield{ + newChild --- End diff -- Do we really need `{` around `newChild`?
--- 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