peter-toth commented on code in PR #37525:
URL: https://github.com/apache/spark/pull/37525#discussion_r1070949153
##########
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:
> @peter-toth do you agree that it's better to let the caller decide if the
original expr should be included or not?
Well, yes, that would make the code of `multiTransform` much simpler. But
the construction of `exprAliasMap` would become the burden of the caller. (I.e.
the caller need to identify if the `from` expression in a `from` -> `to`
mapping contais any other `from` expression and put the `from -> to` and the
`from -> from` mappings to the separate `exprAliasMap` map.)
> It seems currently the framework always include the original expr.
That's right, currently the original expr is always included and the
traversal/transformation continues with its children. But if nothing is
transformed under the original expression then the original is not included in
the final result. Details here:
https://github.com/apache/spark/pull/38034#discussion_r1070234296
My initial idea behind of this "automatic traversal/transformation
continuation" was that the `rule` is a partial function which can define
complex mappings possibly with conditional logic, which a simple map can't do.
So I wasn't sure that we can expect to caller to write `case` statements
that need to return the original expression too if other `case` statements can
transform any of the original expressions descendants...
Although, this is not an issue in our case as we define the `rule` based on
simple alias maps.
If we want to keep that "automatic traversal/transformation continuation"
logic then we could define the `rule` parameter as `PartialFunction[BaseType,
(Seq[BaseType], Continuation)]` (or maybe as `PartialFunction[BaseType,
Seq[(BaseType, Continuation)]]`) to let the caller to choose the from 3
different`Continuation` enum elements like `ALWAYS`, `AUTO` or `NEVER`.
So we could either define our multitransform:
```
input.multiTransform {
case e if aliasMap.contains(e) => exprAliasMap(e) -> AUTO
}
```
or
```
input.multiTransform {
case e if exprAliasMap.contains(e) => exprAliasMap(e) -> ALWAYS
case e if attrAliasMap.contains(e) => attrAliasMap(e) -> NEVER
}
```
or
```
input.multiTransform {
case e if exprAliasMap.contains(e) => (e +: exprAliasMap(e)) -> NEVER
case e if attrAliasMap.contains(e) => attrAliasMap(e) -> NEVER
}
```
Seems like we need only `AUTO` and `NEVER` so probably we just need a
boolean flag instead of the enum...
Anyways, I hope you agree that `multiTransform` is useful and efficient
helper function but I'm not sure how general purpose swiss army knife we want
to make it.
--
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]