cloud-fan commented on a change in pull request #32301:
URL: https://github.com/apache/spark/pull/32301#discussion_r620122510
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/NestedColumnAliasing.scala
##########
@@ -133,83 +204,77 @@ object NestedColumnAliasing {
}
/**
- * Return root references that are individually accessed as a whole, and
`GetStructField`s
- * or `GetArrayStructField`s which on top of other `ExtractValue`s or
special expressions.
- * Check `SelectedField` to see which expressions should be listed here.
+ * Check [[SelectedField]] to see which expressions should be listed here.
*/
- private def collectRootReferenceAndExtractValue(e: Expression):
Seq[Expression] = e match {
- case _: AttributeReference => Seq(e)
- case GetStructField(_: ExtractValue | _: AttributeReference, _, _) =>
Seq(e)
+ private def isSelectedField(e: Expression): Boolean = e match {
+ case GetStructField(_: ExtractValue | _: AttributeReference, _, _) => true
case GetArrayStructFields(_: MapValues |
_: MapKeys |
_: ExtractValue |
- _: AttributeReference, _, _, _, _) => Seq(e)
- case es if es.children.nonEmpty =>
es.children.flatMap(collectRootReferenceAndExtractValue)
+ _: AttributeReference, _, _, _, _) => true
+ case _ => false
+ }
+
+ /**
+ * Return root references that are individually accessed.
+ */
+ private def collectAttributeReference(e: Expression):
Seq[AttributeReference] = e match {
+ case a: AttributeReference => Seq(a)
+ case g if isSelectedField(g) => Seq.empty
+ case es if es.children.nonEmpty =>
es.children.flatMap(collectAttributeReference)
case _ => Seq.empty
}
/**
- * 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.
+ * Return [[GetStructField]] or [[GetArrayStructFields]] on top of other
[[ExtractValue]]s
+ * or special expressions.
*/
- 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
- }
+ private def collectExtractValue(e: Expression): Seq[ExtractValue] = e match {
+ case g if isSelectedField(g) => Seq(g.asInstanceOf[ExtractValue])
+ case es if es.children.nonEmpty => es.children.flatMap(collectExtractValue)
+ case _ => Seq.empty
+ }
+
+ /**
+ * 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 getAttributeToExtractValues(
+ exprList: Seq[Expression],
+ exclusiveAttrs: Seq[Attribute]): Map[Attribute, Seq[ExtractValue]] = {
- // Note that when we group by extractors with their references, we should
remove
- // cosmetic variations.
+ val nestedFieldReferences = exprList.flatMap(collectExtractValue)
+ val otherRootReferences = exprList.flatMap(collectAttributeReference)
Review comment:
Previously we collected both the nested fields extraction and other root
references at the same time, and split them later. Now we collect them
separately. I think the current code is clearer but is less performant.
How about we use mutable collections to implement this logic with one tree
traversal?
```
val nestedFieldReferences = mutable.ArrayBuffer[ExtractValue]
val otherRootReferences = mutable.ArrayBuffer[AttributeReference]
exprList.foreach(collectRootReferenceAndExtractValue(e,
nestedFieldReferences, otherRootReferences))
```
--
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:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]