cloud-fan commented on a change in pull request #32301: URL: https://github.com/apache/spark/pull/32301#discussion_r638924260
########## File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/NestedColumnAliasing.scala ########## @@ -149,67 +234,52 @@ object NestedColumnAliasing { } /** - * Return two maps in order to replace nested fields to aliases. - * - * If `exclusiveAttrs` is given, any nested field accessors of these attributes - * won't be considered in nested fields aliasing. - * - * 1. ExtractValue -> Alias: A new alias is created for each nested field. - * 2. ExprId -> Seq[Alias]: A reference attribute has multiple aliases pointing it. + * Creates a map from root [[Attribute]]s to non-redundant nested [[ExtractValue]]s. + * Nested field accessors of `exclusiveAttrs` are not considered in nested fields aliasing. */ - def getAliasSubMap(exprList: Seq[Expression], exclusiveAttrs: Seq[Attribute] = Seq.empty) - : Option[(Map[ExtractValue, Alias], Map[ExprId, Seq[Alias]])] = { - val (nestedFieldReferences, otherRootReferences) = - exprList.flatMap(collectRootReferenceAndExtractValue).partition { - case _: ExtractValue => true - case _ => false + def getAttributeToExtractValues( + exprList: Seq[Expression], + exclusiveAttrs: Seq[Attribute]): Map[Attribute, Seq[ExtractValue]] = { + + val nestedFieldReferences = new mutable.ArrayBuffer[ExtractValue]() + val otherRootReferences = new mutable.ArrayBuffer[AttributeReference]() + exprList.foreach { e => + collectRootReferenceAndExtractValue(e).foreach { + case ev: ExtractValue => nestedFieldReferences.append(ev) + case ar: AttributeReference => otherRootReferences.append(ar) } - - // Note that when we group by extractors with their references, we should remove - // cosmetic variations. + } val exclusiveAttrSet = AttributeSet(exclusiveAttrs ++ otherRootReferences) - val aliasSub = nestedFieldReferences.asInstanceOf[Seq[ExtractValue]] + + // Remove cosmetic variations when we group extractors by their references + nestedFieldReferences .filter(!_.references.subsetOf(exclusiveAttrSet)) .groupBy(_.references.head.canonicalized.asInstanceOf[Attribute]) - .flatMap { case (attr, nestedFields: Seq[ExtractValue]) => - // Remove redundant `ExtractValue`s if they share the same parent nest field. + .flatMap { case (attr: Attribute, nestedFields: Seq[ExtractValue]) => + // Remove redundant [[ExtractValue]]s if they share the same parent nest field. // For example, when `a.b` and `a.b.c` are in project list, we only need to alias `a.b`. - // We only need to deal with two `ExtractValue`: `GetArrayStructFields` and - // `GetStructField`. Please refer to the method `collectRootReferenceAndExtractValue`. + // Because `a.b` requires all of the inner fields of `b`, we cannot prune `a.b.c`. val dedupNestedFields = nestedFields.filter { + // See [[collectExtractValue]]: we only need to deal with [[GetArrayStructFields]] and + // [[GetStructField]] case e @ (_: GetStructField | _: GetArrayStructFields) => val child = e.children.head nestedFields.forall(f => child.find(_.semanticEquals(f)).isEmpty) case _ => true - } - - // Each expression can contain multiple nested fields. - // Note that we keep the original names to deliver to parquet in a case-sensitive way. - val nestedFieldToAlias = dedupNestedFields.distinct.map { f => - val exprId = NamedExpression.newExprId - (f, Alias(f, s"_gen_alias_${exprId.id}")(exprId, Seq.empty, None)) - } + }.distinct Review comment: I'm still a bit worried about calling distinct on uncanonicalized expressions. how about ``` .groupBy(_.canonicalized).values.map(_.head).toSeq ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org