cloud-fan commented on code in PR #37525:
URL: https://github.com/apache/spark/pull/37525#discussion_r1070970038
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/AliasAwareOutputExpression.scala:
##########
@@ -16,24 +16,53 @@
*/
package org.apache.spark.sql.execution
-import org.apache.spark.sql.catalyst.expressions.{Alias, Expression,
NamedExpression, SortOrder}
-import org.apache.spark.sql.catalyst.plans.physical.{HashPartitioning,
Partitioning, PartitioningCollection, UnknownPartitioning}
+import scala.collection.mutable
+
+import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute,
Expression, NamedExpression, SortOrder}
+import org.apache.spark.sql.catalyst.plans.physical.{Partitioning,
PartitioningCollection, UnknownPartitioning}
+import org.apache.spark.sql.internal.SQLConf
/**
* A trait that provides functionality to handle aliases in the
`outputExpressions`.
*/
trait AliasAwareOutputExpression extends UnaryExecNode {
protected def outputExpressions: Seq[NamedExpression]
- private lazy val aliasMap = outputExpressions.collect {
- case a @ Alias(child, _) => child.canonicalized -> a.toAttribute
- }.toMap
+ private lazy val aliasMap = {
+ val aliases = mutable.Map[Expression, mutable.ListBuffer[Attribute]]()
+ // Add aliases to the map. If multiple alias is defined for a source
attribute then add all.
+ outputExpressions.foreach {
+ case a @ Alias(child, _) =>
+ // This prepend is needed to make the first element of the
`ListBuffer` point to the last
+ // occurrence of an aliased child. This is to keep the previous
behavior:
+ // - when we return `Partitioning`s in `outputPartitioning()`, the
first should be same as
+ // it was previously
+ // - when we return a `SortOrder` in `outputOrdering()`, it should be
should be same as
+ // previously
+ a.toAttribute +=: aliases.getOrElseUpdate(child.canonicalized,
mutable.ListBuffer.empty)
+ case _ =>
+ }
+ // Append identity mapping of an attribute to the map if both the
attribute and its aliased
+ // version can be found in `outputExpressions`.
+ outputExpressions.foreach {
+ case a: Attribute if aliases.contains(a.canonicalized) =>
aliases(a.canonicalized) += a
+ case _ =>
+ }
+ aliases
+ }
protected def hasAlias: Boolean = aliasMap.nonEmpty
- protected def normalizeExpression(exp: Expression): Expression = {
+ // This function returns a limited number of alternatives to mapped `exp`.
+ protected def normalizeExpression(exp: Expression): Seq[Expression] = {
+ exp.multiTransform {
+ case e: Expression if aliasMap.contains(e.canonicalized) =>
aliasMap(e.canonicalized).toSeq
Review Comment:
I think `multiTransform` is very powerful and we should let the caller side
have more control. I like the blow more to keep the API simple
```
input.multiTransform {
case e if exprAliasMap.contains(e) => (e +: exprAliasMap(e)) -> NEVER
case e if attrAliasMap.contains(e) => attrAliasMap(e) -> NEVER
}
```
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]